Skip to content

Commit

Permalink
fix: change traits to use Box<dyn Any>
Browse files Browse the repository at this point in the history
When using Box<dyn ProviderConfig>, you would have to upcast the dyn ProviderConfig to dyn Any. This feature is currently experimental (rust-lang/rust#65991). As ProviderConfig is always going to be turned into an Any, it has no real purpose anyways.
BREAKING
  • Loading branch information
mark-beck committed May 27, 2024
1 parent ae1b69d commit 9ceffcd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
17 changes: 5 additions & 12 deletions src/common/traits/module_provider.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{key_handle::KeyHandle, module_provider_config::ProviderConfig};
use super::key_handle::KeyHandle;
use crate::common::error::SecurityModuleError;
use std::fmt::Debug;
use std::{any::Any, fmt::Debug};

/// Defines the interface for a security module provider.
///
Expand All @@ -26,11 +26,8 @@ pub trait Provider: Send + Sync + KeyHandle + Debug {
///
/// A `Result` that, on success, contains `Ok(())`, indicating that the key was created successfully.
/// On failure, it returns a `SecurityModuleError`.
fn create_key(
&mut self,
key_id: &str,
config: Box<dyn ProviderConfig>,
) -> Result<(), SecurityModuleError>;
fn create_key(&mut self, key_id: &str, config: Box<dyn Any>)
-> Result<(), SecurityModuleError>;

/// Loads an existing cryptographic key identified by `key_id`.
///
Expand All @@ -46,11 +43,7 @@ pub trait Provider: Send + Sync + KeyHandle + Debug {
///
/// A `Result` that, on success, contains `Ok(())`, indicating that the key was loaded successfully.
/// On failure, it returns a `SecurityModuleError`.
fn load_key(
&mut self,
key_id: &str,
config: Box<dyn ProviderConfig>,
) -> Result<(), SecurityModuleError>;
fn load_key(&mut self, key_id: &str, config: Box<dyn Any>) -> Result<(), SecurityModuleError>;

/// Initializes the security module and returns a handle for further operations.
///
Expand Down
12 changes: 4 additions & 8 deletions src/tpm/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ impl Provider for AndroidProvider {
fn create_key(
&mut self,
key_id: &str,
config: Box<dyn ProviderConfig>,
config: Box<dyn Any>,
) -> Result<(), SecurityModuleError> {
info!("generating key! {}", key_id);

// load config
let config = *(Box::new(config) as Box<dyn Any>)
let config = *config
.downcast::<AndroidConfig>()
.map_err(|_| SecurityModuleError::InitializationError("Wrong Config".to_owned()))?;
self.apply_config(config)?;
Expand Down Expand Up @@ -192,15 +192,11 @@ impl Provider for AndroidProvider {
///
/// Returns `Ok(())` if the key loading is successful, otherwise returns an error of type `SecurityModuleError`.
#[instrument]
fn load_key(
&mut self,
key_id: &str,
config: Box<dyn ProviderConfig>,
) -> Result<(), SecurityModuleError> {
fn load_key(&mut self, key_id: &str, config: Box<dyn Any>) -> Result<(), SecurityModuleError> {
self.key_id = key_id.to_owned();

// load config
let config = *(Box::new(config) as Box<dyn Any>)
let config = *config
.downcast::<AndroidConfig>()
.map_err(|_| SecurityModuleError::InitializationError("Wrong Config".to_owned()))?;
self.apply_config(config)?;
Expand Down

0 comments on commit 9ceffcd

Please sign in to comment.