11package net .sharksystem .asap .protocol ;
22
3- import net .sharksystem .Utils ;
43import net .sharksystem .asap .ASAPException ;
54import net .sharksystem .asap .ASAPSecurityException ;
5+ import net .sharksystem .asap .protocol .ASAP_1_0 ;
6+ import net .sharksystem .asap .protocol .PDU_Impl ;
7+ import net .sharksystem .crypto .ASAPBasicKeyStorage ;
68
7- import javax .crypto .BadPaddingException ;
8- import javax .crypto .Cipher ;
9- import javax .crypto .IllegalBlockSizeException ;
10- import javax .crypto .NoSuchPaddingException ;
9+ import javax .crypto .*;
10+ import javax .crypto .spec .SecretKeySpec ;
1111import java .io .*;
1212import java .security .*;
1313
1414class CryptoSession {
15- public static final int MAX_ENCRYPTION_BLOCK_SIZE = 240 ;
15+ private static final int MAX_ENCRYPTION_BLOCK_SIZE = 128 ;
1616 private Signature signature ;
1717 private CharSequence recipient ;
18- private ASAPReadonlyKeyStorage keyStorage ;
18+ private ASAPBasicKeyStorage keyStorage ;
1919 private Cipher cipher = null ;
2020 private PublicKey publicKey ;
2121 private byte cmd ;
@@ -25,13 +25,13 @@ class CryptoSession {
2525 private ByteArrayOutputStream asapMessageOS ;
2626 private InputStreamCopy verifyStream ;
2727
28- CryptoSession (ASAPReadonlyKeyStorage keyStorage ) {
28+ CryptoSession (ASAPBasicKeyStorage keyStorage ) {
2929 this .keyStorage = keyStorage ;
3030 }
3131
3232 CryptoSession (byte cmd , OutputStream os , boolean sign , boolean encrypted ,
3333 CharSequence recipient ,
34- ASAPReadonlyKeyStorage keyStorage )
34+ ASAPBasicKeyStorage keyStorage )
3535 throws ASAPSecurityException {
3636
3737 this .cmd = cmd ;
@@ -113,7 +113,7 @@ byte getCMD() {
113113 return this .cmd ;
114114 }
115115
116- OutputStream getOutputStream () {
116+ public OutputStream getOutputStream () {
117117 return this .effectivOS ;
118118 }
119119
@@ -160,20 +160,35 @@ public void finish() throws ASAPSecurityException {
160160 // encrypted asap message
161161 byte [] asapMessageAsBytes = this .asapMessageOS .toByteArray ();
162162
163- // TODO: create AES key, encrypt with RSA, send and encrypt rest with that AES key
163+ // get symmetric key
164+ SecretKey encryptionKey = this .keyStorage .generateSymmetricKey ();
165+ byte [] encodedSymmetricKey = encryptionKey .getEncoded ();
164166
165- // that stuff will not work.
166- int i = 0 ;
167- while (i + MAX_ENCRYPTION_BLOCK_SIZE < asapMessageAsBytes .length ) {
168- this .cipher .update (asapMessageAsBytes , i , MAX_ENCRYPTION_BLOCK_SIZE );
169- i += MAX_ENCRYPTION_BLOCK_SIZE ;
170- }
167+ // encrypt key
168+ byte [] encryptedSymmetricKeyBytes = this .cipher .doFinal (encodedSymmetricKey );
169+
170+ // send encrypted key
171+ this .writeByteArray (encryptedSymmetricKeyBytes , this .realOS );
172+
173+ // encrypt message with symmetric key
174+ try {
175+ this .cipher = Cipher .getInstance (keyStorage .getSymmetricEncryptionAlgorithm ());
176+ this .cipher .init (Cipher .ENCRYPT_MODE , encryptionKey );
177+
178+ // block by block
179+ int i = 0 ;
180+ while (i + MAX_ENCRYPTION_BLOCK_SIZE < asapMessageAsBytes .length ) {
181+ this .cipher .update (asapMessageAsBytes , i , MAX_ENCRYPTION_BLOCK_SIZE );
182+ i += MAX_ENCRYPTION_BLOCK_SIZE ;
183+ }
171184
172- int lastStepLen = asapMessageAsBytes .length - i ;
173- byte [] encryptedBytes = this .cipher .doFinal (asapMessageAsBytes , i , lastStepLen );
185+ int lastStepLen = asapMessageAsBytes .length - i ;
186+ byte [] encryptedContent = this .cipher .doFinal (asapMessageAsBytes , i , lastStepLen );
174187
175- this .writeByteArray (encryptedBytes , this .realOS );
176- this .realOS .write (encryptedBytes );
188+ this .writeByteArray (encryptedContent , this .realOS );
189+ } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException e ) {
190+ throw new ASAPSecurityException (this .getLogStart (), e );
191+ }
177192 } catch (IllegalBlockSizeException | BadPaddingException | IOException e ) {
178193 throw new ASAPSecurityException (this .getLogStart (), e );
179194 }
@@ -236,30 +251,32 @@ public boolean verify(String sender, InputStream is) throws IOException, ASAPExc
236251
237252 ////////////////////////////////// decrypt
238253
239- InputStream decrypt (InputStream is ) throws ASAPSecurityException {
254+ public InputStream decrypt (InputStream is ) throws ASAPSecurityException {
240255 return this .decrypt (is , this .keyStorage .getPrivateKey ());
241256 }
242257
243- // parameter private key is usually no option. There is just one - burt was good for debugging
258+ // parameter private key is usually not an option. Good entry for testing / debugging, though
244259 private InputStream decrypt (InputStream is , PrivateKey privateKey ) throws ASAPSecurityException {
245260 try {
261+ // read encrypted symmetric key
262+ byte [] encryptedSymmetricKey = this .readByteArray (is );
263+
264+ // decrypt encoded symmetric key
246265 this .cipher = Cipher .getInstance (keyStorage .getRSAEncryptionAlgorithm ());
247266 this .cipher .init (Cipher .DECRYPT_MODE , privateKey );
267+ byte [] encodedSymmetricKey = this .cipher .doFinal (encryptedSymmetricKey );
248268
249- // read encrypted message
250- byte [] messageBytes = this .readByteArray (is );
251-
252- /*
253- // read len
254- int len = PDU_Impl.readIntegerParameter(is);
255- byte[] messageBytes = new byte[len];
269+ // create symmetric key object
270+ SecretKey symmetricKey =
271+ new SecretKeySpec (encodedSymmetricKey , this .keyStorage .getSymmetricKeyType ());
256272
257- // read encrypted bytes from stream
258- is.read(messageBytes);
259- */
273+ // read content
274+ byte [] encryptedContent = this .readByteArray (is );
260275
261- // decrypt
262- byte [] decryptedBytes = this .cipher .doFinal (messageBytes );
276+ // decrypt content
277+ Cipher symmetricCipher = Cipher .getInstance (keyStorage .getSymmetricEncryptionAlgorithm ());
278+ symmetricCipher .init (Cipher .DECRYPT_MODE , symmetricKey );
279+ byte [] decryptedBytes = symmetricCipher .doFinal (encryptedContent );
263280 return new ByteArrayInputStream (decryptedBytes );
264281 } catch (BadPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException |
265282 NoSuchPaddingException | InvalidKeyException | IOException | ASAPException e ) {
0 commit comments