Skip to content

Commit

Permalink
provider-example: use Error::Other
Browse files Browse the repository at this point in the history
Previously we had to use `Error::General` when translating
error instances from the hpke-rs dependencies of the provider-example
into `rustls::error::Error` instances, because one of the upstream error
types didn't implement `StdError`.

This commit updates the hpke-rs dependency, bringing in a fix for this
and allowing usage of the more appropriate `Error::GeneralError` error
type.
  • Loading branch information
cpu committed Nov 27, 2023
1 parent af80fa3 commit 078f033
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion provider-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ der = "0.7.0"
ecdsa = "0.16.8"
env_logger = "0.10"
hmac = "0.12.0"
hpke-rs = "0.1.0"
hpke-rs = "0.1.2"
hpke-rs-crypto = "0.1.2"
hpke-rs-rust-crypto = "0.1.2"
p256 = "0.13.2"
Expand Down
22 changes: 10 additions & 12 deletions provider-example/src/hpke.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::error::Error as StdError;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;

use hpke_rs_crypto::types::{AeadAlgorithm, KdfAlgorithm, KemAlgorithm};
use hpke_rs_crypto::HpkeCrypto;
use hpke_rs_rust_crypto::HpkeRustCrypto;
use rustls::crypto::hpke::{
EncapsulatedSecret, Hpke, HpkePrivateKey, HpkeProvider, HpkePublicKey, HpkeSuite,
};
use rustls::Error;
use rustls::{Error, OtherError};

pub static HPKE_PROVIDER: &'static dyn HpkeProvider = &HpkeRsProvider {};

Expand All @@ -18,9 +20,9 @@ impl HpkeProvider for HpkeRsProvider {
fn start(&self, suite: &HpkeSuite) -> Result<Box<dyn Hpke>, Error> {
Ok(Box::new(HpkeRs(hpke_rs::Hpke::new(
hpke_rs::Mode::Base,
KemAlgorithm::try_from(suite.kem.get_u16()).map_err(general_err)?,
KdfAlgorithm::try_from(suite.sym.kdf_id.get_u16()).map_err(general_err)?,
AeadAlgorithm::try_from(suite.sym.aead_id.get_u16()).map_err(general_err)?,
KemAlgorithm::try_from(suite.kem.get_u16()).map_err(other_err)?,
KdfAlgorithm::try_from(suite.sym.kdf_id.get_u16()).map_err(other_err)?,
AeadAlgorithm::try_from(suite.sym.aead_id.get_u16()).map_err(other_err)?,
))))
}

Expand Down Expand Up @@ -59,7 +61,7 @@ impl Hpke for HpkeRs {
let (enc, ciphertext) = self
.0
.seal(&pk_r, info, aad, plaintext, None, None, None)
.map_err(general_err)?;
.map_err(other_err)?;
Ok((EncapsulatedSecret(enc.to_vec()), ciphertext))
}

Expand All @@ -83,14 +85,10 @@ impl Hpke for HpkeRs {
None,
None,
)
.map_err(general_err)
.map_err(other_err)
}
}

// TODO(XXX): Switch to using `Error::Other(Error::OtherError(err))` once a hpke-rs release
// with https://github.com/franziskuskiefer/hpke-rs/pull/44 is available.
fn general_err(err: impl Debug) -> Error {
// Presently hpke_rs::HpkeError does not implement std::error::Error, so we use Debug
// and create a general error.
Error::General(format!("{:?}", err))
fn other_err(err: impl StdError + Send + Sync + 'static) -> Error {
Error::Other(OtherError(Arc::new(err)))
}

0 comments on commit 078f033

Please sign in to comment.