From 188772365a1ab85a9ff1f3e94f11d1e269bcf823 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Sun, 12 Jul 2020 18:57:57 +0000 Subject: [PATCH] Initial commit --- .gitignore | 2 + .rustfmt.toml | 64 +++++++++++ Cargo.toml | 20 ++++ LICENSE | 7 ++ README.md | 10 ++ src/byte_array.rs | 97 +++++++++++++++++ src/byte_vec.rs | 52 +++++++++ src/key_ops.rs | 58 ++++++++++ src/lib.rs | 193 ++++++++++++++++++++++++++++++++++ src/tests.rs | 263 ++++++++++++++++++++++++++++++++++++++++++++++ src/utils.rs | 32 ++++++ 11 files changed, 798 insertions(+) create mode 100644 .gitignore create mode 100644 .rustfmt.toml create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/byte_array.rs create mode 100644 src/byte_vec.rs create mode 100644 src/key_ops.rs create mode 100644 src/lib.rs create mode 100644 src/tests.rs create mode 100644 src/utils.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..afb030d --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,64 @@ +max_width = 100 +hard_tabs = false +tab_spaces = 4 +newline_style = "Auto" +use_small_heuristics = "Default" +indent_style = "Block" +wrap_comments = false +format_code_in_doc_comments = false +comment_width = 80 +normalize_comments = false +normalize_doc_attributes = false +format_strings = false +format_macro_matchers = true +format_macro_bodies = true +empty_item_single_line = true +struct_lit_single_line = true +fn_single_line = false +where_single_line = false +imports_indent = "Block" +imports_layout = "Mixed" +merge_imports = true +reorder_imports = true +reorder_modules = true +reorder_impl_items = false +type_punctuation_density = "Wide" +space_before_colon = false +space_after_colon = true +spaces_around_ranges = false +binop_separator = "Front" +remove_nested_parens = true +combine_control_expr = true +overflow_delimited_expr = false +struct_field_align_threshold = 0 +enum_discrim_align_threshold = 0 +match_arm_blocks = true +force_multiline_blocks = false +fn_args_layout = "Tall" +brace_style = "SameLineWhere" +control_brace_style = "AlwaysSameLine" +trailing_semicolon = true +trailing_comma = "Vertical" +match_block_trailing_comma = false +blank_lines_upper_bound = 1 +blank_lines_lower_bound = 0 +edition = "2018" +version = "One" +inline_attribute_width = 0 +merge_derives = true +use_try_shorthand = false +use_field_init_shorthand = false +force_explicit_abi = true +condense_wildcard_suffixes = false +color = "Auto" +unstable_features = false +disable_all_formatting = false +skip_children = false +hide_parse_errors = false +error_on_line_overflow = false +error_on_unformatted = false +report_todo = "Never" +report_fixme = "Never" +ignore = [] +emit_mode = "Files" +make_backup = false diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3062828 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "jsonwebkey" +version = "0.0.1" +authors = ["Nick Hynes "] +description = "JSON Web Key (JWK) (de)serialization, generation, and conversion." +readme = "README.md" +repository = "https://github.com/nhynes/jwk-rs" +license = "MIT" +edition = "2018" + +[dependencies] +base64 = "0.12" +bitflags = "1.2" +derive_more = "0.99" +paste = "0.1" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +syn = { version = "1.0", features = ["full"] } # required to parse const generics +thiserror = "1.0" +zeroize = { version = "1.1", features = ["zeroize_derive"] } diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7fedcda --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 Nick Hynes + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..05903b5 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# jsonwebkey + +**JSON Web Key (JWK) (de)serialization, generation, and conversion.** + +This library aims to be [spec](https://tools.ietf.org/html/rfc7517#section-4.3) compliant and secure. + +Features: +- [x] Serialization and deserialization of _Required_ and _Recommended_ key types (HS256, RS256, ES256) +- [ ] Conversion to PEM for interop with existing JWT libraries (e.g., [jsonwebtoken](https://crates.io/crates/jsonwebtoken)) +- [ ] Key generation (particularly for testing) diff --git a/src/byte_array.rs b/src/byte_array.rs new file mode 100644 index 0000000..708bda8 --- /dev/null +++ b/src/byte_array.rs @@ -0,0 +1,97 @@ +use std::{array::FixedSizeArray, fmt}; + +use derive_more::{AsRef, Deref, From}; +use serde::{ + de::{self, Deserialize, Deserializer}, + ser::{Serialize, Serializer}, +}; +use zeroize::{Zeroize, Zeroizing}; + +use crate::utils::{deserialize_base64, serialize_base64}; + +#[derive(Zeroize, Deref, AsRef, From)] +#[zeroize(drop)] +pub struct ByteArray(pub [u8; N]); + +impl fmt::Debug for ByteArray { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if cfg!(debug_assertions) { + write!(f, "{}", base64::encode(self.0.as_slice())) + } else { + write!(f, "ByteArray<{}>", N) + } + } +} + +impl PartialEq for ByteArray { + fn eq(&self, other: &Self) -> bool { + self.0.as_slice() == other.0.as_slice() + } +} + +impl Eq for ByteArray {} + +impl ByteArray { + pub fn try_from_slice(bytes: impl AsRef<[u8]>) -> Result { + let mut arr = Self([0u8; N]); + let bytes = bytes.as_ref(); + if bytes.len() != N { + Err(format!("expected {} bytes but got {}", N, bytes.len())) + } else { + arr.0.copy_from_slice(bytes); + Ok(arr) + } + } +} + +impl Serialize for ByteArray { + fn serialize(&self, s: S) -> Result { + serialize_base64(self.0.as_slice(), s) + } +} + +impl<'de, const N: usize> Deserialize<'de> for ByteArray { + fn deserialize>(d: D) -> Result { + let bytes = Zeroizing::new(deserialize_base64(d)?); + Self::try_from_slice(&*bytes).map_err(|_| { + de::Error::invalid_length(bytes.len(), &format!("{} base64-encoded bytes", N).as_str()) + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + static BYTES: &[u8] = &[1, 2, 3, 4, 5, 6, 7]; + static BASE64_JSON: &str = "\"AQIDBAUGBw==\""; + + fn get_de() -> serde_json::Deserializer> { + serde_json::Deserializer::from_str(&BASE64_JSON) + } + + #[test] + fn test_serde_byte_array_good() { + let arr = ByteArray::<7>::try_from_slice(BYTES).unwrap(); + let b64 = serde_json::to_string(&arr).unwrap(); + assert_eq!(b64, BASE64_JSON); + let bytes: ByteArray<7> = serde_json::from_str(&b64).unwrap(); + assert_eq!(bytes.as_ref(), BYTES); + } + + #[test] + fn test_serde_deserialize_byte_array_invalid() { + let mut de = serde_json::Deserializer::from_str("\"Z\""); + ByteArray::<0>::deserialize(&mut de).unwrap_err(); + } + + #[test] + fn test_serde_base64_deserialize_array_long() { + ByteArray::<6>::deserialize(&mut get_de()).unwrap_err(); + } + + #[test] + fn test_serde_base64_deserialize_array_short() { + ByteArray::<8>::deserialize(&mut get_de()).unwrap_err(); + } +} diff --git a/src/byte_vec.rs b/src/byte_vec.rs new file mode 100644 index 0000000..056d13e --- /dev/null +++ b/src/byte_vec.rs @@ -0,0 +1,52 @@ +use std::fmt; + +use derive_more::{AsRef, Deref, From}; +use serde::{ + de::{Deserialize, Deserializer}, + ser::{Serialize, Serializer}, +}; +use zeroize::Zeroize; + +use crate::utils::{deserialize_base64, serialize_base64}; + +#[derive(PartialEq, Eq, Zeroize, Deref, AsRef, From)] +#[zeroize(drop)] +pub struct ByteVec(pub Vec); + +impl fmt::Debug for ByteVec { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if cfg!(debug_assertions) { + write!(f, "{:?}", self.0) + } else { + write!(f, "ByteVec") + } + } +} + +impl Serialize for ByteVec { + fn serialize(&self, s: S) -> Result { + serialize_base64(&self.0, s) + } +} + +impl<'de> Deserialize<'de> for ByteVec { + fn deserialize>(d: D) -> Result { + Ok(Self(deserialize_base64(d)?)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + static BYTES: &[u8] = &[1, 2, 3, 4, 5, 6, 7]; + static BASE64_JSON: &str = "\"AQIDBAUGBw==\""; + + #[test] + fn test_serde_byte_vec() { + let b64 = serde_json::to_string(&ByteVec(BYTES.to_vec())).unwrap(); + assert_eq!(b64, BASE64_JSON); + let bytes: ByteVec = serde_json::from_str(&b64).unwrap(); + assert_eq!(bytes.as_slice(), BYTES); + } +} diff --git a/src/key_ops.rs b/src/key_ops.rs new file mode 100644 index 0000000..5bd331e --- /dev/null +++ b/src/key_ops.rs @@ -0,0 +1,58 @@ +use serde::{ + de::{self, Deserialize, Deserializer}, + ser::{Serialize, SerializeSeq, Serializer}, +}; + +macro_rules! impl_key_ops { + ($(($key_op:ident, $i:literal)),+,) => { + paste::item! { + bitflags::bitflags! { + #[derive(Default)] + pub struct KeyOps: u16 { + $(const [<$key_op:upper>] = $i;)* + } + } + } + + impl Serialize for KeyOps { + fn serialize(&self, s: S) -> Result { + let mut seq = s.serialize_seq(Some(self.bits().count_ones() as usize))?; + $( + if self.contains(paste::expr! { KeyOps::[<$key_op:upper>] }) { + seq.serialize_element(stringify!($key_op))?; + } + )+ + seq.end() + } + } + + impl<'de> Deserialize<'de> for KeyOps { + fn deserialize>(d: D) -> Result { + let op_strs: Vec = Deserialize::deserialize(d)?; + let mut ops = KeyOps::default(); + for op_str in op_strs { + $( + if op_str == stringify!($key_op) { + ops |= paste::expr! { KeyOps::[<$key_op:upper>] }; + continue; + } + )+ + return Err(de::Error::custom(&format!("invalid key op: `{}`", op_str))); + } + Ok(ops) + } + } + }; +} + +#[rustfmt::skip] +impl_key_ops!( + (sign, 0b00000001), + (verify, 0b00000010), + (encrypt, 0b00000100), + (decrypt, 0b00001000), + (wrapKey, 0b00010000), + (unwrapKey, 0b00100000), + (deriveKey, 0b01000000), + (deriveBits, 0b10000000), +); diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0dcaf13 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,193 @@ +#![allow(incomplete_features)] +#![feature(box_syntax, const_generics, fixed_size_array)] + +mod byte_array; +mod byte_vec; +mod key_ops; +#[cfg(test)] +mod tests; +mod utils; + +use serde::{Deserialize, Serialize}; +use zeroize::Zeroize; + +pub use byte_array::ByteArray; +pub use byte_vec::ByteVec; +pub use key_ops::KeyOps; + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct JsonWebKey { + #[serde(flatten)] + pub key_type: Box, + + #[serde(default, rename = "use", skip_serializing_if = "Option::is_none")] + pub key_use: Option, + + #[serde(default, skip_serializing_if = "KeyOps::is_empty")] + pub key_ops: KeyOps, + + #[serde(default, rename = "kid", skip_serializing_if = "Option::is_none")] + pub key_id: Option, + + #[serde(default, rename = "alg", skip_serializing_if = "Option::is_none")] + pub algorithm: Option, +} + +impl JsonWebKey { + pub fn from_slice(bytes: impl AsRef<[u8]>) -> Result { + Ok(serde_json::from_slice(bytes.as_ref())?) + } +} + +impl std::str::FromStr for JsonWebKey { + type Err = Error; + fn from_str(json: &str) -> Result { + let jwk = Self::from_slice(json.as_bytes())?; + + // Validate alg. + use JsonWebAlgorithm::*; + use KeyType::*; + let alg = match &jwk.algorithm { + Some(alg) => alg, + None => return Ok(jwk), + }; + match (alg, &*jwk.key_type) { + ( + ES256, + EC { + params: Curve::P256 { .. }, + }, + ) + | (RS256, RSA { .. }) + | (HS256, Symmetric { .. }) => Ok(jwk), + _ => Err(Error::MismatchedAlgorithm), + } + } +} + +impl std::fmt::Display for JsonWebKey { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if f.alternate() { + write!(f, "{}", serde_json::to_string_pretty(self).unwrap()) + } else { + write!(f, "{}", serde_json::to_string(self).unwrap()) + } + } +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "kty")] +pub enum KeyType { + EC { + #[serde(flatten)] + params: Curve, + }, + RSA { + #[serde(flatten)] + public: RsaPublic, + #[serde(flatten, default, skip_serializing_if = "Option::is_none")] + private: Option, + }, + #[serde(rename = "oct")] + Symmetric { + #[serde(rename = "k")] + key: ByteVec, + }, +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "crv")] +pub enum Curve { + /// prime256v1 + #[serde(rename = "P-256")] + P256 { + /// Private point. + #[serde(skip_serializing_if = "Option::is_none")] + d: Option>, + x: ByteArray<32>, + y: ByteArray<32>, + }, +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct RsaPublic { + /// Public exponent. Must be 65537. + pub e: PublicExponent, + /// Modulus, p*q. + pub n: ByteVec, +} + +const PUBLIC_EXPONENT: u32 = 65537; +const PUBLIC_EXPONENT_B64: &str = "AQAB"; // little-endian, strip zeros +const PUBLIC_EXPONENT_B64_PADDED: &str = "AQABAA=="; +#[derive(Debug, PartialEq, Eq)] +pub struct PublicExponent; + +impl Serialize for PublicExponent { + fn serialize(&self, s: S) -> Result { + PUBLIC_EXPONENT_B64.serialize(s) + } +} + +impl<'de> Deserialize<'de> for PublicExponent { + fn deserialize>(d: D) -> Result { + let e = String::deserialize(d)?; + if e == PUBLIC_EXPONENT_B64 || e == PUBLIC_EXPONENT_B64_PADDED { + Ok(Self) + } else { + Err(serde::de::Error::custom(&format!( + "public exponent must be {}", + PUBLIC_EXPONENT + ))) + } + } +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct RsaPrivate { + /// Private exponent. + pub d: ByteVec, + /// First prime factor. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub p: Option, + /// Second prime factor. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub q: Option, + /// First factor Chinese Remainder Theorem (CRT) exponent. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub dp: Option, + /// Second factor Chinese Remainder Theorem (CRT) exponent. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub dq: Option, + /// First CRT coefficient. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub qi: Option, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum KeyUse { + #[serde(rename = "sig")] + Signing, + #[serde(rename = "enc")] + Encryption, +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Zeroize)] +pub enum JsonWebAlgorithm { + HS256, + RS256, + ES256, +} + +#[derive(thiserror::Error)] +#[cfg_attr(debug_assertions, derive(Debug))] +pub enum Error { + #[error(transparent)] + Serde(#[from] serde_json::Error), + + #[error(transparent)] + Base64Decode(#[from] base64::DecodeError), + + #[error("mismatched algorithm for key type")] + MismatchedAlgorithm, +} diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..590c458 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,263 @@ +use super::*; + +use std::str::FromStr; + +#[test] +fn deserialize_es256() { + // Generated using https://mkjwk.org + let jwk_str = r#"{ + "kty": "EC", + "d": "ZoKQ9j4dhIBlMRVrv-QG8P_T9sutv3_95eio9MtpgKg", + "use": "enc", + "crv": "P-256", + "kid": "a key", + "x": "QOMHmv96tVlJv-uNqprnDSKIj5AiLTXKRomXYnav0N0", + "y": "TjYZoHnctatEE6NCrKmXQdJJPnNzZEX8nBmZde3AY4k", + "alg": "ES256" + }"#; + let jwk = JsonWebKey::from_str(jwk_str).unwrap(); + assert_eq!( + jwk, + JsonWebKey { + key_type: box KeyType::EC { + // The parameters were decoded using a 10-liner Rust script. + params: Curve::P256 { + d: Some( + [ + 102, 130, 144, 246, 62, 29, 132, 128, 101, 49, 21, 107, 191, 228, 6, + 240, 255, 211, 246, 203, 173, 191, 127, 253, 229, 232, 168, 244, 203, + 105, 128, 168 + ] + .into() + ), + x: [ + 64, 227, 7, 154, 255, 122, 181, 89, 73, 191, 235, 141, 170, 154, 231, 13, + 34, 136, 143, 144, 34, 45, 53, 202, 70, 137, 151, 98, 118, 175, 208, 221 + ] + .into(), + y: [ + 78, 54, 25, 160, 121, 220, 181, 171, 68, 19, 163, 66, 172, 169, 151, 65, + 210, 73, 62, 115, 115, 100, 69, 252, 156, 25, 153, 117, 237, 192, 99, 137 + ] + .into(), + }, + }, + algorithm: Some(JsonWebAlgorithm::ES256), + key_id: Some("a key".into()), + key_ops: KeyOps::empty(), + key_use: Some(KeyUse::Encryption), + } + ); +} + +#[test] +fn serialize_es256() { + let jwk = JsonWebKey { + key_type: box KeyType::EC { + params: Curve::P256 { + d: None, + x: [1u8; 32].into(), + y: [2u8; 32].into(), + }, + }, + key_id: None, + algorithm: None, + key_ops: KeyOps::empty(), + key_use: None, + }; + assert_eq!( + jwk.to_string(), + r#"{"kty":"EC","crv":"P-256","x":"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=","y":"AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI="}"# + ); +} + +#[test] +fn deserialize_hs256() { + let jwk_str = r#"{ + "kty": "oct", + "k": "tAON6Q", + "alg": "HS256", + "key_ops": ["verify", "sign"] + }"#; + let jwk = JsonWebKey::from_str(jwk_str).unwrap(); + assert_eq!( + jwk, + JsonWebKey { + key_type: box KeyType::Symmetric { + // The parameters were decoded using a 10-liner Rust script. + key: vec![180, 3, 141, 233].into(), + }, + algorithm: Some(JsonWebAlgorithm::HS256), + key_id: None, + key_ops: KeyOps::SIGN | KeyOps::VERIFY, + key_use: None, + } + ); +} + +#[test] +fn serialize_hs256() { + let jwk = JsonWebKey { + key_type: box KeyType::Symmetric { + key: vec![42; 16].into(), + }, + key_id: None, + algorithm: None, + key_ops: KeyOps::empty(), + key_use: None, + }; + assert_eq!( + jwk.to_string(), + r#"{"kty":"oct","k":"KioqKioqKioqKioqKioqKg=="}"# + ); +} + +#[test] +fn deserialize_rs256() { + let jwk_str = r#"{ + "p": "_LSip5o4eaGf25uvwyUq9ubRtKemrCaoCxumoj63Au0", + "kty": "RSA", + "q": "l20iLpicEW3uja0Zg2xP6DjZa86bD4IQ3wFXCcKCf1c", + "d": "Xo0VAHtfV38HwJbAI6X-Fu7vuyoQjnuiSlQhcSjxn0BZfLP_DKxdJ2ANgTGVE0x243YHqhWRHLobbmDcnUuMOQ", + "e": "AQAB", + "qi": "2mzAaSr7I1D3vDtOhbWKS9-9ELRHKbAHz4dhn4DSCBo", + "dp": "-kyswxeVEpyM6wdU2xRobu-HDMn145PSZFY6AX_e460", + "alg": "RS256", + "dq": "OqMWE3khJlatg8s-D_hHUSOCfg65WN4C7ng0XiEmK20", + "n": "lXpGmBoIxj56TpptApaac6V19_7WWbq0a14a5UHBBlkc54NwIUa2X4p9OeK2sy6rLQ_1g1AcSwfsVUy8MP-Riw" + }"#; + let jwk = JsonWebKey::from_str(jwk_str).unwrap(); + assert_eq!( + jwk, + JsonWebKey { + key_type: box KeyType::RSA { + public: RsaPublic { + e: PublicExponent, + n: vec![ + 149, 122, 70, 152, 26, 8, 198, 62, 122, 78, 154, 109, 2, 150, 154, 115, + 165, 117, 247, 254, 214, 89, 186, 180, 107, 94, 26, 229, 65, 193, 6, 89, + 28, 231, 131, 112, 33, 70, 182, 95, 138, 125, 57, 226, 182, 179, 46, 171, + 45, 15, 245, 131, 80, 28, 75, 7, 236, 85, 76, 188, 48, 255, 145, 139 + ] + .into() + }, + private: Some(RsaPrivate { + d: vec![ + 94, 141, 21, 0, 123, 95, 87, 127, 7, 192, 150, 192, 35, 165, 254, 22, 238, + 239, 187, 42, 16, 142, 123, 162, 74, 84, 33, 113, 40, 241, 159, 64, 89, + 124, 179, 255, 12, 172, 93, 39, 96, 13, 129, 49, 149, 19, 76, 118, 227, + 118, 7, 170, 21, 145, 28, 186, 27, 110, 96, 220, 157, 75, 140, 57 + ] + .into(), + p: Some( + vec![ + 252, 180, 162, 167, 154, 56, 121, 161, 159, 219, 155, 175, 195, 37, 42, + 246, 230, 209, 180, 167, 166, 172, 38, 168, 11, 27, 166, 162, 62, 183, + 2, 237 + ] + .into() + ), + q: Some( + vec![ + 151, 109, 34, 46, 152, 156, 17, 109, 238, 141, 173, 25, 131, 108, 79, + 232, 56, 217, 107, 206, 155, 15, 130, 16, 223, 1, 87, 9, 194, 130, 127, + 87 + ] + .into() + ), + dp: Some( + vec![ + 250, 76, 172, 195, 23, 149, 18, 156, 140, 235, 7, 84, 219, 20, 104, + 110, 239, 135, 12, 201, 245, 227, 147, 210, 100, 86, 58, 1, 127, 222, + 227, 173 + ] + .into() + ), + dq: Some( + vec![ + 58, 163, 22, 19, 121, 33, 38, 86, 173, 131, 203, 62, 15, 248, 71, 81, + 35, 130, 126, 14, 185, 88, 222, 2, 238, 120, 52, 94, 33, 38, 43, 109 + ] + .into() + ), + qi: Some( + vec![ + 218, 108, 192, 105, 42, 251, 35, 80, 247, 188, 59, 78, 133, 181, 138, + 75, 223, 189, 16, 180, 71, 41, 176, 7, 207, 135, 97, 159, 128, 210, 8, + 26 + ] + .into() + ) + }) + }, + algorithm: Some(JsonWebAlgorithm::RS256), + key_id: None, + key_ops: KeyOps::empty(), + key_use: None, + } + ); +} + +#[test] +fn serialize_rs256() { + let jwk = JsonWebKey { + key_type: box KeyType::RSA { + public: RsaPublic { + e: PublicExponent, + n: vec![105, 183, 62].into(), + }, + private: Some(RsaPrivate { + d: vec![105, 183, 63].into(), + p: None, + q: None, + dp: None, + dq: None, + qi: None, + }), + }, + key_id: None, + algorithm: None, + key_ops: KeyOps::empty(), + key_use: None, + }; + assert_eq!( + jwk.to_string(), + r#"{"kty":"RSA","e":"AQAB","n":"abc-","d":"abc_"}"# + ); +} + +#[test] +fn mismatched_algorithm() { + macro_rules! assert_mismatched_alg { + ($jwk_str:literal) => { + match JsonWebKey::from_str($jwk_str) { + Err(Error::MismatchedAlgorithm) => {} + v => panic!("expected MismatchedAlgorithm, got {:?}", v), + } + }; + } + + assert_mismatched_alg!(r#"{ "kty": "oct", "k": "tAON6Q", "alg": "ES256" }"#); + assert_mismatched_alg!(r#"{ "kty": "oct", "k": "tAON6Q", "alg": "RS256" }"#); + + assert_mismatched_alg!( + r#"{ + "kty": "EC", + "d": "ZoKQ9j4dhIBlMRVrv-QG8P_T9sutv3_95eio9MtpgKg", + "crv": "P-256", + "x": "QOMHmv96tVlJv-uNqprnDSKIj5AiLTXKRomXYnav0N0", + "y": "TjYZoHnctatEE6NCrKmXQdJJPnNzZEX8nBmZde3AY4k", + "alg": "RS256" + }"# + ); + assert_mismatched_alg!( + r#"{ + "kty": "EC", + "d": "ZoKQ9j4dhIBlMRVrv-QG8P_T9sutv3_95eio9MtpgKg", + "crv": "P-256", + "x": "QOMHmv96tVlJv-uNqprnDSKIj5AiLTXKRomXYnav0N0", + "y": "TjYZoHnctatEE6NCrKmXQdJJPnNzZEX8nBmZde3AY4k", + "alg": "HS256" + }"# + ); +} diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..aaa5797 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,32 @@ +use serde::{ + de::{self, Deserialize, Deserializer}, + ser::{Serialize, Serializer}, +}; +use zeroize::Zeroizing; + +fn base64_config() -> base64::Config { + base64::Config::new(base64::CharacterSet::UrlSafe, true /* pad */) +} + +fn base64_encode(bytes: impl AsRef<[u8]>) -> String { + base64::encode_config(bytes, base64_config()) +} + +fn base64_decode(b64: impl AsRef<[u8]>) -> Result, base64::DecodeError> { + base64::decode_config(b64, base64_config()) +} + +pub fn serialize_base64(bytes: impl AsRef<[u8]>, s: S) -> Result { + base64_encode(bytes).serialize(s) +} + +pub fn deserialize_base64<'de, D: Deserializer<'de>>(d: D) -> Result, D::Error> { + let base64_str = Zeroizing::new(String::deserialize(d)?); + base64_decode(&*base64_str).map_err(|e| { + #[cfg(debug_assertions)] + let err_msg = e.to_string().to_lowercase(); + #[cfg(not(debug_assertions))] + let err_msg = "invalid base64"; + de::Error::custom(err_msg.strip_suffix(".").unwrap_or(&err_msg)) + }) +}