Skip to content

Commit 068c1e8

Browse files
committed
There is still a very weired bug in this encryption stuff and cannot figure out. It does not harm any other function anyway. Commit to keep this status and make a break.
1 parent 6cdb9be commit 068c1e8

File tree

7 files changed

+133
-45
lines changed

7 files changed

+133
-45
lines changed

src/net/sharksystem/Utils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,26 @@ public static Collection<Integer> getErasInRange(Collection<Integer> searchSpace
8484

8585
}
8686

87+
public static boolean compareArrays(byte[] a, byte[] b) {
88+
if(a.length != b.length) {
89+
System.out.println("not same length");
90+
return false;
91+
}
92+
93+
if(a.length == 0) {
94+
System.out.println("len zero");
95+
return false;
96+
}
97+
98+
for(int i = 0; i < a.length; i++) {
99+
System.out.println(i + ": " + a[i] + " == " + b[i]);
100+
if(a[i] != b[i]) {
101+
System.out.println("no longer same");
102+
return false;
103+
}
104+
}
105+
106+
return true;
107+
}
108+
87109
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public interface ASAPReadonlyKeyStorage {
1313
*/
1414
PrivateKey getPrivateKey() throws ASAPSecurityException;
1515

16+
// debugging
17+
PrivateKey getPrivateKey(CharSequence subjectID) throws ASAPSecurityException;
18+
1619
/**
1720
*
1821
* @param subjectID

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ 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+
8184
cryptoSession.sendCmd();
8285

8386
InterestPDU_Impl.sendPDUWithoutCmd(sender, recipient, format, channel, eraFrom, eraTo,
@@ -107,6 +110,16 @@ public void assimilate(CharSequence sender, CharSequence recipient, CharSequence
107110
new ByteArrayInputStream(data), os, signed);
108111
}
109112

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+
110123
@Override
111124
public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
112125
byte cmd = PDU_Impl.readByte(is);
@@ -117,6 +130,11 @@ public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
117130
cmd = (byte)(cmd & CMD_MASK);
118131

119132
if(encrypted) {
133+
// CryptoSession cryptoSession = new CryptoSession(this.signAndEncryptionKeyStorage);
134+
// debugging
135+
CryptoSession cryptoSession = this.getSameCryptoSession();
136+
InputStream decryptedIS = cryptoSession.decrypt(is);
137+
is = decryptedIS;
120138
}
121139

122140
int flagsInt = PDU_Impl.readByte(is);

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.sharksystem.asap.protocol;
22

3-
import jdk.internal.util.xml.impl.Input;
3+
import net.sharksystem.Utils;
4+
import net.sharksystem.asap.ASAPException;
45
import net.sharksystem.asap.ASAPSecurityException;
56

67
import javax.crypto.BadPaddingException;
@@ -10,9 +11,11 @@
1011
import java.io.*;
1112
import java.security.InvalidKeyException;
1213
import java.security.NoSuchAlgorithmException;
14+
import java.security.PrivateKey;
1315
import java.security.PublicKey;
1416

1517
class CryptoSession {
18+
private CharSequence recipient;
1619
private ASAPReadonlyKeyStorage keyStorage;
1720
private Cipher cipher = null;
1821
private PublicKey publicKey;
@@ -22,26 +25,36 @@ class CryptoSession {
2225
private OutputStream realOS;
2326
private ByteArrayOutputStream asapMessageOS;
2427
private byte[] asapMessageAsBytes;
28+
private ByteArrayOutputStream senderSiteTest;
29+
private byte[] byte2Send;
2530

2631
CryptoSession(ASAPReadonlyKeyStorage keyStorage) {
2732
this.keyStorage = keyStorage;
2833
}
2934

3035
InputStream decrypt(InputStream is) throws ASAPSecurityException {
36+
return this.decrypt(is, this.keyStorage.getPrivateKey());
37+
}
38+
39+
private InputStream decrypt(InputStream is, PrivateKey privateKey) throws ASAPSecurityException {
3140
try {
3241
this.cipher = Cipher.getInstance(keyStorage.getRSAEncryptionAlgorithm());
33-
this.cipher.init(Cipher.DECRYPT_MODE, this.keyStorage.getPrivateKey());
42+
this.cipher.init(Cipher.DECRYPT_MODE, privateKey);
3443

3544
// read len
36-
int len = 42; // TODO
37-
45+
int len = PDU_Impl.readIntegerParameter(is);
3846
byte[] messageBytes = new byte[len];
47+
48+
// read encrypted bytes from stream
3949
is.read(messageBytes);
50+
51+
Utils.compareArrays(messageBytes, this.byte2Send);
52+
53+
// decrypt
4054
byte[] decryptedBytes = this.cipher.doFinal(messageBytes);
4155
return new ByteArrayInputStream(decryptedBytes);
42-
} catch (BadPaddingException | IllegalBlockSizeException |
43-
NoSuchAlgorithmException | NoSuchPaddingException |
44-
InvalidKeyException | IOException e) {
56+
} catch (BadPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException |
57+
NoSuchPaddingException | InvalidKeyException | IOException | ASAPException e) {
4558
throw new ASAPSecurityException(this.getLogStart(), e);
4659
}
4760
}
@@ -54,6 +67,8 @@ InputStream decrypt(InputStream is) throws ASAPSecurityException {
5467
this.cmd = cmd;
5568
this.realOS = os;
5669
this.effectivOS = os; // still this one
70+
this.keyStorage = keyStorage;
71+
this.recipient = recipient;
5772

5873
if(encrypted) {
5974
// add to command
@@ -149,9 +164,24 @@ public void finish() throws ASAPSecurityException {
149164
try {
150165
byte[] encryptedBytes = this.cipher.doFinal(this.asapMessageAsBytes);
151166
// write data len
167+
168+
// write len
152169
PDU_Impl.sendNonNegativeIntegerParameter(encryptedBytes.length, this.realOS);
153170
// write data
154171
this.realOS.write(encryptedBytes);
172+
173+
// debug - decrypt
174+
175+
// make a test
176+
this.senderSiteTest = new ByteArrayOutputStream();
177+
PDU_Impl.sendNonNegativeIntegerParameter(encryptedBytes.length, senderSiteTest);
178+
senderSiteTest.write(encryptedBytes);
179+
PrivateKey privateKey = this.keyStorage.getPrivateKey(this.recipient);
180+
this.byte2Send = encryptedBytes;
181+
ByteArrayInputStream decrypt = (ByteArrayInputStream) this.decrypt(new ByteArrayInputStream(senderSiteTest.toByteArray()), privateKey);
182+
int i = 42;
183+
/*
184+
*/
155185
} catch (IllegalBlockSizeException | BadPaddingException | IOException e) {
156186
throw new ASAPSecurityException(this.getLogStart(), e);
157187
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected static void sendNonNegativeLongParameter(long longValue, OutputStream
204204
sendNonNegativeIntegerParameter((int) longValue, os);
205205
}
206206

207-
protected byte readByteParameter(InputStream is) throws IOException, ASAPException {
207+
static byte readByteParameter(InputStream is) throws IOException, ASAPException {
208208
return PDU_Impl.readByte(is);
209209
}
210210

@@ -216,18 +216,18 @@ static byte readByte(InputStream is) throws IOException, ASAPException {
216216
return (byte) value;
217217
}
218218

219-
protected short readShortParameter(InputStream is) throws IOException, ASAPException {
220-
int value = this.readByteParameter(is);
219+
static short readShortParameter(InputStream is) throws IOException, ASAPException {
220+
int value = readByteParameter(is);
221221
value = value << 8;
222-
int right = this.readByteParameter(is);
222+
int right = readByteParameter(is);
223223
value += right;
224224
return (short) value;
225225
}
226226

227-
protected int readIntegerParameter(InputStream is) throws IOException, ASAPException {
228-
int value = this.readShortParameter(is);
227+
static int readIntegerParameter(InputStream is) throws IOException, ASAPException {
228+
int value = readShortParameter(is);
229229
value = value << 16;
230-
int right = this.readShortParameter(is);
230+
int right = readShortParameter(is);
231231
value += right;
232232
return value;
233233
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.sharksystem.asap.protocol;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
12+
public class EncryptionTests {
13+
public static final String ALICE_ID = "Alice";
14+
public static final String BOB_ID = "Bob";
15+
16+
@Test
17+
public void sendAndReceiveInterestCanBeEncrypted() throws IOException, ASAPException {
18+
TestASAPKeyStorage keyStorage = new TestASAPKeyStorage();
19+
// add Bob
20+
keyStorage.createTestPeer(BOB_ID);
21+
22+
ASAP_1_0 asapModem = new ASAP_Modem_Impl(keyStorage);
23+
24+
String sender = ALICE_ID;
25+
String recipient = BOB_ID;
26+
String channel = "AliceURI";
27+
String format = "format";
28+
29+
ByteArrayOutputStream os = new ByteArrayOutputStream();
30+
31+
/////////////////////// encrypted
32+
asapModem.interest(sender, recipient, format, channel, os,false, true);
33+
34+
// try t read output
35+
InputStream is = new ByteArrayInputStream(os.toByteArray());
36+
37+
ASAP_PDU_1_0 asap_pdu_1_0 = asapModem.readPDU(is);
38+
39+
ASAP_Interest_PDU_1_0 interestPDU = (ASAP_Interest_PDU_1_0) asap_pdu_1_0;
40+
41+
Assert.assertTrue(interestPDU.getChannelUri().equalsIgnoreCase(channel));
42+
Assert.assertTrue(interestPDU.getFormat().equalsIgnoreCase(format));
43+
Assert.assertTrue(interestPDU.getSender().equalsIgnoreCase(sender));
44+
Assert.assertTrue(interestPDU.getRecipient().equalsIgnoreCase(recipient));
45+
}
46+
}

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -111,37 +111,6 @@ public void sendAndReceiveInterest2() throws IOException, ASAPException {
111111
Assert.assertFalse(interestPDU.eraToSet());
112112
}
113113

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

0 commit comments

Comments
 (0)