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
/* 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/. */

#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;

use std::borrow::Cow;
use std::collections::hash_map::{Entry, HashMap};
use std::os::raw::c_char;
use std::sync::Mutex;
use std::{mem, ptr, slice};

use byteorder::{ByteOrder, LE};
use rustc_serialize::hex::{FromHex, ToHex};

use dcap_ql::{quote, Quote3Error};

mod ql;

use crate::ql::*;

fn u16_le(v: u16) -> [u8; 2] {
    let mut ret = [0u8; 2];
    LE::write_u16(&mut ret, v);
    ret
}

#[no_mangle]
pub extern "C" fn sgx_ql_write_persistent_data(
    _buf: *const u8,
    _len: u32,
    _label: *const c_char,
) -> Quote3Error {
    Quote3Error::PlatformLibUnavailable
}

#[no_mangle]
pub extern "C" fn sgx_ql_read_persistent_data(
    _buf: *mut u8,
    _len: *mut u32,
    _label: *const c_char,
) -> Quote3Error {
    Quote3Error::PlatformLibUnavailable
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
struct PckCertInfo<'a> {
    certchain: Cow<'a, [u8]>,
    tcbm_cpusvn: Cow<'a, [u8]>,
    tcbm_pcesvn: u16,
}

/// Get a quote with the “raw TCB”, meaning `PPID_RSA3072_ENCRYPTED`.
///
/// Results may be cached.
// Use the Quoting Library (recursively) to try to get a quote with the “raw
// TCB”, meaning `PPID_RSA3072_ENCRYPTED`. Hopefully, the QL will call
// `sgx_ql_get_quote_config` again in the same instance of this module, except
// now `get_certdata` will return early with an error because it is
// re-entered. This error should cause the QL to generate a
// `PPID_RSA3072_ENCRYPTED` quote.
//
// It doesn't matter which enclave the quote is for, so we just use a
// purpose-built built-in enclave to get a report.
fn get_quote_with_raw_tcb() -> Result<Vec<u8>, Quote3Error> {
    let ti = dcap_ql::target_info().map_err(|e| {
        error!("PPID query: failed to obtain target info: {:?}", e);
        Quote3Error::NoPlatformCertData
    })?;
    let mut loader = dcap_ql::enclave_loader().map_err(|e| {
        error!("PPID query: failed to load enclave loader: {}", e);
        Quote3Error::NoPlatformCertData
    })?;
    let report = report_test::report(&ti, &mut loader).map_err(|e| {
        error!("PPID query: {}", e);
        Quote3Error::NoPlatformCertData
    })?;
    let quote = dcap_ql::quote(&report).map_err(|e| {
        error!("PPID query: failed to obtain quote: {:?}", e);
        Quote3Error::NoPlatformCertData
    })?;
    Ok(quote)
}

/// Check if a quote matches the QE ID and parse the QE certification data from
/// the quote.
fn parse_certdata(
    qe_id: quote::QeId,
    quote: &[u8],
) -> Result<quote::Qe3CertDataPpid<'static>, Quote3Error> {
    let quote = quote::Quote::parse(quote).map_err(|e| {
        error!("PPID query: failed to parse quote: {}", e);
        Quote3Error::NoPlatformCertData
    })?;

    let quote::QuoteHeader::V3 {
        qe3_vendor_id,
        user_data,
        ..
    } = quote.header();

    if **qe3_vendor_id != quote::QE3_VENDOR_ID_INTEL {
        error!("PPID query: QE vendor ID is not Intel");
        return Err(Quote3Error::NoPlatformCertData);
    }

    let mut qe_id = qe_id.into_owned();
    qe_id.resize(20usize, 0u8);
    if *qe_id != **user_data {
        error!("PPID query: QE ID mismatch");
        return Err(Quote3Error::NoPlatformCertData);
    }

    let sig = quote
        .signature::<quote::Quote3SignatureEcdsaP256>()
        .map_err(|e| {
            error!("PPID query: {}", e);
            Quote3Error::NoPlatformCertData
        })?;

    let cd = sig
        .certification_data::<quote::Qe3CertDataPpid>()
        .map_err(|e| {
            error!("PPID query: {}", e);
            Quote3Error::NoPlatformCertData
        })?;

    // PpidEncryptedRsa2048 / PpidCleartext not supported
    if sig.certification_data_type() != quote::CertificationDataType::PpidEncryptedRsa3072 {
        error!(
            "PPID query: Invalid certification data type: {:?}",
            sig.certification_data_type()
        );
        return Err(Quote3Error::NoPlatformCertData);
    }

    Ok(cd.clone_owned())
}

