rfc3281.rs (6133B)
1 // This Source Code Form is subject to the terms of the Mozilla Public 2 // License, v. 2.0. If a copy of the MPL was not distributed with this 3 // file, You can obtain one at https://mozilla.org/MPL/2.0/. 4 5 use bcder::{ 6 decode::{Constructed, DecodeError, Source}, 7 BitString, Oid, 8 }; 9 use x509_certificate::{asn1time::*, rfc3280::*, rfc5280::*}; 10 11 /// Attribute certificate. 12 /// 13 /// ```ASN.1 14 /// AttributeCertificate ::= SEQUENCE { 15 /// acinfo AttributeCertificateInfo, 16 /// signatureAlgorithm AlgorithmIdentifier, 17 /// signatureValue BIT STRING 18 /// } 19 /// ``` 20 #[derive(Clone, Debug, Eq, PartialEq)] 21 pub struct AttributeCertificate { 22 pub ac_info: AttributeCertificateInfo, 23 pub signature_algorithm: AlgorithmIdentifier, 24 pub signature_value: BitString, 25 } 26 27 impl AttributeCertificate { 28 pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> { 29 cons.take_sequence(|cons| { 30 let ac_info = AttributeCertificateInfo::take_from(cons)?; 31 let signature_algorithm = AlgorithmIdentifier::take_from(cons)?; 32 let signature_value = BitString::take_from(cons)?; 33 34 Ok(Self { 35 ac_info, 36 signature_algorithm, 37 signature_value, 38 }) 39 }) 40 } 41 } 42 43 /// Attribute certificate info. 44 /// 45 /// ```ASN.1 46 /// AttributeCertificateInfo ::= SEQUENCE { 47 /// version AttCertVersion -- version is v2, 48 /// holder Holder, 49 /// issuer AttCertIssuer, 50 /// signature AlgorithmIdentifier, 51 /// serialNumber CertificateSerialNumber, 52 /// attrCertValidityPeriod AttCertValidityPeriod, 53 /// attributes SEQUENCE OF Attribute, 54 /// issuerUniqueID UniqueIdentifier OPTIONAL, 55 /// extensions Extensions OPTIONAL 56 /// } 57 /// ``` 58 #[derive(Clone, Debug, Eq, PartialEq)] 59 pub struct AttributeCertificateInfo { 60 pub version: AttCertVersion, 61 pub holder: Holder, 62 pub issuer: AttCertIssuer, 63 pub signature: AlgorithmIdentifier, 64 pub serial_number: CertificateSerialNumber, 65 pub attr_cert_validity_period: AttCertValidityPeriod, 66 pub attributes: Vec<Attribute>, 67 pub issuer_unique_ud: Option<UniqueIdentifier>, 68 pub extensions: Option<Extensions>, 69 } 70 71 impl AttributeCertificateInfo { 72 pub fn take_from<S: Source>(cons: &Constructed<S>) -> Result<Self, DecodeError<S::Error>> { 73 Err(cons.content_err("AttributeCertificateInfo parsing not implemented")) 74 } 75 } 76 77 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 78 pub enum AttCertVersion { 79 V2 = 1, 80 } 81 82 /// Holder 83 /// 84 /// ```ASN.1 85 /// Holder ::= SEQUENCE { 86 /// baseCertificateID [0] IssuerSerial OPTIONAL, 87 /// -- the issuer and serial number of 88 /// -- the holder's Public Key Certificate 89 /// entityName [1] GeneralNames OPTIONAL, 90 /// -- the name of the claimant or role 91 /// objectDigestInfo [2] ObjectDigestInfo OPTIONAL 92 /// -- used to directly authenticate the holder, 93 /// -- for example, an executable 94 /// ``` 95 #[derive(Clone, Debug, Eq, PartialEq)] 96 pub struct Holder { 97 pub base_certificate_id: Option<IssuerSerial>, 98 pub entity_name: Option<GeneralNames>, 99 pub object_digest_info: Option<ObjectDigestInfo>, 100 } 101 102 #[derive(Clone, Debug, Eq, PartialEq)] 103 pub enum DigestedObjectType { 104 PublicKey = 0, 105 PublicKeyCert = 1, 106 OtherObjectTypes = 2, 107 } 108 109 /// Object digest info. 110 /// 111 /// ```ASN.1 112 /// ObjectDigestInfo ::= SEQUENCE { 113 /// digestedObjectType ENUMERATED { 114 /// publicKey (0), 115 /// publicKeyCert (1), 116 /// otherObjectTypes (2) }, 117 /// -- otherObjectTypes MUST NOT 118 /// -- be used in this profile 119 /// otherObjectTypeID OBJECT IDENTIFIER OPTIONAL, 120 /// digestAlgorithm AlgorithmIdentifier, 121 /// objectDigest BIT STRING 122 /// ``` 123 #[derive(Clone, Debug, Eq, PartialEq)] 124 pub struct ObjectDigestInfo { 125 pub digested_object_type: DigestedObjectType, 126 pub other_object_type_id: Oid, 127 pub digest_algorithm: AlgorithmIdentifier, 128 pub object_digest: BitString, 129 } 130 131 /// Att cert issuer 132 /// 133 /// ```ASN.1 134 /// AttCertIssuer ::= CHOICE { 135 /// v1Form GeneralNames, -- MUST NOT be used in this 136 /// -- profile 137 /// v2Form [0] V2Form -- v2 only 138 /// } 139 /// ``` 140 #[derive(Clone, Debug, Eq, PartialEq)] 141 pub enum AttCertIssuer { 142 V1Form(GeneralNames), 143 V2Form(Box<V2Form>), 144 } 145 146 /// V2 Form 147 /// 148 /// ```ASN.1 149 /// V2Form ::= SEQUENCE { 150 /// issuerName GeneralNames OPTIONAL, 151 /// baseCertificateID [0] IssuerSerial OPTIONAL, 152 /// objectDigestInfo [1] ObjectDigestInfo OPTIONAL 153 /// -- issuerName MUST be present in this profile 154 /// -- baseCertificateID and objectDigestInfo MUST NOT 155 /// -- be present in this profile 156 /// } 157 /// ``` 158 #[derive(Clone, Debug, Eq, PartialEq)] 159 pub struct V2Form { 160 pub issuer_name: Option<GeneralNames>, 161 pub base_certificate_id: Option<IssuerSerial>, 162 pub object_digest_info: Option<ObjectDigestInfo>, 163 } 164 165 /// Issuer serial. 166 /// 167 /// IssuerSerial ::= SEQUENCE { 168 /// issuer GeneralNames, 169 /// serial CertificateSerialNumber, 170 /// issuerUID UniqueIdentifier OPTIONAL 171 /// } 172 #[derive(Clone, Debug, Eq, PartialEq)] 173 pub struct IssuerSerial { 174 pub issuer: GeneralNames, 175 pub serial: CertificateSerialNumber, 176 pub issuer_uid: Option<UniqueIdentifier>, 177 } 178 179 /// Att cert validity period 180 /// 181 /// ```ASN.1 182 /// AttCertValidityPeriod ::= SEQUENCE { 183 /// notBeforeTime GeneralizedTime, 184 /// notAfterTime GeneralizedTime 185 /// } 186 #[derive(Clone, Debug, Eq, PartialEq)] 187 pub struct AttCertValidityPeriod { 188 pub not_before_time: GeneralizedTime, 189 pub not_after_time: GeneralizedTime, 190 } 191 192 /// Attribute 193 /// 194 /// ```ASN.1 195 /// Attribute ::= SEQUENCE { 196 /// type AttributeType, 197 /// values SET OF AttributeValue 198 /// -- at least one value is required 199 /// } 200 /// ``` 201 #[derive(Clone, Debug, Eq, PartialEq)] 202 pub struct Attribute { 203 pub typ: AttributeType, 204 pub values: Vec<AttributeValue>, 205 } 206 207 pub type AttributeType = Oid; 208 209 // TODO Any. 210 pub type AttributeValue = Option<()>;