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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::cri_attributes::*;
use crate::error::{X509Error, X509Result};
use crate::extensions::*;
use crate::x509::{
parse_signature_value, AlgorithmIdentifier, SubjectPublicKeyInfo, X509Name, X509Version,
};
use asn1_rs::{BitString, FromDer};
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: BitString<'a>,
}
impl<'a> X509CertificationRequest<'a> {
pub fn requested_extensions(&self) -> Option<impl Iterator<Item = &ParsedExtension>> {
self.certification_request_info
.iter_attributes()
.find_map(|attr| {
if let ParsedCriAttribute::ExtensionRequest(requested) = &attr.parsed_attribute {
Some(requested.extensions.iter().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 if *signature_alg == OID_SIG_ED25519 {
&signature::ED25519
} 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))
}
}
impl<'a> FromDer<'a, X509Error> for X509CertificationRequest<'a> {
fn from_der(i: &'a [u8]) -> X509Result<'a, 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)
}
}
#[derive(Debug, PartialEq)]
pub struct X509CertificationRequestInfo<'a> {
pub version: X509Version,
pub subject: X509Name<'a>,
pub subject_pki: SubjectPublicKeyInfo<'a>,
attributes: Vec<X509CriAttribute<'a>>,
pub raw: &'a [u8],
}
impl<'a> X509CertificationRequestInfo<'a> {
#[inline]
pub fn attributes(&self) -> &[X509CriAttribute] {
&self.attributes
}
#[inline]
pub fn iter_attributes(&self) -> impl Iterator<Item = &X509CriAttribute> {
self.attributes.iter()
}
pub fn find_attribute(&self, oid: &Oid) -> Option<&X509CriAttribute> {
self.attributes.iter().find(|&ext| ext.oid == *oid)
}
pub fn attributes_map(&self) -> Result<HashMap<Oid, &X509CriAttribute>, X509Error> {
self.attributes
.iter()
.try_fold(HashMap::new(), |mut m, ext| {
if m.contains_key(&ext.oid) {
return Err(X509Error::DuplicateAttributes);
}
m.insert(ext.oid.clone(), ext);
Ok(m)
})
}
}
impl<'a> FromDer<'a, X509Error> for X509CertificationRequestInfo<'a> {
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)
}
}