66import net .sharksystem .crypto .ASAPCryptoAlgorithms ;
77import net .sharksystem .utils .Serialization ;
88
9- import javax .crypto .*;
10- import javax .crypto .spec .SecretKeySpec ;
119import java .io .*;
12- import java .security .*;
1310
1411class 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 }
0 commit comments