/// Get the PCK cert for a QE given its QE certification data.
///
/// Results may be cached.
// This function interacts with the Intel Trusted Services API to get the PCK
// cert. The `OCP_APIM_SUBSCRIPTION_KEY` environment variable must be set with
// the user's API key.
fn get_pckcert(certdata: &quote::Qe3CertDataPpid) -> Result<PckCertInfo<'static>, Quote3Error> {
    const CPUSVN_LEN: usize = 16;
    const TCBM_LEN: usize = CPUSVN_LEN + 2; // sizeof(CPUSVN) + sizeof(ISVSVN)

    let api_key = std::env::var("OCP_APIM_SUBSCRIPTION_KEY").map_err(|_| {
        error!("OCP_APIM_SUBSCRIPTION_KEY environment variable not set");
        Quote3Error::PlatformLibUnavailable
    })?;

    let url = std::env::var("DCAP_PCKCERT_URL").ok().map_or(
        "https://api.trustedservices.intel.com/sgx/certification/v1/pckcert".into(),
        Cow::from,
    );

    // Send request
    let mut response = reqwest::Client::new()
        .get(&*url)
        .header("Ocp-Apim-Subscription-Key", api_key)
        .query(&[
            ("encrypted_ppid", certdata.ppid.to_hex()),
            ("cpusvn", certdata.cpusvn.to_hex()),
            ("pcesvn", u16_le(certdata.pcesvn).to_hex()),
            ("pceid", u16_le(certdata.pceid).to_hex()),
        ])
        .send()
        .map_err(|e| {
            error!("Failed to obtain PCK certificate: {:?}", e);
            Quote3Error::NoPlatformCertData
        })?;

    // Parse response
    if !response.status().is_success() {
        error!(
            "Failed to obtain PCK certificate, got HTTP status {}",
            response.status()
        );
        return Err(Quote3Error::NoPlatformCertData);
    }

    let tcbm_hex = response
        .headers()
        .get("SGX-TCBm")
        .map_or("".into(), |v| String::from_utf8_lossy(v.as_bytes()));
    let mut tcbm = tcbm_hex.from_hex().unwrap_or_default();
    if tcbm.len() != TCBM_LEN {
        error!("Invalid SGX-TCBm header: {:?}", tcbm_hex);
        return Err(Quote3Error::NoPlatformCertData);
    }
    let tcbm_pcesvn = LE::read_u16(&tcbm[CPUSVN_LEN..]);
    tcbm.truncate(CPUSVN_LEN);

    let mut cert = PckCertInfo {
        certchain: vec![].into(),
        tcbm_cpusvn: tcbm.into(),
        tcbm_pcesvn,
    };

    let buf = cert.certchain.to_mut();
    response.copy_to(buf).map_err(|e| {
        error!("Failed to obtain PCK certificate: {:?}", e);
        Quote3Error::NoPlatformCertData
    })?;
    if buf.last() != Some(&b'\n') {
        buf.push(b'\n');
    }
    let chain = response
        .headers()
        .get("SGX-PCK-Certificate-Issuer-Chain")
        .map_or(&[][..], |v| v.as_bytes());
    buf.extend_from_slice(chain);
    if buf.last() != Some(&b'\n') {
        buf.push(b'\n');
    }

    Ok(cert)
}

