Skip to content

Commit 4614ba4

Browse files
committed
1 parent fcaed96 commit 4614ba4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2002
-665
lines changed

boring-sys/build/main.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::env;
33
use std::ffi::OsString;
44
use std::fs;
55
use std::io;
6-
use std::io::Write;
76
use std::path::{Path, PathBuf};
87
use std::process::{Command, Output};
98
use std::sync::OnceLock;
@@ -281,8 +280,8 @@ fn get_boringssl_cmake_config(config: &Config) -> cmake::Config {
281280
boringssl_cmake.define("CMAKE_TOOLCHAIN_FILE", toolchain_file);
282281

283282
// 21 is the minimum level tested. You can give higher value.
284-
boringssl_cmake.define("ANDROID_NATIVE_API_LEVEL", "21");
285-
boringssl_cmake.define("ANDROID_STL", "c++_shared");
283+
boringssl_cmake.define("ANDROID_NATIVE_API_LEVEL", "21"); // NOTE: cloudflare uses CMAKE_SYSTEM_VERSION
284+
boringssl_cmake.define("ANDROID_STL", "c++_shared"); // NOTE: cloudflare uses CMAKE_ANDROID_STL_TYPE
286285
}
287286

288287
"macos" => {
@@ -457,25 +456,18 @@ fn get_extra_clang_args_for_bindgen(config: &Config) -> Vec<String> {
457456
// When cross-compiling for Apple targets, tell bindgen to use SDK sysroot,
458457
// and *don't* use system headers of the host macOS.
459458
let sdk = get_apple_sdk_name(config);
460-
let output = std::process::Command::new("xcrun")
461-
.args(["--show-sdk-path", "--sdk", sdk])
462-
.output()
463-
.unwrap();
464-
if !output.status.success() {
465-
if let Some(exit_code) = output.status.code() {
466-
println!("cargo:warning=xcrun failed: exit code {exit_code}");
467-
} else {
468-
println!("cargo:warning=xcrun failed: killed");
459+
match run_command(Command::new("xcrun").args(["--show-sdk-path", "--sdk", sdk])) {
460+
Ok(output) => {
461+
let sysroot = std::str::from_utf8(&output.stdout).expect("xcrun output");
462+
params.push("-isysroot".to_string());
463+
// There is typically a newline at the end which confuses clang.
464+
params.push(sysroot.trim_end().to_string());
465+
}
466+
Err(e) => {
467+
println!("cargo:warning={e}");
468+
// Uh... let's try anyway, I guess?
469469
}
470-
std::io::stderr().write_all(&output.stderr).unwrap();
471-
// Uh... let's try anyway, I guess?
472-
return params;
473470
}
474-
let mut sysroot = String::from_utf8(output.stdout).unwrap();
475-
// There is typically a newline at the end which confuses clang.
476-
sysroot.truncate(sysroot.trim_end().len());
477-
params.push("-isysroot".to_string());
478-
params.push(sysroot);
479471
}
480472
"android" => {
481473
let mut android_sysroot = config

boring-sys/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
non_upper_case_globals,
1111
unused_imports
1212
)]
13-
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
1413

1514
use std::convert::TryInto;
1615
use std::ffi::c_void;
@@ -26,6 +25,13 @@ use std::os::raw::{c_char, c_int, c_uint, c_ulong};
2625
mod generated {
2726
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
2827
}
28+
29+
// explicitly require presence of some symbols to check if the bindings worked
30+
pub use generated::{ssl_compliance_policy_t, ERR_add_error_data, SSL_set1_groups}; // if these are missing, your include path is incorrect or has a wrong version of boringssl
31+
pub use generated::{BIO_new, OPENSSL_free, SSL_ERROR_NONE}; // if these are missing, your include path is incorrect
32+
33+
// NOTE: cloudflare has MLKEM768 as well, check if we need this later
34+
2935
pub use generated::*;
3036

3137
#[cfg(target_pointer_width = "64")]

boring/src/aes.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//! ```
3939
//!
4040
use crate::ffi;
41-
use crate::libc_types::{c_int, c_uint, size_t};
41+
use crate::libc_types::c_int;
4242
use openssl_macros::corresponds;
4343
use std::mem::MaybeUninit;
4444
use std::ptr;
@@ -63,8 +63,8 @@ impl AesKey {
6363

6464
let mut aes_key = MaybeUninit::uninit();
6565
let r = ffi::AES_set_encrypt_key(
66-
key.as_ptr() as *const _,
67-
key.len() as c_uint * 8,
66+
key.as_ptr(),
67+
(key.len() * 8).try_into().map_err(|_| KeyError(()))?,
6868
aes_key.as_mut_ptr(),
6969
);
7070
if r == 0 {
@@ -87,8 +87,8 @@ impl AesKey {
8787

8888
let mut aes_key = MaybeUninit::uninit();
8989
let r = ffi::AES_set_decrypt_key(
90-
key.as_ptr() as *const _,
91-
key.len() as c_uint * 8,
90+
key.as_ptr(),
91+
(key.len() * 8).try_into().map_err(|_| KeyError(()))?,
9292
aes_key.as_mut_ptr(),
9393
);
9494

@@ -125,12 +125,11 @@ pub fn wrap_key(
125125
assert!(out.len() >= in_.len() + 8); // Ciphertext is 64 bits longer (see 2.2.1)
126126

127127
let written = ffi::AES_wrap_key(
128-
&key.0 as *const _ as *mut _, // this is safe, the implementation only uses the key as a const pointer.
129-
iv.as_ref()
130-
.map_or(ptr::null(), |iv| iv.as_ptr() as *const _),
131-
out.as_ptr() as *mut _,
132-
in_.as_ptr() as *const _,
133-
in_.len() as size_t,
128+
std::ptr::addr_of!(key.0).cast_mut(), // this is safe, the implementation only uses the key as a const pointer.
129+
iv.as_ref().map_or(ptr::null(), |iv| iv.as_ptr()),
130+
out.as_mut_ptr(),
131+
in_.as_ptr(),
132+
in_.len(),
134133
);
135134
if written <= 0 {
136135
Err(KeyError(()))
@@ -164,12 +163,11 @@ pub fn unwrap_key(
164163
assert!(out.len() + 8 <= in_.len());
165164

166165
let written = ffi::AES_unwrap_key(
167-
&key.0 as *const _ as *mut _, // this is safe, the implementation only uses the key as a const pointer.
168-
iv.as_ref()
169-
.map_or(ptr::null(), |iv| iv.as_ptr() as *const _),
170-
out.as_ptr() as *mut _,
171-
in_.as_ptr() as *const _,
172-
in_.len() as size_t,
166+
std::ptr::addr_of!(key.0).cast_mut(), // this is safe, the implementation only uses the key as a const pointer.
167+
iv.as_ref().map_or(ptr::null(), |iv| iv.as_ptr().cast()),
168+
out.as_ptr().cast_mut(),
169+
in_.as_ptr().cast(),
170+
in_.len(),
173171
);
174172

175173
if written <= 0 {

boring/src/asn1.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! let tomorrow = Asn1Time::days_from_now(1);
2626
//! ```
2727
use crate::ffi;
28-
use crate::libc_types::{c_char, c_int, c_long, time_t};
28+
use crate::libc_types::{c_int, c_long, time_t};
2929
use foreign_types::{ForeignType, ForeignTypeRef};
3030
use std::cmp::Ordering;
3131
use std::ffi::CString;
@@ -405,7 +405,7 @@ impl Asn1StringRef {
405405
return Err(ErrorStack::get());
406406
}
407407

408-
Ok(OpensslString::from_ptr(ptr as *mut c_char))
408+
Ok(OpensslString::from_ptr(ptr.cast()))
409409
}
410410
}
411411

@@ -544,7 +544,7 @@ impl Asn1BitStringRef {
544544
#[corresponds(ASN1_STRING_length)]
545545
#[must_use]
546546
pub fn len(&self) -> usize {
547-
unsafe { ffi::ASN1_STRING_length(self.as_ptr() as *const _) as usize }
547+
unsafe { ffi::ASN1_STRING_length(self.as_ptr().cast_const()) as usize }
548548
}
549549

550550
/// Determines if the string is empty.
@@ -586,7 +586,7 @@ impl Asn1Object {
586586
unsafe {
587587
ffi::init();
588588
let txt = CString::new(txt).map_err(ErrorStack::internal_error)?;
589-
let obj: *mut ffi::ASN1_OBJECT = cvt_p(ffi::OBJ_txt2obj(txt.as_ptr() as *const _, 0))?;
589+
let obj: *mut ffi::ASN1_OBJECT = cvt_p(ffi::OBJ_txt2obj(txt.as_ptr(), 0))?;
590590
Ok(Asn1Object::from_ptr(obj))
591591
}
592592
}
@@ -603,9 +603,9 @@ impl Asn1ObjectRef {
603603
impl fmt::Display for Asn1ObjectRef {
604604
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
605605
unsafe {
606-
let mut buf = [0; 80];
606+
let mut buf = [0u8; 80];
607607
let len = ffi::OBJ_obj2txt(
608-
buf.as_mut_ptr() as *mut _,
608+
buf.as_mut_ptr().cast(),
609609
buf.len() as c_int,
610610
self.as_ptr(),
611611
0,

boring/src/bio.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::ffi;
22
use crate::ffi::BIO_new_mem_buf;
3+
use crate::try_int;
34
use std::marker::PhantomData;
45
use std::ptr;
56
use std::slice;
@@ -19,17 +20,9 @@ impl Drop for MemBioSlice<'_> {
1920

2021
impl<'a> MemBioSlice<'a> {
2122
pub fn new(buf: &'a [u8]) -> Result<MemBioSlice<'a>, ErrorStack> {
22-
type BufLen = isize;
23-
2423
ffi::init();
2524

26-
assert!(buf.len() <= BufLen::MAX as usize);
27-
let bio = unsafe {
28-
cvt_p(BIO_new_mem_buf(
29-
buf.as_ptr() as *const _,
30-
buf.len() as BufLen,
31-
))?
32-
};
25+
let bio = unsafe { cvt_p(BIO_new_mem_buf(buf.as_ptr().cast(), try_int(buf.len())?))? };
3326

3427
Ok(MemBioSlice(bio, PhantomData))
3528
}
@@ -65,7 +58,7 @@ impl MemBio {
6558
unsafe {
6659
let mut ptr = ptr::null_mut();
6760
let len = ffi::BIO_get_mem_data(self.0, &mut ptr);
68-
if ptr.is_null() {
61+
if ptr.is_null() || len < 0 {
6962
return &[];
7063
}
7164
slice::from_raw_parts(ptr.cast_const().cast(), len as usize)

0 commit comments

Comments
 (0)