|  | 
|  | 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