forked from latchset/kryoptic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
142 lines (130 loc) · 4.35 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
// Copyright 2023 Simo Sorce
// See LICENSE.txt file for terms
#[derive(Debug)]
pub struct Pkcs11Callbacks;
impl bindgen::callbacks::ParseCallbacks for Pkcs11Callbacks {
fn int_macro(
&self,
name: &str,
_: i64,
) -> Option<bindgen::callbacks::IntKind> {
if name == "CK_TRUE" || name == "CK_FALSE" {
Some(bindgen::callbacks::IntKind::Custom {
name: "CK_BBOOL",
is_signed: false,
})
} else if name.starts_with("CRYPTOKI_VERSION") {
Some(bindgen::callbacks::IntKind::Custom {
name: "CK_BYTE",
is_signed: false,
})
} else if name.starts_with("CK") {
Some(bindgen::callbacks::IntKind::Custom {
name: "CK_ULONG",
is_signed: false,
})
} else {
None
}
}
}
fn build_ossl() {
let openssl_path = std::path::PathBuf::from("openssl")
.canonicalize()
.expect("cannot canonicalize path");
#[cfg(feature = "fips")]
let (libpath, bldargs) = {
let providers_path = openssl_path
.join("providers")
.canonicalize()
.expect("OpenSSL providers path unavailable");
println!(
"cargo:rustc-link-search={}",
providers_path.to_str().unwrap()
);
println!("cargo:rustc-link-lib=static=fips");
(
format!("{}/libfips.a", providers_path.to_str().unwrap())
.to_string(),
["--debug", "enable-fips"],
)
};
#[cfg(not(feature = "fips"))]
let (libpath, bldargs) = {
println!("cargo:rustc-link-search={}", openssl_path.to_str().unwrap());
println!("cargo:rustc-link-lib=static=crypto");
(
format!("{}/libcrypto.a", openssl_path.to_str().unwrap())
.to_string(),
["--debug"],
)
};
match std::path::Path::new(&libpath).try_exists() {
Ok(true) => (),
_ => {
/* openssl: ./Configure --debug enable-fips */
if !std::process::Command::new("./Configure")
.current_dir(&openssl_path)
.args(bldargs)
.output()
.expect("could not run openssl `Configure`")
.status
.success()
{
// Panic if the command was not successful.
panic!("could not configure OpenSSL");
}
if !std::process::Command::new("make")
.current_dir(&openssl_path)
.output()
.expect("could not run openssl `make`")
.status
.success()
{
// Panic if the command was not successful.
panic!("could not build OpenSSL");
}
}
}
let include_path = openssl_path
.join("include")
.canonicalize()
.expect("OpenSSL include path unavailable");
bindgen::Builder::default()
.header("fips.h")
.clang_arg(format!("-I{}", include_path.to_str().unwrap()))
.clang_arg("-std=c90") /* workaround [-Wimplicit-int] */
.derive_default(true)
.formatter(bindgen::Formatter::Prettyplease)
.allowlist_item("ossl_.*")
.allowlist_item("OSSL_.*")
.allowlist_item("openssl_.*")
.allowlist_item("OPENSSL_.*")
.allowlist_item("CRYPTO_.*")
.allowlist_item("c_.*")
.allowlist_item("EVP_.*")
.allowlist_item("evp_.*")
.allowlist_item("BN_.*")
.allowlist_item("LN_aes.*")
.generate()
.expect("Unable to generate bindings")
.write_to_file("src/ossl/bindings.rs")
.expect("Couldn't write bindings!");
}
fn main() {
/* PKCS11 Headers */
bindgen::Builder::default()
.header("pkcs11_headers/3.1/pkcs11.h")
.derive_default(true)
.formatter(bindgen::Formatter::Prettyplease)
.blocklist_type("CK_FUNCTION_LIST_PTR")
.blocklist_type("CK_FUNCTION_LIST_3_0_PTR")
.blocklist_type("CK_INTERFACE")
.parse_callbacks(Box::new(Pkcs11Callbacks))
.generate()
.expect("Unable to generate bindings")
.write_to_file("src/pkcs11/bindings.rs")
.expect("Couldn't write bindings!");
/* OpenSSL Cryptography */
build_ossl();
}