Skip to content

Commit 9be6e4c

Browse files
committed
Guard the null mode in MessageEncryptionMechanism hashCode and toString, so the unencrypted mechanism can be hashed and printed like any other.
1 parent c574aa7 commit 9be6e4c

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎CONTRIBUTORS.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@
450450
<li>Adam Vartanian &lt;https://github.com/flooey&gt; use of ShortBuffer exception and buffer size pre-check in Cipher.doFinal().</li>
451451
<li>Bernd &lt;https://github.com/ecki&gt; Fix to make PGPUtil.pipeFileContents use buffer and not leak file handle.</li>
452452
<li>Shartung &lt;https://github.com/shartung&gt; Additional EC Key Agreement algorithms in support of German BSI TR-03111.</li>
453-
<li>Paul Schaub &lt;https://github.com/vanitasvitae&gt; bringing PGPSecretKey.getUserIds() into line with PGPPublicKey.getUserIds(). Exception message fix in BcPublicKeyDataDecryptorFactory. Additional tests on PGP key ring generation. Improved functionality of PGPSignatureSubpacketGenerator, PGPPublicKeyRing. Tweaks to PGPDataEncryptorBuilder interface, fix for JcaPGP/BcPGP Ed25519 private key conversion. Added configurable CRC detection to ArmoredInputStream, additional control character skipping in ArmoredInputStream. Rewind code for PGPPBEEncryptedData, addition of PGPSignature.getDigestPrefix(). Wrong list traversal fix in PGPSecretKeyRing. Further improvement to use of generics in PGP API. General interop improvements. PGP Public / Secure keyring ignore marker packets when reading. Initial work on PGP session key handling, filtering literal data for canoncialization. Addition of direct key identified key-ring construction. PGPSecretKeyRing.insertOrReplacePublicKey addition. Addition of utility methods for joining/merging signatures and public keys. Addition of PGP regexp packet, PolicyURI packet handling, UTF8 comment testing. Efficiency improvements to TruncatedStream. Initial Argon2 support for OpenPGP. General cleanups. Fast CRC24 implementation, SHA3 addtions to BcImplProvider, improvements to One Pass Signature support, signatue validation, read() consistency in BCPGInputStream. Contributions to AEAD support (v6 & v5) in PGP API. Addition of PGP WildCard ID, moving the PGP example code into the 21st century. Security patches for encrypted data generation, initial thread safe certification verification. Support for V6 EC keys, V6 signatures, V6 encryption, V6 PKESK, PGP packet criticality, and Preferred AEAD CipherSuites sigsubpacket support. Introduce high-level OpenPGP API for message creation/consumption and certificate evaluation. OpenPGP fuzz testing. Fix to prevent a null pointer exception on processing a partial stripped key. Moving the Argon2 memory size exponent bounds check from S2K packet parsing to decryption time. ArmoredInputStream CSF dash-escape hardening. Report and initial patch for OnePassSignaturePacket defaulting to the Legacy packet format for v6 packets (github #2347). PGPKeyPairGenerator factory methods for the OpenPGP brainpool curves (github #2375). Support for OpenPGP External Secret Keys, and the initial OpenPGP smart card API (bcpgsc) with YubiKey and simulator backends (github #2339). Initial implementation of the JCE bindings for smart card decryption and the pluggable YubiKey decryptor factory provider (github #2374).</li>
453+
<li>Paul Schaub &lt;https://github.com/vanitasvitae&gt; bringing PGPSecretKey.getUserIds() into line with PGPPublicKey.getUserIds(). Exception message fix in BcPublicKeyDataDecryptorFactory. Additional tests on PGP key ring generation. Improved functionality of PGPSignatureSubpacketGenerator, PGPPublicKeyRing. Tweaks to PGPDataEncryptorBuilder interface, fix for JcaPGP/BcPGP Ed25519 private key conversion. Added configurable CRC detection to ArmoredInputStream, additional control character skipping in ArmoredInputStream. Rewind code for PGPPBEEncryptedData, addition of PGPSignature.getDigestPrefix(). Wrong list traversal fix in PGPSecretKeyRing. Further improvement to use of generics in PGP API. General interop improvements. PGP Public / Secure keyring ignore marker packets when reading. Initial work on PGP session key handling, filtering literal data for canoncialization. Addition of direct key identified key-ring construction. PGPSecretKeyRing.insertOrReplacePublicKey addition. Addition of utility methods for joining/merging signatures and public keys. Addition of PGP regexp packet, PolicyURI packet handling, UTF8 comment testing. Efficiency improvements to TruncatedStream. Initial Argon2 support for OpenPGP. General cleanups. Fast CRC24 implementation, SHA3 addtions to BcImplProvider, improvements to One Pass Signature support, signatue validation, read() consistency in BCPGInputStream. Contributions to AEAD support (v6 & v5) in PGP API. Addition of PGP WildCard ID, moving the PGP example code into the 21st century. Security patches for encrypted data generation, initial thread safe certification verification. Support for V6 EC keys, V6 signatures, V6 encryption, V6 PKESK, PGP packet criticality, and Preferred AEAD CipherSuites sigsubpacket support. Introduce high-level OpenPGP API for message creation/consumption and certificate evaluation. OpenPGP fuzz testing. Fix to prevent a null pointer exception on processing a partial stripped key. Moving the Argon2 memory size exponent bounds check from S2K packet parsing to decryption time. ArmoredInputStream CSF dash-escape hardening. Report and initial patch for OnePassSignaturePacket defaulting to the Legacy packet format for v6 packets (github #2347). PGPKeyPairGenerator factory methods for the OpenPGP brainpool curves (github #2375). Support for OpenPGP External Secret Keys, and the initial OpenPGP smart card API (bcpgsc) with YubiKey and simulator backends (github #2339). Initial implementation of the JCE bindings for smart card decryption and the pluggable YubiKey decryptor factory provider (github #2374). Correcting MessageEncryptionMechanism.unencrypted() to report no encryption mode rather than SEIPDv1.</li>
454454
<li>Nick of Nexxar &lt;https://github.com/nros&gt; update to OpenPGP package to handle a broader range of EC curves.</li>
455455
<li>catbref &lt;https://github.com/catbref&gt; sample implementation of RFC 7748/Ed25519 (incorporated work from github users Valodim and str4d as well).</li>
456456
<li>gerlion &lt;https://github.com/gerlion&gt; detection of concurrency issue with pre-1.60 EC math library.</li>

