| 1 |
#[cfg(feature = "bindgen")] |
| 2 |
use bindgen::callbacks::{ItemInfo, MacroParsingBehavior, ParseCallbacks}; |
| 3 |
#[cfg(feature = "bindgen")] |
| 4 |
use bindgen::{MacroTypeVariation, RustTarget}; |
| 5 |
use std::io::Write; |
| 6 |
use std::path::PathBuf; |
| 7 |
#[cfg(not(feature = "bindgen"))] |
| 8 |
use std::process; |
| 9 |
use std::{env, fs}; |
| 10 |
|
| 11 |
const INCLUDES: &str = " |
| 12 |
#include <openssl/aes.h> |
| 13 |
#include <openssl/asn1.h> |
| 14 |
#include <openssl/bio.h> |
| 15 |
#include <openssl/cmac.h> |
| 16 |
#include <openssl/conf.h> |
| 17 |
#include <openssl/crypto.h> |
| 18 |
#include <openssl/dh.h> |
| 19 |
#include <openssl/dsa.h> |
| 20 |
#include <openssl/ec.h> |
| 21 |
#include <openssl/err.h> |
| 22 |
#include <openssl/evp.h> |
| 23 |
#include <openssl/hmac.h> |
| 24 |
#include <openssl/objects.h> |
| 25 |
#include <openssl/opensslv.h> |
| 26 |
#include <openssl/pem.h> |
| 27 |
#include <openssl/pkcs12.h> |
| 28 |
#include <openssl/pkcs7.h> |
| 29 |
#include <openssl/rand.h> |
| 30 |
#include <openssl/rsa.h> |
| 31 |
#include <openssl/safestack.h> |
| 32 |
#include <openssl/sha.h> |
| 33 |
#include <openssl/ssl.h> |
| 34 |
#include <openssl/stack.h> |
| 35 |
#include <openssl/x509.h> |
| 36 |
#include <openssl/x509_vfy.h> |
| 37 |
#include <openssl/x509v3.h> |
| 38 |
|
| 39 |
#if !defined(OPENSSL_IS_AWSLC) |
| 40 |
// this must be included after ssl.h for libressl! |
| 41 |
#include <openssl/srtp.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#if !(defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)) |
| 45 |
#include <openssl/cms.h> |
| 46 |
#endif |
| 47 |
|
| 48 |
#if !defined(OPENSSL_NO_COMP) && !(defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)) |
| 49 |
#include <openssl/comp.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
#if !defined(OPENSSL_IS_BORINGSSL) |
| 53 |
#include <openssl/ocsp.h> |
| 54 |
#endif |
| 55 |
|
| 56 |
#if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000 |
| 57 |
#include <openssl/kdf.h> |
| 58 |
#endif |
| 59 |
|
| 60 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000 |
| 61 |
#include <openssl/decoder.h> |
| 62 |
#include <openssl/encoder.h> |
| 63 |
#include <openssl/provider.h> |
| 64 |
#include <openssl/params.h> |
| 65 |
#include <openssl/param_build.h> |
| 66 |
#endif |
| 67 |
|
| 68 |
#if OPENSSL_VERSION_NUMBER >= 0x30200000 |
| 69 |
#include <openssl/quic.h> |
| 70 |
#endif |
| 71 |
|
| 72 |
#if defined(LIBRESSL_VERSION_NUMBER) || defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC) |
| 73 |
#include <openssl/poly1305.h> |
| 74 |
#endif |
| 75 |
|
| 76 |
#if defined(OPENSSL_IS_BORINGSSL) |
| 77 |
#include <openssl/mldsa.h> |
| 78 |
#include <openssl/mlkem.h> |
| 79 |
#endif |
| 80 |
|
| 81 |
#if OPENSSL_VERSION_NUMBER >= 0x30200000 |
| 82 |
#include <openssl/thread.h> |
| 83 |
#endif |
| 84 |
"; |
| 85 |
|
| 86 |
/// Attempts to find and use pre-generated AWS-LC Rust bindings from the |
| 87 |
/// installation directory. Returns `true` if pre-generated bindings were |
| 88 |
/// found and installed, `false` otherwise. |
| 89 |
/// |
| 90 |
/// AWS-LC installations may ship pre-generated bindings at: |
| 91 |
/// $PREFIX/share/rust/aws_lc_bindings.rs |
| 92 |
/// |
| 93 |
/// where $PREFIX is the parent of the include directory. These bindings are |
| 94 |
/// generated with the same bindgen options used by aws-lc-sys, and the |
| 95 |
/// handful of static inline functions that matter (e.g., BIO_get_mem_data) |
| 96 |
/// are already implemented in pure Rust in src/lib.rs. |
| 97 |
/// |
| 98 |
/// **Contract:** The pregenerated bindings must not contain `extern "C"` |
| 99 |
/// declarations for static inline wrapper functions (i.e., those that would |
| 100 |
/// normally be compiled from `awslc_static_wrapper.c`). When this function |
| 101 |
/// returns `true`, the caller skips both bindgen and the static wrapper |
| 102 |
/// compilation step; the required shims are instead provided as pure Rust |
| 103 |
/// in `openssl-sys/src/lib.rs`. |
| 104 |
fn try_pregenerated_awslc_bindings(include_dirs: &[PathBuf]) -> bool { |
| 105 |
for include_dir in include_dirs { |
| 106 |
let Some(prefix_dir) = include_dir.parent() else { |
| 107 |
continue; |
| 108 |
}; |
| 109 |
let bindings_path = prefix_dir |
| 110 |
.join("share") |
| 111 |
.join("rust") |
| 112 |
.join("aws_lc_bindings.rs"); |
| 113 |
if bindings_path.exists() { |
| 114 |
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 115 |
println!( |
| 116 |
"cargo:warning=Using pre-generated AWS-LC bindings from {}", |
| 117 |
bindings_path.display() |
| 118 |
); |
| 119 |
println!("cargo:rerun-if-changed={}", bindings_path.display()); |
| 120 |
println!("cargo:rustc-cfg=awslc_pregenerated"); |
| 121 |
fs::copy(&bindings_path, out_dir.join("bindgen.rs")) |
| 122 |
.expect("Failed to copy pre-generated AWS-LC Rust bindings"); |
| 123 |
return true; |
| 124 |
} |
| 125 |
} |
| 126 |
false |
| 127 |
} |
| 128 |
|
| 129 |
#[cfg(feature = "bindgen")] |
| 130 |
pub fn run(include_dirs: &[PathBuf]) { |
| 131 |
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 132 |
|
| 133 |
let mut builder = bindgen::builder() |
| 134 |
.parse_callbacks(Box::new(OpensslCallbacks)) |
| 135 |
.rust_target(RustTarget::stable(70, 0).unwrap()) |
| 136 |
.ctypes_prefix("::libc") |
| 137 |
.raw_line("use libc::*;") |
| 138 |
.raw_line("#[cfg(windows)] use std::os::windows::raw::HANDLE;") |
| 139 |
.raw_line("type evp_pkey_st = EVP_PKEY;") |
| 140 |
.allowlist_file(".*[/\\\\]openssl/[^/\\\\]+\\.h") |
| 141 |
.allowlist_recursively(false) |
| 142 |
// libc is missing pthread_once_t on macOS |
| 143 |
.blocklist_type("CRYPTO_ONCE") |
| 144 |
.blocklist_function("CRYPTO_THREAD_run_once") |
| 145 |
// we don't want to mess with va_list |
| 146 |
.blocklist_function("BIO_vprintf") |
| 147 |
.blocklist_function("BIO_vsnprintf") |
| 148 |
.blocklist_function("ERR_vset_error") |
| 149 |
.blocklist_function("ERR_add_error_vdata") |
| 150 |
.blocklist_function("EVP_KDF_vctrl") |
| 151 |
.blocklist_type("OSSL_FUNC_core_vset_error_fn") |
| 152 |
.blocklist_type("OSSL_FUNC_BIO_vprintf_fn") |
| 153 |
.blocklist_type("OSSL_FUNC_BIO_vsnprintf_fn") |
| 154 |
// struct hostent * does not exist on Windows |
| 155 |
.blocklist_function("BIO_gethostbyname") |
| 156 |
// Maintain compatibility for existing enum definitions |
| 157 |
.rustified_enum("point_conversion_form_t") |
| 158 |
// Maintain compatibility for pre-union definitions |
| 159 |
.blocklist_type("GENERAL_NAME") |
| 160 |
.blocklist_type("GENERAL_NAME_st") |
| 161 |
.blocklist_type("EVP_PKEY") |
| 162 |
.blocklist_type("evp_pkey_st") |
| 163 |
.layout_tests(false) |
| 164 |
.header_contents("includes.h", INCLUDES); |
| 165 |
|
| 166 |
for include_dir in include_dirs { |
| 167 |
builder = builder |
| 168 |
.clang_arg("-I") |
| 169 |
.clang_arg(include_dir.display().to_string()); |
| 170 |
} |
| 171 |
|
| 172 |
builder |
| 173 |
.generate() |
| 174 |
.unwrap() |
| 175 |
.write_to_file(out_dir.join("bindgen.rs")) |
| 176 |
.unwrap(); |
| 177 |
} |
| 178 |
|
| 179 |
#[cfg(feature = "bindgen")] |
| 180 |
pub fn run_boringssl(include_dirs: &[PathBuf]) { |
| 181 |
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 182 |
|
| 183 |
fs::File::create(out_dir.join("boring_static_wrapper.h")) |
| 184 |
.expect("Failed to create boring_static_wrapper.h") |
| 185 |
.write_all(INCLUDES.as_bytes()) |
| 186 |
.expect("Failed to write contents to boring_static_wrapper.h"); |
| 187 |
|
| 188 |
let mut builder = bindgen::builder() |
| 189 |
.rust_target(RustTarget::stable(70, 0).unwrap()) |
| 190 |
.ctypes_prefix("::libc") |
| 191 |
.raw_line("use libc::*;") |
| 192 |
.derive_default(false) |
| 193 |
.enable_function_attribute_detection() |
| 194 |
.default_macro_constant_type(MacroTypeVariation::Signed) |
| 195 |
.rustified_enum("point_conversion_form_t") |
| 196 |
.allowlist_file(".*[/\\\\]openssl/[^/]+\\.h") |
| 197 |
.allowlist_recursively(false) |
| 198 |
.blocklist_function("BIO_vsnprintf") |
| 199 |
.blocklist_function("OPENSSL_vasprintf") |
| 200 |
.wrap_static_fns(true) |
| 201 |
.wrap_static_fns_path(out_dir.join("boring_static_wrapper").display().to_string()) |
| 202 |
.layout_tests(false) |
| 203 |
.header( |
| 204 |
out_dir |
| 205 |
.join("boring_static_wrapper.h") |
| 206 |
.display() |
| 207 |
.to_string(), |
| 208 |
); |
| 209 |
|
| 210 |
for include_dir in include_dirs { |
| 211 |
builder = builder |
| 212 |
.clang_arg("-I") |
| 213 |
.clang_arg(include_dir.display().to_string()); |
| 214 |
} |
| 215 |
|
| 216 |
builder |
| 217 |
.generate() |
| 218 |
.unwrap() |
| 219 |
.write_to_file(out_dir.join("bindgen.rs")) |
| 220 |
.unwrap(); |
| 221 |
|
| 222 |
cc::Build::new() |
| 223 |
.file(out_dir.join("boring_static_wrapper.c")) |
| 224 |
.includes(include_dirs) |
| 225 |
.compile("boring_static_wrapper"); |
| 226 |
} |
| 227 |
|
| 228 |
#[cfg(not(feature = "bindgen"))] |
| 229 |
pub fn run_boringssl(include_dirs: &[PathBuf]) { |
| 230 |
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 231 |
|
| 232 |
fs::File::create(out_dir.join("boring_static_wrapper.h")) |
| 233 |
.expect("Failed to create boring_static_wrapper.h") |
| 234 |
.write_all(INCLUDES.as_bytes()) |
| 235 |
.expect("Failed to write contents to boring_static_wrapper.h"); |
| 236 |
|
| 237 |
let mut bindgen_cmd = process::Command::new("bindgen"); |
| 238 |
bindgen_cmd |
| 239 |
.arg("-o") |
| 240 |
.arg(out_dir.join("bindgen.rs")) |
| 241 |
.arg("--rust-target=1.80") |
| 242 |
.arg("--ctypes-prefix=::libc") |
| 243 |
.arg("--raw-line=use libc::*;") |
| 244 |
.arg("--no-derive-default") |
| 245 |
.arg("--enable-function-attribute-detection") |
| 246 |
.arg("--default-macro-constant-type=signed") |
| 247 |
.arg("--rustified-enum=point_conversion_form_t") |
| 248 |
.arg("--allowlist-file=.*[/\\\\]openssl/[^/]+\\.h") |
| 249 |
.arg("--no-recursive-allowlist") |
| 250 |
.arg("--blocklist-function=BIO_vsnprintf") |
| 251 |
.arg("--blocklist-function=OPENSSL_vasprintf") |
| 252 |
.arg("--experimental") |
| 253 |
.arg("--wrap-static-fns") |
| 254 |
.arg("--wrap-static-fns-path") |
| 255 |
.arg(out_dir.join("boring_static_wrapper").display().to_string()) |
| 256 |
.arg("--no-layout-tests") |
| 257 |
.arg(out_dir.join("boring_static_wrapper.h")) |
| 258 |
.arg("--") |
| 259 |
.arg(format!("--target={}", env::var("TARGET").unwrap())); |
| 260 |
|
| 261 |
for include_dir in include_dirs { |
| 262 |
bindgen_cmd.arg("-I").arg(include_dir.display().to_string()); |
| 263 |
} |
| 264 |
|
| 265 |
let result = bindgen_cmd.status().expect("bindgen failed to execute"); |
| 266 |
assert!(result.success()); |
| 267 |
|
| 268 |
cc::Build::new() |
| 269 |
.file(out_dir.join("boring_static_wrapper.c")) |
| 270 |
.includes(include_dirs) |
| 271 |
.compile("boring_static_wrapper"); |
| 272 |
} |
| 273 |
|
| 274 |
#[cfg(feature = "bindgen")] |
| 275 |
mod bindgen_options { |
| 276 |
use bindgen::callbacks::{ItemInfo, ParseCallbacks}; |
| 277 |
|
| 278 |
#[derive(Debug)] |
| 279 |
pub struct StripPrefixCallback { |
| 280 |
remove_prefix: Option<String>, |
| 281 |
} |
| 282 |
|
| 283 |
impl StripPrefixCallback { |
| 284 |
pub fn new(prefix: &str) -> StripPrefixCallback { |
| 285 |
StripPrefixCallback { |
| 286 |
remove_prefix: Some(prefix.to_string()), |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
impl ParseCallbacks for StripPrefixCallback { |
| 292 |
fn generated_name_override(&self, item_info: ItemInfo<'_>) -> Option<String> { |
| 293 |
self.remove_prefix |
| 294 |
.as_ref() |
| 295 |
.and_then(|s| item_info.name.strip_prefix(s.as_str()).map(String::from)) |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
#[cfg(feature = "bindgen")] |
| 301 |
pub fn run_awslc(include_dirs: &[PathBuf], symbol_prefix: Option<String>) { |
| 302 |
// If the AWS-LC installation ships pre-generated Rust bindings, use them |
| 303 |
// directly. These bindings already handle symbol prefix stripping. |
| 304 |
if try_pregenerated_awslc_bindings(include_dirs) { |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 309 |
|
| 310 |
fs::File::create(out_dir.join("awslc_static_wrapper.h")) |
| 311 |
.expect("Failed to create awslc_static_wrapper.h") |
| 312 |
.write_all(INCLUDES.as_bytes()) |
| 313 |
.expect("Failed to write contents to awslc_static_wrapper.h"); |
| 314 |
|
| 315 |
let mut builder = bindgen::builder() |
| 316 |
.rust_target(RustTarget::stable(70, 0).unwrap()) |
| 317 |
.ctypes_prefix("::libc") |
| 318 |
.raw_line("use libc::*;") |
| 319 |
.derive_default(false) |
| 320 |
.enable_function_attribute_detection() |
| 321 |
.default_macro_constant_type(MacroTypeVariation::Signed) |
| 322 |
.rustified_enum("point_conversion_form_t") |
| 323 |
.allowlist_file(r".*(/|\\)openssl((/|\\)[^/\\]+)+\.h") |
| 324 |
.wrap_static_fns(true) |
| 325 |
.wrap_static_fns_path(out_dir.join("awslc_static_wrapper").display().to_string()) |
| 326 |
.layout_tests(false) |
| 327 |
.header(out_dir.join("awslc_static_wrapper.h").display().to_string()); |
| 328 |
|
| 329 |
if let Some(prefix) = symbol_prefix { |
| 330 |
use bindgen_options::StripPrefixCallback; |
| 331 |
let callback = StripPrefixCallback::new(prefix.as_str()); |
| 332 |
builder = builder.parse_callbacks(Box::from(callback)); |
| 333 |
} |
| 334 |
|
| 335 |
for include_dir in include_dirs { |
| 336 |
builder = builder |
| 337 |
.clang_arg("-I") |
| 338 |
.clang_arg(include_dir.display().to_string()); |
| 339 |
} |
| 340 |
|
| 341 |
builder |
| 342 |
.generate() |
| 343 |
.unwrap() |
| 344 |
.write_to_file(out_dir.join("bindgen.rs")) |
| 345 |
.unwrap(); |
| 346 |
|
| 347 |
cc::Build::new() |
| 348 |
.file(out_dir.join("awslc_static_wrapper.c")) |
| 349 |
.includes(include_dirs) |
| 350 |
.compile("awslc_static_wrapper"); |
| 351 |
} |
| 352 |
|
| 353 |
#[cfg(not(feature = "bindgen"))] |
| 354 |
pub fn run_awslc(include_dirs: &[PathBuf], symbol_prefix: Option<String>) { |
| 355 |
// If the AWS-LC installation ships pre-generated Rust bindings, use them |
| 356 |
// directly. These bindings already handle symbol prefix stripping. |
| 357 |
if try_pregenerated_awslc_bindings(include_dirs) { |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
if symbol_prefix.is_some() { |
| 362 |
panic!("aws-lc installation has prefixed symbols, but bindgen-cli does not support removing prefixes. \ |
| 363 |
Enable the bindgen crate feature to support this installation.") |
| 364 |
} |
| 365 |
|
| 366 |
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 367 |
|
| 368 |
fs::File::create(out_dir.join("awslc_static_wrapper.h")) |
| 369 |
.expect("Failed to create awslc_static_wrapper.h") |
| 370 |
.write_all(INCLUDES.as_bytes()) |
| 371 |
.expect("Failed to write contents to awslc_static_wrapper.h"); |
| 372 |
|
| 373 |
let mut bindgen_cmd = process::Command::new("bindgen"); |
| 374 |
bindgen_cmd |
| 375 |
.arg("-o") |
| 376 |
.arg(out_dir.join("bindgen.rs")) |
| 377 |
.arg("--rust-target=1.80") |
| 378 |
.arg("--ctypes-prefix=::libc") |
| 379 |
.arg("--raw-line=use libc::*;") |
| 380 |
.arg("--no-derive-default") |
| 381 |
.arg("--enable-function-attribute-detection") |
| 382 |
.arg("--default-macro-constant-type=signed") |
| 383 |
.arg("--rustified-enum=point_conversion_form_t") |
| 384 |
.arg(r"--allowlist-file=.*(/|\\)openssl((/|\\)[^/\\]+)+\.h") |
| 385 |
.arg("--experimental") |
| 386 |
.arg("--wrap-static-fns") |
| 387 |
.arg("--wrap-static-fns-path") |
| 388 |
.arg(out_dir.join("awslc_static_wrapper").display().to_string()) |
| 389 |
.arg(out_dir.join("awslc_static_wrapper.h")) |
| 390 |
.arg("--") |
| 391 |
.arg(format!("--target={}", env::var("TARGET").unwrap())); |
| 392 |
|
| 393 |
for include_dir in include_dirs { |
| 394 |
bindgen_cmd.arg("-I").arg(include_dir.display().to_string()); |
| 395 |
} |
| 396 |
|
| 397 |
let result = bindgen_cmd.status().expect("bindgen failed to execute"); |
| 398 |
assert!(result.success()); |
| 399 |
|
| 400 |
cc::Build::new() |
| 401 |
.file(out_dir.join("awslc_static_wrapper.c")) |
| 402 |
.includes(include_dirs) |
| 403 |
.compile("awslc_static_wrapper"); |
| 404 |
} |
| 405 |
|
| 406 |
#[cfg(feature = "bindgen")] |
| 407 |
#[derive(Debug)] |
| 408 |
struct OpensslCallbacks; |
| 409 |
|
| 410 |
#[cfg(feature = "bindgen")] |
| 411 |
impl ParseCallbacks for OpensslCallbacks { |
| 412 |
// for now we'll continue hand-writing constants |
| 413 |
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior { |
| 414 |
MacroParsingBehavior::Ignore |
| 415 |
} |
| 416 |
|
| 417 |
fn item_name(&self, item_info: ItemInfo) -> Option<String> { |
| 418 |
match item_info.name { |
| 419 |
// Our original definitions of these are wrong, so rename to avoid breakage |
| 420 |
"CRYPTO_EX_new" |
| 421 |
| "CRYPTO_EX_dup" |
| 422 |
| "CRYPTO_EX_free" |
| 423 |
| "BIO_meth_set_write" |
| 424 |
| "BIO_meth_set_read" |
| 425 |
| "BIO_meth_set_puts" |
| 426 |
| "BIO_meth_set_ctrl" |
| 427 |
| "BIO_meth_set_create" |
| 428 |
| "BIO_meth_set_destroy" |
| 429 |
| "CRYPTO_set_locking_callback" |
| 430 |
| "CRYPTO_set_id_callback" |
| 431 |
| "SSL_CTX_set_tmp_dh_callback" |
| 432 |
| "SSL_set_tmp_dh_callback" |
| 433 |
| "SSL_CTX_set_tmp_ecdh_callback" |
| 434 |
| "SSL_set_tmp_ecdh_callback" |
| 435 |
| "SSL_CTX_callback_ctrl" |
| 436 |
| "SSL_CTX_set_alpn_select_cb" => Some(format!("{}__fixed_rust", item_info.name)), |
| 437 |
// On NetBSD, "off_t" is generated as "__off_t". |
| 438 |
"__off_t" => Some("off_t".to_string()), |
| 439 |
_ => None, |
| 440 |
} |
| 441 |
} |
| 442 |
} |