Skip to content

Commit 6d711ac

Browse files
committed
Use MaybeUninit for raw_ticket_key
1 parent 91c95bb commit 6d711ac

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

boring/src/ssl/callbacks.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use crate::ssl::TicketKeyCallbackResult;
1212
use crate::x509::{X509StoreContext, X509StoreContextRef};
1313
use foreign_types::ForeignType;
1414
use foreign_types::ForeignTypeRef;
15-
use libc::c_char;
16-
use libc::{c_int, c_uchar, c_uint, c_void};
15+
use libc::{c_char, c_int, c_uchar, c_uint, c_void};
1716
use std::ffi::CStr;
17+
use std::mem::MaybeUninit;
1818
use std::ptr;
1919
use std::slice;
2020
use std::str;
@@ -270,6 +270,11 @@ where
270270
}
271271
}
272272

273+
unsafe fn to_uninit<'a, T: 'a>(ptr: *mut T) -> &'a mut MaybeUninit<T> {
274+
assert!(!ptr.is_null());
275+
unsafe { &mut *ptr.cast::<MaybeUninit<T>>() }
276+
}
277+
273278
pub(super) unsafe extern "C" fn raw_ticket_key<F>(
274279
ssl: *mut ffi::SSL,
275280
key_name: *mut u8,
@@ -283,8 +288,8 @@ where
283288
&SslRef,
284289
&mut [u8; 16],
285290
&mut [u8; ffi::EVP_MAX_IV_LENGTH as usize],
286-
*mut ffi::EVP_CIPHER_CTX,
287-
*mut ffi::HMAC_CTX,
291+
&mut MaybeUninit<ffi::EVP_CIPHER_CTX>,
292+
&mut MaybeUninit<ffi::HMAC_CTX>,
288293
bool,
289294
) -> TicketKeyCallbackResult
290295
+ 'static
@@ -294,32 +299,34 @@ where
294299
// SAFETY: boring provides valid inputs.
295300
let ssl = unsafe { SslRef::from_ptr_mut(ssl) };
296301

302+
let evp_ctx = unsafe { to_uninit(evp_ctx) };
303+
let hmac_ctx = unsafe { to_uninit(hmac_ctx) };
304+
297305
let ssl_context = ssl.ssl_context().to_owned();
298306
let callback = ssl_context
299307
.ex_data::<F>(SslContext::cached_ex_index::<F>())
300308
.expect("expected session resumption callback");
301309

302310
// SAFETY: the callback guarantees that key_name is 16 bytes
303311
let key_name =
304-
unsafe { slice::from_raw_parts_mut(key_name, ffi::SSL_TICKET_KEY_NAME_LEN as usize) };
305-
let key_name = <&mut [u8; 16]>::try_from(key_name).expect("boring provides a 16-byte key name");
312+
unsafe { to_uninit(key_name.cast::<[u8; ffi::SSL_TICKET_KEY_NAME_LEN as usize]>()) };
306313

307314
// SAFETY: the callback provides 16 bytes iv
308315
//
309316
// https://github.com/google/boringssl/blob/main/ssl/ssl_session.cc#L331
310-
let iv = unsafe { core::slice::from_raw_parts_mut(iv, ffi::EVP_MAX_IV_LENGTH as usize) };
311-
let iv = <&mut [u8; ffi::EVP_MAX_IV_LENGTH as usize]>::try_from(iv)
312-
.expect("boring provides a 16-byte iv");
317+
let iv = unsafe { to_uninit(iv.cast::<[u8; ffi::EVP_MAX_IV_LENGTH as usize]>()) };
313318

314319
// When encrypting a new ticket, encrypt will be one.
315320
let encrypt = encrypt == 1;
316321

317322
// Zero-initialize the key_name and iv, since the application is expected to populate these
318323
// fields in the encrypt mode.
319324
if encrypt {
320-
unsafe { ptr::write(key_name, [0; 16]) };
321-
unsafe { ptr::write(iv, [0; ffi::EVP_MAX_IV_LENGTH as usize]) };
325+
*key_name = MaybeUninit::zeroed();
326+
*iv = MaybeUninit::zeroed();
322327
}
328+
let key_name = unsafe { key_name.assume_init_mut() };
329+
let iv = unsafe { iv.assume_init_mut() };
323330

324331
callback(ssl, key_name, iv, evp_ctx, hmac_ctx, encrypt).into()
325332
}

boring/src/ssl/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,8 +1254,8 @@ impl SslContextBuilder {
12541254
&SslRef,
12551255
&mut [u8; 16],
12561256
&mut [u8; ffi::EVP_MAX_IV_LENGTH as usize],
1257-
*mut ffi::EVP_CIPHER_CTX,
1258-
*mut ffi::HMAC_CTX,
1257+
&mut MaybeUninit<ffi::EVP_CIPHER_CTX>,
1258+
&mut MaybeUninit<ffi::HMAC_CTX>,
12591259
bool,
12601260
) -> TicketKeyCallbackResult
12611261
+ 'static

