Skip to content
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

add EchConfigListBytes for encrypted client hello configs #46

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- stable
- beta
- nightly
os: [ubuntu-20.04]
os: [ubuntu-latest]
# but only stable on macos/windows (slower platforms)
include:
- os: macos-latest
Expand Down Expand Up @@ -57,7 +57,7 @@ jobs:

wasm_build:
name: Build wasm32
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
Expand Down Expand Up @@ -90,7 +90,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, macos-latest, windows-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- name: Checkout sources
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustls-pki-types"
version = "1.4.1"
version = "1.5.0"
edition = "2021"
rust-version = "1.60"
license = "MIT OR Apache-2.0"
Expand Down
140 changes: 111 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ impl PrivatePkcs1KeyDer<'_> {

impl<'a> From<&'a [u8]> for PrivatePkcs1KeyDer<'a> {
fn from(slice: &'a [u8]) -> Self {
Self(Der(DerInner::Borrowed(slice)))
Self(Der(BytesInner::Borrowed(slice)))
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Vec<u8>> for PrivatePkcs1KeyDer<'a> {
fn from(vec: Vec<u8>) -> Self {
Self(Der(DerInner::Owned(vec)))
Self(Der(BytesInner::Owned(vec)))
}
}

Expand Down Expand Up @@ -291,14 +291,14 @@ impl PrivateSec1KeyDer<'_> {

impl<'a> From<&'a [u8]> for PrivateSec1KeyDer<'a> {
fn from(slice: &'a [u8]) -> Self {
Self(Der(DerInner::Borrowed(slice)))
Self(Der(BytesInner::Borrowed(slice)))
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Vec<u8>> for PrivateSec1KeyDer<'a> {
fn from(vec: Vec<u8>) -> Self {
Self(Der(DerInner::Owned(vec)))
Self(Der(BytesInner::Owned(vec)))
}
}

Expand Down Expand Up @@ -333,14 +333,14 @@ impl PrivatePkcs8KeyDer<'_> {

impl<'a> From<&'a [u8]> for PrivatePkcs8KeyDer<'a> {
fn from(slice: &'a [u8]) -> Self {
Self(Der(DerInner::Borrowed(slice)))
Self(Der(BytesInner::Borrowed(slice)))
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Vec<u8>> for PrivatePkcs8KeyDer<'a> {
fn from(vec: Vec<u8>) -> Self {
Self(Der(DerInner::Owned(vec)))
Self(Der(BytesInner::Owned(vec)))
}
}

Expand Down Expand Up @@ -500,6 +500,52 @@ impl CertificateDer<'_> {
}
}

/// A TLS-encoded Encrypted Client Hello (ECH) configuration list (`ECHConfigList`); as specified in
/// [draft-ietf-tls-esni-18 §4](https://datatracker.ietf.org/doc/html/draft-ietf-tls-esni-18#section-4)
#[derive(Clone, Eq, PartialEq)]
pub struct EchConfigListBytes<'a>(BytesInner<'a>);

impl EchConfigListBytes<'_> {
/// Converts this config into its owned variant, unfreezing borrowed content (if any)
#[cfg(feature = "alloc")]
pub fn into_owned(self) -> EchConfigListBytes<'static> {
EchConfigListBytes(self.0.into_owned())
}
}

impl fmt::Debug for EchConfigListBytes<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
hex(f, self.as_ref())
}
}

impl AsRef<[u8]> for EchConfigListBytes<'_> {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}

impl Deref for EchConfigListBytes<'_> {
type Target = [u8];

fn deref(&self) -> &Self::Target {
self.as_ref()
}
}

impl<'a> From<&'a [u8]> for EchConfigListBytes<'a> {
fn from(slice: &'a [u8]) -> Self {
Self(BytesInner::Borrowed(slice))
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Vec<u8>> for EchConfigListBytes<'a> {
fn from(vec: Vec<u8>) -> Self {
Self(BytesInner::Owned(vec))
}
}

/// An abstract signature verification algorithm.
///
/// One of these is needed per supported pair of public key type (identified
Expand Down Expand Up @@ -657,23 +703,19 @@ impl UnixTime {
/// This wrapper type is used to represent DER-encoded data in a way that is agnostic to whether
/// the data is owned (by a `Vec<u8>`) or borrowed (by a `&[u8]`). Support for the owned
/// variant is only available when the `alloc` feature is enabled.
#[derive(Clone)]
pub struct Der<'a>(DerInner<'a>);
#[derive(Clone, Eq, PartialEq)]
pub struct Der<'a>(BytesInner<'a>);

