Skip to content

Commit 91c95bb

Browse files
committed
initialize key_name and iv. mark fn as _unsafe to allow for future changes to the api
1 parent cdeeae6 commit 91c95bb

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

boring/src/ssl/callbacks.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ where
282282
F: Fn(
283283
&SslRef,
284284
&mut [u8; 16],
285-
*mut u8,
285+
&mut [u8; ffi::EVP_MAX_IV_LENGTH as usize],
286286
*mut ffi::EVP_CIPHER_CTX,
287287
*mut ffi::HMAC_CTX,
288288
bool,
@@ -304,9 +304,23 @@ where
304304
unsafe { slice::from_raw_parts_mut(key_name, ffi::SSL_TICKET_KEY_NAME_LEN as usize) };
305305
let key_name = <&mut [u8; 16]>::try_from(key_name).expect("boring provides a 16-byte key name");
306306

307+
// SAFETY: the callback provides 16 bytes iv
308+
//
309+
// 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");
313+
307314
// When encrypting a new ticket, encrypt will be one.
308315
let encrypt = encrypt == 1;
309316

317+
// Zero-initialize the key_name and iv, since the application is expected to populate these
318+
// fields in the encrypt mode.
319+
if encrypt {
320+
unsafe { ptr::write(key_name, [0; 16]) };
321+
unsafe { ptr::write(iv, [0; ffi::EVP_MAX_IV_LENGTH as usize]) };
322+
}
323+
310324
callback(ssl, key_name, iv, evp_ctx, hmac_ctx, encrypt).into()
311325
}
312326

boring/src/ssl/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,13 +1240,20 @@ impl SslContextBuilder {
12401240
/// # Panics
12411241
///
12421242
/// This method panics if this `Ssl` is associated with a RPK context.
1243+
///
1244+
/// # Safety
1245+
///
1246+
/// The application is responsible for correctly setting the key_name, iv, encryption context
1247+
/// and hmac context. See the [`SSL_CTX_set_tlsext_ticket_key_cb`] docs for additional info.
1248+
///
1249+
/// [`SSL_CTX_set_tlsext_ticket_key_cb`]: https://commondatastorage.googleapis.com/chromium-boringssl-docs/ssl.h.html#SSL_CTX_set_tlsext_ticket_key_cb
12431250
#[corresponds(SSL_CTX_set_tlsext_ticket_key_cb)]
1244-
pub fn set_ticket_key_callback<F>(&mut self, callback: F)
1251+
pub unsafe fn set_ticket_key_callback_unsafe<F>(&mut self, callback: F)
12451252
where
12461253
F: Fn(
12471254
&SslRef,
12481255
&mut [u8; 16],
1249-
*mut u8,
1256+
&mut [u8; ffi::EVP_MAX_IV_LENGTH as usize],
12501257
*mut ffi::EVP_CIPHER_CTX,
12511258
*mut ffi::HMAC_CTX,
12521259
bool,

boring/src/ssl/test/session_resumption.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ fn custom_callback_success() {
5757

5858
let mut server = Server::builder();
5959
server.expected_connections_count(2);
60-
server
61-
.ctx()
62-
.set_ticket_key_callback(test_success_tickey_key_callback);
60+
unsafe {
61+
server
62+
.ctx()
63+
.set_ticket_key_callback_unsafe(test_success_tickey_key_callback)
64+
};
6365
let server = server.build();
6466

6567
let mut client = server.client();
@@ -100,9 +102,11 @@ fn custom_callback_unrecognized_decryption_ticket() {
100102

101103
let mut server = Server::builder();
102104
server.expected_connections_count(2);
103-
server
104-
.ctx()
105-
.set_ticket_key_callback(test_noop_tickey_key_callback);
105+
unsafe {
106+
server
107+
.ctx()
108+
.set_ticket_key_callback_unsafe(test_noop_tickey_key_callback)
109+
};
106110
let server = server.build();
107111

108112
let mut client = server.client();
@@ -141,21 +145,24 @@ fn custom_callback_unrecognized_decryption_ticket() {
141145
// TicketKeyCallbackResult::Noop in decryption mode.
142146
fn test_noop_tickey_key_callback(
143147
_ssl: &SslRef,
144-
_key_name: &mut [u8; 16],
145-
_iv: *mut u8,
148+
key_name: &mut [u8; 16],
149+
iv: &mut [u8; ffi::EVP_MAX_IV_LENGTH as usize],
146150
evp_ctx: *mut ffi::EVP_CIPHER_CTX,
147151
hmac_ctx: *mut ffi::HMAC_CTX,
148152
encrypt: bool,
149153
) -> TicketKeyCallbackResult {
150154
// These should only be used for testing purposes.
151-
const TEST_CBC_IV: [u8; 16] = [1; 16];
155+
const TEST_CBC_IV: [u8; ffi::EVP_MAX_IV_LENGTH as usize] = [1; ffi::EVP_MAX_IV_LENGTH as usize];
152156
const TEST_AES_128_CBC_KEY: [u8; 16] = [2; 16];
153157
const TEST_HMAC_KEY: [u8; 32] = [3; 32];
154158

155159
let digest = MessageDigest::sha256();
156160
let cipher = Cipher::aes_128_cbc();
157161

158162
if encrypt {
163+
assert_eq!(key_name, &[0; 16]);
164+
assert_eq!(iv, &[0; 16]);
165+
159166
NOOP_ENCRYPTION_CALLED_BACK.fetch_add(1, Ordering::SeqCst);
160167
// Set the encryption context.
161168
let ret = unsafe {
@@ -193,21 +200,24 @@ fn test_noop_tickey_key_callback(
193200
// Custom callback to encrypt and decrypt session tickets
194201
fn test_success_tickey_key_callback(
195202
_ssl: &SslRef,
196-
_key_name: &mut [u8; 16],
197-
_iv: *mut u8,
203+
key_name: &mut [u8; 16],
204+
iv: &mut [u8; ffi::EVP_MAX_IV_LENGTH as usize],
198205
evp_ctx: *mut ffi::EVP_CIPHER_CTX,
199206
hmac_ctx: *mut ffi::HMAC_CTX,
200207
encrypt: bool,
201208
) -> TicketKeyCallbackResult {
202209
// These should only be used for testing purposes.
203-
const TEST_CBC_IV: [u8; 16] = [1; 16];
210+
const TEST_CBC_IV: [u8; ffi::EVP_MAX_IV_LENGTH as usize] = [1; ffi::EVP_MAX_IV_LENGTH as usize];
204211
const TEST_AES_128_CBC_KEY: [u8; 16] = [2; 16];
205212
const TEST_HMAC_KEY: [u8; 32] = [3; 32];
206213

207214
let digest = MessageDigest::sha256();
208215
let cipher = Cipher::aes_128_cbc();
209216

210217
if encrypt {
218+
assert_eq!(key_name, &[0; 16]);
219+
assert_eq!(iv, &[0; 16]);
220+
211221
SUCCESS_ENCRYPTION_CALLED_BACK.fetch_add(1, Ordering::SeqCst);
212222
// Set the encryption context.
213223
let ret = unsafe {
@@ -236,6 +246,7 @@ fn test_success_tickey_key_callback(
236246
assert!(ret == 1);
237247
} else {
238248
SUCCESS_DECRYPTION_CALLED_BACK.fetch_add(1, Ordering::SeqCst);
249+
// Set the decryption context.
239250
let ret = unsafe {
240251
ffi::EVP_DecryptInit_ex(
241252
evp_ctx,

0 commit comments

Comments
 (0)