-
Notifications
You must be signed in to change notification settings - Fork 46
Allow setting a base path for shielding-key file IO #1318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
8550d7d
560c787
2906f8f
e9dbe6e
177503d
5645420
0c1d6b9
8ea4fff
b229b3e
c0c59e3
1a9e09b
d09a37c
4350094
7b6ee1d
562cd68
75d027a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ use crate::sgx_reexport_prelude::*; | |
| use crate::{ | ||
| error::{Error, Result}, | ||
| traits::{ShieldingCryptoDecrypt, ShieldingCryptoEncrypt}, | ||
| ToPubkey, | ||
| }; | ||
| use sgx_crypto_helper::{ | ||
| rsa3072::{Rsa3072KeyPair, Rsa3072PubKey}, | ||
|
|
@@ -64,56 +65,88 @@ impl ShieldingCryptoEncrypt for Rsa3072PubKey { | |
| } | ||
| } | ||
|
|
||
| impl ToPubkey for Rsa3072KeyPair { | ||
| type Error = Error; | ||
| type Pubkey = Rsa3072PubKey; | ||
|
|
||
| fn pubkey(&self) -> Result<Self::Pubkey> { | ||
| self.export_pubkey().map_err(|e| Error::Other(format!("{:?}", e).into())) | ||
coax1d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| pub trait RsaSealing { | ||
| fn unseal_pubkey(&self) -> Result<Rsa3072PubKey>; | ||
|
|
||
| fn unseal_pair(&self) -> Result<Rsa3072KeyPair>; | ||
|
|
||
| fn exists(&self) -> bool; | ||
|
|
||
| fn create_sealed_if_absent(&self) -> Result<()>; | ||
|
|
||
| fn create_sealed(&self) -> Result<()>; | ||
| } | ||
|
|
||
| #[cfg(feature = "sgx")] | ||
| pub mod sgx { | ||
| use super::*; | ||
| use derive_more::Display; | ||
| use crate::key_repository::KeyRepository; | ||
| use itp_settings::files::RSA3072_SEALED_KEY_FILE; | ||
| use itp_sgx_io::{seal, unseal, SealedIO, StaticSealedIO}; | ||
| use itp_sgx_io::{seal, unseal, SealedIO}; | ||
| use log::*; | ||
| use std::sgxfs::SgxFile; | ||
| use std::{path::PathBuf, sgxfs::SgxFile}; | ||
|
|
||
| /// Gets a repository for an Rsa3072 keypair and initializes | ||
| /// a fresh key pair if it doesn't exist at `path`. | ||
| pub fn get_rsa3072_repository( | ||
| path: PathBuf, | ||
| ) -> Result<KeyRepository<Rsa3072KeyPair, Rsa3072Seal>> { | ||
| let rsa_seal = Rsa3072Seal::new(path); | ||
| rsa_seal.create_sealed_if_absent()?; | ||
| let shielding_key = rsa_seal.unseal_pair()?; | ||
| Ok(KeyRepository::new(shielding_key, rsa_seal.into())) | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct Rsa3072Seal { | ||
| base_path: PathBuf, | ||
| } | ||
|
|
||
| impl Rsa3072Seal { | ||
| pub fn unseal_pubkey() -> Result<Rsa3072PubKey> { | ||
| let pair = Self::unseal_from_static_file()?; | ||
| let pubkey = | ||
| pair.export_pubkey().map_err(|e| Error::Other(format!("{:?}", e).into()))?; | ||
| Ok(pubkey) | ||
| pub fn new(base_path: PathBuf) -> Self { | ||
| Self { base_path } | ||
| } | ||
| } | ||
|
|
||
| pub fn create_sealed_if_absent() -> Result<()> { | ||
| if SgxFile::open(RSA3072_SEALED_KEY_FILE).is_err() { | ||
| info!("[Enclave] Keyfile not found, creating new! {}", RSA3072_SEALED_KEY_FILE); | ||
| return create_sealed() | ||
| pub fn path(&self) -> PathBuf { | ||
| self.base_path.join(RSA3072_SEALED_KEY_FILE) | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn create_sealed() -> Result<()> { | ||
| let rsa_keypair = | ||
| Rsa3072KeyPair::new().map_err(|e| Error::Other(format!("{:?}", e).into()))?; | ||
| // println!("[Enclave] generated RSA3072 key pair. Cleartext: {}", rsa_key_json); | ||
| Rsa3072Seal::seal_to_static_file(&rsa_keypair) | ||
| } | ||
| impl RsaSealing for Rsa3072Seal { | ||
| fn unseal_pubkey(&self) -> Result<Rsa3072PubKey> { | ||
| self.unseal()?.pubkey() | ||
| } | ||
|
|
||
| #[derive(Copy, Clone, Debug, Display)] | ||
| pub struct Rsa3072Seal; | ||
| fn unseal_pair(&self) -> Result<Rsa3072KeyPair> { | ||
| self.unseal() | ||
| } | ||
|
|
||
| impl StaticSealedIO for Rsa3072Seal { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the static implementation. |
||
| type Error = Error; | ||
| type Unsealed = Rsa3072KeyPair; | ||
| fn unseal_from_static_file() -> Result<Self::Unsealed> { | ||
| let raw = unseal(RSA3072_SEALED_KEY_FILE)?; | ||
| let key: Rsa3072KeyPair = serde_json::from_slice(&raw) | ||
| .map_err(|e| Error::Other(format!("{:?}", e).into()))?; | ||
| Ok(key.into()) | ||
| fn exists(&self) -> bool { | ||
clangenb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| SgxFile::open(self.path()).is_ok() | ||
| } | ||
|
|
||
| fn seal_to_static_file(unsealed: &Self::Unsealed) -> Result<()> { | ||
| let key_json = serde_json::to_vec(&unsealed) | ||
| .map_err(|e| Error::Other(format!("{:?}", e).into()))?; | ||
| Ok(seal(&key_json, RSA3072_SEALED_KEY_FILE)?) | ||
| fn create_sealed_if_absent(&self) -> Result<()> { | ||
| if !self.exists() { | ||
| info!("Keyfile not found, creating new! {}", self.path().display()); | ||
| return self.create_sealed() | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn create_sealed(&self) -> Result<()> { | ||
| let rsa_keypair = | ||
| Rsa3072KeyPair::new().map_err(|e| Error::Other(format!("{:?}", e).into()))?; | ||
| info!("Generated RSA3072 key pair. PubKey: {:?}", rsa_keypair.pubkey()?); | ||
| self.seal(&rsa_keypair) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -122,11 +155,16 @@ pub mod sgx { | |
| type Unsealed = Rsa3072KeyPair; | ||
|
|
||
| fn unseal(&self) -> Result<Self::Unsealed> { | ||
| Self::unseal_from_static_file() | ||
| let raw = unseal(self.path())?; | ||
| let key: Rsa3072KeyPair = serde_json::from_slice(&raw) | ||
| .map_err(|e| Error::Other(format!("{:?}", e).into()))?; | ||
| Ok(key.into()) | ||
| } | ||
|
|
||
| fn seal(&self, unsealed: &Self::Unsealed) -> Result<()> { | ||
| Self::seal_to_static_file(unsealed) | ||
| let key_json = serde_json::to_vec(&unsealed) | ||
| .map_err(|e| Error::Other(format!("{:?}", e).into()))?; | ||
| Ok(seal(&key_json, self.path())?) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -33,7 +33,8 @@ use crate::{ | |||||
| error::{Error, Result}, | ||||||
| initialization::global_components::{ | ||||||
| GLOBAL_FULL_PARACHAIN_HANDLER_COMPONENT, GLOBAL_FULL_SOLOCHAIN_HANDLER_COMPONENT, | ||||||
| GLOBAL_SIDECHAIN_IMPORT_QUEUE_COMPONENT, GLOBAL_STATE_HANDLER_COMPONENT, | ||||||
| GLOBAL_SHIELDING_KEY_REPOSITORY_COMPONENT, GLOBAL_SIDECHAIN_IMPORT_QUEUE_COMPONENT, | ||||||
| GLOBAL_STATE_HANDLER_COMPONENT, | ||||||
| }, | ||||||
| rpc::worker_api_direct::sidechain_io_handler, | ||||||
| utils::{ | ||||||
|
|
@@ -50,16 +51,17 @@ use itp_import_queue::PushToQueue; | |||||
| use itp_node_api::metadata::NodeMetadata; | ||||||
| use itp_nonce_cache::{MutateNonce, Nonce, GLOBAL_NONCE_CACHE}; | ||||||
| use itp_settings::worker_mode::{ProvideWorkerMode, WorkerMode, WorkerModeProvider}; | ||||||
| use itp_sgx_crypto::{ed25519, Ed25519Seal, Rsa3072Seal}; | ||||||
| use itp_sgx_crypto::{ed25519, key_repository::AccessPubkey, Ed25519Seal}; | ||||||
| use itp_sgx_io::StaticSealedIO; | ||||||
| use itp_storage::{StorageProof, StorageProofChecker}; | ||||||
| use itp_types::{ShardIdentifier, SignedBlock}; | ||||||
| use itp_utils::write_slice_and_whitespace_pad; | ||||||
| use log::*; | ||||||
| use once_cell::sync::OnceCell; | ||||||
| use sgx_types::sgx_status_t; | ||||||
| use sp_core::crypto::Pair; | ||||||
| use sp_runtime::traits::BlakeTwo256; | ||||||
| use std::{boxed::Box, slice, vec::Vec}; | ||||||
| use std::{boxed::Box, path::PathBuf, slice, vec::Vec}; | ||||||
|
|
||||||
| mod attestation; | ||||||
| mod empty_impls; | ||||||
|
|
@@ -83,6 +85,8 @@ pub mod test; | |||||
| pub type Hash = sp_core::H256; | ||||||
| pub type AuthorityPair = sp_core::ed25519::Pair; | ||||||
|
|
||||||
| static BASE_PATH: OnceCell<PathBuf> = OnceCell::new(); | ||||||
|
|
||||||
| /// Initialize the enclave. | ||||||
| #[no_mangle] | ||||||
| pub unsafe extern "C" fn init( | ||||||
|
|
@@ -91,6 +95,18 @@ pub unsafe extern "C" fn init( | |||||
| untrusted_worker_addr: *const u8, | ||||||
| untrusted_worker_addr_size: u32, | ||||||
| ) -> sgx_status_t { | ||||||
| // Initialize the logging environment in the enclave. | ||||||
| env_logger::init(); | ||||||
|
Comment on lines
+98
to
+99
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this form |
||||||
|
|
||||||
| // Todo: This will be changed to be a param of the `init` ecall: | ||||||
| // https://github.com/integritee-network/worker/issues/1292 | ||||||
| // | ||||||
| // Until the above task is finished, we just fall back to the | ||||||
| // static behaviour, which uses the PWD already. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, do you hate the British? ;)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought code was universal American English 👯♂️ |
||||||
| let pwd = std::env::current_dir().expect("Works on all supported platforms; qed"); | ||||||
| info!("Setting base_dir to pwd: {}", pwd.display()); | ||||||
| BASE_PATH.set(pwd.clone()).expect("We only init this once here; qed."); | ||||||
|
|
||||||
| let mu_ra_url = | ||||||
| match String::decode(&mut slice::from_raw_parts(mu_ra_addr, mu_ra_addr_size as usize)) | ||||||
| .map_err(Error::Codec) | ||||||
|
|
@@ -109,7 +125,7 @@ pub unsafe extern "C" fn init( | |||||
| Err(e) => return e.into(), | ||||||
| }; | ||||||
|
|
||||||
| match initialization::init_enclave(mu_ra_url, untrusted_worker_url) { | ||||||
| match initialization::init_enclave(mu_ra_url, untrusted_worker_url, pwd) { | ||||||
| Err(e) => e.into(), | ||||||
| Ok(()) => sgx_status_t::SGX_SUCCESS, | ||||||
| } | ||||||
|
|
@@ -120,7 +136,15 @@ pub unsafe extern "C" fn get_rsa_encryption_pubkey( | |||||
| pubkey: *mut u8, | ||||||
| pubkey_size: u32, | ||||||
| ) -> sgx_status_t { | ||||||
| let rsa_pubkey = match Rsa3072Seal::unseal_pubkey() { | ||||||
| let shielding_key_repository = match GLOBAL_SHIELDING_KEY_REPOSITORY_COMPONENT.get() { | ||||||
| Ok(s) => s, | ||||||
| Err(e) => { | ||||||
| error!("{:?}", e); | ||||||
| return sgx_status_t::SGX_ERROR_UNEXPECTED | ||||||
| }, | ||||||
| }; | ||||||
|
|
||||||
| let rsa_pubkey = match shielding_key_repository.retrieve_pubkey() { | ||||||
| Ok(key) => key, | ||||||
| Err(e) => return e.into(), | ||||||
| }; | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.