1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
pub mod certificate;
mod upgrade;
mod verifier;
use libp2p_identity::Keypair;
use libp2p_identity::PeerId;
use std::sync::Arc;
pub use futures_rustls::TlsStream;
pub use upgrade::Config;
pub use upgrade::UpgradeError;
const P2P_ALPN: [u8; 6] = *b"libp2p";
pub fn make_client_config(
keypair: &Keypair,
remote_peer_id: Option<PeerId>,
) -> Result<rustls::ClientConfig, certificate::GenError> {
let (certificate, private_key) = certificate::generate(keypair)?;
let mut crypto = rustls::ClientConfig::builder()
.with_cipher_suites(verifier::CIPHERSUITES)
.with_safe_default_kx_groups()
.with_protocol_versions(verifier::PROTOCOL_VERSIONS)
.expect("Cipher suites and kx groups are configured; qed")
.with_custom_certificate_verifier(Arc::new(
verifier::Libp2pCertificateVerifier::with_remote_peer_id(remote_peer_id),
))
.with_single_cert(vec![certificate], private_key)
.expect("Client cert key DER is valid; qed");
crypto.alpn_protocols = vec![P2P_ALPN.to_vec()];
Ok(crypto)
}
pub fn make_server_config(
keypair: &Keypair,
) -> Result<rustls::ServerConfig, certificate::GenError> {
let (certificate, private_key) = certificate::generate(keypair)?;
let mut crypto = rustls::ServerConfig::builder()
.with_cipher_suites(verifier::CIPHERSUITES)
.with_safe_default_kx_groups()
.with_protocol_versions(verifier::PROTOCOL_VERSIONS)
.expect("Cipher suites and kx groups are configured; qed")
.with_client_cert_verifier(Arc::new(verifier::Libp2pCertificateVerifier::new()))
.with_single_cert(vec![certificate], private_key)
.expect("Server cert key DER is valid; qed");
crypto.alpn_protocols = vec![P2P_ALPN.to_vec()];
Ok(crypto)
}