Skip to content

Commit ab18110

Browse files
authored
Merge pull request #140 from subbu963/main
feat: Add support for custom SSL certificates
2 parents 3161a3e + 3f8a4d4 commit ab18110

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

Cargo.lock

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ crypto-wasi = { version = "0.1.1", optional = true }
3535
chat-prompts = { version = "0.3", optional = true }
3636
wasi-nn = { git = "https://github.com/second-state/wasmedge-wasi-nn", branch = "ggml", optional = true }
3737
endpoints = { version = "0.2", optional = true }
38+
rustls-pemfile = "1.0.4"
3839

3940
[features]
4041
default = ["tls"]

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ cargo build --target wasm32-wasi --release
1313
wasmedge --dir .:. target/wasm32-wasi/release/wasmedge_quickjs.wasm example_js/hello.js WasmEdge Runtime
1414
Hello WasmEdge Runtime
1515
```
16+
17+
### Usage with custom ssl certs
18+
```bash
19+
$ wasmedge --dir .:. --dir /etc/ssl:/etc/ssl:readonly --env SSL_CERT_FILE="/etc/ssl/cert.pem" target/wasm32-wasi/release/wasmedge_quickjs.wasm example_js/wasi_https_fetch.js
20+
```
21+
substitute the value of `/etc/ssl` and `/etc/ssl/cert.pem` with the location of your cert folder and cert file

scripts/get_cert.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
# Check if a domain is provided as an argument
4+
if [ -z "$1" ]; then
5+
echo "Usage: $0 <domain>"
6+
exit 1
7+
fi
8+
9+
# Retrieve and print the combined TLS certificates
10+
openssl s_client -showcerts -connect "$1":443 2>/dev/null < /dev/null | awk '/BEGIN CERTIFICATE/,/END CERTIFICATE/{print}'

src/event_loop/certs.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::{env, io};
2+
use std::fs::File;
3+
use std::io::BufReader;
4+
use rustls::Certificate;
5+
6+
7+
const ENV_CERT_FILE: &str = "SSL_CERT_FILE";
8+
9+
pub fn load_certs_from_env() -> io::Result<Vec<Certificate>> {
10+
let file_name = match env::var(ENV_CERT_FILE) {
11+
Ok(val) => val,
12+
Err(_) => {
13+
return io::Result::Err(io::Error::from(io::ErrorKind::NotFound));
14+
},
15+
};
16+
let file = File::open(file_name)?;
17+
let mut reader = BufReader::new(file);
18+
let mut certs = rustls_pemfile::certs(&mut reader)?;
19+
Ok(certs.into_iter().map(Certificate).collect())
20+
}

src/event_loop/mod.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod poll;
22
pub mod wasi_fs;
33
mod wasi_sock;
4+
mod certs;
45

56
use crate::{quickjs_sys as qjs, Context, JsClassTool, JsValue};
67
use std::borrow::BorrowMut;
@@ -134,13 +135,21 @@ impl AsyncTlsConn {
134135

135136
let io = tokio::net::TcpStream::connect(addr).await?;
136137
let mut root_store = rustls::RootCertStore::empty();
137-
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
138-
OwnedTrustAnchor::from_subject_spki_name_constraints(
139-
ta.subject,
140-
ta.spki,
141-
ta.name_constraints,
142-
)
143-
}));
138+
if let Ok(custom_certs) = certs::load_certs_from_env() {
139+
log::info!("using custom certs");
140+
for cert in custom_certs {
141+
root_store.add(&cert).unwrap();
142+
}
143+
} else {
144+
log::info!("falling back to webpki certs");
145+
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
146+
OwnedTrustAnchor::from_subject_spki_name_constraints(
147+
ta.subject,
148+
ta.spki,
149+
ta.name_constraints,
150+
)
151+
}));
152+
}
144153

145154
let config = rustls::ClientConfig::builder()
146155
.with_safe_defaults()

0 commit comments

Comments
 (0)