Skip to content

Commit ba6c485

Browse files
committed
Signing finished. Must use AES instead of RSA for encryption though. Much easier on the long run and faster.
1 parent 371f2e7 commit ba6c485

File tree

8 files changed

+135
-56
lines changed

8 files changed

+135
-56
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,25 @@ public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
117117
cmd = (byte)(cmd & CMD_MASK);
118118

119119
if(encrypted) {
120-
CryptoSession cryptoSession = new CryptoSession(this.signAndEncryptionKeyStorage);
121-
InputStream decryptedIS = cryptoSession.decrypt(is);
122-
is = decryptedIS;
120+
try {
121+
CryptoSession cryptoSession = new CryptoSession(this.signAndEncryptionKeyStorage);
122+
InputStream decryptedIS = cryptoSession.decrypt(is);
123+
is = decryptedIS;
124+
}
125+
catch(ASAPSecurityException e) {
126+
System.out.println(this.getLogStart() + "cannot decrypt message. TODO: Store (according to some rules) and forward it?!");
127+
}
123128
}
124129

125130
int flagsInt = PDU_Impl.readByte(is);
126131

132+
InputStream realIS = is;
133+
CryptoSession verifyCryptoSession = null;
134+
if(PDU_Impl.flagSet(PDU_Impl.SIGNED_TO_BIT_POSITION, flagsInt)) {
135+
verifyCryptoSession = new CryptoSession(this.signAndEncryptionKeyStorage);
136+
is = verifyCryptoSession.setupInputStreamListener(is, flagsInt);
137+
}
138+
127139
PDU_Impl pdu = null;
128140

129141
switch(cmd) {
@@ -133,15 +145,12 @@ public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
133145
default: throw new ASAPException("unknown command: " + cmd);
134146
}
135147

