|
| 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 | +} |
0 commit comments