1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::cri_attributes::*;
#[cfg(feature = "verify")]
use crate::error::X509Error;
use crate::error::X509Result;
use crate::extensions::*;
use crate::x509::{
parse_signature_value, AlgorithmIdentifier, SubjectPublicKeyInfo, X509Name, X509Version,
};
use der_parser::ber::BitStringObject;
use der_parser::der::*;
use der_parser::oid::Oid;
use der_parser::*;
use nom::Offset;
#[cfg(feature = "verify")]
use oid_registry::*;
use std::collections::HashMap;
#[derive(Debug, PartialEq)]
pub struct X509CertificationRequest<'a> {
pub certification_request_info: X509CertificationRequestInfo<'a>,
pub signature_algorithm: AlgorithmIdentifier<'a>,
pub signature_value: BitStringObject<'a>,
}
impl<'a> X509CertificationRequest<'a> {
pub fn from_der(i: &'a [u8]) -> X509Result<Self> {
parse_der_sequence_defined_g(|i, _| {
let (i, certification_request_info) = X509CertificationRequestInfo::from_der(i)?;
let (i, signature_algorithm) = AlgorithmIdentifier::from_der(i)?;
let (i, signature_value) = parse_signature_value(i)?;
let cert = X509CertificationRequest {
certification_request_info,
signature_algorithm,
signature_value,
};
Ok((i, cert))
})(i)
}
pub fn requested_extensions(&self) -> Option<impl Iterator<Item = &ParsedExtension<'a>>> {
self.certification_request_info
.attributes
.values()
.find_map(|attr| {
if let ParsedCriAttribute::ExtensionRequest(requested) = &attr.parsed_attribute {
Some(
requested
.extensions
.values()
.map(|ext| &ext.parsed_extension),
)
} else {
None
}
})
}
#[cfg(feature = "verify")]
pub fn verify_signature(&self) -> Result<(), X509Error> {
use ring::signature;
let spki = &self.certification_request_info.subject_pki;
let signature_alg = &self.signature_algorithm.algorithm;
let verification_alg: &dyn signature::VerificationAlgorithm =
if *signature_alg == OID_PKCS1_SHA1WITHRSA {
&signature::RSA_PKCS1_1024_8192_SHA1_FOR_LEGACY_USE_ONLY
} else if *signature_alg == OID_PKCS1_SHA256WITHRSA {
&signature::RSA_PKCS1_2048_8192_SHA256
} else if *signature_alg == OID_PKCS1_SHA384WITHRSA {
&signature::RSA_PKCS1_2048_8192_SHA384
} else if *signature_alg == OID_PKCS1_SHA512WITHRSA {
&signature::RSA_PKCS1_2048_8192_SHA512
} else if *signature_alg == OID_SIG_ECDSA_WITH_SHA256 {
&signature::ECDSA_P256_SHA256_ASN1
} else if *signature_alg == OID_SIG_ECDSA_WITH_SHA384 {
&signature::ECDSA_P384_SHA384_ASN1
} else {
return Err(X509Error::SignatureUnsupportedAlgorithm);
};
let key = signature::UnparsedPublicKey::new(verification_alg, spki.subject_public_key.data);
let sig = self.signature_value.data;
key.verify(self.certification_request_info.raw, sig)
.or(Err(X509Error::SignatureVerificationError))
}
}
#[derive(Debug, PartialEq)]
pub struct X509CertificationRequestInfo<'a> {
pub version: X509Version,
pub subject: X509Name<'a>,
pub subject_pki: SubjectPublicKeyInfo<'a>,
pub attributes: HashMap<Oid<'a>, X509CriAttribute<'a>>,
pub raw: &'a [u8],
}
impl<'a> X509CertificationRequestInfo<'a> {
pub fn from_der(i: &'a [u8]) -> X509Result<Self> {
let start_i = i;
parse_der_sequence_defined_g(move |i, _| {
let (i, version) = X509Version::from_der_required(i)?;
let (i, subject) = X509Name::from_der(i)?;
let (i, subject_pki) = SubjectPublicKeyInfo::from_der(i)?;
let (i, attributes) = parse_cri_attributes(i)?;
let len = start_i.offset(i);
let tbs = X509CertificationRequestInfo {
version,
subject,
subject_pki,
attributes,
raw: &start_i[..len],
};
Ok((i, tbs))
})(i)
}
}