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
use anyhow::anyhow;
use anyhow::Result;
use std::process;
use structopt::StructOpt;
use teaclave_binder::proto::{ECallCommand, RawJsonInput, RawJsonOutput};
use teaclave_binder::TeeBinder;
use teaclave_types::TeeServiceResult;
fn attestation(opt: &AttestationOpt) -> anyhow::Result<()> {
env_logger::init_from_env(
env_logger::Env::new()
.filter_or("TEACLAVE_LOG", "RUST_LOG")
.write_style_or("TEACLAVE_LOG_STYLE", "RUST_LOG_STYLE"),
);
let tee = TeeBinder::new(env!("CARGO_PKG_NAME"))?;
run(&tee, opt)?;
tee.finalize();
Ok(())
}
fn start_enclave_remote_attestation(tee: &TeeBinder, opt: &AttestationOpt) -> anyhow::Result<()> {
let cmd = ECallCommand::Raw;
let json = serde_json::to_string(opt)?;
let input = RawJsonInput::new(json);
match tee.invoke::<RawJsonInput, TeeServiceResult<RawJsonOutput>>(cmd, input) {
Err(e) => Err(anyhow!("{:?}", e)),
Ok(Err(e)) => Err(anyhow!("{:?}", e)),
_ => Ok(()),
}
}
fn run(tee: &TeeBinder, opt: &AttestationOpt) -> anyhow::Result<()> {
start_enclave_remote_attestation(tee, opt)?;
Ok(())
}
#[derive(Debug, StructOpt)]
#[structopt(name = "teaclave_sgx_tool", about = "Teaclave SGX tool.")]
struct Opt {
#[structopt(subcommand)]
command: Command,
}
#[derive(Debug, StructOpt, serde::Serialize)]
struct AttestationOpt {
#[structopt(long, default_value = "sgx_epid")]
algorithm: String,
#[structopt(long, default_value = "https://api.trustedservices.intel.com:443")]
url: String,
#[structopt(long, default_value = "00000000000000000000000000000000")]
key: String,
#[structopt(long, default_value = "00000000000000000000000000000000")]
spid: String,
}
#[derive(Debug, StructOpt)]
enum Command {
#[structopt(name = "status")]
Status,
#[structopt(name = "attestation")]
Attestation(AttestationOpt),
}
fn status() {
let cpuid = raw_cpuid::CpuId::new();
println!(
"Vendor: {}",
cpuid
.get_vendor_info()
.as_ref()
.map_or_else(|| "unknown", |vf| vf.as_str(),)
);
println!(
"CPU Model: {:?}",
cpuid
.get_processor_brand_string()
.as_ref()
.map_or("n/a", |s| s.as_str())
);
println!("SGX: ");
println!(
" Has SGX: {}",
cpuid
.get_extended_feature_info()
.as_ref()
.map_or_else(|| "n/a".to_string(), |ext| ext.has_sgx().to_string(),)
);
let sgx_info = cpuid.get_sgx_info();
match sgx_info {
Some(sgx_info) => {
println!(" Has SGX1: {}", sgx_info.has_sgx1());
println!(" Has SGX2: {}", sgx_info.has_sgx2());
println!(" Supports ENCLV instruction leaves EINCVIRTCHILD, EDECVIRTCHILD, and ESETCONTEXT: {}", sgx_info.has_enclv_leaves_einvirtchild_edecvirtchild_esetcontext());
println!(
" Supports ENCLS instruction leaves ETRACKC, ERDINFO, ELDBC, and ELDUC: {}",
sgx_info.has_encls_leaves_etrackc_erdinfo_eldbc_elduc()
);
println!(
" Bit vector of supported extended SGX features: {:#010X}",
sgx_info.miscselect()
);
println!(
" Maximum supported enclave size in non-64-bit mode: 2^{}",
sgx_info.max_enclave_size_non_64bit()
);
println!(
" Maximum supported enclave size in 64-bit mode: 2^{}",
sgx_info.max_enclave_size_64bit()
);
println!(" Bits of SECS.ATTRIBUTES[127:0] set with ECREATE: {:#018X} (lower) {:#018X} (upper)", sgx_info.secs_attributes().0, sgx_info.secs_attributes().1);
for i in sgx_info.iter() {
match i {
raw_cpuid::SgxSectionInfo::Epc(epc) => {
println!(" EPC physical base: {:#018X}", epc.physical_base());
println!(
" EPC size: {:#018X} ({}M)",
epc.size(),
epc.size() / 1024 / 1024
);
}
}
}
}
None => println!(" Intel SGX: n/a"),
}
println!(
" Supports flexible launch control: {}",
cpuid
.get_extended_feature_info()
.as_ref()
.map_or_else(|| "n/a".to_string(), |ext| ext.has_sgx_lc().to_string(),)
);
println!(
" SGX device: /dev/sgx {}, /dev/isgx {}",
std::path::Path::new("/dev/sgx").exists(),
std::path::Path::new("/dev/isgx").exists()
);
println!(
" AESM service: {}",
std::path::Path::new("/var/run/aesmd/aesm.socket").exists()
);
for module in &["isgx", "sgx", "intel_sgx"] {
println!("\nKernel module ({}):", module);
if process::Command::new("modinfo")
.arg(module)
.status()
.is_err()
{
println!("failed to execute modinfo {}", module);
}
}
}
fn main() -> Result<()> {
let args = Opt::from_args();
match args.command {
Command::Status => status(),
Command::Attestation(opt) => attestation(&opt)?,
};
Ok(())
}