‎pg/src/main/java/org/bouncycastle/openpgp/api/MessageEncryptionMechanism.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ private MessageEncryptionMechanism(EncryptedDataPacketType mode,
2929
this.aeadAlgorithm = aeadAlgorithm;
3030
}
3131

32+
/**
33+
* Return the encryption mode (packet type) the message will use, or null where it will not be
34+
* encrypted at all - see {@link #unencrypted()}, which a sign-only message uses. Test with
35+
* {@link #isEncrypted()} before switching on the result.
36+
*
37+
* @return encryption mode, null if unencrypted
38+
*/
3239
public EncryptedDataPacketType getMode()
3340
{
3441
return mode;
@@ -119,7 +126,8 @@ public boolean isEncrypted()
119126
@Override
120127
public int hashCode()
121128
{
122-
return mode.hashCode()
129+
// an unencrypted mechanism has no mode
130+
return (mode == null ? 0 : mode.hashCode())
123131
+ 13 * symmetricKeyAlgorithm
124132
+ 17 * aeadAlgorithm;
125133
}
@@ -148,6 +156,11 @@ && getSymmetricKeyAlgorithm() == m.getSymmetricKeyAlgorithm()
148156
@Override
149157
public String toString()
150158
{
159+
if (mode == null)
160+
{
161+
return "unencrypted";
162+
}
163+
151164
String out = mode.name() + "[cipher: " + symmetricKeyAlgorithm;
152165
if (mode == EncryptedDataPacketType.SEIPDv2 || mode == EncryptedDataPacketType.LIBREPGP_OED)
153166
{

‎pg/src/test/java/org/bouncycastle/openpgp/api/test/OpenPGPMessageProcessorTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,11 @@ private void processUnencryptedMessage(OpenPGPApi api)
731731
OpenPGPMessageInputStream.Result result = mIn.getResult();
732732
isTrue(result.getEncryptionMethod().equals(MessageEncryptionMechanism.unencrypted()));
733733
isNull(result.getEncryptionMethod().getMode());
734+
735+
// the null mode must not reach the hashCode/toString of an otherwise ordinary object -
736+
// equals() holding while hashCode() throws would also break any hashed collection
737+
isEquals(MessageEncryptionMechanism.unencrypted().hashCode(), result.getEncryptionMethod().hashCode());
738+
isEquals("unencrypted", result.getEncryptionMethod().toString());
734739
}
735740

736741
private void testVerificationOfSEIPD1MessageWithTamperedCiphertext(OpenPGPApi api)

0 commit comments

Comments
 (0)