1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! # jsonwebkey-convert
//! Handle Json Web Key without nightly rust compiler.
//!
//! ## Load JSON Web Key Set
//!
//! ```
//! use jsonwebkey_convert::JsonWebKeySet;
//! # use jsonwebkey_convert::Error;
//!
//! # fn main() -> Result<(), Error> {
//! # let jwks_str = include_str!("../testfiles/example-public-key.json");
//! let jwks: JsonWebKeySet = jwks_str.parse()?;
//! # Ok(())
//! # }
//! ```
//!
//!
//! ## Convert PEM to JWK
//! `pem_support` feature is required.
//!
//! ```
//! use jsonwebkey_convert::*;
//! use jsonwebkey_convert::der::FromPem;
//!
//! # fn main() -> Result<(), Error> {
//! # let pem_data = include_str!("../testfiles/test1.pem");
//! let rsa_jwk = RSAPublicKey::from_pem(pem_data)?;
//! let jwk_byte_vec = serde_json::to_string(&rsa_jwk);
//! # Ok(())
//! # }
//! ```
//!
//! ## Convert JWK to PEM
//! `pem_support` feature is required.
//!
//! ```
//! use jsonwebkey_convert::*;
//! use jsonwebkey_convert::der::ToPem;
//!
//! # fn main() -> Result<(), Error> {
//! # let jwk_data = include_str!("../testfiles/test1.json");
//! let rsa_jwk: RSAPublicKey = jwk_data.parse()?;
//! let pem_data = rsa_jwk.to_pem()?;
//! # Ok(())
//! # }
//! ```

/// DER and PEM format support
#[cfg(feature = "simple_asn1")]
pub mod der;

/// Json Web Token support
#[cfg(feature = "jsonwebtoken")]
pub mod jwt;

