Skip to content

Commit d3ebdb6

Browse files
committed
SharkNet is there. Still some tests fail. Anyway, successful day. Working with cryptography is no longer such a pain in the..
1 parent 1580965 commit d3ebdb6

14 files changed

+427
-112
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.sharksystem.asap;
2+
3+
import net.sharksystem.asap.protocol.ASAPConnection;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
9+
public interface ASAPConnectionHandler {
10+
ASAPConnection handleConnection(InputStream is, OutputStream os) throws IOException, ASAPException;
11+
}

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimilationPDU, A
521521
//<<<<<<<<<<<<<<<<<<debug
522522
b = new StringBuilder();
523523
b.append(this.getLogStart());
524-
b.append("listener is null - no callback");
524+
b.append("no chunk received listener found");
525525
System.out.println(b.toString());
526526
//>>>>>>>>>>>>>>>>>>>debug
527527
}

src/net/sharksystem/asap/ASAPPeer.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* That interface hides those different engines.
2323
*/
24-
public interface ASAPPeer {
24+
public interface ASAPPeer extends ASAPConnectionHandler {
2525
long DEFAULT_MAX_PROCESSING_TIME = Long.MAX_VALUE;
2626

2727
/**
@@ -51,16 +51,7 @@ public interface ASAPPeer {
5151
*/
5252
ASAPEngine getASAPEngine(CharSequence format) throws IOException, ASAPException;
5353

54-
/**
55-
* handle that newly established connection to another ASAP peer
56-
* @param is
57-
* @param os
58-
* @throws IOException
59-
* @throws ASAPException
60-
*/
61-
public ASAPConnection handleConnection(InputStream is, OutputStream os) throws IOException, ASAPException;
62-
63-
public void pushInterests(OutputStream os) throws IOException, ASAPException;
54+
void pushInterests(OutputStream os) throws IOException, ASAPException;
6455

6556
Set<CharSequence> getOnlinePeers();
6657

src/net/sharksystem/asap/ASAPPeerFS.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.Utils;
34
import net.sharksystem.asap.management.ASAPManagementMessageHandler;
45
import net.sharksystem.asap.protocol.*;
56
import net.sharksystem.asap.sharknet.SharkNet;
@@ -249,7 +250,7 @@ public ASAPChunkReceivedListener getListenerByFormat(CharSequence format) throws
249250
}
250251

251252
private String getEngineFolderByAppName(CharSequence appName) {
252-
return this.rootFolderName.toString() + "/" + appName;
253+
return this.rootFolderName.toString() + "/" + Utils.url2FileName(appName.toString());
253254
}
254255

255256
@Override
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package net.sharksystem.asap.sharknet;
22

33
public interface SharkNet {
4-
public static final String SHARKNET_FORMAT = "asap/sharknet";
4+
String SHARKNET_FORMAT = "asap/sharknet";
55
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package net.sharksystem.asap.sharknet;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.asap.ASAPSecurityException;
5+
import net.sharksystem.crypto.ASAPCryptoAlgorithms;
6+
import net.sharksystem.crypto.BasicCryptoParameters;
7+
import net.sharksystem.utils.Serialization;
8+
9+
import java.io.ByteArrayInputStream;
10+
import java.io.ByteArrayOutputStream;
11+
import java.io.IOException;
12+
13+
class SharkNetMessage {
14+
private static final int SIGNED_MASK = 0x1;
15+
private static final int ENCRYPTED_MASK = 0x2;
16+
private final byte[] snMessage;
17+
private final CharSequence snSender;
18+
private final boolean verified;
19+
private final boolean encrypted;
20+
private final CharSequence topic;
21+
22+
public SharkNetMessage(byte[] snMessage, String snSender, boolean verified, boolean encrypted) {
23+
this(snMessage, null, snSender, verified, encrypted);
24+
}
25+
26+
public SharkNetMessage(byte[] message, CharSequence topic, CharSequence sender, boolean verified, boolean encrypted) {
27+
this.snMessage = message;
28+
this.snSender = sender;
29+
this.verified = verified;
30+
this.encrypted = encrypted;
31+
this.topic = topic;
32+
}
33+
34+
static byte[] serializeMessage(byte[] message, CharSequence topic, CharSequence recipient,
35+
boolean sign, boolean encrypt, CharSequence ownerID, BasicCryptoParameters basicCryptoParameters)
36+
throws IOException, ASAPException {
37+
38+
// merge content, sender and recipient
39+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
40+
Serialization.writeByteArray(message, baos);
41+
Serialization.writeCharSequenceParameter(ownerID, baos);
42+
Serialization.writeCharSequenceParameter(recipient, baos);
43+
message = baos.toByteArray();
44+
45+
byte flags = 0;
46+
if(sign) {
47+
byte[] signature = ASAPCryptoAlgorithms.sign(message, basicCryptoParameters);
48+
baos = new ByteArrayOutputStream();
49+
Serialization.writeByteArray(message, baos); // message has three parts: content, sender, receiver
50+
// append signature
51+
Serialization.writeByteArray(signature, baos);
52+
// attach signature to message
53+
message = baos.toByteArray();
54+
flags += SIGNED_MASK;
55+
}
56+
57+
if(encrypt) {
58+
message = ASAPCryptoAlgorithms.produceEncryptedMessagePackage(
59+
message, recipient, basicCryptoParameters);
60+
flags += ENCRYPTED_MASK;
61+
}
62+
63+
// serialize SN message
64+
baos = new ByteArrayOutputStream();
65+
Serialization.writeByteParameter(flags, baos);
66+
Serialization.writeByteArray(message, baos);
67+
68+
return baos.toByteArray();
69+
}
70+
71+
static SharkNetMessage parseMessage(byte[] message, String sender, String uri,
72+
CharSequence ownerID, BasicCryptoParameters basicCryptoParameters) throws IOException, ASAPException {
73+
74+
ByteArrayInputStream bais = new ByteArrayInputStream(message);
75+
byte flags = Serialization.readByte(bais);
76+
byte[] tmpMessage = Serialization.readByteArray(bais);
77+
78+
boolean signed = (flags & SIGNED_MASK) != 0;
79+
boolean encrypted = (flags & ENCRYPTED_MASK) != 0;
80+
81+
if(encrypted) {
82+
// decrypt
83+
bais = new ByteArrayInputStream(tmpMessage);
84+
ASAPCryptoAlgorithms.EncryptedMessagePackage
85+
encryptedMessagePackage = ASAPCryptoAlgorithms.parseEncryptedMessagePackage(bais);
86+
87+
// for me?
88+
if(!encryptedMessagePackage.getRecipient().equals(ownerID)) {
89+
throw new ASAPException("SharkNetMessage: message not for me");
90+
}
91+
92+
// replace message with decrypted message
93+
tmpMessage = ASAPCryptoAlgorithms.decryptPackage(
94+
encryptedMessagePackage, basicCryptoParameters);
95+
}
96+
97+
byte[] signature = null;
98+
byte[] signedMessage = null;
99+
if(signed) {
100+
// split message from signature
101+
bais = new ByteArrayInputStream(tmpMessage);
102+
tmpMessage = Serialization.readByteArray(bais);
103+
signedMessage = tmpMessage;
104+
signature = Serialization.readByteArray(bais);
105+
}
106+
bais = new ByteArrayInputStream(tmpMessage);
107+
byte[] snMessage = Serialization.readByteArray(bais);
108+
String snSender = Serialization.readCharSequenceParameter(bais);
109+
String snReceiver = Serialization.readCharSequenceParameter(bais);
110+
111+
boolean verified = false; // initialize
112+
if(signature != null) {
113+
try {
114+
verified = ASAPCryptoAlgorithms.verify(
115+
signedMessage, signature, snSender, basicCryptoParameters);
116+
}
117+
catch(ASAPSecurityException e) {
118+
// verified definitely false
119+
verified = false;
120+
}
121+
}
122+
123+
return new SharkNetMessage(snMessage, snSender, verified, encrypted);
124+
}
125+
126+
public byte[] getContent() { return this.snMessage;}
127+
128+
public CharSequence getSender() { return this.snSender; }
129+
public boolean verified() { return this.verified; }
130+
public boolean encrypted() { return this.encrypted; }
131+
}

src/net/sharksystem/asap/sharknet/SharkNetPeer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package net.sharksystem.asap.sharknet;
22

3+
import net.sharksystem.asap.ASAPConnectionHandler;
34
import net.sharksystem.asap.ASAPException;
45
import net.sharksystem.asap.ASAPPeer;
56

67
import java.io.IOException;
78

8-
public interface SharkNetPeer {
9+
public interface SharkNetPeer extends ASAPConnectionHandler {
910
CharSequence getOwnerID();
1011

1112
void sendSharkNetMessage(byte[] message, CharSequence topic, CharSequence recipient,
Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package net.sharksystem.asap.sharknet;
22

33
import net.sharksystem.asap.*;
4+
import net.sharksystem.asap.protocol.ASAPConnection;
45
import net.sharksystem.asap.util.Helper;
5-
import net.sharksystem.crypto.ASAPCryptoAlgorithms;
66
import net.sharksystem.crypto.BasicCryptoParameters;
7-
import net.sharksystem.utils.Serialization;
87

9-
import java.io.ByteArrayInputStream;
10-
import java.io.ByteArrayOutputStream;
11-
import java.io.IOException;
8+
import java.io.*;
129
import java.util.HashSet;
1310
import java.util.Iterator;
1411
import java.util.Set;
@@ -40,45 +37,14 @@ public CharSequence getOwnerID() {
4037
return this.asapPeer.getOwner();
4138
}
4239

43-
private static final int SIGNED_MASK = 0x1;
44-
private static final int ENCRYPTED_MASK = 0x2;
45-
4640
@Override
4741
public void sendSharkNetMessage(byte[] message, CharSequence topic, CharSequence recipient,
4842
boolean sign, boolean encrypt) throws IOException, ASAPException {
4943

5044
// serialize sn message
51-
52-
// merge content, sender and recipient
53-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
54-
Serialization.writeByteArray(message, baos);
55-
Serialization.writeCharSequenceParameter(this.getOwnerID(), baos);
56-
Serialization.writeCharSequenceParameter(recipient, baos);
57-
message = baos.toByteArray();
58-
59-
byte flags = 0;
60-
if(sign) {
61-
byte[] signature = ASAPCryptoAlgorithms.sign(message, this.basicCryptoParameters);
62-
baos = new ByteArrayOutputStream();
63-
Serialization.writeByteArray(message, baos);
64-
Serialization.writeByteArray(signature, baos);
65-
// attach signature to message
66-
message = baos.toByteArray();
67-
flags += SIGNED_MASK;
68-
}
69-
70-
if(encrypt) {
71-
message = ASAPCryptoAlgorithms.produceEncryptedMessagePackage(
72-
message, recipient, this.basicCryptoParameters);
73-
flags += ENCRYPTED_MASK;
74-
}
75-
76-
// serialize SN message
77-
baos = new ByteArrayOutputStream();
78-
Serialization.writeByteParameter(flags, baos);
79-
Serialization.writeByteArray(message, baos);
80-
81-
this.sharkNetEngine.add(topic, baos.toByteArray());
45+
this.sharkNetEngine.add(topic,
46+
SharkNetMessage.serializeMessage(message, topic, recipient, sign, encrypt,
47+
this.getOwnerID(), this.basicCryptoParameters));
8248
}
8349

8450
@Override
@@ -104,54 +70,18 @@ public void chunkReceived(String format, String sender, String uri, int era) thr
10470
byte[] message = msgIter.next();
10571

10672
// deserialize SNMessage
107-
ByteArrayInputStream bais = new ByteArrayInputStream(message);
10873
try {
109-
byte flags = Serialization.readByte(bais);
110-
byte[] snMessage = Serialization.readByteArray(bais);
111-
112-
boolean signed = (flags & SIGNED_MASK) != 0;
113-
boolean encrypted = (flags & ENCRYPTED_MASK) != 0;
114-
115-
if(encrypted) {
116-
// decrypt
117-
bais = new ByteArrayInputStream(snMessage);
118-
ASAPCryptoAlgorithms.EncryptedMessagePackage
119-
encryptedMessagePackage = ASAPCryptoAlgorithms.parseEncryptedMessagePackage(bais);
120-
121-
// for me?
122-
if(!encryptedMessagePackage.getRecipient().equals(this.getOwnerID())) {
123-
System.out.println(this.getLogStart() + "message not for me");
124-
continue; // still in asapStorage and will be redistributed
125-
}
126-
127-
// replace message with decrypted message
128-
snMessage = ASAPCryptoAlgorithms.decryptPackage(
129-
encryptedMessagePackage, this.basicCryptoParameters);
130-
}
131-
132-
byte[] signature = null;
133-
if(signed) {
134-
// split message from signature
135-
bais = new ByteArrayInputStream(snMessage);
136-
snMessage = Serialization.readByteArray(bais);
137-
signature = Serialization.readByteArray(bais);
138-
}
139-
bais = new ByteArrayInputStream(snMessage);
140-
String snSender = Serialization.readCharSequenceParameter(bais);
141-
String snReceiver = Serialization.readCharSequenceParameter(bais);
142-
143-
boolean verified = false; // initialize
144-
if(signature != null) {
145-
verified = ASAPCryptoAlgorithms.verify(
146-
snMessage, signature, snSender, this.basicCryptoParameters);
147-
}
74+
SharkNetMessage snMessage =
75+
SharkNetMessage.parseMessage(message, sender, uri, this.getOwnerID(), this.basicCryptoParameters);
14876

14977
// we have anything - tell listeners
15078
for(SharkNetMessageListener l : this.snListenerSet) {
151-
l.messageReceived(snMessage, uri, snSender, verified, encrypted);
79+
l.messageReceived(snMessage.getContent(), uri, snMessage.getSender(), snMessage.verified(),
80+
snMessage.encrypted());
15281
}
15382
} catch (ASAPException e) {
15483
System.out.println(this.getLogStart() + "problems when deserializing SharkNet message");
84+
e.printStackTrace();
15585
continue; // try next
15686
}
15787
}
@@ -161,4 +91,9 @@ public void chunkReceived(String format, String sender, String uri, int era) thr
16191
private String getLogStart() {
16292
return this.getClass().getSimpleName() + ": ";
16393
}
94+
95+
@Override
96+
public ASAPConnection handleConnection(InputStream is, OutputStream os) throws IOException, ASAPException {
97+
return this.asapPeer.handleConnection(is, os);
98+
}
16499
}

src/net/sharksystem/asap/util/ASAPPeerHandleConnectionThread.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.InputStream;
55
import java.io.OutputStream;
66

7+
import net.sharksystem.asap.ASAPConnectionHandler;
78
import net.sharksystem.asap.ASAPException;
89
import net.sharksystem.asap.ASAPPeer;
910

@@ -12,11 +13,11 @@
1213
* @author thsc
1314
*/
1415
public class ASAPPeerHandleConnectionThread extends Thread {
15-
private final ASAPPeer engine;
16+
private final ASAPConnectionHandler engine;
1617
private final InputStream is;
1718
private final OutputStream os;
1819

19-
public ASAPPeerHandleConnectionThread(ASAPPeer engine, InputStream is, OutputStream os) {
20+
public ASAPPeerHandleConnectionThread(ASAPConnectionHandler engine, InputStream is, OutputStream os) {
2021
this.engine = engine;
2122
this.is = is;
2223
this.os = os;

src/net/sharksystem/crypto/ASAPCryptoAlgorithms.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public static byte[] decryptSymmetric(byte[] encryptedContent, SecretKey symmetr
168168
return symmetricCipher.doFinal(encryptedContent);
169169
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException
170170
| IllegalBlockSizeException | BadPaddingException e) {
171-
throw new ASAPSecurityException("problems when encrypting with symmetric key", e);
171+
throw new ASAPSecurityException("problems when decrypting with symmetric key", e);
172172
}
173173
}
174174

0 commit comments

Comments
 (0)