@@ -155,8 +155,8 @@ private byte[] convertToEncodedMPI(byte[] encryptedSessionInfo)
155155 }
156156 }
157157
158- public ContainedPacket generateV3 (int encAlgorithm , byte [] sessionInfo )
159- throws PGPException
158+ public ContainedPacket generateV3 (byte [] sessionInfo )
159+ throws PGPException
160160 {
161161 long keyId ;
162162 if (useWildcardRecipient )
@@ -167,7 +167,7 @@ public ContainedPacket generateV3(int encAlgorithm, byte[] sessionInfo)
167167 {
168168 keyId = pubKey .getKeyID ();
169169 }
170- byte [] encryptedSessionInfo = encryptSessionInfoV3 (pubKey , sessionInfo );
170+ byte [] encryptedSessionInfo = encryptSessionInfo (pubKey , sessionInfo , sessionInfo , sessionInfo [ 0 ] );
171171 byte [][] encodedEncSessionInfo = encodeEncryptedSessionInfo (encryptedSessionInfo );
172172 return PublicKeyEncSessionPacket .createV3PKESKPacket (keyId , pubKey .getAlgorithm (), encodedEncSessionInfo );
173173 }
@@ -187,91 +187,75 @@ public ContainedPacket generateV6(byte[] sessionInfo)
187187 keyFingerprint = pubKey .getFingerprint ();
188188 keyVersion = pubKey .getVersion ();
189189 }
190- byte [] encryptedSessionInfo = encryptSessionInfoV6 (pubKey , sessionInfo );
190+ // In V6, do not include the symmetric-key algorithm in the session-info
191+ byte [] sessionInfoWithoutAlgId = new byte [sessionInfo .length - 1 ];
192+ System .arraycopy (sessionInfo , 1 , sessionInfoWithoutAlgId , 0 , sessionInfoWithoutAlgId .length );
193+
194+ byte [] encryptedSessionInfo = encryptSessionInfo (pubKey , sessionInfo , sessionInfoWithoutAlgId , (byte )0 );
191195 byte [][] encodedEncSessionInfo = encodeEncryptedSessionInfo (encryptedSessionInfo );
192196 return PublicKeyEncSessionPacket .createV6PKESKPacket (keyVersion , keyFingerprint , pubKey .getAlgorithm (), encodedEncSessionInfo );
193197 }
194198
195199 /**
196200 * Encrypt a session key using the recipients public key.
197- * @param pubKey recipients public key
198- * @param fullSessionInfo full session info (sym-alg-id + session-key + 2 octet checksum)
201+ *
202+ * @param pubKey recipients public key
203+ * @param fullSessionInfo full session info (sym-alg-id + session-key + 2 octet checksum)
199204 * @param sessionInfoToEncrypt for v3: full session info; for v6: just the session-key
200- * @param optSymAlgId for v3: session key algorithm ID; for v6: empty array
205+ * @param optSymAlgId for v3: session key algorithm ID; for v6: empty array
201206 * @return encrypted session info
202207 * @throws PGPException
203208 */
204209 protected abstract byte [] encryptSessionInfo (PGPPublicKey pubKey ,
205210 byte [] fullSessionInfo ,
206211 byte [] sessionInfoToEncrypt ,
207- byte [] optSymAlgId )
212+ byte optSymAlgId )
208213 throws PGPException ;
209214
210- /**
211- * Encrypt a session key for a v3 PKESK.
212- * @param pubKey recipients public key
213- * @param sessionInfo session info (sym-alg-id + session-key + 2 octet checksum)
214- * @return encrypted session info
215- * @throws PGPException
216- */
217- protected byte [] encryptSessionInfoV3 (PGPPublicKey pubKey , byte [] sessionInfo )
218- throws PGPException
219- {
220- return encryptSessionInfo (pubKey , sessionInfo , sessionInfo , new byte []{sessionInfo [0 ]});
221- }
222-
223- /**
224- * Encrypt a session key for a v6 PKESK.
225- * @param pubKey recipients public key
226- * @param sessionInfo session info (sym-alg-id + session-key + 2 octet checksum)
227- * @return encrypted session info
228- * @throws PGPException
229- */
230- protected byte [] encryptSessionInfoV6 (PGPPublicKey pubKey , byte [] sessionInfo )
231- throws PGPException
232- {
233- // In V6, do not include the symmetric-key algorithm in the session-info
234- byte [] sessionInfoWithoutAlgId = new byte [sessionInfo .length - 1 ];
235- System .arraycopy (sessionInfo , 1 , sessionInfoWithoutAlgId , 0 , sessionInfoWithoutAlgId .length );
236-
237- return encryptSessionInfo (pubKey , sessionInfo , sessionInfoWithoutAlgId , new byte [0 ]);
238- }
239215
240216 protected static byte [] concatECDHEphKeyWithWrappedSessionKey (byte [] ephPubEncoding , byte [] wrappedSessionKey )
241217 throws IOException
242218 {
243219 // https://www.rfc-editor.org/rfc/rfc9580.html#section-11.5-16
244220
245221 byte [] mpiEncodedEphemeralKey = new MPInteger (new BigInteger (1 , ephPubEncoding ))
246- .getEncoded ();
222+ .getEncoded ();
247223 byte [] out = new byte [mpiEncodedEphemeralKey .length + 1 + wrappedSessionKey .length ];
248224 // eph key
249225 System .arraycopy (mpiEncodedEphemeralKey , 0 , out , 0 , mpiEncodedEphemeralKey .length );
250226 // enc session-key len
251- out [mpiEncodedEphemeralKey .length ] = (byte ) wrappedSessionKey .length ;
227+ out [mpiEncodedEphemeralKey .length ] = (byte )wrappedSessionKey .length ;
252228 // enc session-key
253229 System .arraycopy (wrappedSessionKey , 0 , out , mpiEncodedEphemeralKey .length + 1 , wrappedSessionKey .length );
254230
255231 return out ;
256232 }
257233
258- // private static byte[] getSessionInfo(byte[] ephPubEncoding, int symmetricKeyAlgorithm, byte[] c)
259- // {
260- // return getSessionInfo(ephPubEncoding, new byte[]{(byte) symmetricKeyAlgorithm}, c);
261- // }
262-
263- protected static byte [] getSessionInfo (byte [] ephPubEncoding , byte [] optSymKeyAlgorithm , byte [] wrappedSessionKey )
234+ protected static byte [] getSessionInfo (byte [] ephPubEncoding , byte optSymKeyAlgorithm , byte [] wrappedSessionKey )
264235 {
265- int len = ephPubEncoding .length + 1 + optSymKeyAlgorithm .length + wrappedSessionKey .length ;
236+ int len = ephPubEncoding .length + 1 + wrappedSessionKey .length ;
237+ if (optSymKeyAlgorithm != 0 )
238+ {
239+ len ++;
240+ }
266241 byte [] out = new byte [len ];
267242 // ephemeral pub key
268243 System .arraycopy (ephPubEncoding , 0 , out , 0 , ephPubEncoding .length );
269244 // len of two/one next fields
270- out [ephPubEncoding .length ] = (byte ) (wrappedSessionKey .length + optSymKeyAlgorithm . length );
245+ out [ephPubEncoding .length ] = (byte )(wrappedSessionKey .length + 1 );
271246 // (optional) sym key alg
272- System .arraycopy (optSymKeyAlgorithm , 0 , out , ephPubEncoding .length + 1 , optSymKeyAlgorithm .length );
273- // wrapped session key
274- System .arraycopy (wrappedSessionKey , 0 , out , ephPubEncoding .length + 1 + optSymKeyAlgorithm .length , wrappedSessionKey .length );
247+ if (optSymKeyAlgorithm != 0 )
248+ {
249+ out [ephPubEncoding .length + 1 ] = optSymKeyAlgorithm ;
250+ // wrapped session key
251+ System .arraycopy (wrappedSessionKey , 0 , out , ephPubEncoding .length + 1 + 1 , wrappedSessionKey .length );
252+ }
253+ else
254+ {
255+ // wrapped session key
256+ System .arraycopy (wrappedSessionKey , 0 , out , ephPubEncoding .length + 1 , wrappedSessionKey .length );
257+ }
258+
275259 return out ;
276260 }
277261}
0 commit comments