use num_bigint::BigUint;
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use std::str::FromStr;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
    #[error("Public Key Parse Error: {0}")]
    PubKeyParse(&'static str),
    #[cfg(feature = "simple_asn1")]
    #[error(transparent)]
    ANS1DecodeError(#[from] simple_asn1::ASN1DecodeErr),
    #[cfg(feature = "simple_asn1")]
    #[error(transparent)]
    ANS1EncodeError(#[from] simple_asn1::ASN1EncodeErr),
    #[error(transparent)]
    #[cfg(feature = "pem")]
    PEMParseError(#[from] pem::PemError),
    #[error(transparent)]
    Base64UrlError(#[from] base64::DecodeError),
    #[error(transparent)]
    JSONParseError(#[from] serde_json::Error),
}

/// A set of Json Web Keys
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JsonWebKeySet {
    pub keys: Vec<JsonWebKey>,
}

impl JsonWebKeySet {
    pub fn from_bytes(data: &[u8]) -> Result<JsonWebKeySet, Error> {
        Ok(serde_json::from_slice(data)?)
    }
}

impl FromStr for JsonWebKeySet {
    type Err = Error;
    fn from_str(data: &str) -> Result<JsonWebKeySet, Error> {
        Ok(serde_json::from_str(data)?)
    }
}

/// A JSON Web Key
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JsonWebKey {
    ECPrivateKey {
        #[serde(flatten)]
        value: ECPrivateKey,
    },
    ECPublicKey {
        #[serde(flatten)]
        value: ECPublicKey,
    },
    RSAPrivateKey {
        #[serde(flatten)]
        value: Box<RSAPrivateKey>,
    },
    RSAPublicKey {
        #[serde(flatten)]
        value: RSAPublicKey,
    },
    SymmetricKey {
        #[serde(flatten)]
        value: SymmetricKey,
    },
}
impl FromStr for JsonWebKey {
    type Err = Error;
    fn from_str(data: &str) -> Result<JsonWebKey, Error> {
        Ok(serde_json::from_str(data)?)
    }
}

impl JsonWebKey {
    pub fn from_bytes(data: &[u8]) -> Result<JsonWebKey, Error> {
        Ok(serde_json::from_slice(data)?)
    }

    pub fn ec_private_key(&self) -> Option<&ECPrivateKey> {
        match self {
            JsonWebKey::ECPrivateKey { value } => Some(value),
            _ => None,
        }
    }

    pub fn ec_public_key(&self) -> Option<&ECPublicKey> {
        match self {
            JsonWebKey::ECPublicKey { value } => Some(value),
            _ => None,
        }
    }

    pub fn rsa_private_key(&self) -> Option<&RSAPrivateKey> {
        match self {
            JsonWebKey::RSAPrivateKey { value } => Some(value),
            _ => None,
        }
    }

    pub fn rsa_public_key(&self) -> Option<&RSAPublicKey> {
        match self {
            JsonWebKey::RSAPublicKey { value } => Some(value),
            _ => None,
        }
    }

    pub fn symmetric_key(&self) -> Option<&SymmetricKey> {
        match self {
            JsonWebKey::SymmetricKey { value } => Some(value),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Copy, Serialize, Deserialize)]
pub enum ECCurveParameter {
    #[serde(rename = "P-256")]
    P256,
    #[serde(rename = "P-384")]
    P384,
    #[serde(rename = "P-521")]
    P521,
}

/// BASE64 encoded big integer
#[derive(Debug, Clone, PartialEq)]
pub struct Base64BigUint {
    big_uint: BigUint,
    base64: String,
}

impl Base64BigUint {
    pub fn to_base64url(&self) -> String {
        base64::encode_config(&self.big_uint.to_bytes_be(), base64::URL_SAFE_NO_PAD)
    }

    pub fn from_base64url(value: &str) -> Result<Self, Error> {
        Ok(Base64BigUint {
            big_uint: BigUint::from_bytes_be(&base64::decode_config(
                value,
                base64::URL_SAFE_NO_PAD,
            )?),
            base64: value.to_string(),
        })
    }
}

impl Into<BigUint> for Base64BigUint {
    fn into(self) -> BigUint {
        self.big_uint
    }
}

impl Into<Base64BigUint> for BigUint {
    fn into(self) -> Base64BigUint {
        Base64BigUint {
            base64: base64::encode_config(self.to_bytes_be(), base64::URL_SAFE_NO_PAD),
            big_uint: self,
        }
    }
}

impl Serialize for Base64BigUint {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.to_base64url().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Base64BigUint {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Ok(Base64BigUint::from_base64url(&value).map_err(|e| {
            D::Error::custom(format!("failed to decode BASE64 URL: {} : {}", value, e))
        })?)
    }
}

// RFC 7518
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
//#[serde(deny_unknown_fields)]
pub struct ECPublicKey {
    #[serde(flatten)]
    pub generic: Generic,
    pub crv: ECCurveParameter,
    pub x: Base64BigUint,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub y: Option<Base64BigUint>,
}

impl FromStr for ECPublicKey {
    type Err = Error;
    fn from_str(data: &str) -> Result<Self, Error> {
        Ok(serde_json::from_str(data)?)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
//#[serde(deny_unknown_fields)]
pub struct ECPrivateKey {
    #[serde(flatten)]
    pub public_key: ECPublicKey,
    pub d: Base64BigUint,
}

impl FromStr for ECPrivateKey {
    type Err = Error;
    fn from_str(data: &str) -> Result<Self, Error> {
        Ok(serde_json::from_str(data)?)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
//#[serde(deny_unknown_fields)]
pub struct RSAPublicKey {
    #[serde(flatten)]
    pub generic: Generic,
    pub n: Base64BigUint,
    pub e: Base64BigUint,
}

impl FromStr for RSAPublicKey {
    type Err = Error;
    fn from_str(data: &str) -> Result<Self, Error> {
        Ok(serde_json::from_str(data)?)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
//#[serde(deny_unknown_fields)]
pub struct RSAPrivateKey {
    #[serde(flatten)]
    pub public_key: RSAPublicKey,
    pub d: Base64BigUint,
    #[serde(flatten)]
    pub optimizations: Option<RSAPrivateKeyOptimizations>,
    pub oth: Option<Vec<RSAPrivateKeyOtherPrimesInfo>>,
}

impl FromStr for RSAPrivateKey {
    type Err = Error;
    fn from_str(data: &str) -> Result<Self, Error> {
        Ok(serde_json::from_str(data)?)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
//#[serde(deny_unknown_fields)]
pub struct RSAPrivateKeyOptimizations {
    pub p: Base64BigUint,
    pub q: Base64BigUint,
    pub dp: Base64BigUint,
    pub dq: Base64BigUint,
    pub qi: Base64BigUint,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
//#[serde(deny_unknown_fields)]
pub struct RSAPrivateKeyOtherPrimesInfo {
    pub r: Base64BigUint,
    pub d: Base64BigUint,
    pub t: Base64BigUint,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SymmetricKey {
    #[serde(flatten)]
    pub generic: Generic,
    pub k: String,
}

impl FromStr for SymmetricKey {
    type Err = Error;
    fn from_str(data: &str) -> Result<Self, Error> {
        Ok(serde_json::from_str(data)?)
    }
}

/// A type of JWK.
/// See RFC 7518 Section 6.1.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum KeyType {
    #[serde(rename = "EC")]
    EllipticCurve,
    #[serde(rename = "RSA")]
    Rsa,
    #[serde(rename = "oct")]
    OctetSequence,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum KeyUse {
    #[serde(rename = "sig")]
    Signature,
    #[serde(rename = "enc")]
    Encryption,
}

/// Generic parameters for JSON Web Key.
/// See RFC 7517, Section 4.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Generic {
    // Generic parameters
    pub kty: KeyType,
    #[serde(skip_serializing_if = "Option::is_none", rename = "use")]
    pub use_: Option<KeyUse>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_ops: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alg: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kid: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub x5u: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub x5c: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub x5t: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "x5t#S256")]
    pub x5t_s256: Option<String>,
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn load_rsa_pubkey1() -> Result<(), serde_json::Error> {
        let pubkey_json: JsonWebKey =
            serde_json::from_str(include_str!("../testfiles/test1.json"))?;
        let pubkey_data = pubkey_json.rsa_public_key().unwrap();
        assert_eq!(pubkey_data.generic.kty, KeyType::Rsa);
        assert_eq!(pubkey_data.e.big_uint, BigUint::from(65537u64));
        Ok(())
    }

    #[test]
    fn load_rsa_pubkey2() -> Result<(), serde_json::Error> {
        let pubkey_json: JsonWebKey =
            serde_json::from_str(include_str!("../testfiles/test2.json"))?;
        let pubkey_data = pubkey_json.rsa_public_key().unwrap();
        assert_eq!(pubkey_data.generic.kty, KeyType::Rsa);
        assert_eq!(pubkey_data.generic.alg.as_ref().unwrap(), "RS256");
        assert_eq!(pubkey_data.generic.use_.unwrap(), KeyUse::Signature);
        assert_eq!(
            pubkey_data.generic.kid.as_ref().unwrap(),
            "ctFNPw6mrKynlD3atDovZGBlbWRXj7IK0IBODJ_hqeI"
        );
        assert_eq!(
            pubkey_data.generic.x5t.as_ref().unwrap(),
            "ZsHe1ebgPQqmqNF8rjKqWEjh4hk"
        );
        assert_eq!(
            pubkey_data.generic.x5t_s256.as_ref().unwrap(),
            "VaYCCwkyvl8K71fldYXJtNjHAPTGom2ylqdAbedtKUI"
        );
        assert_eq!(pubkey_data.e.big_uint, BigUint::from(65537u64));
        Ok(())
    }

    #[test]
    fn load_private_keys() -> Result<(), serde_json::Error> {
        let privkey_json: JsonWebKeySet =
            serde_json::from_str(include_str!("../testfiles/example-private-key.json"))?;
        assert_eq!(privkey_json.keys.len(), 2);

        let ec_private_key = privkey_json.keys[0].ec_private_key().unwrap();
        assert_eq!(
            ec_private_key.public_key.generic.kty,
            KeyType::EllipticCurve
        );
        assert_eq!(ec_private_key.public_key.crv, ECCurveParameter::P256);
        assert_eq!(
            ec_private_key.public_key.generic.use_.unwrap(),
            KeyUse::Encryption
        );
        assert_eq!(ec_private_key.public_key.generic.kid.as_ref().unwrap(), "1");

        let rsa_private_key = privkey_json.keys[1].rsa_private_key().unwrap();
        assert_eq!(rsa_private_key.public_key.generic.kty, KeyType::Rsa);
        assert!(rsa_private_key.optimizations.is_some());

        Ok(())
    }

    #[test]
    fn load_public_keys() -> Result<(), serde_json::Error> {
        let pubkey_json: JsonWebKeySet =
            serde_json::from_str(include_str!("../testfiles/example-public-key.json"))?;

        let ec_public_key = pubkey_json.keys[0].ec_public_key().unwrap();
        assert_eq!(ec_public_key.generic.kty, KeyType::EllipticCurve);
        assert_eq!(ec_public_key.crv, ECCurveParameter::P256);
        assert_eq!(ec_public_key.generic.use_.unwrap(), KeyUse::Encryption);
        assert_eq!(ec_public_key.generic.kid.as_ref().unwrap(), "1");
        assert_eq!(
            ec_public_key.x.to_base64url(),
            "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4"
        );
        assert_eq!(
            ec_public_key.y.as_ref().unwrap().to_base64url(),
            "4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM"
        );

        let rsa_public_key = pubkey_json.keys[1].rsa_public_key().unwrap();
        assert_eq!(rsa_public_key.generic.kty, KeyType::Rsa);
        assert_eq!(rsa_public_key.e.to_base64url(), "AQAB");
        assert_eq!(rsa_public_key.n.to_base64url(), "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw");
        assert_eq!(rsa_public_key.generic.alg.as_ref().unwrap(), "RS256");
        Ok(())
    }

    #[test]
    fn load_symmetric_keys() -> Result<(), serde_json::Error> {
        let symmetric_json: JsonWebKeySet =
            serde_json::from_str(include_str!("../testfiles/example-symmetric-keys.json"))?;

        let symmetric_key = symmetric_json.keys[0].symmetric_key().unwrap();
        assert_eq!(symmetric_key.generic.kty, KeyType::OctetSequence);
        assert_eq!(symmetric_key.generic.alg.as_ref().unwrap(), "A128KW");
        assert_eq!(symmetric_key.k, "GawgguFyGrWKav7AX4VKUg");

        let symmetric_key = symmetric_json.keys[1].symmetric_key().unwrap();
        assert_eq!(symmetric_key.generic.kty, KeyType::OctetSequence);
        assert_eq!(symmetric_key.k, "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow");
        assert_eq!(
            symmetric_key.generic.kid.as_ref().unwrap(),
            "HMAC key used in JWS spec Appendix A.1 example"
        );
        Ok(())
    }
}