rc_crypto/
contentsignature.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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::str;

use base64::{
    engine::general_purpose::{STANDARD, URL_SAFE},
    Engine,
};

use crate::error::*;
use crate::signature;

/// Verify content signatures, with the ECDSA P384 curve and SHA-384 hashing (NIST384p / secp384r1).
///
/// These signatures are typically used to guarantee integrity of data between our servers and clients.
/// This is a critical part of systems like Remote Settings or the experiment platform.
///
/// The equivalent implementation for Gecko is ``security/manager/ssl/nsIContentSignatureVerifier.idl``.
///
/// Decode a string with colon separated hexadecimal pairs into an array of bytes
/// (eg. "3C:01:44" -> [60, 1, 68]).
fn decode_root_hash(input: &str) -> Result<Vec<u8>> {
    let bytes_hex = input.split(':');

    let mut result: Vec<u8> = vec![];
    for byte_hex in bytes_hex {
        let byte = match hex::decode(byte_hex) {
            Ok(v) => v,
            Err(_) => return Err(ErrorKind::RootHashFormatError(input.to_string()).into()),
        };
        result.extend(byte);
    }

    Ok(result)
}

/// Split a certificate chain in PEM format into a list of certificates bytes,
/// decoded from base64.
fn split_pem(pem_content: &[u8]) -> Result<Vec<Vec<u8>>> {
    let pem_str = match str::from_utf8(pem_content) {
        Ok(v) => v,
        Err(e) => {
            return Err(ErrorKind::PEMFormatError(e.to_string()).into());
        }
    };

    let pem_lines = pem_str.split('\n');

    let mut blocks: Vec<Vec<u8>> = vec![];
    let mut block: Vec<u8> = vec![];
    let mut read = false;
    for line in pem_lines {
        if line.contains("-----BEGIN CERTIFICATE") {
            read = true;
        } else if line.contains("-----END CERTIFICATE") {
            read = false;
            let decoded = match STANDARD.decode(&block) {
                Ok(v) => v,
                Err(e) => return Err(ErrorKind::PEMFormatError(e.to_string()).into()),
            };
            blocks.push(decoded);
            block.clear();
        } else if read {
            block.extend_from_slice(line.as_bytes());
        }
    }
    if read {
        return Err(ErrorKind::PEMFormatError("Missing end header".into()).into());
    }
    if blocks.is_empty() {
        return Err(ErrorKind::PEMFormatError("Missing PEM data".into()).into());
    }

    Ok(blocks)
}

