|
| 1 | +// SPDX-FileCopyrightText: 2022 Profian Inc. <[email protected]> |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 3 | + |
| 4 | +use serde::{Deserialize, Deserializer}; |
| 5 | +use sgx::parameters::{Features, MiscSelect}; |
| 6 | +use validation_common::Measurements; |
| 7 | + |
| 8 | +#[derive(Clone, Deserialize, Debug, Eq, PartialEq)] |
| 9 | +pub enum SgxFeatures { |
| 10 | + CET, |
| 11 | + Debug, |
| 12 | + EInitKey, |
| 13 | + KSS, |
| 14 | + ProvisioningKey, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Clone, Deserialize, Debug, Eq, PartialEq)] |
| 18 | +pub enum SgxMiscSelect { |
| 19 | + EXINFO, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Clone, Deserialize, Debug, Default, Eq, PartialEq)] |
| 23 | +pub struct Config { |
| 24 | + /// Values for `mrsigner` in the report body, as `Measurements::signer()` |
| 25 | + /// This is the list of public keys which have signed the Enarx binary. |
| 26 | + /// Values for `mrenclave` in the report body, as `Measurements::hash()` |
| 27 | + /// This is the hash of the Enclave environment after the Enarx binary is loaded but |
| 28 | + /// before any workload is loaded, so this is a hash of the Enarx binary in memory. |
| 29 | + #[serde(default, flatten)] |
| 30 | + pub measurements: Measurements<32>, |
| 31 | + |
| 32 | + /// Values for `features`. |
| 33 | + /// Checked against `sgx::parameters::attributes::Attributes::features()` |
| 34 | + /// These are CPU features reported by the CPU firmware, and most features are not |
| 35 | + /// relevant to security concerns, but are used for code execution. Only the features |
| 36 | + /// relevant for security are parsed here. |
| 37 | + #[serde(default)] |
| 38 | + #[serde(deserialize_with = "from_features")] |
| 39 | + pub features: Features, |
| 40 | + |
| 41 | + /// Minimum value for `isv_svn`. |
| 42 | + /// Checked against `sgx::report::ReportBody::enclave_security_version()` |
| 43 | + /// This is the security version of the enclave. |
| 44 | + pub enclave_security_version: Option<u16>, |
| 45 | + |
| 46 | + /// Value for `isv_prodid`, do not allow other ids. |
| 47 | + /// Checked against `sgx::report::ReportBody::enclave_product_id()` |
| 48 | + pub enclave_product_id: Option<u16>, |
| 49 | + |
| 50 | + /// Extra enclave creation parameters |
| 51 | + /// Checked against `sgx::report::ReportBody::misc_select()` |
| 52 | + #[serde(default)] |
| 53 | + #[serde(deserialize_with = "from_misc_select")] |
| 54 | + pub misc_select: MiscSelect, |
| 55 | +} |
| 56 | + |
| 57 | +fn from_features<'de, D>(deserializer: D) -> Result<Features, D::Error> |
| 58 | +where |
| 59 | + D: Deserializer<'de>, |
| 60 | +{ |
| 61 | + let s: Vec<SgxFeatures> = Deserialize::deserialize(deserializer)?; |
| 62 | + |
| 63 | + let mut flags = Features::empty(); |
| 64 | + |
| 65 | + // Must be set according to Intel SGX documentation, this indicates permission |
| 66 | + // to create SGX enclaves. |
| 67 | + flags |= Features::INIT; |
| 68 | + |
| 69 | + // Required by Enarx, as Wasmtime requires 64-bit, and modern systems are all 64-bit anyway |
| 70 | + flags |= Features::MODE64BIT; |
| 71 | + |
| 72 | + for flag in s { |
| 73 | + match flag { |
| 74 | + SgxFeatures::CET => { |
| 75 | + flags |= Features::CET; |
| 76 | + } |
| 77 | + SgxFeatures::Debug => { |
| 78 | + flags |= Features::DEBUG; |
| 79 | + } |
| 80 | + SgxFeatures::EInitKey => { |
| 81 | + flags |= Features::EINIT_KEY; |
| 82 | + } |
| 83 | + SgxFeatures::KSS => { |
| 84 | + flags |= Features::KSS; |
| 85 | + } |
| 86 | + SgxFeatures::ProvisioningKey => { |
| 87 | + flags |= Features::PROVISIONING_KEY; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Ok(flags) |
| 93 | +} |
| 94 | + |
| 95 | +fn from_misc_select<'de, D>(deserializer: D) -> Result<MiscSelect, D::Error> |
| 96 | +where |
| 97 | + D: Deserializer<'de>, |
| 98 | +{ |
| 99 | + let s: Vec<SgxMiscSelect> = Deserialize::deserialize(deserializer)?; |
| 100 | + |
| 101 | + let mut flags = MiscSelect::default(); |
| 102 | + |
| 103 | + for flag in s { |
| 104 | + match flag { |
| 105 | + SgxMiscSelect::EXINFO => { |
| 106 | + flags |= MiscSelect::EXINFO; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + Ok(flags) |
| 112 | +} |
| 113 | + |
| 114 | +#[cfg(test)] |
| 115 | +mod tests { |
| 116 | + use super::*; |
| 117 | + use std::collections::HashSet; |
| 118 | + use validation_common::Digest; |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn empty_config() { |
| 122 | + assert!(toml::from_str::<Config>("").is_err()); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn serde() { |
| 127 | + const SIGNER: &str = |
| 128 | + r#"signer = ["2eba0f494f428e799c22d6f12778aebea4dc8d991f9e63fd3cddd57ac6eb5dd9"]"#; |
| 129 | + |
| 130 | + let signer: HashSet<_> = vec![Digest([ |
| 131 | + 0x2e, 0xba, 0x0f, 0x49, 0x4f, 0x42, 0x8e, 0x79, 0x9c, 0x22, 0xd6, 0xf1, 0x27, 0x78, |
| 132 | + 0xae, 0xbe, 0xa4, 0xdc, 0x8d, 0x99, 0x1f, 0x9e, 0x63, 0xfd, 0x3c, 0xdd, 0xd5, 0x7a, |
| 133 | + 0xc6, 0xeb, 0x5d, 0xd9, |
| 134 | + ])] |
| 135 | + .into_iter() |
| 136 | + .collect(); |
| 137 | + |
| 138 | + let config: Config = toml::from_str(&format!( |
| 139 | + r#" |
| 140 | +{SIGNER} |
| 141 | +features = ["Debug"] |
| 142 | +misc_select = ["EXINFO"] |
| 143 | +"#, |
| 144 | + )) |
| 145 | + .expect("Couldn't deserialize"); |
| 146 | + |
| 147 | + assert_eq!(config.measurements.signer, signer); |
| 148 | + assert_eq!( |
| 149 | + config.features.bits(), |
| 150 | + (Features::DEBUG | Features::INIT | Features::MODE64BIT).bits() |
| 151 | + ); |
| 152 | + assert!(config.misc_select.contains(MiscSelect::EXINFO)); |
| 153 | + } |
| 154 | + |
| 155 | + #[test] |
| 156 | + fn too_short() { |
| 157 | + let config: Result<Config, toml::de::Error> = toml::from_str( |
| 158 | + r#" |
| 159 | + signer = ["41c179d5c0d5bc4915752ccf9bbd2baa574716832235ef5bb998fadcda1e46"] |
| 160 | + "#, |
| 161 | + ); |
| 162 | + assert!(config.is_err()); |
| 163 | + } |
| 164 | +} |
0 commit comments