2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-10-31 07:55:46 -04:00
|
|
|
pub use deno_native_certs;
|
2021-08-07 08:49:38 -04:00
|
|
|
pub use rustls;
|
2024-07-01 20:09:47 -04:00
|
|
|
use rustls::pki_types::CertificateDer;
|
|
|
|
use rustls::pki_types::PrivateKeyDer;
|
|
|
|
use rustls::pki_types::ServerName;
|
2021-12-06 18:48:11 -05:00
|
|
|
pub use rustls_pemfile;
|
2024-04-25 17:00:04 -04:00
|
|
|
pub use rustls_tokio_stream::*;
|
2021-08-07 08:49:38 -04:00
|
|
|
pub use webpki;
|
|
|
|
pub use webpki_roots;
|
|
|
|
|
2024-07-01 20:09:47 -04:00
|
|
|
use rustls::client::danger::HandshakeSignatureValid;
|
|
|
|
use rustls::client::danger::ServerCertVerified;
|
|
|
|
use rustls::client::danger::ServerCertVerifier;
|
|
|
|
use rustls::client::WebPkiServerVerifier;
|
2021-08-07 08:49:38 -04:00
|
|
|
use rustls::ClientConfig;
|
2023-05-16 20:19:23 -04:00
|
|
|
use rustls::DigitallySignedStruct;
|
2024-07-01 20:09:47 -04:00
|
|
|
use rustls::RootCertStore;
|
2021-12-06 18:48:11 -05:00
|
|
|
use rustls_pemfile::certs;
|
2024-04-08 13:36:34 -04:00
|
|
|
use rustls_pemfile::ec_private_keys;
|
2021-12-06 18:48:11 -05:00
|
|
|
use rustls_pemfile::pkcs8_private_keys;
|
|
|
|
use rustls_pemfile::rsa_private_keys;
|
2021-08-07 08:49:38 -04:00
|
|
|
use serde::Deserialize;
|
2021-08-25 08:25:12 -04:00
|
|
|
use std::io::BufRead;
|
2021-08-07 08:49:38 -04:00
|
|
|
use std::io::BufReader;
|
|
|
|
use std::io::Cursor;
|
2024-07-01 20:09:47 -04:00
|
|
|
use std::net::IpAddr;
|
2021-08-07 08:49:38 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
mod tls_key;
|
|
|
|
pub use tls_key::*;
|
|
|
|
|
2024-10-12 19:53:38 -04:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum TlsError {
|
|
|
|
#[error(transparent)]
|
|
|
|
Rustls(#[from] rustls::Error),
|
|
|
|
#[error("Unable to add pem file to certificate store: {0}")]
|
|
|
|
UnableAddPemFileToCert(std::io::Error),
|
|
|
|
#[error("Unable to decode certificate")]
|
|
|
|
CertInvalid,
|
|
|
|
#[error("No certificates found in certificate data")]
|
|
|
|
CertsNotFound,
|
|
|
|
#[error("No keys found in key data")]
|
|
|
|
KeysNotFound,
|
|
|
|
#[error("Unable to decode key")]
|
|
|
|
KeyDecode,
|
|
|
|
}
|
|
|
|
|
2023-05-01 16:42:05 -04:00
|
|
|
/// Lazily resolves the root cert store.
|
|
|
|
///
|
|
|
|
/// This was done because the root cert store is not needed in all cases
|
|
|
|
/// and takes a bit of time to initialize.
|
|
|
|
pub trait RootCertStoreProvider: Send + Sync {
|
2024-10-12 19:53:38 -04:00
|
|
|
fn get_or_try_init(
|
|
|
|
&self,
|
|
|
|
) -> Result<&RootCertStore, deno_core::error::AnyError>;
|
2023-05-01 16:42:05 -04:00
|
|
|
}
|
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
// This extension has no runtime apis, it only exports some shared native functions.
|
|
|
|
deno_core::extension!(deno_tls);
|
2021-08-07 08:49:38 -04:00
|
|
|
|
2024-07-01 20:09:47 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NoCertificateVerification {
|
|
|
|
pub ic_allowlist: Vec<String>,
|
|
|
|
default_verifier: Arc<WebPkiServerVerifier>,
|
|
|
|
}
|
2022-05-18 07:32:12 -04:00
|
|
|
|
2024-07-01 20:09:47 -04:00
|
|
|
impl NoCertificateVerification {
|
|
|
|
pub fn new(ic_allowlist: Vec<String>) -> Self {
|
|
|
|
Self {
|
|
|
|
ic_allowlist,
|
|
|
|
default_verifier: WebPkiServerVerifier::builder(
|
|
|
|
create_default_root_cert_store().into(),
|
|
|
|
)
|
|
|
|
.build()
|
|
|
|
.unwrap(),
|
|
|
|
}
|
2022-05-18 07:32:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-19 10:09:17 -04:00
|
|
|
impl ServerCertVerifier for NoCertificateVerification {
|
2024-07-01 20:09:47 -04:00
|
|
|
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
|
|
|
|
self.default_verifier.supported_verify_schemes()
|
|
|
|
}
|
|
|
|
|
2021-08-09 10:53:21 -04:00
|
|
|
fn verify_server_cert(
|
|
|
|
&self,
|
2024-07-01 20:09:47 -04:00
|
|
|
end_entity: &rustls::pki_types::CertificateDer<'_>,
|
|
|
|
intermediates: &[rustls::pki_types::CertificateDer<'_>],
|
|
|
|
server_name: &rustls::pki_types::ServerName<'_>,
|
2021-12-06 18:48:11 -05:00
|
|
|
ocsp_response: &[u8],
|
2024-07-01 20:09:47 -04:00
|
|
|
now: rustls::pki_types::UnixTime,
|
2024-10-12 19:53:38 -04:00
|
|
|
) -> Result<ServerCertVerified, rustls::Error> {
|
2024-07-01 20:09:47 -04:00
|
|
|
if self.ic_allowlist.is_empty() {
|
2022-05-18 07:32:12 -04:00
|
|
|
return Ok(ServerCertVerified::assertion());
|
|
|
|
}
|
|
|
|
let dns_name_or_ip_address = match server_name {
|
|
|
|
ServerName::DnsName(dns_name) => dns_name.as_ref().to_owned(),
|
2024-07-01 20:09:47 -04:00
|
|
|
ServerName::IpAddress(ip_address) => {
|
|
|
|
Into::<IpAddr>::into(*ip_address).to_string()
|
|
|
|
}
|
2022-05-18 07:32:12 -04:00
|
|
|
_ => {
|
|
|
|
// NOTE(bartlomieju): `ServerName` is a non-exhaustive enum
|
|
|
|
// so we have this catch all errors here.
|
2024-10-12 19:53:38 -04:00
|
|
|
return Err(rustls::Error::General(
|
|
|
|
"Unknown `ServerName` variant".to_string(),
|
|
|
|
));
|
2021-12-06 18:48:11 -05:00
|
|
|
}
|
2022-05-18 07:32:12 -04:00
|
|
|
};
|
2024-07-01 20:09:47 -04:00
|
|
|
if self.ic_allowlist.contains(&dns_name_or_ip_address) {
|
2022-05-18 07:32:12 -04:00
|
|
|
Ok(ServerCertVerified::assertion())
|
2021-08-09 10:53:21 -04:00
|
|
|
} else {
|
2024-07-01 20:09:47 -04:00
|
|
|
self.default_verifier.verify_server_cert(
|
2022-05-18 07:32:12 -04:00
|
|
|
end_entity,
|
|
|
|
intermediates,
|
|
|
|
server_name,
|
|
|
|
ocsp_response,
|
|
|
|
now,
|
|
|
|
)
|
2021-08-09 10:53:21 -04:00
|
|
|
}
|
|
|
|
}
|
2022-05-18 07:32:12 -04:00
|
|
|
|
|
|
|
fn verify_tls12_signature(
|
|
|
|
&self,
|
|
|
|
message: &[u8],
|
2024-07-01 20:09:47 -04:00
|
|
|
cert: &rustls::pki_types::CertificateDer,
|
2022-05-18 07:32:12 -04:00
|
|
|
dss: &DigitallySignedStruct,
|
2024-10-12 19:53:38 -04:00
|
|
|
) -> Result<HandshakeSignatureValid, rustls::Error> {
|
2024-07-01 20:09:47 -04:00
|
|
|
if self.ic_allowlist.is_empty() {
|
2022-05-18 07:32:12 -04:00
|
|
|
return Ok(HandshakeSignatureValid::assertion());
|
|
|
|
}
|
|
|
|
filter_invalid_encoding_err(
|
2024-07-01 20:09:47 -04:00
|
|
|
self
|
|
|
|
.default_verifier
|
|
|
|
.verify_tls12_signature(message, cert, dss),
|
2022-05-18 07:32:12 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_tls13_signature(
|
|
|
|
&self,
|
|
|
|
message: &[u8],
|
2024-07-01 20:09:47 -04:00
|
|
|
cert: &rustls::pki_types::CertificateDer,
|
2022-05-18 07:32:12 -04:00
|
|
|
dss: &DigitallySignedStruct,
|
2024-10-12 19:53:38 -04:00
|
|
|
) -> Result<HandshakeSignatureValid, rustls::Error> {
|
2024-07-01 20:09:47 -04:00
|
|
|
if self.ic_allowlist.is_empty() {
|
2022-05-18 07:32:12 -04:00
|
|
|
return Ok(HandshakeSignatureValid::assertion());
|
|
|
|
}
|
|
|
|
filter_invalid_encoding_err(
|
2024-07-01 20:09:47 -04:00
|
|
|
self
|
|
|
|
.default_verifier
|
|
|
|
.verify_tls13_signature(message, cert, dss),
|
2022-05-18 07:32:12 -04:00
|
|
|
)
|
|
|
|
}
|
2021-08-09 10:53:21 -04:00
|
|
|
}
|
|
|
|
|
2021-08-07 08:49:38 -04:00
|
|
|
#[derive(Deserialize, Default, Debug, Clone)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
#[serde(default)]
|
|
|
|
pub struct Proxy {
|
|
|
|
pub url: String,
|
|
|
|
pub basic_auth: Option<BasicAuth>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Default, Debug, Clone)]
|
|
|
|
#[serde(default)]
|
|
|
|
pub struct BasicAuth {
|
|
|
|
pub username: String,
|
|
|
|
pub password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_default_root_cert_store() -> RootCertStore {
|
2024-07-01 20:09:47 -04:00
|
|
|
let root_cert_store = rustls::RootCertStore {
|
|
|
|
roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
|
|
|
|
};
|
|
|
|
debug_assert!(!root_cert_store.is_empty());
|
2021-08-07 08:49:38 -04:00
|
|
|
root_cert_store
|
|
|
|
}
|
|
|
|
|
2023-11-01 17:11:01 -04:00
|
|
|
pub enum SocketUse {
|
|
|
|
/// General SSL: No ALPN
|
|
|
|
GeneralSsl,
|
|
|
|
/// HTTP: h1 and h2
|
|
|
|
Http,
|
|
|
|
/// http/1.1 only
|
|
|
|
Http1Only,
|
|
|
|
/// http/2 only
|
|
|
|
Http2Only,
|
|
|
|
}
|
|
|
|
|
2021-08-07 08:49:38 -04:00
|
|
|
pub fn create_client_config(
|
|
|
|
root_cert_store: Option<RootCertStore>,
|
2021-09-30 03:26:15 -04:00
|
|
|
ca_certs: Vec<Vec<u8>>,
|
2021-08-10 07:19:45 -04:00
|
|
|
unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
maybe_cert_chain_and_key: TlsKeys,
|
2023-11-01 17:11:01 -04:00
|
|
|
socket_use: SocketUse,
|
2024-10-12 19:53:38 -04:00
|
|
|
) -> Result<ClientConfig, TlsError> {
|
2021-08-10 07:19:45 -04:00
|
|
|
if let Some(ic_allowlist) = unsafely_ignore_certificate_errors {
|
2021-12-06 18:48:11 -05:00
|
|
|
let client_config = ClientConfig::builder()
|
2024-07-01 20:09:47 -04:00
|
|
|
.dangerous()
|
|
|
|
.with_custom_certificate_verifier(Arc::new(
|
|
|
|
NoCertificateVerification::new(ic_allowlist),
|
|
|
|
));
|
2021-12-06 18:48:11 -05:00
|
|
|
|
|
|
|
// NOTE(bartlomieju): this if/else is duplicated at the end of the body of this function.
|
|
|
|
// However it's not really feasible to deduplicate it as the `client_config` instances
|
|
|
|
// are not type-compatible - one wants "client cert", the other wants "transparency policy
|
|
|
|
// or client cert".
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
let mut client = match maybe_cert_chain_and_key {
|
|
|
|
TlsKeys::Static(TlsKey(cert_chain, private_key)) => client_config
|
2024-07-01 20:09:47 -04:00
|
|
|
.with_client_auth_cert(cert_chain, private_key.clone_key())
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
.expect("invalid client key or certificate"),
|
|
|
|
TlsKeys::Null => client_config.with_no_client_auth(),
|
|
|
|
TlsKeys::Resolver(_) => unimplemented!(),
|
|
|
|
};
|
2021-12-06 18:48:11 -05:00
|
|
|
|
2023-11-01 17:11:01 -04:00
|
|
|
add_alpn(&mut client, socket_use);
|
2021-12-06 18:48:11 -05:00
|
|
|
return Ok(client);
|
2021-12-01 11:13:11 -05:00
|
|
|
}
|
|
|
|
|
2024-07-01 20:09:47 -04:00
|
|
|
let mut root_cert_store =
|
|
|
|
root_cert_store.unwrap_or_else(create_default_root_cert_store);
|
|
|
|
// If custom certs are specified, add them to the store
|
|
|
|
for cert in ca_certs {
|
|
|
|
let reader = &mut BufReader::new(Cursor::new(cert));
|
|
|
|
// This function does not return specific errors, if it fails give a generic message.
|
|
|
|
for r in rustls_pemfile::certs(reader) {
|
|
|
|
match r {
|
|
|
|
Ok(cert) => {
|
|
|
|
root_cert_store.add(cert)?;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2024-10-12 19:53:38 -04:00
|
|
|
return Err(TlsError::UnableAddPemFileToCert(e));
|
2021-12-06 18:48:11 -05:00
|
|
|
}
|
|
|
|
}
|
2024-07-01 20:09:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let client_config =
|
|
|
|
ClientConfig::builder().with_root_certificates(root_cert_store);
|
2021-12-06 18:48:11 -05:00
|
|
|
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
let mut client = match maybe_cert_chain_and_key {
|
|
|
|
TlsKeys::Static(TlsKey(cert_chain, private_key)) => client_config
|
2024-07-01 20:09:47 -04:00
|
|
|
.with_client_auth_cert(cert_chain, private_key.clone_key())
|
refactor(ext/tls): Implement required functionality for later SNI support (#23686)
Precursor to #23236
This implements the SNI features, but uses private symbols to avoid
exposing the functionality at this time. Note that to properly test this
feature, we need to add a way for `connectTls` to specify a hostname.
This is something that should be pushed into that API at a later time as
well.
```ts
Deno.test(
{ permissions: { net: true, read: true } },
async function listenResolver() {
let sniRequests = [];
const listener = Deno.listenTls({
hostname: "localhost",
port: 0,
[resolverSymbol]: (sni: string) => {
sniRequests.push(sni);
return {
cert,
key,
};
},
});
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-1",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
{
const conn = await Deno.connectTls({
hostname: "localhost",
[serverNameSymbol]: "server-2",
port: listener.addr.port,
});
const [_handshake, serverConn] = await Promise.all([
conn.handshake(),
listener.accept(),
]);
conn.close();
serverConn.close();
}
assertEquals(sniRequests, ["server-1", "server-2"]);
listener.close();
},
);
```
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
2024-05-09 12:54:47 -04:00
|
|
|
.expect("invalid client key or certificate"),
|
|
|
|
TlsKeys::Null => client_config.with_no_client_auth(),
|
|
|
|
TlsKeys::Resolver(_) => unimplemented!(),
|
|
|
|
};
|
2021-12-06 18:48:11 -05:00
|
|
|
|
2023-11-01 17:11:01 -04:00
|
|
|
add_alpn(&mut client, socket_use);
|
2021-12-06 18:48:11 -05:00
|
|
|
Ok(client)
|
2021-08-07 08:49:38 -04:00
|
|
|
}
|
|
|
|
|
2023-11-01 17:11:01 -04:00
|
|
|
fn add_alpn(client: &mut ClientConfig, socket_use: SocketUse) {
|
|
|
|
match socket_use {
|
|
|
|
SocketUse::Http1Only => {
|
|
|
|
client.alpn_protocols = vec!["http/1.1".into()];
|
|
|
|
}
|
|
|
|
SocketUse::Http2Only => {
|
|
|
|
client.alpn_protocols = vec!["h2".into()];
|
|
|
|
}
|
|
|
|
SocketUse::Http => {
|
|
|
|
client.alpn_protocols = vec!["h2".into(), "http/1.1".into()];
|
|
|
|
}
|
|
|
|
SocketUse::GeneralSsl => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:25:12 -04:00
|
|
|
pub fn load_certs(
|
|
|
|
reader: &mut dyn BufRead,
|
2024-10-12 19:53:38 -04:00
|
|
|
) -> Result<Vec<CertificateDer<'static>>, TlsError> {
|
2024-07-01 20:09:47 -04:00
|
|
|
let certs: Result<Vec<_>, _> = certs(reader).collect();
|
|
|
|
|
2024-10-12 19:53:38 -04:00
|
|
|
let certs = certs.map_err(|_| TlsError::CertInvalid)?;
|
2021-08-25 08:25:12 -04:00
|
|
|
|
|
|
|
if certs.is_empty() {
|
2024-10-12 19:53:38 -04:00
|
|
|
return Err(TlsError::CertsNotFound);
|
2021-08-25 08:25:12 -04:00
|
|
|
}
|
|
|
|
|
2024-07-01 20:09:47 -04:00
|
|
|
Ok(certs)
|
2021-08-25 08:25:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts with -----BEGIN RSA PRIVATE KEY-----
|
2024-07-01 20:09:47 -04:00
|
|
|
fn load_rsa_keys(
|
|
|
|
mut bytes: &[u8],
|
2024-10-12 19:53:38 -04:00
|
|
|
) -> Result<Vec<PrivateKeyDer<'static>>, TlsError> {
|
2024-07-01 20:09:47 -04:00
|
|
|
let keys: Result<Vec<_>, _> = rsa_private_keys(&mut bytes).collect();
|
2024-10-12 19:53:38 -04:00
|
|
|
let keys = keys.map_err(|_| TlsError::KeyDecode)?;
|
2024-07-01 20:09:47 -04:00
|
|
|
Ok(keys.into_iter().map(PrivateKeyDer::Pkcs1).collect())
|
2021-08-25 08:25:12 -04:00
|
|
|
}
|
|
|
|
|
2024-04-08 13:36:34 -04:00
|
|
|
/// Starts with -----BEGIN EC PRIVATE KEY-----
|
2024-07-01 20:09:47 -04:00
|
|
|
fn load_ec_keys(
|
|
|
|
mut bytes: &[u8],
|
2024-10-12 19:53:38 -04:00
|
|
|
) -> Result<Vec<PrivateKeyDer<'static>>, TlsError> {
|
2024-07-01 20:09:47 -04:00
|
|
|
let keys: Result<Vec<_>, std::io::Error> =
|
|
|
|
ec_private_keys(&mut bytes).collect();
|
2024-10-12 19:53:38 -04:00
|
|
|
let keys2 = keys.map_err(|_| TlsError::KeyDecode)?;
|
2024-07-01 20:09:47 -04:00
|
|
|
Ok(keys2.into_iter().map(PrivateKeyDer::Sec1).collect())
|
2024-04-08 13:36:34 -04:00
|
|
|
}
|
|
|
|
|
2021-08-25 08:25:12 -04:00
|
|
|
/// Starts with -----BEGIN PRIVATE KEY-----
|
2024-07-01 20:09:47 -04:00
|
|
|
fn load_pkcs8_keys(
|
|
|
|
mut bytes: &[u8],
|
2024-10-12 19:53:38 -04:00
|
|
|
) -> Result<Vec<PrivateKeyDer<'static>>, TlsError> {
|
2024-07-01 20:09:47 -04:00
|
|
|
let keys: Result<Vec<_>, std::io::Error> =
|
|
|
|
pkcs8_private_keys(&mut bytes).collect();
|
2024-10-12 19:53:38 -04:00
|
|
|
let keys2 = keys.map_err(|_| TlsError::KeyDecode)?;
|
2024-07-01 20:09:47 -04:00
|
|
|
Ok(keys2.into_iter().map(PrivateKeyDer::Pkcs8).collect())
|
2021-08-25 08:25:12 -04:00
|
|
|
}
|
|
|
|
|
2022-05-18 07:32:12 -04:00
|
|
|
fn filter_invalid_encoding_err(
|
2024-10-12 19:53:38 -04:00
|
|
|
to_be_filtered: Result<HandshakeSignatureValid, rustls::Error>,
|
|
|
|
) -> Result<HandshakeSignatureValid, rustls::Error> {
|
2022-05-18 07:32:12 -04:00
|
|
|
match to_be_filtered {
|
2024-10-12 19:53:38 -04:00
|
|
|
Err(rustls::Error::InvalidCertificate(
|
|
|
|
rustls::CertificateError::BadEncoding,
|
|
|
|
)) => Ok(HandshakeSignatureValid::assertion()),
|
2022-05-18 07:32:12 -04:00
|
|
|
res => res,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:09:47 -04:00
|
|
|
pub fn load_private_keys(
|
|
|
|
bytes: &[u8],
|
2024-10-12 19:53:38 -04:00
|
|
|
) -> Result<Vec<PrivateKeyDer<'static>>, TlsError> {
|
2021-08-25 08:25:12 -04:00
|
|
|
let mut keys = load_rsa_keys(bytes)?;
|
|
|
|
|
|
|
|
if keys.is_empty() {
|
|
|
|
keys = load_pkcs8_keys(bytes)?;
|
|
|
|
}
|
|
|
|
|
2024-04-08 13:36:34 -04:00
|
|
|
if keys.is_empty() {
|
|
|
|
keys = load_ec_keys(bytes)?;
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:25:12 -04:00
|
|
|
if keys.is_empty() {
|
2024-10-12 19:53:38 -04:00
|
|
|
return Err(TlsError::KeysNotFound);
|
2021-08-25 08:25:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(keys)
|
|
|
|
}
|