Skip to content

Commit 463548d

Browse files
committed
Reduce diff
1 parent d6260e4 commit 463548d

5 files changed

+43
-57
lines changed

pg/src/main/java/org/bouncycastle/openpgp/PGPPublicKeyEncryptedData.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public class PGPPublicKeyEncryptedData
3535
}
3636

3737
private boolean confirmCheckSum(
38-
byte[] sessionInfo)
38+
byte[] sessionInfo)
3939
{
4040
int check = 0;
41+
4142
for (int i = 1; i != sessionInfo.length - 2; i++)
4243
{
4344
check += sessionInfo[i] & 0xff;
@@ -98,6 +99,8 @@ public PGPSessionKey getSessionKey(
9899
throws PGPException
99100
{
100101
byte[] sessionInfo = dataDecryptorFactory.recoverSessionData(keyData, encData);
102+
103+
// Confirm and discard checksum
101104
if (containsChecksum(keyData.getAlgorithm()))
102105
{
103106
if (!confirmCheckSum(sessionInfo))
@@ -107,15 +110,13 @@ public PGPSessionKey getSessionKey(
107110
sessionInfo = Arrays.copyOf(sessionInfo, sessionInfo.length - 2);
108111
}
109112

110-
111-
byte[] sessionKey;
113+
byte[] sessionKey = Arrays.copyOfRange(sessionInfo, 1, sessionInfo.length);
112114
int algorithm;
113115

114116
// OCB (LibrePGP v5 style AEAD)
115117
if (encData instanceof AEADEncDataPacket)
116118
{
117119
algorithm = ((AEADEncDataPacket) encData).getAlgorithm();
118-
sessionKey = Arrays.copyOfRange(sessionInfo, 1, sessionInfo.length);
119120
}
120121

121122
// SEIPD (OpenPGP v4 / OpenPGP v6)
@@ -125,12 +126,10 @@ else if (encData instanceof SymmetricEncIntegrityPacket)
125126
if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_1)
126127
{
127128
algorithm = sessionInfo[0];
128-
sessionKey = Arrays.copyOfRange(sessionInfo, 1, sessionInfo.length);
129129
}
130130
else if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_2)
131131
{
132132
algorithm = seipd.getCipherAlgorithm();
133-
sessionKey = Arrays.copyOfRange(sessionInfo, 1, sessionInfo.length);
134133
}
135134
else
136135
{
@@ -141,7 +140,6 @@ else if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_2)
141140
else
142141
{
143142
algorithm = sessionInfo[0];
144-
sessionKey = Arrays.copyOfRange(sessionInfo, 1, sessionInfo.length);
145143
}
146144

147145
return new PGPSessionKey(algorithm & 0xff, sessionKey);

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

+4-14
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,12 @@ protected boolean containsSKAlg(int pkeskVersion)
5656
return pkeskVersion != PublicKeyEncSessionPacket.VERSION_6;
5757
}
5858

59-
protected boolean confirmCheckSum(
60-
byte[] sessionInfo, int algorithm)
59+
protected static void checkRange(int pLen, byte[] enc)
60+
throws PGPException
6161
{
62-
// X25519, X448 does not include a checksum
63-
if (algorithm == PublicKeyAlgorithmTags.X25519 || algorithm == PublicKeyAlgorithmTags.X448)
62+
if (pLen > enc.length)
6463
{
65-
return true;
64+
throw new PGPException("encoded length out of range");
6665
}
67-
68-
int check = 0;
69-
for (int i = 1; i != sessionInfo.length - 2; i++)
70-
{
71-
check += sessionInfo[i] & 0xff;
72-
}
73-
74-
return (sessionInfo[sessionInfo.length - 2] == (byte)(check >> 8))
75-
&& (sessionInfo[sessionInfo.length - 1] == (byte)(check));
7666
}
7767
}

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@
77
public interface PublicKeyDataDecryptorFactory
88
extends PGPDataDecryptorFactory
99
{
10+
/**
11+
* Recover the plain session info by decrypting the encrypted session key.
12+
* The session info ALWAYS has the symmetric algorithm ID prefixed, so the return value is:
13+
* <pre>[sym-alg][session-key][checksum]?</pre>
14+
*
15+
* @param pkesk public-key encrypted session-key packet
16+
* @param encData encrypted data (sed/seipd/oed) packet
17+
* @return decrypted session info
18+
* @throws PGPException
19+
*/
1020
byte[] recoverSessionData(PublicKeyEncSessionPacket pkesk, InputStreamPacket encData)
1121
throws PGPException;
1222

1323
/**
14-
* @deprecated use {@link #recoverSessionData(PublicKeyEncSessionPacket, InputStreamPacket)} (PublicKeyEncSessionPacket, InputStreamPacket)} instead.
24+
* Recover the plain session info by decrypting the encrypted session key.
25+
* This method returns the decrypted session info as-is (without prefixing missing cipher algorithm),
26+
* so the return value is:
27+
* <pre>[sym-alg]?[session-key][checksum]?</pre>
28+
*
29+
* @deprecated use {@link #recoverSessionData(PublicKeyEncSessionPacket, InputStreamPacket)} instead.
1530
* @param keyAlgorithm public key algorithm
1631
* @param secKeyData encrypted session key data
1732
* @param pkeskVersion version of the PKESK packet
18-
* @return
33+
* @return decrypted session info
1934
* @throws PGPException
2035
*/
2136
byte[] recoverSessionData(int keyAlgorithm, byte[][] secKeyData, int pkeskVersion)

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

-9
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,4 @@ private static byte[] unwrapSessionData(byte[] keyEnc, int symmetricKeyAlgorithm
297297
c.init(false, key);
298298
return c.unwrap(keyEnc, 0, keyEnc.length);
299299
}
300-
301-
private static void checkRange(int pLen, byte[] enc)
302-
throws PGPException
303-
{
304-
if (pLen > enc.length)
305-
{
306-
throw new PGPException("encoded length out of range");
307-
}
308-
}
309300
}

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

+17-25
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ public byte[] recoverSessionData(PublicKeyEncSessionPacket pkesk, InputStreamPac
147147
public byte[] recoverSessionData(int keyAlgorithm, byte[][] secKeyData, int pkeskVersion)
148148
throws PGPException
149149
{
150-
if (keyAlgorithm == PublicKeyAlgorithmTags.ECDH ||
151-
keyAlgorithm == PublicKeyAlgorithmTags.X25519 ||
152-
keyAlgorithm == PublicKeyAlgorithmTags.X448)
150+
if (keyAlgorithm == PublicKeyAlgorithmTags.ECDH || keyAlgorithm == PublicKeyAlgorithmTags.X25519 || keyAlgorithm == PublicKeyAlgorithmTags.X448)
153151
{
154152
throw new PGPException("ECDH requires use of PGPPrivateKey for decryption");
155153
}
@@ -264,18 +262,12 @@ private byte[] decryptSessionData(JcaPGPKeyConverter converter, PGPPrivateKey pr
264262
byte[] keyEnc;
265263

266264
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);
271266

272267
pEnc = new byte[pLen];
273268
System.arraycopy(enc, 2, pEnc, 0, pLen);
274269
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);
279271

280272
keyEnc = new byte[keyLen];
281273
System.arraycopy(enc, 2 + pLen + 1, keyEnc, 0, keyLen);
@@ -341,11 +333,8 @@ private byte[] decryptSessionData(JcaPGPKeyConverter converter, PGPPrivateKey pr
341333
byte[] ephemeralKey = Arrays.copyOf(enc, pLen);
342334

343335
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);
349338

350339
// encrypted session key
351340
int sesKeyLen = size - (containsSKAlg ? 1 : 0);
@@ -363,18 +352,12 @@ private byte[] decryptSessionData(JcaPGPKeyConverter converter, PGPPrivateKey pr
363352
}
364353
}
365354

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)
373357
throws PGPException, GeneralSecurityException
374358
{
375359
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);
378361
Cipher c = helper.createKeyWrapper(symmetricKeyAlgorithm);
379362
c.init(Cipher.UNWRAP_MODE, key);
380363
return c.unwrap(keyEnc, "Session", Cipher.SECRET_KEY);
@@ -456,4 +439,13 @@ private byte[] decryptSessionData(int keyAlgorithm, PrivateKey privKey, int expe
456439
throw new PGPException("exception decrypting session data", e);
457440
}
458441
}
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+
}
459451
}

0 commit comments

Comments
 (0)