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
//! A password, owned, zeroized, and distinguishable from a key at compile time.
//!
//! Two features in this crate take a password rather than a key, for the same reason:
//! PBES2 JWE key management (RFC 7518 §4.8, [`crate::jwe`]) and password-encrypted
//! PKCS#8 key files (RFC 5958, [`crate::jwk`]). Both derive a key from it, and neither
//! can accept a key in its place.
//!
//! A password outlives its derived key by design: the caller keeps typing it, and the
//! library derives, uses and forgets. [`Password`] owns its bytes in [`Zeroizing`], and
//! so does every derived value built from it. The guarantee is deliberately modest — a
//! password in a terminal or a config file is already not secret at rest — but the
//! derived value is the thing an attacker actually wants, and nothing here leaves it in
//! a plain `Vec`.
//!
//! The iteration-count constants are *not* here: they belong to the scheme that uses
//! them ([`PBES2_MIN_ITERATIONS`](crate::jwe::password::PBES2_MIN_ITERATIONS) and
//! friends for PBES2, [`PKCS8_MIN_ITERATIONS`](crate::jwk::PKCS8_MIN_ITERATIONS) for
//! PKCS#8), and they differ because the two schemes have different ecosystems and
//! different senders choosing the work.
use Zeroizing;
/// A password, owned, zeroized, and distinguishable from a key at compile time.
///
/// RFC 7518 §4.8 defines the PBES2 password input as an octet sequence, and requires
/// that a password "represented as a text string" be UTF-8 encoded to get those
/// octets. Both forms are real — a human types text, an export file holds bytes — and
/// they are given separate constructors so the encoding decision is visible at the
/// call site rather than implicit in whichever `Into` happened to apply.
///
/// Two properties are worth knowing before using it:
///
/// * **Whitespace and non-ASCII are preserved exactly.** Normalizing, trimming or
/// case-folding a password is the caller's business and usually a bug: RFC 7520's
/// own PBES2 example password contains U+2013 EN DASH twice, and the recipient must
/// derive over the same octets the sender did.
/// * **It is not compared.** Nothing here offers equality; the password is fed to
/// PBKDF2 and the AES-KW integrity check on the wrapped key decides whether it was
/// right. A `PartialEq` on a secret invites the constant-time mistake.
///
/// # Examples
///
/// ```
/// use jose_rs::jwe::Password;
///
/// // Text, encoded as RFC 7518 §4.8 requires.
/// let password = Password::from_utf8("entrap\u{2013}peter_long\u{2013}credit_tun");
/// // Raw octets, exactly as supplied.
/// let from_bytes = Password::from_bytes(b"\x00\x01\x02password".as_slice());
/// // The characters are preserved, and the non-ASCII ones travel as their UTF-8
/// // octets — which is what a recipient has to reproduce byte for byte.
/// assert!(password.as_ref().windows(3).any(|w| w == [0xe2, 0x80, 0x93]));
/// assert_eq!(from_bytes.as_ref(), b"\x00\x01\x02password");
/// ```
/// `Debug` says how long the secret is and nothing else.
///
/// Length is kept because it is the diagnostic that matters ("the config file read an
/// empty password"), and it is not itself sensitive.