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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
pub mod dummy_verifier;
#[cfg(feature = "ring_verifier")]
pub mod ring_verifier;
#[cfg(feature = "ring_verifier")]
pub mod x509;
#[cfg(feature = "rc_crypto_verifier")]
pub mod rc_crypto_verifier;
use super::net::{Requester, Response};
use crate::client::Collection;
use async_trait::async_trait;
use log::debug;
use serde_json::{json, Value};
use thiserror::Error;
use url::{ParseError as URLParseError, Url};
#[cfg(not(test))]
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(not(test))]
fn epoch_seconds() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
}
#[cfg(test)]
use mock_instant;
#[cfg(test)]
fn epoch_seconds() -> u64 {
mock_instant::MockClock::time().as_secs()
}
#[async_trait]
pub trait Verification: Send + Sync {
async fn fetch_certificate_chain(
&self,
requester: &'_ (dyn Requester + 'static),
collection: &Collection,
) -> Result<Vec<u8>, SignatureError> {
let x5u = collection.metadata["signature"]["x5u"]
.as_str()
.ok_or(SignatureError::MissingSignatureField())?;
debug!("Fetching certificate {}", x5u);
let response = requester
.get(Url::parse(x5u)?)
.await
.map_err(|_err| SignatureError::HTTPBackendError())?;
if !response.is_success() {
return Err(SignatureError::CertificateDownloadError {
url: x5u.to_string(),
response,
});
}
Ok(response.body)
}
fn serialize_data(&self, collection: &Collection) -> Result<Vec<u8>, SignatureError> {
let mut sorted_records = collection.records.to_vec();
sorted_records.sort_by_cached_key(|r| r.id().to_owned());
let serialized = canonical_json::to_string(&json!({
"data": sorted_records.into_iter().map(|r| r.value).collect::<Vec<Value>>(),
"last_modified": collection.timestamp.to_string()
}))?;
let data = format!("Content-Signature:\x00{}", serialized);
Ok(data.as_bytes().to_vec())
}
async fn verify(
&self,
requester: &'_ (dyn Requester + 'static),
collection: &Collection,
root_hash: &str,
) -> Result<(), SignatureError> {
let pem_bytes = self.fetch_certificate_chain(requester, collection).await?;
let signature_bytes = collection.metadata["signature"]["signature"]
.as_str()
.unwrap_or("")
.as_bytes();
let data_bytes = self.serialize_data(collection)?;
let now = epoch_seconds();
self.verify_nist384p_chain(
now,
&pem_bytes,
root_hash,
&collection.signer,
&data_bytes,
signature_bytes,
)
}
fn verify_nist384p_chain(
&self,
epoch_seconds: u64,
pem_bytes: &[u8],
root_hash: &str,
subject_cn: &str,
message: &[u8],
signature: &[u8],
) -> Result<(), SignatureError>;
fn verify_sha256_hash(&self, content: &[u8], expected: &[u8]) -> Result<(), SignatureError>;
}
#[derive(Debug, Error)]
pub enum SignatureError {
#[error("signature mismatch: {0}")]
MismatchError(String),
#[error("certificate could not be downloaded from {}: HTTP {}", url, response.status)]
CertificateDownloadError { url: String, response: Response },
#[error("certificate content could not be parsed: {0}")]
CertificateContentError(String),
#[error("root certificate fingerprint has bad format: {0}")]
RootHashFormatError(String),
#[error("root certificate fingerprint does not match: {0}")]
InvalidCertificateIssuer(String),
#[error("certificate alternate subject does not match: {0}")]
InvalidCertificateSubject(String),
#[error("certificate expired")]
CertificateExpired,
#[error("certificate chain could not be verified")]
CertificateTrustError,
#[error("certificate chain was signed with unsupported algorithm")]
UnsupportedSignatureAlgorithm,
#[error("could not hash message: {0}")]
HashingError(String),
#[error("signature contains invalid base64: {0}")]
BadSignatureContent(String),
#[error("signature payload has no x5u field")]
MissingSignatureField(),
#[error("HTTP backend issue")]
HTTPBackendError(),
#[error("bad URL format: {0}")]
URLError(#[from] URLParseError),
#[error("data could not be serialized: {0}")]
SerializationError(#[from] canonical_json::CanonicalJSONError),
}
#[cfg(test)]
mod tests {
use super::dummy_verifier::DummyVerifier;
use crate::client::net::TestHttpClient;
use crate::client::{net::DummyClient, net::Requester};
use crate::{Collection, Record, SignatureError, Verification};
use env_logger;
use httpmock::MockServer;
use mock_instant::MockClock;
use serde_json::json;
use std::time::Duration;
impl PartialEq for SignatureError {
fn eq(&self, other: &Self) -> bool {
std::mem::discriminant(self) == std::mem::discriminant(other)
}
}
#[allow(clippy::vec_init_then_push)]
async fn verify_signature(
mock_server: &MockServer,
collection: Collection,
root_hash: &str,
certificate: &str,
expected_result: Result<(), SignatureError>,
) {
let _ = viaduct::set_backend(&viaduct_reqwest::ReqwestBackend);
let mut get_pem_certificate = mock_server.mock(|when, then| {
when.path(
"/chains/remote-settings.content-signature.mozilla.org-2020-09-04-17-16-15.chain",
);
then.body(certificate);
});
#[allow(unused_mut, clippy::vec_init_then_push)]
let mut verifiers: Vec<Box<dyn Verification>> = Vec::new();
#[cfg(feature = "ring_verifier")]
verifiers.push(Box::new(super::ring_verifier::RingVerifier {}));
#[cfg(feature = "rc_crypto_verifier")]
verifiers.push(Box::new(super::rc_crypto_verifier::RcCryptoVerifier {}));
let viaduct_client: Box<dyn Requester + 'static> =
Box::new(crate::client::net::ViaductClient);
for verifier in &verifiers {
assert_eq!(
verifier
.verify(viaduct_client.as_ref(), &collection, root_hash)
.await,
expected_result
);
}
get_pem_certificate.assert_hits(verifiers.len());
get_pem_certificate.delete();
}
fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}
#[tokio::test]
async fn test_missing_x5u() {
let verifier = DummyVerifier {};
let collection = Collection {
bid: "".to_string(),
cid: "".to_string(),
metadata: json!({}),
records: vec![],
timestamp: 0,
signer: "".to_string(),
};
let dummy_client: Box<dyn Requester + 'static> = Box::new(DummyClient);
let err = verifier
.fetch_certificate_chain(dummy_client.as_ref(), &collection)
.await
.unwrap_err();
match err {
SignatureError::MissingSignatureField() => (),
e => panic!("Unexpected error type: {:?}", e),
};
}
#[tokio::test]
async fn test_bad_x5u_urls() {
let verifier = DummyVerifier {};
let _ = viaduct::set_backend(&viaduct_reqwest::ReqwestBackend);
let mock_server = MockServer::start();
let mock_server_address = mock_server.url("/file.pem");
let mut pem_mock = mock_server.mock(|when, then| {
when.path("/file.pem");
then.status(404);
});
let expectations: Vec<(&str, &str)> = vec![
("%^", "bad URL format: relative URL without a base"),
("http://localhost:9999/bad", "HTTP backend issue"),
(&mock_server_address, "/file.pem: HTTP 404"),
];
for (url, error) in expectations {
let collection = Collection {
bid: "".to_string(),
cid: "".to_string(),
metadata: json!({
"signature": {
"x5u": url
}
}),
records: vec![],
timestamp: 0,
signer: "".to_string(),
};
let viaduct_client: Box<dyn Requester + 'static> =
Box::new(crate::client::net::ViaductClient);
let err = verifier
.fetch_certificate_chain(viaduct_client.as_ref(), &collection)
.await
.unwrap_err();
assert!(err.to_string().contains(error), "{}", err.to_string());
}
pem_mock.delete();
}
#[tokio::test]
async fn test_bad_x5u_urls_request_404() {
let verifier = DummyVerifier {};
let url = "https://example.com/file.pem";
let test_client: Box<dyn Requester + 'static> = Box::new(TestHttpClient::new(vec![]));
let collection = Collection {
bid: "".to_string(),
cid: "".to_string(),
metadata: json!({
"signature": {
"x5u": url.to_string()
}
}),
records: vec![],
timestamp: 0,
signer: "".to_string(),
};
let err = verifier
.fetch_certificate_chain(test_client.as_ref(), &collection)
.await
.unwrap_err();
assert!(err
.to_string()
.eq("certificate could not be downloaded from https://example.com/file.pem: HTTP 404"));
}
#[tokio::test]
async fn test_records_canonicaljson_serialization() {
let verifier = DummyVerifier {};
let collection = Collection {
bid: "".to_string(),
cid: "".to_string(),
metadata: json!({}),
records: vec![Record::new(json!({"last_modified": 42, "id": "bonjour"}))],
timestamp: 1337,
signer: "".to_string(),
};
let bytes = verifier.serialize_data(&collection).unwrap();
let s = String::from_utf8(bytes).unwrap();
assert_eq!(s, "Content-Signature:\u{0}{\"data\":[{\"id\":\"bonjour\",\"last_modified\":42}],\"last_modified\":\"1337\"}");
}
#[tokio::test]
async fn test_verify_signature() {
init();
let mock_server: MockServer = MockServer::start();
const ROOT_HASH: &str = "3C:01:44:6A:BE:90:36:CE:A9:A0:9A:CA:A3:A5:20:AC:62:8F:20:A7:AE:32:CE:86:1C:B2:EF:B7:0F:A0:C7:45";
const VALID_CERTIFICATE: &str = "\
-----BEGIN CERTIFICATE-----
MIIDBjCCAougAwIBAgIIFml6g0ldRGowCgYIKoZIzj0EAwMwgaMxCzAJBgNVBAYT
AlVTMRwwGgYDVQQKExNNb3ppbGxhIENvcnBvcmF0aW9uMS8wLQYDVQQLEyZNb3pp
bGxhIEFNTyBQcm9kdWN0aW9uIFNpZ25pbmcgU2VydmljZTFFMEMGA1UEAww8Q29u
dGVudCBTaWduaW5nIEludGVybWVkaWF0ZS9lbWFpbEFkZHJlc3M9Zm94c2VjQG1v
emlsbGEuY29tMB4XDTIxMDIwMzE1MDQwNVoXDTIxMDQyNDE1MDQwNVowgakxCzAJ
BgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFp
biBWaWV3MRwwGgYDVQQKExNNb3ppbGxhIENvcnBvcmF0aW9uMRcwFQYDVQQLEw5D
bG91ZCBTZXJ2aWNlczE2MDQGA1UEAxMtcmVtb3RlLXNldHRpbmdzLmNvbnRlbnQt
c2lnbmF0dXJlLm1vemlsbGEub3JnMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE8pKb
HX4IiD0SCy+NO7gwKqRRZ8IhGd8PTaIHIBgM6RDLRyDeswXgV+2kGUoHyzkbNKZt
zlrS3AhqeUCtl1g6ECqSmZBbRTjCpn/UCpCnMLL0T0goxtAB8Rmi3CdM0cBUo4GD
MIGAMA4GA1UdDwEB/wQEAwIHgDATBgNVHSUEDDAKBggrBgEFBQcDAzAfBgNVHSME
GDAWgBQlZawrqt0eUz/t6OdN45oKfmzy6DA4BgNVHREEMTAvgi1yZW1vdGUtc2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b3VudGFp
biBWaWV3MRwwGgYDVQQKExNNb3ppbGxhIENvcnBvcmF0aW9uMRcwFQYDVQQLEw5D
bG91ZCBTZXJ2aWNlczE2MDQGA1UEAxMtcmVtb3RlLXNldHRpbmdzLmNvbnRlbnQt
c2lnbmF0dXJlLm1vemlsbGEub3JnMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEDmOX
N5IGlUqCvu6xkOKr020Eo3kY2uPdJO0ZihVUoglk1ktQPss184OajFOMKm/BJX4W
IsZUzQoRL8NgGfZDwBjT95Q87lhOWEWs5AU/nMXIYwDp7rpUPaUqw0QLMikdo4GD
MIGAMA4GA1UdDwEB/wQEAwIHgDATBgNVHSUEDDAKBggrBgEFBQcDAzAfBgNVHSME
GDAWgBSgHUoXT4zCKzVF8WPx2nBwp8744TA4BgNVHREEMTAvgi1yZW1vdGUtc2V0
dGluZ3MuY29udGVudC1zaWduYXR1cmUubW96aWxsYS5vcmcwCgYIKoZIzj0EAwMD
aQAwZgIxAJvyynyPqRmRMqf95FPH5xfcoT3jb/2LOkUifGDtjtZ338ScpT2glUK8
HszKVANqXQIxAIygMaeTiD9figEusmHMthBdFoIoHk31x4MHukAy+TWZ863X6/V2
6/ZrZMpinvalid==
-----END CERTIFICATE-----";
const VALID_SIGNATURE: &str = r#"fJJcOpwdnkjEWFeHXfdOJN6GaGLuDTPGzQOxA2jn6ldIleIk6KqMhZcy2GZv2uYiGwl6DERWwpaoUfQFLyCAOcVjck1qlaaEFZGY1BQba9p99xEc9FNQ3YPPfvSSZqsw"#;
const INVALID_SIGNATURE: &str = r#"invalid-signature: oPRadsg_5wnnUXlRIjamXKPWyyGe4VLt-KR4-PJTK2hq4hF196L3nbvne1_7-HfpoVRR4BLsHWtnnt6700CTt5kNgwvrE8aJ3nXFa0vJBoOvIRco-vCt-rJ7acEu0IFG"#;
let march_12_2021 = Duration::from_secs(1615559719);
MockClock::set_time(march_12_2021);
verify_signature(
&mock_server,
Collection {
bid: "main".to_owned(),
cid: "pioneer-study-addons".to_owned(),
metadata: json!({
"signature": json!({
"x5u": mock_server.url("/chains/remote-settings.content-signature.mozilla.org-2020-09-04-17-16-15.chain"),
"signature": VALID_SIGNATURE
})
}),
timestamp: 1603992731957,
records: vec![],
signer: "remote-settings.content-signature.mozilla.org".to_string(),
},
ROOT_HASH,
VALID_CERTIFICATE,
Ok(()),
).await;
verify_signature(
&mock_server,
Collection {
bid: "main".to_owned(),
cid: "pioneer-study-addons".to_owned(),
metadata: json!({
"signature": json!({
"x5u": mock_server.url("/chains/remote-settings.content-signature.mozilla.org-2020-09-04-17-16-15.chain"),
"signature": VALID_SIGNATURE
})
}),
timestamp: 1594998798350,
records: vec![Record::new(json!({"id": "bad-record"}))],
signer: "remote-settings.content-signature.mozilla.org".to_string(),
},
ROOT_HASH,
VALID_CERTIFICATE,
Err(SignatureError::MismatchError("".to_string())),
).await;
verify_signature(
&mock_server,
Collection {
bid: "main".to_owned(),
cid: "pioneer-study-addons".to_owned(),
metadata: json!({
"signature": json!({
"x5u": mock_server.url("/chains/remote-settings.content-signature.mozilla.org-2020-09-04-17-16-15.chain"),
"signature": INVALID_SIGNATURE
})
}),
timestamp: 1594998798350,
records: vec![],
signer: "remote-settings.content-signature.mozilla.org".to_string(),
},
ROOT_HASH,
VALID_CERTIFICATE,
Err(SignatureError::BadSignatureContent(
base64::DecodeError::InvalidByte(17, 58).to_string(),
)),
).await;
verify_signature(
&mock_server,
Collection {
bid: "main".to_owned(),
cid: "pioneer-study-addons".to_owned(),
metadata: json!({
"signature": json!({
"x5u": mock_server.url("/chains/remote-settings.content-signature.mozilla.org-2020-09-04-17-16-15.chain"),
"signature": VALID_SIGNATURE
})
}),
timestamp: 1594998798350,
records: vec![],
signer: "remote-settings.content-signature.mozilla.org".to_string(),
},
ROOT_HASH,
INVALID_CERTIFICATE,
Err(SignatureError::CertificateContentError("".to_string())),
).await;
verify_signature(
&mock_server,
Collection {
bid: "main".to_owned(),
cid: "pioneer-study-addons".to_owned(),
metadata: json!({
"signature": json!({
"x5u": mock_server.url("/chains/remote-settings.content-signature.mozilla.org-2020-09-04-17-16-15.chain"),
"signature": VALID_SIGNATURE
})
}),
timestamp: 1594998798350,
records: vec![],
signer: "normandy.content-signature.mozilla.org".to_string(),
},
ROOT_HASH,
VALID_CERTIFICATE,
Err(SignatureError::InvalidCertificateSubject("".to_string())),
).await;
verify_signature(
&mock_server,
Collection {
bid: "main".to_owned(),
cid: "pioneer-study-addons".to_owned(),
metadata: json!({
"signature": json!({
"x5u": mock_server.url("/chains/remote-settings.content-signature.mozilla.org-2020-09-04-17-16-15.chain"),
"signature": VALID_SIGNATURE
})
}),
timestamp: 1603992731957,
records: vec![],
signer: "remote-settings.content-signature.mozilla.org".to_string(),
},
"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00",
VALID_CERTIFICATE,
Err(SignatureError::InvalidCertificateIssuer("".to_string())),
).await;
MockClock::set_time(Duration::default());
verify_signature(
&mock_server,
Collection {
bid: "main".to_owned(),
cid: "pioneer-study-addons".to_owned(),
metadata: json!({
"signature": json!({
"x5u": mock_server.url("/chains/remote-settings.content-signature.mozilla.org-2020-09-04-17-16-15.chain"),
"signature": VALID_SIGNATURE
})
}),
timestamp: 1603992731957,
records: vec![],
signer: "remote-settings.content-signature.mozilla.org".to_string(),
},
ROOT_HASH,
VALID_CERTIFICATE,
Err(SignatureError::CertificateExpired),
).await;
}
}