From 8773f0e1faca1352abb1e1b421c327149f36bf86 Mon Sep 17 00:00:00 2001 From: Kornel Date: Wed, 1 Oct 2025 03:38:59 +0100 Subject: [PATCH] Use Ref foreign type instead of forgetting --- boring/src/hmac.rs | 8 +++----- boring/src/ssl/callbacks.rs | 16 ++++++++-------- boring/src/ssl/mod.rs | 8 ++++---- boring/src/ssl/test/session_resumption.rs | 12 ++++++------ boring/src/symm.rs | 4 ++-- 5 files changed, 23 insertions(+), 25 deletions(-) diff --git a/boring/src/hmac.rs b/boring/src/hmac.rs index 4a593877..9816fb57 100644 --- a/boring/src/hmac.rs +++ b/boring/src/hmac.rs @@ -1,9 +1,7 @@ use crate::cvt; use crate::error::ErrorStack; +use crate::foreign_types::ForeignTypeRef; use crate::hash::MessageDigest; -use std::ffi::c_void; - -use foreign_types::ForeignType; foreign_type_and_impl_send_sync! { type CType = ffi::HMAC_CTX; @@ -12,7 +10,7 @@ foreign_type_and_impl_send_sync! { pub struct HmacCtx; } -impl HmacCtx { +impl HmacCtxRef { /// Configures HmacCtx to use `md` as the hash function and `key` as the key. /// /// https://commondatastorage.googleapis.com/chromium-boringssl-docs/hmac.h.html#HMAC_Init_ex @@ -26,7 +24,7 @@ impl HmacCtx { unsafe { cvt(ffi::HMAC_Init_ex( self.as_ptr(), - key.as_ptr() as *const c_void, + key.as_ptr().cast(), key.len(), md.as_ptr(), // ENGINE api is deprecated diff --git a/boring/src/ssl/callbacks.rs b/boring/src/ssl/callbacks.rs index 5598b6af..ed724f79 100644 --- a/boring/src/ssl/callbacks.rs +++ b/boring/src/ssl/callbacks.rs @@ -8,15 +8,15 @@ use super::{ }; use crate::error::ErrorStack; use crate::ffi; -use crate::hmac::HmacCtx; +use crate::hmac::HmacCtxRef; use crate::ssl::TicketKeyCallbackResult; -use crate::symm::CipherCtx; +use crate::symm::CipherCtxRef; use crate::x509::{X509StoreContext, X509StoreContextRef}; use foreign_types::ForeignType; use foreign_types::ForeignTypeRef; use libc::{c_char, c_int, c_uchar, c_uint, c_void}; use std::ffi::CStr; -use std::mem::{ManuallyDrop, MaybeUninit}; +use std::mem::MaybeUninit; use std::ptr; use std::slice; use std::str; @@ -290,8 +290,8 @@ where &SslRef, &mut [u8; 16], &mut [u8; ffi::EVP_MAX_IV_LENGTH as usize], - &mut CipherCtx, - &mut HmacCtx, + &mut CipherCtxRef, + &mut HmacCtxRef, bool, ) -> TicketKeyCallbackResult + 'static @@ -328,10 +328,10 @@ where let iv = unsafe { iv.assume_init_mut() }; // The EVP_CIPHER_CTX and HMAC_CTX are owned by boringSSL. - let mut evp_ctx = ManuallyDrop::new(unsafe { CipherCtx::from_ptr(evp_ctx) }); - let mut hmac_ctx = ManuallyDrop::new(unsafe { HmacCtx::from_ptr(hmac_ctx) }); + let evp_ctx = unsafe { CipherCtxRef::from_ptr_mut(evp_ctx) }; + let hmac_ctx = unsafe { HmacCtxRef::from_ptr_mut(hmac_ctx) }; - callback(ssl, key_name, iv, &mut evp_ctx, &mut hmac_ctx, encrypt).into() + callback(ssl, key_name, iv, evp_ctx, hmac_ctx, encrypt).into() } pub(super) unsafe extern "C" fn raw_alpn_select( diff --git a/boring/src/ssl/mod.rs b/boring/src/ssl/mod.rs index 1be720ac..19688c39 100644 --- a/boring/src/ssl/mod.rs +++ b/boring/src/ssl/mod.rs @@ -81,7 +81,7 @@ use crate::dh::DhRef; use crate::ec::EcKeyRef; use crate::error::ErrorStack; use crate::ex_data::Index; -use crate::hmac::HmacCtx; +use crate::hmac::HmacCtxRef; use crate::nid::Nid; use crate::pkey::{HasPrivate, PKeyRef, Params, Private}; use crate::srtp::{SrtpProtectionProfile, SrtpProtectionProfileRef}; @@ -89,7 +89,7 @@ use crate::ssl::bio::BioMethod; use crate::ssl::callbacks::*; use crate::ssl::error::InnerError; use crate::stack::{Stack, StackRef, Stackable}; -use crate::symm::CipherCtx; +use crate::symm::CipherCtxRef; use crate::x509::store::{X509Store, X509StoreBuilder, X509StoreBuilderRef, X509StoreRef}; use crate::x509::verify::X509VerifyParamRef; use crate::x509::{ @@ -1158,8 +1158,8 @@ impl SslContextBuilder { &SslRef, &mut [u8; 16], &mut [u8; ffi::EVP_MAX_IV_LENGTH as usize], - &mut CipherCtx, - &mut HmacCtx, + &mut CipherCtxRef, + &mut HmacCtxRef, bool, ) -> TicketKeyCallbackResult + 'static diff --git a/boring/src/ssl/test/session_resumption.rs b/boring/src/ssl/test/session_resumption.rs index 2eb62116..f7f481db 100644 --- a/boring/src/ssl/test/session_resumption.rs +++ b/boring/src/ssl/test/session_resumption.rs @@ -1,12 +1,12 @@ use super::server::Server; use crate::ssl::test::MessageDigest; -use crate::ssl::HmacCtx; +use crate::ssl::HmacCtxRef; use crate::ssl::SslRef; use crate::ssl::SslSession; use crate::ssl::SslSessionCacheMode; use crate::ssl::TicketKeyCallbackResult; use crate::symm::Cipher; -use crate::symm::CipherCtx; +use crate::symm::CipherCtxRef; use std::sync::atomic::{AtomicU8, Ordering}; use std::sync::OnceLock; @@ -148,8 +148,8 @@ fn test_noop_tickey_key_callback( _ssl: &SslRef, key_name: &mut [u8; 16], iv: &mut [u8; ffi::EVP_MAX_IV_LENGTH as usize], - evp_ctx: &mut CipherCtx, - hmac_ctx: &mut HmacCtx, + evp_ctx: &mut CipherCtxRef, + hmac_ctx: &mut HmacCtxRef, encrypt: bool, ) -> TicketKeyCallbackResult { // These should only be used for testing purposes. @@ -188,8 +188,8 @@ fn test_success_tickey_key_callback( _ssl: &SslRef, key_name: &mut [u8; 16], iv: &mut [u8; ffi::EVP_MAX_IV_LENGTH as usize], - evp_ctx: &mut CipherCtx, - hmac_ctx: &mut HmacCtx, + evp_ctx: &mut CipherCtxRef, + hmac_ctx: &mut HmacCtxRef, encrypt: bool, ) -> TicketKeyCallbackResult { // These should only be used for testing purposes. diff --git a/boring/src/symm.rs b/boring/src/symm.rs index 831430f5..1a2dc599 100644 --- a/boring/src/symm.rs +++ b/boring/src/symm.rs @@ -53,7 +53,7 @@ //! ``` use crate::ffi; -use foreign_types::ForeignType; +use foreign_types::ForeignTypeRef; use libc::{c_int, c_uint}; use openssl_macros::corresponds; use std::cmp; @@ -76,7 +76,7 @@ foreign_type_and_impl_send_sync! { pub struct CipherCtx; } -impl CipherCtx { +impl CipherCtxRef { /// Configures CipherCtx for a fresh encryption operation using `cipher`. /// /// https://commondatastorage.googleapis.com/chromium-boringssl-docs/cipher.h.html#EVP_EncryptInit_ex