1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-14 04:41:15 +00:00
actix-web/awc/tests/test_rustls_client.rs

147 lines
4.4 KiB
Rust
Raw Normal View History

#![cfg(feature = "rustls-0_23-webpki-roots")]
2021-01-17 05:19:32 +00:00
extern crate tls_rustls_0_23 as rustls;
2021-01-17 05:19:32 +00:00
use std::{
io::BufReader,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
use actix_http::HttpService;
2019-12-12 17:08:38 +00:00
use actix_http_test::test_server;
2021-04-16 19:48:37 +00:00
use actix_service::{fn_service, map_config, ServiceFactoryExt};
use actix_tls::connect::rustls_0_23::webpki_roots_cert_store;
2021-04-01 14:26:13 +00:00
use actix_utils::future::ok;
2021-01-17 05:19:32 +00:00
use actix_web::{dev::AppConfig, http::Version, web, App, HttpResponse};
use rustls::{
2024-02-03 23:55:01 +00:00
pki_types::{CertificateDer, PrivateKeyDer, ServerName},
ClientConfig, ServerConfig,
};
use rustls_pemfile::{certs, pkcs8_private_keys};
2021-01-17 05:19:32 +00:00
fn tls_config() -> ServerConfig {
2024-05-19 09:12:32 +00:00
let rcgen::CertifiedKey { cert, key_pair } =
rcgen::generate_simple_self_signed(["localhost".to_owned()]).unwrap();
let cert_file = cert.pem();
let key_file = key_pair.serialize_pem();
2021-01-17 05:19:32 +00:00
let cert_file = &mut BufReader::new(cert_file.as_bytes());
let key_file = &mut BufReader::new(key_file.as_bytes());
2024-02-03 23:55:01 +00:00
let cert_chain = certs(cert_file).collect::<Result<Vec<_>, _>>().unwrap();
let mut keys = pkcs8_private_keys(key_file)
.collect::<Result<Vec<_>, _>>()
.unwrap();
2021-01-17 05:19:32 +00:00
ServerConfig::builder()
.with_no_client_auth()
2024-02-03 23:55:01 +00:00
.with_single_cert(cert_chain, PrivateKeyDer::Pkcs8(keys.remove(0)))
.unwrap()
}
mod danger {
2024-02-03 23:55:01 +00:00
use rustls::{
client::danger::{ServerCertVerified, ServerCertVerifier},
pki_types::UnixTime,
};
use super::*;
2024-02-03 23:55:01 +00:00
#[derive(Debug)]
2021-01-17 05:19:32 +00:00
pub struct NoCertificateVerification;
impl ServerCertVerifier for NoCertificateVerification {
fn verify_server_cert(
&self,
2024-02-03 23:55:01 +00:00
_end_entity: &CertificateDer<'_>,
_intermediates: &[CertificateDer<'_>],
_server_name: &ServerName<'_>,
_ocsp_response: &[u8],
2024-02-03 23:55:01 +00:00
_now: UnixTime,
) -> Result<ServerCertVerified, rustls::Error> {
2024-02-03 23:55:01 +00:00
Ok(rustls::client::danger::ServerCertVerified::assertion())
}
fn verify_tls12_signature(
&self,
_message: &[u8],
_cert: &CertificateDer<'_>,
_dss: &rustls::DigitallySignedStruct,
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
}
fn verify_tls13_signature(
&self,
_message: &[u8],
_cert: &CertificateDer<'_>,
_dss: &rustls::DigitallySignedStruct,
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
}
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
rustls::crypto::aws_lc_rs::default_provider()
2024-02-03 23:55:01 +00:00
.signature_verification_algorithms
.supported_schemes()
}
}
}
2021-01-17 05:19:32 +00:00
#[actix_rt::test]
async fn test_connection_reuse_h2() {
2019-11-26 05:25:50 +00:00
let num = Arc::new(AtomicUsize::new(0));
let num2 = num.clone();
2019-12-12 17:08:38 +00:00
let srv = test_server(move || {
2019-11-26 05:25:50 +00:00
let num2 = num2.clone();
2021-04-16 19:48:37 +00:00
fn_service(move |io| {
2019-11-26 05:25:50 +00:00
num2.fetch_add(1, Ordering::Relaxed);
ok(io)
})
.and_then(
HttpService::build()
2019-12-20 11:36:48 +00:00
.h2(map_config(
2021-02-11 23:03:17 +00:00
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok))),
2019-12-20 11:36:48 +00:00
|_| AppConfig::default(),
2019-12-20 11:27:32 +00:00
))
.rustls_0_23(tls_config())
2019-11-26 05:25:50 +00:00
.map_err(|_| ()),
)
2020-03-08 07:42:45 +00:00
})
.await;
let mut config = ClientConfig::builder()
2021-10-20 21:32:05 +00:00
.with_root_certificates(webpki_roots_cert_store())
.with_no_client_auth();
2019-11-26 05:25:50 +00:00
let protos = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
config.alpn_protocols = protos;
// disable TLS verification
2019-11-26 05:25:50 +00:00
config
.dangerous()
2021-01-17 05:19:32 +00:00
.set_certificate_verifier(Arc::new(danger::NoCertificateVerification));
let client = awc::Client::builder()
.connector(awc::Connector::new().rustls_0_23(Arc::new(config)))
2019-11-26 05:25:50 +00:00
.finish();
2019-11-26 05:25:50 +00:00
// req 1
let request = client.get(srv.surl("/")).send();
let response = request.await.unwrap();
assert!(response.status().is_success());
2019-11-26 05:25:50 +00:00
// req 2
let req = client.post(srv.surl("/"));
let response = req.send().await.unwrap();
assert!(response.status().is_success());
assert_eq!(response.version(), Version::HTTP_2);
2019-11-26 05:25:50 +00:00
// one connection
assert_eq!(num.load(Ordering::Relaxed), 1);
}