Skip to content

Commit bbe312a

Browse files
committed
about adding cryptography
1 parent e674f64 commit bbe312a

File tree

12 files changed

+214
-122
lines changed

12 files changed

+214
-122
lines changed

src/net/sharksystem/asap/ASAPException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ public ASAPException() {
1313
public ASAPException(String message) {
1414
super(message);
1515
}
16+
17+
public ASAPException(String message, Throwable cause) {
18+
super(message, cause);
19+
}
1620

1721
}

src/net/sharksystem/asap/ASAPSecurityException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ public ASAPSecurityException() {
77
public ASAPSecurityException(String message) {
88
super(message);
99
}
10+
public ASAPSecurityException(String message, Throwable cause) {
11+
super(message, cause);
12+
}
1013
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public interface ASAPSignAndEncryptionKeyStorage {
1717
*
1818
* @param subjectID
1919
* @return public key of recipient - to encrypt
20+
* @throws ASAPSecurityException if key cannot be found
2021
*/
2122
PublicKey getPublicKey(CharSequence subjectID) throws ASAPSecurityException;
2223
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,12 @@ void interest(CharSequence sender, CharSequence recipient, CharSequence format,
9797
* @param os stream that PDU is to be sent
9898
* @param sign sign message when sending
9999
* @param encrypted encrypt method - of possible
100-
* @param mustBeEncrypted encrypt can be set true. There could not bot a public key of receipient. If hit flag ist
101-
* set true - an exception is thrown. Otherwise. message is sent unencrypted.
102100
* @throws IOException
103101
* @throws ASAPException
104102
*/
105103
void interest(CharSequence sender, CharSequence recipient, CharSequence format,
106104
CharSequence channel, int eraFrom, int eraTo,
107-
OutputStream os, boolean sign, boolean encrypted, boolean mustBeEncrypted)
105+
OutputStream os, boolean sign, boolean encrypted)
108106
throws IOException, ASAPException, ASAPSecurityException;
109107

110108
/**
@@ -117,8 +115,8 @@ void interest(CharSequence sender, CharSequence recipient, CharSequence format,
117115
* @throws ASAPException protocol exception: mandatory parameter missing, invalid combination of parameters, ..
118116
*/
119117
void interest(CharSequence sender, CharSequence format, CharSequence sourcePeer,
120-
CharSequence channel, OutputStream os, boolean signed, boolean encrypted,
121-
boolean mustBeEncrypted) throws IOException, ASAPException;
118+
CharSequence channel, OutputStream os, boolean signed, boolean encrypted)
119+
throws IOException, ASAPException;
122120

123121
/*
124122
ASSIMILATE: Peer (optional) issues data (mandatory) to a channel (mandatory) in a format (mandatory) of a

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ public void offer(CharSequence recipient, CharSequence format, CharSequence chan
4545
@Override
4646
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
4747
CharSequence channel, OutputStream os, boolean signed,
48-
boolean encryted, boolean mustBeEncrypted) throws IOException, ASAPException {
48+
boolean encryted) throws IOException, ASAPException {
4949

5050
this.interest(sender, recipient, format, channel, ERA_NOT_DEFINED, ERA_NOT_DEFINED, os,
51-
signed, encryted, mustBeEncrypted);
51+
signed, encryted);
5252
}
5353

5454
@Override
5555
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
5656
CharSequence channel, OutputStream os) throws IOException, ASAPException {
5757

58-
this.interest(sender, recipient, format, channel, os, false, false, false);
58+
this.interest(sender, recipient, format, channel, os, false, false);
5959
}
6060

6161
@Override
@@ -64,17 +64,26 @@ public void interest(CharSequence sender, CharSequence recipient, CharSequence f
6464
throws IOException, ASAPException {
6565

6666
this.interest(sender, recipient, format, channel, eraFrom, eraTo, os,
67-
signed, false, false);
67+
signed, false);
6868
}
6969

7070
@Override
7171
public void interest(CharSequence sender, CharSequence recipient, CharSequence format,
7272
CharSequence channel, int eraFrom, int eraTo, OutputStream os, boolean signed,
73-
boolean encryted, boolean mustBeEncrypted)
73+
boolean encrypted)
7474
throws IOException, ASAPException, ASAPSecurityException {
7575

76-
InterestPDU_Impl.sendPDU(sender, recipient, format, channel, eraFrom, eraTo, os,
77-
signed, encryted, mustBeEncrypted, this.signAndEncryptionKeyStorage);
76+
// prepare encryption and signing if required
77+
CryptoSession cryptoSession = new CryptoSession(ASAP_1_0.INTEREST_CMD,
78+
os, signed, encrypted, recipient, this.signAndEncryptionKeyStorage);
79+
80+
cryptoSession.sendHeader();
81+
82+
InterestPDU_Impl.sendPDUWithoutCmd(sender, recipient, format, channel, eraFrom, eraTo,
83+
cryptoSession.getOutputStream());
84+
85+
// finish crypto session - if any
86+
cryptoSession.finish();
7887
}
7988

8089
@Override
@@ -103,17 +112,17 @@ public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
103112

104113
// encrypted?
105114
boolean encrypted = (cmd & ENCRYPTED_MASK) != 0;
106-
// remove encrypted flag anyway
115+
// remove encrypted flag
107116
cmd = (byte)(cmd & CMD_MASK);
108117

109118
int flagsInt = PDU_Impl.readByte(is);
110119

111120
ASAP_PDU_1_0 pdu = null;
112121

113122
switch(cmd) {
114-
case ASAP_1_0.OFFER_CMD: pdu = new OfferPDU_Impl(flagsInt, is); break;
115-
case ASAP_1_0.INTEREST_CMD: pdu = new InterestPDU_Impl(flagsInt, is); break;
116-
case ASAP_1_0.ASSIMILATE_CMD: pdu = new AssimilationPDU_Impl(flagsInt, is); break;
123+
case ASAP_1_0.OFFER_CMD: pdu = new OfferPDU_Impl(flagsInt, encrypted, is); break;
124+
case ASAP_1_0.INTEREST_CMD: pdu = new InterestPDU_Impl(flagsInt, encrypted, is); break;
125+
case ASAP_1_0.ASSIMILATE_CMD: pdu = new AssimilationPDU_Impl(flagsInt, encrypted, is); break;
117126
default: throw new ASAPException("unknown command: " + cmd);
118127
}
119128

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ public interface ASAP_PDU_1_0 {
4242
*/
4343
boolean senderSet();
4444

45+
/**
46+
* @return true if received message was encrypted and could obviously be encrypted
47+
*/
48+
boolean encrypted();
49+
50+
/**
51+
* @return true if received message was signed
52+
*/
53+
boolean signed();
54+
55+
/**
56+
* @return true if received message was signed and signature could be verified
57+
*/
58+
boolean verified();
59+
4560
/**
4661
* @return a flag that indicates whether the optional recipient parameter was transmitted
4762
*/

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class AssimilationPDU_Impl extends PDU_Impl implements ASAP_AssimilationPDU_1_0
1919

2020
// PDU: CMD | FLAGS | PEER | RECIPIENT | FORMAT | CHANNEL | ERA | OFFSETS | LENGTH | DATA
2121

22-
public AssimilationPDU_Impl(int flagsInt, InputStream is) throws IOException, ASAPException {
23-
super(ASAP_1_0.ASSIMILATE_CMD);
22+
public AssimilationPDU_Impl(int flagsInt, boolean encrypted, InputStream is) throws IOException, ASAPException {
23+
super(ASAP_1_0.ASSIMILATE_CMD, encrypted);
2424

2525
evaluateFlags(flagsInt);
2626

@@ -51,7 +51,7 @@ static void sendPDU(CharSequence peer, CharSequence recipientPeer, CharSequence
5151
// first: check protocol errors
5252
PDU_Impl.checkValidEra(era);
5353
PDU_Impl.checkValidFormat(format);
54-
PDU_Impl.checkValidSign(peer, signed);
54+
// PDU_Impl.checkValidSign(peer, signed);
5555
PDU_Impl.checkValidStream(os);
5656

5757
// create parameter bytes
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package net.sharksystem.asap.protocol;
2+
3+
import net.sharksystem.asap.ASAPSecurityException;
4+
5+
import javax.crypto.BadPaddingException;
6+
import javax.crypto.Cipher;
7+
import javax.crypto.IllegalBlockSizeException;
8+
import javax.crypto.NoSuchPaddingException;
9+
import java.io.IOException;
10+
import java.io.OutputStream;
11+
import java.security.InvalidKeyException;
12+
import java.security.NoSuchAlgorithmException;
13+
import java.security.PublicKey;
14+
15+
class CryptoSession {
16+
private byte cmd;
17+
private final OutputStream os;
18+
19+
CryptoSession(byte cmd, OutputStream os, boolean sign, boolean encrypted,
20+
CharSequence recipient,
21+
ASAPSignAndEncryptionKeyStorage keyStorage)
22+
throws ASAPSecurityException {
23+
24+
this.cmd = cmd;
25+
this.os = os;
26+
27+
if(encrypted) {
28+
// add to command
29+
this.cmd += ASAP_1_0.ENCRYPTED_CMD;
30+
31+
// there must be a keyStorage
32+
if(keyStorage == null) {
33+
throw new ASAPSecurityException("asap message is to be encrypted if possible " +
34+
"but there is not key store at all - fatal, give up");
35+
}
36+
37+
PublicKey publicKey = keyStorage.getPublicKey(recipient);
38+
// there should be an exception - but better safe than sorry
39+
if(publicKey == null) {
40+
throw new ASAPSecurityException(
41+
"message must be encrypted but recipients' public key cannot be found");
42+
}
43+
44+
// we have at least the chance
45+
// encryption?
46+
try {
47+
Cipher cipher = Cipher.getInstance("TODO_Cipher_Algorithm");
48+
cipher.init(Cipher.ENCRYPT_MODE, keyStorage.getPublicKey(recipient));
49+
50+
byte[] message = new byte[0];
51+
cipher.doFinal(message);
52+
} catch (NoSuchAlgorithmException e) {
53+
e.printStackTrace();
54+
} catch (InvalidKeyException e) {
55+
e.printStackTrace();
56+
} catch (BadPaddingException e) {
57+
e.printStackTrace();
58+
} catch (IllegalBlockSizeException e) {
59+
e.printStackTrace();
60+
} catch (NoSuchPaddingException e) {
61+
e.printStackTrace();
62+
}
63+
}
64+
65+
66+
/*
67+
if(sign) {
68+
// there must be a keyStorage
69+
if(keyStorage == null) {
70+
throw new ASAPSecurityException("asap message is to be signed but there is not key store - fatal, give up");
71+
}
72+
73+
// signing needs a private key - check of available
74+
if(keyStorage.getPrivateKey() == null) {
75+
// assume, an exception already documented lack of a private key. if not
76+
throw new ASAPSecurityException("asap message is to be signed but no private key - fatal, give up");
77+
}
78+
79+
// ok, we can sign
80+
if(sign) {
81+
// anything was written into a bytearray
82+
83+
// produce signature
84+
Signature signature = null;
85+
try {
86+
signature = Signature.getInstance("TODO_signing_algorithm");
87+
signature.initSign(keyStorage.getPrivateKey()); // desperate try
88+
byte[] bytes2Sign = bufferOS.toByteArray();
89+
signature.update(bytes2Sign);
90+
byte[] signatureBytes = signature.sign();
91+
92+
// send out anything, including signature
93+
94+
// TODO need number of bytes payload to find signature later.
95+
os.write(bytes2Sign);
96+
os.write(signatureBytes);
97+
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
98+
throw new ASAPSecurityException(e.getLocalizedMessage());
99+
}
100+
}
101+
}
102+
103+
*/
104+
105+
106+
}
107+
108+
public void sendHeader() throws IOException {
109+
PDU_Impl.sendCmd(this.cmd, this.os);
110+
}
111+
112+
byte getCMD() {
113+
return this.cmd;
114+
}
115+
116+
OutputStream getOutputStream() {
117+
return this.os;
118+
}
119+
120+
public void finish() {
121+
122+
}
123+
124+
private String getLogStart() {
125+
return this.getClass().getSimpleName() + ": ";
126+
}
127+
}

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package net.sharksystem.asap.protocol;
22

33
import net.sharksystem.asap.ASAPException;
4-
import net.sharksystem.asap.ASAPSecurityException;
54

6-
import java.io.ByteArrayOutputStream;
75
import java.io.IOException;
86
import java.io.InputStream;
97
import java.io.OutputStream;
10-
import java.security.*;
118

129
class InterestPDU_Impl extends PDU_Impl implements ASAP_Interest_PDU_1_0 {
1310
private int eraFrom;
1411
private int eraTo;
1512

16-
InterestPDU_Impl(int flagsInt, InputStream is) throws IOException, ASAPException {
17-
super(ASAP_1_0.INTEREST_CMD);
13+
InterestPDU_Impl(int flagsInt, boolean encrypted, InputStream is) throws IOException, ASAPException {
14+
super(ASAP_1_0.INTEREST_CMD, encrypted);
1815

1916
evaluateFlags(flagsInt);
2017

@@ -34,10 +31,8 @@ private void readFromEra(InputStream is) throws IOException, ASAPException {
3431
this.eraFrom = this.readIntegerParameter(is);
3532
}
3633

37-
static void sendPDU(CharSequence sender, CharSequence recipient, CharSequence format,
38-
CharSequence channel, int eraFrom, int eraTo, OutputStream os,
39-
boolean sign, boolean encrypted, boolean mustBeEncrypted,
40-
ASAPSignAndEncryptionKeyStorage keyStorage)
34+
static void sendPDUWithoutCmd(CharSequence sender, CharSequence recipient, CharSequence format,
35+
CharSequence channel, int eraFrom, int eraTo, OutputStream os)
4136
throws IOException, ASAPException {
4237

4338
if(format == null || format.length() < 1) format = ASAP_1_0.ANY_FORMAT;
@@ -46,12 +41,8 @@ static void sendPDU(CharSequence sender, CharSequence recipient, CharSequence fo
4641
PDU_Impl.checkValidEra(eraFrom);
4742
PDU_Impl.checkValidEra(eraTo);
4843
PDU_Impl.checkValidFormat(format);
49-
PDU_Impl.checkValidSign(sender, sign);
5044
PDU_Impl.checkValidStream(os);
5145

52-
// Basis test
53-
os = PDU_Impl.prepareCrypto(os, sign, encrypted, mustBeEncrypted, recipient, keyStorage);
54-
5546
// create parameter bytes
5647
int flags = 0;
5748
flags = PDU_Impl.setFlag(sender, flags, SENDER_BIT_POSITION);
@@ -60,7 +51,7 @@ static void sendPDU(CharSequence sender, CharSequence recipient, CharSequence fo
6051
flags = PDU_Impl.setFlag(eraFrom, flags, ERA_FROM_BIT_POSITION);
6152
flags = PDU_Impl.setFlag(eraTo, flags, ERA_TO_BIT_POSITION);
6253

63-
PDU_Impl.sendHeader(ASAP_1_0.INTEREST_CMD, flags, os);
54+
PDU_Impl.sendFlags(flags, os);
6455

6556
PDU_Impl.sendCharSequenceParameter(sender, os); // opt
6657
PDU_Impl.sendCharSequenceParameter(recipient, os); // opt

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
class OfferPDU_Impl extends PDU_Impl implements ASAP_OfferPDU_1_0 {
1010

11-
public OfferPDU_Impl(int flagsInt, InputStream is) throws IOException, ASAPException {
12-
super(ASAP_1_0.OFFER_CMD);
11+
public OfferPDU_Impl(int flagsInt, boolean encrypted, InputStream is) throws IOException, ASAPException {
12+
super(ASAP_1_0.OFFER_CMD, encrypted);
1313
evaluateFlags(flagsInt);
1414

1515
if(this.senderSet()) { this.readSender(is); }
@@ -24,7 +24,7 @@ static void sendPDU(CharSequence peer, CharSequence format, CharSequence channel
2424
// first: check protocol errors
2525
PDU_Impl.checkValidEra(era);
2626
PDU_Impl.checkValidFormat(format);
27-
PDU_Impl.checkValidSign(peer, signed);
27+
// PDU_Impl.checkValidSign(peer, signed);
2828
PDU_Impl.checkValidStream(os);
2929

3030
// create parameter bytes

0 commit comments

Comments
 (0)