Skip to content

Commit

Permalink
Merge pull request #140 from subbu963/main
Browse files Browse the repository at this point in the history
feat: Add support for custom SSL certificates
  • Loading branch information
L-jasmine authored May 31, 2024
2 parents 3161a3e + 3f8a4d4 commit ab18110
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ crypto-wasi = { version = "0.1.1", optional = true }
chat-prompts = { version = "0.3", optional = true }
wasi-nn = { git = "https://github.com/second-state/wasmedge-wasi-nn", branch = "ggml", optional = true }
endpoints = { version = "0.2", optional = true }
rustls-pemfile = "1.0.4"

[features]
default = ["tls"]
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ cargo build --target wasm32-wasi --release
wasmedge --dir .:. target/wasm32-wasi/release/wasmedge_quickjs.wasm example_js/hello.js WasmEdge Runtime
Hello WasmEdge Runtime
```

### Usage with custom ssl certs
```bash
$ 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
```
substitute the value of `/etc/ssl` and `/etc/ssl/cert.pem` with the location of your cert folder and cert file
10 changes: 10 additions & 0 deletions scripts/get_cert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Check if a domain is provided as an argument
if [ -z "$1" ]; then
echo "Usage: $0 <domain>"
exit 1
fi

# Retrieve and print the combined TLS certificates
openssl s_client -showcerts -connect "$1":443 2>/dev/null < /dev/null | awk '/BEGIN CERTIFICATE/,/END CERTIFICATE/{print}'
20 changes: 20 additions & 0 deletions src/event_loop/certs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::{env, io};
use std::fs::File;
use std::io::BufReader;
use rustls::Certificate;


const ENV_CERT_FILE: &str = "SSL_CERT_FILE";

pub fn load_certs_from_env() -> io::Result<Vec<Certificate>> {
let file_name = match env::var(ENV_CERT_FILE) {
Ok(val) => val,
Err(_) => {
return io::Result::Err(io::Error::from(io::ErrorKind::NotFound));
},
};
let file = File::open(file_name)?;
let mut reader = BufReader::new(file);
let mut certs = rustls_pemfile::certs(&mut reader)?;
Ok(certs.into_iter().map(Certificate).collect())
}
23 changes: 16 additions & 7 deletions src/event_loop/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod poll;
pub mod wasi_fs;
mod wasi_sock;
mod certs;

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

let io = tokio::net::TcpStream::connect(addr).await?;
let mut root_store = rustls::RootCertStore::empty();
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
if let Ok(custom_certs) = certs::load_certs_from_env() {
log::info!("using custom certs");
for cert in custom_certs {
root_store.add(&cert).unwrap();
}
} else {
log::info!("falling back to webpki certs");
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
}

let config = rustls::ClientConfig::builder()
.with_safe_defaults()
Expand Down

0 comments on commit ab18110

Please sign in to comment.