Skip to content

Commit 328ab86

Browse files
committed
Reduce diff
1 parent f3cacf7 commit 328ab86

File tree

5 files changed

+40
-48
lines changed

5 files changed

+40
-48
lines changed

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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 4 additions & 14 deletions
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

Lines changed: 17 additions & 2 deletions
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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,4 @@ private static byte[] unwrapSessionData(byte[] keyEnc, int symmetricKeyAlgorithm
307307
c.init(false, key);
308308
return c.unwrap(keyEnc, 0, keyEnc.length);
309309
}
310-
311-
private static void checkRange(int pLen, byte[] enc)
312-
throws PGPException
313-
{
314-
if (pLen > enc.length)
315-
{
316-
throw new PGPException("encoded length out of range");
317-
}
318-
}
319310
}

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ public byte[] recoverSessionData(PublicKeyEncSessionPacket pkesk, InputStreamPac
146146
public byte[] recoverSessionData(int keyAlgorithm, byte[][] secKeyData, int pkeskVersion)
147147
throws PGPException
148148
{
149-
if (keyAlgorithm == PublicKeyAlgorithmTags.ECDH ||
150-
keyAlgorithm == PublicKeyAlgorithmTags.X25519 ||
151-
keyAlgorithm == PublicKeyAlgorithmTags.X448)
149+
if (keyAlgorithm == PublicKeyAlgorithmTags.ECDH || keyAlgorithm == PublicKeyAlgorithmTags.X25519 || keyAlgorithm == PublicKeyAlgorithmTags.X448)
152150
{
153151
throw new PGPException("ECDH requires use of PGPPrivateKey for decryption");
154152
}
@@ -263,18 +261,12 @@ private byte[] decryptSessionData(JcaPGPKeyConverter converter, PGPPrivateKey pr
263261
byte[] keyEnc;
264262

265263
pLen = ((((enc[0] & 0xff) << 8) + (enc[1] & 0xff)) + 7) / 8;
266-
if ((2 + pLen + 1) > enc.length)
267-
{
268-
throw new PGPException("encoded length out of range");
269-
}
264+
checkRange(2 + pLen + 1, enc);
270265

271266
pEnc = new byte[pLen];
272267
System.arraycopy(enc, 2, pEnc, 0, pLen);
273268
int keyLen = enc[pLen + 2] & 0xff;
274-
if ((2 + pLen + 1 + keyLen) > enc.length)
275-
{
276-
throw new PGPException("encoded length out of range");
277-
}
269+
checkRange(2 + pLen + 1 + keyLen, enc);
278270

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

351343
int size = enc[pLen] & 0xff;
352-
// checkRange
353-
if ((pLen + 1 + size) > enc.length)
354-
{
355-
throw new PGPException("encoded length out of range");
356-
}
344+
345+
checkRange(pLen + 1 + size, enc);
357346

358347
// encrypted session key
359348
int sesKeyLen = size - (containsSKAlg ? 1 : 0);
@@ -458,4 +447,13 @@ private byte[] decryptSessionData(int keyAlgorithm, PrivateKey privKey, int expe
458447
throw new PGPException("exception decrypting session data", e);
459448
}
460449
}
450+
451+
private static void checkRange(int pLen, byte[] enc)
452+
throws PGPException
453+
{
454+
if (pLen > enc.length)
455+
{
456+
throw new PGPException("encoded length out of range");
457+
}
458+
}
461459
}

0 commit comments

Comments
 (0)