-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8550d7d
[itp-sgx-crypto] add `ToPubkey` and `AccessPubkey` traits
clangenb 560c787
[itp-sgx-crypto] refactor the Rsa3072 stuff to no longer use static f…
clangenb 2906f8f
[itp-sgx-crypto] set-base-path to the PWD
clangenb e9dbe6e
[enclave-runtime] more explanation about using the PWD
clangenb 177503d
[enclave-runtime] add todo for replacing the once-cell.
clangenb 5645420
taplo fmt
clangenb 0c1d6b9
add some doc
clangenb 8ea4fff
typo
clangenb b229b3e
Merge branch 'master' into cl/set-base-path-of-shielding-key
clangenb c0c59e3
[sgx-crypto] log full path instead of just filename.
clangenb 1a9e09b
Merge branch 'master' into cl/set-base-path-of-shielding-key
clangenb d09a37c
[enclave-runtime] add todo for replacing `once_cell`
clangenb 4350094
[itp-sgx-crypto] log the pubKey of the RSA key when it is generated.
clangenb 7b6ee1d
fix clippy
clangenb 562cd68
[itp-sgx-crypto] move the file name of the rsa-key from itp-settings …
clangenb 75d027a
[itp-sgx-crypto] remove unnecessary clone.
clangenb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,86 @@ 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}; | ||
|
|
||
| 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! {}", RSA3072_SEALED_KEY_FILE); | ||
| return self.create_sealed() | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn create_sealed(&self) -> Result<()> { | ||
| let rsa_keypair = | ||
| Rsa3072KeyPair::new().map_err(|e| Error::Other(format!("{:?}", e).into()))?; | ||
| // println!("[Enclave] generated RSA3072 key pair. Cleartext: {}", rsa_key_json); | ||
OverOrion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self.seal(&rsa_keypair) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -122,11 +153,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())?) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,15 +51,16 @@ use itp_component_container::ComponentGetter; | |
| 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; | ||
| 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 std::{boxed::Box, slice, vec::Vec}; | ||
| use std::{boxed::Box, path::PathBuf, slice, vec::Vec}; | ||
|
|
||
| mod attestation; | ||
| mod empty_impls; | ||
|
|
@@ -82,6 +84,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( | ||
|
|
@@ -90,6 +94,15 @@ 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 | ||
| 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) | ||
|
|
@@ -108,7 +121,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, | ||
| } | ||
|
|
@@ -119,7 +132,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(), | ||
| }; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.