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
//#![deny(missing_docs)]
//#![deny(warnings)]

//! This library is usefull for developing C/C++ AWS Nitro Enclave applications
//! with custom functionality like enclave-to-enclave
//! secure communication and mutual attestation.
//!
//!

use std::fmt;
use std::string::String;

use aws_cose::error::COSEError;
use aws_nitro_enclaves_cose as aws_cose;
use hex;
use webpki;

use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use serde_json;

use chrono::prelude::*;
use chrono::serde::ts_milliseconds;
use chrono::{DateTime, Duration, Utc};

use itertools::Itertools;
use std::collections::HashMap;

use x509_parser::prelude::*;

use openssl::bn::BigNumContext;
use openssl::ec::*;
use openssl::nid::Nid;

static ALL_SIGALGS: &[&webpki::SignatureAlgorithm] = &[
    &webpki::ECDSA_P256_SHA256,
    &webpki::ECDSA_P256_SHA384,
    &webpki::ECDSA_P384_SHA256,
    &webpki::ECDSA_P384_SHA384,
    &webpki::ED25519,
    #[cfg(feature = "alloc")]
    &webpki::RSA_PKCS1_2048_8192_SHA256,
    #[cfg(feature = "alloc")]
    &webpki::RSA_PKCS1_2048_8192_SHA384,
    #[cfg(feature = "alloc")]
    &webpki::RSA_PKCS1_2048_8192_SHA512,
    #[cfg(feature = "alloc")]
    &webpki::RSA_PKCS1_3072_8192_SHA384,
];

#[derive(Debug, Serialize, Deserialize)]
struct NitroAdDocPayload {
    module_id: String,
    digest: String,

    #[serde(with = "ts_milliseconds")]
    timestamp: DateTime<Utc>,

    #[serde(serialize_with = "ser_peer_public")]
    pcrs: HashMap<u8, ByteBuf>,

    #[serde(skip_serializing)]
    certificate: ByteBuf,

    #[serde(skip_serializing)]
    cabundle: Vec<ByteBuf>,

    // optional
    #[serde(skip_serializing_if = "Option::is_none")]
    public_key: Option<ByteBuf>,

    // optional
    #[serde(skip_serializing_if = "Option::is_none")]
    user_data: Option<ByteBuf>,

    // optional
    #[serde(skip_serializing_if = "Option::is_none")]
    nonce: Option<ByteBuf>,
}

fn ser_peer_public<S>(peer_public: &HashMap<u8, ByteBuf>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    let map = peer_public
        .iter()
        .sorted()
        .map(|(k, v)| (k, hex::encode(v.to_vec())));
    serializer.collect_map(map)
}

#[derive(Debug)]
pub enum NitroAdError {
    COSEError(COSEError),
    CBORError(serde_cbor::Error),
    VerificationError(webpki::Error),
    SerializationError(serde_json::Error),
    Error(String),
}

impl fmt::Display for NitroAdError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "NitroAdError: ")
    }
}

impl From<COSEError> for NitroAdError {
    fn from(err: COSEError) -> NitroAdError {
        NitroAdError::COSEError(err)
    }
}

impl From<serde_cbor::Error> for NitroAdError {
    fn from(err: serde_cbor::Error) -> NitroAdError {
        NitroAdError::CBORError(err)
    }
}

impl From<webpki::Error> for NitroAdError {
    fn from(err: webpki::Error) -> NitroAdError {
        NitroAdError::VerificationError(err)
    }
}

impl From<serde_json::Error> for NitroAdError {
    fn from(err: serde_json::Error) -> NitroAdError {
        NitroAdError::SerializationError(err)
    }
}

pub struct NitroAdDoc {
    payload_ref: NitroAdDocPayload,
}

