-
Notifications
You must be signed in to change notification settings - Fork 90
/
ibmse.rs
324 lines (280 loc) · 11.1 KB
/
ibmse.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
// Copyright (C) Copyright IBM Corp. 2024
//
// SPDX-License-Identifier: Apache-2.0
//
use crate::TeeEvidenceParsedClaim;
use anyhow::{anyhow, bail, Context, Result};
use core::result::Result::Ok;
use log::{debug, info, warn};
use openssl::encrypt::{Decrypter, Encrypter};
use openssl::pkey::{PKey, Private, Public};
use openssl::rsa::Padding;
use pv::attest::{
AdditionalData, AttestationFlags, AttestationItems, AttestationMeasAlg, AttestationMeasurement,
AttestationRequest, AttestationVersion,
};
use pv::misc::{open_file, read_certs};
use pv::request::{BootHdrTags, CertVerifier, HkdVerifier, ReqEncrCtx, Request, SymKeyType};
use pv::uv::ConfigUid;
use serde::{Deserialize, Serialize};
use serde_with::{base64::Base64, hex::Hex, serde_as};
use std::{env, fs};
const DEFAULT_CERTS_OFFLINE_VERIFICATION: &str = "false";
const DEFAULT_SE_HOST_KEY_DOCUMENTS_ROOT: &str = "/run/confidential-containers/ibmse/hkds";
const DEFAULT_SE_CERTIFICATES_ROOT: &str = "/run/confidential-containers/ibmse/certs";
const DEFAULT_SE_CERTIFICATE_ROOT_CA: &str = "/run/confidential-containers/ibmse/root_ca.crt";
const DEFAULT_SE_CERTIFICATE_REVOCATION_LISTS_ROOT: &str =
"/run/confidential-containers/ibmse/crls";
const DEFAULT_SE_IMAGE_HEADER_FILE: &str = "/run/confidential-containers/ibmse/hdr/hdr.bin";
const DEFAULT_SE_MEASUREMENT_ENCR_KEY_PRIVATE: &str =
"/run/confidential-containers/ibmse/rsa/encrypt_key.pem";
const DEFAULT_SE_MEASUREMENT_ENCR_KEY_PUBLIC: &str =
"/run/confidential-containers/ibmse/rsa/encrypt_key.pub";
macro_rules! env_or_default {
($env:literal, $default:ident) => {
match env::var($env) {
Ok(env_path) => env_path,
Err(_) => $default.into(),
}
};
}
fn list_files_in_folder(dir: &str) -> Result<Vec<String>> {
let mut file_paths = Vec::new();
for entry in fs::read_dir(dir)? {
let path = entry?.path();
if path.is_file() {
if let Some(path_str) = path.to_str() {
file_paths.push(path_str.to_string());
}
}
}
Ok(file_paths)
}
#[repr(C)]
#[serde_as]
#[derive(Debug, Serialize, Deserialize)]
pub struct SeAttestationResponse {
#[serde_as(as = "Base64")]
measurement: Vec<u8>,
#[serde_as(as = "Base64")]
additional_data: Vec<u8>,
#[serde_as(as = "Base64")]
user_data: Vec<u8>,
#[serde_as(as = "Base64")]
cuid: ConfigUid,
#[serde_as(as = "Base64")]
encr_measurement_key: Vec<u8>,
#[serde_as(as = "Base64")]
encr_request_nonce: Vec<u8>,
#[serde_as(as = "Base64")]
image_hdr_tags: BootHdrTags,
}
#[repr(C)]
#[serde_as]
#[derive(Debug, Serialize, Deserialize)]
pub struct SeAttestationClaims {
#[serde_as(as = "Hex")]
cuid: ConfigUid,
user_data: String,
version: u32,
#[serde_as(as = "Hex")]
image_phkh: Vec<u8>,
#[serde_as(as = "Hex")]
attestation_phkh: Vec<u8>,
#[serde_as(as = "Hex")]
tag: [u8; 16],
}
#[repr(C)]
#[serde_as]
#[derive(Debug, Serialize, Deserialize)]
pub struct SeAttestationRequest {
#[serde_as(as = "Base64")]
request_blob: Vec<u8>,
measurement_size: u32,
additional_size: u32,
#[serde_as(as = "Base64")]
encr_measurement_key: Vec<u8>,
#[serde_as(as = "Base64")]
encr_request_nonce: Vec<u8>,
#[serde_as(as = "Base64")]
image_hdr_tags: BootHdrTags,
}
#[derive(Debug)]
pub struct SeVerifierImpl {
private_key: PKey<Private>,
public_key: PKey<Public>,
}
impl SeVerifierImpl {
pub fn new() -> Result<Self> {
let pri_key_file = env_or_default!(
"SE_MEASUREMENT_ENCR_KEY_PRIVATE",
DEFAULT_SE_MEASUREMENT_ENCR_KEY_PRIVATE
);
let priv_contents = fs::read(pri_key_file)?;
let private_key = PKey::private_key_from_pem(&priv_contents)?;
let pub_key_file = env_or_default!(
"SE_MEASUREMENT_ENCR_KEY_PUBLIC",
DEFAULT_SE_MEASUREMENT_ENCR_KEY_PUBLIC
);
let pub_contents = fs::read(pub_key_file)?;
let public_key = PKey::public_key_from_pem(&pub_contents)?;
Ok(Self {
private_key,
public_key,
})
}
fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>> {
let mut decrypter = Decrypter::new(&self.private_key)?;
decrypter.set_rsa_padding(Padding::PKCS1)?;
let buffer_len = decrypter.decrypt_len(ciphertext)?;
let mut decrypted = vec![0; buffer_len];
let decrypted_len = decrypter.decrypt(ciphertext, &mut decrypted)?;
decrypted.truncate(decrypted_len);
Ok(decrypted)
}
fn encrypt(&self, text: &[u8]) -> Result<Vec<u8>> {
let mut encrypter = Encrypter::new(&self.public_key)?;
encrypter.set_rsa_padding(Padding::PKCS1)?;
let buffer_len = encrypter.encrypt_len(text)?;
let mut encrypted = vec![0; buffer_len];
let len = encrypter.encrypt(text, &mut encrypted)?;
encrypted.truncate(len);
Ok(encrypted)
}
pub fn evaluate(&self, evidence: &[u8]) -> Result<TeeEvidenceParsedClaim> {
info!("IBM SE verify API called.");
// evidence is serialized SeAttestationResponse String bytes
let se_response: SeAttestationResponse = serde_json::from_slice(evidence)?;
let meas_key = self
.decrypt(&se_response.encr_measurement_key)
.context("decrypt Measurement Key")?;
let nonce = self
.decrypt(&se_response.encr_request_nonce)
.context("decrypt Request Nonce")?;
let nonce_array: [u8; 16] = nonce
.try_into()
.map_err(|_| anyhow!("Failed to convert nonce from Vec<u8> to [u8; 16], It must have exactly 16 elements."))?;
let meas_key = PKey::hmac(&meas_key)?;
let items = AttestationItems::new(
&se_response.image_hdr_tags,
&se_response.cuid,
Some(&se_response.user_data),
Some(&nonce_array),
Some(&se_response.additional_data),
);
let measurement =
AttestationMeasurement::calculate(items, AttestationMeasAlg::HmacSha512, &meas_key)?;
if !measurement.eq_secure(&se_response.measurement) {
debug!("Recieved: {:?}", se_response.measurement);
debug!("Calculated: {:?}", measurement.as_ref());
bail!("Failed to verify the measurement!");
}
let mut att_flags = AttestationFlags::default();
att_flags.set_image_phkh();
att_flags.set_attest_phkh();
let add_data = AdditionalData::from_slice(&se_response.additional_data, &att_flags)?;
debug!("additional_data: {:?}", add_data);
let image_phkh = add_data
.image_public_host_key_hash()
.ok_or(anyhow!("Failed to get image_public_host_key_hash."))?;
let attestation_phkh = add_data
.attestation_public_host_key_hash()
.ok_or(anyhow!("Failed to get attestation_public_host_key_hash."))?;
let claims = SeAttestationClaims {
cuid: se_response.cuid,
user_data: String::from_utf8(se_response.user_data.clone())?,
version: AttestationVersion::One as u32,
image_phkh: image_phkh.to_vec(),
attestation_phkh: attestation_phkh.to_vec(),
tag: *se_response.image_hdr_tags.tag(),
};
serde_json::to_value(claims).context("build json value from the se claims")
}
pub async fn generate_supplemental_challenge(&self, _tee_parameters: String) -> Result<String> {
let se_certificate_root =
env_or_default!("SE_CERTIFICATES_ROOT", DEFAULT_SE_CERTIFICATES_ROOT);
let ca_certs = list_files_in_folder(&se_certificate_root)?;
let crl_root = env_or_default!(
"SE_CERTIFICATE_REVOCATION_LISTS_ROOT",
DEFAULT_SE_CERTIFICATE_REVOCATION_LISTS_ROOT
);
let crls = list_files_in_folder(&crl_root)?;
let root_ca_path =
env_or_default!("SE_CERTIFICATE_ROOT_CA", DEFAULT_SE_CERTIFICATE_ROOT_CA);
let ca_option: Option<String> = if std::path::Path::new(&root_ca_path).exists() {
Some(root_ca_path)
} else {
None::<String>
};
let offline_certs_verify = env_or_default!(
"CERTS_OFFLINE_VERIFICATION",
DEFAULT_CERTS_OFFLINE_VERIFICATION
);
let offline_certs_verify: bool = offline_certs_verify.parse::<bool>().unwrap_or(false);
let mut attestation_flags = AttestationFlags::default();
attestation_flags.set_image_phkh();
attestation_flags.set_attest_phkh();
let mut arcb = AttestationRequest::new(
AttestationVersion::One,
AttestationMeasAlg::HmacSha512,
attestation_flags,
)?;
let hkds_root = env_or_default!(
"DEFAULT_SE_HOST_KEY_DOCUMENTS_ROOT",
DEFAULT_SE_HOST_KEY_DOCUMENTS_ROOT
);
let hkds = list_files_in_folder(&hkds_root)?;
for hkd in &hkds {
let hk = std::fs::read(hkd).context("read host-key document")?;
let certs = read_certs(&hk)?;
if certs.is_empty() {
warn!("The host key document in '{hkd}' contains empty certificate!");
}
if certs.len() != 1 {
warn!("The host key document in '{hkd}' contains more than one certificate!")
}
let c = certs
.first()
.ok_or(anyhow!("File does not contain a X509 certificate"))?;
const DEFAULT_SE_SKIP_CERTS_VERIFICATION: &str = "false";
let skip_certs_env = env_or_default!(
"SE_SKIP_CERTS_VERIFICATION",
DEFAULT_SE_SKIP_CERTS_VERIFICATION
);
let skip_certs: bool = skip_certs_env.parse::<bool>().unwrap_or(false);
if !skip_certs {
let verifier = CertVerifier::new(
ca_certs.as_slice(),
crls.as_slice(),
ca_option.clone(),
offline_certs_verify,
)?;
verifier.verify(c)?;
}
arcb.add_hostkey(c.public_key()?);
}
let encr_ctx = ReqEncrCtx::random(SymKeyType::Aes256)?;
let request_blob = arcb.encrypt(&encr_ctx)?;
let conf_data = arcb.confidential_data();
let encr_measurement_key = self.encrypt(conf_data.measurement_key())?;
let nonce = conf_data
.nonce()
.as_ref()
.ok_or(anyhow!("Failed to get nonce binding"))?
.value();
let encr_request_nonce = self.encrypt(nonce)?;
let se_img_hdr = env_or_default!("SE_IMAGE_HEADER_FILE", DEFAULT_SE_IMAGE_HEADER_FILE);
let mut hdr_file = open_file(se_img_hdr)?;
let image_hdr_tags = BootHdrTags::from_se_image(&mut hdr_file)?;
let se_attestation_request = SeAttestationRequest {
request_blob,
measurement_size: AttestationMeasAlg::HmacSha512.exp_size(),
additional_size: arcb.flags().expected_additional_size(),
encr_measurement_key,
encr_request_nonce,
image_hdr_tags,
};
let challenge = serde_json::to_string(&se_attestation_request)?;
Ok(challenge)
}
}