pcs/
lib.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
/* Copyright (c) Fortanix, Inc.
 *
 * 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/.
 */

#![deny(warnings)]
extern crate failure;
extern crate percent_encoding;
extern crate yasna;
#[macro_use]
extern crate quick_error;

use std::fmt;

use serde::de::{self};
use serde::{Deserialize, Deserializer, Serialize};
pub use yasna::ASN1Error;
#[cfg(feature = "verify")]
use {
    mbedtls::Error as MbedError,
    mbedtls::alloc::{Box as MbedtlsBox, List as MbedtlsList},
    mbedtls::x509::certificate::Certificate,
    std::ffi::CString,
    std::ops::Deref,
};

pub use crate::pckcrl::PckCrl;
pub use crate::pckcrt::{PckCert, PckCerts, SGXPCKCertificateExtension, SGXType};
pub use crate::qe_identity::{QeIdentity, QeIdentitySigned};
pub use crate::tcb_info::{Fmspc, TcbInfo, TcbData};

mod io;
mod pckcrl;
mod pckcrt;
mod pckid;
mod qe_identity;
mod tcb_info;

pub type CpuSvn = [u8; 16];
pub type EncPpid = Vec<u8>;
pub type PceId = u16;
pub type PceIsvsvn = u16;
pub type QeId = [u8; 16];
pub use crate::pckid::PckID;

quick_error! {
    #[derive(Debug)]
    pub enum Error {
        MissingCaChain{
            display("CA chain was unexpectedly empty")
        }
        IncorrectCA {
            display("Invalid CA")
        }
        InvalidCaFormat {
            display("CA certificate could not be parsed")
        }
        InvalidPckFormat(err: ASN1Error){
            display("Invalid formatted PckCert: {}", err)
        }
        InvalidPck(err: String){
            display("Invalid PCK: {}", err)
        }
        InvalidPcks(err: String){
            display("Invalid PCKs: {}", err)
        }
        InvalidFormatQe3Quote{
            display("Qe3 Quote could not be parsed")
        }
        NoPckForTcbFound{
            display("No PCK matching the TCB was found")
        }
        #[cfg(feature = "verify")]
        InvalidCrl(err: MbedError){
            display("Invalid CRL: {}", err)
        }
        InvalidCrlFormat{
            display("Invalid CRL format")
        }
        InvalidTcbInfo(err: String){
            display("Invalid TCB info: {}", err)
        }
        UnknownTcbType(tcb_type: u16){
            display("Unknown TCB type: {}", tcb_type)
        }
        #[cfg(feature = "verify")]
        InvalidQe3Id(err: MbedError){
            display("Invalid QE3 ID: {}", err)
        }
        InvalidFormatQe3Identity{
            display("Invalid QE3 Identity format")
        }
        IoError(err: std::io::Error){
            display("I/O error: {}", err)
            from()
        }
        ParseError(err: serde_json::error::Error){
            from()
            display("json error: {}", err)
        }
        NoPckCertData{
            display("Empty PckCerts")
        }
        EncodingError(err: serde_json::error::Error){
            display("json error: {}", err)
        }
        UnknownTcbInfoVersion(version: u16){
            display("The TCB Info structure has unexpected version: {}", version)
        }
        EnclaveTcbLevelNotFound {
            display("TCB level not found for enclave")
        }
        UnknownQeIdentityVersion(version: u16){
            display("The QEIdentity structure has unexpected version: {}", version)
        }
        InvalidDcapAttestationFormat{
            display("The DCAP Attestation certificate has an unexpected format")
        }
    }
}

pub trait VerificationType {}

#[derive(Clone, Debug)]
pub struct Verified;

impl VerificationType for Verified {}

#[derive(Clone, Debug)]
pub struct Unverified;

impl VerificationType for Unverified {}

/// Intel specifies raw ECDSA signatures in a different format than mbedtls. Convert ECDSA
/// signature to RFC5480 ASN.1 representation.
fn get_ecdsa_sig_der(sig: &[u8]) -> Result<Vec<u8>, ()> {
    if sig.len() % 2 != 0 {
        return Err(());
    }

    let (r_bytes, s_bytes) = sig.split_at(sig.len() / 2);
    let r = num::BigUint::from_bytes_be(r_bytes);
    let s = num::BigUint::from_bytes_be(s_bytes);

    let der = yasna::construct_der(|writer| {
        writer.write_sequence(|writer| {
            writer.next().write_biguint(&r);
            writer.next().write_biguint(&s);
        })
    });

    Ok(der)
}

