-
Notifications
You must be signed in to change notification settings - Fork 999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QUIC support #1334
Closed
Closed
QUIC support #1334
Changes from 158 commits
Commits
Show all changes
247 commits
Select commit
Hold shift + click to select a range
7492883
Add libp2p-transport-quic crate
Demi-Marie 1f98ed9
Add copyright notice and beginnings of a transport
Demi-Marie 52b8614
Bogus implementation that at least compiles
Demi-Marie b544f67
Remove an `unimplemented!`
Demi-Marie 432c1b6
Report peer addresses
Demi-Marie 606ede2
Remove code duplication between QUIC and TCP transports
Demi-Marie 10f16ba
Switch back to quinn master
Demi-Marie bf184c5
Implement `dial`
Demi-Marie 2157f52
Add `use` lines for std future related types
Demi-Marie b4fd310
Initial StreamMuxer impl
Demi-Marie 752c844
Last version using ‘quinn’
Demi-Marie 202ccda
Compiling (but not working) quinn-proto based libp2p-quic
Demi-Marie 3a0ba66
More progress on libp2p-quic
Demi-Marie 3c27505
The doc test passes!
Demi-Marie 24728a6
Merge branch 'stable-futures' into demi-quic-stable-futures
Demi-Marie 49ebd43
Preserve order of outgoing connections
Demi-Marie 974d117
Test suite compiles!
Demi-Marie c8ae241
Handle making new connections
Demi-Marie b35d42a
Merge branch 'stable-futures' into demi-quic-stable-futures
Demi-Marie e47e5de
All tests compile
Demi-Marie f19dd46
Remove remaining unimplemented!()
Demi-Marie 67307a3
Merge branch 'stable-futures' into demi-quic-stable-futures
Demi-Marie f26323c
Simple fixes
Demi-Marie 5ca62ae
Implement sending messages
Demi-Marie 40221da
Pin futures_codec to avoid compilation errors
Demi-Marie 5bf85dc
Fix the rest of the compiler errors
Demi-Marie 67bea72
Bump dependencies
Demi-Marie 2d0c619
Merge branch 'dm-bump-deps' into demi-quic-stable-futures
Demi-Marie 7d9d200
Implement complete QUIC protocol logic
Demi-Marie 804cf95
Fix warning in testsuite
Demi-Marie 514ee6b
Switch to parking_lot
Demi-Marie 827424d
Outbound connections must fail when a connection is lost
Demi-Marie ac74e6b
Add missing wakeups
Demi-Marie 5395065
Move all of the driver-waking code into a single function
Demi-Marie 9c2f14d
Consolodate the I/O code
Demi-Marie 59d2065
Refactor some common code
Demi-Marie 98cd81a
More cleanups
Demi-Marie 7a43bf0
Formatting cleanups
Demi-Marie 7406e18
Implement certificate generation for libp2p
Demi-Marie 0914f09
Start working on certificate verification
Demi-Marie f78c89f
Add connection.rs
Demi-Marie 9f5b847
Finish X.509 parsing
Demi-Marie 9e2c63a
Some wrong attempts at certificate verification
Demi-Marie b8091e4
Use webpki to verify the certificate self-signature
Demi-Marie 2f135c1
Remove spurious debug assertion
Demi-Marie fbc7e94
Remove commented-out code and excessive logging
Demi-Marie 0bfe0d0
Cleanup certificate verification
Demi-Marie 20f63a4
Remove connection.rs
Demi-Marie b02cf2e
Get QUIC connections to work
Demi-Marie 9d8c151
Merge branch 'stable-futures' into demi-quic-stable-futures
Demi-Marie b8b9907
Fix the tests to avoid hangs
Demi-Marie bb8dd01
testing
Demi-Marie ef68cb2
Actually accept connections
Demi-Marie 5213f6c
Ensure the driver can actually be woken up
Demi-Marie 920812d
Merge branch 'stable-futures' into demi-quic-stable-futures
Demi-Marie b7ea623
Futures need to be polled to completion!
Demi-Marie a6be20e
Clean up the driver and I/O code
Demi-Marie e765ca8
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie a56a6af
Go much farther before hanging
Demi-Marie 44a6cfb
Make libp2p-quic more robust
Demi-Marie cc80c1a
Cleanups
Demi-Marie 60dc020
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 040d986
Futures portion of libp2p-quic almost complete
Demi-Marie 4dc5f46
Move the endpoint code into a separate module
Demi-Marie b6d137b
Sending outgoing traffic must not block processing incoming traffic
Demi-Marie 5e56934
Another failed attempt to fix the hang!
Demi-Marie d4c6ab1
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie e684c1e
Hopefully fix the hangs
Demi-Marie 522cb33
Fix the hangs for real
Demi-Marie a5a4cd0
Fix wildcard_expansion test case
Demi-Marie 6ab6114
Don’t use keep alives
Demi-Marie 60a40af
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 7d39812
Add a keep-alive interval to prevent spurious timeouts
Demi-Marie d7bbbe1
Don’t use Poll::{self, Pending, Ready}
Demi-Marie 9b98ad8
Do not check for ECONNRESET on transmit
Demi-Marie a30fe76
Refactor networking code
Demi-Marie 0aba2f2
Cleanup I/O paths
Demi-Marie 7c10638
Return peer IDs from Transport impl
Demi-Marie 30c2a1a
Sending endpoint events could require checking for timers
Demi-Marie 62b1f7e
Move the connection code to its own module
Demi-Marie b2564d4
Don’t rely on a fork of `async-std`
Demi-Marie 3d73033
Avoid a panic and work around a quinn-proto bug
Demi-Marie 5e479bd
Remove unnecessary #[allow(dead_code)]
Demi-Marie 01dca1d
Handle a critical Basic Constraints extension
Demi-Marie 2f33051
Let WebPKI verify the certificate self-signature
Demi-Marie 28b889f
Add error file
Demi-Marie 88400d5
Split up certificate verification
Demi-Marie e21c7de
Merge branch 'master' into merged
Demi-Marie c4a86a0
Simplify certificate verification
Demi-Marie 535dda5
Clean up certificate verification code
Demi-Marie e8ea100
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 6b3ca09
Don’t try to use `ifaddrs` in the browser
Demi-Marie 0f22699
Remove ugly workaround and clean up some code
Demi-Marie 05fae6b
Return an error if an unwritten stream is read
Demi-Marie 7e6029c
Reading from an unwritten stream must fail
Demi-Marie d678cc4
Refactor connection code
Demi-Marie a243363
Fix clippy lints and test endpoint shutdown
Demi-Marie 204b9c2
Only forbid warnings when testing
Demi-Marie 35cbc16
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 6fe9091
Add Cargo metadata
Demi-Marie 13e0eb2
Wake up the endpoint driver when needed
Demi-Marie d54f069
Respond to code review and fix tests
Demi-Marie 6b74ccb
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 8ef346d
Move EndpointMessage to endpoint.rs
Demi-Marie a105db5
Reformat
Demi-Marie ee97e31
Move the configuration code into the endpoint
Demi-Marie e22d2a7
Fix comment
Demi-Marie e002946
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie ded54d5
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 1f51fd6
Fix broken links in rustdoc
Demi-Marie f87a4f2
Apply suggestions from code review
Demi-Marie 82755aa
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 2d5981a
Refactor as per @tomaka’s suggestion
Demi-Marie 6db3d49
Implement Transport for Endpoint
Demi-Marie f3e9fca
QUIC is not QUIC/UDP/IP
Demi-Marie 7fef35f
Avoid &Arc<Self>, as it is unstable.
Demi-Marie 7fe8d67
Merge branch 'demi-quic-stable-futures' of github.com:DemiMarie-parit…
Demi-Marie 40895c2
Avoid references from connection to endpoint
Demi-Marie 599154f
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 321f776
Avoid use of deprecated elided lifetimes
Demi-Marie 36c2e7b
Add a bunch of documentation
Demi-Marie 7e119f5
Fix broken links in documentation
Demi-Marie df9a149
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 2d8d15c
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie dfe12a8
Bump dependencies of libp2p-quic (only)
Demi-Marie c18e88e
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie d6794af
Be more selective about enabling lints
Demi-Marie 96aff73
Merge the connection hashmaps and split them into a separate module
Demi-Marie c851d8b
`Muxer` now uses a wrapper around `Connection`
Demi-Marie 984b75d
Consolidate error messages
Demi-Marie ba1c5fb
Tests should not require `Outbound` to implement `Stream`
Demi-Marie 0a2a423
Outbound should not implement Future
Demi-Marie 25eb755
Move timer and last timeout to ConnectionDriver
Demi-Marie 5821540
Enhanced logging
Demi-Marie 40e81fe
Fix shutdown and clean up the code
Demi-Marie b2815d8
Fix doc comments and bump dependencies
Demi-Marie c99784d
Fix compilation error in websockets
Demi-Marie e2e75b0
Adapt to changed rustls API
Demi-Marie 557b2a8
Fix error handling
Demi-Marie 487ae32
Use the public type alias for ‘DistinguishedNames’
Demi-Marie d43ecbc
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 4753706
Fix intra-rustdoc links
Demi-Marie 88d5b45
Use a temporary fork of webpki
Demi-Marie 245b746
Adapt to changed extension handling API in webpki
Demi-Marie 103bd74
Delete unneeded code and refactor
Demi-Marie a9197a0
Simplify code even more
Demi-Marie 212f4f9
Clean up certificate verification
Demi-Marie 579ce9e
Refactor TLS and connection code
Demi-Marie 7775c1f
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 45793ca
Move libp2p-tls to its own crate
Demi-Marie 04edc7e
Move stream_map and stream up a directory
Demi-Marie fd358e3
Move task wakeup into stream_map.rs
Demi-Marie b107974
Inline functions used only once
Demi-Marie 6e1205d
Remove git submodule
Demi-Marie 9e3ef0c
More cleanups
Demi-Marie 50e1053
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie b5b1d10
Fix panic when connection closed too soon
Demi-Marie 9574e01
Be more rigorous about send stream counts
Demi-Marie 5cba805
Use quinn-proto’s built-in send stream count
Demi-Marie dcf0f42
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie d9c2c67
Use a patch instead of a git dependency
Demi-Marie 4d6c709
Apply suggestions from Max Inden
Demi-Marie fa935ce
Cleanup docs and remove a callback
Demi-Marie 5bd1743
Add a span for each test run
Demi-Marie c2d798a
Switch to a working quinn-proto branch
Demi-Marie a20f95a
Move the multiaddr to the configuration
Demi-Marie 9528dc7
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 8f13f20
Switch back to quinn-proto master
Demi-Marie 8dc4a0a
Fix compilation of tests
Demi-Marie 6c072c9
Implement alternate X.509 verifier
Demi-Marie ece2ff2
Fix doc tests
Demi-Marie 26fc93b
protocols/tls needs dangerous_configuration
Demi-Marie fc39b7b
Refactor certificate verification code
Demi-Marie 77fa579
Revert "Refactor certificate verification code"
Demi-Marie 2b701b9
Distinguished unsupported from mismatching signature algorithms
Demi-Marie 35421b6
Replace the old webpki-based verifier
Demi-Marie ac25552
Minor cleanups
Demi-Marie 050e6e4
Remove use of webpki in certificate verification
Demi-Marie 817851c
Use a working quinn-proto version
Demi-Marie 7666267
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie d2ef7d2
Remove unneeded feature in yasna
Demi-Marie 6362998
Packets cannot send themselves
Demi-Marie c775c93
Send an empty distinguished name
Demi-Marie 9395b1d
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 4017506
Make tracing optional
Demi-Marie c3c9393
Update description of libp2p-tls
Demi-Marie d0ab229
libp2p-tls → libp2p-x509
Demi-Marie 5307280
Fix silly macro error
Demi-Marie eff0ec8
Bump async-tls
Demi-Marie d013863
Merge branch 'better-async-tls' into demi-quic-stable-futures
Demi-Marie 729a845
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie def5bd6
Fix silly typos in Cargo.toml
Demi-Marie 9d83c2c
Avoid depending on pem
Demi-Marie f71cbc1
Implement certificate signature verification manually
Demi-Marie 16d26e9
Switch to x509-signature crate
Demi-Marie 53fd874
Better error handling
Demi-Marie b367b9d
Switch to a released version of x509-signature
Demi-Marie 7087dc0
Merge branch 'demi-quic-stable-futures' into external-x509
Demi-Marie 072e2c7
Fix bogus unwrap
Demi-Marie 8ab0a3b
Fix doc test
Demi-Marie 63afb4c
Clean up copyright headers
Demi-Marie 941fa6d
Apply suggestions from code review
Demi-Marie 8f8e58c
Merge branch 'demi-quic-stable-futures' of github.com:DemiMarie-parit…
Demi-Marie b3469a7
Remove unwraps from certificate generation
Demi-Marie fbcb517
Clean up documentation and lints
Demi-Marie 046ba18
Sort dependencies
Demi-Marie e31722e
Remove a potential panic and dead code
Demi-Marie 016de39
Explain that QUIC resends lost packets
Demi-Marie ec3787c
We already ignore transmit I/O errors
Demi-Marie 45fdfd6
Remove unused field
Demi-Marie 4680cab
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie f855893
Update to x509-signature 0.2.0
Demi-Marie a532c11
Switch to the TLS patch that will be merged
Demi-Marie 8570e45
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 416f99e
err-derive ⇒ thiserror
Demi-Marie 90da0f7
‘err-derive’ ⇒ ‘thiserror’ in libp2p-x509
Demi-Marie 5f8a376
Switch to rustls master
Demi-Marie 81f0b0b
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie ebc2382
Bump rcgen
Demi-Marie ec5c331
Bump x509-signature dependency
Demi-Marie 5155e78
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie b9e0b48
Upgrade to the newest x509-signature crate
Demi-Marie 54128d9
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 2e6fd3f
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie a3ba313
Update to quinn-proto and rustls master
Demi-Marie aa4a805
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie 9da5732
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie b0c599e
Merge remote-tracking branch 'upstream/master' into quiccc-again
tomaka a8de82b
QUICk fix
tomaka 341a39d
Working prototype
tomaka 9eb6478
Working well enough
tomaka 5b54d80
Work
tomaka db4c2aa
Merge branch 'master' into demi-quic-stable-futures
Demi-Marie b8fcd4c
Trying to get the tests working
Demi-Marie dfc087c
Merge branch 'master' into quiccc-again
Demi-Marie 63ea646
Do not discard NewAddress messages
Demi-Marie 538f5bb
Merge branch 'quiccc-again' into demi-quic-stable-futures
Demi-Marie 6a5bbe9
Avoid hangs when running tests
Demi-Marie ce977a9
Wait until the handshake is complete
Demi-Marie d7ac0cc
A handshake with no certificate is unfinished
Demi-Marie e7e6d84
Re-enable several tests
Demi-Marie 7f5925f
Fix some warnings in the tests
Demi-Marie 54035a0
Remove some commented-out test code
Demi-Marie 02058a6
Remove unused fields from the endpoint
Demi-Marie 49d6f84
Avoid looping forever if no certificate is sent
Demi-Marie ac583ad
Working libp2p-quic (again)
Demi-Marie aac03f8
Remove some disabled code
Demi-Marie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "libp2p-tls" | ||
version = "0.16.0" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
edition = "2018" | ||
description = "TLS encryption for libp2p" | ||
license = "MIT" | ||
repository = "https://github.com/libp2p/rust-libp2p" | ||
keywords = ["peer-to-peer", "libp2p", "networking", "tls"] | ||
categories = ["network-programming", "asynchronous"] | ||
|
||
[dependencies] | ||
quinn = { git = "https://github.com/djc/quinn", optional = true, package = "quinn-proto" } | ||
rustls = "0.17.0" | ||
ring = "0.16.11" | ||
rcgen = "0.7.0" | ||
webpki = "0.21.2" | ||
untrusted = "0.7.0" | ||
log = "0.4.8" | ||
libp2p-core = { path = "../../core", version = "0.16.0" } | ||
yasna = "0.3.1" | ||
|
||
[features] | ||
default = ["quic"] | ||
quic = ["quinn"] | ||
|
||
[lib] | ||
name = "libp2p_tls" | ||
path = "src/tls.rs" | ||
Demi-Marie marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright 2017-2018 Parity Technologies (UK) Ltd. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the "Software"), | ||
// to deal in the Software without restriction, including without limitation | ||
// the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
// and/or sell copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
//! Certificate handling for libp2p | ||
//! | ||
//! This handles generation, signing, and verification. | ||
//! | ||
//! This crate uses the `log` crate to emit log output. Events that will occur normally are output | ||
//! at `trace` level, while “expected” error conditions (ones that can result during correct use of the | ||
//! library) are logged at `debug` level. | ||
|
||
use super::LIBP2P_SIGNING_PREFIX_LENGTH; | ||
use libp2p_core::identity; | ||
use log::error; | ||
|
||
const LIBP2P_OID: &[u64] = &[1, 3, 6, 1, 4, 1, 53594, 1, 1]; | ||
const LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH: usize = 65; | ||
static LIBP2P_SIGNATURE_ALGORITHM: &rcgen::SignatureAlgorithm = &rcgen::PKCS_ECDSA_P256_SHA256; | ||
// preferred, but not supported by rustls yet | ||
//const LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH: usize = 32; | ||
//static LIBP2P_SIGNATURE_ALGORITHM: &rcgen::SignatureAlgorithm = &rcgen::PKCS_ED25519; | ||
Demi-Marie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// same but with P-384 | ||
//const LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH: usize = 97; | ||
//static LIBP2P_SIGNATURE_ALGORITHM: &rcgen::SignatureAlgorithm = &rcgen::PKCS_ECDSA_P384_SHA384; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we ever use P-384 in libp2p? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No good reason. |
||
|
||
fn encode_signed_key(public_key: identity::PublicKey, signature: &[u8]) -> rcgen::CustomExtension { | ||
let public_key = public_key.into_protobuf_encoding(); | ||
let contents = yasna::construct_der(|writer| { | ||
writer.write_sequence(|writer| { | ||
writer | ||
.next() | ||
.write_bitvec_bytes(&public_key, public_key.len() * 8); | ||
writer | ||
.next() | ||
.write_bitvec_bytes(signature, signature.len() * 8); | ||
}) | ||
}); | ||
let mut ext = rcgen::CustomExtension::from_oid_content(LIBP2P_OID, contents); | ||
ext.set_criticality(true); | ||
ext | ||
} | ||
|
||
fn gen_signed_keypair(keypair: &identity::Keypair) -> (rcgen::KeyPair, rcgen::CustomExtension) { | ||
let temp_keypair = rcgen::KeyPair::generate(&LIBP2P_SIGNATURE_ALGORITHM) | ||
.expect("we pass valid parameters, and assume we have enough memory and randomness; qed"); | ||
let mut signing_buf = | ||
[0u8; LIBP2P_SIGNING_PREFIX_LENGTH + LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH]; | ||
let public = temp_keypair.public_key_raw(); | ||
assert_eq!( | ||
public.len(), | ||
LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH, | ||
"ed25519 public keys are {} bytes", | ||
LIBP2P_SIGNATURE_ALGORITHM_PUBLIC_KEY_LENGTH | ||
); | ||
signing_buf[..LIBP2P_SIGNING_PREFIX_LENGTH].copy_from_slice(&super::LIBP2P_SIGNING_PREFIX[..]); | ||
signing_buf[LIBP2P_SIGNING_PREFIX_LENGTH..].copy_from_slice(public); | ||
let signature = keypair.sign(&signing_buf).expect("signing failed"); | ||
( | ||
temp_keypair, | ||
encode_signed_key(keypair.public(), &signature), | ||
) | ||
} | ||
|
||
/// Generates a self-signed TLS certificate that includes a libp2p-specific certificate extension | ||
/// containing the public key of the given keypair. | ||
pub(crate) fn make_cert(keypair: &identity::Keypair) -> rcgen::Certificate { | ||
let mut params = rcgen::CertificateParams::new(vec![]); | ||
let (cert_keypair, libp2p_extension) = gen_signed_keypair(keypair); | ||
params.custom_extensions.push(libp2p_extension); | ||
params.alg = &LIBP2P_SIGNATURE_ALGORITHM; | ||
params.key_pair = Some(cert_keypair); | ||
rcgen::Certificate::from_params(params) | ||
.expect("certificate generation with valid params will succeed; qed") | ||
} | ||
|
||
/// Extracts the `PeerId` from a certificate’s libp2p extension. It is erroneous | ||
/// to call this unless the certificate is known to be a well-formed X.509 | ||
/// certificate with a valid libp2p extension. The certificate verifiers in this | ||
/// crate validate check this. | ||
/// | ||
/// If you get `Err` from this function, there is a bug somewhere. Either you | ||
/// called it without checking the preconditions, or there is a bug in this | ||
/// library or one of its dependencies. | ||
pub fn extract_peerid(certificate: &[u8]) -> Result<libp2p_core::PeerId, webpki::Error> { | ||
let mut id = None; | ||
let cb = &mut |oid: untrusted::Input<'_>, value, _, _| match oid.as_slice_less_safe() { | ||
super::LIBP2P_OID_BYTES => { | ||
if id.is_some() { | ||
error!( | ||
"multiple libp2p extensions should have been detected \ | ||
earlier; something is wrong" | ||
); | ||
id = Some(Err(webpki::Error::UnknownIssuer)) | ||
} | ||
id = Some(match extract_libp2p_peerid(value) { | ||
Demi-Marie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Ok(value) => Ok(value), | ||
Err(_) => { | ||
error!( | ||
"bogus libp2p extension should have been detected \ | ||
earlier; something is wrong" | ||
); | ||
Err(webpki::Error::UnknownIssuer) | ||
} | ||
}); | ||
webpki::Understood::Yes | ||
} | ||
_ => webpki::Understood::No, | ||
}; | ||
webpki::EndEntityCert::from_with_extension_cb(certificate, cb)?; | ||
id.unwrap_or(Err(webpki::Error::UnknownIssuer)) | ||
} | ||
|
||
fn extract_libp2p_peerid( | ||
extension: untrusted::Input<'_>, | ||
) -> Result<libp2p_core::PeerId, ring::error::Unspecified> { | ||
use ring::{error::Unspecified, io::der}; | ||
extension | ||
.read_all(Unspecified, |mut reader| { | ||
let inner = der::expect_tag_and_get_value(&mut reader, der::Tag::Sequence)?; | ||
inner.read_all(Unspecified, |mut reader| { | ||
let public_key = | ||
der::bit_string_with_no_unused_bits(&mut reader)?.as_slice_less_safe(); | ||
der::bit_string_with_no_unused_bits(&mut reader)?; | ||
identity::PublicKey::from_protobuf_encoding(public_key).map_err(|_| Unspecified) | ||
}) | ||
}) | ||
.map(From::from) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2020 Parity Technologies (UK) Ltd. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the "Software"), | ||
// to deal in the Software without restriction, including without limitation | ||
// the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
// and/or sell copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
//! TLS configuration for `libp2p-quic`. | ||
#![deny( | ||
Demi-Marie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exceeding_bitshifts, | ||
invalid_type_param_default, | ||
missing_fragment_specifier, | ||
mutable_transmutes, | ||
no_mangle_const_items, | ||
overflowing_literals, | ||
patterns_in_fns_without_body, | ||
pub_use_of_private_extern_crate, | ||
unknown_crate_types, | ||
const_err, | ||
order_dependent_trait_objects, | ||
illegal_floating_point_literal_pattern, | ||
improper_ctypes, | ||
late_bound_lifetime_arguments, | ||
non_camel_case_types, | ||
non_shorthand_field_patterns, | ||
non_snake_case, | ||
non_upper_case_globals, | ||
no_mangle_generic_items, | ||
path_statements, | ||
private_in_public, | ||
safe_packed_borrows, | ||
stable_features, | ||
type_alias_bounds, | ||
tyvar_behind_raw_pointer, | ||
unconditional_recursion, | ||
unused, | ||
unused_allocation, | ||
unused_comparisons, | ||
unused_mut, | ||
unreachable_pub, | ||
while_true, | ||
anonymous_parameters, | ||
bare_trait_objects, | ||
elided_lifetimes_in_paths, | ||
missing_copy_implementations, | ||
missing_debug_implementations, | ||
missing_docs, | ||
single_use_lifetimes, | ||
trivial_casts, | ||
trivial_numeric_casts, | ||
unused_extern_crates, | ||
unused_import_braces, | ||
unused_qualifications, | ||
clippy::all | ||
)] | ||
#![forbid(unsafe_code)] | ||
|
||
mod certificate; | ||
mod verifier; | ||
|
||
pub use certificate::extract_peerid; | ||
use std::sync::Arc; | ||
|
||
const LIBP2P_SIGNING_PREFIX: [u8; 21] = *b"libp2p-tls-handshake:"; | ||
const LIBP2P_SIGNING_PREFIX_LENGTH: usize = LIBP2P_SIGNING_PREFIX.len(); | ||
const LIBP2P_OID_BYTES: &[u8] = &[43, 6, 1, 4, 1, 131, 162, 90, 1, 1]; | ||
|
||
fn make_client_config( | ||
certificate: rustls::Certificate, | ||
key: rustls::PrivateKey, | ||
verifier: Arc<verifier::Libp2pCertificateVerifier>, | ||
) -> rustls::ClientConfig { | ||
let mut crypto = rustls::ClientConfig::new(); | ||
crypto.versions = vec![rustls::ProtocolVersion::TLSv1_3]; | ||
crypto.enable_early_data = true; | ||
crypto | ||
.set_single_client_cert(vec![certificate], key) | ||
.expect("we have a valid certificate; qed"); | ||
crypto.dangerous().set_certificate_verifier(verifier); | ||
crypto | ||
} | ||
|
||
fn make_server_config( | ||
certificate: rustls::Certificate, | ||
key: rustls::PrivateKey, | ||
verifier: Arc<verifier::Libp2pCertificateVerifier>, | ||
) -> rustls::ServerConfig { | ||
let mut crypto = rustls::ServerConfig::new(verifier); | ||
crypto.versions = vec![rustls::ProtocolVersion::TLSv1_3]; | ||
crypto | ||
.set_single_cert(vec![certificate], key) | ||
.expect("we have a valid certificate; qed"); | ||
crypto | ||
} | ||
|
||
/// Create TLS client and server configurations for libp2p. | ||
pub fn make_tls_config( | ||
keypair: &libp2p_core::identity::Keypair, | ||
) -> (rustls::ClientConfig, rustls::ServerConfig) { | ||
let cert = certificate::make_cert(&keypair); | ||
let private_key = cert.serialize_private_key_der(); | ||
let verifier = Arc::new(verifier::Libp2pCertificateVerifier); | ||
let cert = rustls::Certificate( | ||
cert.serialize_der() | ||
.expect("serialization of a valid cert will succeed; qed"), | ||
); | ||
let key = rustls::PrivateKey(private_key); | ||
( | ||
make_client_config(cert.clone(), key.clone(), verifier.clone()), | ||
make_server_config(cert, key, verifier), | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that you're trying to reduce code duplication, but right now the
multiaddr
crate is a no-std-friendly crate that doesn't rely on any feature of the operating system. I'm not sure that it's worth breaking this assumption.On the other hand, I don't really know where to put this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can make this conditional on an
std
feature.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suggestion is to put this into core, specifically https://github.com/libp2p/rust-libp2p/blob/master/core/src/translation.rs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I get some opinions regarding the suggestion to put this into core? I also think that
host_addresses
conflates too much functionality for something reusable. Its primary concern is getting the host IP and network addresses which suggests a signature like:or even better:
A
Multiaddr
(with arbitrary suffix) can easily be constructed by mapping over the result.