Skip to content

Commit 895cb2f

Browse files
committed
Avoid leaking crypto errors
1 parent 6a5fd9d commit 895cb2f

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ rustdoc-args = ["--cfg", "docsrs"]
7171

7272
[package.metadata.cargo_check_external_types]
7373
allowed_external_types = [
74-
"aws_lc_rs::*",
7574
"bytes::*",
7675
"http::*",
7776
"http_body::*",

src/account.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ impl Key {
537537
pub fn generate() -> Result<(Self, PrivateKeyDer<'static>), Error> {
538538
let rng = crypto::SystemRandom::new();
539539
let pkcs8 =
540-
crypto::EcdsaKeyPair::generate_pkcs8(&crypto::ECDSA_P256_SHA256_FIXED_SIGNING, &rng)?;
540+
crypto::EcdsaKeyPair::generate_pkcs8(&crypto::ECDSA_P256_SHA256_FIXED_SIGNING, &rng)
541+
.map_err(|_| Error::Crypto)?;
541542
Ok((
542543
Self::new(pkcs8.as_ref(), rng)?,
543544
PrivatePkcs8KeyDer::from(pkcs8.as_ref().to_vec()).into(),
@@ -577,7 +578,10 @@ impl Signer for Key {
577578
}
578579

579580
fn sign(&self, payload: &[u8]) -> Result<Self::Signature, Error> {
580-
Ok(self.inner.sign(&self.rng, payload)?)
581+
self
582+
.inner
583+
.sign(&self.rng, payload)
584+
.map_err(|_| Error::Crypto)
581585
}
582586
}
583587

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,26 +321,29 @@ mod crypto {
321321
pub(crate) use ring as ring_like;
322322

323323
pub(crate) use ring_like::digest::{Digest, SHA256, digest};
324-
pub(crate) use ring_like::error::{KeyRejected, Unspecified};
325324
pub(crate) use ring_like::hmac;
326325
pub(crate) use ring_like::rand::SystemRandom;
327326
pub(crate) use ring_like::signature::{ECDSA_P256_SHA256_FIXED_SIGNING, EcdsaKeyPair};
328327
pub(crate) use ring_like::signature::{KeyPair, Signature};
329328

329+
use super::Error;
330+
330331
#[cfg(feature = "aws-lc-rs")]
331332
pub(crate) fn p256_key_pair_from_pkcs8(
332333
pkcs8: &[u8],
333334
_: &SystemRandom,
334-
) -> Result<EcdsaKeyPair, KeyRejected> {
335+
) -> Result<EcdsaKeyPair, Error> {
335336
EcdsaKeyPair::from_pkcs8(&ECDSA_P256_SHA256_FIXED_SIGNING, pkcs8)
337+
.map_err(|_| Error::KeyRejected)
336338
}
337339

338340
#[cfg(all(feature = "ring", not(feature = "aws-lc-rs")))]
339341
pub(crate) fn p256_key_pair_from_pkcs8(
340342
pkcs8: &[u8],
341343
rng: &SystemRandom,
342-
) -> Result<EcdsaKeyPair, KeyRejected> {
344+
) -> Result<EcdsaKeyPair, Error> {
343345
EcdsaKeyPair::from_pkcs8(&ECDSA_P256_SHA256_FIXED_SIGNING, pkcs8, rng)
346+
.map_err(|_| Error::KeyRejected)
344347
}
345348
}
346349

src/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ pub enum Error {
3131
#[error(transparent)]
3232
Api(#[from] Problem),
3333
/// Failed from cryptographic operations
34-
#[error("cryptographic operation failed: {0}")]
35-
Crypto(#[from] crypto::Unspecified),
34+
#[error("cryptographic operation failed")]
35+
Crypto,
3636
/// Failed to instantiate a private key
37-
#[error("invalid key bytes: {0}")]
38-
CryptoKey(#[from] crypto::KeyRejected),
37+
#[error("invalid key bytes")]
38+
KeyRejected,
3939
/// HTTP failure
4040
#[error("HTTP request failure: {0}")]
4141
Http(#[from] http::Error),

0 commit comments

Comments
 (0)