Skip to content

Commit b3d23e3

Browse files
committed
Implement EncryptedDataPacketType.of() and expose PGPEncryptedDataList.getEncryptedData()
1 parent 6509a25 commit b3d23e3

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ public PGPEncryptedData get(
154154
return (PGPEncryptedData)methods.get(index);
155155
}
156156

157+
public InputStreamPacket getEncryptedData()
158+
{
159+
return data;
160+
}
161+
157162
/**
158163
* Gets the number of encryption methods in this list.
159164
*/

pg/src/main/java/org/bouncycastle/openpgp/api/EncryptedDataPacketType.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package org.bouncycastle.openpgp.api;
22

3+
import org.bouncycastle.bcpg.AEADEncDataPacket;
4+
import org.bouncycastle.bcpg.InputStreamPacket;
5+
import org.bouncycastle.bcpg.SymmetricEncDataPacket;
6+
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
7+
import org.bouncycastle.bcpg.UnsupportedPacketVersionException;
8+
import org.bouncycastle.openpgp.PGPEncryptedDataList;
9+
import org.bouncycastle.openpgp.PGPException;
10+
311
/**
412
* Encryption Mode.
513
*/
@@ -34,4 +42,58 @@ public enum EncryptedDataPacketType
3442
* Support for this feature is signalled using {@link org.bouncycastle.bcpg.sig.Features#FEATURE_AEAD_ENCRYPTED_DATA}.
3543
*/
3644
LIBREPGP_OED // "v5"
45+
;
46+
47+
/**
48+
* Detect the type of the PGPEncryptedDataList's encrypted data packet.
49+
*
50+
* @param encDataList encrypted data list
51+
* @return encrypted data packet type
52+
* @throws PGPException if an unexpected data packet is encountered.
53+
*/
54+
public static EncryptedDataPacketType of(PGPEncryptedDataList encDataList)
55+
throws PGPException
56+
{
57+
return of(encDataList.getEncryptedData());
58+
}
59+
60+
/**
61+
* Detect the type the provided encrypted data packet.
62+
*
63+
* @param encData encrypted data packet
64+
* @return encrypted data packet type
65+
* @throws PGPException if an unexpected data packet is encountered.
66+
*/
67+
public static EncryptedDataPacketType of(InputStreamPacket encData)
68+
throws PGPException
69+
{
70+
if (encData instanceof SymmetricEncIntegrityPacket)
71+
{
72+
SymmetricEncIntegrityPacket seipd = (SymmetricEncIntegrityPacket) encData;
73+
if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_1)
74+
{
75+
return SEIPDv1;
76+
}
77+
else if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_2)
78+
{
79+
return SEIPDv2;
80+
}
81+
else
82+
{
83+
throw new UnsupportedPacketVersionException("Symmetrically-Encrypted Integrity-Protected Data Packet of unknown version encountered: " + seipd.getVersion());
84+
}
85+
}
86+
else if (encData instanceof AEADEncDataPacket)
87+
{
88+
return LIBREPGP_OED;
89+
}
90+
else if (encData instanceof SymmetricEncDataPacket)
91+
{
92+
return SED;
93+
}
94+
else
95+
{
96+
throw new PGPException("Unexpected packet type: " + encData.getClass().getName());
97+
}
98+
}
3799
}

0 commit comments

Comments
 (0)