Skip to content

Commit b62fa94

Browse files
authored
Adding the possibility to use windows openssl backend instead of schannel (#466)
* Add the possibility to use windows openssl backend instead of schannel * Make the windows static ssl build first evaluated
1 parent 9c2d8a4 commit b62fa94

File tree

6 files changed

+105
-1
lines changed

6 files changed

+105
-1
lines changed

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ spnego = ["curl-sys/spnego"]
4747
rustls = ["curl-sys/rustls"]
4848
static-curl = ["curl-sys/static-curl"]
4949
static-ssl = ["curl-sys/static-ssl"]
50+
windows-static-ssl = ["static-curl", "curl-sys/windows-static-ssl"]
5051
force-system-lib-on-osx = ['curl-sys/force-system-lib-on-osx']
5152
protocol-ftp = ["curl-sys/protocol-ftp"]
5253
zlib-ng-compat = ["curl-sys/zlib-ng-compat", "static-curl"]
@@ -72,6 +73,11 @@ name = "ssl_cert_blob"
7273
path = "examples/ssl_cert_blob.rs"
7374
required-features = ["ssl"]
7475

76+
[[example]]
77+
name = "ssl_client_auth"
78+
path = "examples/ssl_client_auth.rs"
79+
required-features = []
80+
7581
[[example]]
7682
name = "aws_sigv4"
7783
path = "examples/aws_sigv4.rs"

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ with various Cargo features:
133133
- `upkeep_7_62_0`: Enable curl_easy_upkeep() support, introduced in curl 7.62.0. Disabled by default.
134134
- `poll_7_68_0`: Enable curl_multi_poll()/curl_multi_wakeup() support, requires curl 7.68.0 or later. Disabled by default.
135135
- `ntlm`: Enable NTLM support in curl. Disabled by default.
136+
- `windows-static-ssl`: Enable Openssl support on Windows via the static build provided by vcpkg. Incompatible with `ssl` (use `--no-default-features`). Disabled by default.
137+
138+
Note that to install openssl on windows via vcpkg the following commands needs to be ran:
139+
```shell
140+
git clone https://github.com/microsoft/vcpkg
141+
cd vcpkg
142+
./bootstrap-vcpkg.bat -disableMetrics
143+
./vcpkg.exe integrate install
144+
./vcpkg.exe install openssl:x64-windows-static-md
145+
```
136146

137147
## Version Support
138148

curl-sys/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ http2 = ["libnghttp2-sys"]
4949
mesalink = []
5050
rustls = ["rustls-ffi"]
5151
static-curl = []
52+
windows-static-ssl = []
5253
static-ssl = ["openssl-sys/vendored"]
5354
spnego = []
5455
force-system-lib-on-osx = []

curl-sys/build.rs

+20
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,26 @@ fn main() {
274274
cfg.define("USE_RUSTLS", None)
275275
.file("curl/lib/vtls/rustls.c")
276276
.include(env::var_os("DEP_RUSTLS_FFI_INCLUDE").unwrap());
277+
} else if cfg!(feature = "windows-static-ssl") {
278+
if windows {
279+
cfg.define("USE_OPENSSL", None)
280+
.file("curl/lib/vtls/openssl.c");
281+
// We need both openssl and zlib
282+
// Those can be installed with
283+
// ```shell
284+
// git clone https://github.com/microsoft/vcpkg
285+
// cd vcpkg
286+
// ./bootstrap-vcpkg.bat -disableMetrics
287+
// ./vcpkg.exe integrate install
288+
// ./vcpkg.exe install openssl:x64-windows-static-md
289+
// ```
290+
#[cfg(target_env = "msvc")]
291+
vcpkg::Config::new().find_package("openssl").ok();
292+
#[cfg(target_env = "msvc")]
293+
vcpkg::Config::new().find_package("zlib").ok();
294+
} else {
295+
panic!("Not available on non windows platform")
296+
}
277297
} else if cfg!(feature = "ssl") {
278298
if windows {
279299
// For windows, spnego feature is auto on in case ssl feature is on.

examples/ssl_cert_blob.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ fn read_file(path: impl AsRef<Path>) -> Result<Vec<u8>> {
1616
fn main() -> Result<()> {
1717
let argv = env::args().collect::<Vec<_>>();
1818
if argv.len() < 4 {
19-
bail!("usage: ssl_cert_blob URL CERT KEY");
19+
bail!("usage: ssl_cert_blob URL CERT KEY CAINFO? PASSWORD?");
2020
}
2121
let url = &argv[1];
2222
let cert_path = &argv[2];
2323
let key_path = &argv[3];
24+
let cainfo = if argv.len() >= 5 {
25+
Some(&argv[4])
26+
} else {
27+
None
28+
};
29+
let password = if argv.len() >= 6 {
30+
Some(&argv[5])
31+
} else {
32+
None
33+
};
2434

2535
let mut handle = Easy::new();
2636

@@ -33,9 +43,20 @@ fn main() -> Result<()> {
3343

3444
let cert_blob = read_file(cert_path)?;
3545
let key_blob = read_file(key_path)?;
46+
let ca_blob = if let Some(cainfo) = cainfo {
47+
Some(read_file(cainfo)?)
48+
} else {
49+
None
50+
};
3651

3752
handle.ssl_cert_blob(&cert_blob)?;
3853
handle.ssl_key_blob(&key_blob)?;
54+
if let Some(password) = password {
55+
handle.key_password(password)?;
56+
}
57+
if let Some(ca_blob) = ca_blob {
58+
handle.ssl_cainfo_blob(&ca_blob)?;
59+
}
3960

4061
handle.perform()?;
4162
Ok(())

examples/ssl_client_auth.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::env;
2+
use std::io::{stdout, Write};
3+
4+
use anyhow::{bail, Result};
5+
use curl::easy::Easy;
6+
7+
fn main() -> Result<()> {
8+
let argv = env::args().collect::<Vec<_>>();
9+
if argv.len() < 4 {
10+
bail!("usage: ssl_client_auth URL CERT KEY CAINFO? PASSWORD?");
11+
}
12+
let url = &argv[1];
13+
let cert_path = &argv[2];
14+
let key_path = &argv[3];
15+
let cainfo = if argv.len() >= 5 {
16+
Some(&argv[4])
17+
} else {
18+
None
19+
};
20+
let password = if argv.len() >= 6 {
21+
Some(&argv[5])
22+
} else {
23+
None
24+
};
25+
26+
let mut handle = Easy::new();
27+
28+
handle.url(url)?;
29+
handle.verbose(true)?;
30+
handle.write_function(|data| {
31+
stdout().write_all(data).unwrap();
32+
Ok(data.len())
33+
})?;
34+
35+
handle.ssl_cert(&cert_path)?;
36+
handle.ssl_key(&key_path)?;
37+
if let Some(password) = password {
38+
handle.key_password(password)?;
39+
}
40+
if let Some(cainfo) = cainfo {
41+
handle.cainfo(cainfo)?;
42+
}
43+
44+
handle.perform()?;
45+
Ok(())
46+
}

0 commit comments

Comments
 (0)