Skip to content

Commit f0d5c85

Browse files
committed
Require the component key that issued a third-party certification or trust delegation to hold the certification key flag when the signature was made, so that a SIGN_DATA-only subkey is no longer attributed the issuing certificate's authority to bind identities and delegate introducer trust.
1 parent 8a3265b commit f0d5c85

4 files changed

Lines changed: 264 additions & 1 deletion

File tree

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ <h3>2.1.2 Defects Fixed</h3>
138138
<li>org.bouncycastle.operator.DefaultKemEncapsulationLengthProvider.getEncapsulationLength() looked its argument up in a table of the KEMs whose encapsulation lengths are registered - ML-KEM, NTRU, HQC, FrodoKEM and composite ML-KEM - and dereferenced the result without checking it. A CMS RFC 9629 KEMRecipientInfo recipient using any other KEM which does register a key wrapping cipher, BIKE, NTRU+ or SMAUG-T, got as far as the wrap and then failed with a NullPointerException carrying no indication of which algorithm was at fault. The lookup now throws IllegalArgumentException naming the KEM's OID, as the sibling DefaultKemAlgorithmIdentifierFinder does, and the contract is documented on the KemEncapsulationLengthProvider interface (github #2398).</li>
139139
<li>AbstractTlsServer.notifyHandshakeBeginning() cleared the "status_request" extension recorded from the previous ClientHello but not "status_request_v2" or "trusted_ca_keys", which processClientExtensions assigns only when the ClientHello carries any extensions at all. A TlsServer instance driven through more than one handshake - a renegotiation, say - therefore carried both fields over, and a following ClientHello with no extensions had the server decide whether to echo them from the earlier handshake's values, sending the client an extension it had not offered; RFC 5246 sec. 7.4.1.4 has a client abort with unsupported_extension over exactly that. Both are now cleared alongside "status_request". Reaching this needed a TlsServer overriding allowMultiCertStatus() or allowTrustedCAIndication(), which default to false and, for the JSSE provider, were not previously settable at all.</li>
140140
<li>org.bouncycastle.pqc.crypto.aimer.AIMerSigner.verifySignature() sliced the signature out of the signed-message envelope generateSignature() produces - the message followed by the signature - at offset message.length without first checking the envelope was that long. A truncated or otherwise short signature therefore threw ArrayIndexOutOfBoundsException instead of returning false, which reached the caller unchecked out of Signature.verify() on the BCPQC "AIMer" services, and because only the signature-sized run of bytes at that offset was read, a valid signature with data appended to it still verified, so the accepted encoding was not unique. The envelope is now required to be exactly message.length plus the parameter set's signature size before it is read, and anything else is reported as a failed verification, matching the guard the Falcon, Faest, Mayo, Snova and QRUOV signers already apply. A correctly formed signature is unaffected (github #2401).</li>
141+
<li>The high-level OpenPGP certificate API accepted a third-party certification or trust delegation from any component key of the issuing certificate, without requiring that component to have been granted the authority to certify. OpenPGPCertificate.getCertificationBy() and getDelegationBy() resolve a third-party signature by matching its issuer key identifier against every key of the third-party certificate, then verify the issuer's own binding chain and the signature itself; nothing checked that the issuing component carried the RFC 9580 Section 5.2.3.29 certification flag (KeyFlags.CERTIFY_OTHER, "This key may be used to certify other keys") at the signature's creation time. A subkey bound only with SIGN_DATA - the online signing subkey of exactly the offline-primary arrangement those flags exist to express - could therefore issue a positive user-id certification or a full-trust, depth-one direct-key delegation that the API returned as a valid signature chain, so an application treating that chain as an identity or introducer-trust decision would attribute it to the certificate as a whole. This does not forge the primary key's signature; it lets a compromised restricted subkey act with the primary key's identity-issuing authority, and the same held for a legacy RSA subkey bound only for encryption, whose algorithm is nonetheless able to sign. A third-party certification or delegation is now only attributed to the issuing certificate when the component key that made it is the primary key, or is a subkey holding CERTIFY_OTHER when the signature was created; certifications by a certification-capable subkey continue to be accepted, so the check is on the granted capability rather than on being the primary key. Primary keys are accepted whatever their key flags say - a primary key is certification-capable by construction, and certificates carrying no key flags subpacket at all are common. Third-party revocations are deliberately left outside the rule, since declining to honour one would keep trust alive rather than withdraw it. Affects 1.81 through 1.85.2.</li>
141142
</ul>
142143

