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
//! JWE (JSON Web Encryption) key management algorithms.
//!
//! This module provides implementations of various JWE key management algorithms
//! as specified in RFC 7518. Each key type is strongly typed to prevent misuse.
//!
//! # Supported Algorithms
//!
//! ## RSA Key Management
//! - `RSA-OAEP` - RSA with OAEP using SHA-1
//!
//! Note: RSA-OAEP-256 (with SHA-256) is not currently supported because the underlying
//! boring/superboring crates do not expose the API to specify the OAEP hash function.
//!
//! ## Symmetric Key Wrap
//! - `A256KW` - AES-256 Key Wrap (recommended)
//! - `A128KW` - AES-128 Key Wrap
//!
//! ## ECDH Key Agreement
//! - `ECDH-ES+A256KW` - ECDH with AES-256 Key Wrap (recommended)
//! - `ECDH-ES+A128KW` - ECDH with AES-128 Key Wrap
//!
//! Both are available with P-256 keys (`EcdhEs*`) and X25519 keys (`X25519EcdhEs*`).
//! The `apu` and `apv` parameters of a received token are used in the key derivation.
//!
//! # Content Encryption
//!
//! All key management algorithms support these content encryption algorithms:
//! - `A256GCM` - AES-256-GCM (default, recommended)
//! - `A128GCM` - AES-128-GCM
//!
//! # Sender Authentication
//!
//! Authenticated encryption authenticates the ciphertext, not the sender.
//! With the asymmetric modes (`RSA-OAEP` and `ECDH-ES`), the encryption key is public,
//! so anyone can produce a token that decrypts successfully, carrying any claims.
//! Successful decryption in these modes proves nothing about who sent the token,
//! and checking issuer or audience claims is no substitute, since the sender picked them too.
//!
//! The AES key wrap modes (`A128KW`, `A256KW`) are different: producing a decryptable
//! token requires the shared secret, so decryption authenticates the sender as one of
//! the key holders.
//!
//! When the sender's identity matters with the asymmetric modes, verify a signature
//! after decryption, for example a signed token carried in a custom claim.
//!
//! # Examples
//!
//! ## RSA-OAEP
//!
//! ```rust
//! use jwt_simple::prelude::*;
//!
//! // Generate a key pair
//! let decryption_key = RsaOaepDecryptionKey::generate(2048).unwrap();
//! let encryption_key = decryption_key.encryption_key();
//!
//! // Encrypt
//! let claims = Claims::create(Duration::from_hours(1))
//! .with_subject("user@example.com");
//! let token = encryption_key.encrypt(claims).unwrap();
//!
//! // Decrypt
//! let claims = decryption_key.decrypt_token::<NoCustomClaims>(&token, None).unwrap();
//! ```
//!
//! ## AES Key Wrap
//!
//! ```rust
//! use jwt_simple::prelude::*;
//!
//! // Generate a symmetric key
//! let key = A256KWKey::generate();
//!
//! // Encrypt
//! let claims = Claims::create(Duration::from_hours(1));
//! let token = key.encrypt(claims).unwrap();
//!
//! // Decrypt
//! let claims = key.decrypt_token::<NoCustomClaims>(&token, None).unwrap();
//! ```
//!
//! ## ECDH-ES+A256KW
//!
//! ```rust
//! use jwt_simple::prelude::*;
//!
//! // Generate a key pair
//! let decryption_key = EcdhEsA256KWDecryptionKey::generate();
//! let encryption_key = decryption_key.encryption_key();
//!
//! // Encrypt
//! let claims = Claims::create(Duration::from_hours(1));
//! let token = encryption_key.encrypt(claims).unwrap();
//!
//! // Decrypt
//! let claims = decryption_key.decrypt_token::<NoCustomClaims>(&token, None).unwrap();
//! ```
//!
//! ## ECDH-ES+A256KW with X25519, and JWK
//!
//! Asymmetric keys can be shared as JSON Web Keys.
//!
//! ```rust
//! use jwt_simple::prelude::*;
//!
//! // The recipient publishes its public key.
//! let decryption_key = X25519EcdhEsA256KWDecryptionKey::generate().with_key_id("recipient");
//! let jwk = decryption_key.encryption_key().to_jwk();
//!
//! // The sender encrypts with the public key.
//! let encryption_key = X25519EcdhEsA256KWEncryptionKey::from_jwk(&jwk).unwrap();
//! let claims = Claims::create(Duration::from_hours(1));
//! let token = encryption_key.encrypt(claims).unwrap();
//!
//! let claims = decryption_key.decrypt_token::<NoCustomClaims>(&token, None).unwrap();
//! ```
//!
//! The P-256 and RSA-OAEP keys work the same way:
//!
//! ```rust
//! use jwt_simple::prelude::*;
//!
//! let decryption_key = EcdhEsA128KWDecryptionKey::generate();
//! let jwk = decryption_key.encryption_key().to_jwk();
//! let encryption_key = EcdhEsA128KWEncryptionKey::from_jwk(&jwk).unwrap();
//! let token = encryption_key
//! .encrypt(Claims::create(Duration::from_hours(1)))
//! .unwrap();
//! decryption_key.decrypt_token::<NoCustomClaims>(&token, None).unwrap();
//!
//! let decryption_key = RsaOaepDecryptionKey::generate(2048).unwrap();
//! let private_jwk = decryption_key.to_jwk();
//! let decryption_key = RsaOaepDecryptionKey::from_jwk(&private_jwk).unwrap();
//! let public_jwk = decryption_key.encryption_key().to_jwk();
//! let encryption_key = RsaOaepEncryptionKey::from_jwk(&public_jwk).unwrap();
//! assert_eq!(encryption_key.jwk_thumbprint(), decryption_key.jwk_thumbprint());
//! ```
pub use ;
pub use ContentEncryption;
pub use ;
pub use ;
pub use ;