|
| 1 | +//! Trust anchor-related structures as defined in RFC 5914 |
| 2 | +
|
| 3 | +use crate::ext::pkix::{CertificatePolicies, NameConstraints}; |
| 4 | +use crate::TbsCertificate; |
| 5 | +use crate::{ext::Extensions, Certificate}; |
| 6 | + |
| 7 | +use der::asn1::{OctetString, Utf8String}; |
| 8 | +use der::{Choice, Sequence}; |
| 9 | +use flagset::{flags, FlagSet}; |
| 10 | +use spki::SubjectPublicKeyInfo; |
| 11 | +use x501::name::Name; |
| 12 | + |
| 13 | +/// ```text |
| 14 | +/// TrustAnchorInfo ::= SEQUENCE { |
| 15 | +/// version TrustAnchorInfoVersion DEFAULT v1, |
| 16 | +/// pubKey SubjectPublicKeyInfo, |
| 17 | +/// keyId KeyIdentifier, |
| 18 | +/// taTitle TrustAnchorTitle OPTIONAL, |
| 19 | +/// certPath CertPathControls OPTIONAL, |
| 20 | +/// exts [1] EXPLICIT Extensions OPTIONAL, |
| 21 | +/// taTitleLangTag [2] UTF8String OPTIONAL |
| 22 | +/// } |
| 23 | +/// |
| 24 | +/// TrustAnchorInfoVersion ::= INTEGER { v1(1) } |
| 25 | +/// |
| 26 | +/// TrustAnchorTitle ::= UTF8String (SIZE (1..64)) |
| 27 | +/// ``` |
| 28 | +#[derive(Clone, Debug, PartialEq, Eq, Sequence)] |
| 29 | +#[allow(missing_docs)] |
| 30 | +pub struct TrustAnchorInfo<'a> { |
| 31 | + #[asn1(default = "Default::default")] |
| 32 | + pub version: u8, |
| 33 | + |
| 34 | + pub pub_key: SubjectPublicKeyInfo<'a>, |
| 35 | + |
| 36 | + pub key_id: OctetString<'a>, |
| 37 | + |
| 38 | + #[asn1(optional = "true")] |
| 39 | + pub ta_title: Option<Utf8String<'a>>, |
| 40 | + |
| 41 | + #[asn1(optional = "true")] |
| 42 | + pub cert_path: Option<CertPathControls<'a>>, |
| 43 | + |
| 44 | + #[asn1(context_specific = "1", tag_mode = "EXPLICIT", optional = "true")] |
| 45 | + pub extensions: Option<Extensions<'a>>, |
| 46 | + |
| 47 | + #[asn1(context_specific = "2", tag_mode = "IMPLICIT", optional = "true")] |
| 48 | + pub ta_title_lang_tag: Option<Utf8String<'a>>, |
| 49 | +} |
| 50 | + |
| 51 | +/// ```text |
| 52 | +/// CertPathControls ::= SEQUENCE { |
| 53 | +/// taName Name, |
| 54 | +/// certificate [0] Certificate OPTIONAL, |
| 55 | +/// policySet [1] CertificatePolicies OPTIONAL, |
| 56 | +/// policyFlags [2] CertPolicyFlags OPTIONAL, |
| 57 | +/// nameConstr [3] NameConstraints OPTIONAL, |
| 58 | +/// pathLenConstraint [4] INTEGER (0..MAX) OPTIONAL |
| 59 | +/// } |
| 60 | +/// ``` |
| 61 | +#[derive(Clone, Debug, Eq, PartialEq, Sequence)] |
| 62 | +#[allow(missing_docs)] |
| 63 | +pub struct CertPathControls<'a> { |
| 64 | + pub ta_name: Name<'a>, |
| 65 | + |
| 66 | + #[asn1(context_specific = "0", tag_mode = "IMPLICIT", optional = "true")] |
| 67 | + pub certificate: Option<Certificate<'a>>, |
| 68 | + |
| 69 | + #[asn1(context_specific = "1", tag_mode = "IMPLICIT", optional = "true")] |
| 70 | + pub policy_set: Option<CertificatePolicies<'a>>, |
| 71 | + |
| 72 | + #[asn1(context_specific = "2", tag_mode = "IMPLICIT", optional = "true")] |
| 73 | + pub policy_flags: Option<CertPolicyFlags<'a>>, |
| 74 | + |
| 75 | + #[asn1(context_specific = "3", tag_mode = "IMPLICIT", optional = "true")] |
| 76 | + pub name_constr: Option<NameConstraints<'a>>, |
| 77 | + |
| 78 | + #[asn1(context_specific = "4", tag_mode = "IMPLICIT", optional = "true")] |
| 79 | + pub path_len_constraint: Option<u32>, |
| 80 | +} |
| 81 | + |
| 82 | +flags! { |
| 83 | + /// Certificate policies as defined in [RFC 5280 Section 4.2.1.13]. |
| 84 | + /// |
| 85 | + /// ```text |
| 86 | + /// CertPolicyFlags ::= BIT STRING { |
| 87 | + /// inhibitPolicyMapping (0), |
| 88 | + /// requireExplicitPolicy (1), |
| 89 | + /// inhibitAnyPolicy (2) |
| 90 | + /// } |
| 91 | + /// ``` |
| 92 | + /// |
| 93 | + /// [RFC 5280 Section 4.2.1.13]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.13 |
| 94 | + #[allow(missing_docs)] |
| 95 | + pub enum CertPolicies: u8 { |
| 96 | + InhibitPolicyMapping = 1 << 0, |
| 97 | + RequireExplicitPolicy = 1 << 1, |
| 98 | + InhibitAnyPolicy = 1 << 2, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Certificate policy flags as defined in [RFC 5280 Section 4.2.1.13]. |
| 103 | +/// |
| 104 | +/// [RFC 5280 Section 4.2.1.13]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.13 |
| 105 | +pub type CertPolicyFlags<'a> = FlagSet<CertPolicies>; |
| 106 | + |
| 107 | +/// ```text |
| 108 | +/// TrustAnchorChoice ::= CHOICE { |
| 109 | +/// certificate Certificate, |
| 110 | +/// tbsCert [1] EXPLICIT TBSCertificate, |
| 111 | +/// taInfo [2] EXPLICIT TrustAnchorInfo |
| 112 | +/// } |
| 113 | +/// ``` |
| 114 | +#[derive(Clone, Debug, PartialEq, Eq, Choice)] |
| 115 | +#[allow(clippy::large_enum_variant)] |
| 116 | +#[allow(missing_docs)] |
| 117 | +pub enum TrustAnchorChoice<'a> { |
| 118 | + Certificate(Certificate<'a>), |
| 119 | + |
| 120 | + #[asn1(context_specific = "1", tag_mode = "EXPLICIT", constructed = "true")] |
| 121 | + TbsCertificate(TbsCertificate<'a>), |
| 122 | + |
| 123 | + #[asn1(context_specific = "2", tag_mode = "EXPLICIT", constructed = "true")] |
| 124 | + TaInfo(TrustAnchorInfo<'a>), |
| 125 | +} |
0 commit comments