Skip to content

Commit 0d98999

Browse files
committed
Make security setting available at API level.
1 parent d15d097 commit 0d98999

17 files changed

+380
-84
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.sharksystem.asap;
2+
3+
import net.sharksystem.asap.protocol.ASAP_AssimilationPDU_1_0;
4+
5+
interface ASAPCommunicationSetting {
6+
boolean allowedToCreateChannel(ASAP_AssimilationPDU_1_0 asapAssimilationPDU);
7+
8+
/**
9+
* @param on if true - message must be encrypted
10+
*/
11+
void setSendEncryptedMessages(boolean on);
12+
13+
/**
14+
* @param on if true - message must be signed
15+
*/
16+
void setSendSignedMessages(boolean on);
17+
18+
}

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.sharksystem.asap.management.ASAPManagementStorageImpl;
55
import net.sharksystem.asap.protocol.*;
66
import net.sharksystem.asap.util.Log;
7+
import net.sharksystem.crypto.ASAPCommunicationCryptoSettings;
78

89
import java.io.IOException;
910
import java.io.InputStream;
@@ -18,7 +19,7 @@
1819
* @author thsc
1920
*/
2021
public abstract class ASAPEngine extends ASAPStorageImpl implements ASAPStorage, ASAPProtocolEngine, ASAPManagementStorage {
21-
private DefaultPeerSecurityAdministrator securityAdministrator = new DefaultPeerSecurityAdministrator();
22+
private DefaultSecurityAdministrator securityAdministrator = new DefaultSecurityAdministrator();
2223

2324
public static final String ANONYMOUS_OWNER = "anon";
2425
static String DEFAULT_OWNER = ANONYMOUS_OWNER;
@@ -56,6 +57,22 @@ private void saveStatus() throws IOException {
5657
}
5758
}
5859

