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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

//! This crate provides TLS-based remote attestation mechanism for Teaclave,
//! supporting both EPID and ECDSA attestation. By default, Intel Attestation
//! Service is used for RA.

#![cfg_attr(feature = "mesalock_sgx", no_std)]
#[cfg(feature = "mesalock_sgx")]
#[macro_use]
extern crate sgx_tstd as std;

use std::prelude::v1::*;
use std::sync::Arc;

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

/// Errors that can happen during attestation and verification process
#[derive(thiserror::Error, Debug)]
pub enum AttestationError {
    #[error("OCall error")]
    OCallError(sgx_types::sgx_status_t),
    #[error("Attestation Service error")]
    AttestationServiceError,
    #[error("Platform error")]
    PlatformError(sgx_types::sgx_status_t),
    #[error("Report error")]
    ReportError,
    #[error("Report error")]
    ConnectionError,
    #[error("Attestation Service API version not compatible")]
    ApiVersionNotCompatible,
}

/// Remote attestation configuration
#[derive(Clone)]
pub enum AttestationConfig {
    /// Trust enclave without attestation
    NoAttestation,
    /// Perform attestation before trusting enclave
    WithAttestation(AttestationServiceConfig),
}

/// Remote attestation algorithm
#[derive(Clone)]
pub(crate) enum AttestationAlgorithm {
    /// Use Intel EPID
    SgxEpid,
    /// Use ECDSA
    SgxEcdsa,
}

impl AttestationAlgorithm {
    pub(crate) fn from_str(s: &str) -> Option<Self> {
        match s {
            "sgx_epid" => Some(AttestationAlgorithm::SgxEpid),
            "sgx_ecdsa" => Some(AttestationAlgorithm::SgxEcdsa),
            _ => None,
        }
    }
}

/// Attestation Service Configuration
#[derive(Clone)]
pub struct AttestationServiceConfig {
    /// Algorithm to use
    algo: AttestationAlgorithm,
    /// URL of attestation service
    as_url: url::Url,
    /// IAS API Key
    api_key: String,
    /// SPID
    spid: sgx_types::sgx_spid_t,
}

pub struct DcapConfig {}

impl AttestationConfig {
    /// Creates `AttestationConfig` for no attestation
    pub fn no_attestation() -> Arc<Self> {
        Arc::new(Self::NoAttestation)
    }

    /// Creates `AttestationConfig` for attestation using given values
    pub fn new(algorithm: &str, url: &str, api_key: &str, spid_str: &str) -> Result<Arc<Self>> {
        if cfg!(sgx_sim) {
            return Ok(Self::no_attestation());
        }

        use core::convert::TryFrom;

        let mut spid = sgx_types::sgx_spid_t::default();
        let hex = hex::decode(spid_str).context("Illegal SPID provided")?;
        spid.id = <[u8; 16]>::try_from(hex.as_slice()).context("Illegal SPID provided")?;

        let algo = AttestationAlgorithm::from_str(algorithm)
            .context("Unsupported remote attestation algorithm")?;

        let att_service_cfg = AttestationServiceConfig {
            algo,
            as_url: url::Url::parse(url).context("Invalid URL")?,
            api_key: api_key.to_string(),
            spid,
        };

        Ok(Arc::new(Self::WithAttestation(att_service_cfg)))
    }

    /// Crate attestation config from Teaclave runtime configuration.
    pub fn from_teaclave_config(config: &teaclave_config::RuntimeConfig) -> Result<Arc<Self>> {
        let as_config = &config.attestation;
        Self::new(
            &as_config.algorithm,
            &as_config.url,
            &as_config.key,
            &as_config.spid,
        )
    }
}

/// AttestationReport can be endorsed by either the Intel Attestation Service
/// using EPID or Data Center Attestation
/// Service (platform dependent) using ECDSA.
#[derive(Default, Serialize, Deserialize)]
pub struct EndorsedAttestationReport {
    /// Attestation report generated by the hardware
    pub report: Vec<u8>,
    /// Singature of the report
    pub signature: Vec<u8>,
    /// Certificate matching the signing key of the signature
    pub signing_cert: Vec<u8>,
}

/// Configuration for TLS communication in Remote Attestation
#[derive(Debug)]
pub struct AttestedTlsConfig {
    pub cert: Vec<u8>,
    pub private_key: Vec<u8>,
    pub time: std::time::SystemTime,
    pub validity: std::time::Duration,
}

#[macro_use]
mod cert;
pub mod report;
pub mod verifier;

cfg_if::cfg_if! {
    if #[cfg(feature = "mesalock_sgx")]  {
        mod service;
        pub mod key;
        mod platform;
        mod attestation;
        pub use attestation::RemoteAttestation;
    }
}

#[cfg(all(feature = "enclave_unit_test", feature = "mesalock_sgx"))]
pub mod tests {
    use super::*;
    use teaclave_test_utils::*;

    pub fn run_tests() -> bool {
        run_tests!(platform::tests::run_tests, report::tests::run_tests)
    }
}