fn intel_signature_deserializer<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<u8>, D::Error> {
    let signature = String::deserialize(deserializer)?;
    let signature = &base16::decode(signature.as_bytes()).map_err(de::Error::custom)?;
    crate::get_ecdsa_sig_der(signature).map_err(|_| de::Error::custom("Failed ECDSA signature conversion"))
}

#[cfg(feature = "verify")]
fn create_cert_chain(certs: &Vec<String>) -> Result<(Vec<MbedtlsBox<Certificate>>, MbedtlsBox<Certificate>), Error> {
    fn str_to_cert_box(ca: &String) -> Result<MbedtlsBox<Certificate>, Error> {
        let ca = CString::new(ca.as_bytes()).map_err(|_| Error::InvalidCaFormat)?;
        Certificate::from_pem(ca.as_bytes_with_nul()).map_err(|_| Error::InvalidCaFormat)
    }
    if let Some((last_cert, certs)) = certs.split_last() {
        let chain = certs.iter().map(str_to_cert_box).collect::<Result<Vec<_>, _>>()?;
        let last_cert = str_to_cert_box(last_cert)?;
        Ok((chain, last_cert))
    } else {
        Err(Error::MissingCaChain)
    }
}

// Typically, certificates are verified directly against a pool of trusted root
// certificates. The DCAP attestation verification logic works differently.
// It first verifies against a root certificate included in the attestation,
// and then checks that the root certificate included in the attestation is
// a trusted root certificate.
//
// There are two different versions of the SGX root CA in circulation (both
// available in tests/data/ of this crate). They share the same key, but
// have a different expiration date and a different CRL reference (PEM vs. DER
// format). Because we have existing DCAP verifiers configured with only one
// of the certificates, we perform a certificate verification of the root
// in the attestation against the trusted root, rather than look for a
// byte-for-byte match between the attestation root and the trusted root.
#[cfg(feature = "verify")]
fn check_root_ca<B: Deref<Target = [u8]>>(trusted_root_certs: &[B], candidate: &MbedtlsList<Certificate>) -> Result<(), Error> {
    if trusted_root_certs
        .iter()
        .filter_map(|trusted_der| Certificate::from_der(&**trusted_der).ok())
        .any(|trusted| Certificate::verify(candidate, &std::iter::once(trusted).collect(), None, None).is_ok())
    {
        return Ok(());
    } else {
        return Err(Error::IncorrectCA);
    }
}

#[cfg(test)]
#[cfg(not(target_env = "sgx"))]
fn get_cert_subject(cert: &str) -> String {
    let der = &pkix::pem::pem_to_der(cert.trim(), Some(pkix::pem::PEM_CERTIFICATE))
        .ok_or(ASN1Error::new(yasna::ASN1ErrorKind::Invalid))
        .unwrap();
    get_cert_subject_from_der(der)
}

#[cfg(test)]
#[cfg(not(target_env = "sgx"))]
fn get_cert_subject_from_der(cert: &Vec<u8>) -> String {
    use pkix::FromBer;
    let cert = pkix::x509::GenericCertificate::from_ber(&cert).unwrap();
    let name = cert.tbscert.subject.get(&*pkix::oid::commonName).unwrap();
    String::from_utf8_lossy(&name.value()).to_string()
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, Copy)]
pub enum TcbStatus {
    UpToDate,
    SWHardeningNeeded,
    ConfigurationNeeded,
    ConfigurationAndSWHardeningNeeded,
    OutOfDate,
    OutOfDateConfigurationNeeded,
    Revoked,
}

impl fmt::Display for TcbStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TcbStatus::UpToDate => write!(f, "Up to Date"),
            TcbStatus::SWHardeningNeeded => write!(f, "Software Hardening Needed"),
            TcbStatus::ConfigurationNeeded => write!(f, "Configuration Needed"),
            TcbStatus::ConfigurationAndSWHardeningNeeded => write!(f, "Configuration And Software Hardening Needed"),
            TcbStatus::OutOfDate => write!(f, "Out of Date"),
            TcbStatus::OutOfDateConfigurationNeeded => write!(f, "Out of Date, Configuration Needed"),
            TcbStatus::Revoked => write!(f, "Revoked"),
        }
    }
}