Skip to content

Commit c75348c

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

File tree

7 files changed

+204
-53
lines changed

7 files changed

+204
-53
lines changed

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
* @author thsc
2020
*/
2121
public abstract class ASAPEngine extends ASAPStorageImpl implements ASAPStorage, ASAPProtocolEngine, ASAPManagementStorage {
22-
private DefaultSecurityAdministrator securityAdministrator = new DefaultSecurityAdministrator();
22+
private DefaultSecurityAdministrator securityAdministrator;
23+
24+
public void setSecurityAdministrator(DefaultSecurityAdministrator securityAdministrator) {
25+
this.securityAdministrator = securityAdministrator;
26+
}
2327

2428
public static final String ANONYMOUS_OWNER = "anon";
2529
static String DEFAULT_OWNER = ANONYMOUS_OWNER;
@@ -533,6 +537,11 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimilationPDU, A
533537
}
534538

535539
private boolean hasSufficientCrypto(ASAP_PDU_1_0 pdu) {
540+
if(this.getCryptoControl() == null) {
541+
System.out.println(this.getLogStart() + "crypto control set allow anything");
542+
return true;
543+
}
544+
536545
boolean proceed = this.getCryptoControl().allowed2Process(pdu);
537546
if(!proceed) {
538547
System.out.println(this.getLogStart() + "no sufficient crypto: " + pdu);

src/net/sharksystem/asap/ASAPPeer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,6 @@ void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget
113113
throws IOException, ASAPException;
114114

115115
void setASAPBasicKeyStorage(ASAPBasicKeyStorage asapBasicKeyStorage);
116+
117+
ASAPCommunicationSetting getASAPCommunicationControl();
116118
}

src/net/sharksystem/asap/ASAPPeerFS.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ public class ASAPPeerFS implements
2020
private HashMap<CharSequence, EngineSetting> folderMap;
2121
private final long maxExecutionTime;
2222
private ASAPBasicKeyStorage asapBasicKeyStorage;
23+
private DefaultSecurityAdministrator defaultSecurityAdministrator = new DefaultSecurityAdministrator();
24+
25+
public ASAPCommunicationCryptoSettings getASAPCommunicationCryptoSettings() {
26+
return this.defaultSecurityAdministrator;
27+
}
28+
29+
public ASAPCommunicationSetting getASAPCommunicationControl() {
30+
return this.defaultSecurityAdministrator;
31+
}
2332

2433
public static ASAPPeer createASAPPeer(CharSequence owner, CharSequence rootFolder,
2534
long maxExecutionTime,
@@ -134,6 +143,8 @@ private void setupEngine(CharSequence folderName, CharSequence formatName) throw
134143
ASAPEngine asapEngine = ASAPEngineFS.getASAPStorage(this.getOwner().toString(),
135144
fileName, formatName);
136145

146+
asapEngine.setSecurityAdministrator(this.defaultSecurityAdministrator);
147+
137148
EngineSetting setting = new EngineSetting(
138149
fileName, // folder
139150
this.listener// listener
@@ -198,6 +209,7 @@ public ASAPEngine getEngineByFormat(CharSequence format) throws ASAPException, I
198209
asapEngine = ASAPEngineFS.getASAPEngine(owner.toString(), engineSetting.folder.toString(), format);
199210
engineSetting.setASAPEngine(asapEngine); // remember - keep that object
200211
}
212+
asapEngine.setSecurityAdministrator(this.defaultSecurityAdministrator);
201213
return asapEngine;
202214
}
203215

@@ -214,6 +226,8 @@ public ASAPEngine createEngineByFormat(CharSequence format) throws ASAPException
214226
ASAPEngine asapEngine = ASAPEngineFS.getASAPEngine(String.valueOf(this.getOwner()), folderName, format);
215227
this.folderMap.put(format, new EngineSetting(folderName, listener));
216228

229+
asapEngine.setSecurityAdministrator(this.defaultSecurityAdministrator);
230+
217231
return asapEngine;
218232
}
219233

