@@ -147,9 +147,7 @@ public byte[] recoverSessionData(PublicKeyEncSessionPacket pkesk, InputStreamPac
147
147
public byte [] recoverSessionData (int keyAlgorithm , byte [][] secKeyData , int pkeskVersion )
148
148
throws PGPException
149
149
{
150
- if (keyAlgorithm == PublicKeyAlgorithmTags .ECDH ||
151
- keyAlgorithm == PublicKeyAlgorithmTags .X25519 ||
152
- keyAlgorithm == PublicKeyAlgorithmTags .X448 )
150
+ if (keyAlgorithm == PublicKeyAlgorithmTags .ECDH || keyAlgorithm == PublicKeyAlgorithmTags .X25519 || keyAlgorithm == PublicKeyAlgorithmTags .X448 )
153
151
{
154
152
throw new PGPException ("ECDH requires use of PGPPrivateKey for decryption" );
155
153
}
@@ -264,18 +262,12 @@ private byte[] decryptSessionData(JcaPGPKeyConverter converter, PGPPrivateKey pr
264
262
byte [] keyEnc ;
265
263
266
264
pLen = ((((enc [0 ] & 0xff ) << 8 ) + (enc [1 ] & 0xff )) + 7 ) / 8 ;
267
- if ((2 + pLen + 1 ) > enc .length )
268
- {
269
- throw new PGPException ("encoded length out of range" );
270
- }
265
+ checkRange (2 + pLen + 1 , enc );
271
266
272
267
pEnc = new byte [pLen ];
273
268
System .arraycopy (enc , 2 , pEnc , 0 , pLen );
274
269
int keyLen = enc [pLen + 2 ] & 0xff ;
275
- if ((2 + pLen + 1 + keyLen ) > enc .length )
276
- {
277
- throw new PGPException ("encoded length out of range" );
278
- }
270
+ checkRange (2 + pLen + 1 + keyLen , enc );
279
271
280
272
keyEnc = new byte [keyLen ];
281
273
System .arraycopy (enc , 2 + pLen + 1 , keyEnc , 0 , keyLen );
@@ -341,11 +333,8 @@ private byte[] decryptSessionData(JcaPGPKeyConverter converter, PGPPrivateKey pr
341
333
byte [] ephemeralKey = Arrays .copyOf (enc , pLen );
342
334
343
335
int size = enc [pLen ] & 0xff ;
344
- // checkRange
345
- if ((pLen + 1 + size ) > enc .length )
346
- {
347
- throw new PGPException ("encoded length out of range" );
348
- }
336
+
337
+ checkRange (pLen + 1 + size , enc );
349
338
350
339
// encrypted session key
351
340
int sesKeyLen = size - (containsSKAlg ? 1 : 0 );
@@ -363,18 +352,12 @@ private byte[] decryptSessionData(JcaPGPKeyConverter converter, PGPPrivateKey pr
363
352
}
364
353
}
365
354
366
- private Key getSessionKey (JcaPGPKeyConverter converter ,
367
- PGPPrivateKey privKey ,
368
- String agreementName ,
369
- PublicKey publicKey ,
370
- int symmetricKeyAlgorithm ,
371
- byte [] keyEnc ,
372
- AlgorithmParameterSpec ukms )
355
+ private Key getSessionKey (JcaPGPKeyConverter converter , PGPPrivateKey privKey , String agreementName ,
356
+ PublicKey publicKey , int symmetricKeyAlgorithm , byte [] keyEnc , AlgorithmParameterSpec ukms )
373
357
throws PGPException , GeneralSecurityException
374
358
{
375
359
PrivateKey privateKey = converter .getPrivateKey (privKey );
376
- String wrapName = RFC6637Utils .getKeyEncryptionOID (symmetricKeyAlgorithm ).getId ();
377
- Key key = JcaJcePGPUtil .getSecret (helper , publicKey , wrapName , agreementName , ukms , privateKey );
360
+ Key key = JcaJcePGPUtil .getSecret (helper , publicKey , RFC6637Utils .getKeyEncryptionOID (symmetricKeyAlgorithm ).getId (), agreementName , ukms , privateKey );
378
361
Cipher c = helper .createKeyWrapper (symmetricKeyAlgorithm );
379
362
c .init (Cipher .UNWRAP_MODE , key );
380
363
return c .unwrap (keyEnc , "Session" , Cipher .SECRET_KEY );
@@ -456,4 +439,13 @@ private byte[] decryptSessionData(int keyAlgorithm, PrivateKey privKey, int expe
456
439
throw new PGPException ("exception decrypting session data" , e );
457
440
}
458
441
}
442
+
443
+ private static void checkRange (int pLen , byte [] enc )
444
+ throws PGPException
445
+ {
446
+ if (pLen > enc .length )
447
+ {
448
+ throw new PGPException ("encoded length out of range" );
449
+ }
450
+ }
459
451
}
0 commit comments