Skip to content

Commit 6114940

Browse files
author
gefeili
committed
revert the wrong commit #1768 Implement message decryption using SEIPDv2 and PKESKv6 packets
1 parent 1232b9f commit 6114940

11 files changed

+203
-899
lines changed

pg/src/main/java/org/bouncycastle/bcpg/PublicKeyEncSessionPacket.java

-11
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,12 @@ else if (version == VERSION_6)
6060
// anon recipient
6161
keyVersion = 0;
6262
keyFingerprint = new byte[0];
63-
keyID = 0L;
6463
}
6564
else
6665
{
6766
keyVersion = in.read();
6867
keyFingerprint = new byte[keyInfoLen - 1];
6968
in.readFully(keyFingerprint);
70-
// Derived key-ID from fingerprint
71-
// TODO: Replace with getKeyIdentifier
72-
if (keyVersion == PublicKeyPacket.VERSION_4)
73-
{
74-
keyID = FingerprintUtil.keyIdFromV4Fingerprint(keyFingerprint);
75-
}
76-
else
77-
{
78-
keyID = FingerprintUtil.keyIdFromV6Fingerprint(keyFingerprint);
79-
}
8069
}
8170
}
8271
else

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

+12-78
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ public int getSymmetricAlgorithm(
7272
{
7373
if (keyData.getVersion() == PublicKeyEncSessionPacket.VERSION_3)
7474
{
75-
byte[] plain = dataDecryptorFactory.recoverSessionData(keyData, encData);
75+
byte[] plain = dataDecryptorFactory.recoverSessionData(keyData.getAlgorithm(), keyData.getEncSessionKey());
7676
// symmetric cipher algorithm is stored in first octet of session data
7777
return plain[0];
7878
}
7979
else if (keyData.getVersion() == PublicKeyEncSessionPacket.VERSION_6)
8080
{
81-
// PKESK v6 stores the cipher algorithm in the SEIPD v2 packet fields.
81+
// PKESK v5 stores the cipher algorithm in the SEIPD v2 packet fields.
8282
return ((SymmetricEncIntegrityPacket)encData).getCipherAlgorithm();
8383
}
8484
else
@@ -98,57 +98,16 @@ public PGPSessionKey getSessionKey(
9898
PublicKeyDataDecryptorFactory dataDecryptorFactory)
9999
throws PGPException
100100
{
101-
byte[] sessionInfo = dataDecryptorFactory.recoverSessionData(keyData, encData);
102-
103-
// Confirm and discard checksum
104-
if (containsChecksum(keyData.getAlgorithm()))
105-
{
106-
if (!confirmCheckSum(sessionInfo))
107-
{
108-
throw new PGPException("Key checksum failed.");
109-
}
110-
sessionInfo = Arrays.copyOf(sessionInfo, sessionInfo.length - 2);
111-
}
112-
113-
byte[] sessionKey = Arrays.copyOfRange(sessionInfo, 1, sessionInfo.length);
114-
int algorithm;
115-
116-
// OCB (LibrePGP v5 style AEAD)
117-
if (encData instanceof AEADEncDataPacket)
101+
byte[] sessionData = dataDecryptorFactory.recoverSessionData(keyData.getAlgorithm(), keyData.getEncSessionKey());
102+
if (keyData.getAlgorithm() == PublicKeyAlgorithmTags.X25519 || keyData.getAlgorithm() == PublicKeyAlgorithmTags.X448)
118103
{
119-
algorithm = ((AEADEncDataPacket) encData).getAlgorithm();
120-
}
121-
122-
// SEIPD (OpenPGP v4 / OpenPGP v6)
123-
else if (encData instanceof SymmetricEncIntegrityPacket)
124-
{
125-
SymmetricEncIntegrityPacket seipd = (SymmetricEncIntegrityPacket) encData;
126-
if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_1)
127-
{
128-
algorithm = sessionInfo[0];
129-
}
130-
else if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_2)
131-
{
132-
algorithm = seipd.getCipherAlgorithm();
133-
}
134-
else
135-
{
136-
throw new UnsupportedPacketVersionException("Unsupported SEIPD packet version: " + seipd.getVersion());
137-
}
104+
return new PGPSessionKey(sessionData[0] & 0xff, Arrays.copyOfRange(sessionData, 1, sessionData.length));
138105
}
139-
// SED (Legacy, no integrity protection!)
140-
else
106+
if (!confirmCheckSum(sessionData))
141107
{
142-
algorithm = sessionInfo[0];
108+
throw new PGPKeyValidationException("key checksum failed");
143109
}
144-
145-
return new PGPSessionKey(algorithm & 0xff, sessionKey);
146-
}
147-
148-
private boolean containsChecksum(int algorithm)
149-
{
150-
return algorithm != PublicKeyAlgorithmTags.X25519 &&
151-
algorithm != PublicKeyAlgorithmTags.X448;
110+
return new PGPSessionKey(sessionData[0] & 0xff, Arrays.copyOfRange(sessionData, 1, sessionData.length - 2));
152111
}
153112

