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
#![allow(clippy::nonstandard_macro_braces)]
extern crate sgx_types;
use std::sync::Arc;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use sgx_types::error::SgxStatus;
use sgx_types::types::Spid;
#[derive(thiserror::Error, Debug)]
pub enum AttestationError {
#[error("OCall error")]
OCallError(SgxStatus),
#[error("Attestation Service error")]
AttestationServiceError,
#[error("Platform error")]
PlatformError(SgxStatus),
#[error("Report error")]
ReportError,
#[error("Report error")]
ConnectionError,
#[error("Attestation Service API version not compatible")]
ApiVersionNotCompatible,
}
#[derive(Clone)]
pub enum AttestationConfig {
NoAttestation,
WithAttestation(AttestationServiceConfig),
}
#[derive(Clone)]
pub(crate) enum AttestationAlgorithm {
SgxEpid,
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,
}
}
}
#[derive(Clone)]
#[allow(dead_code)]
pub struct AttestationServiceConfig {
algo: AttestationAlgorithm,
as_url: url::Url,
api_key: String,
spid: Spid,
}
pub struct DcapConfig {}
impl AttestationConfig {
pub fn no_attestation() -> Arc<Self> {
Arc::new(Self::NoAttestation)
}
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());
}
let mut spid = Spid::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)))
}
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,
)
}
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct EndorsedAttestationReport {
pub report: Vec<u8>,
pub signature: Vec<u8>,
pub certs: Vec<Vec<u8>>,
}
#[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(any(feature = "mesalock_sgx", feature = "libos"))] {
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::test_init_sgx_quote,
platform::tests::test_create_sgx_isv_enclave_report,
platform::tests::test_get_sgx_quote,
report::tests::test_sgx_quote_parse_from,
report::tests::test_attestation_report_from_cert,
report::tests::test_attestation_report_from_cert_api_version_not_compatible
)
}
}