Skip to content

Commit d45636f

Browse files
committed
Sometime a little break makes the trick. Encryption works.
1 parent 068c1e8 commit d45636f

File tree

5 files changed

+67
-35
lines changed

5 files changed

+67
-35
lines changed

src/net/sharksystem/asap/protocol/ASAP_Modem_Impl.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ public void interest(CharSequence sender, CharSequence recipient, CharSequence f
7878
os, signed, encrypted, recipient,
7979
this.signAndEncryptionKeyStorage);
8080

81-
// debugging
82-
this.c = cryptoSession;
83-
8481
cryptoSession.sendCmd();
8582

8683
InterestPDU_Impl.sendPDUWithoutCmd(sender, recipient, format, channel, eraFrom, eraTo,
@@ -110,16 +107,6 @@ public void assimilate(CharSequence sender, CharSequence recipient, CharSequence
110107
new ByteArrayInputStream(data), os, signed);
111108
}
112109

113-
// debug
114-
private CryptoSession c = null;
115-
private CryptoSession getSameCryptoSession() {
116-
if(c == null) {
117-
this.c = new CryptoSession(this.signAndEncryptionKeyStorage);
118-
}
119-
120-
return this.c;
121-
}
122-
123110
@Override
124111
public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
125112
byte cmd = PDU_Impl.readByte(is);
@@ -130,9 +117,7 @@ public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
130117
cmd = (byte)(cmd & CMD_MASK);
131118

132119
if(encrypted) {
133-
// CryptoSession cryptoSession = new CryptoSession(this.signAndEncryptionKeyStorage);
134-
// debugging
135-
CryptoSession cryptoSession = this.getSameCryptoSession();
120+
CryptoSession cryptoSession = new CryptoSession(this.signAndEncryptionKeyStorage);
136121
InputStream decryptedIS = cryptoSession.decrypt(is);
137122
is = decryptedIS;
138123
}

