From d706290bb34030e0189b64af84abab5c7431658b Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 29 Dec 2025 17:28:04 +0000 Subject: [PATCH 1/3] Avoid useless malloc for SSL_set_tlsext_status_ocsp_resp --- boring/src/ssl/mod.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/boring/src/ssl/mod.rs b/boring/src/ssl/mod.rs index 8b437dbf3..5dee12e60 100644 --- a/boring/src/ssl/mod.rs +++ b/boring/src/ssl/mod.rs @@ -3579,17 +3579,15 @@ impl SslRef { } /// Sets the OCSP response to be returned to the client. - #[corresponds(SSL_set_tlsext_status_ocsp_resp)] + #[corresponds(SSL_set_ocsp_response)] pub fn set_ocsp_status(&mut self, response: &[u8]) -> Result<(), ErrorStack> { unsafe { assert!(response.len() <= c_int::MAX as usize); - let p = cvt_p(ffi::OPENSSL_malloc(response.len() as _))?; - ptr::copy_nonoverlapping(response.as_ptr(), p as *mut u8, response.len()); - cvt(ffi::SSL_set_tlsext_status_ocsp_resp( + cvt(ffi::SSL_set_ocsp_response( self.as_ptr(), - p as *mut c_uchar, + response.as_ptr(), response.len(), - ) as c_int) + )) } } From 70e900ea2df1224b0595f4e602aff5f9a1daf0ed Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 29 Dec 2025 19:41:08 +0000 Subject: [PATCH 2/3] mem::forget -= 1 --- boring/src/x509/store.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/boring/src/x509/store.rs b/boring/src/x509/store.rs index c3686bc80..f4edca034 100644 --- a/boring/src/x509/store.rs +++ b/boring/src/x509/store.rs @@ -48,7 +48,7 @@ use crate::x509::{X509Object, X509}; use crate::{cvt, cvt_p}; use foreign_types::{ForeignType, ForeignTypeRef}; use openssl_macros::corresponds; -use std::mem; +use std::mem::ManuallyDrop; foreign_type_and_impl_send_sync! { type CType = ffi::X509_STORE; @@ -73,9 +73,7 @@ impl X509StoreBuilder { /// Constructs the `X509Store`. #[must_use] pub fn build(self) -> X509Store { - let store = X509Store(self.0); - mem::forget(self); - store + X509Store(ManuallyDrop::new(self).0) } } From e6693f27e39731eb60919a7c72bf1ce792e5b643 Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 29 Dec 2025 17:27:48 +0000 Subject: [PATCH 3/3] Use std helper methods for pointer casts --- boring/src/aes.rs | 28 +++++---- boring/src/asn1.rs | 12 ++-- boring/src/bio.rs | 7 +-- boring/src/bn.rs | 13 ++--- boring/src/derive.rs | 9 +-- boring/src/dsa.rs | 10 ++-- boring/src/ec.rs | 8 +-- boring/src/ecdsa.rs | 14 ++--- boring/src/hash.rs | 6 +- boring/src/lib.rs | 2 +- boring/src/macros.rs | 2 +- boring/src/memcmp.rs | 9 +-- boring/src/nid.rs | 6 +- boring/src/pkcs12.rs | 4 +- boring/src/pkcs5.rs | 8 +-- boring/src/pkey.rs | 8 +-- boring/src/rsa.rs | 16 +++--- boring/src/sha.rs | 12 ++-- boring/src/sign.rs | 20 +++---- boring/src/srtp.rs | 2 +- boring/src/ssl/bio.rs | 12 ++-- boring/src/ssl/callbacks.rs | 16 +++--- boring/src/ssl/mod.rs | 109 ++++++++++++++---------------------- boring/src/stack.rs | 26 ++++----- boring/src/string.rs | 4 +- boring/src/symm.rs | 9 ++- boring/src/util.rs | 8 +-- boring/src/x509/mod.rs | 52 ++++++++--------- boring/src/x509/verify.rs | 6 +- 29 files changed, 190 insertions(+), 248 deletions(-) diff --git a/boring/src/aes.rs b/boring/src/aes.rs index 8a47cbafc..2a46b6125 100644 --- a/boring/src/aes.rs +++ b/boring/src/aes.rs @@ -38,7 +38,7 @@ //! ``` //! use crate::ffi; -use libc::{c_int, c_uint, size_t}; +use libc::{c_int, c_uint}; use openssl_macros::corresponds; use std::mem::MaybeUninit; use std::ptr; @@ -63,7 +63,7 @@ impl AesKey { let mut aes_key = MaybeUninit::uninit(); let r = ffi::AES_set_encrypt_key( - key.as_ptr() as *const _, + key.as_ptr(), key.len() as c_uint * 8, aes_key.as_mut_ptr(), ); @@ -87,7 +87,7 @@ impl AesKey { let mut aes_key = MaybeUninit::uninit(); let r = ffi::AES_set_decrypt_key( - key.as_ptr() as *const _, + key.as_ptr(), key.len() as c_uint * 8, aes_key.as_mut_ptr(), ); @@ -125,12 +125,11 @@ pub fn wrap_key( assert!(out.len() >= in_.len() + 8); // Ciphertext is 64 bits longer (see 2.2.1) let written = ffi::AES_wrap_key( - &key.0 as *const _ as *mut _, // this is safe, the implementation only uses the key as a const pointer. - iv.as_ref() - .map_or(ptr::null(), |iv| iv.as_ptr() as *const _), - out.as_ptr() as *mut _, - in_.as_ptr() as *const _, - in_.len() as size_t, + std::ptr::addr_of!(key.0).cast_mut(), // this is safe, the implementation only uses the key as a const pointer. + iv.as_ref().map_or(ptr::null(), |iv| iv.as_ptr()), + out.as_mut_ptr(), + in_.as_ptr(), + in_.len(), ); if written <= 0 { Err(KeyError(())) @@ -164,12 +163,11 @@ pub fn unwrap_key( assert!(out.len() + 8 <= in_.len()); let written = ffi::AES_unwrap_key( - &key.0 as *const _ as *mut _, // this is safe, the implementation only uses the key as a const pointer. - iv.as_ref() - .map_or(ptr::null(), |iv| iv.as_ptr() as *const _), - out.as_ptr() as *mut _, - in_.as_ptr() as *const _, - in_.len() as size_t, + std::ptr::addr_of!(key.0).cast_mut(), // this is safe, the implementation only uses the key as a const pointer. + iv.as_ref().map_or(ptr::null(), |iv| iv.as_ptr().cast()), + out.as_ptr().cast_mut(), + in_.as_ptr().cast(), + in_.len(), ); if written <= 0 { diff --git a/boring/src/asn1.rs b/boring/src/asn1.rs index 6099217dd..27f375f29 100644 --- a/boring/src/asn1.rs +++ b/boring/src/asn1.rs @@ -26,7 +26,7 @@ //! ``` use crate::ffi; use foreign_types::{ForeignType, ForeignTypeRef}; -use libc::{c_char, c_int, c_long, time_t}; +use libc::{c_int, c_long, time_t}; use std::cmp::Ordering; use std::ffi::CString; use std::fmt; @@ -408,7 +408,7 @@ impl Asn1StringRef { return Err(ErrorStack::get()); } - Ok(OpensslString::from_ptr(ptr as *mut c_char)) + Ok(OpensslString::from_ptr(ptr.cast())) } } @@ -549,7 +549,7 @@ impl Asn1BitStringRef { #[corresponds(ASN1_STRING_length)] #[must_use] pub fn len(&self) -> usize { - unsafe { ffi::ASN1_STRING_length(self.as_ptr() as *const _) as usize } + unsafe { ffi::ASN1_STRING_length(self.as_ptr().cast_const()) as usize } } /// Determines if the string is empty. @@ -591,7 +591,7 @@ impl Asn1Object { unsafe { ffi::init(); let txt = CString::new(txt).map_err(ErrorStack::internal_error)?; - let obj: *mut ffi::ASN1_OBJECT = cvt_p(ffi::OBJ_txt2obj(txt.as_ptr() as *const _, 0))?; + let obj: *mut ffi::ASN1_OBJECT = cvt_p(ffi::OBJ_txt2obj(txt.as_ptr(), 0))?; Ok(Asn1Object::from_ptr(obj)) } } @@ -608,9 +608,9 @@ impl Asn1ObjectRef { impl fmt::Display for Asn1ObjectRef { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { unsafe { - let mut buf = [0; 80]; + let mut buf = [0u8; 80]; let len = ffi::OBJ_obj2txt( - buf.as_mut_ptr() as *mut _, + buf.as_mut_ptr().cast(), buf.len() as c_int, self.as_ptr(), 0, diff --git a/boring/src/bio.rs b/boring/src/bio.rs index 2e6b2572d..ff1e83e29 100644 --- a/boring/src/bio.rs +++ b/boring/src/bio.rs @@ -27,12 +27,7 @@ impl<'a> MemBioSlice<'a> { ffi::init(); assert!(buf.len() <= BufLen::MAX as usize); - let bio = unsafe { - cvt_p(BIO_new_mem_buf( - buf.as_ptr() as *const _, - buf.len() as BufLen, - ))? - }; + let bio = unsafe { cvt_p(BIO_new_mem_buf(buf.as_ptr().cast(), buf.len() as BufLen))? }; Ok(MemBioSlice(bio, PhantomData)) } diff --git a/boring/src/bn.rs b/boring/src/bn.rs index b8464e1d2..659d516e4 100644 --- a/boring/src/bn.rs +++ b/boring/src/bn.rs @@ -24,7 +24,7 @@ //! [`BIGNUM`]: https://wiki.openssl.org/index.php/Manual:Bn_internal(3) use crate::ffi; use foreign_types::{ForeignType, ForeignTypeRef}; -use libc::{c_int, size_t}; +use libc::c_int; use std::cmp::Ordering; use std::ffi::CString; use std::ops::{Add, Deref, Div, Mul, Neg, Rem, Shl, Shr, Sub}; @@ -831,7 +831,7 @@ impl BigNum { ffi::init(); let c_str = CString::new(s.as_bytes()).map_err(ErrorStack::internal_error)?; let mut bn = ptr::null_mut(); - cvt(ffi::BN_dec2bn(&mut bn, c_str.as_ptr() as *const _))?; + cvt(ffi::BN_dec2bn(&mut bn, c_str.as_ptr()))?; Ok(BigNum::from_ptr(bn)) } } @@ -843,7 +843,7 @@ impl BigNum { ffi::init(); let c_str = CString::new(s.as_bytes()).map_err(ErrorStack::internal_error)?; let mut bn = ptr::null_mut(); - cvt(ffi::BN_hex2bn(&mut bn, c_str.as_ptr() as *const _))?; + cvt(ffi::BN_hex2bn(&mut bn, c_str.as_ptr()))?; Ok(BigNum::from_ptr(bn)) } } @@ -865,12 +865,7 @@ impl BigNum { unsafe { ffi::init(); assert!(n.len() <= c_int::MAX as usize); - cvt_p(ffi::BN_bin2bn( - n.as_ptr(), - n.len() as size_t, - ptr::null_mut(), - )) - .map(|p| BigNum::from_ptr(p)) + cvt_p(ffi::BN_bin2bn(n.as_ptr(), n.len(), ptr::null_mut())).map(|p| BigNum::from_ptr(p)) } } } diff --git a/boring/src/derive.rs b/boring/src/derive.rs index a8d822d4d..a094c66d7 100644 --- a/boring/src/derive.rs +++ b/boring/src/derive.rs @@ -64,14 +64,7 @@ impl<'a> Deriver<'a> { #[corresponds(EVP_PKEY_derive)] pub fn derive(&mut self, buf: &mut [u8]) -> Result { let mut len = buf.len(); - unsafe { - cvt(ffi::EVP_PKEY_derive( - self.0, - buf.as_mut_ptr() as *mut _, - &mut len, - )) - .map(|_| len) - } + unsafe { cvt(ffi::EVP_PKEY_derive(self.0, buf.as_mut_ptr(), &mut len)).map(|_| len) } } /// A convenience function which derives a shared secret and returns it in a new buffer. diff --git a/boring/src/dsa.rs b/boring/src/dsa.rs index ca7efaf32..bc6068add 100644 --- a/boring/src/dsa.rs +++ b/boring/src/dsa.rs @@ -103,7 +103,7 @@ where unsafe { let mut pub_key = ptr::null(); DSA_get0_key(self.as_ptr(), &mut pub_key, ptr::null_mut()); - BigNumRef::from_ptr(pub_key as *mut _) + BigNumRef::from_ptr(pub_key.cast_mut()) } } } @@ -132,7 +132,7 @@ where unsafe { let mut priv_key = ptr::null(); DSA_get0_key(self.as_ptr(), ptr::null_mut(), &mut priv_key); - BigNumRef::from_ptr(priv_key as *mut _) + BigNumRef::from_ptr(priv_key.cast_mut()) } } } @@ -154,7 +154,7 @@ where unsafe { let mut p = ptr::null(); DSA_get0_pqg(self.as_ptr(), &mut p, ptr::null_mut(), ptr::null_mut()); - BigNumRef::from_ptr(p as *mut _) + BigNumRef::from_ptr(p.cast_mut()) } } @@ -164,7 +164,7 @@ where unsafe { let mut q = ptr::null(); DSA_get0_pqg(self.as_ptr(), ptr::null_mut(), &mut q, ptr::null_mut()); - BigNumRef::from_ptr(q as *mut _) + BigNumRef::from_ptr(q.cast_mut()) } } @@ -174,7 +174,7 @@ where unsafe { let mut g = ptr::null(); DSA_get0_pqg(self.as_ptr(), ptr::null_mut(), ptr::null_mut(), &mut g); - BigNumRef::from_ptr(g as *mut _) + BigNumRef::from_ptr(g.cast_mut()) } } } diff --git a/boring/src/ec.rs b/boring/src/ec.rs index 6bf3f09fb..745b81fcb 100644 --- a/boring/src/ec.rs +++ b/boring/src/ec.rs @@ -183,7 +183,7 @@ impl EcGroupRef { pub fn generator(&self) -> &EcPointRef { unsafe { let ptr = ffi::EC_GROUP_get0_generator(self.as_ptr()); - EcPointRef::from_ptr(ptr as *mut _) + EcPointRef::from_ptr(ptr.cast_mut()) } } @@ -497,7 +497,7 @@ where pub fn private_key(&self) -> &BigNumRef { unsafe { let ptr = ffi::EC_KEY_get0_private_key(self.as_ptr()); - BigNumRef::from_ptr(ptr as *mut _) + BigNumRef::from_ptr(ptr.cast_mut()) } } } @@ -512,7 +512,7 @@ where pub fn public_key(&self) -> &EcPointRef { unsafe { let ptr = ffi::EC_KEY_get0_public_key(self.as_ptr()); - EcPointRef::from_ptr(ptr as *mut _) + EcPointRef::from_ptr(ptr.cast_mut()) } } @@ -543,7 +543,7 @@ where pub fn group(&self) -> &EcGroupRef { unsafe { let ptr = ffi::EC_KEY_get0_group(self.as_ptr()); - EcGroupRef::from_ptr(ptr as *mut _) + EcGroupRef::from_ptr(ptr.cast_mut()) } } diff --git a/boring/src/ecdsa.rs b/boring/src/ecdsa.rs index 4d5bb0979..27be5c51f 100644 --- a/boring/src/ecdsa.rs +++ b/boring/src/ecdsa.rs @@ -2,7 +2,7 @@ use crate::ffi; use foreign_types::{ForeignType, ForeignTypeRef}; -use libc::{c_int, size_t}; +use libc::c_int; use openssl_macros::corresponds; use std::mem; use std::ptr; @@ -36,10 +36,10 @@ impl EcdsaSig { assert!(data.len() <= c_int::MAX as usize); let sig = cvt_p(ffi::ECDSA_do_sign( data.as_ptr(), - data.len() as size_t, + data.len(), eckey.as_ptr(), ))?; - Ok(EcdsaSig::from_ptr(sig as *mut _)) + Ok(EcdsaSig::from_ptr(sig)) } } @@ -51,7 +51,7 @@ impl EcdsaSig { let sig = cvt_p(ffi::ECDSA_SIG_new())?; ECDSA_SIG_set0(sig, r.as_ptr(), s.as_ptr()); mem::forget((r, s)); - Ok(EcdsaSig::from_ptr(sig as *mut _)) + Ok(EcdsaSig::from_ptr(sig)) } } @@ -83,7 +83,7 @@ impl EcdsaSigRef { assert!(data.len() <= c_int::MAX as usize); cvt_n(ffi::ECDSA_do_verify( data.as_ptr(), - data.len() as size_t, + data.len(), self.as_ptr(), eckey.as_ptr(), )) @@ -98,7 +98,7 @@ impl EcdsaSigRef { unsafe { let mut r = ptr::null(); ECDSA_SIG_get0(self.as_ptr(), &mut r, ptr::null_mut()); - BigNumRef::from_ptr(r as *mut _) + BigNumRef::from_ptr(r.cast_mut()) } } @@ -109,7 +109,7 @@ impl EcdsaSigRef { unsafe { let mut s = ptr::null(); ECDSA_SIG_get0(self.as_ptr(), ptr::null_mut(), &mut s); - BigNumRef::from_ptr(s as *mut _) + BigNumRef::from_ptr(s.cast_mut()) } } } diff --git a/boring/src/hash.rs b/boring/src/hash.rs index 5deadade2..69f333c45 100644 --- a/boring/src/hash.rs +++ b/boring/src/hash.rs @@ -1,7 +1,7 @@ use crate::ffi; use openssl_macros::corresponds; use std::convert::TryInto; -use std::ffi::{c_uint, c_void}; +use std::ffi::c_uint; use std::fmt; use std::io; use std::io::prelude::*; @@ -196,7 +196,7 @@ impl Hasher { unsafe { cvt(ffi::EVP_DigestUpdate( self.ctx, - data.as_ptr() as *mut _, + data.as_ptr().cast_mut().cast(), data.len(), ))?; } @@ -370,7 +370,7 @@ fn hmac( cvt_p(unsafe { ffi::HMAC( digest.as_ptr(), - key.as_ptr() as *const c_void, + key.as_ptr().cast(), key.len(), data.as_ptr(), data.len(), diff --git a/boring/src/lib.rs b/boring/src/lib.rs index c558b146e..4bdc6c67c 100644 --- a/boring/src/lib.rs +++ b/boring/src/lib.rs @@ -209,6 +209,6 @@ unsafe extern "C" fn free_data_box( _argp: *mut c_void, ) { if !ptr.is_null() { - drop(Box::::from_raw(ptr as *mut T)); + drop(Box::::from_raw(ptr.cast::())); } } diff --git a/boring/src/macros.rs b/boring/src/macros.rs index 56c1d94ec..e2da938aa 100644 --- a/boring/src/macros.rs +++ b/boring/src/macros.rs @@ -28,7 +28,7 @@ macro_rules! private_key_from_pem { cvt_p($f(bio.as_ptr(), ptr::null_mut(), Some(crate::util::invoke_passwd_cb::), - &mut cb as *mut _ as *mut _)) + ptr::from_mut(&mut cb).cast())) .map(|p| ::foreign_types::ForeignType::from_ptr(p)) } } diff --git a/boring/src/memcmp.rs b/boring/src/memcmp.rs index 99cefdbee..4ac08b655 100644 --- a/boring/src/memcmp.rs +++ b/boring/src/memcmp.rs @@ -30,7 +30,6 @@ //! assert!(!eq(&a, &c)); //! ``` use crate::ffi; -use libc::size_t; /// Returns `true` iff `a` and `b` contain the same bytes. /// @@ -64,13 +63,7 @@ use libc::size_t; #[must_use] pub fn eq(a: &[u8], b: &[u8]) -> bool { assert!(a.len() == b.len()); - let ret = unsafe { - ffi::CRYPTO_memcmp( - a.as_ptr() as *const _, - b.as_ptr() as *const _, - a.len() as size_t, - ) - }; + let ret = unsafe { ffi::CRYPTO_memcmp(a.as_ptr().cast(), b.as_ptr().cast(), a.len()) }; ret == 0 } diff --git a/boring/src/nid.rs b/boring/src/nid.rs index 347b30f74..15cfe4eb9 100644 --- a/boring/src/nid.rs +++ b/boring/src/nid.rs @@ -1,6 +1,6 @@ //! A collection of numerical identifiers for OpenSSL objects. use crate::ffi; -use libc::{c_char, c_int}; +use libc::c_int; use openssl_macros::corresponds; use std::ffi::CStr; @@ -87,7 +87,7 @@ impl Nid { #[allow(clippy::trivially_copy_pass_by_ref)] pub fn long_name(&self) -> Result<&'static str, ErrorStack> { unsafe { - let nameptr = cvt_p(ffi::OBJ_nid2ln(self.0) as *mut c_char)?; + let nameptr = cvt_p(ffi::OBJ_nid2ln(self.0).cast_mut())?; CStr::from_ptr(nameptr) .to_str() .map_err(ErrorStack::internal_error) @@ -99,7 +99,7 @@ impl Nid { #[allow(clippy::trivially_copy_pass_by_ref)] pub fn short_name(&self) -> Result<&'static str, ErrorStack> { unsafe { - let nameptr = cvt_p(ffi::OBJ_nid2sn(self.0) as *mut c_char)?; + let nameptr = cvt_p(ffi::OBJ_nid2sn(self.0).cast_mut())?; CStr::from_ptr(nameptr) .to_str() .map_err(ErrorStack::internal_error) diff --git a/boring/src/pkcs12.rs b/boring/src/pkcs12.rs index e8fb7c12e..bb851421e 100644 --- a/boring/src/pkcs12.rs +++ b/boring/src/pkcs12.rs @@ -180,8 +180,8 @@ impl Pkcs12Builder { let keytype = 0; cvt_p(ffi::PKCS12_create( - pass.as_ptr() as *const _ as *mut _, - friendly_name.as_ptr() as *const _ as *mut _, + pass.as_ptr(), + friendly_name.as_ptr(), pkey, cert, ca, diff --git a/boring/src/pkcs5.rs b/boring/src/pkcs5.rs index a27181b3f..1b78c520e 100644 --- a/boring/src/pkcs5.rs +++ b/boring/src/pkcs5.rs @@ -96,7 +96,7 @@ pub fn pbkdf2_hmac( ffi::init(); cvt(ffi::PKCS5_PBKDF2_HMAC( - pass.as_ptr() as *const _, + pass.as_ptr().cast(), pass.len(), salt.as_ptr(), salt.len(), @@ -121,15 +121,15 @@ pub fn scrypt( unsafe { ffi::init(); cvt(ffi::EVP_PBE_scrypt( - pass.as_ptr() as *const _, + pass.as_ptr().cast(), pass.len(), - salt.as_ptr() as *const _, + salt.as_ptr().cast(), salt.len(), n, r, p, maxmem, - key.as_mut_ptr() as *mut _, + key.as_mut_ptr(), key.len(), )) } diff --git a/boring/src/pkey.rs b/boring/src/pkey.rs index 9245f5b64..b141f5bda 100644 --- a/boring/src/pkey.rs +++ b/boring/src/pkey.rs @@ -361,7 +361,7 @@ impl PKey { cvt(ffi::EVP_PKEY_assign( pkey.0, ffi::EVP_PKEY_RSA, - rsa.as_ptr() as *mut _, + rsa.as_ptr().cast(), ))?; mem::forget(rsa); Ok(pkey) @@ -377,7 +377,7 @@ impl PKey { cvt(ffi::EVP_PKEY_assign( pkey.0, ffi::EVP_PKEY_EC, - ec_key.as_ptr() as *mut _, + ec_key.as_ptr().cast(), ))?; mem::forget(ec_key); Ok(pkey) @@ -455,7 +455,7 @@ impl PKey { bio.as_ptr(), ptr::null_mut(), Some(invoke_passwd_cb::), - &mut cb as *mut _ as *mut _, + std::ptr::addr_of_mut!(cb).cast(), )) .map(|p| PKey::from_ptr(p)) } @@ -479,7 +479,7 @@ impl PKey { bio.as_ptr(), ptr::null_mut(), None, - passphrase.as_ptr() as *const _ as *mut _, + passphrase.as_ptr().cast_mut().cast(), )) .map(|p| PKey::from_ptr(p)) } diff --git a/boring/src/rsa.rs b/boring/src/rsa.rs index ff47e71ba..79dfa6df9 100644 --- a/boring/src/rsa.rs +++ b/boring/src/rsa.rs @@ -194,7 +194,7 @@ where unsafe { let mut d = ptr::null(); RSA_get0_key(self.as_ptr(), ptr::null_mut(), ptr::null_mut(), &mut d); - BigNumRef::from_ptr(d as *mut _) + BigNumRef::from_ptr(d.cast_mut()) } } @@ -208,7 +208,7 @@ where if p.is_null() { None } else { - Some(BigNumRef::from_ptr(p as *mut _)) + Some(BigNumRef::from_ptr(p.cast_mut())) } } } @@ -223,7 +223,7 @@ where if q.is_null() { None } else { - Some(BigNumRef::from_ptr(q as *mut _)) + Some(BigNumRef::from_ptr(q.cast_mut())) } } } @@ -238,7 +238,7 @@ where if dp.is_null() { None } else { - Some(BigNumRef::from_ptr(dp as *mut _)) + Some(BigNumRef::from_ptr(dp.cast_mut())) } } } @@ -253,7 +253,7 @@ where if dq.is_null() { None } else { - Some(BigNumRef::from_ptr(dq as *mut _)) + Some(BigNumRef::from_ptr(dq.cast_mut())) } } } @@ -268,7 +268,7 @@ where if qi.is_null() { None } else { - Some(BigNumRef::from_ptr(qi as *mut _)) + Some(BigNumRef::from_ptr(qi.cast_mut())) } } } @@ -391,7 +391,7 @@ where unsafe { let mut n = ptr::null(); RSA_get0_key(self.as_ptr(), &mut n, ptr::null_mut(), ptr::null_mut()); - BigNumRef::from_ptr(n as *mut _) + BigNumRef::from_ptr(n.cast_mut()) } } @@ -402,7 +402,7 @@ where unsafe { let mut e = ptr::null(); RSA_get0_key(self.as_ptr(), ptr::null_mut(), &mut e, ptr::null_mut()); - BigNumRef::from_ptr(e as *mut _) + BigNumRef::from_ptr(e.cast_mut()) } } } diff --git a/boring/src/sha.rs b/boring/src/sha.rs index 4bea478b9..dd0671852 100644 --- a/boring/src/sha.rs +++ b/boring/src/sha.rs @@ -152,7 +152,7 @@ impl Sha1 { #[inline] pub fn update(&mut self, buf: &[u8]) { unsafe { - ffi::SHA1_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); + ffi::SHA1_Update(&mut self.0, buf.as_ptr().cast::(), buf.len()); } } @@ -197,7 +197,7 @@ impl Sha224 { #[inline] pub fn update(&mut self, buf: &[u8]) { unsafe { - ffi::SHA224_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); + ffi::SHA224_Update(&mut self.0, buf.as_ptr().cast::(), buf.len()); } } @@ -242,7 +242,7 @@ impl Sha256 { #[inline] pub fn update(&mut self, buf: &[u8]) { unsafe { - ffi::SHA256_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); + ffi::SHA256_Update(&mut self.0, buf.as_ptr().cast::(), buf.len()); } } @@ -287,7 +287,7 @@ impl Sha384 { #[inline] pub fn update(&mut self, buf: &[u8]) { unsafe { - ffi::SHA384_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); + ffi::SHA384_Update(&mut self.0, buf.as_ptr().cast::(), buf.len()); } } @@ -332,7 +332,7 @@ impl Sha512 { #[inline] pub fn update(&mut self, buf: &[u8]) { unsafe { - ffi::SHA512_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); + ffi::SHA512_Update(&mut self.0, buf.as_ptr().cast::(), buf.len()); } } @@ -377,7 +377,7 @@ impl Sha512_256 { #[inline] pub fn update(&mut self, buf: &[u8]) { unsafe { - ffi::SHA512_256_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); + ffi::SHA512_256_Update(&mut self.0, buf.as_ptr().cast::(), buf.len()); } } diff --git a/boring/src/sign.rs b/boring/src/sign.rs index f13d9150a..eea911e67 100644 --- a/boring/src/sign.rs +++ b/boring/src/sign.rs @@ -198,7 +198,7 @@ impl<'a> Signer<'a> { unsafe { cvt(ffi::EVP_PKEY_CTX_set_rsa_mgf1_md( self.pctx, - md.as_ptr() as *mut _, + md.as_ptr().cast_mut(), )) } } @@ -212,7 +212,7 @@ impl<'a> Signer<'a> { unsafe { cvt(ffi::EVP_DigestUpdate( self.md_ctx, - buf.as_ptr() as *const _, + buf.as_ptr().cast(), buf.len(), )) } @@ -251,7 +251,7 @@ impl<'a> Signer<'a> { let mut len = buf.len(); cvt(ffi::EVP_DigestSignFinal( self.md_ctx, - buf.as_mut_ptr() as *mut _, + buf.as_mut_ptr().cast(), &mut len, ))?; Ok(len) @@ -286,9 +286,9 @@ impl<'a> Signer<'a> { let mut sig_len = sig_buf.len(); cvt(ffi::EVP_DigestSign( self.md_ctx, - sig_buf.as_mut_ptr() as *mut _, + sig_buf.as_mut_ptr(), &mut sig_len, - data_buf.as_ptr() as *const _, + data_buf.as_ptr(), data_buf.len(), ))?; Ok(sig_len) @@ -441,7 +441,7 @@ impl<'a> Verifier<'a> { unsafe { cvt(ffi::EVP_PKEY_CTX_set_rsa_mgf1_md( self.pctx, - md.as_ptr() as *mut _, + md.as_ptr().cast_mut(), )) } } @@ -455,7 +455,7 @@ impl<'a> Verifier<'a> { unsafe { cvt(ffi::EVP_DigestUpdate( self.md_ctx, - buf.as_ptr() as *const _, + buf.as_ptr().cast(), buf.len(), )) } @@ -466,7 +466,7 @@ impl<'a> Verifier<'a> { pub fn verify(&self, signature: &[u8]) -> Result { unsafe { let r = - EVP_DigestVerifyFinal(self.md_ctx, signature.as_ptr() as *mut _, signature.len()); + EVP_DigestVerifyFinal(self.md_ctx, signature.as_ptr().cast_mut(), signature.len()); match r { 1 => Ok(true), 0 => { @@ -484,9 +484,9 @@ impl<'a> Verifier<'a> { unsafe { let r = ffi::EVP_DigestVerify( self.md_ctx, - signature.as_ptr() as *const _, + signature.as_ptr().cast(), signature.len(), - buf.as_ptr() as *const _, + buf.as_ptr().cast(), buf.len(), ); match r { diff --git a/boring/src/srtp.rs b/boring/src/srtp.rs index 8d23b722c..7dffc436e 100644 --- a/boring/src/srtp.rs +++ b/boring/src/srtp.rs @@ -27,7 +27,7 @@ impl SrtpProtectionProfileRef { #[must_use] pub fn name(&self) -> &'static str { - unsafe { CStr::from_ptr((*self.as_ptr()).name as *const _) } + unsafe { CStr::from_ptr((*self.as_ptr()).name.cast()) } .to_str() .expect("should be UTF-8") } diff --git a/boring/src/ssl/bio.rs b/boring/src/ssl/bio.rs index 4b3492faf..82695853f 100644 --- a/boring/src/ssl/bio.rs +++ b/boring/src/ssl/bio.rs @@ -44,7 +44,7 @@ pub fn new(stream: S) -> Result<(*mut BIO, BioMethod), ErrorSta unsafe { let bio = cvt_p(BIO_new(method.0.get()))?; - BIO_set_data(bio, Box::into_raw(state) as *mut _); + BIO_set_data(bio, Box::into_raw(state).cast()); BIO_set_init(bio, 1); Ok((bio, method)) @@ -76,7 +76,7 @@ pub unsafe extern "C" fn take_stream(bio: *mut BIO) -> S { assert!(!data.is_null()); - let state = Box::>::from_raw(data as *mut _); + let state = Box::>::from_raw(data.cast()); BIO_set_data(bio, ptr::null_mut()); @@ -91,7 +91,7 @@ pub unsafe fn set_dtls_mtu_size(bio: *mut BIO, mtu_size: usize) { } unsafe fn state<'a, S: 'a>(bio: *mut BIO) -> &'a mut StreamState { - let data = BIO_get_data(bio) as *mut StreamState; + let data = BIO_get_data(bio).cast::>(); assert!(!data.is_null()); @@ -102,7 +102,7 @@ unsafe extern "C" fn bwrite(bio: *mut BIO, buf: *const c_char, len: c_ BIO_clear_retry_flags(bio); let state = state::(bio); - let buf = slice::from_raw_parts(buf as *const _, len as usize); + let buf = slice::from_raw_parts(buf.cast(), len as usize); match catch_unwind(AssertUnwindSafe(|| state.stream.write(buf))) { Ok(Ok(len)) => len as c_int, @@ -124,7 +124,7 @@ unsafe extern "C" fn bread(bio: *mut BIO, buf: *mut c_char, len: c_int) BIO_clear_retry_flags(bio); let state = state::(bio); - let buf = slice::from_raw_parts_mut(buf as *mut _, len as usize); + let buf = slice::from_raw_parts_mut(buf.cast(), len as usize); match catch_unwind(AssertUnwindSafe(|| state.stream.read(buf))) { Ok(Ok(len)) => len as c_int, @@ -201,7 +201,7 @@ unsafe extern "C" fn destroy(bio: *mut BIO) -> c_int { let data = BIO_get_data(bio); if !data.is_null() { - drop(Box::>::from_raw(data as *mut _)); + drop(Box::>::from_raw(data.cast())); BIO_set_data(bio, ptr::null_mut()); } diff --git a/boring/src/ssl/callbacks.rs b/boring/src/ssl/callbacks.rs index f08409b3c..41c08c6b9 100644 --- a/boring/src/ssl/callbacks.rs +++ b/boring/src/ssl/callbacks.rs @@ -41,7 +41,7 @@ where // SAFETY: The callback won't outlive the context it's associated with // because there is no `X509StoreContextRef::ssl_mut(&mut self)` method. - let verify = unsafe { &*(verify as *const F) }; + let verify = unsafe { &*std::ptr::from_ref::(verify) }; c_int::from(verify(preverify_ok != 0, ctx)) } @@ -90,7 +90,7 @@ where // SAFETY: The callback won't outlive the context it's associated with // because there is no way to get a mutable reference to the `SslContext`, // so the callback can't replace itself. - let verify = unsafe { &*(verify as *const F) }; + let verify = unsafe { &*std::ptr::from_ref::(verify) }; c_int::from(verify(ctx)) } @@ -160,7 +160,7 @@ where // Give the callback mutable slices into which it can write the identity and psk. let identity_sl = - unsafe { slice::from_raw_parts_mut(identity as *mut u8, max_identity_len as usize) }; + unsafe { slice::from_raw_parts_mut(identity.cast::(), max_identity_len as usize) }; let psk_sl = unsafe { slice::from_raw_parts_mut(psk, max_psk_len as usize) }; let ssl_context = ssl.ssl_context().to_owned(); @@ -358,7 +358,7 @@ where match callback(ssl, protos) { Ok(proto) => { - *out = proto.as_ptr() as *const c_uchar; + *out = proto.as_ptr(); *outlen = proto.len() as c_uchar; ffi::SSL_TLSEXT_ERR_OK @@ -442,7 +442,7 @@ where // SAFETY: We can make `callback` outlive `ssl` because it is a callback // stored in the session context set in `Ssl::new` so it is always // guaranteed to outlive the lifetime of this function's scope. - let callback = unsafe { &*(callback as *const F) }; + let callback = unsafe { &*std::ptr::from_ref::(callback) }; callback(ssl, session); @@ -495,7 +495,7 @@ where // SAFETY: We can make `callback` outlive `ssl` because it is a callback // stored in the session context set in `Ssl::new` so it is always // guaranteed to outlive the lifetime of this function's scope. - let callback = unsafe { &*(callback as *const F) }; + let callback = unsafe { &*std::ptr::from_ref::(callback) }; match callback(ssl, data) { Ok(Some(session)) => { @@ -515,7 +515,7 @@ where F: Fn(&SslRef, &str) + 'static + Sync + Send, { // SAFETY: boring provides valid inputs. - let ssl = unsafe { SslRef::from_ptr(ssl as *mut _) }; + let ssl = unsafe { SslRef::from_ptr(ssl.cast_mut()) }; let line = unsafe { CStr::from_ptr(line).to_string_lossy() }; let callback = ssl @@ -625,7 +625,7 @@ pub(super) unsafe extern "C" fn raw_info_callback( { // Due to FFI signature requirements we have to pass a *const SSL into this function, but // foreign-types requires a *mut SSL to get the Rust SslRef - let mut_ref = ssl as *mut ffi::SSL; + let mut_ref = ssl.cast_mut(); // SAFETY: boring provides valid inputs. let ssl = unsafe { SslRef::from_ptr(mut_ref) }; diff --git a/boring/src/ssl/mod.rs b/boring/src/ssl/mod.rs index 5dee12e60..ff228ba9b 100644 --- a/boring/src/ssl/mod.rs +++ b/boring/src/ssl/mod.rs @@ -58,11 +58,11 @@ //! } //! ``` use foreign_types::{ForeignType, ForeignTypeRef, Opaque}; -use libc::{c_char, c_int, c_uchar, c_uint, c_void}; use openssl_macros::corresponds; use std::any::TypeId; use std::collections::HashMap; use std::convert::TryInto; +use std::ffi::{c_char, c_int, c_uchar, c_uint}; use std::ffi::{CStr, CString}; use std::fmt; use std::io; @@ -789,7 +789,7 @@ pub fn select_next_proto<'a>(server: &'a [u8], client: &'a [u8]) -> Option<&'a [ ); if r == ffi::OPENSSL_NPN_NEGOTIATED { - Some(slice::from_raw_parts(out as *const u8, outlen as usize)) + Some(slice::from_raw_parts(out.cast_const(), outlen as usize)) } else { None } @@ -1104,9 +1104,9 @@ impl SslContextBuilder { let callback_index = SslContext::cached_ex_index::(); self.ctx.replace_ex_data(callback_index, callback); + let callback = self.ctx.ex_data(callback_index).unwrap(); - let arg = self.ctx.ex_data(callback_index).unwrap() as *const F as *mut c_void; - + let arg = std::ptr::from_ref(callback).cast_mut().cast(); ffi::SSL_CTX_set_tlsext_servername_arg(self.as_ptr(), arg); ffi::SSL_CTX_set_tlsext_servername_callback(self.as_ptr(), Some(raw_sni::)); } @@ -1273,7 +1273,7 @@ impl SslContextBuilder { unsafe { cvt(ffi::SSL_CTX_load_verify_locations( self.as_ptr(), - file.as_ptr() as *const _, + file.as_ptr(), ptr::null(), )) } @@ -1340,7 +1340,7 @@ impl SslContextBuilder { unsafe { cvt(ffi::SSL_CTX_use_certificate_file( self.as_ptr(), - file.as_ptr() as *const _, + file.as_ptr(), file_type.as_raw(), )) } @@ -1361,7 +1361,7 @@ impl SslContextBuilder { unsafe { cvt(ffi::SSL_CTX_use_certificate_chain_file( self.as_ptr(), - file.as_ptr() as *const _, + file.as_ptr(), )) } } @@ -1400,7 +1400,7 @@ impl SslContextBuilder { unsafe { cvt(ffi::SSL_CTX_use_PrivateKey_file( self.as_ptr(), - file.as_ptr() as *const _, + file.as_ptr(), file_type.as_raw(), )) } @@ -1432,7 +1432,7 @@ impl SslContextBuilder { unsafe { cvt(ffi::SSL_CTX_set_cipher_list( self.as_ptr(), - cipher_list.as_ptr() as *const _, + cipher_list.as_ptr(), )) } } @@ -1453,7 +1453,7 @@ impl SslContextBuilder { unsafe { cvt(ffi::SSL_CTX_set_strict_cipher_list( self.as_ptr(), - cipher_list.as_ptr() as *const _, + cipher_list.as_ptr(), )) } } @@ -1962,7 +1962,7 @@ impl SslContextBuilder { unsafe { cvt_0i(ffi::SSL_CTX_set_verify_algorithm_prefs( self.as_ptr(), - prefs.as_ptr() as *const _, + prefs.as_ptr().cast(), prefs.len(), )) .map(|_| ()) @@ -1988,7 +1988,7 @@ impl SslContextBuilder { unsafe { cvt_0i(ffi::SSL_CTX_set1_curves_list( self.as_ptr(), - curves.as_ptr() as *const _, + curves.as_ptr(), )) .map(|_| ()) } @@ -2221,12 +2221,9 @@ impl SslContextRef { // this only from SslContextBuilder. #[corresponds(SSL_CTX_get_ex_data)] unsafe fn ex_data_mut(&mut self, index: Index) -> Option<&mut T> { - let data = ffi::SSL_CTX_get_ex_data(self.as_ptr(), index.as_raw()); - if data.is_null() { - None - } else { - Some(&mut *(data as *mut T)) - } + ffi::SSL_CTX_get_ex_data(self.as_ptr(), index.as_raw()) + .cast::() + .as_mut() } // Unsafe because SSL contexts are not guaranteed to be unique, we call @@ -2234,8 +2231,8 @@ impl SslContextRef { #[corresponds(SSL_CTX_set_ex_data)] unsafe fn set_ex_data(&mut self, index: Index, data: T) { unsafe { - let data = Box::into_raw(Box::new(data)) as *mut c_void; - ffi::SSL_CTX_set_ex_data(self.as_ptr(), index.as_raw(), data); + let data = Box::into_raw(Box::new(data)); + ffi::SSL_CTX_set_ex_data(self.as_ptr(), index.as_raw(), data.cast()); } } @@ -2458,7 +2455,7 @@ impl SslCipher { if ptr.is_null() { None } else { - Some(Self::from_ptr(ptr as *mut ffi::SSL_CIPHER)) + Some(Self::from_ptr(ptr.cast_mut())) } } } @@ -2541,7 +2538,7 @@ impl SslCipherRef { pub fn version(&self) -> &'static str { let version = unsafe { let ptr = ffi::SSL_CIPHER_get_version(self.as_ptr()); - CStr::from_ptr(ptr as *const _) + CStr::from_ptr(ptr) }; version.to_str().unwrap() @@ -2570,7 +2567,7 @@ impl SslCipherRef { // SSL_CIPHER_description requires a buffer of at least 128 bytes. let mut buf = [0; 128]; let ptr = ffi::SSL_CIPHER_description(self.as_ptr(), buf.as_mut_ptr(), 128); - CStr::from_ptr(ptr.cast()).to_string_lossy().into_owned() + CStr::from_ptr(ptr).to_string_lossy().into_owned() } } @@ -2900,13 +2897,7 @@ impl SslRef { #[corresponds(SSL_set1_curves_list)] pub fn set_curves_list(&mut self, curves: &str) -> Result<(), ErrorStack> { let curves = CString::new(curves).map_err(ErrorStack::internal_error)?; - unsafe { - cvt_0i(ffi::SSL_set1_curves_list( - self.as_ptr(), - curves.as_ptr() as *const _, - )) - .map(|_| ()) - } + unsafe { cvt_0i(ffi::SSL_set1_curves_list(self.as_ptr(), curves.as_ptr())).map(|_| ()) } } /// Returns the curve ID (aka group ID) used for this `SslRef`. @@ -3112,7 +3103,7 @@ impl SslRef { if ptr.is_null() { None } else { - Some(SslCipherRef::from_ptr(ptr as *mut _)) + Some(SslCipherRef::from_ptr(ptr.cast_mut())) } } } @@ -3125,7 +3116,7 @@ impl SslRef { pub fn state_string(&self) -> &'static str { let state = unsafe { let ptr = ffi::SSL_state_string(self.as_ptr()); - CStr::from_ptr(ptr as *const _) + CStr::from_ptr(ptr) }; state.to_str().unwrap_or_default() @@ -3139,7 +3130,7 @@ impl SslRef { pub fn state_string_long(&self) -> &'static str { let state = unsafe { let ptr = ffi::SSL_state_string_long(self.as_ptr()); - CStr::from_ptr(ptr as *const _) + CStr::from_ptr(ptr) }; state.to_str().unwrap_or_default() @@ -3151,9 +3142,7 @@ impl SslRef { #[corresponds(SSL_set_tlsext_host_name)] pub fn set_hostname(&mut self, hostname: &str) -> Result<(), ErrorStack> { let cstr = CString::new(hostname).map_err(ErrorStack::internal_error)?; - unsafe { - cvt(ffi::SSL_set_tlsext_host_name(self.as_ptr(), cstr.as_ptr() as *mut _) as c_int) - } + unsafe { cvt(ffi::SSL_set_tlsext_host_name(self.as_ptr(), cstr.as_ptr())) } } /// Returns the peer's certificate, if present. @@ -3248,7 +3237,7 @@ impl SslRef { pub fn version_str(&self) -> &'static str { let version = unsafe { let ptr = ffi::SSL_get_version(self.as_ptr()); - CStr::from_ptr(ptr as *const _) + CStr::from_ptr(ptr) }; version.to_str().unwrap() @@ -3356,7 +3345,7 @@ impl SslRef { if chain.is_null() { None } else { - Some(StackRef::from_ptr(chain as *mut _)) + Some(StackRef::from_ptr(chain.cast_mut())) } } } @@ -3373,7 +3362,7 @@ impl SslRef { if profile.is_null() { None } else { - Some(SrtpProtectionProfileRef::from_ptr(profile as *mut _)) + Some(SrtpProtectionProfileRef::from_ptr(profile.cast_mut())) } } } @@ -3422,7 +3411,7 @@ impl SslRef { if name.is_null() { None } else { - Some(CStr::from_ptr(name as *const _).to_bytes()) + Some(CStr::from_ptr(name).to_bytes()) } } } @@ -3492,9 +3481,7 @@ impl SslRef { /// value. #[corresponds(SSL_get_client_random)] pub fn client_random(&self, buf: &mut [u8]) -> usize { - unsafe { - ffi::SSL_get_client_random(self.as_ptr(), buf.as_mut_ptr() as *mut c_uchar, buf.len()) - } + unsafe { ffi::SSL_get_client_random(self.as_ptr(), buf.as_mut_ptr(), buf.len()) } } /// Copies the server_random value sent by the server in the TLS handshake into a buffer. @@ -3503,9 +3490,7 @@ impl SslRef { /// value. #[corresponds(SSL_get_server_random)] pub fn server_random(&self, buf: &mut [u8]) -> usize { - unsafe { - ffi::SSL_get_server_random(self.as_ptr(), buf.as_mut_ptr() as *mut c_uchar, buf.len()) - } + unsafe { ffi::SSL_get_server_random(self.as_ptr(), buf.as_mut_ptr(), buf.len()) } } /// Derives keying material for application use in accordance to RFC 5705. @@ -3518,14 +3503,14 @@ impl SslRef { ) -> Result<(), ErrorStack> { unsafe { let (context, contextlen, use_context) = match context { - Some(context) => (context.as_ptr() as *const c_uchar, context.len(), 1), + Some(context) => (context.as_ptr(), context.len(), 1), None => (ptr::null(), 0, 0), }; cvt(ffi::SSL_export_keying_material( self.as_ptr(), - out.as_mut_ptr() as *mut c_uchar, + out.as_mut_ptr(), out.len(), - label.as_ptr() as *const c_char, + label.as_ptr().cast::(), label.len(), context, contextlen, @@ -3609,17 +3594,12 @@ impl SslRef { pub fn set_ex_data(&mut self, index: Index, data: T) { if let Some(old) = self.ex_data_mut(index) { *old = data; - return; } unsafe { - let data = Box::new(data); - ffi::SSL_set_ex_data( - self.as_ptr(), - index.as_raw(), - Box::into_raw(data) as *mut c_void, - ); + let data = Box::into_raw(Box::new(data)); + ffi::SSL_set_ex_data(self.as_ptr(), index.as_raw(), data.cast()); } } @@ -3658,12 +3638,9 @@ impl SslRef { #[corresponds(SSL_get_ex_data)] pub fn ex_data_mut(&mut self, index: Index) -> Option<&mut T> { unsafe { - let data = ffi::SSL_get_ex_data(self.as_ptr(), index.as_raw()); - if data.is_null() { - None - } else { - Some(&mut *(data as *mut T)) - } + ffi::SSL_get_ex_data(self.as_ptr(), index.as_raw()) + .cast::() + .as_mut() } } @@ -3673,7 +3650,7 @@ impl SslRef { /// buffer required. #[corresponds(SSL_get_finished)] pub fn finished(&self, buf: &mut [u8]) -> usize { - unsafe { ffi::SSL_get_finished(self.as_ptr(), buf.as_mut_ptr() as *mut c_void, buf.len()) } + unsafe { ffi::SSL_get_finished(self.as_ptr(), buf.as_mut_ptr().cast(), buf.len()) } } /// Copies the contents of the last Finished message received from the peer into the provided @@ -3683,9 +3660,7 @@ impl SslRef { /// buffer required. #[corresponds(SSL_get_peer_finished)] pub fn peer_finished(&self, buf: &mut [u8]) -> usize { - unsafe { - ffi::SSL_get_peer_finished(self.as_ptr(), buf.as_mut_ptr() as *mut c_void, buf.len()) - } + unsafe { ffi::SSL_get_peer_finished(self.as_ptr(), buf.as_mut_ptr().cast(), buf.len()) } } /// Determines if the initial handshake has been completed. @@ -3812,7 +3787,7 @@ impl SslRef { if data.is_null() { None } else { - Some(slice::from_raw_parts(data as *const u8, len)) + Some(slice::from_raw_parts(data.cast::(), len)) } } } diff --git a/boring/src/stack.rs b/boring/src/stack.rs index 5a95d98c4..622c401ea 100644 --- a/boring/src/stack.rs +++ b/boring/src/stack.rs @@ -48,7 +48,7 @@ impl Drop for Stack { fn drop(&mut self) { unsafe { while self.pop().is_some() {} - OPENSSL_sk_free(self.0 as *mut _); + OPENSSL_sk_free(self.0.cast()); } } } @@ -58,7 +58,7 @@ impl Stack { unsafe { ffi::init(); let ptr = cvt_p(OPENSSL_sk_new_null())?; - Ok(Stack(ptr as *mut _)) + Ok(Stack(ptr.cast())) } } } @@ -132,7 +132,7 @@ impl Drop for IntoIter { fn drop(&mut self) { unsafe { for _ in &mut *self {} - OPENSSL_sk_free(self.stack as *mut _); + OPENSSL_sk_free(self.stack.cast()); } } } @@ -144,7 +144,7 @@ impl Iterator for IntoIter { unsafe { self.idxs .next() - .map(|i| T::from_ptr(OPENSSL_sk_value(self.stack as *mut _, i) as *mut _)) + .map(|i| T::from_ptr(OPENSSL_sk_value(self.stack.cast(), i).cast())) } } @@ -158,7 +158,7 @@ impl DoubleEndedIterator for IntoIter { unsafe { self.idxs .next_back() - .map(|i| T::from_ptr(OPENSSL_sk_value(self.stack as *mut _, i) as *mut _)) + .map(|i| T::from_ptr(OPENSSL_sk_value(self.stack.cast(), i).cast())) } } } @@ -176,7 +176,7 @@ unsafe impl ForeignTypeRef for StackRef { impl StackRef { fn as_stack(&self) -> *mut OPENSSL_STACK { - self.as_ptr() as *mut _ + self.as_ptr().cast() } /// Returns the number of items in the stack. @@ -234,7 +234,7 @@ impl StackRef { /// Pushes a value onto the top of the stack. pub fn push(&mut self, data: T) -> Result<(), ErrorStack> { unsafe { - cvt_0(OPENSSL_sk_push(self.as_stack(), data.as_ptr() as *mut _))?; + cvt_0(OPENSSL_sk_push(self.as_stack(), data.as_ptr().cast()))?; mem::forget(data); Ok(()) } @@ -247,13 +247,13 @@ impl StackRef { if ptr.is_null() { None } else { - Some(T::from_ptr(ptr as *mut _)) + Some(T::from_ptr(ptr.cast())) } } } unsafe fn _get(&self, idx: usize) -> *mut T::CType { - OPENSSL_sk_value(self.as_stack(), idx) as *mut _ + OPENSSL_sk_value(self.as_stack(), idx).cast() } } @@ -323,7 +323,7 @@ impl<'a, T: Stackable> Iterator for Iter<'a, T> { unsafe { self.idxs .next() - .map(|i| T::Ref::from_ptr(OPENSSL_sk_value(self.stack.as_stack(), i) as *mut _)) + .map(|i| T::Ref::from_ptr(OPENSSL_sk_value(self.stack.as_stack(), i).cast())) } } @@ -337,7 +337,7 @@ impl<'a, T: Stackable> DoubleEndedIterator for Iter<'a, T> { unsafe { self.idxs .next_back() - .map(|i| T::Ref::from_ptr(OPENSSL_sk_value(self.stack.as_stack(), i) as *mut _)) + .map(|i| T::Ref::from_ptr(OPENSSL_sk_value(self.stack.as_stack(), i).cast())) } } } @@ -357,7 +357,7 @@ impl<'a, T: Stackable> Iterator for IterMut<'a, T> { unsafe { self.idxs .next() - .map(|i| T::Ref::from_ptr_mut(OPENSSL_sk_value(self.stack.as_stack(), i) as *mut _)) + .map(|i| T::Ref::from_ptr_mut(OPENSSL_sk_value(self.stack.as_stack(), i).cast())) } } @@ -371,7 +371,7 @@ impl<'a, T: Stackable> DoubleEndedIterator for IterMut<'a, T> { unsafe { self.idxs .next_back() - .map(|i| T::Ref::from_ptr_mut(OPENSSL_sk_value(self.stack.as_stack(), i) as *mut _)) + .map(|i| T::Ref::from_ptr_mut(OPENSSL_sk_value(self.stack.as_stack(), i).cast())) } } } diff --git a/boring/src/string.rs b/boring/src/string.rs index 4527f0d7d..94b2d48d1 100644 --- a/boring/src/string.rs +++ b/boring/src/string.rs @@ -1,6 +1,6 @@ use crate::ffi; use foreign_types::ForeignTypeRef; -use libc::{c_char, c_void}; +use libc::c_char; use std::convert::AsRef; use std::ffi::CStr; use std::fmt; @@ -83,5 +83,5 @@ impl fmt::Debug for OpensslStringRef { } unsafe fn free(buf: *mut c_char) { - crate::ffi::OPENSSL_free(buf as *mut c_void); + crate::ffi::OPENSSL_free(buf.cast()); } diff --git a/boring/src/symm.rs b/boring/src/symm.rs index 877b4a1a0..816e84a8a 100644 --- a/boring/src/symm.rs +++ b/boring/src/symm.rs @@ -417,7 +417,6 @@ impl Crypter { key.len() as c_uint, ))?; - let key = key.as_ptr() as *mut _; let iv = match (iv, t.iv_len()) { (Some(iv), Some(len)) => { if iv.len() != len { @@ -429,7 +428,7 @@ impl Crypter { ptr::null_mut(), ))?; } - iv.as_ptr() as *mut _ + iv.as_ptr().cast_mut() } (Some(_) | None, None) => ptr::null_mut(), (None, Some(_)) => panic!("an IV is required for this cipher"), @@ -438,7 +437,7 @@ impl Crypter { crypter.ctx, ptr::null(), ptr::null_mut(), - key, + key.as_ptr().cast_mut(), iv, mode, ))?; @@ -468,7 +467,7 @@ impl Crypter { self.ctx, ffi::EVP_CTRL_GCM_SET_TAG, tag.len() as c_int, - tag.as_ptr() as *mut _, + tag.as_ptr().cast_mut().cast(), )) } } @@ -608,7 +607,7 @@ impl Crypter { self.ctx, ffi::EVP_CTRL_GCM_GET_TAG, tag.len() as c_int, - tag.as_mut_ptr() as *mut _, + tag.as_mut_ptr().cast(), )) } } diff --git a/boring/src/util.rs b/boring/src/util.rs index d34fd8984..6583f0aaf 100644 --- a/boring/src/util.rs +++ b/boring/src/util.rs @@ -46,10 +46,10 @@ pub unsafe extern "C" fn invoke_passwd_cb( where F: FnOnce(&mut [u8]) -> Result, { - let callback = &mut *(cb_state as *mut CallbackState); + let callback = &mut *cb_state.cast::>(); let result = panic::catch_unwind(AssertUnwindSafe(|| { - let pass_slice = slice::from_raw_parts_mut(buf as *mut u8, size as usize); + let pass_slice = slice::from_raw_parts_mut(buf.cast::(), size as usize); callback.cb.take().unwrap()(pass_slice) })); @@ -80,14 +80,14 @@ impl ForeignTypeExt for FT {} pub trait ForeignTypeRefExt: ForeignTypeRef { unsafe fn from_const_ptr<'a>(ptr: *const Self::CType) -> &'a Self { - Self::from_ptr(ptr as *mut Self::CType) + Self::from_ptr(ptr.cast_mut()) } unsafe fn from_const_ptr_opt<'a>(ptr: *const Self::CType) -> Option<&'a Self> { if ptr.is_null() { None } else { - Some(Self::from_const_ptr(ptr as *mut Self::CType)) + Some(Self::from_const_ptr(ptr.cast_mut())) } } } diff --git a/boring/src/x509/mod.rs b/boring/src/x509/mod.rs index bd5884332..49372e26f 100644 --- a/boring/src/x509/mod.rs +++ b/boring/src/x509/mod.rs @@ -105,12 +105,9 @@ impl X509StoreContextRef { #[must_use] pub fn ex_data(&self, index: Index) -> Option<&T> { unsafe { - let data = ffi::X509_STORE_CTX_get_ex_data(self.as_ptr(), index.as_raw()); - if data.is_null() { - None - } else { - Some(&*(data as *const T)) - } + ffi::X509_STORE_CTX_get_ex_data(self.as_ptr(), index.as_raw()) + .cast::() + .as_ref() } } @@ -118,12 +115,9 @@ impl X509StoreContextRef { #[corresponds(X509_STORE_CTX_get_ex_data)] pub fn ex_data_mut(&mut self, index: Index) -> Option<&mut T> { unsafe { - let data = ffi::X509_STORE_CTX_get_ex_data(self.as_ptr(), index.as_raw()); - if data.is_null() { - None - } else { - Some(&mut *(data as *mut T)) - } + ffi::X509_STORE_CTX_get_ex_data(self.as_ptr(), index.as_raw()) + .cast::() + .as_mut() } } @@ -145,7 +139,7 @@ impl X509StoreContextRef { ffi::X509_STORE_CTX_set_ex_data( self.as_ptr(), index.as_raw(), - Box::into_raw(data) as *mut c_void, + Box::into_raw(data).cast(), ); } } @@ -546,7 +540,7 @@ impl X509Ref { if stack.is_null() { None } else { - Some(Stack::from_ptr(stack as *mut _)) + Some(Stack::from_ptr(stack.cast())) } } } @@ -575,7 +569,7 @@ impl X509Ref { if stack.is_null() { None } else { - Some(Stack::from_ptr(stack as *mut _)) + Some(Stack::from_ptr(stack.cast())) } } } @@ -620,7 +614,7 @@ impl X509Ref { cvt(ffi::X509_digest( self.as_ptr(), hash_type.as_ptr(), - digest.buf.as_mut_ptr() as *mut _, + digest.buf.as_mut_ptr(), &mut len, ))?; digest.len = len as usize; @@ -664,7 +658,7 @@ impl X509Ref { let mut signature = ptr::null(); X509_get0_signature(&mut signature, ptr::null_mut(), self.as_ptr()); assert!(!signature.is_null()); - Asn1BitStringRef::from_ptr(signature as *mut _) + Asn1BitStringRef::from_ptr(signature.cast_mut()) } } @@ -676,7 +670,7 @@ impl X509Ref { let mut algor = ptr::null(); X509_get0_signature(ptr::null_mut(), &mut algor, self.as_ptr()); assert!(!algor.is_null()); - X509AlgorithmRef::from_ptr(algor as *mut _) + X509AlgorithmRef::from_ptr(algor.cast_mut()) } } @@ -725,7 +719,7 @@ impl X509Ref { unsafe { cvt_n(ffi::X509_check_host( self.as_ptr(), - host.as_ptr() as _, + host.as_ptr().cast(), host.len(), 0, std::ptr::null_mut(), @@ -877,7 +871,7 @@ pub struct X509v3Context<'a>(ffi::X509V3_CTX, PhantomData<(&'a X509Ref, &'a Conf impl X509v3Context<'_> { #[must_use] pub fn as_ptr(&self) -> *mut ffi::X509V3_CTX { - &self.0 as *const _ as *mut _ + std::ptr::addr_of!(self.0).cast_mut() } } @@ -932,8 +926,8 @@ impl X509Extension { &mut ctx } }; - let name = name.as_ptr() as *mut _; - let value = value.as_ptr() as *mut _; + let name = name.as_ptr().cast_mut(); + let value = value.as_ptr().cast_mut(); cvt_p(ffi::X509V3_EXT_nconf(conf, context_ptr, name, value)) .map(|p| X509Extension::from_ptr(p)) @@ -978,7 +972,7 @@ impl X509Extension { } }; let name = name.as_raw(); - let value = value.as_ptr() as *mut _; + let value = value.as_ptr().cast_mut(); cvt_p(ffi::X509V3_EXT_nconf_nid(conf, context_ptr, name, value)) .map(|p| X509Extension::from_ptr(p)) @@ -1024,7 +1018,7 @@ impl X509NameBuilder { assert!(value.len() <= ValueLen::MAX as usize); cvt(ffi::X509_NAME_add_entry_by_txt( self.0.as_ptr(), - field.as_ptr() as *mut _, + field.as_ptr().cast_mut(), ffi::MBSTRING_UTF8, value.as_ptr(), value.len() as ValueLen, @@ -1047,7 +1041,7 @@ impl X509NameBuilder { assert!(value.len() <= ValueLen::MAX as usize); cvt(ffi::X509_NAME_add_entry_by_txt( self.0.as_ptr(), - field.as_ptr() as *mut _, + field.as_ptr().cast_mut(), ty.as_raw(), value.as_ptr(), value.len() as ValueLen, @@ -1066,7 +1060,7 @@ impl X509NameBuilder { self.0.as_ptr(), field.as_raw(), ffi::MBSTRING_UTF8, - value.as_ptr() as *mut _, + value.as_ptr().cast_mut(), value.len() as ValueLen, -1, 0, @@ -1088,7 +1082,7 @@ impl X509NameBuilder { self.0.as_ptr(), field.as_raw(), ty.as_raw(), - value.as_ptr() as *mut _, + value.as_ptr().cast_mut(), value.len() as ValueLen, -1, 0, @@ -1756,7 +1750,7 @@ impl X509AlgorithmRef { let mut oid = ptr::null(); X509_ALGOR_get0(&mut oid, ptr::null_mut(), ptr::null_mut(), self.as_ptr()); assert!(!oid.is_null()); - Asn1ObjectRef::from_ptr(oid as *mut _) + Asn1ObjectRef::from_ptr(oid.cast_mut()) } } } @@ -1799,7 +1793,7 @@ use crate::ffi::X509_OBJECT_get0_X509; #[allow(bad_style)] unsafe fn X509_OBJECT_free(x: *mut ffi::X509_OBJECT) { ffi::X509_OBJECT_free_contents(x); - ffi::OPENSSL_free(x as *mut libc::c_void); + ffi::OPENSSL_free(x.cast()); } unsafe fn get_new_x509_store_ctx_idx(f: ffi::CRYPTO_EX_free) -> c_int { diff --git a/boring/src/x509/verify.rs b/boring/src/x509/verify.rs index 8f3c5942a..3ca7ca9d1 100644 --- a/boring/src/x509/verify.rs +++ b/boring/src/x509/verify.rs @@ -120,7 +120,7 @@ impl X509VerifyParamRef { let raw_host = if host.is_empty() { "\0" } else { host }; cvt(ffi::X509_VERIFY_PARAM_set1_host( self.as_ptr(), - raw_host.as_ptr() as *const _, + raw_host.as_ptr().cast(), host.len(), )) } @@ -134,7 +134,7 @@ impl X509VerifyParamRef { let raw_email = if email.is_empty() { "\0" } else { email }; cvt(ffi::X509_VERIFY_PARAM_set1_email( self.as_ptr(), - raw_email.as_ptr() as *const _, + raw_email.as_ptr().cast(), email.len(), )) } @@ -157,7 +157,7 @@ impl X509VerifyParamRef { }; cvt(ffi::X509_VERIFY_PARAM_set1_ip( self.as_ptr(), - buf.as_ptr() as *const _, + buf.as_ptr().cast(), len, )) }