|
21 | 21 | import org.bouncycastle.bcpg.FingerprintUtil; |
22 | 22 | import org.bouncycastle.bcpg.KeyIdentifier; |
23 | 23 | import org.bouncycastle.bcpg.PacketFormat; |
| 24 | +import org.bouncycastle.bcpg.PublicKeyPacket; |
24 | 25 | import org.bouncycastle.bcpg.PublicKeyUtils; |
25 | 26 | import org.bouncycastle.bcpg.SignatureSubpacket; |
26 | 27 | import org.bouncycastle.bcpg.SignatureSubpacketTags; |
@@ -718,7 +719,9 @@ private OpenPGPSignatureChain getSignatureChainFor(OpenPGPCertificateComponent c |
718 | 719 | boolean isPrimaryKey = component == getPrimaryKey(); |
719 | 720 | if (isPrimaryKey && chainsForComponent.getCertificationAt(evaluationDate) == null) |
720 | 721 | { |
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). |
722 | 725 | OpenPGPUserId primaryUserId = getPrimaryUserId(evaluationDate); |
723 | 726 | if (primaryUserId != null) |
724 | 727 | { |
@@ -849,6 +852,21 @@ private boolean isBoundBy(OpenPGPCertificateComponent component, |
849 | 852 | OpenPGPComponentKey root, |
850 | 853 | Date evaluationTime) |
851 | 854 | { |
| 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 | + |
852 | 870 | OpenPGPSignature.OpenPGPSignatureSubpacket keyExpiration = |
853 | 871 | component.getApplyingSubpacket(evaluationTime, SignatureSubpacketTags.KEY_EXPIRE_TIME); |
854 | 872 | if (keyExpiration != null) |
@@ -892,6 +910,46 @@ private boolean isBoundBy(OpenPGPCertificateComponent component, |
892 | 910 | } |
893 | 911 | } |
894 | 912 |
|
| 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 | + |
895 | 953 | /** |
896 | 954 | * Return a {@link List} containing all currently marked, valid encryption keys. |
897 | 955 | * |
|
0 commit comments