This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.rs
195 lines (165 loc) · 5.66 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*!
* Contains the build process for WolfSSL
*/
extern crate bindgen;
use autotools::Config;
use std::collections::HashSet;
use std::env;
use std::path::PathBuf;
use std::process::Command;
static WOLFSSL_VERSION: &str = "wolfssl-5.6.0-stable";
/**
* Work around for bindgen creating duplicate values.
*/
#[derive(Debug)]
struct IgnoreMacros(HashSet<String>);
impl bindgen::callbacks::ParseCallbacks for IgnoreMacros {
fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior {
if self.0.contains(name) {
bindgen::callbacks::MacroParsingBehavior::Ignore
} else {
bindgen::callbacks::MacroParsingBehavior::Default
}
}
}
/**
* Extract WolfSSL
*/
fn extract_wolfssl(dest: &str) -> std::io::Result<()> {
Command::new("tar")
.arg("-zxvf")
.arg(format!("vendor/{WOLFSSL_VERSION}.tar.gz"))
.arg("-C")
.arg(dest)
.status()
.unwrap();
Ok(())
}
/**
Builds WolfSSL
*/
fn build_wolfssl(dest: &str) -> PathBuf {
// Create the config
let mut conf = Config::new(format!("{dest}/{WOLFSSL_VERSION}"));
// Configure it
conf.reconf("-ivf")
// Only build the static library
.enable_static()
.disable_shared()
// Enable TLS/1.3
.enable("tls13", None)
// Disable old TLS versions
.disable("oldtls", None)
// Enable single threaded mode
.enable("singlethreaded", None)
// Enable D/TLS
.enable("dtls", None)
// Enable single precision
.enable("sp", None)
// Enable single precision ASM
.enable("sp-asm", None)
// Enable setting the D/TLS MTU size
.enable("dtls-mtu", None)
// Disable SHA3
.disable("sha3", None)
// Disable DH key exchanges
.disable("dh", None)
// Enable elliptic curve exchanges
.enable("supportedcurves", None)
.enable("curve25519", None)
// Enable Secure Renegotiation
.enable("secure-renegotiation", None)
// Enable SNI
.enable("sni", None)
// CFLAGS
.cflag("-g")
.cflag("-fPIC")
.cflag("-DWOLFSSL_DTLS_ALLOW_FUTURE")
.cflag("-DWOLFSSL_MIN_RSA_BITS=2048")
.cflag("-DWOLFSSL_MIN_ECC_BITS=256");
if cfg!(feature = "debug") {
conf.enable("debug", None);
}
if cfg!(feature = "postquantum") {
// Post Quantum support is provided by liboqs
if let Some(include) = std::env::var_os("DEP_OQS_ROOT") {
let oqs_path = &include.into_string().unwrap();
conf.cflag(format!("-I{oqs_path}/build/include/"));
conf.ldflag(format!("-L{oqs_path}/build/lib/"));
conf.with("liboqs", None);
} else {
panic!("Post Quantum requested but liboqs appears to be missing?");
}
}
if build_target::target_arch().unwrap() == build_target::Arch::X86_64 {
// Enable Intel ASM optmisations
conf.enable("intelasm", None);
// Enable AES hardware acceleration
conf.enable("aesni", None);
}
if build_target::target_arch().unwrap() == build_target::Arch::AARCH64 {
// Enable ARM ASM optimisations
conf.enable("armasm", None);
}
if build_target::target_arch().unwrap() == build_target::Arch::ARM {
// Enable ARM ASM optimisations
conf.enable("armasm", None);
}
// Build and return the config
conf.build()
}
fn main() -> std::io::Result<()> {
// Get the build directory
let dst_string = env::var("OUT_DIR").unwrap();
// Extract WolfSSL
extract_wolfssl(&dst_string)?;
// Configure and build WolfSSL
let dst = build_wolfssl(&dst_string);
// We want to block some macros as they are incorrectly creating duplicate values
// https://github.com/rust-lang/rust-bindgen/issues/687
// TODO: Reach out to tlspuffin and ask if we can incorporate this code and credit them
let mut hash_ignored_macros = HashSet::new();
for i in &[
"IPPORT_RESERVED",
"EVP_PKEY_DH",
"BIO_CLOSE",
"BIO_NOCLOSE",
"CRYPTO_LOCK",
"ASN1_STRFLGS_ESC_MSB",
"SSL_MODE_RELEASE_BUFFERS",
// Wolfssl 4.3.0
"GEN_IPADD",
"EVP_PKEY_RSA",
] {
hash_ignored_macros.insert(i.to_string());
}
let ignored_macros = IgnoreMacros(hash_ignored_macros);
let dst_include = format!("{dst_string}/include");
// Build the Rust binding
let builder = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(format!("-I{dst_include}/"))
.parse_callbacks(Box::new(ignored_macros))
.formatter(bindgen::Formatter::Rustfmt);
let builder = builder
.allowlist_file(format!("{dst_include}/wolfssl/.*.h"))
.allowlist_file(format!("{dst_include}/wolfssl/wolfcrypt/.*.h"))
.allowlist_file(format!("{dst_include}/wolfssl/openssl/compat_types.h"));
let builder = builder.blocklist_function("wolfSSL_BIO_vprintf");
let bindings: bindgen::Bindings = builder.generate().expect("Unable to generate bindings");
// Write out the bindings
bindings
.write_to_file(dst.join("bindings.rs"))
.expect("Couldn't write bindings!");
// Tell cargo to tell rustc to link in WolfSSL
println!("cargo:rustc-link-lib=static=wolfssl");
if cfg!(feature = "postquantum") {
println!("cargo:rustc-link-lib=static=oqs");
}
println!("cargo:rustc-link-search=native={}/lib/", dst_string);
println!("cargo:include={}", dst_string);
// Invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=wrapper.h");
// That should do it...
Ok(())
}