/// Verify that the signature matches the input data.
///
/// The data must be prefixed with ``Content-Signature:\u{0}``.
/// The signature must be provided as base 64 url-safe encoded.
/// The certificate chain, provided as PEM, must be valid at the provided current time.
/// The root certificate content must match the provided root hash, and the leaf
/// subject name must match the provided hostname.
pub fn verify(
    input: &[u8],
    signature: &[u8],
    pem_bytes: &[u8],
    seconds_since_epoch: u64,
    root_sha256_hash: &str,
    hostname: &str,
) -> Result<()> {
    let certificates = split_pem(pem_bytes)?;

    let mut certificates_slices: Vec<&[u8]> = vec![];
    for certificate in &certificates {
        certificates_slices.push(certificate);
    }

    let root_hash_bytes = decode_root_hash(root_sha256_hash)?;

    nss::pkixc::verify_code_signing_certificate_chain(
        certificates_slices,
        seconds_since_epoch,
        &root_hash_bytes,
        hostname,
    )
    .map_err(|err| match err.kind() {
        nss::ErrorKind::CertificateIssuerError => ErrorKind::CertificateIssuerError,
        nss::ErrorKind::CertificateValidityError => ErrorKind::CertificateValidityError,
        nss::ErrorKind::CertificateSubjectError => ErrorKind::CertificateSubjectError,
        _ => ErrorKind::CertificateChainError(err.to_string()),
    })?;

    let leaf_cert = certificates.first().unwrap(); // PEM parse fails if len == 0.

    let public_key_bytes = match nss::cert::extract_ec_public_key(leaf_cert) {
        Ok(bytes) => bytes,
        Err(err) => return Err(ErrorKind::CertificateContentError(err.to_string()).into()),
    };

    let signature_bytes = match URL_SAFE.decode(signature) {
        Ok(b) => b,
        Err(err) => return Err(ErrorKind::SignatureContentError(err.to_string()).into()),
    };

    // Since signature is NIST384p / secp384r1, we can perform a few safety checks.
    if signature_bytes.len() != 96 {
        return Err(ErrorKind::SignatureContentError(format!(
            "signature contains {} bytes instead of {}",
            signature_bytes.len(),
            96
        ))
        .into());
    }
    if public_key_bytes.len() != 96 + 1 {
        // coordinates with x04 prefix.
        return Err(ErrorKind::CertificateContentError(format!(
            "public key contains {} bytes instead of {}",
            public_key_bytes.len(),
            97
        ))
        .into());
    }

    let signature_alg = &signature::ECDSA_P384_SHA384;
    let public_key = signature::UnparsedPublicKey::new(signature_alg, &public_key_bytes);
    // Note that if the provided key type or curve is incorrect here, the signature will
    // be considered as invalid.
    match public_key.verify(input, &signature_bytes) {
        Ok(_) => Ok(()),
        Err(err) => Err(ErrorKind::SignatureMismatchError(err.to_string()).into()),
    }
}

#[cfg(test)]
mod test {
    use super::*;

    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_CERT_CHAIN: &[u8] = b"\
-----BEGIN CERTIFICATE-----
MIIDBjCCAougAwIBAgIIFml6g0ldRGowCgYIKoZIzj0EAwMwgaMxCzAJBgNVBAYT
AlVTMRwwGgYDVQQKExNNb3ppbGxhIENvcnBvcmF0aW9uMS8wLQYDVQQLEyZNb3pp
bGxhIEFNTyBQcm9kdWN0aW9uIFNpZ25pbmcgU2VydmljZTFFMEMGA1UEAww8Q29u
dGVudCBTaWduaW5nIEludGVybWVkaWF0ZS9lbWFpbEFkZHJlc3M9Zm94c2VjQG1v
emlsbGEuY29tMB4XDTIxMDIwMzE1MDQwNVoXDTIxMDQyNDE1MDQwNVowgakxCzAJ
BgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFp
biBWaWV3MRwwGgYDVQQKExNNb3ppbGxhIENvcnBvcmF0aW9uMRcwFQYDVQQLEw5D
bG91ZCBTZXJ2aWNlczE2MDQGA1UEAxMtcmVtb3RlLXNldHRpbmdzLmNvbnRlbnQt
c2lnbmF0dXJlLm1vemlsbGEub3JnMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE8pKb
HX4IiD0SCy+NO7gwKqRRZ8IhGd8PTaIHIBgM6RDLRyDeswXgV+2kGUoHyzkbNKZt
zlrS3AhqeUCtl1g6ECqSmZBbRTjCpn/UCpCnMLL0T0goxtAB8Rmi3CdM0cBUo4GD
MIGAMA4GA1UdDwEB/wQEAwIHgDATBgNVHSUEDDAKBggrBgEFBQcDAzAfBgNVHSME
GDAWgBQlZawrqt0eUz/t6OdN45oKfmzy6DA4BgNVHREEMTAvgi1yZW1vdGUtc2V0
dGluZ3MuY29udGVudC1zaWdu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AC32fBjE5lWYEgsxNM5VWlQjbgTG0dc3dYiufxfDFeMbAPmD
DzpIgN3jHW2uRqa/MJ+egHhv7kGFL68uVLboqk/qHr+SOCc1LNeSMCuQqvHwwM0+
AU1GxhzBWDkealTS34FpVxF4sT5sKLODdIS5HXJr2COHHfYkw2SW/Sfpt6fsOwaF
2iiDaK4LPWHWhhIYa6yaynJ+6O6KPlpvKYCChaTOVdc+ikyeiSO6AakJykr5Gy7d
PkkK7MDCxuY6psHj7iJQ59YK7ujQB8QYdzuXBuLLo5hc5gBcq3PJs0fLT2YFcQHA
dj+olGaDn38T0WI8ycWaFhQfKwATeLWfiQepr8JfoNlC2vvSDzGUGfdAfZfsJJZ8
5xZxahHoTFGS0mDRfXqzKH5uD578GgjOZp0fULmzkcjWsgzdpDhadGjExRZFKlAy
iKv8cXTONrGY0fyBDKennuX0uAca3V0Qm6v2VRp+7wG/pywWwc5n+04qgxTQPxgO
6pPB9UUsNbaLMDR5QPYAWrNhqJ7B07XqIYJZSwGP5xB9NqUZLF4z+AOMYgWtDpmg
IKdcFKAt3fFrpyMhlfIKkLfmm0iDjmfmIXbDGBJw9SE=
-----END CERTIFICATE-----";
    const VALID_INPUT: &[u8] =
        b"Content-Signature:\x00{\"data\":[],\"last_modified\":\"1603992731957\"}";
    const VALID_SIGNATURE: &[u8] = b"fJJcOpwdnkjEWFeHXfdOJN6GaGLuDTPGzQOxA2jn6ldIleIk6KqMhZcy2GZv2uYiGwl6DERWwpaoUfQFLyCAOcVjck1qlaaEFZGY1BQba9p99xEc9FNQ3YPPfvSSZqsw";
    const VALID_HOSTNAME: &str = "remote-settings.content-signature.mozilla.org";

