Skip to content

Commit

Permalink
update to latest wtransport (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah authored Jun 22, 2024
1 parent f7e5e2f commit 65a72a6
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 118 deletions.
82 changes: 50 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tracing = "0.1.40"
tracing-subscriber = "0.3.18"
url = "2.5.0"
uuid = "1.7.0"
wtransport = "0.1.11"
wtransport = "0.1.13"

[workspace.dependencies.derive_more]
version = "0.99"
Expand Down
73 changes: 73 additions & 0 deletions crates/replicate/server/src/chad/certificate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use base64::prelude::{Engine, BASE64_URL_SAFE_NO_PAD};

/// Newtype on [`wtransport::Identity`].
pub(super) struct Certificate {
pub(super) cert: wtransport::Identity,
pub(super) base64_hash: String,
}

impl Certificate {
pub fn new(cert: wtransport::Identity) -> Self {
let cert_hash = cert
.certificate_chain()
.as_slice()
.last()
.expect("should be at least one cert")
.hash();
let base64_hash = BASE64_URL_SAFE_NO_PAD.encode(cert_hash.as_ref());
Self { cert, base64_hash }
}

pub fn self_signed<I, S>(
subject_alt_names: I,
) -> Result<Self, wtransport::tls::error::InvalidSan>
where
I: Iterator<Item = S>,
S: AsRef<str>,
{
wtransport::Identity::self_signed(subject_alt_names).map(Self::new)
}
}

impl Clone for Certificate {
fn clone(&self) -> Self {
Self {
cert: self.cert.clone_identity(),
base64_hash: self.base64_hash.clone(),
}
}
}

impl From<wtransport::Identity> for Certificate {
fn from(value: wtransport::Identity) -> Self {
Self::new(value)
}
}

impl std::fmt::Debug for Certificate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple(std::any::type_name::<Self>())
.field(&self.cert.certificate_chain())
.finish()
}
}

impl std::fmt::Display for Certificate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list()
.entries(
self.cert
.certificate_chain()
.as_slice()
.iter()
.map(|c| c.hash().fmt(wtransport::tls::Sha256DigestFmt::DottedHex)),
)
.finish()
}
}

impl AsRef<wtransport::Identity> for Certificate {
fn as_ref(&self) -> &wtransport::Identity {
&self.cert
}
}
Loading

0 comments on commit 65a72a6

Please sign in to comment.