Skip to content

Commit 6cdb9be

Browse files
committed
before making more PDU methods static
1 parent bbe312a commit 6cdb9be

File tree

6 files changed

+188
-48
lines changed

6 files changed

+188
-48
lines changed

src/net/sharksystem/SharkException.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/net/sharksystem/asap/protocol/ASAPSignAndEncryptionKeyStorage.java renamed to src/net/sharksystem/asap/protocol/ASAPReadonlyKeyStorage.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.security.PrivateKey;
66
import java.security.PublicKey;
77

8-
public interface ASAPSignAndEncryptionKeyStorage {
8+
public interface ASAPReadonlyKeyStorage {
99
/**
1010
*
1111
* @return private key of local device - for signing
@@ -20,4 +20,14 @@ public interface ASAPSignAndEncryptionKeyStorage {
2020
* @throws ASAPSecurityException if key cannot be found
2121
*/
2222
PublicKey getPublicKey(CharSequence subjectID) throws ASAPSecurityException;
23+
24+
/**
25+
* @return public key of local device - for signing
26+
* @throws ASAPSecurityException
27+
*/
28+
PublicKey getPublicKey() throws ASAPSecurityException;
29+
30+
String getRSAEncryptionAlgorithm();
31+
32+
String getRSASigningAlgorithm();
2333
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import java.util.List;
1111

1212
public class ASAP_Modem_Impl implements ASAP_1_0 {
13-
private final ASAPSignAndEncryptionKeyStorage signAndEncryptionKeyStorage;
13+
private final ASAPReadonlyKeyStorage signAndEncryptionKeyStorage;
1414

1515
public ASAP_Modem_Impl() {
1616
this.signAndEncryptionKeyStorage = null; // no key storage
1717
}
1818

19-
public ASAP_Modem_Impl(ASAPSignAndEncryptionKeyStorage signAndEncryptionKeyStorage) {
19+
public ASAP_Modem_Impl(ASAPReadonlyKeyStorage signAndEncryptionKeyStorage) {
2020
this.signAndEncryptionKeyStorage = signAndEncryptionKeyStorage;
2121
}
2222

@@ -75,9 +75,10 @@ public void interest(CharSequence sender, CharSequence recipient, CharSequence f
7575

7676
// prepare encryption and signing if required
7777
CryptoSession cryptoSession = new CryptoSession(ASAP_1_0.INTEREST_CMD,
78-
os, signed, encrypted, recipient, this.signAndEncryptionKeyStorage);
78+
os, signed, encrypted, recipient,
79+
this.signAndEncryptionKeyStorage);
7980

80-
cryptoSession.sendHeader();
81+
cryptoSession.sendCmd();
8182

8283
InterestPDU_Impl.sendPDUWithoutCmd(sender, recipient, format, channel, eraFrom, eraTo,
8384
cryptoSession.getOutputStream());
@@ -115,6 +116,9 @@ public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
115116
// remove encrypted flag
116117
cmd = (byte)(cmd & CMD_MASK);
117118

119+
if(encrypted) {
120+
}
121+
118122
int flagsInt = PDU_Impl.readByte(is);
119123

120124
ASAP_PDU_1_0 pdu = null;

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

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,59 @@
11
package net.sharksystem.asap.protocol;
22

3+
import jdk.internal.util.xml.impl.Input;
34
import net.sharksystem.asap.ASAPSecurityException;
45

56
import javax.crypto.BadPaddingException;
67
import javax.crypto.Cipher;
78
import javax.crypto.IllegalBlockSizeException;
89
import javax.crypto.NoSuchPaddingException;
9-
import java.io.IOException;
10-
import java.io.OutputStream;
10+
import java.io.*;
1111
import java.security.InvalidKeyException;
1212
import java.security.NoSuchAlgorithmException;
1313
import java.security.PublicKey;
1414

1515
class CryptoSession {
16+
private ASAPReadonlyKeyStorage keyStorage;
17+
private Cipher cipher = null;
18+
private PublicKey publicKey;
1619
private byte cmd;
17-
private final OutputStream os;
20+
21+
private OutputStream effectivOS;
22+
private OutputStream realOS;
23+
private ByteArrayOutputStream asapMessageOS;
24+
private byte[] asapMessageAsBytes;
25+
26+
CryptoSession(ASAPReadonlyKeyStorage keyStorage) {
27+
this.keyStorage = keyStorage;
28+
}
29+
30+
InputStream decrypt(InputStream is) throws ASAPSecurityException {
31+
try {
32+
this.cipher = Cipher.getInstance(keyStorage.getRSAEncryptionAlgorithm());
33+
this.cipher.init(Cipher.DECRYPT_MODE, this.keyStorage.getPrivateKey());
34+
35+
// read len
36+
int len = 42; // TODO
37+
38+
byte[] messageBytes = new byte[len];
39+
is.read(messageBytes);
40+
byte[] decryptedBytes = this.cipher.doFinal(messageBytes);
41+
return new ByteArrayInputStream(decryptedBytes);
42+
} catch (BadPaddingException | IllegalBlockSizeException |
43+
NoSuchAlgorithmException | NoSuchPaddingException |
44+
InvalidKeyException | IOException e) {
45+
throw new ASAPSecurityException(this.getLogStart(), e);
46+
}
47+
}
1848

1949
CryptoSession(byte cmd, OutputStream os, boolean sign, boolean encrypted,
2050
CharSequence recipient,
21-
ASAPSignAndEncryptionKeyStorage keyStorage)
51+
ASAPReadonlyKeyStorage keyStorage)
2252
throws ASAPSecurityException {
2353

2454
this.cmd = cmd;
25-
this.os = os;
55+
this.realOS = os;
56+
this.effectivOS = os; // still this one
2657

2758
if(encrypted) {
2859
// add to command
@@ -34,32 +65,25 @@ class CryptoSession {
3465
"but there is not key store at all - fatal, give up");
3566
}
3667

37-
PublicKey publicKey = keyStorage.getPublicKey(recipient);
68+
this.publicKey = keyStorage.getPublicKey(recipient);
3869
// there should be an exception - but better safe than sorry
39-
if(publicKey == null) {
70+
if(this.publicKey == null) {
4071
throw new ASAPSecurityException(
4172
"message must be encrypted but recipients' public key cannot be found");
4273
}
4374

44-
// we have at least the chance
45-
// encryption?
75+
// let's see if we can setup cipher
4676
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();
77+
this.cipher = Cipher.getInstance(keyStorage.getRSAEncryptionAlgorithm());
78+
this.cipher.init(Cipher.ENCRYPT_MODE, this.publicKey);
79+
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException e) {
80+
throw new ASAPSecurityException(this.getLogStart(), e);
6281
}
82+
83+
// cipher is ready - we can encrypt
84+
this.asapMessageOS = new ByteArrayOutputStream();
85+
// pud will make a detour
86+
this.effectivOS = this.asapMessageOS;
6387
}
6488

6589

@@ -105,20 +129,33 @@ class CryptoSession {
105129

106130
}
107131

108-
public void sendHeader() throws IOException {
109-
PDU_Impl.sendCmd(this.cmd, this.os);
132+
public void sendCmd() throws IOException {
133+
// send cmd in clear
134+
PDU_Impl.sendCmd(this.cmd, this.realOS);
110135
}
111136

112137
byte getCMD() {
113138
return this.cmd;
114139
}
115140

116141
OutputStream getOutputStream() {
117-
return this.os;
142+
return this.effectivOS;
118143
}
119144

120-
public void finish() {
121-
145+
public void finish() throws ASAPSecurityException {
146+
if(cipher != null) {
147+
// we are to encrypt
148+
this.asapMessageAsBytes = this.asapMessageOS.toByteArray();
149+
try {
150+
byte[] encryptedBytes = this.cipher.doFinal(this.asapMessageAsBytes);
151+
// write data len
152+
PDU_Impl.sendNonNegativeIntegerParameter(encryptedBytes.length, this.realOS);
153+
// write data
154+
this.realOS.write(encryptedBytes);
155+
} catch (IllegalBlockSizeException | BadPaddingException | IOException e) {
156+
throw new ASAPSecurityException(this.getLogStart(), e);
157+
}
158+
}
122159
}
123160

124161
private String getLogStart() {

test/net/sharksystem/asap/protocol/PDUTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
import static net.sharksystem.asap.protocol.ASAP_1_0.ERA_NOT_DEFINED;
1212

1313
public class PDUTests {
14+
public static final String ALICE_ID = "Alice";
15+
public static final String BOB_ID = "Bob";
1416

1517
@Test
1618
public void sendAndReceiveOffer() throws IOException, ASAPException {
19+
1720
ASAP_1_0 protocolEngine = new ASAP_Modem_Impl();
1821

1922
String peer = "Alice";
@@ -110,10 +113,14 @@ public void sendAndReceiveInterest2() throws IOException, ASAPException {
110113

111114
@Test
112115
public void sendAndReceiveInterestCanBeEncrypted() throws IOException, ASAPException {
113-
ASAP_1_0 protocolEngine = new ASAP_Modem_Impl();
116+
TestASAPKeyStorage keyStorage = new TestASAPKeyStorage();
117+
// add Bob
118+
keyStorage.createTestPeer(BOB_ID);
114119

115-
String sender = "Alice";
116-
String recipient = "Bob";
120+
ASAP_1_0 protocolEngine = new ASAP_Modem_Impl(keyStorage);
121+
122+
String sender = ALICE_ID;
123+
String recipient = BOB_ID;
117124
String channel = "AliceURI";
118125
String format = "format";
119126

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package net.sharksystem.asap.protocol;
2+
3+
import net.sharksystem.asap.ASAPSecurityException;
4+
import net.sharksystem.asap.util.Log;
5+
6+
import java.security.*;
7+
import java.util.HashMap;
8+
9+
public class TestASAPKeyStorage implements ASAPReadonlyKeyStorage {
10+
private PrivateKey privateKey;
11+
private PublicKey publicKey;
12+
private long timeInMillis = 0;
13+
14+
public static final String DEFAULT_RSA_ENCRYPTION_ALGORITHM = "RSA/ECB/PKCS1Padding";
15+
public static final String DEFAULT_SIGNATURE_METHOD = "SHA256withRSA";
16+
17+
private HashMap<String, KeyPair> peerKeyPairs = new HashMap<>();
18+
19+
TestASAPKeyStorage() throws ASAPSecurityException {
20+
// generate owners key pair;
21+
KeyPair keyPair = this.generateKeyPair();
22+
23+
this.privateKey = keyPair.getPrivate();
24+
this.publicKey = keyPair.getPublic();
25+
this.timeInMillis = System.currentTimeMillis();
26+
}
27+
28+
private KeyPair generateKeyPair() throws ASAPSecurityException {
29+
Log.writeLog(this, "create key pair");
30+
KeyPairGenerator keyGen = null;
31+
try {
32+
keyGen = KeyPairGenerator.getInstance("RSA");
33+
} catch (NoSuchAlgorithmException e) {
34+
throw new ASAPSecurityException(e.getLocalizedMessage());
35+
}
36+
37+
SecureRandom secRandom = new SecureRandom();
38+
try {
39+
keyGen.initialize(2048, secRandom);
40+
return keyGen.generateKeyPair();
41+
}
42+
catch(RuntimeException re) {
43+
throw new ASAPSecurityException(re.getLocalizedMessage());
44+
}
45+
}
46+
47+
public void createTestPeer(String id) throws ASAPSecurityException {
48+
this.peerKeyPairs.put(id, this.generateKeyPair());
49+
}
50+
51+
@Override
52+
public PrivateKey getPrivateKey() throws ASAPSecurityException {
53+
if(this.privateKey == null) throw new ASAPSecurityException("private key does not exist");
54+
return this.privateKey;
55+
}
56+
57+
@Override
58+
public PublicKey getPublicKey(CharSequence subjectID) throws ASAPSecurityException {
59+
KeyPair keyPair = this.peerKeyPairs.get(subjectID);
60+
if(keyPair == null) throw new ASAPSecurityException("no key pair for " + subjectID);
61+
62+
return keyPair.getPublic();
63+
}
64+
65+
/**
66+
* In reality there cannot be such a method - but we are in a test.
67+
* @param subjectID
68+
* @return
69+
* @throws ASAPSecurityException
70+
*/
71+
public PrivateKey getPrivateKey(CharSequence subjectID) throws ASAPSecurityException {
72+
KeyPair keyPair = this.peerKeyPairs.get(subjectID);
73+
if(keyPair == null) throw new ASAPSecurityException("no key pair for " + subjectID);
74+
75+
return keyPair.getPrivate();
76+
}
77+
78+
@Override
79+
public PublicKey getPublicKey() throws ASAPSecurityException {
80+
if(this.publicKey == null) throw new ASAPSecurityException("public key does not exist");
81+
return this.publicKey;
82+
}
83+
84+
@Override
85+
public String getRSAEncryptionAlgorithm() {
86+
return DEFAULT_RSA_ENCRYPTION_ALGORITHM;
87+
}
88+
89+
@Override
90+
public String getRSASigningAlgorithm() {
91+
return DEFAULT_SIGNATURE_METHOD;
92+
}
93+
}

0 commit comments

Comments
 (0)