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
#[cfg(feature = "logging")]
use crate::log::{debug, trace};
use crate::x509;
use crate::{key, DistinguishedName};
use crate::{CertificateError, Error};
#[derive(Debug, Clone)]
pub struct OwnedTrustAnchor {
subject_dn_header_len: usize,
subject_dn: DistinguishedName,
spki: Vec<u8>,
name_constraints: Option<Vec<u8>>,
}
impl OwnedTrustAnchor {
pub(crate) fn to_trust_anchor(&self) -> webpki::TrustAnchor {
webpki::TrustAnchor {
subject: &self.subject_dn.as_ref()[self.subject_dn_header_len..],
spki: &self.spki,
name_constraints: self.name_constraints.as_deref(),
}
}
pub fn from_subject_spki_name_constraints(
subject: impl Into<Vec<u8>>,
spki: impl Into<Vec<u8>>,
name_constraints: Option<impl Into<Vec<u8>>>,
) -> Self {
let (subject_dn, subject_dn_header_len) = {
let mut subject = subject.into();
let before_len = subject.len();
x509::wrap_in_sequence(&mut subject);
let header_len = subject.len().saturating_sub(before_len);
(DistinguishedName::from(subject), header_len)
};
Self {
subject_dn_header_len,
subject_dn,
spki: spki.into(),
name_constraints: name_constraints.map(|x| x.into()),
}
}
pub fn subject(&self) -> &DistinguishedName {
&self.subject_dn
}
}
#[derive(Debug, Clone)]
pub struct RootCertStore {
pub roots: Vec<OwnedTrustAnchor>,
}
impl RootCertStore {
pub fn empty() -> Self {
Self { roots: Vec::new() }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
self.roots.len()
}
pub fn add(&mut self, der: &key::Certificate) -> Result<(), Error> {
self.add_internal(&der.0)
}
pub fn add_server_trust_anchors(
&mut self,
trust_anchors: impl Iterator<Item = OwnedTrustAnchor>,
) {
self.roots.extend(trust_anchors);
}
pub fn add_parsable_certificates(&mut self, der_certs: &[impl AsRef<[u8]>]) -> (usize, usize) {
let mut valid_count = 0;
let mut invalid_count = 0;
for der_cert in der_certs {
#[cfg_attr(not(feature = "logging"), allow(unused_variables))]
match self.add_internal(der_cert.as_ref()) {
Ok(_) => valid_count += 1,
Err(err) => {
trace!("invalid cert der {:?}", der_cert.as_ref());
debug!("certificate parsing failed: {:?}", err);
invalid_count += 1;
}
}
}
debug!(
"add_parsable_certificates processed {} valid and {} invalid certs",
valid_count, invalid_count
);
(valid_count, invalid_count)
}
fn add_internal(&mut self, der: &[u8]) -> Result<(), Error> {
let ta = webpki::TrustAnchor::try_from_cert_der(der)
.map_err(|_| Error::InvalidCertificate(CertificateError::BadEncoding))?;
self.roots
.push(OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
));
Ok(())
}
}
mod tests {
#[test]
fn ownedtrustanchor_subject_is_correctly_encoding_dn() {
let subject = b"subject".to_owned();
let ota = super::OwnedTrustAnchor::from_subject_spki_name_constraints(
subject,
b"".to_owned(),
None::<Vec<u8>>,
);
let expected_prefix = vec![ring::io::der::Tag::Sequence as u8, subject.len() as u8];
assert_eq!(
ota.subject().as_ref(),
[expected_prefix, subject.to_vec()].concat()
);
}
}