#[no_mangle]
pub extern "C" fn sgx_ql_get_quote_config(
    cert_id: &PckCertId,
    p_cert_config: *mut *const Config,
) -> Quote3Error {
    lazy_static! {
        static ref CERTDATA_CACHE: Mutex<HashMap<quote::QeId<'static>, quote::Qe3CertDataPpid<'static>>> =
            Mutex::default();
        static ref CERT_CACHE: Mutex<HashMap<quote::Qe3CertDataPpid<'static>, PckCertInfo<'static>>> =
            Mutex::default();
        static ref ENTERED_ONCE: Mutex<()> = Mutex::default();
    }
    let _ = env_logger::try_init();

    match (|| {
        // Interpret input data
        let certdata;
        match cert_id.crypto_suite {
            PCE_ALG_RSA_OAEP_3072 => unsafe {
                if cert_id.encrypted_ppid_len == 0
                    && cert_id.encrypted_ppid.is_null()
                    && !cert_id.qe3_id.is_null()
                    && cert_id.qe3_id_size == 16
                {
                    // Try to get QE3_ID to PPID mapping
                    let qe3_id = slice::from_raw_parts(cert_id.qe3_id, cert_id.qe3_id_size as _);
                    let guard = CERTDATA_CACHE.lock().unwrap();
                    let cached = guard.get(qe3_id).cloned();
                    drop(guard);
                    if let Some(cd) = cached {
                        debug!("Found cached QE certificate data");
                        certdata = cd
                    } else {
                        // Ideally, we would keep this in a thread-local
                        // variable, but there is no guarantee that the second
                        // call to this function is going to happen on the same
                        // stack.
                        let _guard = ENTERED_ONCE
                            .try_lock()
                            .map_err(|_| Quote3Error::NoPlatformCertData)?;

                        // NB. `CERTDATA_CACHE` must not be locked during this call
                        let quote = get_quote_with_raw_tcb()?;
                        certdata = parse_certdata(qe3_id.into(), &quote)?;
                        CERTDATA_CACHE
                            .lock()
                            .unwrap()
                            .insert(qe3_id.to_owned().into(), certdata.clone());
                    }
                } else if cert_id.encrypted_ppid_len != 384 || cert_id.encrypted_ppid.is_null() {
                    return Err(Quote3Error::InvalidParameter);
                } else {
                    // This code path never gets called by DCAP QL 1.0
                    certdata = quote::Qe3CertDataPpid {
                        ppid: slice::from_raw_parts(
                            cert_id.encrypted_ppid,
                            cert_id.encrypted_ppid_len as _,
                        )
                        .to_owned()
                        .into(),
                        cpusvn: cert_id
                            .platform_cpu_svn
                            .as_ref()
                            .ok_or(Quote3Error::InvalidParameter)?
                            .to_vec()
                            .into(),
                        pcesvn: *cert_id
                            .platform_pce_isv_svn
                            .as_ref()
                            .ok_or(Quote3Error::InvalidParameter)?,
                        pceid: cert_id.pce_id,
                    };
                }
            },
            v => {
                error!("Unrecognized PPID crypto suite: {}", v);
                return Err(Quote3Error::NoPlatformCertData);
            }
        }

        // Create result structure
        let mut cache = CERT_CACHE.lock().unwrap();
        let pckcert = match cache.entry(certdata) {
            Entry::Occupied(e) => {
                debug!("Found cached PCK certificate");
                e.into_mut()
            }
            Entry::Vacant(e) => {
                let pckcert = get_pckcert(e.key())?;
                e.insert(pckcert)
            }
        }
        .clone();

        if pckcert.certchain.len() > u32::max_value() as usize {
            error!(
                "Returned PCK certificate too large: {} bytes",
                pckcert.certchain.len()
            );
            return Err(Quote3Error::NoPlatformCertData);
        }

        let mut buf = Vec::with_capacity(mem::size_of::<Config>() + pckcert.certchain.len());
        buf.resize(mem::size_of::<Config>(), 0);
        buf.extend_from_slice(&pckcert.certchain);
        let mut cert_cpu_svn = [0u8; 16];
        cert_cpu_svn.copy_from_slice(&pckcert.tcbm_cpusvn);

        unsafe {
            let start = Box::into_raw(buf.into_boxed_slice());
            assert_eq!(mem::align_of::<Config>(), mem::align_of_val(&*start));
            ptr::write(
                start as *mut Config,
                Config {
                    version: ConfigVersion::V1,
                    cert_cpu_svn,
                    cert_pce_isv_svn: pckcert.tcbm_pcesvn,
                    cert_data_size: pckcert.certchain.len() as _,
                    cert_data: (start as *const u8).add(mem::size_of::<Config>()),
                },
            );
            *p_cert_config = start as *const Config;
        }
        Ok(())
    })() {
        Ok(()) => Quote3Error::Success,
        Err(v) => v,
    }
}

#[no_mangle]
pub extern "C" fn sgx_ql_free_quote_config(p_cert_config: *const Config) -> Quote3Error {
    let _ = env_logger::try_init();

    match (|| unsafe {
        let cert_data_size = p_cert_config
            .as_ref()
            .ok_or(Quote3Error::InvalidParameter)?
            .cert_data_size;
        let buflen = cert_data_size as usize + mem::size_of::<Config>();
        Box::from_raw(slice::from_raw_parts_mut(p_cert_config as *mut u8, buflen));
        Ok(())
    })() {
        Ok(()) => Quote3Error::Success,
        Err(v) => v,
    }
}