Skip to content

Commit 8cdfe0d

Browse files
author
gefeili
committed
Seperate getSessionInfo into two versions.
1 parent a8240b8 commit 8cdfe0d

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

pg/src/main/java/org/bouncycastle/openpgp/operator/PublicKeyKeyEncryptionMethodGenerator.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public abstract class PublicKeyKeyEncryptionMethodGenerator
2323
public static final long WILDCARD_KEYID = 0L;
2424
/**
2525
* @deprecated use WILDCARD_KEYID
26-
* */
26+
*/
2727
public static final long WILDCARD = 0L;
2828
public static final byte[] WILDCARD_FINGERPRINT = new byte[0];
2929

@@ -173,13 +173,12 @@ private byte[] convertToEncodedMPI(byte[] encryptedSessionInfo)
173173
*
174174
* @param sessionInfo session-key algorithm + session-key + checksum
175175
* @return version 3 PKESK packet
176-
*
177176
* @throws PGPException
178177
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-version-3-public-key-encryp">
179-
* RFC9580 - Version 3 Public Key Encrypted Session Key Packet</a>
178+
* RFC9580 - Version 3 Public Key Encrypted Session Key Packet</a>
180179
*/
181180
public ContainedPacket generateV3(byte[] sessionInfo)
182-
throws PGPException
181+
throws PGPException
183182
{
184183
long keyId;
185184
if (useWildcardRecipient)
@@ -203,10 +202,9 @@ public ContainedPacket generateV3(byte[] sessionInfo)
203202
*
204203
* @param sessionInfo session-key algorithm id + session-key + checksum
205204
* @return PKESKv6 packet
206-
*
207205
* @throws PGPException if the PKESK packet cannot be generated
208206
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-version-6-public-key-encryp">
209-
* RFC9580 - Version 6 Public Key Encrypted Session Key Packet</a>
207+
* RFC9580 - Version 6 Public Key Encrypted Session Key Packet</a>
210208
*/
211209
public ContainedPacket generateV6(byte[] sessionInfo)
212210
throws PGPException
@@ -227,7 +225,7 @@ public ContainedPacket generateV6(byte[] sessionInfo)
227225
byte[] sessionInfoWithoutAlgId = new byte[sessionInfo.length - 1];
228226
System.arraycopy(sessionInfo, 1, sessionInfoWithoutAlgId, 0, sessionInfoWithoutAlgId.length);
229227

230-
byte[] encryptedSessionInfo = encryptSessionInfo(pubKey, sessionInfo, sessionInfoWithoutAlgId, (byte)0);
228+
byte[] encryptedSessionInfo = encryptSessionInfo(pubKey, sessionInfo, sessionInfoWithoutAlgId, (byte)0);
231229
byte[][] encodedEncSessionInfo = encodeEncryptedSessionInfo(encryptedSessionInfo);
232230
return PublicKeyEncSessionPacket.createV6PKESKPacket(keyVersion, keyFingerprint, pubKey.getAlgorithm(), encodedEncSessionInfo);
233231
}
@@ -250,19 +248,29 @@ protected abstract byte[] encryptSessionInfo(PGPPublicKey pubKey,
250248

251249
protected static byte[] getSessionInfo(byte[] ephPubEncoding, byte optSymKeyAlgorithm, byte[] wrappedSessionKey)
252250
{
253-
int len = ephPubEncoding.length + wrappedSessionKey.length + (optSymKeyAlgorithm != 0 ? 2 : 1);
251+
int len = ephPubEncoding.length + wrappedSessionKey.length + 2;
254252
byte[] out = new byte[len];
255253
// ephemeral pub key
256254
System.arraycopy(ephPubEncoding, 0, out, 0, ephPubEncoding.length);
257255
// len of two/one next fields
258-
out[ephPubEncoding.length] = (byte)(wrappedSessionKey.length + (optSymKeyAlgorithm != 0 ? 1 : 0));
259-
// (optional) sym key alg
260-
if (optSymKeyAlgorithm != 0)
261-
{
262-
out[ephPubEncoding.length + 1] = optSymKeyAlgorithm;
263-
}
256+
out[ephPubEncoding.length] = (byte)(wrappedSessionKey.length + 1);
257+
// sym key alg
258+
out[ephPubEncoding.length + 1] = optSymKeyAlgorithm;
264259
// wrapped session key
265260
System.arraycopy(wrappedSessionKey, 0, out, len - wrappedSessionKey.length, wrappedSessionKey.length);
266261
return out;
267262
}
263+
264+
protected static byte[] getSessionInfo(byte[] ephPubEncoding, byte[] wrappedSessionKey)
265+
{
266+
int len = ephPubEncoding.length + wrappedSessionKey.length + 1;
267+
byte[] out = new byte[len];
268+
// ephemeral pub key
269+
System.arraycopy(ephPubEncoding, 0, out, 0, ephPubEncoding.length);
270+
// len of two/one next fields
271+
out[ephPubEncoding.length] = (byte)wrappedSessionKey.length;
272+
// wrapped session key
273+
System.arraycopy(wrappedSessionKey, 0, out, ephPubEncoding.length + 1, wrappedSessionKey.length);
274+
return out;
275+
}
268276
}

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPublicKeyKeyEncryptionMethodGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private byte[] encryptSessionInfoWithECDHKey(byte[] sessionInfo,
220220
// wrap the padded session info using the shared-secret public key
221221
// https://www.rfc-editor.org/rfc/rfc9580.html#section-11.5-16
222222
return getSessionInfo(new MPInteger(new BigInteger(1, ephPubEncoding))
223-
.getEncoded(), (byte)0, getWrapper(symmetricKeyAlgorithm, key, paddedSessionData));
223+
.getEncoded(), getWrapper(symmetricKeyAlgorithm, key, paddedSessionData));
224224
}
225225

226226
private byte[] encryptSessionInfoWithX25519X448Key(PublicKeyPacket pubKeyPacket,

pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcePublicKeyKeyEncryptionMethodGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private byte[] encryptSessionInfoWithECDHKey(PublicKeyPacket pubKeyPacket, Strin
265265
// wrap the padded session info using the shared-secret public key
266266
// https://www.rfc-editor.org/rfc/rfc9580.html#section-11.5-16
267267
return getSessionInfo(new MPInteger(new BigInteger(1, ephPubEncoding))
268-
.getEncoded(), (byte)0, getWrapper(symmetricKeyAlgorithm, sessionInfo, secret, paddedSessionData));
268+
.getEncoded(), getWrapper(symmetricKeyAlgorithm, sessionInfo, secret, paddedSessionData));
269269
}
270270

271271
/**

0 commit comments

Comments
 (0)