Skip to content

Commit f8a8d14

Browse files
committed
Still no disimprovement - keep it.
1 parent 0d98999 commit f8a8d14

File tree

10 files changed

+81
-60
lines changed

10 files changed

+81
-60
lines changed

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private void saveStatus() throws IOException {
5757
}
5858
}
5959

60-
PermissionControl getPermissionControl() {
60+
CryptoControl getCryptoControl() {
6161
return this.securityAdministrator;
6262
}
6363

@@ -370,6 +370,8 @@ private String getLogStart() {
370370
public void handleASAPOffer(ASAP_OfferPDU_1_0 asapOffer, ASAP_1_0 protocol, OutputStream os)
371371
throws ASAPException, IOException {
372372

373+
if(!hasSufficientCrypto(asapOffer)) return;
374+
373375
/*
374376
if(this.isASAPManagementMessage(asapOffer)) {
375377
//<<<<<<<<<<<<<<<<<<debug
@@ -395,6 +397,9 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimilationPDU, A
395397
InputStream is, OutputStream os, ASAPChunkReceivedListener listener)
396398
throws ASAPException, IOException {
397399

400+
// before we start - lets crypto
401+
if(!hasSufficientCrypto(asapAssimilationPDU)) return;
402+
398403
String sender = asapAssimilationPDU.getSender();
399404
int eraSender = asapAssimilationPDU.getEra();
400405

@@ -527,9 +532,21 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimilationPDU, A
527532
}
528533
}
529534

535+
private boolean hasSufficientCrypto(ASAP_PDU_1_0 pdu) {
536+
boolean proceed = this.getCryptoControl().allowed2Process(pdu);
537+
if(!proceed) {
538+
System.out.println(this.getLogStart() + "no sufficient crypto: " + pdu);
539+
}
540+
541+
return proceed;
542+
}
543+
530544
public void handleASAPInterest(ASAP_Interest_PDU_1_0 asapInterest, ASAP_1_0 protocol, OutputStream os)
531545
throws ASAPException, IOException {
532546

547+
// before we start - lets crypto
548+
if(!hasSufficientCrypto(asapInterest)) return;
549+
533550
// get remote peer
534551
String peer = asapInterest.getSender();
535552

src/net/sharksystem/asap/ASAPPeer.java

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

33
import net.sharksystem.asap.protocol.*;
4+
import net.sharksystem.crypto.ASAPBasicKeyStorage;
45

56
import java.io.IOException;
67
import java.io.InputStream;
@@ -110,4 +111,6 @@ void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget
110111

111112
void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget, byte[] messageAsBytes)
112113
throws IOException, ASAPException;
114+
115+
void setASAPBasicKeyStorage(ASAPBasicKeyStorage asapBasicKeyStorage);
113116
}

src/net/sharksystem/asap/ASAPPeerFS.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.sharksystem.asap.protocol.*;
55
import net.sharksystem.asap.util.Helper;
66
import net.sharksystem.asap.util.Log;
7+
import net.sharksystem.crypto.ASAPBasicKeyStorage;
78
import net.sharksystem.crypto.ASAPCommunicationCryptoSettings;
89

910
import java.io.*;
@@ -18,6 +19,7 @@ public class ASAPPeerFS implements
1819
private CharSequence owner;
1920
private HashMap<CharSequence, EngineSetting> folderMap;
2021
private final long maxExecutionTime;
22+
private ASAPBasicKeyStorage asapBasicKeyStorage;
2123

2224
public static ASAPPeer createASAPPeer(CharSequence owner, CharSequence rootFolder,
2325
long maxExecutionTime,
@@ -271,7 +273,8 @@ public Set<CharSequence> getFormats() {
271273

272274
public ASAPConnection handleConnection(InputStream is, OutputStream os) {
273275
ASAPPersistentConnection asapConnection = new ASAPPersistentConnection(
274-
is, os, this, new ASAP_Modem_Impl(), this,
276+
is, os, this, new ASAP_Modem_Impl(),
277+
this, this.asapBasicKeyStorage,
275278
maxExecutionTime, this, this);
276279

277280
StringBuilder sb = new StringBuilder();
@@ -488,7 +491,11 @@ public void pushInterests(OutputStream os) throws IOException, ASAPException {
488491
try {
489492
ASAPEngine managementEngine = this.getEngineByFormat(ASAP_1_0.ASAP_MANAGEMENT_FORMAT);
490493
System.out.println(this.getLogStart() + "send interest for app/format: " + ASAP_1_0.ASAP_MANAGEMENT_FORMAT);
491-
protocol.interest(this.owner, null, ASAP_1_0.ASAP_MANAGEMENT_FORMAT,null, -1, -1, os, false);
494+
protocol.interest(this.owner, null,
495+
ASAP_1_0.ASAP_MANAGEMENT_FORMAT,
496+
null, ASAP_1_0.ERA_NOT_DEFINED, ASAP_1_0.ERA_NOT_DEFINED,
497+
os,
498+
this.getASAPCommunicationCryptoSettings());
492499
}
493500
catch(Exception e) {
494501
// ignore - engine does not exist
@@ -497,7 +504,14 @@ public void pushInterests(OutputStream os) throws IOException, ASAPException {
497504
for(CharSequence format : this.folderMap.keySet()) {
498505
if(format.toString().equalsIgnoreCase(ASAP_1_0.ASAP_MANAGEMENT_FORMAT)) continue; // already sent
499506
System.out.println(this.getLogStart() + "send interest for app/format: " + format);
500-
protocol.interest(this.owner, null, format,null, -1, -1, os, false);
507+
try {
508+
protocol.interest(this.owner, null,
509+
format, null, ASAP_1_0.ERA_NOT_DEFINED, ASAP_1_0.ERA_NOT_DEFINED,
510+
os, this.getASAPCommunicationCryptoSettings());
511+
}
512+
catch(ASAPSecurityException e) {
513+
// give next engine a try
514+
}
501515
}
502516
}
503517

@@ -530,6 +544,11 @@ public void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence ur
530544
this.sendOnlineASAPAssimilateMessage(format, urlTarget, null, messageAsBytes, ASAP.INITIAL_ERA);
531545
}
532546

547+
@Override
548+
public void setASAPBasicKeyStorage(ASAPBasicKeyStorage asapBasicKeyStorage) {
549+
this.asapBasicKeyStorage = asapBasicKeyStorage;
550+
}
551+
533552
public void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget,
534553
Set<CharSequence> recipients, byte[] messageAsBytes) throws IOException, ASAPException {
535554

src/net/sharksystem/asap/PermissionControl.java renamed to src/net/sharksystem/asap/CryptoControl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
import net.sharksystem.asap.protocol.ASAP_PDU_1_0;
44

5-
public interface PermissionControl {
5+
public interface CryptoControl {
66
boolean allowed2Process(ASAP_PDU_1_0 pdu);
77
}

src/net/sharksystem/asap/DefaultSecurityAdministrator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.io.IOException;
88

99
public class DefaultSecurityAdministrator implements ASAPCommunicationSetting,
10-
ASAPEnginePermissionSettings, PermissionControl, ASAPCommunicationCryptoSettings {
10+
ASAPEnginePermissionSettings, CryptoControl, ASAPCommunicationCryptoSettings {
1111

1212
private boolean encryptedMessagesOnly = false;
1313
private boolean signedMessagesOnly = false;

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

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

33
import net.sharksystem.asap.*;
44
import net.sharksystem.asap.util.Log;
5+
import net.sharksystem.crypto.ASAPBasicKeyStorage;
56

67
import java.io.IOException;
78
import java.io.InputStream;
@@ -25,10 +26,11 @@ public class ASAPPersistentConnection extends ASAPProtocolEngine
2526

2627
public ASAPPersistentConnection(InputStream is, OutputStream os, ASAPPeer asapPeer,
2728
ASAP_1_0 protocol, ASAPUndecryptableMessageHandler unencryptableMessageHandler,
29+
ASAPBasicKeyStorage asapBasicKeyStorage,
2830
long maxExecutionTime, ASAPConnectionListener asapConnectionListener,
2931
ThreadFinishedListener threadFinishedListener) {
3032

31-
super(is, os, protocol, unencryptableMessageHandler);
33+
super(is, os, protocol, unencryptableMessageHandler, asapBasicKeyStorage);
3234

3335
this.asapPeer = asapPeer;
3436
this.maxExecutionTime = maxExecutionTime;
@@ -174,7 +176,7 @@ private synchronized void checkRunningOnlineMessageSender() {
174176
}
175177

176178
public void run() {
177-
ASAP_1_0 protocol = new ASAP_Modem_Impl(this.unencryptableMessageHandler);
179+
ASAP_1_0 protocol = new ASAP_Modem_Impl(this.asapBasicKeyStorage, this.undecryptableMessageHandler);
178180

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

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

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

33
import net.sharksystem.asap.ASAPException;
44
import net.sharksystem.asap.ASAPUndecryptableMessageHandler;
5+
import net.sharksystem.crypto.ASAPBasicKeyStorage;
56

67
import java.io.IOException;
78
import java.io.InputStream;
@@ -13,14 +14,17 @@ public abstract class ASAPProtocolEngine {
1314
protected final ASAP_1_0 protocol;
1415
protected final InputStream is;
1516
protected final OutputStream os;
16-
protected final ASAPUndecryptableMessageHandler unencryptableMessageHandler;
17+
protected final ASAPUndecryptableMessageHandler undecryptableMessageHandler;
18+
protected final ASAPBasicKeyStorage asapBasicKeyStorage;
1719

1820
public ASAPProtocolEngine(InputStream is, OutputStream os, ASAP_1_0 protocol,
19-
ASAPUndecryptableMessageHandler unencryptableMessageHandler) {
21+
ASAPUndecryptableMessageHandler undecryptableMessageHandler,
22+
ASAPBasicKeyStorage asapBasicKeyStorage) {
2023
this.is = is;
2124
this.os = os;
2225
this.protocol = protocol;
23-
this.unencryptableMessageHandler = unencryptableMessageHandler;
26+
this.undecryptableMessageHandler = undecryptableMessageHandler;
27+
this.asapBasicKeyStorage = asapBasicKeyStorage;
2428
}
2529

2630
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ void interest(CharSequence sender, CharSequence recipient, CharSequence format,
7575
CharSequence channel, int eraFrom, int eraTo,
7676
OutputStream os, boolean signed) throws IOException, ASAPException;
7777

78+
void interest(CharSequence sender, CharSequence recipient, CharSequence format,
79+
CharSequence channel, int eraFrom, int eraTo,
80+
OutputStream os, ASAPCommunicationCryptoSettings cryptoSettings) throws IOException, ASAPException;
7881

7982
/**
8083
* @param sender identifies sender - can be null

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,24 @@ public void interest(CharSequence sender, CharSequence recipient, CharSequence f
7474

7575
@Override
7676
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
77-
CharSequence channel, int eraFrom, int eraTo, OutputStream os, boolean signed)
77+
CharSequence channel, int eraFrom, int eraTo, OutputStream os, boolean signed)
7878
throws IOException, ASAPException {
7979

8080
this.interest(sender, recipient, format, channel, eraFrom, eraTo, os,
8181
signed, false);
8282
}
8383

84+
@Override
85+
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
86+
CharSequence channel, int eraFrom, int eraTo, OutputStream os,
87+
ASAPCommunicationCryptoSettings cryptoSettings)
88+
throws IOException, ASAPException {
89+
90+
this.interest(sender, recipient, format, channel, eraFrom, eraTo, os,
91+
cryptoSettings.mustSign(), cryptoSettings.mustEncrypt());
92+
}
93+
94+
8495
@Override
8596
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
8697
CharSequence channel, int eraFrom, int eraTo, OutputStream os, boolean signed,

test/net/sharksystem/asap/Workbench.java

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.sharksystem.cmdline.ExampleASAPChunkReceivedListener;
66
import net.sharksystem.cmdline.TCPStream;
77
import org.junit.Assert;
8+
import org.junit.Test;
89

910
import java.io.IOException;
1011
import java.util.Iterator;
@@ -21,8 +22,8 @@ public class Workbench {
2122
public static final int EXAMPLE_PORT = 7070;
2223
public static final String EXAMPLE_MESSAGE_STRING = "Hi";
2324

24-
// TODO
25-
public void routeEncryptedMessageTest() throws IOException, ASAPException, InterruptedException {
25+
@Test
26+
public void noExchangeNotSigned() throws IOException, ASAPException, InterruptedException {
2627
ASAPEngineFS.removeFolder(WORKING_SUB_DIRECTORY); // clean previous version before
2728

2829
///// Prepare Alice
@@ -36,10 +37,9 @@ public void routeEncryptedMessageTest() throws IOException, ASAPException, Inter
3637

3738
// setup chat on alice peer
3839
ASAPEngine aliceChatEngine = alicePeer.createEngineByFormat(APPNAME);
39-
aliceChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeEncrypted(true);
40-
aliceChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeSigned(true);
41-
aliceChatEngine.getASAPCommunicationControl().setSendEncryptedMessages(true);
42-
aliceChatEngine.getASAPCommunicationControl().setSendSignedMessages(true);
40+
// false is default but makes test more obvious
41+
aliceChatEngine.getASAPCommunicationControl().setSendEncryptedMessages(false);
42+
aliceChatEngine.getASAPCommunicationControl().setSendSignedMessages(false);
4343

4444
// create a message
4545
String messageAlice = EXAMPLE_MESSAGE_STRING;
@@ -61,10 +61,9 @@ public void routeEncryptedMessageTest() throws IOException, ASAPException, Inter
6161

6262
// setup chat on alice peer
6363
ASAPEngine bobChatEngine = bobPeer.createEngineByFormat(APPNAME);
64-
aliceChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeEncrypted(true);
65-
aliceChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeSigned(true);
66-
aliceChatEngine.getASAPCommunicationControl().setSendEncryptedMessages(true);
67-
aliceChatEngine.getASAPCommunicationControl().setSendSignedMessages(true);
64+
// bob expects signed and encrypted what Alice not provides
65+
bobChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeEncrypted(true);
66+
bobChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeSigned(true);
6867

6968
/////////////// create a connection - in real apps it is presumably a bluetooth wifi direct etc. connection
7069
// TCPStream is a helper class for connection establishment
@@ -98,44 +97,7 @@ public void routeEncryptedMessageTest() throws IOException, ASAPException, Inter
9897
// bob chunk received listener must have received something
9998
List<ExampleASAPChunkReceivedListener.ASAPChunkReceivedParameters> receivedList =
10099
bobChunkListener.getReceivedList();
100+
Assert.assertTrue(receivedList.isEmpty());
101101

102-
Assert.assertNotNull(receivedList);
103-
Assert.assertFalse(receivedList.isEmpty());
104-
105-
// there must be a single entry - get it
106-
Assert.assertTrue(receivedList.size() == 1);
107-
ExampleASAPChunkReceivedListener.ASAPChunkReceivedParameters parameters = receivedList.get(0);
108-
109-
// get chunk storage
110-
ASAPChunkStorage receivedChunkStorage = bobChatEngine.getReceivedChunksStorage(parameters.getSender());
111-
112-
// get chunk
113-
ASAPChunk chunk = receivedChunkStorage.getChunk(parameters.getUri(), parameters.getEra());
114-
115-
Iterator<byte[]> messages = chunk.getMessages();
116-
117-
// there must a one and only one
118-
byte[] messageBytesReceived = messages.next();
119-
120-
// convert to String
121-
String receivedMessage = new String(messageBytesReceived);
122-
123-
System.out.println(bobChatEngine.getOwner() + " received a message: " + receivedMessage);
124-
125-
Assert.assertTrue(receivedMessage.equals(EXAMPLE_MESSAGE_STRING));
126-
127-
// make it a bit easier with a helper class
128-
ASAPMessages receivedMessages =
129-
Helper.getMessagesByChunkReceivedInfos(parameters.getFormat(), parameters.getSender(),
130-
parameters.getUri(),
131-
bobFolder, // peers' root directory!
132-
parameters.getEra());
133-
134-
Iterator<byte[]> msgInter = receivedMessages.getMessages();
135-
while(msgInter.hasNext()) {
136-
byte[] msgBytes = msgInter.next();
137-
String msg = new String(msgBytes);
138-
System.out.println("message received: " + msg);
139-
}
140102
}
141103
}

0 commit comments

Comments
 (0)