Skip to content

Commit bba16eb

Browse files
authored
Allow conversion between Algorithm and KeyAlgorithm (#516)
1 parent 09ccfe6 commit bba16eb

2 files changed

Lines changed: 86 additions & 21 deletions

File tree

src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub enum ErrorKind {
5252
Signing(String),
5353
/// When the algorithm from string doesn't match the one passed to `from_str`
5454
InvalidAlgorithmName,
55+
/// When the algorithm is not supported
56+
UnsupportedAlgorithm,
5557
/// When a key is provided with an invalid format
5658
InvalidKeyFormat,
5759

@@ -106,6 +108,7 @@ impl StdError for Error {
106108
ErrorKind::InvalidSubject => None,
107109
ErrorKind::ImmatureSignature => None,
108110
ErrorKind::InvalidAlgorithm => None,
111+
ErrorKind::UnsupportedAlgorithm => None,
109112
ErrorKind::InvalidAlgorithmName => None,
110113
ErrorKind::InvalidKeyFormat => None,
111114
ErrorKind::Base64(err) => Some(err),
@@ -130,6 +133,7 @@ impl fmt::Display for Error {
130133
| ErrorKind::InvalidSubject
131134
| ErrorKind::ImmatureSignature
132135
| ErrorKind::InvalidAlgorithm
136+
| ErrorKind::UnsupportedAlgorithm
133137
| ErrorKind::InvalidKeyFormat
134138
| ErrorKind::InvalidEddsaKey
135139
| ErrorKind::InvalidAlgorithmName => write!(f, "{:?}", self.0),

src/jwk.rs

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ use std::{fmt, str::FromStr};
88
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
99

1010
use crate::crypto::CryptoProvider;
11+
use crate::errors::{self, Error, ErrorKind, new_error};
1112
use crate::serialization::b64_encode;
12-
use crate::{
13-
Algorithm, AlgorithmFamily, EncodingKey,
14-
errors::{self, Error, ErrorKind},
15-
};
13+
use crate::{Algorithm, AlgorithmFamily, EncodingKey};
1614

1715
/// The intended usage of the public `KeyType`. This enum is serialized `untagged`
1816
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
@@ -221,6 +219,47 @@ impl FromStr for KeyAlgorithm {
221219
}
222220
}
223221

222+
impl From<Algorithm> for KeyAlgorithm {
223+
fn from(alg: Algorithm) -> Self {
224+
match alg {
225+
Algorithm::HS256 => KeyAlgorithm::HS256,
226+
Algorithm::HS384 => KeyAlgorithm::HS384,
227+
Algorithm::HS512 => KeyAlgorithm::HS512,
228+
Algorithm::ES256 => KeyAlgorithm::ES256,
229+
Algorithm::ES384 => KeyAlgorithm::ES384,
230+
Algorithm::RS256 => KeyAlgorithm::RS256,
231+
Algorithm::RS384 => KeyAlgorithm::RS384,
232+
Algorithm::RS512 => KeyAlgorithm::RS512,
233+
Algorithm::PS256 => KeyAlgorithm::PS256,
234+
Algorithm::PS384 => KeyAlgorithm::PS384,
235+
Algorithm::PS512 => KeyAlgorithm::PS512,
236+
Algorithm::EdDSA => KeyAlgorithm::EdDSA,
237+
}
238+
}
239+
}
240+
241+
impl TryFrom<KeyAlgorithm> for Algorithm {
242+
type Error = Error;
243+
244+
fn try_from(alg: KeyAlgorithm) -> Result<Self, Self::Error> {
245+
match alg {
246+
KeyAlgorithm::HS256 => Ok(Algorithm::HS256),
247+
KeyAlgorithm::HS384 => Ok(Algorithm::HS384),
248+
KeyAlgorithm::HS512 => Ok(Algorithm::HS512),
249+
KeyAlgorithm::ES256 => Ok(Algorithm::ES256),
250+
KeyAlgorithm::ES384 => Ok(Algorithm::ES384),
251+
KeyAlgorithm::RS256 => Ok(Algorithm::RS256),
252+
KeyAlgorithm::RS384 => Ok(Algorithm::RS384),
253+
KeyAlgorithm::RS512 => Ok(Algorithm::RS512),
254+
KeyAlgorithm::PS256 => Ok(Algorithm::PS256),
255+
KeyAlgorithm::PS384 => Ok(Algorithm::PS384),
256+
KeyAlgorithm::PS512 => Ok(Algorithm::PS512),
257+
KeyAlgorithm::EdDSA => Ok(Algorithm::EdDSA),
258+
_ => Err(new_error(ErrorKind::UnsupportedAlgorithm)),
259+
}
260+
}
261+
}
262+
224263
impl fmt::Display for KeyAlgorithm {
225264
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
226265
write!(f, "{:?}", self)
@@ -443,23 +482,7 @@ impl Jwk {
443482
/// Edwards curve based keys are not supported.
444483
pub fn from_encoding_key(key: &EncodingKey, alg: Algorithm) -> errors::Result<Self> {
445484
Ok(Self {
446-
common: CommonParameters {
447-
key_algorithm: Some(match alg {
448-
Algorithm::HS256 => KeyAlgorithm::HS256,
449-
Algorithm::HS384 => KeyAlgorithm::HS384,
450-
Algorithm::HS512 => KeyAlgorithm::HS512,
451-
Algorithm::ES256 => KeyAlgorithm::ES256,
452-
Algorithm::ES384 => KeyAlgorithm::ES384,
453-
Algorithm::RS256 => KeyAlgorithm::RS256,
454-
Algorithm::RS384 => KeyAlgorithm::RS384,
455-
Algorithm::RS512 => KeyAlgorithm::RS512,
456-
Algorithm::PS256 => KeyAlgorithm::PS256,
457-
Algorithm::PS384 => KeyAlgorithm::PS384,
458-
Algorithm::PS512 => KeyAlgorithm::PS512,
459-
Algorithm::EdDSA => KeyAlgorithm::EdDSA,
460-
}),
461-
..Default::default()
462-
},
485+
common: CommonParameters { key_algorithm: Some(alg.into()), ..Default::default() },
463486
algorithm: match key.family() {
464487
AlgorithmFamily::Hmac => AlgorithmParameters::OctetKey(OctetKeyParameters {
465488
key_type: OctetKeyType::Octet,
@@ -573,6 +596,7 @@ mod tests {
573596
use wasm_bindgen_test::wasm_bindgen_test;
574597

575598
use crate::Algorithm;
599+
use crate::errors::ErrorKind;
576600
use crate::jwk::{
577601
AlgorithmParameters, Jwk, JwkSet, KeyAlgorithm, OctetKeyType, RSAKeyParameters,
578602
ThumbprintHash,
@@ -634,4 +658,41 @@ mod tests {
634658

635659
assert_eq!(tp.as_str(), "NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs");
636660
}
661+
662+
#[test]
663+
#[wasm_bindgen_test]
664+
fn check_alg_key_alg_conversion() {
665+
let pairs = [
666+
(Algorithm::HS256, KeyAlgorithm::HS256),
667+
(Algorithm::HS384, KeyAlgorithm::HS384),
668+
(Algorithm::HS512, KeyAlgorithm::HS512),
669+
(Algorithm::ES256, KeyAlgorithm::ES256),
670+
(Algorithm::ES384, KeyAlgorithm::ES384),
671+
(Algorithm::RS256, KeyAlgorithm::RS256),
672+
(Algorithm::RS384, KeyAlgorithm::RS384),
673+
(Algorithm::RS512, KeyAlgorithm::RS512),
674+
(Algorithm::PS256, KeyAlgorithm::PS256),
675+
(Algorithm::PS384, KeyAlgorithm::PS384),
676+
(Algorithm::PS512, KeyAlgorithm::PS512),
677+
(Algorithm::EdDSA, KeyAlgorithm::EdDSA),
678+
];
679+
680+
for (alg, k_alg) in pairs {
681+
assert_eq!(KeyAlgorithm::from(alg), k_alg);
682+
assert_eq!(Algorithm::try_from(k_alg), Ok(alg));
683+
}
684+
685+
assert!(
686+
Algorithm::try_from(KeyAlgorithm::RSA1_5)
687+
.is_err_and(|e| *e.kind() == ErrorKind::UnsupportedAlgorithm)
688+
);
689+
assert!(
690+
Algorithm::try_from(KeyAlgorithm::RSA_OAEP)
691+
.is_err_and(|e| *e.kind() == ErrorKind::UnsupportedAlgorithm)
692+
);
693+
assert!(
694+
Algorithm::try_from(KeyAlgorithm::RSA_OAEP_256)
695+
.is_err_and(|e| *e.kind() == ErrorKind::UnsupportedAlgorithm)
696+
);
697+
}
637698
}

0 commit comments

Comments
 (0)