boring/src/ssl/test/session_resumption.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::ssl::SslSessionCacheMode;
66
use crate::ssl::TicketKeyCallbackResult;
77
use crate::symm::Cipher;
88
use std::ffi::c_void;
9+
use std::mem::MaybeUninit;
910
use std::sync::atomic::{AtomicU8, Ordering};
1011
use std::sync::OnceLock;
1112

@@ -147,8 +148,8 @@ fn test_noop_tickey_key_callback(
147148
_ssl: &SslRef,
148149
key_name: &mut [u8; 16],
149150
iv: &mut [u8; ffi::EVP_MAX_IV_LENGTH as usize],
150-
evp_ctx: *mut ffi::EVP_CIPHER_CTX,
151-
hmac_ctx: *mut ffi::HMAC_CTX,
151+
evp_ctx: &mut MaybeUninit<ffi::EVP_CIPHER_CTX>,
152+
hmac_ctx: &mut MaybeUninit<ffi::HMAC_CTX>,
152153
encrypt: bool,
153154
) -> TicketKeyCallbackResult {
154155
// These should only be used for testing purposes.
@@ -167,7 +168,7 @@ fn test_noop_tickey_key_callback(
167168
// Set the encryption context.
168169
let ret = unsafe {
169170
ffi::EVP_EncryptInit_ex(
170-
evp_ctx,
171+
evp_ctx.as_mut_ptr(),
171172
cipher.as_ptr(),
172173
// ENGINE api is deprecated
173174
core::ptr::null_mut(),
@@ -180,7 +181,7 @@ fn test_noop_tickey_key_callback(
180181
// Set the hmac context.
181182
let ret = unsafe {
182183
ffi::HMAC_Init_ex(
183-
hmac_ctx,
184+
hmac_ctx.as_mut_ptr(),
184185
TEST_HMAC_KEY.as_ptr() as *const c_void,
185186
TEST_HMAC_KEY.len(),
186187
digest.as_ptr(),
@@ -202,8 +203,8 @@ fn test_success_tickey_key_callback(
202203
_ssl: &SslRef,
203204
key_name: &mut [u8; 16],
204205
iv: &mut [u8; ffi::EVP_MAX_IV_LENGTH as usize],
205-
evp_ctx: *mut ffi::EVP_CIPHER_CTX,
206-
hmac_ctx: *mut ffi::HMAC_CTX,
206+
evp_ctx: &mut MaybeUninit<ffi::EVP_CIPHER_CTX>,
207+
hmac_ctx: &mut MaybeUninit<ffi::HMAC_CTX>,
207208
encrypt: bool,
208209
) -> TicketKeyCallbackResult {
209210
// These should only be used for testing purposes.
@@ -222,7 +223,7 @@ fn test_success_tickey_key_callback(
222223
// Set the encryption context.
223224
let ret = unsafe {
224225
ffi::EVP_EncryptInit_ex(
225-
evp_ctx,
226+
evp_ctx.as_mut_ptr(),
226227
cipher.as_ptr(),
227228
// ENGINE api is deprecated
228229
core::ptr::null_mut(),
@@ -235,7 +236,7 @@ fn test_success_tickey_key_callback(
235236
// Set the hmac context.
236237
let ret = unsafe {
237238
ffi::HMAC_Init_ex(
238-
hmac_ctx,
239+
hmac_ctx.as_mut_ptr(),
239240
TEST_HMAC_KEY.as_ptr() as *const c_void,
240241
TEST_HMAC_KEY.len(),
241242
digest.as_ptr(),
@@ -249,7 +250,7 @@ fn test_success_tickey_key_callback(
249250
// Set the decryption context.
250251
let ret = unsafe {
251252
ffi::EVP_DecryptInit_ex(
252-
evp_ctx,
253+
evp_ctx.as_mut_ptr(),
253254
cipher.as_ptr(),
254255
// ENGINE api is deprecated
255256
core::ptr::null_mut(),
@@ -262,7 +263,7 @@ fn test_success_tickey_key_callback(
262263
// Set the hmac context.
263264
let ret = unsafe {
264265
ffi::HMAC_Init_ex(
265-
hmac_ctx,
266+
hmac_ctx.as_mut_ptr(),
266267
TEST_HMAC_KEY.as_ptr() as *const c_void,
267268
TEST_HMAC_KEY.len(),
268269
digest.as_ptr(),

0 commit comments

Comments
 (0)