impl NitroAdDoc {
    pub fn from_bytes(
        bytes: &[u8],
        root_cert: &[u8],
        unix_ts_sec: u64,
    ) -> Result<Self, NitroAdError> {
        let ad_doc_cose = aws_cose::COSESign1::from_bytes(bytes)?;

        // for validation flow details see here:
        // https://github.com/aws/aws-nitro-enclaves-nsm-api/blob/main/docs/attestation_process.md

        // no Signature checks for now - no key specified 
        let ad_payload = ad_doc_cose.get_payload(None)?;
        let ad_parsed: NitroAdDocPayload = serde_cbor::from_slice(&ad_payload)?;

        (ad_parsed.module_id.len() > 0)
            .then(|| ())
            .ok_or(NitroAdError::Error(String::from("module_id is empty")))?;

        (ad_parsed.digest == "SHA384")
            .then(|| ())
            .ok_or(NitroAdError::Error(String::from(
                "digest signature is unknown",
            )))?;

        // validate timestamp range
        let ts_start = Utc.ymd(2020, 1, 1).and_hms(0, 0, 0);
        let ts_end = Utc::now() + Duration::days(1);
        (ad_parsed.timestamp > ts_start && ad_parsed.timestamp < ts_end)
            .then(|| ())
            .ok_or(NitroAdError::Error(String::from(
                "timestamp field has wrong value",
            )))?;

        // validate pcr map length
        let pcrs_len = ad_parsed.pcrs.len() as u8;
        ((1..32).contains(&pcrs_len))
            .then(|| ())
            .ok_or(NitroAdError::Error(String::from(
                "wrong number of PCRs in the map",
            )))?;

        // validate pcr items
        for i in 0..pcrs_len {
            (ad_parsed.pcrs.contains_key(&i))
                .then(|| ())
                .ok_or(NitroAdError::Error(format!("PCR{} is missing", i)))?;

            let pcr_len = ad_parsed.pcrs[&i].len();
            ([32, 48, 64].contains(&pcr_len))
                .then(|| ())
                .ok_or(NitroAdError::Error(format!(
                    "PCR{} len is other than 32/48/64 bytes",
                    i
                )))?;
            //println!("prc{:2}:  {}", i, hex::encode( ad_parsed.pcrs[&i].to_vec() ) );
        }

        // validate 'certificate' member against
        // 'cabundle' with root cert replaced with our trusted hardcoded one
        let ee: &[u8] = &ad_parsed.certificate;

        let interm: Vec<ByteBuf> = ad_parsed.cabundle.clone();
        let interm = &interm[1..]; // skip first (claimed root) cert

        let interm_slices: Vec<_> = interm.iter().map(|x| x.as_slice()).collect();
        let interm_slices: &[&[u8]] = &interm_slices.to_vec();

        let anchors = vec![webpki::trust_anchor_util::cert_der_as_trust_anchor(root_cert).unwrap()];
        let anchors = webpki::TLSServerTrustAnchors(&anchors);

        let time = webpki::Time::from_seconds_since_unix_epoch(unix_ts_sec);

        let cert = webpki::EndEntityCert::from(ee)?;
        cert.verify_is_valid_tls_server_cert(ALL_SIGALGS, &anchors, interm_slices, time)?;

        let res = parse_x509_certificate(ee);
        match res {
            Ok((rem, cert)) => {
                (rem.is_empty())
                    .then(|| ())
                    .ok_or(NitroAdError::Error(String::from("rem isnot empty")))?;

                (cert.tbs_certificate.version == X509Version::V3)
                    .then(|| ())
                    .ok_or(NitroAdError::Error(String::from("wrong cert version")))?;

                let ee_pub_key = cert.tbs_certificate.subject_pki.subject_public_key.data;

                let group = EcGroup::from_curve_name(Nid::SECP384R1).unwrap();
                let mut ctx = BigNumContext::new().unwrap();
                let point = EcPoint::from_bytes(&group, &ee_pub_key, &mut ctx).unwrap();
                let key = EcKey::from_public_key(&group, &point).unwrap();

                // [TODO] remove all above parse_x509_certificate() stuff and extract public key with webpki after issue
                // https://github.com/briansmith/webpki/issues/85
                // become fixed

                if !ad_doc_cose.verify_signature(&key)? {
                    return Err(NitroAdError::COSEError(COSEError::UnimplementedError));  //should be SignatureError(openssl::error::ErrorStack)
                }
            }
            _ => {
                return Err(NitroAdError::Error(format!(
                    "x509 parsing failed: {:?}",
                    res
                )))
            }
        }

        Ok(NitroAdDoc {
            payload_ref: ad_parsed,
        })
    }

