Skip to content

Commit d5a03e9

Browse files
committed
pkey_ctx/Selection: make Enum and rename trait
1 parent cfe9956 commit d5a03e9

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

openssl/src/pkey_ctx.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,41 @@ impl NonceType {
135135

136136
cfg_if! {
137137
if #[cfg(ossl300)] {
138+
#[derive(Debug, PartialEq)]
139+
pub(crate) enum Selection {
140+
/// Key parameters
141+
KeyParameters,
142+
/// Public key (including parameters, if applicable).
143+
PublicKey,
144+
/// Keypair, which includes private key, public key, and parameters (if available).
145+
Keypair,
146+
}
147+
148+
impl From<Selection> for i32 {
149+
fn from(value: Selection) -> Self {
150+
match value {
151+
Selection::KeyParameters => ffi::EVP_PKEY_KEY_PARAMETERS,
152+
Selection::PublicKey => ffi::EVP_PKEY_PUBLIC_KEY,
153+
Selection::Keypair => ffi::EVP_PKEY_KEYPAIR,
154+
}
155+
}
156+
}
157+
138158
/// Selection for fromdata operation.
139-
pub(crate) trait Selection {
140-
const SELECTION: c_int;
159+
pub(crate) trait SelectionT {
160+
const SELECTION: Selection;
141161
}
142162

143-
impl Selection for Params {
144-
const SELECTION: c_int = ffi::EVP_PKEY_KEY_PARAMETERS;
163+
impl SelectionT for Params {
164+
const SELECTION: Selection = Selection::KeyParameters;
145165
}
146166

147-
impl Selection for Public {
148-
const SELECTION: c_int = ffi::EVP_PKEY_PUBLIC_KEY;
167+
impl SelectionT for Public {
168+
const SELECTION: Selection = Selection::PublicKey;
149169
}
150170

151-
impl Selection for Private {
152-
const SELECTION: c_int = ffi::EVP_PKEY_KEYPAIR;
171+
impl SelectionT for Private {
172+
const SELECTION: Selection = Selection::Keypair;
153173
}
154174
}
155175
}
@@ -475,13 +495,18 @@ impl<T> PkeyCtxRef<T> {
475495
#[inline]
476496
#[cfg(ossl300)]
477497
#[allow(dead_code)]
478-
pub(crate) fn fromdata<K: Selection>(
498+
pub(crate) fn fromdata<K: SelectionT>(
479499
&mut self,
480500
params: &ParamsRef<'_>,
481501
) -> Result<PKey<K>, ErrorStack> {
482502
let mut key_ptr = ptr::null_mut();
483503
cvt(unsafe {
484-
ffi::EVP_PKEY_fromdata(self.as_ptr(), &mut key_ptr, K::SELECTION, params.as_ptr())
504+
ffi::EVP_PKEY_fromdata(
505+
self.as_ptr(),
506+
&mut key_ptr,
507+
K::SELECTION.into(),
508+
params.as_ptr(),
509+
)
485510
})?;
486511
Ok(unsafe { PKey::from_ptr(key_ptr) })
487512
}
@@ -971,7 +996,7 @@ impl<T> PkeyCtxRef<T> {
971996
/// Creates a new `PKey` from the given ID and parameters.
972997
#[cfg(ossl300)]
973998
#[allow(dead_code)]
974-
pub(crate) fn pkey_from_params<K: Selection>(
999+
pub(crate) fn pkey_from_params<K: SelectionT>(
9751000
id: Id,
9761001
params: &ParamsRef<'_>,
9771002
) -> Result<PKey<K>, ErrorStack> {

0 commit comments

Comments
 (0)