Skip to content

Commit ba3e8b1

Browse files
committed
fix/error: handle crypto error of ring with error chain
1 parent e4abf46 commit ba3e8b1

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/cache/gcs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,14 @@ fn sign_rsa(
312312
alg: &'static dyn signature::RsaEncoding,
313313
) -> Result<String> {
314314
let key_pair = signature::RsaKeyPair::from_pkcs8(key)
315+
.map_err(|e| -> Error { Error::from(e) })
315316
.chain_err(|| "failed to deserialize rsa key")?;
316317

317318
let mut signature = vec![0; key_pair.public_modulus_len()];
318319
let rng = ring::rand::SystemRandom::new();
319320
key_pair
320321
.sign(alg, &rng, signing_input.as_bytes(), &mut signature)
322+
.map_err(|e| -> Error { Error::from(e) })
321323
.chain_err(|| "failed to sign JWT claim")?;
322324

323325
Ok(base64::encode_config(&signature, base64::URL_SAFE_NO_PAD))

src/errors.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ error_chain! {
6161
Which(err: which::Error) {
6262
display("{}", err)
6363
}
64+
CryptoError(msg : String) {
65+
display("Cryptographic ops failed: {}", msg)
66+
}
67+
}
68+
}
69+
70+
71+
72+
impl From<ring::error::Unspecified> for Error {
73+
fn from(e: ring::error::Unspecified) -> Self {
74+
Error::from(ErrorKind::CryptoError(e.to_string()))
75+
}
76+
}
77+
78+
impl From<ring::error::KeyRejected> for Error {
79+
fn from(e: ring::error::KeyRejected) -> Self {
80+
Error::from(ErrorKind::CryptoError(e.to_string()))
6481
}
6582
}
6683

0 commit comments

Comments
 (0)