Skip to main content

bssl_sys/
lib.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#![no_std]
6#![allow(unknown_lints)]
7#![allow(non_upper_case_globals)]
8#![allow(non_camel_case_types)]
9#![allow(non_snake_case)]
10#![allow(unnecessary_transmutes)]
11
12// --- Copied from third_party/boringssl/src/rust/bssl-sys/src/lib.rs ---
13use core::ffi::c_ulong;
14
15// Wrap the bindgen output in a module and re-export it, so we can override it
16// as needed.
17mod bindgen {
18    #[cfg(not(bindgen_rs_file))]
19    include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
20    // Some static build systems (e.g. bazel) do not support the `OUT_DIR`
21    // configuration used by Cargo. They can specify a complete path to the
22    // generated bindings as an environment variable.
23    #[cfg(bindgen_rs_file)]
24    include!(env!("BINDGEN_RS_FILE"));
25}
26pub use bindgen::*;
27
28// bindgen does not handle C constants correctly. See
29// https://github.com/rust-lang/rust-bindgen/issues/923. Work around this bug by
30// redefining some constants with the correct type. Once the bindgen bug has
31// been fixed, remove this.
32pub const ASN1_STRFLGS_ESC_2253: c_ulong = bindgen::ASN1_STRFLGS_ESC_2253 as c_ulong;
33pub const ASN1_STRFLGS_ESC_CTRL: c_ulong = bindgen::ASN1_STRFLGS_ESC_CTRL as c_ulong;
34pub const ASN1_STRFLGS_ESC_MSB: c_ulong = bindgen::ASN1_STRFLGS_ESC_MSB as c_ulong;
35pub const ASN1_STRFLGS_ESC_QUOTE: c_ulong = bindgen::ASN1_STRFLGS_ESC_QUOTE as c_ulong;
36pub const ASN1_STRFLGS_UTF8_CONVERT: c_ulong = bindgen::ASN1_STRFLGS_UTF8_CONVERT as c_ulong;
37pub const ASN1_STRFLGS_IGNORE_TYPE: c_ulong = bindgen::ASN1_STRFLGS_IGNORE_TYPE as c_ulong;
38pub const ASN1_STRFLGS_SHOW_TYPE: c_ulong = bindgen::ASN1_STRFLGS_SHOW_TYPE as c_ulong;
39pub const ASN1_STRFLGS_DUMP_ALL: c_ulong = bindgen::ASN1_STRFLGS_DUMP_ALL as c_ulong;
40pub const ASN1_STRFLGS_DUMP_UNKNOWN: c_ulong = bindgen::ASN1_STRFLGS_DUMP_UNKNOWN as c_ulong;
41pub const ASN1_STRFLGS_DUMP_DER: c_ulong = bindgen::ASN1_STRFLGS_DUMP_DER as c_ulong;
42pub const ASN1_STRFLGS_RFC2253: c_ulong = bindgen::ASN1_STRFLGS_RFC2253 as c_ulong;
43pub const XN_FLAG_COMPAT: c_ulong = bindgen::XN_FLAG_COMPAT as c_ulong;
44pub const XN_FLAG_SEP_MASK: c_ulong = bindgen::XN_FLAG_SEP_MASK as c_ulong;
45pub const XN_FLAG_SEP_COMMA_PLUS: c_ulong = bindgen::XN_FLAG_SEP_COMMA_PLUS as c_ulong;
46pub const XN_FLAG_SEP_CPLUS_SPC: c_ulong = bindgen::XN_FLAG_SEP_CPLUS_SPC as c_ulong;
47pub const XN_FLAG_SEP_SPLUS_SPC: c_ulong = bindgen::XN_FLAG_SEP_SPLUS_SPC as c_ulong;
48pub const XN_FLAG_SEP_MULTILINE: c_ulong = bindgen::XN_FLAG_SEP_MULTILINE as c_ulong;
49pub const XN_FLAG_DN_REV: c_ulong = bindgen::XN_FLAG_DN_REV as c_ulong;
50pub const XN_FLAG_FN_MASK: c_ulong = bindgen::XN_FLAG_FN_MASK as c_ulong;
51pub const XN_FLAG_FN_SN: c_ulong = bindgen::XN_FLAG_FN_SN as c_ulong;
52pub const XN_FLAG_SPC_EQ: c_ulong = bindgen::XN_FLAG_SPC_EQ as c_ulong;
53pub const XN_FLAG_DUMP_UNKNOWN_FIELDS: c_ulong = bindgen::XN_FLAG_DUMP_UNKNOWN_FIELDS as c_ulong;
54pub const XN_FLAG_RFC2253: c_ulong = bindgen::XN_FLAG_RFC2253 as c_ulong;
55pub const XN_FLAG_ONELINE: c_ulong = bindgen::XN_FLAG_ONELINE as c_ulong;
56
57pub fn init() {
58    // This function does nothing.
59    // TODO(davidben): Remove rust-openssl's dependency on this and remove this.
60}
61
62bssl_macros::bssl_enum! {
63    /// Library code of the BoringSSL errors.
64    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
65    pub enum LibCode: i32 {
66        /// No module
67        None = ERR_LIB_NONE as i32,
68        /// Module `SYS`
69        Sys = ERR_LIB_SYS as i32,
70        /// Module `BN`
71        Bn = ERR_LIB_BN as i32,
72        /// Module `RSA`
73        Rsa = ERR_LIB_RSA as i32,
74        /// Module `DH`
75        Dh = ERR_LIB_DH as i32,
76        /// Module `EVP`
77        Evp = ERR_LIB_EVP as i32,
78        /// Module `BUF`
79        Buf = ERR_LIB_BUF as i32,
80        /// Module `OBJ`
81        Obj = ERR_LIB_OBJ as i32,
82        /// Module `PEM`
83        Pem = ERR_LIB_PEM as i32,
84        /// Module `DSA`
85        Dsa = ERR_LIB_DSA as i32,
86        /// Module `X509`
87        X509 = ERR_LIB_X509 as i32,
88        /// Module `ASN1`
89        Asn1 = ERR_LIB_ASN1 as i32,
90        /// Module `CONF`
91        Conf = ERR_LIB_CONF as i32,
92        /// Module `CRYPTO`
93        Crypto = ERR_LIB_CRYPTO as i32,
94        /// Module `EC`
95        Ec = ERR_LIB_EC as i32,
96        /// Module `SSL`
97        Ssl = ERR_LIB_SSL as i32,
98        /// Module `BIO`
99        Bio = ERR_LIB_BIO as i32,
100        /// Module `PKCS7`
101        Pkcs7 = ERR_LIB_PKCS7 as i32,
102        /// Module `PKCS8`
103        Pkcs8 = ERR_LIB_PKCS8 as i32,
104        /// Module `X509V3`
105        X509v3 = ERR_LIB_X509V3 as i32,
106        /// Module `RAND`
107        Rand = ERR_LIB_RAND as i32,
108        /// Module `ENGINE`
109        Engine = ERR_LIB_ENGINE as i32,
110        /// Module `OCSP`
111        Ocsp = ERR_LIB_OCSP as i32,
112        /// Module `UI`
113        Ui = ERR_LIB_UI as i32,
114        /// Module `COMP`
115        Comp = ERR_LIB_COMP as i32,
116        /// Module `ECDSA`
117        Ecdsa = ERR_LIB_ECDSA as i32,
118        /// Module `ECDH`
119        Ecdh = ERR_LIB_ECDH as i32,
120        /// Module `HMAC`
121        Hmac = ERR_LIB_HMAC as i32,
122        /// Module `DIGEST`
123        Digest = ERR_LIB_DIGEST as i32,
124        /// Module `CIPHER`
125        Cipher = ERR_LIB_CIPHER as i32,
126        /// Module `HKDF`
127        Hkdf = ERR_LIB_HKDF as i32,
128        /// Module `TRUST_TOKEN`
129        TrustToken = ERR_LIB_TRUST_TOKEN as i32,
130        /// Module `CMS`
131        Cms = ERR_LIB_CMS as i32,
132        /// User sourced
133        User = ERR_LIB_USER as i32,
134    }
135}
136
137// --- End of Copied Content ---
138
139// Re-expose the retired ERR_GET_REASON_RUST helper for backward compatibility
140// with older Rust clients (like Android KeyMint) in the Fuchsia tree.
141//
142// In the new BoringSSL, ERR_GET_REASON is a native C function, so we can
143// just call it directly.
144#[inline]
145pub unsafe fn ERR_GET_REASON_RUST(code: u32) -> core::ffi::c_int {
146    ERR_GET_REASON(code)
147}