Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2023, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | */ |
| 15 | |
| 16 | #![deny( |
| 17 | missing_docs, |
Nabil Wadih | fa7afff | 2023-04-14 13:21:48 -0700 | [diff] [blame] | 18 | unsafe_op_in_unsafe_fn, |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 19 | clippy::indexing_slicing, |
| 20 | clippy::unwrap_used, |
| 21 | clippy::panic, |
| 22 | clippy::expect_used |
| 23 | )] |
Alice Wang | cfcb954 | 2023-09-21 14:02:07 +0000 | [diff] [blame] | 24 | #![cfg_attr(not(any(feature = "std", test)), no_std)] |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 25 | |
David Benjamin | 2a72f97 | 2023-06-07 12:21:18 -0400 | [diff] [blame] | 26 | //! Rust BoringSSL bindings |
Alice Wang | cfcb954 | 2023-09-21 14:02:07 +0000 | [diff] [blame] | 27 | extern crate alloc; |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 28 | |
| 29 | extern crate core; |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 30 | |
Adam Langley | 6a4c717 | 2024-01-06 15:47:00 -0800 | [diff] [blame] | 31 | use alloc::vec::Vec; |
Adam Langley | ec6a405 | 2024-01-05 14:55:24 -0800 | [diff] [blame] | 32 | use core::ffi::c_void; |
| 33 | |
Adam Langley | 677414b | 2024-01-05 15:50:16 -0800 | [diff] [blame] | 34 | #[macro_use] |
| 35 | mod macros; |
| 36 | |
Nabil Wadih | 7991692 | 2023-05-19 09:04:11 -0700 | [diff] [blame] | 37 | pub mod aead; |
| 38 | |
Nabil Wadih | 39da68f | 2023-02-16 18:07:41 -0800 | [diff] [blame] | 39 | pub mod aes; |
| 40 | |
Nabil Wadih | c6c9c38 | 2023-06-05 17:55:37 -0700 | [diff] [blame] | 41 | /// Ciphers. |
| 42 | pub mod cipher; |
| 43 | |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 44 | pub mod digest; |
| 45 | |
David Benjamin | 2a72f97 | 2023-06-07 12:21:18 -0400 | [diff] [blame] | 46 | /// Ed25519, a signature scheme. |
Nabil Wadih | b0a026f | 2023-05-12 09:30:10 -0700 | [diff] [blame] | 47 | pub mod ed25519; |
| 48 | |
Nabil Wadih | cc57542 | 2023-02-22 17:50:58 -0800 | [diff] [blame] | 49 | pub mod hkdf; |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 50 | |
Nabil Wadih | cc57542 | 2023-02-22 17:50:58 -0800 | [diff] [blame] | 51 | pub mod hmac; |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 52 | |
Maurice Lam | f7629e1 | 2023-06-01 02:07:47 +0000 | [diff] [blame] | 53 | pub mod x25519; |
| 54 | |
Adam Langley | a8e5e34 | 2024-01-06 16:28:22 -0800 | [diff] [blame] | 55 | pub mod ec; |
Maurice Lam | 37be47b | 2023-06-01 02:07:35 +0000 | [diff] [blame] | 56 | pub mod ecdh; |
Adam Langley | 169128d | 2024-01-07 17:47:37 -0800 | [diff] [blame^] | 57 | pub mod ecdsa; |
Maurice Lam | 37be47b | 2023-06-01 02:07:35 +0000 | [diff] [blame] | 58 | |
Adam Langley | a8e5e34 | 2024-01-06 16:28:22 -0800 | [diff] [blame] | 59 | mod scoped; |
| 60 | |
| 61 | mod mem; |
| 62 | pub use mem::constant_time_compare; |
Maurice Lam | 37be47b | 2023-06-01 02:07:35 +0000 | [diff] [blame] | 63 | |
Adam Langley | 929518a | 2024-01-08 12:22:26 -0800 | [diff] [blame] | 64 | mod rand; |
| 65 | pub use rand::{rand_array, rand_bytes}; |
| 66 | |
Nabil Wadih | 92de195 | 2023-03-20 09:58:31 -0700 | [diff] [blame] | 67 | #[cfg(test)] |
| 68 | mod test_helpers; |
| 69 | |
Adam Langley | 518172c | 2024-01-06 10:41:48 -0800 | [diff] [blame] | 70 | /// Error type for when a "signature" (either a public-key signature or a MAC) |
| 71 | /// is incorrect. |
| 72 | #[derive(Debug)] |
| 73 | pub struct InvalidSignatureError; |
| 74 | |
Adam Langley | ec6a405 | 2024-01-05 14:55:24 -0800 | [diff] [blame] | 75 | /// FfiSlice exists to provide `as_ffi_ptr` on slices. Calling `as_ptr` on an |
| 76 | /// empty Rust slice may return the alignment of the type, rather than NULL, as |
| 77 | /// the pointer. When passing pointers into C/C++ code, that is not a valid |
| 78 | /// pointer. Thus this method should be used whenever passing a pointer to a |
| 79 | /// slice into BoringSSL code. |
| 80 | trait FfiSlice { |
| 81 | fn as_ffi_ptr(&self) -> *const u8; |
| 82 | fn as_ffi_void_ptr(&self) -> *const c_void { |
| 83 | self.as_ffi_ptr() as *const c_void |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | impl FfiSlice for [u8] { |
| 88 | fn as_ffi_ptr(&self) -> *const u8 { |
| 89 | if self.is_empty() { |
| 90 | core::ptr::null() |
| 91 | } else { |
| 92 | self.as_ptr() |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl<const N: usize> FfiSlice for [u8; N] { |
| 98 | fn as_ffi_ptr(&self) -> *const u8 { |
| 99 | if N == 0 { |
| 100 | core::ptr::null() |
| 101 | } else { |
| 102 | self.as_ptr() |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /// See the comment [`FfiSlice`]. |
| 108 | trait FfiMutSlice { |
| 109 | fn as_mut_ffi_ptr(&mut self) -> *mut u8; |
| 110 | fn as_ffi_void_ptr(&mut self) -> *mut c_void { |
| 111 | self.as_mut_ffi_ptr() as *mut c_void |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl FfiMutSlice for [u8] { |
| 116 | fn as_mut_ffi_ptr(&mut self) -> *mut u8 { |
| 117 | if self.is_empty() { |
| 118 | core::ptr::null_mut() |
| 119 | } else { |
| 120 | self.as_mut_ptr() |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | impl<const N: usize> FfiMutSlice for [u8; N] { |
| 126 | fn as_mut_ffi_ptr(&mut self) -> *mut u8 { |
| 127 | if N == 0 { |
| 128 | core::ptr::null_mut() |
| 129 | } else { |
| 130 | self.as_mut_ptr() |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Nabil Wadih | cc57542 | 2023-02-22 17:50:58 -0800 | [diff] [blame] | 135 | /// This is a helper struct which provides functions for passing slices over FFI. |
Adam Langley | ec6a405 | 2024-01-05 14:55:24 -0800 | [diff] [blame] | 136 | /// |
| 137 | /// Deprecated: use `FfiSlice` which adds less noise and lets one grep for `as_ptr` |
| 138 | /// as a sign of something to check. |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 139 | struct CSlice<'a>(&'a [u8]); |
| 140 | |
Nabil Wadih | cc57542 | 2023-02-22 17:50:58 -0800 | [diff] [blame] | 141 | impl<'a> From<&'a [u8]> for CSlice<'a> { |
| 142 | fn from(value: &'a [u8]) -> Self { |
| 143 | Self(value) |
| 144 | } |
| 145 | } |
| 146 | |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 147 | impl CSlice<'_> { |
Nabil Wadih | cc57542 | 2023-02-22 17:50:58 -0800 | [diff] [blame] | 148 | /// Returns a raw pointer to the value, which is safe to pass over FFI. |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 149 | pub fn as_ptr<T>(&self) -> *const T { |
| 150 | if self.0.is_empty() { |
Alice Wang | cfcb954 | 2023-09-21 14:02:07 +0000 | [diff] [blame] | 151 | core::ptr::null() |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 152 | } else { |
| 153 | self.0.as_ptr() as *const T |
| 154 | } |
| 155 | } |
Nabil Wadih | b0a026f | 2023-05-12 09:30:10 -0700 | [diff] [blame] | 156 | |
| 157 | pub fn len(&self) -> usize { |
| 158 | self.0.len() |
| 159 | } |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Nabil Wadih | cc57542 | 2023-02-22 17:50:58 -0800 | [diff] [blame] | 162 | /// This is a helper struct which provides functions for passing mutable slices over FFI. |
Adam Langley | ec6a405 | 2024-01-05 14:55:24 -0800 | [diff] [blame] | 163 | /// |
| 164 | /// Deprecated: use `FfiMutSlice` which adds less noise and lets one grep for |
| 165 | /// `as_ptr` as a sign of something to check. |
Nabil Wadih | cc57542 | 2023-02-22 17:50:58 -0800 | [diff] [blame] | 166 | struct CSliceMut<'a>(&'a mut [u8]); |
| 167 | |
| 168 | impl CSliceMut<'_> { |
| 169 | /// Returns a raw pointer to the value, which is safe to pass over FFI. |
| 170 | pub fn as_mut_ptr<T>(&mut self) -> *mut T { |
| 171 | if self.0.is_empty() { |
Alice Wang | cfcb954 | 2023-09-21 14:02:07 +0000 | [diff] [blame] | 172 | core::ptr::null_mut() |
Nabil Wadih | cc57542 | 2023-02-22 17:50:58 -0800 | [diff] [blame] | 173 | } else { |
| 174 | self.0.as_mut_ptr() as *mut T |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | pub fn len(&self) -> usize { |
| 179 | self.0.len() |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | impl<'a> From<&'a mut [u8]> for CSliceMut<'a> { |
| 184 | fn from(value: &'a mut [u8]) -> Self { |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 185 | Self(value) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /// A helper trait implemented by types which reference borrowed foreign types. |
| 190 | /// |
| 191 | /// # Safety |
| 192 | /// |
| 193 | /// Implementations of `ForeignTypeRef` must guarantee the following: |
| 194 | /// |
| 195 | /// - `Self::from_ptr(x).as_ptr() == x` |
Maurice Lam | 37be47b | 2023-06-01 02:07:35 +0000 | [diff] [blame] | 196 | /// - `Self::from_ptr_mut(x).as_ptr() == x` |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 197 | unsafe trait ForeignTypeRef: Sized { |
| 198 | /// The raw C type. |
| 199 | type CType; |
| 200 | |
| 201 | /// Constructs a shared instance of this type from its raw type. |
| 202 | /// |
| 203 | /// # Safety |
| 204 | /// |
| 205 | /// `ptr` must be a valid, immutable, instance of the type for the `'a` lifetime. |
| 206 | #[inline] |
| 207 | unsafe fn from_ptr<'a>(ptr: *mut Self::CType) -> &'a Self { |
| 208 | debug_assert!(!ptr.is_null()); |
Nabil Wadih | fa7afff | 2023-04-14 13:21:48 -0700 | [diff] [blame] | 209 | unsafe { &*(ptr as *mut _) } |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /// Constructs a mutable reference of this type from its raw type. |
| 213 | /// |
| 214 | /// # Safety |
| 215 | /// |
| 216 | /// `ptr` must be a valid, unique, instance of the type for the `'a` lifetime. |
| 217 | #[inline] |
| 218 | unsafe fn from_ptr_mut<'a>(ptr: *mut Self::CType) -> &'a mut Self { |
| 219 | debug_assert!(!ptr.is_null()); |
Nabil Wadih | fa7afff | 2023-04-14 13:21:48 -0700 | [diff] [blame] | 220 | unsafe { &mut *(ptr as *mut _) } |
Nabil Wadih | be79283 | 2023-02-14 11:46:37 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /// Returns a raw pointer to the wrapped value. |
| 224 | #[inline] |
| 225 | fn as_ptr(&self) -> *mut Self::CType { |
| 226 | self as *const _ as *mut _ |
| 227 | } |
| 228 | } |
Maurice Lam | 37be47b | 2023-06-01 02:07:35 +0000 | [diff] [blame] | 229 | |
| 230 | /// A helper trait implemented by types which has an owned reference to foreign types. |
| 231 | /// |
| 232 | /// # Safety |
| 233 | /// |
| 234 | /// Implementations of `ForeignType` must guarantee the following: |
| 235 | /// |
| 236 | /// - `Self::from_ptr(x).as_ptr() == x` |
| 237 | unsafe trait ForeignType { |
| 238 | /// The raw C type. |
| 239 | type CType; |
| 240 | |
| 241 | /// Constructs an instance of this type from its raw type. |
| 242 | /// |
| 243 | /// # Safety |
| 244 | /// |
| 245 | /// - `ptr` must be a valid, immutable, instance of `CType`. |
| 246 | /// - Ownership of `ptr` is passed to the implementation, and will free `ptr` when dropped. |
| 247 | unsafe fn from_ptr(ptr: *mut Self::CType) -> Self; |
| 248 | |
| 249 | /// Returns a raw pointer to the wrapped value. |
| 250 | fn as_ptr(&self) -> *mut Self::CType; |
| 251 | } |
Adam Langley | ec6a405 | 2024-01-05 14:55:24 -0800 | [diff] [blame] | 252 | |
Adam Langley | 677414b | 2024-01-05 15:50:16 -0800 | [diff] [blame] | 253 | /// Returns a BoringSSL structure that is initialized by some function. |
| 254 | /// Requires that the given function completely initializes the value. |
| 255 | /// |
| 256 | /// (Tagged `unsafe` because a no-op argument would otherwise expose |
| 257 | /// uninitialized memory.) |
| 258 | unsafe fn initialized_struct<T, F>(init: F) -> T |
| 259 | where |
| 260 | F: FnOnce(*mut T), |
| 261 | { |
| 262 | let mut out_uninit = core::mem::MaybeUninit::<T>::uninit(); |
| 263 | init(out_uninit.as_mut_ptr()); |
| 264 | unsafe { out_uninit.assume_init() } |
| 265 | } |
| 266 | |
Adam Langley | 6a4c717 | 2024-01-06 15:47:00 -0800 | [diff] [blame] | 267 | /// Returns a BoringSSL structure that is initialized by some function. |
| 268 | /// Requires that the given function completely initializes the value or else |
Adam Langley | 470b9cb | 2024-01-06 16:26:19 -0800 | [diff] [blame] | 269 | /// returns false. |
Adam Langley | 6a4c717 | 2024-01-06 15:47:00 -0800 | [diff] [blame] | 270 | /// |
| 271 | /// (Tagged `unsafe` because a no-op argument would otherwise expose |
| 272 | /// uninitialized memory.) |
| 273 | unsafe fn initialized_struct_fallible<T, F>(init: F) -> Option<T> |
| 274 | where |
Adam Langley | 470b9cb | 2024-01-06 16:26:19 -0800 | [diff] [blame] | 275 | F: FnOnce(*mut T) -> bool, |
Adam Langley | 6a4c717 | 2024-01-06 15:47:00 -0800 | [diff] [blame] | 276 | { |
| 277 | let mut out_uninit = core::mem::MaybeUninit::<T>::uninit(); |
Adam Langley | 470b9cb | 2024-01-06 16:26:19 -0800 | [diff] [blame] | 278 | if init(out_uninit.as_mut_ptr()) { |
Adam Langley | 6a4c717 | 2024-01-06 15:47:00 -0800 | [diff] [blame] | 279 | Some(unsafe { out_uninit.assume_init() }) |
| 280 | } else { |
| 281 | None |
| 282 | } |
| 283 | } |
| 284 | |
Adam Langley | ec6a405 | 2024-01-05 14:55:24 -0800 | [diff] [blame] | 285 | /// Wrap a closure that initializes an output buffer and return that buffer as |
| 286 | /// an array. Requires that the closure fully initialize the given buffer. |
| 287 | /// |
| 288 | /// Safety: the closure must fully initialize the array. |
| 289 | unsafe fn with_output_array<const N: usize, F>(func: F) -> [u8; N] |
| 290 | where |
| 291 | F: FnOnce(*mut u8, usize), |
| 292 | { |
| 293 | let mut out_uninit = core::mem::MaybeUninit::<[u8; N]>::uninit(); |
| 294 | let out_ptr = if N != 0 { |
| 295 | out_uninit.as_mut_ptr() as *mut u8 |
| 296 | } else { |
| 297 | core::ptr::null_mut() |
| 298 | }; |
| 299 | func(out_ptr, N); |
| 300 | // Safety: `func` promises to fill all of `out_uninit`. |
| 301 | unsafe { out_uninit.assume_init() } |
| 302 | } |
| 303 | |
| 304 | /// Wrap a closure that initializes an output buffer and return that buffer as |
| 305 | /// an array. The closure returns a [`core::ffi::c_int`] and, if the return value |
| 306 | /// is not one, then the initialization is assumed to have failed and [None] is |
| 307 | /// returned. Otherwise, this function requires that the closure fully |
| 308 | /// initialize the given buffer. |
| 309 | /// |
| 310 | /// Safety: the closure must fully initialize the array if it returns one. |
| 311 | unsafe fn with_output_array_fallible<const N: usize, F>(func: F) -> Option<[u8; N]> |
| 312 | where |
Adam Langley | 0eac348 | 2024-01-18 12:43:33 -0800 | [diff] [blame] | 313 | F: FnOnce(*mut u8, usize) -> bool, |
Adam Langley | ec6a405 | 2024-01-05 14:55:24 -0800 | [diff] [blame] | 314 | { |
| 315 | let mut out_uninit = core::mem::MaybeUninit::<[u8; N]>::uninit(); |
| 316 | let out_ptr = if N != 0 { |
| 317 | out_uninit.as_mut_ptr() as *mut u8 |
| 318 | } else { |
| 319 | core::ptr::null_mut() |
| 320 | }; |
Adam Langley | 0eac348 | 2024-01-18 12:43:33 -0800 | [diff] [blame] | 321 | if func(out_ptr, N) { |
Adam Langley | ec6a405 | 2024-01-05 14:55:24 -0800 | [diff] [blame] | 322 | // Safety: `func` promises to fill all of `out_uninit` if it returns one. |
| 323 | unsafe { Some(out_uninit.assume_init()) } |
| 324 | } else { |
| 325 | None |
| 326 | } |
| 327 | } |
Adam Langley | 677414b | 2024-01-05 15:50:16 -0800 | [diff] [blame] | 328 | |
Adam Langley | 6a4c717 | 2024-01-06 15:47:00 -0800 | [diff] [blame] | 329 | /// Wrap a closure that writes at most `max_output` bytes to fill a vector. |
| 330 | /// It must return the number of bytes written. |
| 331 | #[allow(clippy::unwrap_used)] |
| 332 | unsafe fn with_output_vec<F>(max_output: usize, func: F) -> Vec<u8> |
| 333 | where |
| 334 | F: FnOnce(*mut u8) -> usize, |
| 335 | { |
| 336 | unsafe { |
| 337 | with_output_vec_fallible(max_output, |out_buf| Some(func(out_buf))) |
| 338 | // The closure cannot fail and thus neither can |
| 339 | // `with_output_array_fallible`. |
| 340 | .unwrap() |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /// Wrap a closure that writes at most `max_output` bytes to fill a vector. |
| 345 | /// If successful, it must return the number of bytes written. |
| 346 | unsafe fn with_output_vec_fallible<F>(max_output: usize, func: F) -> Option<Vec<u8>> |
| 347 | where |
| 348 | F: FnOnce(*mut u8) -> Option<usize>, |
| 349 | { |
| 350 | let mut ret = Vec::with_capacity(max_output); |
| 351 | let out = ret.spare_capacity_mut(); |
| 352 | let out_buf = out |
| 353 | .get_mut(0) |
| 354 | .map_or(core::ptr::null_mut(), |x| x.as_mut_ptr()); |
| 355 | |
| 356 | let num_written = func(out_buf)?; |
| 357 | assert!(num_written <= ret.capacity()); |
| 358 | |
| 359 | unsafe { |
| 360 | // Safety: `num_written` bytes have been written to. |
| 361 | ret.set_len(num_written); |
| 362 | } |
| 363 | |
| 364 | Some(ret) |
| 365 | } |
| 366 | |
Adam Langley | a8e5e34 | 2024-01-06 16:28:22 -0800 | [diff] [blame] | 367 | /// Buffer represents an owned chunk of memory on the BoringSSL heap. |
| 368 | /// Call `as_ref()` to get a `&[u8]` from it. |
| 369 | pub struct Buffer { |
| 370 | // This pointer is always allocated by BoringSSL and must be freed using |
| 371 | // `OPENSSL_free`. |
| 372 | pub(crate) ptr: *mut u8, |
| 373 | pub(crate) len: usize, |
| 374 | } |
| 375 | |
| 376 | impl AsRef<[u8]> for Buffer { |
| 377 | fn as_ref(&self) -> &[u8] { |
| 378 | if self.len == 0 { |
| 379 | return &[]; |
| 380 | } |
| 381 | // Safety: `ptr` and `len` describe a valid area of memory and `ptr` |
| 382 | // must be Rust-valid because `len` is non-zero. |
| 383 | unsafe { core::slice::from_raw_parts(self.ptr, self.len) } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | impl Drop for Buffer { |
| 388 | fn drop(&mut self) { |
| 389 | // Safety: `ptr` is owned by this object and is on the BoringSSL heap. |
| 390 | unsafe { |
| 391 | bssl_sys::OPENSSL_free(self.ptr as *mut core::ffi::c_void); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /// Calls `parse_func` with a `CBS` structure pointing at `data`. |
| 397 | /// If that returns a null pointer then it returns [None]. |
| 398 | /// Otherwise, if there's still data left in CBS, it calls `free_func` on the |
| 399 | /// pointer and returns [None]. Otherwise it returns the pointer. |
| 400 | fn parse_with_cbs<T, Parse, Free>(data: &[u8], free_func: Free, parse_func: Parse) -> Option<*mut T> |
| 401 | where |
| 402 | Parse: FnOnce(*mut bssl_sys::CBS) -> *mut T, |
| 403 | Free: FnOnce(*mut T), |
| 404 | { |
| 405 | // Safety: type checking ensures that `cbs` is the correct size. |
| 406 | let mut cbs = |
| 407 | unsafe { initialized_struct(|cbs| bssl_sys::CBS_init(cbs, data.as_ffi_ptr(), data.len())) }; |
| 408 | let ptr = parse_func(&mut cbs); |
| 409 | if ptr.is_null() { |
| 410 | return None; |
| 411 | } |
| 412 | // Safety: `cbs` is still valid after parsing. |
| 413 | if unsafe { bssl_sys::CBS_len(&cbs) } != 0 { |
| 414 | // Safety: `ptr` is still owned by this function. |
| 415 | free_func(ptr); |
| 416 | return None; |
| 417 | } |
| 418 | Some(ptr) |
| 419 | } |
| 420 | |
| 421 | /// Calls `func` with a `CBB` pointer and returns a [Buffer] of the ultimate |
| 422 | /// contents of that CBB. |
| 423 | #[allow(clippy::unwrap_used)] |
| 424 | fn cbb_to_buffer<F: FnOnce(*mut bssl_sys::CBB)>(initial_capacity: usize, func: F) -> Buffer { |
| 425 | // Safety: type checking ensures that `cbb` is the correct size. |
| 426 | let mut cbb = unsafe { |
| 427 | initialized_struct_fallible(|cbb| bssl_sys::CBB_init(cbb, initial_capacity) == 1) |
| 428 | } |
| 429 | // `CBB_init` only fails if out of memory, which isn't something that this crate handles. |
| 430 | .unwrap(); |
| 431 | func(&mut cbb); |
| 432 | |
| 433 | let mut ptr: *mut u8 = core::ptr::null_mut(); |
| 434 | let mut len: usize = 0; |
| 435 | // `CBB_finish` only fails on programming error, which we convert into a |
| 436 | // panic. |
| 437 | assert_eq!(1, unsafe { |
| 438 | bssl_sys::CBB_finish(&mut cbb, &mut ptr, &mut len) |
| 439 | }); |
| 440 | |
| 441 | // Safety: `ptr` is on the BoringSSL heap and ownership is returned by |
| 442 | // `CBB_finish`. |
| 443 | Buffer { ptr, len } |
| 444 | } |
| 445 | |
Adam Langley | 677414b | 2024-01-05 15:50:16 -0800 | [diff] [blame] | 446 | /// Used to prevent external implementations of internal traits. |
| 447 | mod sealed { |
| 448 | pub struct Sealed; |
| 449 | } |