    const INVALID_CERTIFICATE: &[u8] = b"\
    -----BEGIN CERTIFICATE-----
    invalidCertificategIFiJLFfdxFlYwCgYIKoZIzj0EAwMwgaMxCzAJBgNVBAYT
    AlVTMRwwGgYDVQQKExNNb3ppbGxhIENvcnBvcmF0aW9uMS8wLQYDVQQLEyZNb3pp
    bGxhIEFNTyBQcm9kdWN0aW9uIFNpZ25pbmcgU2VydmljZTFFMEMGA1UEAww8Q29u
    dGVudCBTaWduaW5nIEludGVybWVkaWF0ZS9lbWFpbEFkZHJlc3M9Zm94c2VjQG1v
    emlsbGEuY29tMB4XDTIwMDYxNjE3MTYxNVoXDTIwMDkwNDE3MTYxNVowgakxCzAJ
    BgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFp
    biBWaWV3MRwwGgYDVQQKExNNb3ppbGxhIENvcnBvcmF0aW9uMRcwFQYDVQQLEw5D
    bG91ZCBTZXJ2aWNlczE2MDQGA1UEAxMtcmVtb3RlLXNldHRpbmdzLmNvbnRlbnQt
    c2lnbmF0dXJlLm1vemlsbGEub3JnMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEDmOX
    N5IGlUqCvu6xkOKr020Eo3kY2uPdJO0ZihVUoglk1ktQPss184OajFOMKm/BJX4W
    IsZUzQoRL8NgGfZDwBjT95Q87lhOWEWs5AU/nMXIYwDp7rpUPaUqw0QLMikdo4GD
    MIGAMA4GA1UdDwEB/wQEAwIHgDATBgNVHSUEDDAKBggrBgEFBQcDAzAfBgNVHSME
    GDAWgBSgHUoXT4zCKzVF8WPx2nBwp8744TA4BgNVHREEMTAvgi1yZW1vdGUtc2V0
    dGluZ3MuY29udGVudC1zaWduYXR1cmUubW96aWxsYS5vcmcwCgYIKoZIzj0EAwMD
    aQAwZgIxAJvyynyPqRmRMqf95FPH5xfcoT3jb/2LOkUifGDtjtZ338ScpT2glUK8
    HszKVANqXQIxAIygMaeTiD9figEusmHMthBdFoIoHk31x4MHukAy+TWZ863X6/V2
    6/ZrZMpinvalid==
    -----END CERTIFICATE-----";

