Skip to content

Commit f8a1abf

Browse files
committed
pkey/Id: implement TryInto<&CStr>
1 parent 8eb8d9f commit f8a1abf

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

openssl/src/pkey.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ use crate::error::ErrorStack;
5151
use crate::pkey_ctx::PkeyCtx;
5252
use crate::rsa::Rsa;
5353
use crate::symm::Cipher;
54-
use crate::util::{invoke_passwd_cb, CallbackState};
54+
use crate::util::{c_str, invoke_passwd_cb, CallbackState};
5555
use crate::{cvt, cvt_p};
5656
use cfg_if::cfg_if;
5757
use foreign_types::{ForeignType, ForeignTypeRef};
5858
use libc::{c_int, c_long};
5959
use openssl_macros::corresponds;
6060
use std::convert::{TryFrom, TryInto};
61-
use std::ffi::CString;
61+
use std::ffi::{CStr, CString};
6262
use std::fmt;
6363
#[cfg(all(not(any(boringssl, awslc)), ossl110))]
6464
use std::mem;
@@ -154,6 +154,41 @@ impl TryFrom<Id> for &'static str {
154154
}
155155
}
156156

157+
impl TryFrom<Id> for &'static CStr {
158+
type Error = ();
159+
fn try_from(id: Id) -> Result<Self, Self::Error> {
160+
match id {
161+
Id::RSA => Ok(c_str(b"RSA\0")),
162+
#[cfg(any(ossl111, libressl310, boringssl, awslc))]
163+
Id::RSA_PSS => Ok(c_str(b"RSA-PSS\0")),
164+
#[cfg(not(boringssl))]
165+
Id::HMAC => Ok(c_str(b"HMAC\0")),
166+
#[cfg(not(any(boringssl, awslc)))]
167+
Id::CMAC => Ok(c_str(b"CMAC\0")),
168+
Id::DSA => Ok(c_str(b"DSA\0")),
169+
Id::DH => Ok(c_str(b"DH\0")),
170+
#[cfg(ossl110)]
171+
Id::DHX => Ok(c_str(b"DHX\0")),
172+
Id::EC => Ok(c_str(b"EC\0")),
173+
#[cfg(ossl111)]
174+
Id::SM2 => Ok(c_str(b"SM2\0")),
175+
#[cfg(any(ossl110, boringssl, libressl360, awslc))]
176+
Id::HKDF => Ok(c_str(b"HKDF\0")),
177+
#[cfg(any(ossl111, boringssl, libressl370, awslc))]
178+
Id::ED25519 => Ok(c_str(b"Ed25519\0")),
179+
#[cfg(ossl111)]
180+
Id::ED448 => Ok(c_str(b"Ed448\0")),
181+
#[cfg(any(ossl111, boringssl, libressl370, awslc))]
182+
Id::X25519 => Ok(c_str(b"X25519\0")),
183+
#[cfg(ossl111)]
184+
Id::X448 => Ok(c_str(b"X448\0")),
185+
#[cfg(ossl111)]
186+
Id::POLY1305 => Ok(c_str(b"POLY1305\0")),
187+
_ => Err(()),
188+
}
189+
}
190+
}
191+
157192
/// A trait indicating that a key has parameters.
158193
pub unsafe trait HasParams {}
159194

0 commit comments

Comments
 (0)