154113
/**
@@ -210,38 +169,13 @@ private InputStream getDataStream(
210169
}
211170
else
212171
{
172+
boolean withIntegrityPacket = encData instanceof SymmetricEncIntegrityPacket;
213173

214-
if (encData instanceof SymmetricEncIntegrityPacket)
215-
{
216-
SymmetricEncIntegrityPacket seipd = (SymmetricEncIntegrityPacket) encData;
217-
// SEIPD v1 (OpenPGP v4)
218-
if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_1)
219-
{
220-
PGPDataDecryptor dataDecryptor = dataDecryptorFactory.createDataDecryptor(true, sessionKey.getAlgorithm(), sessionKey.getKey());
221-
222-
BCPGInputStream encIn = encData.getInputStream();
174+
PGPDataDecryptor dataDecryptor = dataDecryptorFactory.createDataDecryptor(withIntegrityPacket, sessionKey.getAlgorithm(), sessionKey.getKey());
223175

224-
processSymmetricEncIntegrityPacketDataStream(true, dataDecryptor, encIn);
225-
}
226-
// SEIPD v2 (OpenPGP v6 AEAD)
227-
else
228-
{
229-
PGPDataDecryptor dataDecryptor = dataDecryptorFactory.createDataDecryptor(seipd, sessionKey);
230-
231-
BCPGInputStream encIn = encData.getInputStream();
232-
233-
encStream = new BCPGInputStream(dataDecryptor.getInputStream(encIn));
234-
}
235-
}
236-
// SED (Symmetrically Encrypted Data without Integrity Protection; Deprecated)
237-
else
238-
{
239-
PGPDataDecryptor dataDecryptor = dataDecryptorFactory.createDataDecryptor(false, sessionKey.getAlgorithm(), sessionKey.getKey());
240-
241-
BCPGInputStream encIn = encData.getInputStream();
176+
BCPGInputStream encIn = encData.getInputStream();
242177

243-
processSymmetricEncIntegrityPacketDataStream(false, dataDecryptor, encIn);
244-
}
178+
processSymmetricEncIntegrityPacketDataStream(withIntegrityPacket, dataDecryptor, encIn);
245179

246180
//
247181
// some versions of PGP appear to produce 0 for the extra

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

-76
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
package org.bouncycastle.openpgp.operator;
22

3-
import org.bouncycastle.bcpg.InputStreamPacket;
4-
import org.bouncycastle.bcpg.PublicKeyEncSessionPacket;
53
import org.bouncycastle.openpgp.PGPException;
64

75
public interface PublicKeyDataDecryptorFactory
86
extends PGPDataDecryptorFactory
97
{
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-
*/
20-
byte[] recoverSessionData(PublicKeyEncSessionPacket pkesk, InputStreamPacket encData)
8+
byte[] recoverSessionData(int keyAlgorithm, byte[][] secKeyData)
219
throws PGPException;
22-
23-
/**
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.
30-
* @param keyAlgorithm public key algorithm
31-
* @param secKeyData encrypted session key data
32-
* @param pkeskVersion version of the PKESK packet
33-
* @return decrypted session info
34-
* @throws PGPException
35-
*/
36-
byte[] recoverSessionData(int keyAlgorithm, byte[][] secKeyData, int pkeskVersion)
37-
throws PGPException;
38-
3910
}

0 commit comments

Comments
 (0)