@@ -465,15 +479,6 @@ public ASAPConnection getASAPConnection(CharSequence recipient) {
465479
// ASAP management //
466480
////////////////////////////////////////////////////////////////////////////////////////////////////////////
467481

468-
private DefaultSecurityAdministrator defaultSecurityAdministrator = null;
469-
public ASAPCommunicationCryptoSettings getASAPCommunicationCryptoSettings() {
470-
if(this.defaultSecurityAdministrator == null) {
471-
this.defaultSecurityAdministrator = new DefaultSecurityAdministrator();
472-
}
473-
474-
return this.defaultSecurityAdministrator;
475-
}
476-
477482
public void pushInterests(OutputStream os) throws IOException, ASAPException {
478483
ASAP_1_0 protocol = new ASAP_Modem_Impl();
479484
/*

src/net/sharksystem/asap/DefaultSecurityAdministrator.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
public class DefaultSecurityAdministrator implements ASAPCommunicationSetting,
1010
ASAPEnginePermissionSettings, CryptoControl, ASAPCommunicationCryptoSettings {
1111

12-
private boolean encryptedMessagesOnly = false;
13-
private boolean signedMessagesOnly = false;
12+
private boolean receivedMessageMustBeEncrypted = false;
13+
private boolean receivedMessagesMustBeSigned = false;
1414
private boolean sendEncrypted = false;
1515
private boolean sendSigned;
1616

@@ -21,12 +21,12 @@ public void setRememberEncounteredPeers(boolean on) throws IOException {
2121

2222
@Override
2323
public void setReceivedMessagesMustBeEncrypted(boolean on) throws IOException {
24-
this.encryptedMessagesOnly = on;
24+
this.receivedMessageMustBeEncrypted = on;
2525
}
2626

2727
@Override
2828
public void setReceivedMessagesMustBeSigned(boolean on) throws IOException {
29-
this.signedMessagesOnly = on;
29+
this.receivedMessagesMustBeSigned = on;
3030
}
3131

3232
@Override
@@ -61,12 +61,27 @@ public void setSendSignedMessages(boolean on) {
6161

6262
@Override
6363
public boolean allowed2Process(ASAP_PDU_1_0 pdu) {
64-
if(this.signedMessagesOnly && !pdu.signed()) return false;
65-
if(this.encryptedMessagesOnly && !pdu.encrypted()) return false;
66-
64+
if(this.receivedMessagesMustBeSigned && !pdu.signed()) {
65+
System.out.println(this);
66+
System.out.println(this.getLogStart() + "checked: " + pdu);
67+
System.out.println(this.getLogStart() + "not signed");
68+
return false;
69+
}
70+
if(this.receivedMessageMustBeEncrypted && !pdu.encrypted()) {
71+
System.out.println(this);
72+
System.out.println(this.getLogStart() + "checked: " + pdu);
73+
System.out.println(this.getLogStart() + "not encrypted");
74+
return false;
75+
}
76+
77+
System.out.println(this.getLogStart() + "ok");
6778
return true;
6879
}
6980

81+
private String getLogStart() {
82+
return this.getClass().getSimpleName() + ": ";
83+
}
84+
7085
@Override
7186
public boolean mustEncrypt() {
7287
return this.sendEncrypted;
@@ -76,4 +91,21 @@ public boolean mustEncrypt() {
7691
public boolean mustSign() {
7792
return this.sendSigned;
7893
}
94+
95+
/*
96+
private boolean encryptedMessagesOnly = false;
97+
private boolean signedMessagesOnly = false;
98+
private boolean sendEncrypted = false;
99+
private boolean sendSigned;
100+
101+
*/
102+
public String toString() {
103+
StringBuilder sb = new StringBuilder();
104+
sb.append(this.getLogStart());
105+
sb.append("recEncrypted: " + this.receivedMessageMustBeEncrypted);
106+
sb.append(" | recSigned: " + this.receivedMessagesMustBeSigned);
107+
sb.append(" | sendEncrypted: " + this.sendEncrypted);
108+
sb.append(" | sendSigned: " + this.sendSigned);
109+
return sb.toString();
110+
}
79111
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package net.sharksystem.asap;
2+
3+
import net.sharksystem.asap.util.ASAPPeerHandleConnectionThread;
4+
import net.sharksystem.cmdline.ExampleASAPChunkReceivedListener;
5+
import net.sharksystem.cmdline.TCPStream;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
import java.util.List;
11+
12+
public class CryptoTests {
13+
public static final String WORKING_SUB_DIRECTORY = "cryptoTests/";
14+
public static final String ALICE_PEER_NAME = "Alice";
15+
public static final String BOB_PEER_NAME = "Bob";
16+
public static final String CLARA_PEER_NAME = "Clara";
17+
public static final String APPNAME = "encryptedChat";
18+
public static final String CHAT_TOPIC = "topicA";
19+
public static final int EXAMPLE_PORT = 7070;
20+
public static final String EXAMPLE_MESSAGE_STRING = "Hi";
21+
22+
@Test
23+
public void noExchangeNotSigned() throws IOException, ASAPException, InterruptedException {
24+
ASAPEngineFS.removeFolder(WORKING_SUB_DIRECTORY); // clean previous version before
25+
26+
///// Prepare Alice
27+
String aliceFolder = WORKING_SUB_DIRECTORY + ALICE_PEER_NAME;
28+
29+
// ASAPChunkReceivedListener - an example
30+
ExampleASAPChunkReceivedListener aliceChunkListener = new ExampleASAPChunkReceivedListener(aliceFolder);
31+
32+
// setup alice peer
33+
ASAPPeer alicePeer = ASAPPeerFS.createASAPPeer(ALICE_PEER_NAME, aliceFolder, aliceChunkListener);
34+
35+
// setup chat on alice peer
36+
ASAPEngine aliceChatEngine = alicePeer.createEngineByFormat(APPNAME);
37+
// false is default but makes test more obvious
38+
aliceChatEngine.getASAPCommunicationControl().setSendEncryptedMessages(false);
39+
aliceChatEngine.getASAPCommunicationControl().setSendSignedMessages(false);
40+
41+
// create a message
42+
String messageAlice = EXAMPLE_MESSAGE_STRING;
43+
44+
// transform to bytes - there are more elaborate ways to produce a byte array of course
45+
byte[] messageBytes = messageAlice.getBytes();
46+
47+
// write a message - we are still offline
48+
aliceChatEngine.add(CHAT_TOPIC, messageBytes);
49+
50+
///// Prepare Bob
51+
String bobFolder = WORKING_SUB_DIRECTORY + BOB_PEER_NAME;
52+
53+
// ASAPChunkReceivedListener - an example
54+
ExampleASAPChunkReceivedListener bobChunkListener = new ExampleASAPChunkReceivedListener(bobFolder);
55+
56+
// setup bob peer
57+
ASAPPeer bobPeer = ASAPPeerFS.createASAPPeer(BOB_PEER_NAME, bobFolder, bobChunkListener);
58+
59+
// setup chat on alice peer
60+
ASAPEngine bobChatEngine = bobPeer.createEngineByFormat(APPNAME);
61+
// bob expects signed and encrypted what Alice not provides
62+
bobChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeEncrypted(true);
63+
bobChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeSigned(true);
64+
65+
/////////////// create a connection - in real apps it is presumably a bluetooth wifi direct etc. connection
66+
// TCPStream is a helper class for connection establishment
67+
TCPStream aliceStream = new TCPStream(EXAMPLE_PORT, true, "alice2bob");
68+
TCPStream bobStream = new TCPStream(EXAMPLE_PORT, false, "b2a");
69+
70+
// start tcp server or client and try to connect
71+
aliceStream.start();
72+
bobStream.start();
73+
74+
// wait until connection is established
75+
aliceStream.waitForConnection();
76+
bobStream.waitForConnection();
77+
//////////////// end of connection establishment - a simulation in some way - but real enough. It is real tcp.
78+
79+
// let both asap peers run an asap session
80+
ASAPPeerHandleConnectionThread aliceThread = new ASAPPeerHandleConnectionThread(alicePeer,
81+
aliceStream.getInputStream(), aliceStream.getOutputStream());
82+
83+
// alice is up and running in a thread
84+
aliceThread.start();
85+
86+
// run bob in this test thread
87+
bobPeer.handleConnection(bobStream.getInputStream(), bobStream.getOutputStream());
88+
89+
// at this point give both asap engines some time to run their asap session - then we check what happened.
90+
Thread.sleep(1000);
91+
92+
// we assume the asap session was performed
93+
94+
// bob chunk received listener must have received something
95+
List<ExampleASAPChunkReceivedListener.ASAPChunkReceivedParameters> receivedList =
96+
bobChunkListener.getReceivedList();
97+
Assert.assertTrue(receivedList.isEmpty());
98+
99+
}
100+
}

test/net/sharksystem/asap/V1TestSuite.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
@RunWith(Suite.class)
88
@Suite.SuiteClasses({
9-
PDUTests.class,
109
BatchprocessorTest.class,
1110
BasisMethodsTests.class,
1211
Point2PointTests.class,
1312
Point2PointTests2.class,
1413
UsageExamples.class,
15-
CreateNewChannelFromOutsideTest.class
14+
CreateNewChannelFromOutsideTest.class,
15+
PDUTests.class,
16+
CryptoTests.class
1617
})
1718
public class V1TestSuite {
1819

0 commit comments

Comments
 (0)