    pub fn to_json(&self) -> Result<String, NitroAdError> {
        let str = serde_json::to_string(&self.payload_ref)?;

        Ok(str)
    }
}

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

    #[test]
    fn test_payload_to_valid_json() -> Result<(), NitroAdError> {

        // current ee cert baked into the ../tests/data/nitro_ad_debug.bin attestation document has next time limits
        //
        // notBefore=Mar  5 17:01:49 2021 GMT
        // notAfter=Mar  5 20:01:49 2021 GMT
        //
        // let's substitute test timestamp within above range
        // Use next snippet to export cert
        //
        //let mut f = File::create("./_ee.der").expect("Could not run file!");
        //f.write_all(ee);
        //
        // Then, issue next cmd to see notBefore & notAfter from ./_ee.der
        // $openssl x509 -startdate -enddate -noout -inform der -in ./_ee.der

        
        let ad_blob = include_bytes!("../tests/data/nitro_ad_debug.bin");
        let root_cert = include_bytes!("../tests/data/aws_root.der");

        let nitro_addoc = NitroAdDoc::from_bytes(ad_blob, root_cert, 1614967200)?; // Mar 5 18:00:00 2021 GMT
        let js = nitro_addoc.to_json().unwrap();

        let _: serde::de::IgnoredAny = serde_json::from_str(&js)?;  // test js is valid JSON string (by trying to parse it)

        Ok(())
    }

    #[test]
    #[should_panic]
    fn test_broken_root_cert() { 

        let ad_blob = include_bytes!("../tests/data/nitro_ad_debug.bin");
        let root_cert = include_bytes!("../tests/data/aws_root.der");
        let mut root_cert_copy = root_cert.clone();

        root_cert_copy[200] = 0xff;
        let _nitro_addoc = NitroAdDoc::from_bytes(ad_blob, &root_cert_copy, 1614967200).unwrap(); // Mar 5 18:00:00 2021 GMT
    }

    #[test]
    #[should_panic]
    fn test_expired_ee_cert() { 

        let ad_blob = include_bytes!("../tests/data/nitro_ad_debug.bin");
        let root_cert = include_bytes!("../tests/data/aws_root.der");
        let _nitro_addoc = NitroAdDoc::from_bytes(ad_blob, root_cert, 1618407754).unwrap(); 
    }

    #[test]
    #[should_panic]
    fn test_notyetvalid_ee_cert() { 

        let ad_blob = include_bytes!("../tests/data/nitro_ad_debug.bin");
        let root_cert = include_bytes!("../tests/data/aws_root.der");
        let _nitro_addoc = NitroAdDoc::from_bytes(ad_blob, root_cert, 1614947200).unwrap(); 
    }

    #[test]
    #[should_panic]
    fn test_broken_some_cert_in_ad() { 

        let ad_blob = include_bytes!("../tests/data/nitro_ad_debug.bin");
        let root_cert = include_bytes!("../tests/data/aws_root.der");
        let mut ad_blob_copy = ad_blob.clone();

        ad_blob_copy[0x99f] = 0xff;
        let _nitro_addoc = NitroAdDoc::from_bytes(&ad_blob_copy, root_cert, 1614967200).unwrap();
    }

    #[test]
    #[should_panic]
    fn test_broken_ad_pcrx() { 

        let ad_blob = include_bytes!("../tests/data/nitro_ad_debug.bin");
        let root_cert = include_bytes!("../tests/data/aws_root.der");
        let mut ad_blob_copy = ad_blob.clone();

        ad_blob_copy[0x13b] = 0xff;
        let _nitro_addoc = NitroAdDoc::from_bytes(&ad_blob_copy, root_cert, 1614967200).unwrap();
    }

    #[test]
    #[should_panic]
    fn test_broken_ad_debug_pcrx() {   // mutate zero-filled PCR

        let ad_blob = include_bytes!("../tests/data/nitro_ad_debug.bin");
        let root_cert = include_bytes!("../tests/data/aws_root.der");
        let mut ad_blob_copy = ad_blob.clone();

        ad_blob_copy[0x281] = 0xff;
        let _nitro_addoc = NitroAdDoc::from_bytes(&ad_blob_copy, root_cert, 1614967200).unwrap();
    }

    #[test]
    fn cose_sign1_ec384_validate() {
        let (_, ec_public) = get_ec384_test_key();

        const TEXT: &[u8] = b"It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.";

        // This output was validated against COSE-C implementation
        let cose_doc = aws_cose::COSESign1::from_bytes(&[
            0x84, /* Protected: {1: -35} */
            0x44, 0xA1, 0x01, 0x38, 0x22, /* Unprotected: {4: '11'} */
            0xA1, 0x04, 0x42, 0x31, 0x31, /* payload: */
            0x58, 0x75, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x72, 0x75, 0x74,
            0x68, 0x20, 0x75, 0x6E, 0x69, 0x76, 0x65, 0x72, 0x73, 0x61, 0x6C, 0x6C, 0x79, 0x20,
            0x61, 0x63, 0x6B, 0x6E, 0x6F, 0x77, 0x6C, 0x65, 0x64, 0x67, 0x65, 0x64, 0x2C, 0x20,
            0x74, 0x68, 0x61, 0x74, 0x20, 0x61, 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20,
            0x6D, 0x61, 0x6E, 0x20, 0x69, 0x6E, 0x20, 0x70, 0x6F, 0x73, 0x73, 0x65, 0x73, 0x73,
            0x69, 0x6F, 0x6E, 0x20, 0x6F, 0x66, 0x20, 0x61, 0x20, 0x67, 0x6F, 0x6F, 0x64, 0x20,
            0x66, 0x6F, 0x72, 0x74, 0x75, 0x6E, 0x65, 0x2C, 0x20, 0x6D, 0x75, 0x73, 0x74, 0x20,
            0x62, 0x65, 0x20, 0x69, 0x6E, 0x20, 0x77, 0x61, 0x6E, 0x74, 0x20, 0x6F, 0x66, 0x20,
            0x61, 0x20, 0x77, 0x69, 0x66, 0x65, 0x2E, /* signature - length 48 x 2 */
            0x58, 0x60, /* R: */
            0xCD, 0x42, 0xD2, 0x76, 0x32, 0xD5, 0x41, 0x4E, 0x4B, 0x54, 0x5C, 0x95, 0xFD, 0xE6,
            0xE3, 0x50, 0x5B, 0x93, 0x58, 0x0F, 0x4B, 0x77, 0x31, 0xD1, 0x4A, 0x86, 0x52, 0x31,
            0x75, 0x26, 0x6C, 0xDE, 0xB2, 0x4A, 0xFF, 0x2D, 0xE3, 0x36, 0x4E, 0x9C, 0xEE, 0xE9,
            0xF9, 0xF7, 0x95, 0xA0, 0x15, 0x15, /* S: */
            0x5B, 0xC7, 0x12, 0xAA, 0x28, 0x63, 0xE2, 0xAA, 0xF6, 0x07, 0x8A, 0x81, 0x90, 0x93,
            0xFD, 0xFC, 0x70, 0x59, 0xA3, 0xF1, 0x46, 0x7F, 0x64, 0xEC, 0x7E, 0x22, 0x1F, 0xD1,
            0x63, 0xD8, 0x0B, 0x3B, 0x55, 0x26, 0x25, 0xCF, 0x37, 0x9D, 0x1C, 0xBB, 0x9E, 0x51,
            0x38, 0xCC, 0xD0, 0x7A, 0x19, 0x31,
        ])
        .unwrap();

        assert_eq!(cose_doc.get_payload(Some(&ec_public)).unwrap(), TEXT);
    }

    #[test]
    fn aws_root_cert_used_as_end_entity_cert() {
        let ee: &[u8] = include_bytes!("../tests/data/aws_root.der");
        let ca = include_bytes!("../tests/data/aws_root.der");

        let anchors = vec![webpki::trust_anchor_util::cert_der_as_trust_anchor(ca).unwrap()];
        let anchors = webpki::TLSServerTrustAnchors(&anchors);

        let time = webpki::Time::from_seconds_since_unix_epoch(1616094379); // 18 March 2021

        let cert = webpki::EndEntityCert::from(ee).unwrap();
        assert_eq!(
            Err(webpki::Error::CAUsedAsEndEntity),
            cert.verify_is_valid_tls_server_cert(ALL_SIGALGS, &anchors, &[], time)
        );
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    use openssl::pkey::{Private, Public};

    /// Static SECP384R1/P-384 key to be used when cross-validating the implementation
    fn get_ec384_test_key() -> (EcKey<Private>, EcKey<Public>) {
        let alg = openssl::ec::EcGroup::from_curve_name(openssl::nid::Nid::SECP384R1).unwrap();
        let x = openssl::bn::BigNum::from_hex_str(
            "5a829f62f2f4f095c0e922719285b4b981c677912870a413137a5d7319916fa8\
                584a6036951d06ffeae99ca73ab1a2dc",
        )
        .unwrap();
        let y = openssl::bn::BigNum::from_hex_str(
            "e1b76e08cb20d6afcea7423f8b49ec841dde6f210a6174750bf8136a31549422\
                4df153184557a6c29a1d7994804f604c",
        )
        .unwrap();
        let d = openssl::bn::BigNum::from_hex_str(
            "55c6aa815a31741bc37f0ffddea73af2397bad640816ef22bfb689efc1b6cc68\
                2a73f7e5a657248e3abad500e46d5afc",
        )
        .unwrap();
        let ec_public =
            openssl::ec::EcKey::from_public_key_affine_coordinates(&alg, &x, &y).unwrap();
        let ec_private =
            openssl::ec::EcKey::from_private_components(&alg, &d, &ec_public.public_key()).unwrap();
        (
            //PKey::from_ec_key(ec_private).unwrap(),
            //PKey::from_ec_key(ec_public).unwrap(),
            ec_private, ec_public,
        )
    }
}