143144
<h3>2.1.3 Additional Features and Functionality</h3>

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,8 @@ protected OpenPGPSignatureChains getMergedDanglingExternalSignatureChainEndsFrom
18001800
if (KeyIdentifier.matches(
18011801
rootLink.getSignature().getKeyIdentifiers(),
18021802
issuerKey.getKeyIdentifier(),
1803-
true))
1803+
true)
1804+
&& mayHaveIssued(issuerKey, rootLink.getSignature()))
18041805
{
18051806
OpenPGPSignatureChain externalChain = issuerKey.getSelfSignatureChains()
18061807
.getChainAt(rootLink.getSignature().getCreationTime());
@@ -1812,6 +1813,44 @@ protected OpenPGPSignatureChains getMergedDanglingExternalSignatureChainEndsFrom
18121813
}
18131814
return chainsBy;
18141815
}
1816+
1817+
/**
1818+
* Return true if the given component key of a third-party certificate is permitted to have
1819+
* issued the given signature over a component of this certificate.
1820+
* <p>
1821+
* A key identifier only says which component key made a signature, not whether that
1822+
* component was granted the authority to make it. RFC 9580 Section 5.2.3.29 gives the
1823+
* certification flag ({@link KeyFlags#CERTIFY_OTHER}, "This key may be used to certify
1824+
* other keys") separately from the data-signing flag ({@link KeyFlags#SIGN_DATA}), which
1825+
* is what lets a certificate keep an offline certification primary key alongside online
1826+
* subkeys. Honouring a third-party certification or trust delegation from a subkey that
1827+
* was never granted the certification flag would hand that subkey the primary key's
1828+
* authority to bind identities and delegate introducer trust, so such a signature is not
1829+
* treated as issued by the certificate at all.
1830+
* <p>
1831+
* A primary key is accepted whatever its key flags say: it is certification-capable by
1832+
* construction - it binds the certificate's own user-ids and subkeys - and certificates
1833+
* that carry no key flags subpacket at all are common, where
1834+
* {@link OpenPGPComponentKey#isCertificationKey(Date)} answers false for want of a
1835+
* subpacket to read rather than because authority was withheld.
1836+
* <p>
1837+
* Revocations are deliberately outside this rule: declining to honour a third-party
1838+
* revocation keeps trust alive rather than withdrawing it, which is the more dangerous way
1839+
* to be wrong.
1840+
*
1841+
* @param issuerKey component key of the third-party certificate matching the signature's issuer
1842+
* @param signature third-party signature over a component of this certificate
1843+
* @return true if the signature may be attributed to the third-party certificate
1844+
*/
1845+
private boolean mayHaveIssued(OpenPGPComponentKey issuerKey, OpenPGPComponentSignature signature)
1846+
{
1847+
if (issuerKey.isPrimaryKey() || signature.isRevocation())
1848+
{
1849+
return true;
1850+
}
1851+
1852+
return issuerKey.isCertificationKey(signature.getCreationTime());
1853+
}
18151854
}
18161855

