Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PkeyCtx::new_from_name #2051

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions openssl-sys/src/handwritten/evp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ extern "C" {

pub fn EVP_PKEY_CTX_new(k: *mut EVP_PKEY, e: *mut ENGINE) -> *mut EVP_PKEY_CTX;
pub fn EVP_PKEY_CTX_new_id(id: c_int, e: *mut ENGINE) -> *mut EVP_PKEY_CTX;
pub fn EVP_PKEY_CTX_new_from_name(
ctx: *mut OSSL_LIB_CTX,
name: *const c_char,
property: *const c_char,
) -> *mut EVP_PKEY_CTX;
pub fn EVP_PKEY_CTX_free(ctx: *mut EVP_PKEY_CTX);

pub fn EVP_PKEY_CTX_ctrl(
Expand Down
28 changes: 28 additions & 0 deletions openssl/src/pkey_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ use foreign_types::{ForeignType, ForeignTypeRef};
use libc::c_int;
use openssl_macros::corresponds;
use std::convert::TryFrom;
use std::ffi::CString;
use std::ptr;

/// HKDF modes of operation.
Expand Down Expand Up @@ -125,6 +126,24 @@ impl<T> PkeyCtx<T> {
Ok(PkeyCtx::from_ptr(ptr))
}
}

#[cfg(ossl300)]
pub fn new_from_name(
lib_ctx: &crate::lib_ctx::LibCtxRef,
name: &str,
property: Option<&str>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is sound:

None of the arguments are duplicated, so they must remain unchanged for the lifetime of the returned EVP_PKEY_CTX or of any of its duplicates.

) -> Result<Self, ErrorStack> {
unsafe {
let property = property.map(|s| CString::new(s).unwrap());
let name = CString::new(name).unwrap();
let ptr = cvt_p(ffi::EVP_PKEY_CTX_new_from_name(
lib_ctx.as_ptr(),
name.as_ptr(),
property.map_or(ptr::null_mut(), |s| s.as_ptr()),
))?;
Ok(PkeyCtx::from_ptr(ptr))
}
}
}

impl PkeyCtx<()> {
Expand Down Expand Up @@ -999,4 +1018,13 @@ mod test {
// The digest is the end of the DigestInfo structure.
assert_eq!(result_buf[length - digest.len()..length], digest);
}

#[test]
#[cfg(ossl300)]
fn test_pkeyctx_from_name() {
use crate::pkey::Public;

let lib_ctx = crate::lib_ctx::LibCtx::new().unwrap();
let _: PkeyCtx<Public> = PkeyCtx::new_from_name(lib_ctx.as_ref(), "RSA", None).unwrap();
}
}
Loading