Skip to content

Commit 1580965

Browse files
committed
start building an example of encrypted message routing - call it SharkNet. This will be the killer app. Seriously :)
1 parent 97acc58 commit 1580965

17 files changed

+372
-98
lines changed

src/net/sharksystem/asap/ASAPPeer.java

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

33
import net.sharksystem.asap.protocol.*;
4-
import net.sharksystem.crypto.BasisCryptoParameters;
4+
import net.sharksystem.asap.sharknet.SharkNetMessageListener;
5+
import net.sharksystem.crypto.BasicCryptoParameters;
56

67
import java.io.IOException;
78
import java.io.InputStream;
@@ -112,7 +113,11 @@ void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget
112113
void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget, byte[] messageAsBytes)
113114
throws IOException, ASAPException;
114115

115-
void setASAPBasicKeyStorage(BasisCryptoParameters basisCryptoParameters);
116+
void setASAPBasicKeyStorage(BasicCryptoParameters basicCryptoParameters);
116117

117118
ASAPCommunicationSetting getASAPCommunicationControl();
119+
120+
BasicCryptoParameters getBasicCryptoParameters() throws ASAPSecurityException;
121+
122+
void setSecurityAdministrator(DefaultSecurityAdministrator securityAdministrator);
118123
}

src/net/sharksystem/asap/ASAPPeerFS.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import net.sharksystem.asap.management.ASAPManagementMessageHandler;
44
import net.sharksystem.asap.protocol.*;
5+
import net.sharksystem.asap.sharknet.SharkNet;
6+
import net.sharksystem.asap.sharknet.SharkNetMessageListener;
57
import net.sharksystem.asap.util.Helper;
68
import net.sharksystem.asap.util.Log;
7-
import net.sharksystem.crypto.BasisCryptoParameters;
9+
import net.sharksystem.crypto.ASAPCryptoAlgorithms;
10+
import net.sharksystem.crypto.BasicCryptoKeyStorage;
11+
import net.sharksystem.crypto.BasicCryptoParameters;
812
import net.sharksystem.crypto.ASAPCommunicationCryptoSettings;
913

1014
import java.io.*;
@@ -19,13 +23,18 @@ public class ASAPPeerFS implements
1923
private CharSequence owner;
2024
private HashMap<CharSequence, EngineSetting> folderMap;
2125
private final long maxExecutionTime;
22-
private BasisCryptoParameters basisCryptoParameters;
26+
private BasicCryptoParameters basicCryptoParameters;
2327
private DefaultSecurityAdministrator defaultSecurityAdministrator = new DefaultSecurityAdministrator();
28+
private BasicCryptoKeyStorage basicCryptoKeyStorage;
2429

2530
public ASAPCommunicationCryptoSettings getASAPCommunicationCryptoSettings() {
2631
return this.defaultSecurityAdministrator;
2732
}
2833

