From 078f03334b2de9d8e464755d8836318b9a221346 Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Fri, 24 Nov 2023 11:38:26 -0500 Subject: [PATCH] provider-example: use Error::Other 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. --- provider-example/Cargo.toml | 2 +- provider-example/src/hpke.rs | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/provider-example/Cargo.toml b/provider-example/Cargo.toml index f42aa061a7..a356a8c49b 100644 --- a/provider-example/Cargo.toml +++ b/provider-example/Cargo.toml @@ -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" diff --git a/provider-example/src/hpke.rs b/provider-example/src/hpke.rs index 58c50f0371..2bdad42f0a 100644 --- a/provider-example/src/hpke.rs +++ b/provider-example/src/hpke.rs @@ -1,4 +1,6 @@ +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; @@ -6,7 +8,7 @@ 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 {}; @@ -18,9 +20,9 @@ impl HpkeProvider for HpkeRsProvider { fn start(&self, suite: &HpkeSuite) -> Result, 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)?, )))) } @@ -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)) } @@ -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))) }