18171856
/**
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package org.bouncycastle.openpgp.api.test;
2+
3+
import java.util.Date;
4+
import java.util.Iterator;
5+
6+
import org.bouncycastle.bcpg.HashAlgorithmTags;
7+
import org.bouncycastle.bcpg.PublicKeyPacket;
8+
import org.bouncycastle.bcpg.SignatureSubpacketTags;
9+
import org.bouncycastle.bcpg.sig.KeyFlags;
10+
import org.bouncycastle.openpgp.PGPKeyPair;
11+
import org.bouncycastle.openpgp.PGPException;
12+
import org.bouncycastle.openpgp.PGPPublicKey;
13+
import org.bouncycastle.openpgp.PGPPublicKeyRing;
14+
import org.bouncycastle.openpgp.PGPSignature;
15+
import org.bouncycastle.openpgp.PGPSignatureGenerator;
16+
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
17+
import org.bouncycastle.openpgp.api.KeyPairGeneratorCallback;
18+
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
19+
import org.bouncycastle.openpgp.api.OpenPGPKey;
20+
import org.bouncycastle.openpgp.api.SignatureParameters;
21+
import org.bouncycastle.openpgp.api.SignatureSubpacketsFunction;
22+
import org.bouncycastle.openpgp.api.bc.BcOpenPGPApi;
23+
import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder;
24+
import org.bouncycastle.util.test.SimpleTest;
25+
26+
/**
27+
* Regression test: a third-party certification or trust delegation must only be honoured by
28+
* {@link OpenPGPCertificate} when the component key that actually issued it was granted the
29+
* authority to certify.
30+
* <p>
31+
* RFC 9580 Section 5.2.3.29 separates the certification flag ({@link KeyFlags#CERTIFY_OTHER})
32+
* from the data-signing flag ({@link KeyFlags#SIGN_DATA}), which is what lets a certificate keep
33+
* its certification primary key offline while a signing subkey is online. Accepting a
34+
* certification from a {@code SIGN_DATA}-only subkey would promote the online subkey to the
35+
* offline primary key's authority to bind identities and delegate introducer trust.
36+
* <p>
37+
* The certification-capable primary key and a subkey explicitly carrying {@code CERTIFY_OTHER}
38+
* must keep working - the check is on the granted capability, not on being the primary key.
39+
* See {@code OpenPGPCertificateComponent.mayHaveIssued}.
40+
*/
41+
public class OpenPGPThirdPartyCertificationAuthorityTest
42+
extends SimpleTest
43+
{
44+
private static final String AUTHORITY_UID = "Offline Authority <authority@example.com>";
45+
private static final String SUBJECT_UID = "Subject <subject@example.com>";
46+
47+
public String getName()
48+
{
49+
return "OpenPGPThirdPartyCertificationAuthorityTest";
50+
}
51+
52+
public void performTest()
53+
throws Exception
54+
{
55+
Date now = new Date((System.currentTimeMillis() / 1000L) * 1000L);
56+
Date keyTime = new Date(now.getTime() - 3600L * 1000L);
57+
Date certificationTime = new Date(now.getTime() - 1800L * 1000L);
58+
59+
BcOpenPGPApi api = new BcOpenPGPApi();
60+
61+
// the classic profile is exactly the split this check protects: a CERTIFY_OTHER primary
62+
// key, a SIGN_DATA-only signing subkey and an encryption subkey
63+
OpenPGPKey authority = api.generateKey(PublicKeyPacket.VERSION_6, keyTime)
64+
.classicKey(AUTHORITY_UID).build();
65+
OpenPGPKey subject = api.generateKey(PublicKeyPacket.VERSION_6, keyTime)
66+
.classicKey(SUBJECT_UID).build();
67+
68+
OpenPGPCertificate authorityCert = authority.toCertificate();
69+
PGPPublicKey subjectPrimary = subject.getPGPPublicKeyRing().getPublicKey();
70+
71+
PGPKeyPair signOnly = signingSubkeyOf(authority, now);
72+
isTrue("signing subkey must carry SIGN_DATA",
73+
authority.getKey(signOnly.getKeyIdentifier()).isSigningKey(certificationTime));
74+
isTrue("signing subkey must not carry CERTIFY_OTHER",
75+
!authority.getKey(signOnly.getKeyIdentifier()).isCertificationKey(certificationTime));
76+
77+
// a signing subkey's certification of a third party's user-id is not the authority's
78+
isTrue("SIGN_DATA-only subkey must not certify a third-party user-id",
79+
!certificationAccepted(subject, subjectPrimary, signOnly, authorityCert, certificationTime, now));
80+
81+
// nor is its direct-key signature a delegation of the authority's introducer trust
82+
isTrue("SIGN_DATA-only subkey must not delegate trust",
83+
!delegationAccepted(subject, subjectPrimary, signOnly, authorityCert, certificationTime, now));
84+
85+
// the compatibility assertion: the authority's own certification primary key still works
86+
PGPKeyPair primaryPair = authority.getPrimarySecretKey().unlock().getKeyPair();
87+
isTrue("certification primary key must still certify",
88+
certificationAccepted(subject, subjectPrimary, primaryPair, authorityCert, certificationTime, now));
89+
isTrue("certification primary key must still delegate trust",
90+
delegationAccepted(subject, subjectPrimary, primaryPair, authorityCert, certificationTime, now));
91+
92+
// and the rule is the granted capability, not primary-key-only: a subkey explicitly bound
93+
// with CERTIFY_OTHER is an authority for both
94+
OpenPGPKey delegatingAuthority = keyWithCertifyingSubkey(api, keyTime);
95+
OpenPGPCertificate delegatingCert = delegatingAuthority.toCertificate();
96+
PGPKeyPair certifyingSubkey = certifyingSubkeyOf(delegatingAuthority, certificationTime);
97+
isTrue("certifying subkey must not be the primary key",
98+
certifyingSubkey.getKeyIdentifier() != null
99+
&& !delegatingAuthority.getKey(certifyingSubkey.getKeyIdentifier()).isPrimaryKey());
100+
isTrue("CERTIFY_OTHER subkey must certify a third-party user-id",
101+
certificationAccepted(subject, subjectPrimary, certifyingSubkey, delegatingCert, certificationTime, now));
102+
isTrue("CERTIFY_OTHER subkey must delegate trust",
103+
delegationAccepted(subject, subjectPrimary, certifyingSubkey, delegatingCert, certificationTime, now));
104+
}
105+
106+
/**
107+
* Certify the subject's user-id with the given issuer key pair and report whether the
108+
* certificate accepts the result as a certification by the given third-party certificate.
109+
*/
110+
private boolean certificationAccepted(OpenPGPKey subject, PGPPublicKey subjectPrimary, PGPKeyPair issuer,
111+
OpenPGPCertificate thirdParty, Date creationTime, Date evaluationTime)
112+
throws Exception
113+
{
114+
PGPSignatureGenerator sigGen = new PGPSignatureGenerator(
115+
new BcPGPContentSignerBuilder(issuer.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA256),
116+
issuer.getPublicKey());
117+
sigGen.init(PGPSignature.POSITIVE_CERTIFICATION, issuer.getPrivateKey());
118+
119+
PGPSignatureSubpacketGenerator hashed = new PGPSignatureSubpacketGenerator();
120+
hashed.setSignatureCreationTime(true, creationTime);
121+
hashed.setIssuerFingerprint(true, issuer.getPublicKey());
122+
sigGen.setHashedSubpackets(hashed.generate());
123+
124+
PGPSignature certification = sigGen.generateCertification(SUBJECT_UID, subjectPrimary);
125+
PGPPublicKeyRing ring = PGPPublicKeyRing.insertPublicKey(subject.getPGPPublicKeyRing(),
126+
PGPPublicKey.addCertification(subjectPrimary, SUBJECT_UID, certification));
127+
128+
OpenPGPCertificate.OpenPGPSignatureChain chain = new OpenPGPCertificate(ring)
129+
.getUserId(SUBJECT_UID).getCertificationBy(thirdParty, evaluationTime);
130+
return chain != null && chain.isValid();
131+
}
132+
133+
/**
134+
* Issue a full-trust, depth-one direct-key signature over the subject's primary key with the
135+
* given issuer key pair and report whether the certificate accepts it as a delegation.
136+
*/
137+
private boolean delegationAccepted(OpenPGPKey subject, PGPPublicKey subjectPrimary, PGPKeyPair issuer,
138+
OpenPGPCertificate thirdParty, Date creationTime, Date evaluationTime)
139+
throws Exception
140+
{
141+
PGPSignatureGenerator sigGen = new PGPSignatureGenerator(
142+
new BcPGPContentSignerBuilder(issuer.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA256),
143+
issuer.getPublicKey());
144+
sigGen.init(PGPSignature.DIRECT_KEY, issuer.getPrivateKey());
145+
146+
PGPSignatureSubpacketGenerator hashed = new PGPSignatureSubpacketGenerator();
147+
hashed.setSignatureCreationTime(true, creationTime);
148+
hashed.setIssuerFingerprint(true, issuer.getPublicKey());
149+
hashed.setTrust(true, 1, 120);
150+
sigGen.setHashedSubpackets(hashed.generate());
151+
152+
PGPSignature delegation = sigGen.generateCertification(subjectPrimary);
153+
PGPPublicKeyRing ring = PGPPublicKeyRing.insertPublicKey(subject.getPGPPublicKeyRing(),
154+
PGPPublicKey.addCertification(subjectPrimary, delegation));
155+
156+
OpenPGPCertificate.OpenPGPSignatureChain chain = new OpenPGPCertificate(ring)
157+
.getDelegationBy(thirdParty, evaluationTime);
158+
return chain != null && chain.isValid();
159+
}
160+
161+
/**
162+
* Build a key whose signing subkey is bound with CERTIFY_OTHER in place of SIGN_DATA, so that
163+
* the binding still carries the required primary-key back-signature.
164+
*/
165+
private OpenPGPKey keyWithCertifyingSubkey(BcOpenPGPApi api, Date keyTime)
166+
throws PGPException
167+
{
168+
return api.generateKey(PublicKeyPacket.VERSION_6, keyTime)
169+
.withPrimaryKey()
170+
.addUserId(AUTHORITY_UID)
171+
.addSigningSubkey(
172+
KeyPairGeneratorCallback.Util.signingKey(),
173+
SignatureParameters.Callback.Util.modifyHashedSubpackets(new SignatureSubpacketsFunction()
174+
{
175+
public PGPSignatureSubpacketGenerator apply(PGPSignatureSubpacketGenerator subpackets)
176+
{
177+
subpackets.removePacketsOfType(SignatureSubpacketTags.KEY_FLAGS);
178+
subpackets.setKeyFlags(KeyFlags.CERTIFY_OTHER);
179+
return subpackets;
180+
}
181+
}),
182+
null)
183+
.build();
184+
}
185+
186+
private PGPKeyPair signingSubkeyOf(OpenPGPKey key, Date evaluationTime)
187+
throws Exception
188+
{
189+
return subkeyOf(key, evaluationTime, false);
190+
}
191+
192+
private PGPKeyPair certifyingSubkeyOf(OpenPGPKey key, Date evaluationTime)
193+
throws Exception
194+
{
195+
return subkeyOf(key, evaluationTime, true);
196+
}
197+
198+
private PGPKeyPair subkeyOf(OpenPGPKey key, Date evaluationTime, boolean certifying)
199+
throws Exception
200+
{
201+
for (Iterator<OpenPGPKey.OpenPGPSecretKey> it = key.getSecretKeys().values().iterator(); it.hasNext(); )
202+
{
203+
OpenPGPKey.OpenPGPSecretKey candidate = it.next();
204+
if (candidate.isPrimaryKey())
205+
{
206+
continue;
207+
}
208+
if (certifying ? candidate.isCertificationKey(evaluationTime)
209+
: (candidate.isSigningKey(evaluationTime) && !candidate.isCertificationKey(evaluationTime)))
210+
{
211+
return candidate.unlock().getKeyPair();
212+
}
213+
}
214+
fail("key does not carry the expected " + (certifying ? "certifying" : "signing-only") + " subkey");
215+
return null;
216+
}
217+
218+
public static void main(String[] args)
219+
{
220+
runTest(new OpenPGPThirdPartyCertificationAuthorityTest());
221+
}
222+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class RegressionTest
1818
new OpenPGPMessageGeneratorTest(),
1919
new OpenPGPMessageProcessorTest(),
2020
new OpenPGPSubkeyExpiryForgeryTest(),
21+
new OpenPGPThirdPartyCertificationAuthorityTest(),
2122
new OpenPGPV4KeyGenerationTest(),
2223
new OpenPGPV6KeyGeneratorTest(),
2324
new StaticV6OpenPGPMessageGeneratorTest(),

0 commit comments

Comments
 (0)