34+
public void setSecurityAdministrator(DefaultSecurityAdministrator securityAdministrator) {
35+
this.defaultSecurityAdministrator = securityAdministrator;
36+
}
37+
2938
public ASAPCommunicationSetting getASAPCommunicationControl() {
3039
return this.defaultSecurityAdministrator;
3140
}
@@ -288,7 +297,7 @@ public Set<CharSequence> getFormats() {
288297
public ASAPConnection handleConnection(InputStream is, OutputStream os) {
289298
ASAPPersistentConnection asapConnection = new ASAPPersistentConnection(
290299
is, os, this, new ASAP_Modem_Impl(),
291-
this, this.basisCryptoParameters,
300+
this, this.basicCryptoParameters,
292301
maxExecutionTime, this, this);
293302

294303
StringBuilder sb = new StringBuilder();
@@ -550,18 +559,20 @@ public void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence ur
550559
}
551560

552561
@Override
553-
public void setASAPBasicKeyStorage(BasisCryptoParameters basisCryptoParameters) {
554-
this.basisCryptoParameters = basisCryptoParameters;
562+
public void setASAPBasicKeyStorage(BasicCryptoParameters basicCryptoParameters) {
563+
this.basicCryptoParameters = basicCryptoParameters;
555564
}
556565

557566
public void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget,
558-
Set<CharSequence> recipients, byte[] messageAsBytes) throws IOException, ASAPException {
567+
Set<CharSequence> recipients, byte[] messageAsBytes)
568+
throws IOException, ASAPException {
559569

560570
this.sendOnlineASAPAssimilateMessage(format, urlTarget, recipients, messageAsBytes, ASAP.INITIAL_ERA);
561571
}
562572

563573
public void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget,
564-
Set<CharSequence> recipients, byte[] messageAsBytes, int era) throws IOException, ASAPException {
574+
Set<CharSequence> recipients, byte[] messageAsBytes, int era)
575+
throws IOException, ASAPException {
565576

566577
// setup online message sender thread
567578
Log.writeLog(this, "setup online message sender object");
@@ -588,16 +599,30 @@ private String getLogStart() {
588599

589600
//////////////////////////////// handle message this peer cannot decrypt
590601
@Override
591-
public void handleUndecryptableMessage(byte[] encryptedMessage, CharSequence receiver) {
602+
public void handleUndecryptableMessage(
603+
ASAPCryptoAlgorithms.EncryptedMessagePackage encryptedMessagePackage,
604+
CharSequence receiver) {
605+
592606
System.out.println(this.getLogStart() + "handle undecryptable messages from " + receiver);
593607

594608
try {
595609
ASAPEngine undecryptEngine =
596610
this.getASAPEngine(ASAPUndecryptableMessageHandler.FORMAT_UNDECRYPTABLE_MESSAGES);
597611

598-
undecryptEngine.add(URI_UNDECRYPTABLE_MESSAGES, encryptedMessage);
612+
undecryptEngine.add(
613+
URI_UNDECRYPTABLE_MESSAGES,
614+
ASAPCryptoAlgorithms.getEncryptedMessagePackageAsBytes(encryptedMessagePackage));
599615
} catch (IOException | ASAPException e) {
600616
System.out.println(this.getLogStart() + "cannot handle undecrypted messages - no engine present");
601617
}
602618
}
619+
620+
///////////////////////////////// SharkNet
621+
@Override
622+
public BasicCryptoParameters getBasicCryptoParameters() throws ASAPSecurityException {
623+
if(this.basicCryptoKeyStorage == null) {
624+
this.basicCryptoKeyStorage = new BasicCryptoKeyStorage(this.getOwner().toString());
625+
}
626+
return this.basicCryptoKeyStorage;
627+
}
603628
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.sharksystem.asap;
22

3-
import java.util.BitSet;
3+
import net.sharksystem.crypto.ASAPCryptoAlgorithms;
44

55
public interface ASAPUndecryptableMessageHandler {
66
String FORMAT_UNDECRYPTABLE_MESSAGES = "asap/undecryptable";
@@ -9,5 +9,5 @@ public interface ASAPUndecryptableMessageHandler {
99
* Peer can (and should) receive encrypted messages without being receiver. A peer is not able
1010
* to encrypt that message but could store and forward. That is what ASAP is about.
1111
*/
12-
void handleUndecryptableMessage(byte[] encryptedMessage, CharSequence receiver);
12+
void handleUndecryptableMessage(ASAPCryptoAlgorithms.EncryptedMessagePackage encryptedMessage, CharSequence receiver);
1313
}

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import net.sharksystem.asap.ASAPException;
44
import net.sharksystem.asap.ASAPSecurityException;
5-
import net.sharksystem.crypto.BasisCryptoParameters;
5+
import net.sharksystem.crypto.BasicCryptoParameters;
66
import net.sharksystem.crypto.ASAPCryptoAlgorithms;
77
import net.sharksystem.utils.Serialization;
88

@@ -12,7 +12,7 @@ class ASAPCryptoMessage {
1212
private boolean encrypted;
1313
private boolean sign;
1414
private CharSequence recipient;
15-
private BasisCryptoParameters basisCryptoParameters;
15+
private BasicCryptoParameters basicCryptoParameters;
1616
private byte cmd;
1717

1818
private OutputStream effectiveOS;
@@ -21,26 +21,26 @@ class ASAPCryptoMessage {
2121
private InputStreamCopy inputStreamCopy;
2222
private ASAPCryptoAlgorithms.EncryptedMessagePackage encryptedMessagePackage;
2323

24-
ASAPCryptoMessage(BasisCryptoParameters basisCryptoParameters) {
25-
this.basisCryptoParameters = basisCryptoParameters;
24+
ASAPCryptoMessage(BasicCryptoParameters basicCryptoParameters) {
25+
this.basicCryptoParameters = basicCryptoParameters;
2626
}
2727

2828
ASAPCryptoMessage(byte cmd, OutputStream os, boolean sign, boolean encrypted,
2929
CharSequence recipient,
30-
BasisCryptoParameters basisCryptoParameters)
30+
BasicCryptoParameters basicCryptoParameters)
3131
throws ASAPSecurityException {
3232

3333
this.cmd = cmd;
3434
this.realOS = os;
3535
this.effectiveOS = os; // still this one
36-
this.basisCryptoParameters = basisCryptoParameters;
36+
this.basicCryptoParameters = basicCryptoParameters;
3737
this.recipient = recipient;
3838
this.encrypted = encrypted;
3939
this.sign = sign;
4040

4141
if(encrypted || sign) {
4242
// we need some basic crypto parameters
43-
if(basisCryptoParameters == null) {
43+
if(basicCryptoParameters == null) {
4444
throw new ASAPSecurityException("cannot encrypt or sign without cryptp parameters / key store");
4545
}
4646
this.setupCopyOutputStream();
@@ -56,7 +56,7 @@ class ASAPCryptoMessage {
5656

5757
if(sign) {
5858
// signing needs a private key - check of available
59-
if(basisCryptoParameters.getPrivateKey() == null) {
59+
if(basicCryptoParameters.getPrivateKey() == null) {
6060
throw new ASAPSecurityException("asap message is to be signed but no private key - fatal, give up");
6161
}
6262
}
@@ -85,7 +85,7 @@ public void finish() throws ASAPSecurityException {
8585
// get message as bytes
8686
byte[] asapMessageAsBytes = this.outputStreamCopy.toByteArray();
8787
// produce signature
88-
byte[] signatureBytes = ASAPCryptoAlgorithms.sign(asapMessageAsBytes, this.basisCryptoParameters);
88+
byte[] signatureBytes = ASAPCryptoAlgorithms.sign(asapMessageAsBytes, this.basicCryptoParameters);
8989

9090
if(this.encrypted) {
9191
// have to store it - message and signature will be encrypted
@@ -105,7 +105,7 @@ public void finish() throws ASAPSecurityException {
105105
byte[] asapMessageAsBytes = this.outputStreamCopy.toByteArray();
106106

107107
ASAPCryptoAlgorithms.writeEncryptedMessagePackage(
108-
asapMessageAsBytes, this.recipient, this.basisCryptoParameters, this.realOS);
108+
asapMessageAsBytes, this.recipient, this.basicCryptoParameters, this.realOS);
109109
}
110110
}
111111

@@ -159,7 +159,7 @@ public boolean verify(String sender, InputStream is) throws IOException, ASAPExc
159159
byte[] signatureBytes = Serialization.readByteArray(is);
160160
// debug break
161161
boolean wasVerified =
162-
ASAPCryptoAlgorithms.verify(signedData, signatureBytes, sender, this.basisCryptoParameters);
162+
ASAPCryptoAlgorithms.verify(signedData, signatureBytes, sender, this.basicCryptoParameters);
163163

164164
return wasVerified;
165165
}
@@ -179,30 +179,34 @@ public boolean verify(String sender, InputStream is) throws IOException, ASAPExc
179179
*/
180180
public boolean initDecryption(byte cmd, InputStream is) throws IOException, ASAPException {
181181
// make a copy of encrypted message - it is redundant. Same data in encryptedMessagePackage
182-
InputStream copyStream = this.setupCopyInputStream(cmd, is);
182+
//InputStream copyStream = this.setupCopyInputStream(cmd, is);
183183

184184
this.encryptedMessagePackage =
185-
ASAPCryptoAlgorithms.parseEncryptedMessagePackage(copyStream);
185+
ASAPCryptoAlgorithms.parseEncryptedMessagePackage(is);
186+
// ASAPCryptoAlgorithms.parseEncryptedMessagePackage(copyStream);
186187

187-
if(this.basisCryptoParameters == null) {
188+
if(this.basicCryptoParameters == null) {
188189
System.out.println(this.getLogStart() + "no keystore set: cannot handle encrypted messages");
189190
return false;
190191
}
191192

192-
if(this.basisCryptoParameters.isOwner(this.encryptedMessagePackage.getRecipient())) {
193+
if(this.basicCryptoParameters.isOwner(this.encryptedMessagePackage.getRecipient())) {
193194
return true;
194195
}
195196

196197
return false;
197198
}
198199

199-
byte[] getEncryptedMessage() throws ASAPSecurityException {
200+
ASAPCryptoAlgorithms.EncryptedMessagePackage getEncryptedMessage() throws ASAPSecurityException {
201+
return this.encryptedMessagePackage;
202+
/*
200203
if(this.inputStreamCopy == null) {
201204
throw new ASAPSecurityException(
202205
this.getLogStart() + "no copy made, maybe forgot to initialize decryption?");
203206
}
204207
205208
return this.inputStreamCopy.getCopy();
209+
*/
206210
}
207211

208212
public InputStream doDecryption() throws ASAPSecurityException {
@@ -211,7 +215,7 @@ public InputStream doDecryption() throws ASAPSecurityException {
211215
}
212216

213217
byte[] decryptedBytes =
214-
ASAPCryptoAlgorithms.decryptPackage(this.encryptedMessagePackage, this.basisCryptoParameters);
218+
ASAPCryptoAlgorithms.decryptPackage(this.encryptedMessagePackage, this.basicCryptoParameters);
215219

216220
return new ByteArrayInputStream(decryptedBytes);
217221
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import net.sharksystem.asap.*;
44
import net.sharksystem.asap.util.Log;
5-
import net.sharksystem.crypto.BasisCryptoParameters;
5+
import net.sharksystem.crypto.BasicCryptoParameters;
66

77
import java.io.IOException;
88
import java.io.InputStream;
@@ -26,11 +26,11 @@ public class ASAPPersistentConnection extends ASAPProtocolEngine
2626

2727
public ASAPPersistentConnection(InputStream is, OutputStream os, ASAPPeer asapPeer,
2828
ASAP_1_0 protocol, ASAPUndecryptableMessageHandler unencryptableMessageHandler,
29-
BasisCryptoParameters basisCryptoParameters,
29+
BasicCryptoParameters basicCryptoParameters,
3030
long maxExecutionTime, ASAPConnectionListener asapConnectionListener,
3131
ThreadFinishedListener threadFinishedListener) {
3232

33-
super(is, os, protocol, unencryptableMessageHandler, basisCryptoParameters);
33+
super(is, os, protocol, unencryptableMessageHandler, basicCryptoParameters);
3434

3535
this.asapPeer = asapPeer;
3636
this.maxExecutionTime = maxExecutionTime;
@@ -176,7 +176,7 @@ private synchronized void checkRunningOnlineMessageSender() {
176176
}
177177

178178
public void run() {
179-
ASAP_1_0 protocol = new ASAP_Modem_Impl(this.basisCryptoParameters, this.undecryptableMessageHandler);
179+
ASAP_1_0 protocol = new ASAP_Modem_Impl(this.basicCryptoParameters, this.undecryptableMessageHandler);
180180

181181
try {
182182
// let engine write their interest - at least management interest is sent which als introduces

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import net.sharksystem.asap.ASAPException;
44
import net.sharksystem.asap.ASAPUndecryptableMessageHandler;
5-
import net.sharksystem.crypto.BasisCryptoParameters;
5+
import net.sharksystem.crypto.BasicCryptoParameters;
66

77
import java.io.IOException;
88
import java.io.InputStream;
@@ -15,16 +15,16 @@ public abstract class ASAPProtocolEngine {
1515
protected final InputStream is;
1616
protected final OutputStream os;
1717
protected final ASAPUndecryptableMessageHandler undecryptableMessageHandler;
18-
protected final BasisCryptoParameters basisCryptoParameters;
18+
protected final BasicCryptoParameters basicCryptoParameters;
1919

2020
public ASAPProtocolEngine(InputStream is, OutputStream os, ASAP_1_0 protocol,
2121
ASAPUndecryptableMessageHandler undecryptableMessageHandler,
22-
BasisCryptoParameters basisCryptoParameters) {
22+
BasicCryptoParameters basicCryptoParameters) {
2323
this.is = is;
2424
this.os = os;
2525
this.protocol = protocol;
2626
this.undecryptableMessageHandler = undecryptableMessageHandler;
27-
this.basisCryptoParameters = basisCryptoParameters;
27+
this.basicCryptoParameters = basicCryptoParameters;
2828
}
2929

3030
/**

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import net.sharksystem.asap.ASAPException;
44
import net.sharksystem.asap.ASAPSecurityException;
55
import net.sharksystem.asap.ASAPUndecryptableMessageHandler;
6-
import net.sharksystem.crypto.BasisCryptoParameters;
6+
import net.sharksystem.crypto.ASAPCryptoAlgorithms;
7+
import net.sharksystem.crypto.BasicCryptoParameters;
78
import net.sharksystem.crypto.ASAPCommunicationCryptoSettings;
89
import net.sharksystem.utils.Serialization;
910

@@ -14,7 +15,7 @@
1415
import java.util.List;
1516

1617
public class ASAP_Modem_Impl implements ASAP_1_0 {
17-
private final BasisCryptoParameters signAndEncryptionKeyStorage;
18+
private final BasicCryptoParameters signAndEncryptionKeyStorage;
1819
private final ASAPUndecryptableMessageHandler unencryptableMessageHandler;
1920

2021
public ASAP_Modem_Impl() {
@@ -25,11 +26,11 @@ public ASAP_Modem_Impl(ASAPUndecryptableMessageHandler unencryptableMessageHandl
2526
this(null, unencryptableMessageHandler);
2627
}
2728

28-
public ASAP_Modem_Impl(BasisCryptoParameters signAndEncryptionKeyStorage) {
29+
public ASAP_Modem_Impl(BasicCryptoParameters signAndEncryptionKeyStorage) {
2930
this(signAndEncryptionKeyStorage, null);
3031
}
3132

32-
public ASAP_Modem_Impl(BasisCryptoParameters signAndEncryptionKeyStorage,
33+
public ASAP_Modem_Impl(BasicCryptoParameters signAndEncryptionKeyStorage,
3334
ASAPUndecryptableMessageHandler unencryptableMessageHandler) {
3435
this.signAndEncryptionKeyStorage = signAndEncryptionKeyStorage;
3536
this.unencryptableMessageHandler = unencryptableMessageHandler;
@@ -184,7 +185,7 @@ public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
184185
is = decryptedIS;
185186
} else {
186187
// we cannot decrypt this message - we are not recipient - but we keep and redistribute
187-
byte[] encryptedASAPMessage = cryptoMessage.getEncryptedMessage();
188+
ASAPCryptoAlgorithms.EncryptedMessagePackage encryptedASAPMessage = cryptoMessage.getEncryptedMessage();
188189
if(this.unencryptableMessageHandler != null) {
189190
System.out.println(this.getLogStart() + "call handler to handle unencryptable message");
190191
this.unencryptableMessageHandler.handleUndecryptableMessage(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.sharksystem.asap.sharknet;
2+
3+
public interface SharkNet {
4+
public static final String SHARKNET_FORMAT = "asap/sharknet";
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.sharksystem.asap.sharknet;
2+
3+
public interface SharkNetMessageListener {
4+
void messageReceived(byte[] message, CharSequence topic, CharSequence senderID, boolean verified, boolean encrypted);
5+
}

0 commit comments

Comments
 (0)