Skip to content

Commit 400d275

Browse files
committed
A version 6 OpenPGP certificate is now used only when its primary key carries a valid Direct Key signature, as RFC 9580 sec. 5.2.3.10 requires, rather than falling back to the primary user ID binding the way a version 4 certificate legitimately does, since it is the Direct Key signature that carries a version 6 key's expiration and preferences.
1 parent 658e247 commit 400d275

5 files changed

Lines changed: 256 additions & 2 deletions

File tree

‎CONTRIBUTORS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ We also wish to acknowledge financial and collaborative support from [CISCO](htt
542542
- jmeeder \<https://github.com/jmeeder\> - reporting that RFC 4998 evidence-record generation rejected time stamps from an authority naming the digest with NULL parameters where BC names it with them absent, both of which RFC 5754 requires a receiver to accept (issue #2379).
543543
- rimuln \<https://github.com/rimuln\> - diagnosis and fix for PKCS12 getCertificateAlias returning the alias of an unrelated certificate, tracing it to the alias and certificate enumerations of the keystore's certs table diverging in order once keys() enumerated a copy (issue #2384, PR #2385).
544544
- Yu Bao \<yubao&#064;paypal.com\> - reporting an API gap, on behalf of the PayPal Cyber Security Team, that the high-level OpenPGP message API (OpenPGPMessageProcessor / OpenPGPMessageInputStream) gave a caller no way to bound how far a compressed data packet expands, where the low-level PGPCompressedData it wraps has carried a bounded getDataStream(long) overload all along, and that OpenPGPPolicy exposed no equivalent property to set. Suggesting a protocol whitelist for CRL Distribution Point fetching, which is now the org.bouncycastle.x509.CRLDP_protocols property, and suggesting that an OCSP response was read up to the length the responder declared for itself, now capped by org.bouncycastle.ocsp.max_response_size, and suggesting bounds on the OpenPGP ASCII armor headers, now capped by org.bouncycastle.openpgp.max_armor_header_length and org.bouncycastle.openpgp.max_armor_headers.
545-
- Arpan Sharma \<https://github.com/Arpan0995\> - initial audit of BCPQC provider consistency starting with HQC, which led to the exposure of a number of issues in the JCA provider service interfaces for other BCPQC algorithms. In-depth auditing of PQC signature algorithms in the provider leading to the correction of a number of JCA API compliance issues. Initial implementation of the guard that lets a signature context be set on the composite ML-DSA services before initSign / initVerify, the composite counterpart of the base-engine fix for issue #2396 (issue #2412). Audit of the LMS / HSS stateful private key decoder, establishing that the level count, the one-time index and the persisted tree cache were all accepted without validation (issue #2414); that issue was also the inspiration for the further investigation which found the HSS and XMSS^MT decoders accepting a private key whose declared index disagreed with the traversal state stored beside it, allowing a one-time key to be used twice.
545+
- Arpan Sharma \<https://github.com/Arpan0995\> - initial audit of BCPQC provider consistency starting with HQC, which led to the exposure of a number of issues in the JCA provider service interfaces for other BCPQC algorithms. In-depth auditing of PQC signature algorithms in the provider leading to the correction of a number of JCA API compliance issues. Initial implementation of the guard that lets a signature context be set on the composite ML-DSA services before initSign / initVerify, the composite counterpart of the base-engine fix for issue #2396 (issue #2412). Audit of the LMS / HSS stateful private key decoder, establishing that the level count, the one-time index and the persisted tree cache were all accepted without validation (issue #2414); that issue was also the inspiration for the further investigation which found the HSS and XMSS^MT decoders accepting a private key whose declared index disagreed with the traversal state stored beside it, allowing a one-time key to be used twice. Audit of certificate evaluation in the high-level OpenPGP API, which found a signing subkey escaping the cross-certification requirement and a version 6 key being used without the Direct Key signature RFC 9580 requires.
546546
- Flowdalic \<https://github.com/Flowdalic\> - initial implementation of an AnimalSniffer-based Android API-level compatibility check for the Gradle build (PR #336).
547547
- hannesa2 \<https://github.com/hannesa2\> - initial Dependabot configuration for the Gradle and GitHub Actions ecosystems (PR #883).
548548
- vladhuma \<https://github.com/vladhuma\> - initial implementation of server-side OCSP stapling for the BCJSSE provider, on behalf of Thales Group (PR #1740).

‎docs/releasenotes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Date: 2026, TBD
1616
### 2.1.2 Defects Fixed
1717

1818
- The high-level OpenPGP API (org.bouncycastle.openpgp.api) let a subkey inherit the primary key's Key Flags when its own Subkey Binding signature carried no Key Flags subpacket, which made the two capability decisions taken for one subkey disagree. OpenPGPCertificate.OpenPGPComponentKey.isSigningKey() reads the effective flags, which fell back to the primary key's direct-key or primary user ID self-signature, so a subkey bound with no flags of its own counted as signing-capable; verifyEmbeddedPrimaryKeyBinding reads the binding signature's own flags, found no signing capability there, and so skipped the embedded Primary Key Binding (cross-certification) signature that RFC 9580 sec. 5.2.1.8 and sec. 10.1.3 require of a subkey that can issue signatures. A data signature made by such a subkey was therefore attributed to the certificate and reported valid by OpenPGPSignature.OpenPGPDocumentSignature.isValid() with the cross-certification requirement never applied, where GnuPG refuses the same certificate and message as not cross-certified. An attacker holding a third party's public signing subkey - which is public material - could bind it to their own primary key with a Subkey Binding signature they are able to make, carrying no Key Flags and no embedded Primary Key Binding signature, which they cannot make without the subkey's private key, and have that party's genuine signatures verify as valid under the attacker's own identity: misattribution of a real signature rather than a forgery of a new one, since the signature still has to be one the subkey actually made. Key Flags are a statement about the key the carrying signature refers to (RFC 9580 sec. 5.2.3.29), so a subkey no longer inherits them from the certificate-wide signatures of the primary key: a Subkey Binding signature that omits the subpacket now leaves the subkey with no capabilities rather than the primary's, which makes the flags the cross-certification check consults the same flags every other decision consults. Preferences and the other subpackets a direct-key signature carries are inherited as before, and the primary key itself - whose flags legitimately come from its direct-key or user ID self-signature - is unaffected. The low-level PGPSignature / PGPPublicKeyRing API performs no binding checks by design and is unchanged.
19+
- The high-level OpenPGP API (org.bouncycastle.openpgp.api) used a version 6 key that carried no valid Direct Key signature, falling back to the primary user ID binding as it correctly does for a version 4 key. RFC 9580 sec. 5.2.3.10 requires the opposite: "An implementation MUST ensure that a valid Direct Key signature is present before using a version 6 key. This prevents certain attacks where an adversary strips a self-signature specifying a Key Expiration Time or certain preferences." The certificate grammar says the same structurally, the Direct Key signature being mandatory in the version 6 structure of sec. 10.1.1 and optional in the version 4 one of sec. 10.1.3. Because a version 6 certificate carries its key expiration, features and algorithm preferences on the Direct Key signature - the convention the RFC recommends and the one OpenPGPKeyGenerator follows, its user ID certification carrying no expiration at all - removing that single signature packet from a published certificate silently dropped the expiration along with the preferences and features: OpenPGPCertificate.getSignatureChainFor fell back to the user ID binding, the primary key was still reported bound, and getEncryptionKeys() and getSigningKeys() went on returning the subkeys of a key whose owner had set it to expire. The primary key fingerprint is unchanged by the removal, so a relying party pinning the key by fingerprint still treats it as the same key, and no private key or hash collision is involved; the natural moment for the strip is the key refresh that RFC 9580 names as the reason to refetch a key at all - to learn about changes in expiration, features, preferences and revocation - which is exactly the update it defeats. This is a downgrade rather than a forgery, nothing being attributed to a key that did not authorise it, and the concerning direction is encryption, to a key meant to have been retired. OpenPGPCertificate.isBoundBy now requires a valid Direct Key self-signature on a version 6 primary key before any component of the certificate - the primary key, its subkeys or its identities - is treated as bound, so a version 6 certificate stripped of it offers no keys at all rather than an unexpiring set. Version 4 certificates are unaffected: there the key expiration legitimately lives on the user ID self-signature and the fallback is correct, so it stays. The revocation-only version 6 certificate of sec. 10.1.2, which legitimately carries no Direct Key signature, is unaffected as well - its key was already refused as revoked, and reading the revocation does not go through the binding check.
1920
- The lightweight LMSSigner and HSSSigner refused a key wrapped in ParametersWithRandom, which is how BcContentSignerBuilder passes a key whenever setSecureRandom() has been called - so BcHssLmsContentSignerBuilder built a working signer until a random was set and then failed with "Incorrect Key Parameters", and the two signers themselves raised ClassCastException on the same input. All three now unwrap it, as the promoted ML-DSA and SLH-DSA signers already did. The random is accepted and not used: LMS derives its message randomiser C from the key's seed and the one-time index, so it is deterministic and cannot repeat while q does not. Note SP 800-208 sec. 6.1 asks for C to come from an approved random bit generator, which this implementation does not do; that is unchanged here, and a supplied random is now ignored rather than refused.
2021
- LMS signature verification did not apply two of the checks RFC 8554 sec. 5.4.2 requires before a signature is processed. Step 2g refuses a signature whose LMS typecode is not the one from the public key, and without it the path computation took its height and tree digest from the parameter set the signature named rather than the key's, so a signature claiming a height-25 parameter set drove a 25-level computation against a height-5 key. Step 2i refuses a leaf number q outside the tree, and without it an out-of-range q flowed into the node arithmetic and was left for the candidate-root comparison to catch. Neither was a forgery - the domain separation between D_LEAF and D_INTR and the final comparison saw to that - but both are attacker-chosen work the specification says to refuse up front. Both are now checked, and a signature failing either is still reported as not verifying rather than thrown out of Signature.verify(). The catch around the signature decode in LMSSigner and HSSSigner has also been narrowed to the decode itself, as the corresponding SPI was corrected to do for github #2408: past the parse the engine reports an inconsistent signature by returning false rather than by throwing, so the wider catch caught nothing while standing ready to turn a future internal error into a quiet false.
2122
- The LMS and HSS key parameter classes now apply at construction the checks their decoders apply, so a key built directly cannot be one the decoder would refuse. LMSPrivateKeyParameters accepted an identifier of any length although the decoder reads exactly 16 bytes - such a key encoded but could not be read back - and left q, maxQ and the seed length unchecked; the seed is now required to be at least m bytes at decode as well, where a one-byte seed had been decoding silently and then deriving every one-time key from it. HSSPrivateKeyParameters checked neither its level count nor that it had been given a component key per level and a chaining signature per level below the root, and then indexed both lists, so a mismatch surfaced as IndexOutOfBoundsException - or, where a level happened to match, as a null chaining signature that only failed at signing time; the level is now checked after the reset that fills it in, since a null is legitimate on the way in. LMSPrivateKeyParameters.getInstance(byte[], byte[]) adopted the public key supplied beside the private one without comparing them, so a mismatched public key was simply reported by getPublicKey(); it now cross-checks the identifier, both parameter sets and, where the tree cache already holds it, the root, as the HSS entry point does. The decoders also now report a bad version or seed length as IOException rather than IllegalStateException, so a caller can catch one type for a malformed key, and the package-private LM-OTS public key decoder no longer declares throws Exception or dereferences an unrecognised typecode. The deprecated org.bouncycastle.pqc.crypto.lms copies carry the decoder corrections.

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.bouncycastle.bcpg.FingerprintUtil;
2222
import org.bouncycastle.bcpg.KeyIdentifier;
2323
import org.bouncycastle.bcpg.PacketFormat;
24+
import org.bouncycastle.bcpg.PublicKeyPacket;
2425
import org.bouncycastle.bcpg.PublicKeyUtils;
2526
import org.bouncycastle.bcpg.SignatureSubpacket;
2627
import org.bouncycastle.bcpg.SignatureSubpacketTags;
@@ -718,7 +719,9 @@ private OpenPGPSignatureChain getSignatureChainFor(OpenPGPCertificateComponent c
718719
boolean isPrimaryKey = component == getPrimaryKey();
719720
if (isPrimaryKey && chainsForComponent.getCertificationAt(evaluationDate) == null)
720721
{
721-
// If cert has no direct-key signatures, consider primary UID bindings instead
722+
// If cert has no direct-key signatures, consider primary UID bindings instead.
723+
// A version 6 key never reaches this fallback: isBoundBy() refuses the certificate
724+
// outright unless a valid Direct Key signature is present (RFC 9580, section 5.2.3.10).
722725
OpenPGPUserId primaryUserId = getPrimaryUserId(evaluationDate);
723726
if (primaryUserId != null)
724727
{
@@ -849,6 +852,21 @@ private boolean isBoundBy(OpenPGPCertificateComponent component,
849852
OpenPGPComponentKey root,
850853
Date evaluationTime)
851854
{
855+
// RFC 9580, section 5.2.3.10: "An implementation MUST ensure that a valid Direct Key signature is
856+
// present before using a version 6 key. This prevents certain attacks where an adversary strips a
857+
// self-signature specifying a Key Expiration Time or certain preferences."
858+
// The certificate grammar says the same structurally: the Direct Key signature is mandatory in the
859+
// version 6 structure of section 10.1.1 and optional in the version 4 one of section 10.1.3.
860+
// A version 6 certificate carries the key expiration, the features and the algorithm preferences on
861+
// the Direct Key signature rather than on a User ID self-signature, so treating a User ID binding as
862+
// a substitute - which is correct for a version 4 key, where that information legitimately lives on
863+
// the User ID self-signature - would let that single packet be stripped from a published certificate
864+
// without the loss of what it carried being noticed.
865+
if (requiresDirectKeySignature() && !hasValidDirectKeySignature(evaluationTime))
866+
{
867+
return false;
868+
}
869+
852870
OpenPGPSignature.OpenPGPSignatureSubpacket keyExpiration =
853871
component.getApplyingSubpacket(evaluationTime, SignatureSubpacketTags.KEY_EXPIRE_TIME);
854872
if (keyExpiration != null)
@@ -892,6 +910,46 @@ private boolean isBoundBy(OpenPGPCertificateComponent component,
892910
}
893911
}
894912

913+
/**
914+
* Return true, if RFC 9580 requires the primary key of this certificate to carry a valid Direct Key
915+
* self-signature before any of its components may be used (section 5.2.3.10). This is the case for
916+
* version 6 keys.
917+
*
918+
* @return true if a Direct Key self-signature is mandatory for this certificate
919+
*/
920+
private boolean requiresDirectKeySignature()
921+
{
922+
return getPrimaryKey().getVersion() == PublicKeyPacket.VERSION_6;
923+
}
924+
925+
/**
926+
* Return true, if the primary key carries a Direct Key self-signature which is - at evaluation time -
927+
* both effective and cryptographically valid.
928+
* A Direct Key revocation does not satisfy this, since it is not a certification.
929+
*
930+
* @param evaluationTime evaluation time
931+
* @return true if a valid Direct Key self-signature is present at evaluation time
932+
*/
933+
private boolean hasValidDirectKeySignature(Date evaluationTime)
934+
{
935+
OpenPGPSignatureChain directKeyChain = getSelfSignatureChainsFor(getPrimaryKey())
936+
.getCertificationAt(evaluationTime);
937+
if (directKeyChain == null)
938+
{
939+
return false;
940+
}
941+
942+
try
943+
{
944+
return directKeyChain.isValid(implementation.pgpContentVerifierBuilderProvider(), policy);
945+
}
946+
catch (PGPException e)
947+
{
948+
// Signature verification failed (signature broken?)
949+
return false;
950+
}
951+
}
952+
895953
/**
896954
* Return a {@link List} containing all currently marked, valid encryption keys.
897955
*

0 commit comments

Comments
 (0)