impl<'a> Der<'a> {
/// A const constructor to create a `Der` from a borrowed slice
pub const fn from_slice(der: &'a [u8]) -> Self {
Self(DerInner::Borrowed(der))
Self(BytesInner::Borrowed(der))
}
}

impl AsRef<[u8]> for Der<'_> {
fn as_ref(&self) -> &[u8] {
match &self.0 {
#[cfg(feature = "alloc")]
DerInner::Owned(vec) => vec.as_ref(),
DerInner::Borrowed(slice) => slice,
}
self.0.as_ref()
}
}

Expand All @@ -687,14 +729,14 @@ impl Deref for Der<'_> {

impl<'a> From<&'a [u8]> for Der<'a> {
fn from(slice: &'a [u8]) -> Self {
Self(DerInner::Borrowed(slice))
Self(BytesInner::Borrowed(slice))
}
}

#[cfg(feature = "alloc")]
impl From<Vec<u8>> for Der<'static> {
fn from(vec: Vec<u8>) -> Self {
Self(DerInner::Owned(vec))
Self(BytesInner::Owned(vec))
}
}

Expand All @@ -704,31 +746,41 @@ impl fmt::Debug for Der<'_> {
}
}

impl PartialEq for Der<'_> {
fn eq(&self, other: &Self) -> bool {
self.as_ref().eq(other.as_ref())
}
}

impl Eq for Der<'_> {}

#[derive(Clone)]
enum DerInner<'a> {
#[derive(Debug, Clone)]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: BytesInner also gains a derived Debug in this changeset to be usable with assert_eq! in the added unit test.

enum BytesInner<'a> {
#[cfg(feature = "alloc")]
Owned(Vec<u8>),
Borrowed(&'a [u8]),
}

#[cfg(feature = "alloc")]
impl DerInner<'_> {
fn into_owned(self) -> DerInner<'static> {
DerInner::Owned(match self {
impl BytesInner<'_> {
fn into_owned(self) -> BytesInner<'static> {
BytesInner::Owned(match self {
Self::Owned(vec) => vec,
Self::Borrowed(slice) => slice.to_vec(),
})
}
}

impl AsRef<[u8]> for BytesInner<'_> {
fn as_ref(&self) -> &[u8] {
match &self {
#[cfg(feature = "alloc")]
BytesInner::Owned(vec) => vec.as_ref(),
BytesInner::Borrowed(slice) => slice,
}
}
}

impl PartialEq for BytesInner<'_> {
fn eq(&self, other: &Self) -> bool {
self.as_ref() == other.as_ref()
}
}

impl Eq for BytesInner<'_> {}

// Format an iterator of u8 into a hex string
fn hex<'a>(f: &mut fmt::Formatter<'_>, payload: impl IntoIterator<Item = &'a u8>) -> fmt::Result {
for (i, b) in payload.into_iter().enumerate() {
Expand All @@ -755,4 +807,34 @@ mod tests {
let alg_id = AlgorithmIdentifier::from_slice(&[0x01, 0x02, 0x03]);
assert_eq!(format!("{:?}", alg_id), "0x010203");
}

#[test]
fn bytes_inner_equality() {
let owned_a = BytesInner::Owned(vec![1, 2, 3]);
let owned_b = BytesInner::Owned(vec![4, 5]);
let borrowed_a = BytesInner::Borrowed(&[1, 2, 3]);
let borrowed_b = BytesInner::Borrowed(&[99]);

// Self-equality.
assert_eq!(owned_a, owned_a);
assert_eq!(owned_b, owned_b);
assert_eq!(borrowed_a, borrowed_a);
assert_eq!(borrowed_b, borrowed_b);

// Borrowed vs Owned equality
assert_eq!(owned_a, borrowed_a);
assert_eq!(borrowed_a, owned_a);

// Owned inequality
assert_ne!(owned_a, owned_b);
assert_ne!(owned_b, owned_a);

// Borrowed inequality
assert_ne!(borrowed_a, borrowed_b);
assert_ne!(borrowed_b, borrowed_a);

// Borrowed vs Owned inequality
assert_ne!(owned_a, borrowed_b);
assert_ne!(borrowed_b, owned_a);
}
}
Loading