Skip to content

Commit

Permalink
Add Parsec Basic Client to Parsec Provider Context
Browse files Browse the repository at this point in the history
Create a Parsec Provider context that contains a Parsec Basic Client.
This client will be used for any operation requests coming from the
Parsec Provider, so that it gets redirected to the Parsec Service.

Signed-off-by: Tomás González <[email protected]>
  • Loading branch information
tgonzalezorlandoarm committed Mar 4, 2024
1 parent ec17350 commit afe6782
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
8 changes: 3 additions & 5 deletions parsec-openssl-provider-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// Copyright 2023 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0

use parsec_openssl_provider::{
openssl_errors, parsec_provider_provider_init,
};
use parsec_openssl_provider::{openssl_errors, parsec_provider_provider_init};

use parsec_openssl_provider::parsec_openssl2::{OPENSSL_SUCCESS,OPENSSL_ERROR};
use parsec_openssl_provider::parsec_openssl2::types::VOID_PTR_PTR;
use parsec_openssl_provider::parsec_openssl2::openssl_binding::{OSSL_CORE_HANDLE, OSSL_DISPATCH};
use parsec_openssl_provider::parsec_openssl2::types::VOID_PTR_PTR;
use parsec_openssl_provider::parsec_openssl2::{OPENSSL_ERROR, OPENSSL_SUCCESS};
mod catch;
use catch::r#catch;

Expand Down
4 changes: 3 additions & 1 deletion parsec-openssl-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ categories = ["cryptography", "hardware-support"]
edition = "2021"

[dependencies]
lazy_static = "1.4.0"
log = "0.4"
parsec-client = { git = "https://github.com/parallaxsecond/parsec-client-rust", tag="0.16.0" }
parsec-openssl2 = { path = "../parsec-openssl2" }
openssl-errors = "0.2.0"
log = "0.4"
43 changes: 35 additions & 8 deletions parsec-openssl-provider/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
// Copyright 2023 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use std::mem;
use std::sync::Arc;

pub use openssl_errors;
pub use parsec_openssl2;

use parsec_openssl2::{openssl_binding, types};

use openssl_binding::{
OSSL_CORE_HANDLE, OSSL_DISPATCH, OSSL_FUNC_PROVIDER_GETTABLE_PARAMS,
OSSL_FUNC_PROVIDER_GET_PARAMS, OSSL_FUNC_PROVIDER_QUERY_OPERATION,
OSSL_FUNC_PROVIDER_GET_PARAMS, OSSL_FUNC_PROVIDER_QUERY_OPERATION, OSSL_FUNC_PROVIDER_TEARDOWN,
};
use parsec_openssl2::openssl::error::ErrorStack;
use parsec_openssl2::types::VOID_PTR;
use parsec_openssl2::{openssl_binding, types};

mod provider;
use provider::*;

mod catch;
use catch::r#catch;

// The init function populates the dispatch table and returns a NULL pointer
// to the provider context. This needs to be changed when key management and
// crypto support is added to the provider.
use parsec_client::BasicClient;

struct ParsecProviderContext {
client: BasicClient,
}

