@@ -29,15 +29,13 @@ public class SymmetricKeyEncSessionPacket
2929 */
3030 public static final int VERSION_6 = 6 ;
3131
32- private int version ; // V4, V5, V6
33- private int encAlgorithm ; // V4, V5, V6
34- private S2K s2k ; // V4,
35- // array for exposing raw S2K parameters. Useful for forwards compat.
36- private byte [] s2kBytes ; // Makes only sense for v6, since there we have a counter
37- private byte [] secKeyData ; // V4, V5, V6
38- private int aeadAlgorithm ; // V5, V6
39- private byte [] iv ; // V5, V6
40- private byte [] authTag ; // V5, V6
32+ private final int version ; // V4, V5, V6
33+ private final int encAlgorithm ; // V4, V5, V6
34+ private final int aeadAlgorithm ; // V5, V6
35+ private S2K s2k ; // V4, V5, V6
36+ private byte [] secKeyData ; // V4, V5, V6
37+ private byte [] iv ; // V5, V6
38+ private byte [] authTag ; // V5, V6
4139
4240 public SymmetricKeyEncSessionPacket (
4341 BCPGInputStream in )
@@ -57,6 +55,7 @@ public SymmetricKeyEncSessionPacket(
5755 if (version == VERSION_4 )
5856 {
5957 encAlgorithm = in .read ();
58+ aeadAlgorithm = 0 ;
6059
6160 s2k = new S2K (in );
6261
@@ -207,6 +206,7 @@ public SymmetricKeyEncSessionPacket(
207206
208207 this .version = VERSION_4 ;
209208 this .encAlgorithm = encAlgorithm ;
209+ this .aeadAlgorithm = 0 ;
210210 this .s2k = s2k ;
211211 this .secKeyData = secKeyData ;
212212 }
@@ -339,51 +339,63 @@ public void encode(
339339 BCPGOutputStream out )
340340 throws IOException
341341 {
342+ PacketFormat packetFormat = version > 4 ? PacketFormat .CURRENT : PacketFormat .ROUNDTRIP ;
343+
342344 ByteArrayOutputStream bOut = new ByteArrayOutputStream ();
343- BCPGOutputStream pOut ;
344- if (version == 4 )
345- {
346- pOut = new BCPGOutputStream (bOut );
347- }
348- else
349- {
350- pOut = new BCPGOutputStream (bOut , true );
351- }
345+ BCPGOutputStream pOut = new BCPGOutputStream (bOut , packetFormat );
352346
353347 pOut .write (version );
354- if (version == VERSION_4 )
348+
349+ switch (version )
350+ {
351+ case VERSION_4 :
355352 {
356353 pOut .write (encAlgorithm );
357- pOut . writeObject ( s2k );
354+ s2k . encode ( pOut );
358355
359356 if (secKeyData != null && secKeyData .length > 0 )
360357 {
361358 pOut .write (secKeyData );
362359 }
360+ break ;
363361 }
364- else
362+ case VERSION_5 :
365363 {
366- int s2kLen = 0 ;
367- if (version == VERSION_6 )
368- {
369- s2kLen = s2k .getEncoded ().length ;
370- int count = 1 + 1 + 1 + s2kLen + iv .length ;
371- pOut .write (count ); // len of 5 following fields
372- }
373364 pOut .write (encAlgorithm );
374365 pOut .write (aeadAlgorithm );
375- if (version == VERSION_6 )
366+ s2k .encode (pOut );
367+ pOut .write (iv );
368+
369+ if (secKeyData != null && secKeyData .length > 0 )
376370 {
377- pOut .write (s2kLen );
371+ pOut .write (secKeyData );
378372 }
379- pOut .writeObject (s2k );
373+
374+ pOut .write (authTag );
375+ break ;
376+ }
377+ case VERSION_6 :
378+ {
379+ byte [] s2kEncoded = s2k .getEncoded ();
380+ int count = 1 + 1 + 1 + s2kEncoded .length + iv .length ; // len of 5 following fields
381+
382+ pOut .write (count );
383+ pOut .write (encAlgorithm );
384+ pOut .write (aeadAlgorithm );
385+ pOut .write (s2kEncoded .length );
386+ pOut .write (s2kEncoded );
380387 pOut .write (iv );
381388
382389 if (secKeyData != null && secKeyData .length > 0 )
383390 {
384391 pOut .write (secKeyData );
385392 }
393+
386394 pOut .write (authTag );
395+ break ;
396+ }
397+ default :
398+ throw new IllegalStateException ();
387399 }
388400
389401 pOut .close ();
0 commit comments