Skip to content

Commit 97acc58

Browse files
committed
refacturing: extract crypto code from asap protocol.
1 parent 45f6127 commit 97acc58

File tree

3 files changed

+61
-85
lines changed

3 files changed

+61
-85
lines changed

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

Lines changed: 27 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@
66
import net.sharksystem.crypto.ASAPCryptoAlgorithms;
77
import net.sharksystem.utils.Serialization;
88

9-
import javax.crypto.*;
10-
import javax.crypto.spec.SecretKeySpec;
119
import java.io.*;
12-
import java.security.*;
1310

1411
class ASAPCryptoMessage {
1512
private boolean encrypted;
1613
private boolean sign;
17-
private Signature signature;
1814
private CharSequence recipient;
1915
private BasisCryptoParameters basisCryptoParameters;
2016
private byte cmd;
2117

22-
private OutputStream effectivOS;
18+
private OutputStream effectiveOS;
2319
private OutputStream realOS;
24-
private ByteArrayOutputStream asapMessageOS;
20+
private ByteArrayOutputStream outputStreamCopy;
2521
private InputStreamCopy inputStreamCopy;
2622
private ASAPCryptoAlgorithms.EncryptedMessagePackage encryptedMessagePackage;
2723

@@ -36,7 +32,7 @@ class ASAPCryptoMessage {
3632

3733
this.cmd = cmd;
3834
this.realOS = os;
39-
this.effectivOS = os; // still this one
35+
this.effectiveOS = os; // still this one
4036
this.basisCryptoParameters = basisCryptoParameters;
4137
this.recipient = recipient;
4238
this.encrypted = encrypted;
@@ -47,7 +43,7 @@ class ASAPCryptoMessage {
4743
if(basisCryptoParameters == null) {
4844
throw new ASAPSecurityException("cannot encrypt or sign without cryptp parameters / key store");
4945
}
50-
this.setupTempMessageStorage();
46+
this.setupCopyOutputStream();
5147
}
5248

5349
if(encrypted) {
@@ -66,11 +62,11 @@ class ASAPCryptoMessage {
6662
}
6763
}
6864

69-
private void setupTempMessageStorage() {
70-
if(this.asapMessageOS == null) {
71-
this.asapMessageOS = new ByteArrayOutputStream();
65+
private void setupCopyOutputStream() {
66+
if(this.outputStreamCopy == null) {
67+
this.outputStreamCopy = new ByteArrayOutputStream();
7268
// pud will make a detour
73-
this.effectivOS = this.asapMessageOS;
69+
this.effectiveOS = this.outputStreamCopy;
7470
}
7571
}
7672

@@ -79,27 +75,23 @@ public void sendCmd() throws IOException {
7975
PDU_Impl.sendCmd(this.cmd, this.realOS);
8076
}
8177

82-
byte getCMD() {
83-
return this.cmd;
84-
}
85-
8678
public OutputStream getOutputStream() {
87-
return this.effectivOS;
79+
return this.effectiveOS;
8880
}
8981

9082
public void finish() throws ASAPSecurityException {
9183
if(this.sign) {
9284
try {
9385
// get message as bytes
94-
byte[] asapMessageAsBytes = this.asapMessageOS.toByteArray();
86+
byte[] asapMessageAsBytes = this.outputStreamCopy.toByteArray();
9587
// produce signature
9688
byte[] signatureBytes = ASAPCryptoAlgorithms.sign(asapMessageAsBytes, this.basisCryptoParameters);
9789

9890
if(this.encrypted) {
99-
// have to store it - anything will be encrypted
100-
Serialization.writeByteArray(signatureBytes, this.asapMessageOS);
91+
// have to store it - message and signature will be encrypted
92+
Serialization.writeByteArray(signatureBytes, this.outputStreamCopy);
10193
} else {
102-
// can write anything now
94+
// no encryption planned - write clear to stream
10395
this.realOS.write(asapMessageAsBytes);
10496
Serialization.writeByteArray(signatureBytes, this.realOS);
10597
}
@@ -110,7 +102,7 @@ public void finish() throws ASAPSecurityException {
110102

111103
if(this.encrypted) {
112104
// get maybe signed asap message
113-
byte[] asapMessageAsBytes = this.asapMessageOS.toByteArray();
105+
byte[] asapMessageAsBytes = this.outputStreamCopy.toByteArray();
114106

115107
ASAPCryptoAlgorithms.writeEncryptedMessagePackage(
116108
asapMessageAsBytes, this.recipient, this.basisCryptoParameters, this.realOS);
@@ -147,7 +139,7 @@ byte[] getCopy() {
147139
}
148140
}
149141

150-
public InputStream setupCopyStream(int priorInt, InputStream is)
142+
public InputStream setupCopyInputStream(int priorInt, InputStream is)
151143
throws IOException {
152144

153145
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -163,21 +155,13 @@ public InputStream setupCopyStream(int priorInt, InputStream is)
163155

164156
public boolean verify(String sender, InputStream is) throws IOException, ASAPException {
165157
// try to get senders' public key
166-
PublicKey publicKey = this.basisCryptoParameters.getPublicKey(sender);
167-
if(publicKey == null) return false;
168-
169-
try {
170-
this.signature = Signature.getInstance(this.basisCryptoParameters.getRSASigningAlgorithm());
171-
this.signature.initVerify(publicKey);
172-
// get data which are to be verified
173-
byte[] signedData = this.inputStreamCopy.getCopy();
174-
this.signature.update(signedData);
175-
byte[] signatureBytes = Serialization.readByteArray(is);
176-
boolean wasVerified = this.signature.verify(signatureBytes);
177-
return wasVerified;
178-
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
179-
throw new ASAPSecurityException(this.getLogStart(), e);
180-
}
158+
byte[] signedData = this.inputStreamCopy.getCopy();
159+
byte[] signatureBytes = Serialization.readByteArray(is);
160+
// debug break
161+
boolean wasVerified =
162+
ASAPCryptoAlgorithms.verify(signedData, signatureBytes, sender, this.basisCryptoParameters);
163+
164+
return wasVerified;
181165
}
182166

183167
////////////////////////////////// decrypt
@@ -194,19 +178,8 @@ public boolean verify(String sender, InputStream is) throws IOException, ASAPExc
194178
* @throws ASAPException
195179
*/
196180
public boolean initDecryption(byte cmd, InputStream is) throws IOException, ASAPException {
197-
// make a copy of read data
198-
InputStream copyStream = this.setupCopyStream(cmd, is);
199-
200-
/*
201-
// read recipient
202-
this.recipient = Serialization.readCharSequenceParameter(copyStream);
203-
204-
// read encrypted symmetric key
205-
this.encryptedSymmetricKey = Serialization.readByteArray(copyStream);
206-
207-
// read content
208-
this.encryptedContent = Serialization.readByteArray(copyStream);
209-
*/
181+
// make a copy of encrypted message - it is redundant. Same data in encryptedMessagePackage
182+
InputStream copyStream = this.setupCopyInputStream(cmd, is);
210183

211184
this.encryptedMessagePackage =
212185
ASAPCryptoAlgorithms.parseEncryptedMessagePackage(copyStream);
@@ -216,8 +189,6 @@ public boolean initDecryption(byte cmd, InputStream is) throws IOException, ASAP
216189
return false;
217190
}
218191

219-
// read anything - are we recipient?
220-
// if(this.basisCryptoParameters.isOwner(this.recipient)) {
221192
if(this.basisCryptoParameters.isOwner(this.encryptedMessagePackage.getRecipient())) {
222193
return true;
223194
}
@@ -234,38 +205,13 @@ byte[] getEncryptedMessage() throws ASAPSecurityException {
234205
return this.inputStreamCopy.getCopy();
235206
}
236207

237-
public InputStream doDecryption(InputStream is) throws ASAPSecurityException {
238-
return this.doDecryption(is, this.basisCryptoParameters.getPrivateKey());
239-
}
240-
241-
// parameter private key is usually not an option. Good entry for testing / debugging, though
242-
public InputStream doDecryption(InputStream is, PrivateKey privateKey) throws ASAPSecurityException {
208+
public InputStream doDecryption() throws ASAPSecurityException {
243209
if(this.encryptedMessagePackage == null) {
244210
throw new ASAPSecurityException("forgot to initialize decryption? There are no data");
245211
}
246-
/*
247-
// decrypt encoded symmetric key
248-
this.cipher = Cipher.getInstance(basisCryptoParameters.getRSAEncryptionAlgorithm());
249-
this.cipher.init(Cipher.DECRYPT_MODE, privateKey);
250-
251-
// read encryptedKey in initDecryption
252-
byte[] encodedSymmetricKey = this.cipher.doFinal(this.encryptedSymmetricKey);
253-
*/
254-
255-
byte[] encodedSymmetricKey = ASAPCryptoAlgorithms.decryptAsymmetric(
256-
this.encryptedMessagePackage.getEncryptedSymmetricKey(),
257-
this.basisCryptoParameters);
258-
259-
// create symmetric key object
260-
SecretKey symmetricKey =
261-
ASAPCryptoAlgorithms.createSymmetricKey(encodedSymmetricKey, this.basisCryptoParameters);
262-
263212

264-
// decrypt content
265-
byte[] decryptedBytes = ASAPCryptoAlgorithms.decryptSymmetric(
266-
this.encryptedMessagePackage.getEncryptedContent(),
267-
symmetricKey,
268-
this.basisCryptoParameters);
213+
byte[] decryptedBytes =
214+
ASAPCryptoAlgorithms.decryptPackage(this.encryptedMessagePackage, this.basisCryptoParameters);
269215

270216
return new ByteArrayInputStream(decryptedBytes);
271217
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
180180
boolean ownerIsRecipient = cryptoMessage.initDecryption(cmd, is);
181181
if(ownerIsRecipient) {
182182
// peer is recipient - decrypt and go ahead
183-
InputStream decryptedIS = cryptoMessage.doDecryption(is);
183+
InputStream decryptedIS = cryptoMessage.doDecryption();
184184
is = decryptedIS;
185185
} else {
186186
// we cannot decrypt this message - we are not recipient - but we keep and redistribute
@@ -203,7 +203,7 @@ public ASAP_PDU_1_0 readPDU(InputStream is) throws IOException, ASAPException {
203203
ASAPCryptoMessage verifyCryptoMessage = null;
204204
if(PDU_Impl.flagSet(PDU_Impl.SIGNED_TO_BIT_POSITION, flagsInt)) {
205205
verifyCryptoMessage = new ASAPCryptoMessage(this.signAndEncryptionKeyStorage);
206-
is = verifyCryptoMessage.setupCopyStream(flagsInt, is);
206+
is = verifyCryptoMessage.setupCopyInputStream(flagsInt, is);
207207
}
208208

209209
PDU_Impl pdu = null;

src/net/sharksystem/crypto/ASAPCryptoAlgorithms.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ public static SecretKey createSymmetricKey(byte[] encodedSymmetricKey,
7272
return new SecretKeySpec(encodedSymmetricKey, basisCryptoParameters.getSymmetricKeyType());
7373
}
7474

75+
public static byte[] decryptPackage(EncryptedMessagePackage encryptedMessagePackage,
76+
BasisCryptoParameters basisCryptoParameters) throws ASAPSecurityException {
77+
78+
byte[] encodedSymmetricKey = decryptAsymmetric(
79+
encryptedMessagePackage.getEncryptedSymmetricKey(),
80+
basisCryptoParameters);
81+
82+
// create symmetric key object
83+
SecretKey symmetricKey = createSymmetricKey(encodedSymmetricKey, basisCryptoParameters);
84+
85+
86+
// decrypt content
87+
return decryptSymmetric(encryptedMessagePackage.getEncryptedContent(), symmetricKey, basisCryptoParameters);
88+
}
89+
7590
public interface EncryptedMessagePackage {
7691
CharSequence getRecipient();
7792
byte[] getEncryptedSymmetricKey();
@@ -142,7 +157,8 @@ public static byte[] decryptSymmetric(byte[] encryptedContent, SecretKey symmetr
142157
}
143158
}
144159

145-
public static byte[] decryptAsymmetric(byte[] encryptedBytes, BasisCryptoParameters basisCryptoParameters) throws ASAPSecurityException {
160+
public static byte[] decryptAsymmetric(byte[] encryptedBytes, BasisCryptoParameters basisCryptoParameters)
161+
throws ASAPSecurityException {
146162
try {
147163
Cipher cipher = Cipher.getInstance(basisCryptoParameters.getRSAEncryptionAlgorithm());
148164
cipher.init(Cipher.DECRYPT_MODE, basisCryptoParameters.getPrivateKey());
@@ -166,5 +182,19 @@ public static byte[] sign(byte[] bytes2Sign, BasisCryptoParameters basisCryptoPa
166182
}
167183
}
168184

169-
public static byte[] verify() {return null;}
185+
public static boolean verify(byte[] signedData, byte[] signatureBytes, String sender,
186+
BasisCryptoParameters basisCryptoParameters) throws ASAPSecurityException {
187+
188+
PublicKey publicKey = basisCryptoParameters.getPublicKey(sender);
189+
if(publicKey == null) return false;
190+
191+
try {
192+
Signature signature = Signature.getInstance(basisCryptoParameters.getRSASigningAlgorithm());
193+
signature.initVerify(publicKey); // init with private key
194+
signature.update(signedData); // feed with signed data
195+
return signature.verify(signatureBytes); // check against signature
196+
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
197+
throw new ASAPSecurityException("problems when verifying", e);
198+
}
199+
}
170200
}

0 commit comments

Comments
 (0)