src/net/sharksystem/asap/protocol/CryptoSession.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class CryptoSession {
2525
private OutputStream realOS;
2626
private ByteArrayOutputStream asapMessageOS;
2727
private byte[] asapMessageAsBytes;
28-
private ByteArrayOutputStream senderSiteTest;
29-
private byte[] byte2Send;
3028

3129
CryptoSession(ASAPReadonlyKeyStorage keyStorage) {
3230
this.keyStorage = keyStorage;
@@ -48,8 +46,7 @@ private InputStream decrypt(InputStream is, PrivateKey privateKey) throws ASAPSe
4846
// read encrypted bytes from stream
4947
is.read(messageBytes);
5048

51-
Utils.compareArrays(messageBytes, this.byte2Send);
52-
49+
// debugging
5350
// decrypt
5451
byte[] decryptedBytes = this.cipher.doFinal(messageBytes);
5552
return new ByteArrayInputStream(decryptedBytes);
@@ -159,28 +156,32 @@ OutputStream getOutputStream() {
159156

160157
public void finish() throws ASAPSecurityException {
161158
if(cipher != null) {
162-
// we are to encrypt
159+
// that is our asap message in clear text
163160
this.asapMessageAsBytes = this.asapMessageOS.toByteArray();
164161
try {
162+
// encrypted asap message
165163
byte[] encryptedBytes = this.cipher.doFinal(this.asapMessageAsBytes);
166-
// write data len
164+
//this.debuggingRememberEncryptedASAPMessage = encryptedBytes;
167165

168166
// write len
169167
PDU_Impl.sendNonNegativeIntegerParameter(encryptedBytes.length, this.realOS);
170168
// write data
171169
this.realOS.write(encryptedBytes);
172170

173-
// debug - decrypt
171+
/*
172+
// debugging - decrypt
174173
175174
// make a test
176-
this.senderSiteTest = new ByteArrayOutputStream();
175+
ByteArrayOutputStream senderSiteTest = new ByteArrayOutputStream();
177176
PDU_Impl.sendNonNegativeIntegerParameter(encryptedBytes.length, senderSiteTest);
178177
senderSiteTest.write(encryptedBytes);
178+
179+
this.debuggingEncryptedASAPMessageWithLen = senderSiteTest.toByteArray();
180+
179181
PrivateKey privateKey = this.keyStorage.getPrivateKey(this.recipient);
180-
this.byte2Send = encryptedBytes;
181-
ByteArrayInputStream decrypt = (ByteArrayInputStream) this.decrypt(new ByteArrayInputStream(senderSiteTest.toByteArray()), privateKey);
182+
// ByteArrayInputStream decrypt = (ByteArrayInputStream) this.decrypt(new ByteArrayInputStream(senderSiteTest.toByteArray()), privateKey);
183+
ByteArrayInputStream decrypt = (ByteArrayInputStream) this.decrypt(new ByteArrayInputStream(this.debuggingEncryptedASAPMessageWithLen), privateKey);
182184
int i = 42;
183-
/*
184185
*/
185186
} catch (IllegalBlockSizeException | BadPaddingException | IOException e) {
186187
throw new ASAPSecurityException(this.getLogStart(), e);

test/net/sharksystem/asap/protocol/PDUTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.Test;
66

77
import java.io.*;
8+
import java.security.KeyPair;
89
import java.util.ArrayList;
910
import java.util.List;
1011

@@ -111,6 +112,40 @@ public void sendAndReceiveInterest2() throws IOException, ASAPException {
111112
Assert.assertFalse(interestPDU.eraToSet());
112113
}
113114

115+
@Test
116+
public void sendAndReceiveInterestCanBeEncrypted() throws IOException, ASAPException {
117+
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage();
118+
119+
// add Bob
120+
KeyPair bobKeyPair = keyStorageAlice.createTestPeer(BOB_ID);
121+
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(bobKeyPair);
122+
123+
ASAP_1_0 asapModemAlice = new ASAP_Modem_Impl(keyStorageAlice);
124+
ASAP_1_0 asapModemBob = new ASAP_Modem_Impl(keyStorageBob);
125+
126+
String sender = ALICE_ID;
127+
String recipient = BOB_ID;
128+
String channel = "AliceURI";
129+
String format = "format";
130+
131+
ByteArrayOutputStream os = new ByteArrayOutputStream();
132+
133+
/////////////////////// encrypted
134+
asapModemAlice.interest(sender, recipient, format, channel, os,false, true);
135+
136+
// try t read output
137+
InputStream is = new ByteArrayInputStream(os.toByteArray());
138+
139+
ASAP_PDU_1_0 asap_pdu_1_0 = asapModemBob.readPDU(is);
140+
141+
ASAP_Interest_PDU_1_0 interestPDU = (ASAP_Interest_PDU_1_0) asap_pdu_1_0;
142+
143+
Assert.assertTrue(interestPDU.getChannelUri().equalsIgnoreCase(channel));
144+
Assert.assertTrue(interestPDU.getFormat().equalsIgnoreCase(format));
145+
Assert.assertTrue(interestPDU.getSender().equalsIgnoreCase(sender));
146+
Assert.assertTrue(interestPDU.getRecipient().equalsIgnoreCase(recipient));
147+
}
148+
114149
//////////////////// assimilate /////////////////////////////////////////
115150
@Test
116151
public void sendAndReceiveAssimilate() throws IOException, ASAPException {

test/net/sharksystem/asap/protocol/TestASAPKeyStorage.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public class TestASAPKeyStorage implements ASAPReadonlyKeyStorage {
2525
this.timeInMillis = System.currentTimeMillis();
2626
}
2727

28+
public TestASAPKeyStorage(KeyPair ownerKeyPair) {
29+
this.privateKey = ownerKeyPair.getPrivate();
30+
this.publicKey = ownerKeyPair.getPublic();
31+
}
32+
2833
private KeyPair generateKeyPair() throws ASAPSecurityException {
2934
Log.writeLog(this, "create key pair");
3035
KeyPairGenerator keyGen = null;
@@ -44,8 +49,10 @@ private KeyPair generateKeyPair() throws ASAPSecurityException {
4449
}
4550
}
4651

47-
public void createTestPeer(String id) throws ASAPSecurityException {
48-
this.peerKeyPairs.put(id, this.generateKeyPair());
52+
public KeyPair createTestPeer(String id) throws ASAPSecurityException {
53+
KeyPair keyPair = this.generateKeyPair();
54+
this.peerKeyPairs.put(id, keyPair);
55+
return keyPair;
4956
}
5057

5158
@Override

test/net/sharksystem/asap/protocol/EncryptionTests.java renamed to test/net/sharksystem/asap/protocol/Workbench.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88
import java.io.ByteArrayOutputStream;
99
import java.io.IOException;
1010
import java.io.InputStream;
11+
import java.security.KeyPair;
1112

12-
public class EncryptionTests {
13+
public class Workbench {
1314
public static final String ALICE_ID = "Alice";
1415
public static final String BOB_ID = "Bob";
1516

1617
@Test
1718
public void sendAndReceiveInterestCanBeEncrypted() throws IOException, ASAPException {
18-
TestASAPKeyStorage keyStorage = new TestASAPKeyStorage();
19+
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage();
20+
1921
// add Bob
20-
keyStorage.createTestPeer(BOB_ID);
22+
KeyPair bobKeyPair = keyStorageAlice.createTestPeer(BOB_ID);
23+
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(bobKeyPair);
2124

22-
ASAP_1_0 asapModem = new ASAP_Modem_Impl(keyStorage);
25+
ASAP_1_0 asapModemAlice = new ASAP_Modem_Impl(keyStorageAlice);
26+
ASAP_1_0 asapModemBob = new ASAP_Modem_Impl(keyStorageBob);
2327

2428
String sender = ALICE_ID;
2529
String recipient = BOB_ID;
@@ -29,12 +33,12 @@ public void sendAndReceiveInterestCanBeEncrypted() throws IOException, ASAPExcep
2933
ByteArrayOutputStream os = new ByteArrayOutputStream();
3034

3135
/////////////////////// encrypted
32-
asapModem.interest(sender, recipient, format, channel, os,false, true);
36+
asapModemAlice.interest(sender, recipient, format, channel, os,false, true);
3337

3438
// try t read output
3539
InputStream is = new ByteArrayInputStream(os.toByteArray());
3640

37-
ASAP_PDU_1_0 asap_pdu_1_0 = asapModem.readPDU(is);
41+
ASAP_PDU_1_0 asap_pdu_1_0 = asapModemBob.readPDU(is);
3842

3943
ASAP_Interest_PDU_1_0 interestPDU = (ASAP_Interest_PDU_1_0) asap_pdu_1_0;
4044

0 commit comments

Comments
 (0)