|
1 | 1 | package org.bouncycastle.openpgp.api;
|
2 | 2 |
|
| 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 | + |
3 | 11 | /**
|
4 | 12 | * Encryption Mode.
|
5 | 13 | */
|
@@ -34,4 +42,58 @@ public enum EncryptedDataPacketType
|
34 | 42 | * Support for this feature is signalled using {@link org.bouncycastle.bcpg.sig.Features#FEATURE_AEAD_ENCRYPTED_DATA}.
|
35 | 43 | */
|
36 | 44 | 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 | + } |
37 | 99 | }
|
0 commit comments