Skip to content

Commit 2447c2e

Browse files
djcRalith
authored andcommitted
book: merge certificate code files
1 parent 3610629 commit 2447c2e

File tree

4 files changed

+41
-49
lines changed

4 files changed

+41
-49
lines changed

docs/book/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ rustls-pemfile.workspace = true
2020
name = "certificate-insecure"
2121
path = "src/quinn/certificate-insecure.rs"
2222

23-
[[bin]]
24-
name = "certificate-certsr"
25-
path = "src/quinn/certificate-certs.rs"
26-
2723
[[bin]]
2824
name = "data-transfer"
2925
path = "src/quinn/data-transfer.rs"

docs/book/src/quinn/certificate-certs.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

docs/book/src/quinn/certificate-insecure.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use std::sync::Arc;
1+
use std::{error::Error, sync::Arc};
22

33
use quinn::{
44
ClientConfig,
55
crypto::rustls::{NoInitialCipherSuite, QuicClientConfig},
66
};
7+
use rustls::pki_types::pem::PemObject;
78

89
// Implementation of `ServerCertVerifier` that verifies everything as trustworthy.
910
#[derive(Debug)]
@@ -71,4 +72,38 @@ fn configure_client() -> Result<ClientConfig, NoInitialCipherSuite> {
7172
)?)))
7273
}
7374

74-
fn main() {}
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")
83+
.unwrap()
84+
.map(|cert| cert.unwrap())
85+
.collect();
86+
let key = rustls::pki_types::PrivateKeyDer::from_pem_file("./privkey.pem").unwrap();
87+
Ok((certs, key))
88+
}
89+
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+
> {
97+
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());
100+
Ok((cert_der, key))
101+
}
102+
103+
#[allow(unused_variables)]
104+
fn main() {
105+
let (self_signed_certs, self_signed_key) = generate_self_signed_cert().unwrap();
106+
let (certs, key) = read_certs_from_file().unwrap();
107+
let server_config = quinn::ServerConfig::with_single_cert(certs, key);
108+
let client_config = quinn::ClientConfig::with_platform_verifier();
109+
}

docs/book/src/quinn/certificate.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 certificate-certs.rs:20:31}}
48+
{{#include certificate-insecure.rs:90:101}}
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 certificate-certs.rs:5:18}}
71+
{{#include certificate-insecure.rs:75:88}}
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 certificate-certs.rs:36}}
82+
{{#include certificate-insecure.rs:107}}
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 certificate-certs.rs:37}}
90+
{{#include certificate-insecure.rs:108}}
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)