diff options
author | Steven Fackler <sfackler@gmail.com> | 2021-03-07 11:58:49 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-07 11:58:49 -0500 |
commit | ad741f88f2136a82c0557179fb420cd6283b610a (patch) | |
tree | 75e2b3b4624f4671069adcf82525699b32425297 | |
parent | 5c78192bbf9116d877d29ec9ebf5e5cb1459c1c2 (diff) | |
parent | 61a27426fff82172911404e4a1be660be350a0d2 (diff) | |
download | rust-openssl-ad741f88f2136a82c0557179fb420cd6283b610a.zip |
Merge pull request #1425 from baloo/baloo/oaep_label
expose rsa OAEP label methods
-rw-r--r-- | openssl-sys/src/rsa.rs | 18 | ||||
-rw-r--r-- | openssl/src/encrypt.rs | 38 |
2 files changed, 56 insertions, 0 deletions
diff --git a/openssl-sys/src/rsa.rs b/openssl-sys/src/rsa.rs index dd7cab95..96049808 100644 --- a/openssl-sys/src/rsa.rs +++ b/openssl-sys/src/rsa.rs @@ -61,6 +61,22 @@ pub unsafe fn EVP_PKEY_CTX_set_rsa_oaep_md(ctx: *mut EVP_PKEY_CTX, md: *mut EVP_ ) } +#[cfg(any(ossl102, libressl310))] +pub unsafe fn EVP_PKEY_CTX_set0_rsa_oaep_label( + ctx: *mut EVP_PKEY_CTX, + label: *mut c_void, + len: c_int, +) -> c_int { + EVP_PKEY_CTX_ctrl( + ctx, + EVP_PKEY_RSA, + EVP_PKEY_OP_TYPE_CRYPT, + EVP_PKEY_CTRL_RSA_OAEP_LABEL, + len, + label as *mut c_void, + ) +} + pub const EVP_PKEY_CTRL_RSA_PADDING: c_int = EVP_PKEY_ALG_CTRL + 1; pub const EVP_PKEY_CTRL_RSA_PSS_SALTLEN: c_int = EVP_PKEY_ALG_CTRL + 2; @@ -70,6 +86,8 @@ pub const EVP_PKEY_CTRL_GET_RSA_PADDING: c_int = EVP_PKEY_ALG_CTRL + 6; #[cfg(any(ossl102, libressl310))] pub const EVP_PKEY_CTRL_RSA_OAEP_MD: c_int = EVP_PKEY_ALG_CTRL + 9; +#[cfg(any(ossl102, libressl310))] +pub const EVP_PKEY_CTRL_RSA_OAEP_LABEL: c_int = EVP_PKEY_ALG_CTRL + 10; pub const RSA_PKCS1_PADDING: c_int = 1; pub const RSA_SSLV23_PADDING: c_int = 2; diff --git a/openssl/src/encrypt.rs b/openssl/src/encrypt.rs index 658d7b7b..f80715cd 100644 --- a/openssl/src/encrypt.rs +++ b/openssl/src/encrypt.rs @@ -39,6 +39,7 @@ //! decrypted.truncate(decrypted_len); //! assert_eq!(&*decrypted, data); //! ``` +use libc::{c_int, c_void}; use std::{marker::PhantomData, ptr}; use crate::error::ErrorStack; @@ -157,6 +158,43 @@ impl<'a> Encrypter<'a> { } } + /// Sets the RSA OAEP label. + /// + /// This is only useful for RSA keys. + /// + /// This corresponds to [`EVP_PKEY_CTX_set0_rsa_oaep_label`]. + /// + /// [`EVP_PKEY_CTX_set0_rsa_oaep_label`]: https://www.openssl.org/docs/manmaster/man3/EVP_PKEY_CTX_set0_rsa_oaep_label.html + #[cfg(any(ossl102, libressl310))] + pub fn set_rsa_oaep_label(&mut self, label: &[u8]) -> Result<(), ErrorStack> { + unsafe { + let p = cvt_p(ffi::CRYPTO_malloc( + label.len() as _, + concat!(file!(), "\0").as_ptr() as *const _, + line!() as c_int, + ))?; + ptr::copy_nonoverlapping(label.as_ptr(), p as *mut u8, label.len()); + + cvt(ffi::EVP_PKEY_CTX_set0_rsa_oaep_label( + self.pctx, + p as *mut c_void, + label.len() as c_int, + )) + .map(|_| ()) + .map_err(|e| { + #[cfg(not(ossl110))] + ::ffi::CRYPTO_free(p as *mut c_void); + #[cfg(ossl110)] + ::ffi::CRYPTO_free( + p as *mut c_void, + concat!(file!(), "\0").as_ptr() as *const _, + line!() as c_int, + ); + e + }) + } + } + /// Performs public key encryption. /// /// In order to know the size needed for the output buffer, use [`encrypt_len`](Encrypter::encrypt_len). |