// The init function populates the dispatch table and returns a void pointer
// to the provider context (which contains the parsec basic client).
pub unsafe fn parsec_provider_provider_init(
_handle: *const OSSL_CORE_HANDLE,
_in_: *const OSSL_DISPATCH,
out: *mut *const OSSL_DISPATCH,
provctx: types::VOID_PTR_PTR,
) -> Result<(), parsec_openssl2::Error> {
let parsec_provider_teardown_ptr: ProviderTeardownPtr = parsec_provider_teardown;

let parsec_provider_gettable_params_ptr: ProviderGettableParamsPtr =
parsec_provider_gettable_params;

let parsec_provider_get_params_ptr: ProviderGetParamsPtr = parsec_provider_get_params;

let parsec_provider_query_ptr: ProviderQueryPtr = parsec_provider_query;

static mut DISPATCH_TABLE: [OSSL_DISPATCH; 4] = [parsec_openssl2::ossl_dispatch!(); 4];
static mut DISPATCH_TABLE: [OSSL_DISPATCH; 5] = [parsec_openssl2::ossl_dispatch!(); 5];
static RESULT_INIT: std::sync::Once = std::sync::Once::new();

RESULT_INIT.call_once(|| {
DISPATCH_TABLE = [
parsec_openssl2::ossl_dispatch!(
OSSL_FUNC_PROVIDER_TEARDOWN,
parsec_provider_teardown_ptr
),
parsec_openssl2::ossl_dispatch!(
OSSL_FUNC_PROVIDER_GETTABLE_PARAMS,
parsec_provider_gettable_params_ptr
Expand All @@ -56,7 +69,20 @@ pub unsafe fn parsec_provider_provider_init(
});

*out = DISPATCH_TABLE.as_ptr();
*provctx = std::ptr::null_mut();

match BasicClient::new(Some("parsec-tool".to_string())) {
Err(e) => {
*provctx = std::ptr::null_mut();
log::error!("[Provider Context Error]: {:?}", e);
return Err(parsec_openssl2::Error::SysReturnedNull {
inner: ErrorStack::get(),
});
}
Ok(client) => {
let context = Arc::new(ParsecProviderContext { client });
*provctx = Arc::into_raw(context) as VOID_PTR;
}
}

Ok(())
}
Expand All @@ -65,6 +91,7 @@ openssl_errors::openssl_errors! {
#[allow(clippy::empty_enum)]
library Error("parsec_openssl_provider") {
functions {
PROVIDER_TEARDOWN("parsec_provider_teardown");
PROVIDER_GETTABLE_PARAMS("parsec_provider_gettable_params");
PROVIDER_GET_PARAMS("parsec_provider_get_params");
PROVIDER_QUERY("parsec_provider_query");
Expand Down
15 changes: 12 additions & 3 deletions parsec-openssl-provider/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use parsec_openssl2::{
OPENSSL_SUCCESS, OSSL_PROVIDER,
};

use crate::openssl_binding::{
OSSL_ALGORITHM, OSSL_PARAM, OSSL_PARAM_INTEGER, OSSL_PARAM_UTF8_PTR,
};
use std::sync::Arc;

use crate::openssl_binding::{OSSL_ALGORITHM, OSSL_PARAM, OSSL_PARAM_INTEGER, OSSL_PARAM_UTF8_PTR};
// Parsec provider parameters
pub const PARSEC_PROVIDER_NAME: &[u8; 24] = b"Parsec OpenSSL Provider\0";
pub const PARSEC_PROVIDER_VERSION: &[u8; 6] = b"0.1.0\0";
Expand Down Expand Up @@ -79,6 +79,9 @@ pub type ProviderQueryPtr = unsafe extern "C" fn(
no_cache: *mut ::std::os::raw::c_int,
) -> *const OSSL_ALGORITHM;

// Function pointer of type OSSL_FUNC_PROVIDER_TEARDOWN
pub type ProviderTeardownPtr = unsafe extern "C" fn(provctx: *const OSSL_PROVIDER);

// The null provider implementation currently doesn't supply any algorithms to the core
pub unsafe extern "C" fn parsec_provider_query(
_prov: *mut OSSL_PROVIDER,
Expand All @@ -88,3 +91,9 @@ pub unsafe extern "C" fn parsec_provider_query(
*no_cache = 0;
std::ptr::null_mut()
}

// Teardowns the Provider context
pub unsafe extern "C" fn parsec_provider_teardown(provctx: *const OSSL_PROVIDER) {
// Makes sure the provider context gets dropped
Arc::from_raw(provctx);
}
1 change: 1 addition & 0 deletions parsec-openssl2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use parsec_openssl_sys2::openssl_binding;
pub use parsec_openssl_sys2::param as openssl_provider_param;
pub mod types;

pub use openssl;
pub use openssl2::*;

// OpenSSL expects an integer return value of 1 and 0 for success and error
Expand Down

0 comments on commit afe6782

Please sign in to comment.