-
Notifications
You must be signed in to change notification settings - Fork 105
/
webpki.rs
320 lines (254 loc) · 11.1 KB
/
webpki.rs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
extern crate webpki;
extern crate rcgen;
extern crate ring;
extern crate pem;
#[cfg(feature = "x509-parser")]
use rcgen::CertificateSigningRequest;
use rcgen::{BasicConstraints, Certificate, CertificateParams, DnType, IsCa};
use webpki::{EndEntityCert, TlsServerTrustAnchors, TrustAnchor};
use webpki::SignatureAlgorithm;
use webpki::{Time, DnsNameRef};
use ring::rand::SystemRandom;
use ring::signature;
use ring::signature::{EcdsaKeyPair, EcdsaSigningAlgorithm,
Ed25519KeyPair, RSA_PKCS1_SHA256, RsaKeyPair};
use std::convert::TryFrom;
mod util;
fn sign_msg_ecdsa(cert :&Certificate, msg :&[u8], alg :&'static EcdsaSigningAlgorithm) -> Vec<u8> {
let pk_der = cert.serialize_private_key_der();
let key_pair = EcdsaKeyPair::from_pkcs8(&alg, &pk_der).unwrap();
let system_random = SystemRandom::new();
let signature = key_pair.sign(&system_random, &msg).unwrap();
signature.as_ref().to_vec()
}
fn sign_msg_ed25519(cert :&Certificate, msg :&[u8]) -> Vec<u8> {
let pk_der = cert.serialize_private_key_der();
let key_pair = Ed25519KeyPair::from_pkcs8_maybe_unchecked(&pk_der).unwrap();
let signature = key_pair.sign(&msg);
signature.as_ref().to_vec()
}
fn sign_msg_rsa(cert :&Certificate, msg :&[u8]) -> Vec<u8> {
let pk_der = cert.serialize_private_key_der();
let key_pair = RsaKeyPair::from_pkcs8(&pk_der).unwrap();
let system_random = SystemRandom::new();
let mut signature = vec![0; key_pair.public_modulus_len()];
key_pair.sign(&RSA_PKCS1_SHA256, &system_random, &msg,
&mut signature).unwrap();
signature
}
fn check_cert<'a, 'b>(cert_der :&[u8], cert :&'a Certificate, alg :&SignatureAlgorithm,
sign_fn :impl FnOnce(&'a Certificate, &'b [u8]) -> Vec<u8>) {
println!("{}", cert.serialize_pem().unwrap());
check_cert_ca(cert_der, cert, cert_der, alg, alg, sign_fn);
}
fn check_cert_ca<'a, 'b>(cert_der :&[u8], cert :&'a Certificate, ca_der :&[u8],
cert_alg :&SignatureAlgorithm, ca_alg :&SignatureAlgorithm,
sign_fn :impl FnOnce(&'a Certificate, &'b [u8]) -> Vec<u8>) {
let trust_anchor = TrustAnchor::try_from_cert_der(&ca_der).unwrap();
let trust_anchor_list = &[trust_anchor];
let trust_anchors = TlsServerTrustAnchors(trust_anchor_list);
let end_entity_cert = EndEntityCert::try_from(cert_der).unwrap();
// Set time to Jan 10, 2004
let time = Time::from_seconds_since_unix_epoch(0x40_00_00_00);
// (1/3) Check whether the cert is valid
end_entity_cert.verify_is_valid_tls_server_cert(
&[&cert_alg, &ca_alg],
&trust_anchors,
&[],
time,
).expect("valid TLS server cert");
// (2/3) Check that the cert is valid for the given DNS name
let dns_name = DnsNameRef::try_from_ascii_str("crabs.crabs").unwrap();
end_entity_cert.verify_is_valid_for_dns_name(
dns_name,
).expect("valid for DNS name");
// (3/3) Check that a message signed by the cert is valid.
let msg = b"Hello, World! This message is signed.";
let signature = sign_fn(&cert, msg);
end_entity_cert.verify_signature(
&cert_alg,
msg,
&signature,
).expect("signature is valid");
}
#[test]
fn test_webpki() {
let params = util::default_params();
let cert = Certificate::from_params(params).unwrap();
// Now verify the certificate.
let cert_der = cert.serialize_der().unwrap();
let sign_fn = |cert, msg| sign_msg_ecdsa(cert, msg,
&signature::ECDSA_P256_SHA256_ASN1_SIGNING);
check_cert(&cert_der, &cert, &webpki::ECDSA_P256_SHA256, sign_fn);
}
#[test]
fn test_webpki_256() {
let mut params = util::default_params();
params.alg = &rcgen::PKCS_ECDSA_P256_SHA256;
let cert = Certificate::from_params(params).unwrap();
// Now verify the certificate.
let cert_der = cert.serialize_der().unwrap();
let sign_fn = |cert, msg| sign_msg_ecdsa(cert, msg,
&signature::ECDSA_P256_SHA256_ASN1_SIGNING);
check_cert(&cert_der, &cert, &webpki::ECDSA_P256_SHA256, sign_fn);
}
#[test]
fn test_webpki_384() {
let mut params = util::default_params();
params.alg = &rcgen::PKCS_ECDSA_P384_SHA384;
let cert = Certificate::from_params(params).unwrap();
// Now verify the certificate.
let cert_der = cert.serialize_der().unwrap();
let sign_fn = |cert, msg| sign_msg_ecdsa(cert, msg,
&signature::ECDSA_P384_SHA384_ASN1_SIGNING);
check_cert(&cert_der, &cert, &webpki::ECDSA_P384_SHA384, sign_fn);
}
#[test]
fn test_webpki_25519() {
let mut params = util::default_params();
params.alg = &rcgen::PKCS_ED25519;
let cert = Certificate::from_params(params).unwrap();
// Now verify the certificate.
let cert_der = cert.serialize_der().unwrap();
check_cert(&cert_der, &cert, &webpki::ED25519, &sign_msg_ed25519);
}
#[test]
fn test_webpki_25519_v1_given() {
let mut params = util::default_params();
params.alg = &rcgen::PKCS_ED25519;
let kp = rcgen::KeyPair::from_pem(util::ED25519_TEST_KEY_PAIR_PEM_V1).unwrap();
params.key_pair = Some(kp);
let cert = Certificate::from_params(params).unwrap();
// Now verify the certificate.
let cert_der = cert.serialize_der().unwrap();
check_cert(&cert_der, &cert, &webpki::ED25519, &sign_msg_ed25519);
}
#[test]
fn test_webpki_25519_v2_given() {
let mut params = util::default_params();
params.alg = &rcgen::PKCS_ED25519;
let kp = rcgen::KeyPair::from_pem(util::ED25519_TEST_KEY_PAIR_PEM_V2).unwrap();
params.key_pair = Some(kp);
let cert = Certificate::from_params(params).unwrap();
// Now verify the certificate.
let cert_der = cert.serialize_der().unwrap();
check_cert(&cert_der, &cert, &webpki::ED25519, &sign_msg_ed25519);
}
#[test]
fn test_webpki_rsa_given() {
let mut params = util::default_params();
params.alg = &rcgen::PKCS_RSA_SHA256;
let kp = rcgen::KeyPair::from_pem(util::RSA_TEST_KEY_PAIR_PEM).unwrap();
params.key_pair = Some(kp);
let cert = Certificate::from_params(params).unwrap();
// Now verify the certificate.
let cert_der = cert.serialize_der().unwrap();
check_cert(&cert_der, &cert, &webpki::RSA_PKCS1_2048_8192_SHA256,
&sign_msg_rsa);
}
#[test]
fn test_webpki_separate_ca() {
let mut params = util::default_params();
params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
let ca_cert = Certificate::from_params(params).unwrap();
let ca_der = ca_cert.serialize_der().unwrap();
let mut params = CertificateParams::new(vec!["crabs.crabs".to_string()]);
params.distinguished_name.push(DnType::OrganizationName, "Crab widgits SE");
params.distinguished_name.push(DnType::CommonName, "Dev domain");
let cert = Certificate::from_params(params).unwrap();
let cert_der = cert.serialize_der_with_signer(&ca_cert).unwrap();
let sign_fn = |cert, msg| sign_msg_ecdsa(cert, msg,
&signature::ECDSA_P256_SHA256_ASN1_SIGNING);
check_cert_ca(&cert_der, &cert, &ca_der,
&webpki::ECDSA_P256_SHA256, &webpki::ECDSA_P256_SHA256, sign_fn);
}
#[test]
fn test_webpki_separate_ca_with_other_signing_alg() {
let mut params = util::default_params();
params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
params.alg = &rcgen::PKCS_ECDSA_P256_SHA256;
let ca_cert = Certificate::from_params(params).unwrap();
let ca_der = ca_cert.serialize_der().unwrap();
let mut params = CertificateParams::new(vec!["crabs.crabs".to_string()]);
params.alg = &rcgen::PKCS_ED25519;
params.distinguished_name.push(DnType::OrganizationName, "Crab widgits SE");
params.distinguished_name.push(DnType::CommonName, "Dev domain");
let cert = Certificate::from_params(params).unwrap();
let cert_der = cert.serialize_der_with_signer(&ca_cert).unwrap();
check_cert_ca(&cert_der, &cert, &ca_der,
&webpki::ED25519, &webpki::ECDSA_P256_SHA256, sign_msg_ed25519);
}
/*
// TODO https://github.com/briansmith/webpki/issues/134
// TODO https://github.com/briansmith/webpki/issues/135
#[test]
fn test_webpki_separate_ca_name_constraints() {
let mut params = util::default_params();
params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
params.name_constraints = Some(NameConstraints {
// TODO also add a test with non-empty permitted_subtrees that
// doesn't contain a DirectoryName entry. This isn't possible
// currently due to a limitation of webpki.
permitted_subtrees : vec![GeneralSubtree::DnsName("dev".to_string()), GeneralSubtree::DirectoryName(rcgen::DistinguishedName::new())],
//permitted_subtrees : vec![GeneralSubtree::DnsName("dev".to_string())],
//permitted_subtrees : Vec::new(),
//excluded_subtrees : vec![GeneralSubtree::DnsName("v".to_string())],
excluded_subtrees : Vec::new(),
});
let ca_cert = Certificate::from_params(params).unwrap();
println!("{}", ca_cert.serialize_pem().unwrap());
let ca_der = ca_cert.serialize_der().unwrap();
let mut params = CertificateParams::new(vec!["crabs.dev".to_string()]);
params.distinguished_name = rcgen::DistinguishedName::new();
//params.distinguished_name.push(DnType::OrganizationName, "Crab widgits SE");
//params.distinguished_name.push(DnType::CommonName, "Dev domain");
let cert = Certificate::from_params(params).unwrap();
let cert_der = cert.serialize_der_with_signer(&ca_cert).unwrap();
println!("{}", cert.serialize_pem_with_signer(&ca_cert).unwrap());
let sign_fn = |cert, msg| sign_msg_ecdsa(cert, msg,
&signature::ECDSA_P256_SHA256_ASN1_SIGNING);
check_cert_ca(&cert_der, &cert, &ca_der,
&webpki::ECDSA_P256_SHA256, &webpki::ECDSA_P256_SHA256, sign_fn);
}
*/
#[cfg(feature = "x509-parser")]
#[test]
fn test_webpki_imported_ca() {
use std::convert::TryInto;
let mut params = util::default_params();
params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
let ca_cert = Certificate::from_params(params).unwrap();
let (ca_cert_der, ca_key_der) = (ca_cert.serialize_der().unwrap(), ca_cert.serialize_private_key_der());
let ca_key_pair = ca_key_der.as_slice().try_into().unwrap();
let imported_ca_cert_params = CertificateParams::from_ca_cert_der(ca_cert_der.as_slice(), ca_key_pair)
.unwrap();
let imported_ca_cert = Certificate::from_params(imported_ca_cert_params).unwrap();
let mut params = CertificateParams::new(vec!["crabs.crabs".to_string()]);
params.distinguished_name.push(DnType::OrganizationName, "Crab widgits SE");
params.distinguished_name.push(DnType::CommonName, "Dev domain");
let cert = Certificate::from_params(params).unwrap();
let cert_der = cert.serialize_der_with_signer(&imported_ca_cert).unwrap();
let sign_fn = |cert, msg| sign_msg_ecdsa(cert, msg,
&signature::ECDSA_P256_SHA256_ASN1_SIGNING);
check_cert_ca(&cert_der, &cert, &ca_cert_der,
&webpki::ECDSA_P256_SHA256, &webpki::ECDSA_P256_SHA256, sign_fn);
}
#[cfg(feature = "x509-parser")]
#[test]
fn test_certificate_from_csr() {
let mut params = CertificateParams::new(vec!["crabs.crabs".to_string()]);
params.distinguished_name.push(DnType::OrganizationName, "Crab widgits SE");
params.distinguished_name.push(DnType::CommonName, "Dev domain");
let cert = Certificate::from_params(params).unwrap();
let csr_der = cert.serialize_request_der().unwrap();
let csr = CertificateSigningRequest::from_der(&csr_der).unwrap();
let mut params = util::default_params();
params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
let ca_cert = Certificate::from_params(params).unwrap();
let ca_cert_der = ca_cert.serialize_der().unwrap();
let cert_der = csr.serialize_der_with_signer(&ca_cert).unwrap();
let sign_fn = |cert, msg| sign_msg_ecdsa(cert, msg,
&signature::ECDSA_P256_SHA256_ASN1_SIGNING);
check_cert_ca(&cert_der, &cert, &ca_cert_der,
&webpki::ECDSA_P256_SHA256, &webpki::ECDSA_P256_SHA256, sign_fn);
}