Skip to content

Commit

Permalink
Store quinn' Certificate instead of rustls' in CertificateChain
Browse files Browse the repository at this point in the history
This allows for a convenient iterator yielding references
  • Loading branch information
kim committed Mar 11, 2020
1 parent 636b764 commit 015c498
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
49 changes: 22 additions & 27 deletions quinn-proto/src/crypto/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ impl From<rustls::Certificate> for Certificate {
}
}

impl Into<rustls::Certificate> for Certificate {
fn into(self) -> rustls::Certificate {
self.inner
}
}

/// A chain of signed TLS certificates ending the one to be used by a server
#[derive(Debug, Clone)]
pub struct CertificateChain {
pub(crate) certs: Vec<rustls::Certificate>,
pub(crate) certs: Vec<Certificate>,
}

impl CertificateChain {
Expand All @@ -44,14 +50,22 @@ impl CertificateChain {
pub fn from_pem(pem: &[u8]) -> Result<Self, ParseError> {
Ok(Self {
certs: pemfile::certs(&mut &pem[..])
.map_err(|()| ParseError("malformed certificate chain"))?,
.map_err(|()| ParseError("malformed certificate chain"))?
.into_iter()
.map(|cert| Certificate { inner: cert })
.collect(),
})
}

/// Construct a certificate chain from a list of certificates
pub fn from_certs(certs: impl IntoIterator<Item = Certificate>) -> Self {
certs.into_iter().collect()
}

/// An iterator over the chain of certificates
pub fn iter(&self) -> impl Iterator<Item = &Certificate> {
self.certs.iter()
}
}

impl std::iter::FromIterator<Certificate> for CertificateChain {
Expand All @@ -60,41 +74,22 @@ impl std::iter::FromIterator<Certificate> for CertificateChain {
T: IntoIterator<Item = Certificate>,
{
CertificateChain {
certs: iter.into_iter().map(|x| x.inner).collect(),
certs: iter.into_iter().collect(),
}
}
}

impl From<Vec<rustls::Certificate>> for CertificateChain {
fn from(certs: Vec<rustls::Certificate>) -> Self {
Self { certs }
}
}

impl IntoIterator for CertificateChain {
type Item = Certificate;
type IntoIter = IntoIter;

fn into_iter(self) -> Self::IntoIter {
IntoIter {
inner: self.certs.into_iter(),
Self {
certs: certs
.into_iter()
.map(|cert| Certificate { inner: cert })
.collect(),
}
}
}

/// Iterator which moves out of a [`CertificateChain`]
pub struct IntoIter {
inner: std::vec::IntoIter<rustls::Certificate>,
}

impl Iterator for IntoIter {
type Item = Certificate;

fn next(&mut self) -> Option<Certificate> {
self.inner.next().map(|cert| Certificate { inner: cert })
}
}

/// The private key of a TLS certificate to be used by a server
#[derive(Debug, Clone)]
pub struct PrivateKey {
Expand Down
9 changes: 8 additions & 1 deletion quinn-proto/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,14 @@ impl ServerConfig<crypto::rustls::TlsSession> {
cert_chain: CertificateChain,
key: PrivateKey,
) -> Result<&mut Self, rustls::TLSError> {
Arc::make_mut(&mut self.crypto).set_single_cert(cert_chain.certs, key.inner)?;
Arc::make_mut(&mut self.crypto).set_single_cert(
cert_chain
.certs
.into_iter()
.map(|cert| cert.into())
.collect(),
key.inner,
)?;
Ok(self)
}
}
Expand Down

0 comments on commit 015c498

Please sign in to comment.