Skip to content

Commit 48e0bb3

Browse files
djcRalith
authored andcommitted
book: import more types
1 parent ab0596a commit 48e0bb3

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

docs/book/src/bin/certificate.rs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,43 @@ use quinn::{
44
ClientConfig,
55
crypto::rustls::{NoInitialCipherSuite, QuicClientConfig},
66
};
7-
use rustls::pki_types::pem::PemObject;
7+
use rustls::{
8+
DigitallySignedStruct, SignatureScheme,
9+
client::danger,
10+
crypto::{CryptoProvider, verify_tls12_signature, verify_tls13_signature},
11+
pki_types::{
12+
CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, ServerName, UnixTime, pem::PemObject,
13+
},
14+
};
815

916
// Implementation of `ServerCertVerifier` that verifies everything as trustworthy.
1017
#[derive(Debug)]
11-
struct SkipServerVerification(Arc<rustls::crypto::CryptoProvider>);
18+
struct SkipServerVerification(Arc<CryptoProvider>);
1219

1320
impl SkipServerVerification {
1421
fn new() -> Arc<Self> {
1522
Arc::new(Self(Arc::new(rustls::crypto::ring::default_provider())))
1623
}
1724
}
1825

19-
impl rustls::client::danger::ServerCertVerifier for SkipServerVerification {
26+
impl danger::ServerCertVerifier for SkipServerVerification {
2027
fn verify_server_cert(
2128
&self,
22-
_end_entity: &rustls::pki_types::CertificateDer<'_>,
23-
_intermediates: &[rustls::pki_types::CertificateDer<'_>],
24-
_server_name: &rustls::pki_types::ServerName<'_>,
29+
_end_entity: &CertificateDer<'_>,
30+
_intermediates: &[CertificateDer<'_>],
31+
_server_name: &ServerName<'_>,
2532
_ocsp: &[u8],
26-
_now: rustls::pki_types::UnixTime,
27-
) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
28-
Ok(rustls::client::danger::ServerCertVerified::assertion())
33+
_now: UnixTime,
34+
) -> Result<danger::ServerCertVerified, rustls::Error> {
35+
Ok(danger::ServerCertVerified::assertion())
2936
}
3037
fn verify_tls12_signature(
3138
&self,
3239
message: &[u8],
33-
cert: &rustls::pki_types::CertificateDer<'_>,
34-
dss: &rustls::DigitallySignedStruct,
35-
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
36-
rustls::crypto::verify_tls12_signature(
40+
cert: &CertificateDer<'_>,
41+
dss: &DigitallySignedStruct,
42+
) -> Result<danger::HandshakeSignatureValid, rustls::Error> {
43+
verify_tls12_signature(
3744
message,
3845
cert,
3946
dss,
@@ -44,18 +51,18 @@ impl rustls::client::danger::ServerCertVerifier for SkipServerVerification {
4451
fn verify_tls13_signature(
4552
&self,
4653
message: &[u8],
47-
cert: &rustls::pki_types::CertificateDer<'_>,
48-
dss: &rustls::DigitallySignedStruct,
49-
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
50-
rustls::crypto::verify_tls13_signature(
54+
cert: &CertificateDer<'_>,
55+
dss: &DigitallySignedStruct,
56+
) -> Result<danger::HandshakeSignatureValid, rustls::Error> {
57+
verify_tls13_signature(
5158
message,
5259
cert,
5360
dss,
5461
&self.0.signature_verification_algorithms,
5562
)
5663
}
5764

58-
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
65+
fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
5966
self.0.signature_verification_algorithms.supported_schemes()
6067
}
6168
}
@@ -72,31 +79,21 @@ fn configure_client() -> Result<ClientConfig, NoInitialCipherSuite> {
7279
)?)))
7380
}
7481

75-
fn read_certs_from_file() -> Result<
76-
(
77-
Vec<rustls::pki_types::CertificateDer<'static>>,
78-
rustls::pki_types::PrivateKeyDer<'static>,
79-
),
80-
Box<dyn Error>,
81-
> {
82-
let certs = rustls::pki_types::CertificateDer::pem_file_iter("./fullchain.pem")
82+
fn read_certs_from_file()
83+
-> Result<(Vec<CertificateDer<'static>>, PrivateKeyDer<'static>), Box<dyn Error>> {
84+
let certs = CertificateDer::pem_file_iter("./fullchain.pem")
8385
.unwrap()
8486
.map(|cert| cert.unwrap())
8587
.collect();
86-
let key = rustls::pki_types::PrivateKeyDer::from_pem_file("./privkey.pem").unwrap();
88+
let key = PrivateKeyDer::from_pem_file("./privkey.pem").unwrap();
8789
Ok((certs, key))
8890
}
8991

90-
fn generate_self_signed_cert() -> Result<
91-
(
92-
rustls::pki_types::CertificateDer<'static>,
93-
rustls::pki_types::PrivatePkcs8KeyDer<'static>,
94-
),
95-
Box<dyn Error>,
96-
> {
92+
fn generate_self_signed_cert()
93+
-> Result<(CertificateDer<'static>, PrivatePkcs8KeyDer<'static>), Box<dyn Error>> {
9794
let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_string()])?;
98-
let cert_der = rustls::pki_types::CertificateDer::from(cert.cert);
99-
let key = rustls::pki_types::PrivatePkcs8KeyDer::from(cert.key_pair.serialize_der());
95+
let cert_der = CertificateDer::from(cert.cert);
96+
let key = PrivatePkcs8KeyDer::from(cert.key_pair.serialize_der());
10097
Ok((cert_der, key))
10198
}
10299

docs/book/src/quinn/certificate.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ rustls = { version = "*", features = ["dangerous_configuration", "quic"] }
1919
Then, allow the client to skip the certificate validation by implementing [ServerCertVerifier][ServerCertVerifier] and letting it assert verification for any server.
2020

2121
```rust
22-
{{#include ../bin/certificate.rs:8:60}}
22+
{{#include ../bin/certificate.rs:16:68}}
2323
```
2424

2525
After that, modify the [ClientConfig][ClientConfig] to use this [ServerCertVerifier][ServerCertVerifier] implementation.
2626

2727
```rust
28-
{{#include ../bin/certificate.rs:63:72}}
28+
{{#include ../bin/certificate.rs:71:80}}
2929
```
3030

3131
Finally, if you plug this [ClientConfig][ClientConfig] into the [Endpoint::set_default_client_config()][set_default_client_config] your client endpoint should verify all connections as trustworthy.
@@ -45,7 +45,7 @@ This example uses [rcgen][4] to generate a certificate.
4545
Let's look at an example:
4646

4747
```rust
48-
{{#include ../bin/certificate.rs:90:101}}
48+
{{#include ../bin/certificate.rs:92:98}}
4949
```
5050

5151
_Note that [generate_simple_self_signed][generate_simple_self_signed] returns a [Certificate][2] that can be serialized to both `.der` and `.pem` formats._
@@ -68,7 +68,7 @@ certbot asks for the required data and writes the certificates to `fullchain.pem
6868
These files can then be referenced in code.
6969

7070
```rust
71-
{{#include ../bin/certificate.rs:75:88}}
71+
{{#include ../bin/certificate.rs:82:90}}
7272
```
7373

7474
### Configuring Certificates
@@ -79,15 +79,15 @@ After configuring plug the configuration into the `Endpoint`.
7979
**Configure Server**
8080

8181
```rust
82-
{{#include ../bin/certificate.rs:107}}
82+
{{#include ../bin/certificate.rs:104}}
8383
```
8484

8585
This is the only thing you need to do for your server to be secured.
8686

8787
**Configure Client**
8888

8989
```rust
90-
{{#include ../bin/certificate.rs:108}}
90+
{{#include ../bin/certificate.rs:105}}
9191
```
9292

9393
This is the only thing you need to do for your client to trust a server certificate signed by a conventional certificate authority.

0 commit comments

Comments
 (0)