1212import java .security .*;
1313
1414class CryptoSession {
15+ public static final int MAX_ENCRYPTION_BLOCK_SIZE = 240 ;
1516 private Signature signature ;
1617 private CharSequence recipient ;
1718 private ASAPReadonlyKeyStorage keyStorage ;
@@ -22,6 +23,7 @@ class CryptoSession {
2223 private OutputStream effectivOS ;
2324 private OutputStream realOS ;
2425 private ByteArrayOutputStream asapMessageOS ;
26+ private InputStreamCopy verifyStream ;
2527
2628 CryptoSession (ASAPReadonlyKeyStorage keyStorage ) {
2729 this .keyStorage = keyStorage ;
@@ -157,7 +159,18 @@ public void finish() throws ASAPSecurityException {
157159 try {
158160 // encrypted asap message
159161 byte [] asapMessageAsBytes = this .asapMessageOS .toByteArray ();
160- byte [] encryptedBytes = this .cipher .doFinal (asapMessageAsBytes );
162+
163+ // TODO: create AES key, encrypt with RSA, send and encrypt rest with that AES key
164+
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+ }
171+
172+ int lastStepLen = asapMessageAsBytes .length - i ;
173+ byte [] encryptedBytes = this .cipher .doFinal (asapMessageAsBytes , i , lastStepLen );
161174
162175 this .writeByteArray (encryptedBytes , this .realOS );
163176 this .realOS .write (encryptedBytes );
@@ -168,6 +181,39 @@ public void finish() throws ASAPSecurityException {
168181 }
169182
170183 ////////////////////////////////// verify
184+ private class InputStreamCopy extends InputStream {
185+ private final InputStream is ;
186+ ByteArrayOutputStream copy = new ByteArrayOutputStream ();
187+
188+ InputStreamCopy (byte [] bytes , InputStream is ) throws IOException {
189+ // add byte if any
190+ if (bytes != null && bytes .length > 0 ) {
191+ copy .write (bytes );
192+ }
193+
194+ this .is = is ;
195+ }
196+
197+ @ Override
198+ public int read () throws IOException {
199+ int read = is .read ();
200+ copy .write (read );
201+ return read ;
202+ }
203+
204+ byte [] getCopy () {
205+ return copy .toByteArray ();
206+ }
207+ }
208+
209+ public InputStream setupInputStreamListener (InputStream is , int flagsInt ) throws IOException {
210+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
211+ PDU_Impl .sendFlags (flagsInt , baos );
212+
213+ this .verifyStream = new InputStreamCopy (baos .toByteArray (), is );
214+
215+ return this .verifyStream ;
216+ }
171217
172218 public boolean verify (String sender , InputStream is ) throws IOException , ASAPException {
173219 // try to get senders' public key
@@ -177,6 +223,9 @@ public boolean verify(String sender, InputStream is) throws IOException, ASAPExc
177223 try {
178224 this .signature = Signature .getInstance (this .keyStorage .getRSASigningAlgorithm ());
179225 this .signature .initVerify (publicKey );
226+ // get data which are to be verified
227+ byte [] signedData = this .verifyStream .getCopy ();
228+ this .signature .update (signedData );
180229 byte [] signatureBytes = this .readByteArray (is );
181230 boolean wasVerified = this .signature .verify (signatureBytes );
182231 return wasVerified ;
@@ -221,5 +270,4 @@ private InputStream decrypt(InputStream is, PrivateKey privateKey) throws ASAPSe
221270 private String getLogStart () {
222271 return this .getClass ().getSimpleName () + ": " ;
223272 }
224-
225273}
0 commit comments