11package net .sharksystem .asap .protocol ;
22
3+ import jdk .internal .util .xml .impl .Input ;
34import net .sharksystem .asap .ASAPSecurityException ;
45
56import javax .crypto .BadPaddingException ;
67import javax .crypto .Cipher ;
78import javax .crypto .IllegalBlockSizeException ;
89import javax .crypto .NoSuchPaddingException ;
9- import java .io .IOException ;
10- import java .io .OutputStream ;
10+ import java .io .*;
1111import java .security .InvalidKeyException ;
1212import java .security .NoSuchAlgorithmException ;
1313import java .security .PublicKey ;
1414
1515class 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 () {
0 commit comments