11package net .sharksystem .asap .protocol ;
22
3- import jdk .internal .util .xml .impl .Input ;
3+ import net .sharksystem .Utils ;
4+ import net .sharksystem .asap .ASAPException ;
45import net .sharksystem .asap .ASAPSecurityException ;
56
67import javax .crypto .BadPaddingException ;
1011import java .io .*;
1112import java .security .InvalidKeyException ;
1213import java .security .NoSuchAlgorithmException ;
14+ import java .security .PrivateKey ;
1315import java .security .PublicKey ;
1416
1517class CryptoSession {
18+ private CharSequence recipient ;
1619 private ASAPReadonlyKeyStorage keyStorage ;
1720 private Cipher cipher = null ;
1821 private PublicKey publicKey ;
@@ -22,26 +25,36 @@ class CryptoSession {
2225 private OutputStream realOS ;
2326 private ByteArrayOutputStream asapMessageOS ;
2427 private byte [] asapMessageAsBytes ;
28+ private ByteArrayOutputStream senderSiteTest ;
29+ private byte [] byte2Send ;
2530
2631 CryptoSession (ASAPReadonlyKeyStorage keyStorage ) {
2732 this .keyStorage = keyStorage ;
2833 }
2934
3035 InputStream decrypt (InputStream is ) throws ASAPSecurityException {
36+ return this .decrypt (is , this .keyStorage .getPrivateKey ());
37+ }
38+
39+ private InputStream decrypt (InputStream is , PrivateKey privateKey ) throws ASAPSecurityException {
3140 try {
3241 this .cipher = Cipher .getInstance (keyStorage .getRSAEncryptionAlgorithm ());
33- this .cipher .init (Cipher .DECRYPT_MODE , this . keyStorage . getPrivateKey () );
42+ this .cipher .init (Cipher .DECRYPT_MODE , privateKey );
3443
3544 // read len
36- int len = 42 ; // TODO
37-
45+ int len = PDU_Impl .readIntegerParameter (is );
3846 byte [] messageBytes = new byte [len ];
47+
48+ // read encrypted bytes from stream
3949 is .read (messageBytes );
50+
51+ Utils .compareArrays (messageBytes , this .byte2Send );
52+
53+ // decrypt
4054 byte [] decryptedBytes = this .cipher .doFinal (messageBytes );
4155 return new ByteArrayInputStream (decryptedBytes );
42- } catch (BadPaddingException | IllegalBlockSizeException |
43- NoSuchAlgorithmException | NoSuchPaddingException |
44- InvalidKeyException | IOException e ) {
56+ } catch (BadPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException |
57+ NoSuchPaddingException | InvalidKeyException | IOException | ASAPException e ) {
4558 throw new ASAPSecurityException (this .getLogStart (), e );
4659 }
4760 }
@@ -54,6 +67,8 @@ InputStream decrypt(InputStream is) throws ASAPSecurityException {
5467 this .cmd = cmd ;
5568 this .realOS = os ;
5669 this .effectivOS = os ; // still this one
70+ this .keyStorage = keyStorage ;
71+ this .recipient = recipient ;
5772
5873 if (encrypted ) {
5974 // add to command
@@ -149,9 +164,24 @@ public void finish() throws ASAPSecurityException {
149164 try {
150165 byte [] encryptedBytes = this .cipher .doFinal (this .asapMessageAsBytes );
151166 // write data len
167+
168+ // write len
152169 PDU_Impl .sendNonNegativeIntegerParameter (encryptedBytes .length , this .realOS );
153170 // write data
154171 this .realOS .write (encryptedBytes );
172+
173+ // debug - decrypt
174+
175+ // make a test
176+ this .senderSiteTest = new ByteArrayOutputStream ();
177+ PDU_Impl .sendNonNegativeIntegerParameter (encryptedBytes .length , senderSiteTest );
178+ senderSiteTest .write (encryptedBytes );
179+ PrivateKey privateKey = this .keyStorage .getPrivateKey (this .recipient );
180+ this .byte2Send = encryptedBytes ;
181+ ByteArrayInputStream decrypt = (ByteArrayInputStream ) this .decrypt (new ByteArrayInputStream (senderSiteTest .toByteArray ()), privateKey );
182+ int i = 42 ;
183+ /*
184+ */
155185 } catch (IllegalBlockSizeException | BadPaddingException | IOException e ) {
156186 throw new ASAPSecurityException (this .getLogStart (), e );
157187 }
0 commit comments