Skip to content

Commit e674f64

Browse files
committed
about adding cryptography
1 parent 37f6127 commit e674f64

File tree

13 files changed

+267
-209
lines changed

13 files changed

+267
-209
lines changed

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
378378
InputStream is, OutputStream os, ASAPChunkReceivedListener listener)
379379
throws ASAPException, IOException {
380380

381-
String sender = asapAssimiliationPDU.getPeer();
381+
String sender = asapAssimiliationPDU.getSender();
382382
int eraSender = asapAssimiliationPDU.getEra();
383383

384384
//<<<<<<<<<<<<<<<<<<debug
@@ -514,7 +514,7 @@ public void handleASAPInterest(ASAP_Interest_PDU_1_0 asapInterest, ASAP_1_0 prot
514514
throws ASAPException, IOException {
515515

516516
// get remote peer
517-
String peer = asapInterest.getPeer();
517+
String peer = asapInterest.getSender();
518518

519519
//<<<<<<<<<<<<<<<<<<debug
520520
StringBuilder b = new StringBuilder();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void run() {
217217
/////////////////////////////// process
218218
if(asappdu != null) {
219219
System.out.println(this.getLogStart() + "read valid pdu");
220-
this.setRemotePeer(asappdu.getPeer());
220+
this.setRemotePeer(asappdu.getSender());
221221

222222
try {
223223
this.executor = new ASAPPDUExecutor(asappdu,

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

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
* Descriptions of ASAP protocol data units and some constants
1313
*/
1414
public interface ASAP_1_0 {
15-
byte OFFER_CMD = 'O';
16-
byte INTEREST_CMD = 'I';
17-
byte ASSIMILATE_CMD = 'A';
18-
String ANY_FORMAT = "any_asap";
15+
int CMD_MASK = 0x6; // 0110
16+
byte OFFER_CMD = 0;
17+
byte INTEREST_CMD = 2;
18+
byte ASSIMILATE_CMD = 4;
19+
20+
int ENCRYPTED_MASK = 0x1; // 0001
21+
byte ENCRYPTED_CMD = 1;
22+
String ANY_FORMAT = "ASAP_ANY_FORMAT";
1923
String ASAP_MANAGEMENT_FORMAT = "asap/control";
2024
int ERA_NOT_DEFINED = -1;
2125

@@ -38,15 +42,15 @@ void offer(CharSequence peer, CharSequence format, CharSequence channel, int era
3842
throws IOException, ASAPException;
3943

4044
/**
41-
* @param peer identifies a peer - can be null
45+
* @param recipient identifies a peer - can be null
4246
* @param channel describes a channel (can be null)
4347
* @param format describes format - used to describe an application that can deal with transmitted data format.
4448
* @param os stream that PDU is to be sent
4549
* @param signed message is signed
4650
* @throws IOException exception during writing on stream
4751
* @throws ASAPException protocol exception: mandatory parameter missing, invalid combination of parameters, ..
4852
*/
49-
void offer(CharSequence peer, CharSequence format, CharSequence channel, OutputStream os, boolean signed)
53+
void offer(CharSequence recipient, CharSequence format, CharSequence channel, OutputStream os, boolean signed)
5054
throws IOException, ASAPException;
5155

5256
/*
@@ -55,8 +59,8 @@ void offer(CharSequence peer, CharSequence format, CharSequence channel, OutputS
5559
*/
5660

5761
/**
58-
* @param peer identifies a peer - can be null
59-
* @param sourcePeer wished source (authority) of information (optional, can be null)
62+
* @param sender identifies sender - can be null
63+
* @param recipient can be null - no restriction - any encountered peer will get it.
6064
* @param eraFrom lower limit of era range (-1 means undefined)
6165
* @param eraTo upper limit of era range (-1 means undefined)
6266
* @param channel whished / required channel (can be null)
@@ -66,13 +70,26 @@ void offer(CharSequence peer, CharSequence format, CharSequence channel, OutputS
6670
* @throws IOException exception during writing on stream
6771
* @throws ASAPException protocol exception: mandatory parameter missing, invalid combination of parameters, ..
6872
*/
69-
void interest(CharSequence peer, CharSequence sourcePeer, CharSequence format,
73+
void interest(CharSequence sender, CharSequence recipient, CharSequence format,
7074
CharSequence channel, int eraFrom, int eraTo,
7175
OutputStream os, boolean signed) throws IOException, ASAPException;
7276

77+
7378
/**
74-
* @param peer identifies a peer - can be null
75-
* @param sourcePeer wished source (authority) of information (optional, can be null)
79+
* @param sender identifies sender - can be null
80+
* @param recipient can be null - no restriction - any encountered peer will get it.
81+
* @param channel whished / required channel (can be null)
82+
* @param format describes format - used to describe an application that can deal with transmitted data format.
83+
* @param os stream that PDU is to be sent
84+
* @throws IOException exception during writing on stream
85+
* @throws ASAPException protocol exception: mandatory parameter missing, invalid combination of parameters, ..
86+
*/
87+
void interest(CharSequence sender, CharSequence recipient, CharSequence format,
88+
CharSequence channel, OutputStream os) throws IOException, ASAPException;
89+
90+
/**
91+
* @param sender identifies sender - can be null
92+
* @param recipient can be null - no restriction - any encountered peer will get it.
7693
* @param eraFrom lower limit of era range (-1 means undefined)
7794
* @param eraTo upper limit of era range (-1 means undefined)
7895
* @param channel whished / required channel (can be null)
@@ -85,22 +102,23 @@ void interest(CharSequence peer, CharSequence sourcePeer, CharSequence format,
85102
* @throws IOException
86103
* @throws ASAPException
87104
*/
88-
void interest(CharSequence peer, CharSequence sourcePeer, CharSequence format,
105+
void interest(CharSequence sender, CharSequence recipient, CharSequence format,
89106
CharSequence channel, int eraFrom, int eraTo,
90107
OutputStream os, boolean sign, boolean encrypted, boolean mustBeEncrypted)
91108
throws IOException, ASAPException, ASAPSecurityException;
92109

93110
/**
94-
* @param peer wished source (authority) of information
111+
* @param sender identifies sender - can be null
95112
* @param channel whished / required channel (can be null)
96113
* @param format describes format - used to describe an application that can deal with transmitted data format.
97114
* @param os stream that PDU is to be sent
98115
* @param signed message is signed
99116
* @throws IOException exception during writing on stream
100117
* @throws ASAPException protocol exception: mandatory parameter missing, invalid combination of parameters, ..
101118
*/
102-
void interest(CharSequence peer, CharSequence format, CharSequence sourcePeer, CharSequence channel,
103-
OutputStream os, boolean signed) throws IOException, ASAPException;
119+
void interest(CharSequence sender, CharSequence format, CharSequence sourcePeer,
120+
CharSequence channel, OutputStream os, boolean signed, boolean encrypted,
121+
boolean mustBeEncrypted) throws IOException, ASAPException;
104122

105123
/*
106124
ASSIMILATE: Peer (optional) issues data (mandatory) to a channel (mandatory) in a format (mandatory) of a
@@ -109,8 +127,8 @@ void interest(CharSequence peer, CharSequence format, CharSequence sourcePeer, C
109127

110128
/**
111129
*
112-
* @param peer sender (optional, can be null)
113-
* @param recipientPeer wished recipient (optional, can be null)
130+
* @param sender sender (optional, can be null)
131+
* @param recipient wished recipient (optional, can be null)
114132
* @param channelUri mandatory
115133
* @param format mandatory
116134
* @param offsets applications will probably store a number of messages in a data block. This (optional) list
@@ -121,14 +139,14 @@ void interest(CharSequence peer, CharSequence format, CharSequence sourcePeer, C
121139
* @throws IOException exception during writing on stream
122140
* @throws ASAPException protocol exception: mandatory parameter missing, invalid combination of parameters, ..
123141
*/
124-
void assimilate(CharSequence peer, CharSequence recipientPeer, CharSequence format, CharSequence channelUri, int era,
142+
void assimilate(CharSequence sender, CharSequence recipient, CharSequence format, CharSequence channelUri, int era,
125143
long length, List<Long> offsets, InputStream dataIS, OutputStream os, boolean signed)
126144
throws IOException, ASAPException;
127145

128146
/**
129147
*
130-
* @param peer sender (optional, can be null)
131-
* @param recipientPeer wished recipient (optional, can be null)
148+
* @param sender sender (optional, can be null)
149+
* @param recipient wished recipient (optional, can be null)
132150
* @param channel mandatory
133151
* @param format mandatory
134152
* @param offsets applications will probably store a number of messages in a data block. This (optional) list
@@ -139,7 +157,7 @@ void assimilate(CharSequence peer, CharSequence recipientPeer, CharSequence form
139157
* @throws IOException exception during writing on stream
140158
* @throws ASAPException protocol exception: mandatory parameter missing, invalid combination of parameters, ..
141159
*/
142-
void assimilate(CharSequence peer, CharSequence recipientPeer, CharSequence format, CharSequence channel, int era,
160+
void assimilate(CharSequence sender, CharSequence recipient, CharSequence format, CharSequence channel, int era,
143161
List<Long> offsets, byte[] data, OutputStream os, boolean signed)
144162
throws IOException, ASAPException;
145163

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import java.util.List;
77

88
public interface ASAP_AssimilationPDU_1_0 extends ASAP_PDU_1_0 {
9-
boolean recipientPeerSet();
10-
119
String getRecipientPeer();
1210

1311
byte[] getData() throws IOException;
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package net.sharksystem.asap.protocol;
22

33
public interface ASAP_Interest_PDU_1_0 extends ASAP_PDU_1_0 {
4-
boolean sourcePeerSet();
54
boolean eraFromSet();
65
boolean eraToSet();
7-
8-
String getSourcePeer();
96
int getEraFrom();
107
int getEraTo();
118
}

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

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,77 +19,93 @@ public ASAP_Modem_Impl() {
1919
public ASAP_Modem_Impl(ASAPSignAndEncryptionKeyStorage signAndEncryptionKeyStorage) {
2020
this.signAndEncryptionKeyStorage = signAndEncryptionKeyStorage;
2121
}
22-
// Character are transmitted as bytes: number of bytes (first byte), content following, 0 mean no content
23-
2422

23+
// Character are transmitted as bytes: number of bytes (first byte), content following, 0 mean no content
2524
/*
26-
general structure:
25+
general structure (asap message)
26+
a) no encryption
2727
CMD | FLAGS | ... specifics
28-
*/
28+
29+
b) encryption
30+
CMD | algorithm | recipient | encrypted message len | encrypted FLAGS | ... specifics
31+
*/
2932

3033
@Override
3134
public void offer(CharSequence peer, CharSequence format, CharSequence channel, int era,
3235
OutputStream os, boolean signed) throws IOException, ASAPException {
33-
34-
if(era < 0) throw new ASAPException("era must be a non-negative value: " + era);
3536
OfferPDU_Impl.sendPDU(peer, format, channel, era, os, signed);
3637
}
3738

3839
@Override
39-
public void offer(CharSequence peer, CharSequence format, CharSequence channel,
40+
public void offer(CharSequence recipient, CharSequence format, CharSequence channel,
4041
OutputStream os, boolean signed) throws IOException, ASAPException {
41-
this.offer(peer, format, channel, 0, os, signed);
42+
this.offer(recipient, format, channel, ERA_NOT_DEFINED, os, signed);
4243
}
4344

4445
@Override
45-
public void interest(CharSequence peer, CharSequence sourcePeer, CharSequence format,
46-
CharSequence channel, OutputStream os, boolean signed) throws IOException, ASAPException {
46+
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
47+
CharSequence channel, OutputStream os, boolean signed,
48+
boolean encryted, boolean mustBeEncrypted) throws IOException, ASAPException {
4749

48-
this.interest(peer, sourcePeer, format, channel, ERA_NOT_DEFINED, ERA_NOT_DEFINED, os, signed);
50+
this.interest(sender, recipient, format, channel, ERA_NOT_DEFINED, ERA_NOT_DEFINED, os,
51+
signed, encryted, mustBeEncrypted);
4952
}
5053

5154
@Override
52-
public void interest(CharSequence peer, CharSequence sourcePeer, CharSequence format,
55+
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
56+
CharSequence channel, OutputStream os) throws IOException, ASAPException {
57+
58+
this.interest(sender, recipient, format, channel, os, false, false, false);
59+
}
60+
61+
@Override
62+
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
5363
CharSequence channel, int eraFrom, int eraTo, OutputStream os, boolean signed)
5464
throws IOException, ASAPException {
5565

56-
this.interest(peer, sourcePeer, format, channel, eraFrom, eraTo, os,
66+
this.interest(sender, recipient, format, channel, eraFrom, eraTo, os,
5767
signed, false, false);
5868
}
5969

6070
@Override
61-
public void interest(CharSequence peer, CharSequence sourcePeer, CharSequence format,
71+
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
6272
CharSequence channel, int eraFrom, int eraTo, OutputStream os, boolean signed,
6373
boolean encryted, boolean mustBeEncrypted)
6474
throws IOException, ASAPException, ASAPSecurityException {
6575

66-
InterestPDU_Impl.sendPDU(peer, sourcePeer, format, channel, eraFrom, eraTo, os,
76+
InterestPDU_Impl.sendPDU(sender, recipient, format, channel, eraFrom, eraTo, os,
6777
signed, encryted, mustBeEncrypted, this.signAndEncryptionKeyStorage);
6878
}
6979

7080
@Override
71-
public void assimilate(CharSequence peer, CharSequence recipientPeer, CharSequence format,
81+
public void assimilate(CharSequence sender, CharSequence recipient, CharSequence format,
7282
CharSequence channel, int era, long length, List<Long> offsets, InputStream dataIS,
7383
OutputStream os, boolean signed) throws IOException, ASAPException {
7484

75-
AssimilationPDU_Impl.sendPDU(peer, recipientPeer, format, channel, era, length, offsets, dataIS, os, signed);
85+
AssimilationPDU_Impl.sendPDU(sender, recipient, format, channel, era, length, offsets, dataIS, os, signed);
7686
}
7787

7888
@Override
79-
public void assimilate(CharSequence peer, CharSequence recipientPeer, CharSequence format,
89+
public void assimilate(CharSequence sender, CharSequence recipient, CharSequence format,
8090
CharSequence channel, int era, List<Long> offsets, byte[] data,
8191
OutputStream os, boolean signed) throws IOException, ASAPException {
8292

8393
if(data == null || data.length == 0) throw new ASAPException("data must not be null");
8494
if(era < 0) throw new ASAPException("era must be a non-negative value: " + era);
8595

86-
this.assimilate(peer, recipientPeer, format, channel, era, data.length, offsets,
96+
this.assimilate(sender, recipient, format, channel, era, data.length, offsets,
8797
new ByteArrayInputStream(data), os, signed);
8898
}
8999

90100
@Override
91101
public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
92102
byte cmd = PDU_Impl.readByte(is);
103+
104+
// encrypted?
105+
boolean encrypted = (cmd & ENCRYPTED_MASK) != 0;
106+
// remove encrypted flag anyway
107+
cmd = (byte)(cmd & CMD_MASK);
108+
93109
int flagsInt = PDU_Impl.readByte(is);
94110

95111
ASAP_PDU_1_0 pdu = null;

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ public interface ASAP_PDU_1_0 {
77
byte getCommand();
88

99
/**
10-
* @return ID of sender of this message
10+
* @return ID of sender of this message - optional
1111
*/
12-
String getPeer();
12+
String getSender();
13+
14+
/**
15+
* @return ID of recipient of this message - optional
16+
*/
17+
String getRecipient();
1318

1419
/**
1520
* @return format A URI style is strongly recommended.
@@ -33,9 +38,14 @@ public interface ASAP_PDU_1_0 {
3338
int getEra();
3439

3540
/**
36-
* @return a flag that indicates whether the optional peer parameter was transmitted
41+
* @return a flag that indicates whether the optional sender parameter was transmitted
42+
*/
43+
boolean senderSet();
44+
45+
/**
46+
* @return a flag that indicates whether the optional recipient parameter was transmitted
3747
*/
38-
boolean peerSet();
48+
boolean recipientSet();
3949
/**
4050
* @return a flag that indicates whether the optional channel parameter was transmitted
4151
*/

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public AssimilationPDU_Impl(int flagsInt, InputStream is) throws IOException, AS
2424

2525
evaluateFlags(flagsInt);
2626

27-
if(this.peerSet()) { this.readPeer(is); }
28-
if(this.recipientPeerSet()) { this.readRecipientPeer(is); }
27+
if(this.senderSet()) { this.readSender(is); }
28+
if(this.recipientSet()) { this.readRecipientPeer(is); }
2929
this.readFormat(is);
3030
if(this.channelSet()) { this.readChannel(is); }
3131
if(this.eraSet()) { this.readEra(is); }
@@ -56,7 +56,7 @@ static void sendPDU(CharSequence peer, CharSequence recipientPeer, CharSequence
5656

5757
// create parameter bytes
5858
int flags = 0;
59-
flags = PDU_Impl.setFlag(peer, flags, PEER_BIT_POSITION);
59+
flags = PDU_Impl.setFlag(peer, flags, SENDER_BIT_POSITION);
6060
flags = PDU_Impl.setFlag(recipientPeer, flags, RECIPIENT_PEER_BIT_POSITION);
6161
flags = PDU_Impl.setFlag(channel, flags, CHANNEL_BIT_POSITION);
6262
flags = PDU_Impl.setFlag(era, flags, ERA_BIT_POSITION);

0 commit comments

Comments
 (0)