-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathcert.rs
405 lines (359 loc) Β· 14.4 KB
/
cert.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#![cfg_attr(not(feature = "SGX_MODE_HW"), allow(unused))]
use bit_vec::BitVec;
use chrono::Utc as TzUtc;
use chrono::{Duration, TimeZone};
#[cfg(feature = "SGX_MODE_HW")]
use log::*;
use num_bigint::BigUint;
use sgx_tcrypto::SgxEccHandle;
use sgx_types::{
sgx_ec256_private_t, sgx_ec256_public_t, sgx_platform_info_t, sgx_status_t,
sgx_update_info_bit_t, SgxResult,
};
use std::io::BufReader;
use std::str;
use std::time::{SystemTime, UNIX_EPOCH};
use std::untrusted::time::SystemTimeEx;
use yasna::models::ObjectIdentifier;
#[cfg(feature = "SGX_MODE_HW")]
use super::attestation::get_mr_enclave;
use crate::consts::CERTEXPIRYDAYS;
#[cfg(feature = "SGX_MODE_HW")]
use crate::consts::{SigningMethod, MRSIGNER, SIGNING_METHOD};
#[cfg(feature = "SGX_MODE_HW")]
use super::report::{AttestationReport, SgxQuoteStatus};
extern "C" {
#[allow(dead_code)]
pub fn ocall_get_update_info(
ret_val: *mut sgx_status_t,
platformBlob: *const sgx_platform_info_t,
enclaveTrusted: i32,
update_info: *mut sgx_update_info_bit_t,
) -> sgx_status_t;
}
pub const IAS_REPORT_CA: &[u8] = include_bytes!("../../Intel_SGX_Attestation_RootCA.pem");
const ISSUER: &str = "SecretTEE";
const SUBJECT: &str = "Secret Network Node Certificate";
pub enum Error {
GenericError,
}
pub fn gen_ecc_cert(
payload: String,
prv_k: &sgx_ec256_private_t,
pub_k: &sgx_ec256_public_t,
ecc_handle: &SgxEccHandle,
) -> SgxResult<(Vec<u8>, Vec<u8>)> {
// Generate public key bytes since both DER will use it
let mut pub_key_bytes: Vec<u8> = vec![4];
let mut pk_gx = pub_k.gx;
pk_gx.reverse();
let mut pk_gy = pub_k.gy;
pk_gy.reverse();
pub_key_bytes.extend_from_slice(&pk_gx);
pub_key_bytes.extend_from_slice(&pk_gy);
// Generate Certificate DER
let cert_der = yasna::construct_der(|writer| {
writer.write_sequence(|writer| {
writer.next().write_sequence(|writer| {
// Certificate Version
writer
.next()
.write_tagged(yasna::Tag::context(0), |writer| {
writer.write_i8(2);
});
// Certificate Serial Number (unused but required)
writer.next().write_u8(1);
// Signature Algorithm: ecdsa-with-SHA256
writer.next().write_sequence(|writer| {
writer
.next()
.write_oid(&ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 4, 3, 2]));
});
// Issuer: CN=MesaTEE (unused but required)
writer.next().write_sequence(|writer| {
writer.next().write_set(|writer| {
writer.next().write_sequence(|writer| {
writer
.next()
.write_oid(&ObjectIdentifier::from_slice(&[2, 5, 4, 3]));
writer.next().write_utf8_string(&ISSUER);
});
});
});
// Validity: Issuing/Expiring Time (unused but required)
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let issue_ts = TzUtc.timestamp(now.as_secs() as i64, 0);
let expire = now + Duration::days(CERTEXPIRYDAYS).to_std().unwrap();
let expire_ts = TzUtc.timestamp(expire.as_secs() as i64, 0);
writer.next().write_sequence(|writer| {
writer
.next()
.write_utctime(&yasna::models::UTCTime::from_datetime(&issue_ts));
writer
.next()
.write_utctime(&yasna::models::UTCTime::from_datetime(&expire_ts));
});
// Subject: CN=MesaTEE (unused but required)
writer.next().write_sequence(|writer| {
writer.next().write_set(|writer| {
writer.next().write_sequence(|writer| {
writer
.next()
.write_oid(&ObjectIdentifier::from_slice(&[2, 5, 4, 3]));
writer.next().write_utf8_string(&SUBJECT);
});
});
});
writer.next().write_sequence(|writer| {
// Public Key Algorithm
writer.next().write_sequence(|writer| {
// id-ecPublicKey
writer
.next()
.write_oid(&ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 2, 1]));
// prime256v1
writer
.next()
.write_oid(&ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 3, 1, 7]));
});
// Public Key
writer
.next()
.write_bitvec(&BitVec::from_bytes(&pub_key_bytes));
});
// Certificate V3 Extension
writer
.next()
.write_tagged(yasna::Tag::context(3), |writer| {
writer.write_sequence(|writer| {
writer.next().write_sequence(|writer| {
writer.next().write_oid(&ObjectIdentifier::from_slice(&[
2, 16, 840, 1, 113_730, 1, 13,
]));
writer.next().write_bytes(&payload.into_bytes());
});
});
});
});
// Signature Algorithm: ecdsa-with-SHA256
writer.next().write_sequence(|writer| {
writer
.next()
.write_oid(&ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 4, 3, 2]));
});
// Signature
let sig = {
let tbs = &writer.buf[4..];
ecc_handle.ecdsa_sign_slice(tbs, &prv_k).unwrap()
};
let sig_der = yasna::construct_der(|writer| {
writer.write_sequence(|writer| {
let mut sig_x = sig.x;
sig_x.reverse();
let mut sig_y = sig.y;
sig_y.reverse();
writer.next().write_biguint(&BigUint::from_slice(&sig_x));
writer.next().write_biguint(&BigUint::from_slice(&sig_y));
});
});
writer.next().write_bitvec(&BitVec::from_bytes(&sig_der));
});
});
// Generate Private Key DER
let key_der = yasna::construct_der(|writer| {
writer.write_sequence(|writer| {
writer.next().write_u8(0);
writer.next().write_sequence(|writer| {
writer
.next()
.write_oid(&ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 2, 1]));
writer
.next()
.write_oid(&ObjectIdentifier::from_slice(&[1, 2, 840, 10045, 3, 1, 7]));
});
let inner_key_der = yasna::construct_der(|writer| {
writer.write_sequence(|writer| {
writer.next().write_u8(1);
let mut prv_k_r = prv_k.r;
prv_k_r.reverse();
writer.next().write_bytes(&prv_k_r);
writer
.next()
.write_tagged(yasna::Tag::context(1), |writer| {
writer.write_bitvec(&BitVec::from_bytes(&pub_key_bytes));
});
});
});
writer.next().write_bytes(&inner_key_der);
});
});
Ok((key_der, cert_der))
}
fn extract_asn1_value(cert: &[u8], oid: &[u8]) -> Result<Vec<u8>, Error> {
let mut offset = match cert.windows(oid.len()).position(|window| window == oid) {
Some(size) => size,
None => {
return Err(Error::GenericError);
}
};
offset += 12; // 11 + TAG (0x04)
if offset + 2 >= cert.len() {
return Err(Error::GenericError);
}
// Obtain Netscape Comment length
let mut len = cert[offset] as usize;
if len > 0x80 {
len = (cert[offset + 1] as usize) * 0x100 + (cert[offset + 2] as usize);
offset += 2;
}
// Obtain Netscape Comment
offset += 1;
if offset + len >= cert.len() {
return Err(Error::GenericError);
}
let payload = cert[offset..offset + len].to_vec();
Ok(payload)
}
pub fn get_netscape_comment(cert_der: &[u8]) -> Result<Vec<u8>, Error> {
// Search for Netscape Comment OID
let ns_cmt_oid = &[
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x42, 0x01, 0x0D,
];
extract_asn1_value(cert_der, ns_cmt_oid)
}
#[allow(dead_code)]
pub fn get_cert_pubkey(cert_der: &[u8]) -> Result<Vec<u8>, Error> {
// Search for Public Key prime256v1 OID
let prime256v1_oid = &[0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07];
extract_asn1_value(cert_der, prime256v1_oid)
}
pub fn get_ias_auth_config() -> (Vec<u8>, rustls::RootCertStore) {
// Verify if the signing cert is issued by Intel CA
let mut ias_ca_stripped = IAS_REPORT_CA.to_vec();
ias_ca_stripped.retain(|&x| x != 0x0d && x != 0x0a);
let head_len = "-----BEGIN CERTIFICATE-----".len();
let tail_len = "-----END CERTIFICATE-----".len();
let full_len = ias_ca_stripped.len();
let ias_ca_core: &[u8] = &ias_ca_stripped[head_len..full_len - tail_len];
let ias_cert_dec = base64::decode_config(ias_ca_core, base64::STANDARD).unwrap();
let mut ca_reader = BufReader::new(&IAS_REPORT_CA[..]);
let mut root_store = rustls::RootCertStore::empty();
root_store
.add_pem_file(&mut ca_reader)
.expect("Failed to add CA");
(ias_cert_dec, root_store)
}
#[cfg(not(feature = "SGX_MODE_HW"))]
pub fn verify_ra_cert(cert_der: &[u8]) -> SgxResult<Vec<u8>> {
let payload =
get_netscape_comment(cert_der).map_err(|_err| sgx_status_t::SGX_ERROR_UNEXPECTED)?;
let pk = base64::decode(&payload).unwrap();
Ok(pk)
}
/// # Verifies remote attestation cert
///
/// Logic:
/// 1. Extract public key
/// 2. Extract netscape comment - where the attestation report is located
/// 3. Parse the report itself (verify it is signed by intel)
/// 4. Extract public key from report body
/// 5. Verify enclave signature (mr enclave/signer)
///
#[cfg(feature = "SGX_MODE_HW")]
pub fn verify_ra_cert(cert_der: &[u8]) -> SgxResult<Vec<u8>> {
// Before we reach here, Webpki already verifed the cert is properly signed
let report =
AttestationReport::from_cert(cert_der).map_err(|_| sgx_status_t::SGX_ERROR_UNEXPECTED)?;
// 2. Verify quote status (mandatory field)
verify_quote_status(report.sgx_quote_status)?;
// verify certificate
match SIGNING_METHOD {
SigningMethod::MRENCLAVE => {
let this_mr_enclave = match get_mr_enclave() {
Ok(r) => r,
Err(_) => {
return Err(sgx_status_t::SGX_ERROR_UNEXPECTED);
}
};
if report.sgx_quote_body.isv_enclave_report.mr_enclave != this_mr_enclave {
error!("Got a different mr_enclave than expected. Invalid certificate");
debug!(
"received: {:?} \n expected: {:?}",
report.sgx_quote_body.isv_enclave_report.mr_enclave, this_mr_enclave
);
return Err(sgx_status_t::SGX_ERROR_UNEXPECTED);
}
}
SigningMethod::MRSIGNER => {
if report.sgx_quote_body.isv_enclave_report.mr_signer != MRSIGNER {
error!("Got a different mrsigner than expected. Invalid certificate");
debug!(
"received: {:?} \n expected: {:?}",
report.sgx_quote_body.isv_enclave_report.mr_signer, MRSIGNER
);
return Err(sgx_status_t::SGX_ERROR_UNEXPECTED);
}
}
SigningMethod::NONE => {}
}
let report_public_key = report.sgx_quote_body.isv_enclave_report.report_data[0..32].to_vec();
Ok(report_public_key)
}
#[cfg(all(feature = "SGX_MODE_HW", feature = "production"))]
fn verify_quote_status(quote_status: SgxQuoteStatus) -> Result<(), sgx_status_t> {
match quote_status {
SgxQuoteStatus::OK => Ok(()),
SgxQuoteStatus::SwHardeningNeeded => {
warn!("Attesting enclave is vulnerable, and should be patched");
Ok(())
}
_ => {
error!(
"Invalid attestation quote status - cannot verify remote node: {:?}",
quote_status
);
Err(sgx_status_t::SGX_ERROR_UNEXPECTED)
}
}
}
#[cfg(all(feature = "SGX_MODE_HW", not(feature = "production")))]
fn verify_quote_status(quote_status: SgxQuoteStatus) -> Result<(), sgx_status_t> {
match quote_status {
SgxQuoteStatus::OK => Ok(()),
SgxQuoteStatus::SwHardeningNeeded => {
warn!("Attesting enclave is vulnerable, and should be patched");
Ok(())
}
SgxQuoteStatus::GroupOutOfDate => {
warn!("TCB level of SGX platform service is outdated. You should check for firmware updates");
Ok(())
}
_ => {
error!(
"Invalid attestation quote status - cannot verify remote node: {:?}",
quote_status
);
Err(sgx_status_t::SGX_ERROR_UNEXPECTED)
}
}
}
#[cfg(feature = "test")]
pub mod tests {
use crate::crypto::KeyPair;
use super::sgx_quote_sign_type_t;
use super::verify_ra_cert;
fn test_validate_certificate_valid_sw_mode() {
pub const cert: &[u8] = include_bytes!("../testdata/attestation_cert");
let result = verify_ra_cert(cert);
}
fn test_validate_certificate_valid_signed() {
pub const cert: &[u8] = include_bytes!("../testdata/attestation_cert.der");
let result = verify_ra_cert(cert);
}
fn test_validate_certificate_invalid() {
pub const cert: &[u8] = include_bytes!("../testdata/attestation_cert_invalid");
let result = verify_ra_cert(cert);
}
// we want a weird test because this should never crash
fn test_random_bytes_as_certificate() {}
}