60+
PermissionControl getPermissionControl() {
61+
return this.securityAdministrator;
62+
}
63+
64+
public ASAPEnginePermissionSettings getASAPEnginePermissionSettings() {
65+
return this.securityAdministrator;
66+
}
67+
68+
public ASAPCommunicationSetting getASAPCommunicationControl() {
69+
return this.securityAdministrator;
70+
}
71+
72+
public ASAPCommunicationCryptoSettings getASAPCommunicationCryptoSettings() {
73+
return this.securityAdministrator;
74+
}
75+
5976
@Override
6077
public ASAPChunkStorage getChunkStorage() {
6178
return this.chunkStorage;
@@ -374,12 +391,12 @@ public void handleASAPOffer(ASAP_OfferPDU_1_0 asapOffer, ASAP_1_0 protocol, Outp
374391
// }
375392
}
376393

377-
public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU, ASAP_1_0 protocol,
394+
public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimilationPDU, ASAP_1_0 protocol,
378395
InputStream is, OutputStream os, ASAPChunkReceivedListener listener)
379396
throws ASAPException, IOException {
380397

381-
String sender = asapAssimiliationPDU.getSender();
382-
int eraSender = asapAssimiliationPDU.getEra();
398+
String sender = asapAssimilationPDU.getSender();
399+
int eraSender = asapAssimilationPDU.getEra();
383400

384401
//<<<<<<<<<<<<<<<<<<debug
385402
StringBuilder b = new StringBuilder();
@@ -406,7 +423,7 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
406423

407424
try {
408425
// read URI
409-
String uri = asapAssimiliationPDU.getChannelUri();
426+
String uri = asapAssimilationPDU.getChannelUri();
410427

411428
// get local target for data to come
412429
ASAPChunk localChunk = null;
@@ -419,7 +436,7 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
419436
System.out.println(this.getLogStart()
420437
+ "asked to set up new channel: (uri/sender): " + uri + " | " + sender);
421438
// this channel is new to local peer - am I allowed to create it?
422-
if(!this.securityAdministrator.allowedToCreateChannel(asapAssimiliationPDU)) {
439+
if(!this.securityAdministrator.allowedToCreateChannel(asapAssimilationPDU)) {
423440
System.out.println(this.getLogStart()
424441
+ ".. not allowed .. TODO not yet implemented .. always set up");
425442

@@ -438,10 +455,10 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
438455
incomingChunk.copyMetaData(this.getChannel(uri));
439456
}
440457

441-
List<Integer> messageOffsets = asapAssimiliationPDU.getMessageOffsets();
458+
List<Integer> messageOffsets = asapAssimilationPDU.getMessageOffsets();
442459

443460
// iterate messages and stream into chunk
444-
InputStream protocolInputStream = asapAssimiliationPDU.getInputStream();
461+
InputStream protocolInputStream = asapAssimilationPDU.getInputStream();
445462
long offset = 0;
446463
for(long nextOffset : messageOffsets) {
447464
//<<<<<<<<<<<<<<<<<<debug
@@ -466,11 +483,11 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
466483
b.append("going to read last message: from offset ");
467484
b.append(offset);
468485
b.append(" to end of file - total length: ");
469-
b.append(asapAssimiliationPDU.getLength());
486+
b.append(asapAssimilationPDU.getLength());
470487
System.out.println(b.toString());
471488
//>>>>>>>>>>>>>>>>>>>debug
472489

473-
incomingChunk.addMessage(protocolInputStream, asapAssimiliationPDU.getLength() - offset);
490+
incomingChunk.addMessage(protocolInputStream, asapAssimilationPDU.getLength() - offset);
474491
if(!changed) { changed = true; this.contentChanged();}
475492

476493
// read all messages
@@ -664,7 +681,7 @@ private void sendChunks(CharSequence sender, String remotePeer, ASAPChunkStorage
664681
chunk.getOffsetList(),
665682
chunk.getMessageInputStream(),
666683
os,
667-
false);
684+
this.getASAPCommunicationCryptoSettings());
668685

669686
// remember sent
670687
chunk.deliveredTo(remotePeer);

src/net/sharksystem/asap/PeerSecuritySettings.java renamed to src/net/sharksystem/asap/ASAPEnginePermissionSettings.java

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

33
import java.io.IOException;
44

5-
interface PeerSecuritySettings {
5+
interface ASAPEnginePermissionSettings {
66
/**
77
* Engine can remember peers they encountered. It is assumed that those peers are kept with the local peer (not only
88
* in this engine)
@@ -16,14 +16,14 @@ interface PeerSecuritySettings {
1616
* @param on
1717
* @throws IOException
1818
*/
19-
void setEncryptedMessagesOnly(boolean on) throws IOException;
19+
void setReceivedMessagesMustBeEncrypted(boolean on) throws IOException;
2020

2121
/**
2222
* Engine would ignore all unsigned messages if set true.
2323
* @param on
2424
* @throws IOException
2525
*/
26-
void setSignedMessagesOnly(boolean on) throws IOException;
26+
void setReceivedMessagesMustBeSigned(boolean on) throws IOException;
2727

2828
/**
2929
* Define with what peers an engine is allowed to communicate

src/net/sharksystem/asap/ASAPPeerFS.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
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.ASAPCommunicationCryptoSettings;
78

89
import java.io.*;
910
import java.util.*;
1011

1112
public class ASAPPeerFS implements
12-
ASAPPeer, ASAPConnectionListener, ThreadFinishedListener/*, ASAPChunkReceivedListener */ {
13+
ASAPPeer, ASAPConnectionListener, ThreadFinishedListener, ASAPUndecryptableMessageHandler/*, ASAPChunkReceivedListener */ {
1314

1415
private static final String DEFAULT_ASAP_MANAGEMENT_ENGINE_ROOTFOLDER = "ASAPManagement";
1516
private final CharSequence rootFolderName;
@@ -121,6 +122,8 @@ private ASAPPeerFS(CharSequence owner, CharSequence rootFolderName, long maxExec
121122
}
122123
}
123124
}
125+
126+
System.out.println(this.getLogStart() + "SHOULD also set up engine " + FORMAT_UNDECRYPTABLE_MESSAGES);
124127
}
125128

126129
private void setupEngine(CharSequence folderName, CharSequence formatName) throws IOException, ASAPException {
@@ -268,12 +271,12 @@ public Set<CharSequence> getFormats() {
268271

269272
public ASAPConnection handleConnection(InputStream is, OutputStream os) {
270273
ASAPPersistentConnection asapConnection = new ASAPPersistentConnection(
271-
is, os, this, new ASAP_Modem_Impl(),
274+
is, os, this, new ASAP_Modem_Impl(), this,
272275
maxExecutionTime, this, this);
273276

274277
StringBuilder sb = new StringBuilder();
275278
sb.append(this.getLogStart());
276-
sb.append("handleConnection: ask any asapStorage to increment era.");
279+
sb.append("handleConnection");
277280
System.out.println(sb.toString());
278281

279282
// this.announceNewEra(); announce when connection is actually established
@@ -459,6 +462,15 @@ public ASAPConnection getASAPConnection(CharSequence recipient) {
459462
// ASAP management //
460463
////////////////////////////////////////////////////////////////////////////////////////////////////////////
461464

465+
private DefaultSecurityAdministrator defaultSecurityAdministrator = null;
466+
public ASAPCommunicationCryptoSettings getASAPCommunicationCryptoSettings() {
467+
if(this.defaultSecurityAdministrator == null) {
468+
this.defaultSecurityAdministrator = new DefaultSecurityAdministrator();
469+
}
470+
471+
return this.defaultSecurityAdministrator;
472+
}
473+
462474
public void pushInterests(OutputStream os) throws IOException, ASAPException {
463475
ASAP_1_0 protocol = new ASAP_Modem_Impl();
464476
/*
@@ -549,4 +561,19 @@ private Collection<ASAPEngine> getEngines() {
549561
private String getLogStart() {
550562
return this.getClass().getSimpleName() /* + "(" + this + ")" */ + "(" + this.getOwner() + "): ";
551563
}
564+
565+
//////////////////////////////// handle message this peer cannot decrypt
566+
@Override
567+
public void handleUndecryptableMessage(byte[] encryptedMessage, CharSequence receiver) {
568+
System.out.println(this.getLogStart() + "handle undecryptable messages from " + receiver);
569+
570+
try {
571+
ASAPEngine undecryptEngine =
572+
this.getASAPEngine(ASAPUndecryptableMessageHandler.FORMAT_UNDECRYPTABLE_MESSAGES);
573+
574+
undecryptEngine.add(URI_UNDECRYPTABLE_MESSAGES, encryptedMessage);
575+
} catch (IOException | ASAPException e) {
576+
System.out.println(this.getLogStart() + "cannot handle undecrypted messages - no engine present");
577+
}
578+
}
552579
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.sharksystem.asap;
2+
3+
import java.util.BitSet;
4+
5+
public interface ASAPUndecryptableMessageHandler {
6+
String FORMAT_UNDECRYPTABLE_MESSAGES = "asap/undecryptable";
7+
String URI_UNDECRYPTABLE_MESSAGES = "asap://undecryptable";
8+
/**
9+
* Peer can (and should) receive encrypted messages without being receiver. A peer is not able
10+
* to encrypt that message but could store and forward. That is what ASAP is about.
11+
*/
12+
void handleUndecryptableMessage(byte[] encryptedMessage, CharSequence receiver);
13+
}

src/net/sharksystem/asap/DefaultPeerSecurityAdministrator.java

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package net.sharksystem.asap;
2+
3+
import net.sharksystem.asap.protocol.ASAP_AssimilationPDU_1_0;
4+
import net.sharksystem.asap.protocol.ASAP_PDU_1_0;
5+
import net.sharksystem.crypto.ASAPCommunicationCryptoSettings;
6+
7+
import java.io.IOException;
8+
9+
public class DefaultSecurityAdministrator implements ASAPCommunicationSetting,
10+
ASAPEnginePermissionSettings, PermissionControl, ASAPCommunicationCryptoSettings {
11+
12+
private boolean encryptedMessagesOnly = false;
13+
private boolean signedMessagesOnly = false;
14+
private boolean sendEncrypted = false;
15+
private boolean sendSigned;
16+
17+
@Override
18+
public void setRememberEncounteredPeers(boolean on) throws IOException {
19+
20+
}
21+
22+
@Override
23+
public void setReceivedMessagesMustBeEncrypted(boolean on) throws IOException {
24+
this.encryptedMessagesOnly = on;
25+
}
26+
27+
@Override
28+
public void setReceivedMessagesMustBeSigned(boolean on) throws IOException {
29+
this.signedMessagesOnly = on;
30+
}
31+
32+
@Override
33+
public void setSetAllowedRemotePeers(AllowedRemotePeers safetyLevel) {
34+
35+
}
36+
37+
@Override
38+
public boolean setRevealEngineFormat(String peerName) {
39+
return false;
40+
}
41+
42+
@Override
43+
public boolean setSendOpenMessages(String peerName) {
44+
return false;
45+
}
46+
47+
@Override
48+
public boolean allowedToCreateChannel(ASAP_AssimilationPDU_1_0 asapAssimilationPDU) {
49+
return true; // it is a dummy
50+
}
51+
52+
@Override
53+
public void setSendEncryptedMessages(boolean on) {
54+
this.sendEncrypted = on;
55+
}
56+
57+
@Override
58+
public void setSendSignedMessages(boolean on) {
59+
this.sendSigned = on;
60+
}
61+
62+
@Override
63+
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+
67+
return true;
68+
}
69+
70+
@Override
71+
public boolean mustEncrypt() {
72+
return this.sendEncrypted;
73+
}
74+
75+
@Override
76+
public boolean mustSign() {
77+
return this.sendSigned;
78+
}
79+
}

src/net/sharksystem/asap/PeerSecurityAdministrator.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.sharksystem.asap;
2+
3+
import net.sharksystem.asap.protocol.ASAP_PDU_1_0;
4+
5+
public interface PermissionControl {
6+
boolean allowed2Process(ASAP_PDU_1_0 pdu);
7+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ public void finish() throws ASAPSecurityException {
207207
}
208208
}
209209

210+
public CharSequence getReceiver() {
211+
return this.recipient;
212+
}
213+
210214
////////////////////////////////// verify
211215
private class InputStreamCopy extends InputStream {
212216
private final InputStream is;

0 commit comments

Comments
 (0)