Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/nsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ impl SecureModule {
Self::parse_raw_attestation_doc(&document)
}

fn parse_raw_attestation_doc(document: &[u8]) -> Result<AttestationDoc, AttestationError> {
/// Parse a raw attestation document into an `AttestationDoc`.
///
/// # Errors
/// Returns an error if the document cannot be decoded.
pub fn parse_raw_attestation_doc(document: &[u8]) -> Result<AttestationDoc, AttestationError> {
let cose_document = CoseSign1::from_bytes(document).map_err(AttestationError::Cose)?;

let cbor_attestation_doc = cose_document
Expand Down Expand Up @@ -148,6 +152,19 @@ impl SecureModule {
Self::try_global().expect("NSM global not initialized")
}

/// Attempts to get global NSM instance, initializing it if necessary.
///
/// # Errors
///
/// Propagates `io::Error` if the connection to the NSM fails.
pub async fn try_init_global() -> io::Result<&'static Self> {
let nsm = Self::connect()?;

let secure_module = SECURE_MODULE_GLOBAL.get_or_init(|| async { nsm }).await;

Ok(secure_module)
}

/// Disconnect from the NSM driver.
pub fn disconnect(self) {
drop(self);
Expand Down
13 changes: 3 additions & 10 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,9 @@ where
// Initialize the secure module global if the feature is enabled.
#[cfg(feature = "nsm")]
{
match crate::SecureModule::connect() {
Ok(nsm) => {
crate::nsm::SECURE_MODULE_GLOBAL
.get_or_init(|| async { nsm })
.await
},
Err(e) => {
return Err(Error::NsmConnect(e));
},
};
crate::nsm::SecureModule::try_init_global()
.await
.map_err(Error::NsmConnect)?;
}

let router = Arc::new(self);
Expand Down