Skip to content

Commit 5454c12

Browse files
committed
Add test crate
Signed-off-by: Gowtham Suresh Kumar <gowtham.sureshkumar@arm.com>
1 parent 8059af2 commit 5454c12

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ members = [
44
"parsec-openssl2",
55
"parsec-openssl-provider",
66
"parsec-openssl-provider-shared",
7+
"parsec-openssl-provider-shared-test",
78
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "parsec-openssl-provider-shared-test"
3+
version = "0.1.0"
4+
authors = ["Parsec Project Contributors"]
5+
description = "A parsec openssl provider dynamic library"
6+
license = "Apache-2.0"
7+
readme = "README.md"
8+
keywords = ["security", "service"]
9+
categories = ["cryptography", "hardware-support"]
10+
edition = "2021"
11+
12+
[dependencies]
13+
openssl = "0.10.63"
14+
parsec-openssl2 = { path = "../parsec-openssl2" }
15+
foreign-types = "0.3"
16+
foreign-types-shared = "0.1"
17+
18+
[build-dependencies]
19+
pkg-config = "0.3.18"
20+
bindgen = { version = "0.66.1" }
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2023 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use std::env;
4+
use std::io::{Error, ErrorKind};
5+
use std::path::PathBuf;
6+
7+
const MINIMUM_VERSION: &str = "3.0.0";
8+
9+
fn main() -> std::io::Result<()> {
10+
// Use package config to ensure openssl version 3.0.0 or higher is installed
11+
let openssl = pkg_config::Config::new()
12+
.atleast_version(MINIMUM_VERSION)
13+
.probe("openssl")
14+
.expect("Failed to find openssl version above 3.0.0");
15+
16+
// The include path points to the openssl development headers installed by libss-dev
17+
let openssl_include_path = openssl.include_paths[0]
18+
.clone()
19+
.into_os_string()
20+
.into_string()
21+
.expect("Error converting OsString to String.");
22+
23+
// Generate bindings for the required headers
24+
let openssl_builder = bindgen::Builder::default()
25+
.header(format!("{}/openssl/evp.h", openssl_include_path))
26+
.generate_comments(false)
27+
.size_t_is_usize(true);
28+
29+
// Build the bindings
30+
let openssl_bindings = openssl_builder
31+
.generate()
32+
.map_err(|_| Error::new(ErrorKind::Other, "Unable to generate bindings to openssl"))?;
33+
34+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
35+
openssl_bindings.write_to_file(out_path.join("openssl_test_bindings.rs"))?;
36+
37+
Ok(())
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 202 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use openssl::error::ErrorStack;
5+
use openssl::{lib_ctx::LibCtx, provider::Provider};
6+
use parsec_openssl2::openssl_binding;
7+
use parsec_openssl2::types::VOID_PTR_PTR;
8+
9+
#[allow(non_camel_case_types)]
10+
#[allow(non_upper_case_globals)]
11+
#[allow(non_snake_case)]
12+
#[allow(improper_ctypes)]
13+
#[ignore]
14+
pub mod openssl_test_bindings {
15+
16+
include!(concat!(env!("OUT_DIR"), "/openssl_test_bindings.rs"));
17+
}
18+
19+
pub fn load_provider(lib_ctx: &LibCtx, provider_name: String, provider_path: String) -> Provider {
20+
Provider::set_default_search_path(None, &provider_path);
21+
let provider = Provider::load(Some(&lib_ctx), &provider_name).unwrap();
22+
provider
23+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 202 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use openssl::{lib_ctx::LibCtx, provider::Provider};
4+
use openssl_test_bindings::*;
5+
use parsec_openssl_provider_shared_test::*;
6+
7+
use foreign_types_shared::ForeignType;
8+
use parsec_openssl2::openssl_binding;
9+
use parsec_openssl2::ossl_param;
10+
11+
// Simple test to load a provider. Test fails if load_provider function reports error
12+
#[test]
13+
fn test_loading_parsec_provider() {
14+
let provider_path = String::from("/tmp/parsec/target/debug/");
15+
//let provider_name = String::from("libparsec_openssl_provider_shared");
16+
let provider_name = String::from("default");
17+
let mut lib_ctx: LibCtx = LibCtx::new().unwrap();
18+
let mut provider: Provider = load_provider(&lib_ctx, provider_name, provider_path);
19+
}
20+
21+
pub const PARSEC_PROVIDER_RSA: &[u8; 4] = b"RSA\0";
22+
pub const PARSEC_PROVIDER_PROPERTY: &[u8; 17] = b"provider=default\0";
23+
24+
// Loads a keys from the provider and returns an EVP_PKEY object with the details
25+
// This is working against the default provider and currently loads a key with
26+
// no parameters. In order to test it with the parsec provider 3 changes are needed
27+
// explained in comments below.
28+
#[test]
29+
fn test_loading_keys() {
30+
// Change 1: toggle comment below to load the parsec provider
31+
let provider_path = String::from("/tmp/parsec/target/debug/");
32+
//let provider_name = String::from("libparsec_openssl_provider_shared");
33+
34+
let provider_name = String::from("default");
35+
unsafe {
36+
let mut lib_ctx: LibCtx = LibCtx::new().unwrap();
37+
let mut parsec_pkey: *mut EVP_PKEY = std::ptr::null_mut();
38+
let mut parsec_pkey_ptr: *mut *mut EVP_PKEY = &mut parsec_pkey;
39+
let mut provider: Provider = load_provider(&lib_ctx, provider_name, provider_path);
40+
41+
// Change 2: Setup the param as needed for the parsec provider.
42+
// Create a key beforehand using the parsec-tool and then run the test.
43+
let mut param = ossl_param!();
44+
let mut param_ptr: *mut OSSL_PARAM = &mut param;
45+
46+
let mut evp_ctx: *mut EVP_PKEY_CTX = EVP_PKEY_CTX_new_from_name(
47+
lib_ctx.as_ptr() as *mut ossl_lib_ctx_st,
48+
PARSEC_PROVIDER_RSA.as_ptr() as *const ::std::os::raw::c_char,
49+
PARSEC_PROVIDER_PROPERTY.as_ptr() as *const ::std::os::raw::c_char,
50+
);
51+
assert_ne!(evp_ctx, std::ptr::null_mut());
52+
assert_eq!(EVP_PKEY_fromdata_init(evp_ctx), 1);
53+
assert_eq!(
54+
EVP_PKEY_fromdata(
55+
evp_ctx,
56+
parsec_pkey_ptr,
57+
// Change 3: Select the appropriate macro here to load the param value
58+
EVP_PKEY_KEY_PARAMETERS.try_into().unwrap(),
59+
param_ptr,
60+
),
61+
1
62+
);
63+
64+
EVP_PKEY_CTX_free(evp_ctx);
65+
evp_ctx = std::ptr::null_mut();
66+
//OSSL_PARAM_free(param_ptr);
67+
}
68+
}

0 commit comments

Comments
 (0)