    #[test]
    fn test_decode_root_hash() {
        assert!(decode_root_hash("meh!").is_err());
        assert!(decode_root_hash("3C:rr:44").is_err());

        let result = decode_root_hash(ROOT_HASH).unwrap();
        assert_eq!(
            result,
            vec![
                60, 1, 68, 106, 190, 144, 54, 206, 169, 160, 154, 202, 163, 165, 32, 172, 98, 143,
                32, 167, 174, 50, 206, 134, 28, 178, 239, 183, 15, 160, 199, 69
            ]
        );
    }

    #[test]
    fn test_split_pem() {
        assert!(split_pem(b"meh!").is_err());

        assert!(split_pem(
            b"-----BEGIN CERTIFICATE-----
invalidCertificate
-----END CERTIFICATE-----"
        )
        .is_err());

        assert!(split_pem(
            b"-----BEGIN CERTIFICATE-----
bGxhIEFNTyBQcm9kdWN0aW9uIFNp
-----BEGIN CERTIFICATE-----"
        )
        .is_err());

        let result = split_pem(
            b"-----BEGIN CERTIFICATE-----
AQID
BAUG
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
/f7/
-----END CERTIFICATE-----",
        )
        .unwrap();
        assert_eq!(result, vec![vec![1, 2, 3, 4, 5, 6], vec![253, 254, 255]]);
    }

    #[test]
    fn test_verify_fails_if_invalid() {
        assert!(verify(
            b"msg",
            b"sig",
            b"-----BEGIN CERTIFICATE-----
fdfeff
-----END CERTIFICATE-----",
            42,
            ROOT_HASH,
            "remotesettings.firefox.com",
        )
        .is_err());
    }

    #[test]
    fn test_verify_fails_if_cert_has_expired() {
        assert!(verify(
            VALID_INPUT,
            VALID_SIGNATURE,
            VALID_CERT_CHAIN,
            1215559719, // July 9, 2008
            ROOT_HASH,
            VALID_HOSTNAME,
        )
        .is_err());
    }

    #[test]
    fn test_verify_fails_if_bad_certificate_chain() {
        assert!(verify(
            VALID_INPUT,
            VALID_SIGNATURE,
            INVALID_CERTIFICATE,
            1615559719, // March 12, 2021
            ROOT_HASH,
            VALID_HOSTNAME,
        )
        .is_err());
    }

    #[test]
    fn test_verify_fails_if_mismatch() {
        assert!(verify(
            b"msg",
            VALID_SIGNATURE,
            VALID_CERT_CHAIN,
            1615559719, // March 12, 2021
            ROOT_HASH,
            VALID_HOSTNAME,
        )
        .is_err());
    }

    #[test]
    fn test_verify_fails_if_bad_hostname() {
        assert!(verify(
            VALID_INPUT,
            VALID_SIGNATURE,
            VALID_CERT_CHAIN,
            1615559719, // March 12, 2021
            ROOT_HASH,
            "some.hostname.org",
        )
        .is_err());
    }

    #[test]
    fn test_verify_fails_if_bad_root_hash() {
        assert!(verify(
            VALID_INPUT,
            VALID_SIGNATURE,
            VALID_CERT_CHAIN,
            1615559719, // March 12, 2021
            "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_HOSTNAME,
        )
        .is_err());
    }

    #[test]
    fn test_verify_succeeds_if_valid() {
        verify(
            VALID_INPUT,
            VALID_SIGNATURE,
            VALID_CERT_CHAIN,
            1615559719, // March 12, 2021
            ROOT_HASH,
            VALID_HOSTNAME,
        )
        .unwrap();
    }
}