136-
if(pdu.signed()) {
148+
if(verifyCryptoSession != null) {
137149
String sender = pdu.getSender();
138150
if(sender != null) {
139151
// read signature and try to verify
140152
try {
141-
CryptoSession cryptoSession = new CryptoSession(this.signAndEncryptionKeyStorage);
142-
if(cryptoSession.verify(sender, is)) {
143-
pdu.setVerified(true);
144-
}
153+
pdu.setVerified(verifyCryptoSession.verify(sender, realIS));
145154
}
146155
catch(ASAPException e) {
147156
System.out.println(this.getLogStart() + " cannot verify message");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static void sendPDU(CharSequence peer, CharSequence recipientPeer, CharSequence
5757
// create parameter bytes
5858
int flags = 0;
5959
flags = PDU_Impl.setFlag(peer, flags, SENDER_BIT_POSITION);
60-
flags = PDU_Impl.setFlag(recipientPeer, flags, RECIPIENT_PEER_BIT_POSITION);
60+
flags = PDU_Impl.setFlag(recipientPeer, flags, RECIPIENT_BIT_POSITION);
6161
flags = PDU_Impl.setFlag(channel, flags, CHANNEL_BIT_POSITION);
6262
flags = PDU_Impl.setFlag(era, flags, ERA_BIT_POSITION);
6363
flags = PDU_Impl.setFlag(offsets, flags, OFFSETS_BIT_POSITION);

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.security.*;
1313

1414
class CryptoSession {
15+
public static final int MAX_ENCRYPTION_BLOCK_SIZE = 240;
1516
private Signature signature;
1617
private CharSequence recipient;
1718
private ASAPReadonlyKeyStorage keyStorage;
@@ -22,6 +23,7 @@ class CryptoSession {
2223
private OutputStream effectivOS;
2324
private OutputStream realOS;
2425
private ByteArrayOutputStream asapMessageOS;
26+
private InputStreamCopy verifyStream;
2527

2628
CryptoSession(ASAPReadonlyKeyStorage keyStorage) {
2729
this.keyStorage = keyStorage;
@@ -157,7 +159,18 @@ public void finish() throws ASAPSecurityException {
157159
try {
158160
// encrypted asap message
159161
byte[] asapMessageAsBytes = this.asapMessageOS.toByteArray();
160-
byte[] encryptedBytes = this.cipher.doFinal(asapMessageAsBytes);
162+
163+
// TODO: create AES key, encrypt with RSA, send and encrypt rest with that AES key
164+
165+
// that stuff will not work.
166+
int i = 0;
167+
while(i + MAX_ENCRYPTION_BLOCK_SIZE < asapMessageAsBytes.length) {
168+
this.cipher.update(asapMessageAsBytes, i, MAX_ENCRYPTION_BLOCK_SIZE);
169+
i += MAX_ENCRYPTION_BLOCK_SIZE;
170+
}
171+
172+
int lastStepLen = asapMessageAsBytes.length - i;
173+
byte[] encryptedBytes = this.cipher.doFinal(asapMessageAsBytes, i, lastStepLen);
161174

162175
this.writeByteArray(encryptedBytes, this.realOS);
163176
this.realOS.write(encryptedBytes);
@@ -168,6 +181,39 @@ public void finish() throws ASAPSecurityException {
168181
}
169182

170183
////////////////////////////////// verify
184+
private class InputStreamCopy extends InputStream {
185+
private final InputStream is;
186+
ByteArrayOutputStream copy = new ByteArrayOutputStream();
187+
188+
InputStreamCopy(byte[] bytes, InputStream is) throws IOException {
189+
// add byte if any
190+
if(bytes != null && bytes.length > 0) {
191+
copy.write(bytes);
192+
}
193+
194+
this.is = is;
195+
}
196+
197+
@Override
198+
public int read() throws IOException {
199+
int read = is.read();
200+
copy.write(read);
201+
return read;
202+
}
203+
204+
byte[] getCopy() {
205+
return copy.toByteArray();
206+
}
207+
}
208+
209+
public InputStream setupInputStreamListener(InputStream is, int flagsInt) throws IOException {
210+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
211+
PDU_Impl.sendFlags(flagsInt, baos);
212+
213+
this.verifyStream = new InputStreamCopy(baos.toByteArray(), is);
214+
215+
return this.verifyStream;
216+
}
171217

172218
public boolean verify(String sender, InputStream is) throws IOException, ASAPException {
173219
// try to get senders' public key
@@ -177,6 +223,9 @@ public boolean verify(String sender, InputStream is) throws IOException, ASAPExc
177223
try {
178224
this.signature = Signature.getInstance(this.keyStorage.getRSASigningAlgorithm());
179225
this.signature.initVerify(publicKey);
226+
// get data which are to be verified
227+
byte[] signedData = this.verifyStream.getCopy();
228+
this.signature.update(signedData);
180229
byte[] signatureBytes = this.readByteArray(is);
181230
boolean wasVerified = this.signature.verify(signatureBytes);
182231
return wasVerified;
@@ -221,5 +270,4 @@ private InputStream decrypt(InputStream is, PrivateKey privateKey) throws ASAPSe
221270
private String getLogStart() {
222271
return this.getClass().getSimpleName() + ": ";
223272
}
224-
225273
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void sendPDUWithoutCmd(CharSequence sender, CharSequence recipient, CharS
4747
// create parameter bytes
4848
int flags = 0;
4949
flags = PDU_Impl.setFlag(sender, flags, SENDER_BIT_POSITION);
50-
flags = PDU_Impl.setFlag(recipient, flags, RECIPIENT_PEER_BIT_POSITION);
50+
flags = PDU_Impl.setFlag(recipient, flags, RECIPIENT_BIT_POSITION);
5151
flags = PDU_Impl.setFlag(channel, flags, CHANNEL_BIT_POSITION);
5252
flags = PDU_Impl.setFlag(eraFrom, flags, ERA_FROM_BIT_POSITION);
5353
flags = PDU_Impl.setFlag(eraTo, flags, ERA_TO_BIT_POSITION);

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

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

33
import net.sharksystem.asap.ASAPException;
4-
import net.sharksystem.asap.ASAPSecurityException;
54

65
import java.io.IOException;
76
import java.io.InputStream;
@@ -12,7 +11,7 @@
1211

1312
abstract class PDU_Impl implements ASAP_PDU_1_0, ASAP_PDU_Management {
1413
public static final int SENDER_BIT_POSITION = 0;
15-
public static final int RECIPIENT_PEER_BIT_POSITION = 1;
14+
public static final int RECIPIENT_BIT_POSITION = 1;
1615
public static final int CHANNEL_BIT_POSITION = 2;
1716
public static final int ERA_BIT_POSITION = 3;
1817
public static final int ERA_FROM_BIT_POSITION = 4;
@@ -99,6 +98,16 @@ protected static void sendCmd(byte cmd, OutputStream os) throws IOException {
9998
}
10099

101100
protected void evaluateFlags(int flag) {
101+
this.senderSet = flagSet(SENDER_BIT_POSITION, flag);
102+
this.recipientSet = flagSet(RECIPIENT_BIT_POSITION, flag);
103+
this.channelSet = flagSet(CHANNEL_BIT_POSITION, flag);
104+
this.eraSet = flagSet(ERA_BIT_POSITION, flag);
105+
this.eraFrom = flagSet(ERA_FROM_BIT_POSITION, flag);
106+
this.eraTo = flagSet(ERA_TO_BIT_POSITION, flag);
107+
this.offsetsSet = flagSet(OFFSETS_BIT_POSITION, flag);
108+
this.signed = flagSet(SIGNED_TO_BIT_POSITION, flag);
109+
110+
/*
102111
// sender parameter set ?
103112
int testFlag = 1;
104113
testFlag = testFlag << SENDER_BIT_POSITION;
@@ -107,10 +116,11 @@ protected void evaluateFlags(int flag) {
107116
108117
// recipient peer parameter set ?
109118
testFlag = 1;
110-
testFlag = testFlag << RECIPIENT_PEER_BIT_POSITION;
119+
testFlag = testFlag << RECIPIENT_BIT_POSITION;
111120
result = flag & testFlag;
112121
recipientSet = result != 0;
113122
123+
114124
// channel parameter set ?
115125
testFlag = 1;
116126
testFlag = testFlag << CHANNEL_BIT_POSITION;
@@ -146,6 +156,14 @@ protected void evaluateFlags(int flag) {
146156
testFlag = testFlag << SIGNED_TO_BIT_POSITION;
147157
result = flag & testFlag;
148158
this.signed = result != 0;
159+
*/
160+
161+
}
162+
163+
static boolean flagSet(int bitPosition, int flags) {
164+
int flagMask = 1;
165+
flagMask = flagMask << bitPosition;
166+
return (flags & flagMask) != 0;
149167
}
150168

151169
@Override

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ public void sendAndReceiveInterest2() throws IOException, ASAPException {
114114

115115
@Test
116116
public void sendAndReceiveInterestEncrypted() throws IOException, ASAPException {
117-
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage();
117+
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage(ALICE_ID);
118118

119119
// add Bob
120120
KeyPair bobKeyPair = keyStorageAlice.createTestPeer(BOB_ID);
121-
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(bobKeyPair);
121+
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(BOB_ID, bobKeyPair);
122122

123123
ASAP_1_0 asapModemAlice = new ASAP_Modem_Impl(keyStorageAlice);
124124
ASAP_1_0 asapModemBob = new ASAP_Modem_Impl(keyStorageBob);
@@ -146,6 +146,42 @@ public void sendAndReceiveInterestEncrypted() throws IOException, ASAPException
146146
Assert.assertTrue(interestPDU.getRecipient().equalsIgnoreCase(recipient));
147147
}
148148

149+
@Test
150+
public void sendAndReceiveInterestSigned() throws IOException, ASAPException {
151+
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage(ALICE_ID);
152+
153+
// add Bob
154+
KeyPair bobKeyPair = keyStorageAlice.createTestPeer(BOB_ID);
155+
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(BOB_ID, bobKeyPair);
156+
keyStorageBob.addKeyPair(ALICE_ID, keyStorageAlice.getKeyPair());
157+
158+
ASAP_1_0 asapModemAlice = new ASAP_Modem_Impl(keyStorageAlice);
159+
ASAP_1_0 asapModemBob = new ASAP_Modem_Impl(keyStorageBob);
160+
161+
String sender = ALICE_ID;
162+
String recipient = BOB_ID;
163+
String channel = "AliceURI";
164+
String format = "format";
165+
166+
ByteArrayOutputStream os = new ByteArrayOutputStream();
167+
168+
/////////////////////// encrypted
169+
asapModemAlice.interest(sender, recipient, format, channel, os,true, false);
170+
171+
// try t read output
172+
InputStream is = new ByteArrayInputStream(os.toByteArray());
173+
174+
ASAP_PDU_1_0 asap_pdu_1_0 = asapModemBob.readPDU(is);
175+
176+
ASAP_Interest_PDU_1_0 interestPDU = (ASAP_Interest_PDU_1_0) asap_pdu_1_0;
177+
178+
Assert.assertTrue(interestPDU.getChannelUri().equalsIgnoreCase(channel));
179+
Assert.assertTrue(interestPDU.getFormat().equalsIgnoreCase(format));
180+
Assert.assertTrue(interestPDU.getSender().equalsIgnoreCase(sender));
181+
Assert.assertTrue(interestPDU.getRecipient().equalsIgnoreCase(recipient));
182+
Assert.assertTrue(interestPDU.verified());
183+
}
184+
149185
//////////////////// assimilate /////////////////////////////////////////
150186
@Test
151187
public void sendAndReceiveAssimilate() throws IOException, ASAPException {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@
88

99
public class TestASAPKeyStorage implements ASAPReadonlyKeyStorage {
1010
private final KeyPair keyPair;
11+
private final String name;
1112
private long timeInMillis = 0;
1213

1314
public static final String DEFAULT_RSA_ENCRYPTION_ALGORITHM = "RSA/ECB/PKCS1Padding";
1415
public static final String DEFAULT_SIGNATURE_METHOD = "SHA256withRSA";
1516

1617
private HashMap<String, KeyPair> peerKeyPairs = new HashMap<>();
1718

18-
TestASAPKeyStorage() throws ASAPSecurityException {
19+
TestASAPKeyStorage(String name) throws ASAPSecurityException {
1920
// generate owners key pair;
21+
this.name = name;
2022
this.keyPair = this.generateKeyPair();
2123
this.timeInMillis = System.currentTimeMillis();
2224
}
2325

24-
public TestASAPKeyStorage(KeyPair ownerKeyPair) {
26+
public TestASAPKeyStorage(String name, KeyPair ownerKeyPair) {
27+
this.name = name;
2528
this.keyPair = ownerKeyPair;
2629
}
2730

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

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,14 @@ public class Workbench {
1414
public static final String ALICE_ID = "Alice";
1515
public static final String BOB_ID = "Bob";
1616

17-
@Test
18-
public void sendAndReceiveInterestSigned() throws IOException, ASAPException {
19-
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage();
20-
21-
// add Bob
22-
KeyPair bobKeyPair = keyStorageAlice.createTestPeer(BOB_ID);
23-
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(bobKeyPair);
24-
keyStorageBob.addKeyPair(ALICE_ID, keyStorageAlice.getKeyPair());
25-
26-
ASAP_1_0 asapModemAlice = new ASAP_Modem_Impl(keyStorageAlice);
27-
ASAP_1_0 asapModemBob = new ASAP_Modem_Impl(keyStorageBob);
28-
29-
String sender = ALICE_ID;
30-
String recipient = BOB_ID;
31-
String channel = "AliceURI";
32-
String format = "format";
33-
34-
ByteArrayOutputStream os = new ByteArrayOutputStream();
35-
36-
/////////////////////// encrypted
37-
asapModemAlice.interest(sender, recipient, format, channel, os,true, false);
38-
39-
// try t read output
40-
InputStream is = new ByteArrayInputStream(os.toByteArray());
41-
42-
ASAP_PDU_1_0 asap_pdu_1_0 = asapModemBob.readPDU(is);
43-
44-
ASAP_Interest_PDU_1_0 interestPDU = (ASAP_Interest_PDU_1_0) asap_pdu_1_0;
45-
46-
Assert.assertTrue(interestPDU.getChannelUri().equalsIgnoreCase(channel));
47-
Assert.assertTrue(interestPDU.getFormat().equalsIgnoreCase(format));
48-
Assert.assertTrue(interestPDU.getSender().equalsIgnoreCase(sender));
49-
Assert.assertTrue(interestPDU.getRecipient().equalsIgnoreCase(recipient));
50-
Assert.assertTrue(interestPDU.verified());
51-
}
5217

5318
@Test
5419
public void sendAndReceiveInterestSignedAndEncrypted() throws IOException, ASAPException {
55-
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage();
20+
TestASAPKeyStorage keyStorageAlice = new TestASAPKeyStorage(ALICE_ID);
5621

5722
// add Bob
5823
KeyPair bobKeyPair = keyStorageAlice.createTestPeer(BOB_ID);
59-
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(bobKeyPair);
24+
TestASAPKeyStorage keyStorageBob = new TestASAPKeyStorage(BOB_ID,bobKeyPair);
6025
keyStorageBob.addKeyPair(ALICE_ID, keyStorageAlice.getKeyPair());
6126

6227
ASAP_1_0 asapModemAlice = new ASAP_Modem_Impl(keyStorageAlice);

0 commit comments

Comments
 (0)