@@ -64,7 +64,7 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
64
64
#[ allow( deprecated) ]
65
65
let key_ref = ctx
66
66
. dangerous_get_symmetric_key ( key_to_seal)
67
- . map_err ( |_| PasswordProtectedKeyEnvelopeError :: KeyMissingError ) ?;
67
+ . map_err ( |_| PasswordProtectedKeyEnvelopeError :: KeyMissing ) ?;
68
68
Self :: seal_ref ( key_ref, password)
69
69
}
70
70
@@ -97,7 +97,7 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
97
97
// The envelope key is directly derived from the KDF and used as the key to encrypt the key
98
98
// that should be sealed.
99
99
let envelope_key = derive_key ( kdf_settings, password)
100
- . map_err ( |_| PasswordProtectedKeyEnvelopeError :: KdfError ) ?;
100
+ . map_err ( |_| PasswordProtectedKeyEnvelopeError :: Kdf ) ?;
101
101
102
102
let ( content_format, key_to_seal_bytes) = match key_to_seal. to_encoded_raw ( ) {
103
103
EncodedSymmetricKey :: BitwardenLegacyKey ( key_bytes) => {
@@ -145,7 +145,7 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
145
145
let key = self . unseal_ref ( password) ?;
146
146
#[ allow( deprecated) ]
147
147
ctx. set_symmetric_key ( target_keyslot, key)
148
- . map_err ( |_| PasswordProtectedKeyEnvelopeError :: KeyStoreError ) ?;
148
+ . map_err ( |_| PasswordProtectedKeyEnvelopeError :: KeyStore ) ?;
149
149
Ok ( target_keyslot)
150
150
}
151
151
@@ -161,34 +161,32 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
161
161
. first ( )
162
162
. filter ( |_| self . cose_encrypt . recipients . len ( ) == 1 )
163
163
. ok_or_else ( || {
164
- PasswordProtectedKeyEnvelopeError :: ParsingError (
164
+ PasswordProtectedKeyEnvelopeError :: Parsing (
165
165
"Invalid number of recipients" . to_string ( ) ,
166
166
)
167
167
} ) ?;
168
168
169
169
if recipient. protected . header . alg != Some ( coset:: Algorithm :: PrivateUse ( ALG_ARGON2ID13 ) ) {
170
- return Err ( PasswordProtectedKeyEnvelopeError :: ParsingError (
170
+ return Err ( PasswordProtectedKeyEnvelopeError :: Parsing (
171
171
"Unknown or unsupported KDF algorithm" . to_string ( ) ,
172
172
) ) ;
173
173
}
174
174
175
175
let kdf_settings: Argon2RawSettings =
176
176
( & recipient. unprotected ) . try_into ( ) . map_err ( |_| {
177
- PasswordProtectedKeyEnvelopeError :: ParsingError (
177
+ PasswordProtectedKeyEnvelopeError :: Parsing (
178
178
"Invalid or missing KDF parameters" . to_string ( ) ,
179
179
)
180
180
} ) ?;
181
181
let envelope_key = derive_key ( & kdf_settings, password)
182
- . map_err ( |_| PasswordProtectedKeyEnvelopeError :: KdfError ) ?;
182
+ . map_err ( |_| PasswordProtectedKeyEnvelopeError :: Kdf ) ?;
183
183
let nonce: [ u8 ; crate :: xchacha20:: NONCE_SIZE ] = self
184
184
. cose_encrypt
185
185
. unprotected
186
186
. iv
187
187
. clone ( )
188
188
. try_into ( )
189
- . map_err ( |_| {
190
- PasswordProtectedKeyEnvelopeError :: ParsingError ( "Invalid IV" . to_string ( ) )
191
- } ) ?;
189
+ . map_err ( |_| PasswordProtectedKeyEnvelopeError :: Parsing ( "Invalid IV" . to_string ( ) ) ) ?;
192
190
193
191
let key_bytes = self
194
192
. cose_encrypt
@@ -201,9 +199,7 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
201
199
202
200
SymmetricCryptoKey :: try_from (
203
201
match ContentFormat :: try_from ( & self . cose_encrypt . protected . header ) . map_err ( |_| {
204
- PasswordProtectedKeyEnvelopeError :: ParsingError (
205
- "Invalid content format" . to_string ( ) ,
206
- )
202
+ PasswordProtectedKeyEnvelopeError :: Parsing ( "Invalid content format" . to_string ( ) )
207
203
} ) ? {
208
204
ContentFormat :: BitwardenLegacyKey => EncodedSymmetricKey :: BitwardenLegacyKey (
209
205
BitwardenLegacyKeyBytes :: from ( key_bytes) ,
@@ -212,15 +208,13 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
212
208
EncodedSymmetricKey :: CoseKey ( CoseKeyBytes :: from ( key_bytes) )
213
209
}
214
210
_ => {
215
- return Err ( PasswordProtectedKeyEnvelopeError :: ParsingError (
211
+ return Err ( PasswordProtectedKeyEnvelopeError :: Parsing (
216
212
"Unknown or unsupported content format" . to_string ( ) ,
217
213
) ) ;
218
214
}
219
215
} ,
220
216
)
221
- . map_err ( |_| {
222
- PasswordProtectedKeyEnvelopeError :: ParsingError ( "Failed to decode key" . to_string ( ) )
223
- } )
217
+ . map_err ( |_| PasswordProtectedKeyEnvelopeError :: Parsing ( "Failed to decode key" . to_string ( ) ) )
224
218
}
225
219
226
220
/// Re-seals the key with new KDF parameters (updated settings, salt), and a new password
@@ -268,12 +262,12 @@ impl<Ids: KeyIds> FromStr for PasswordProtectedKeyEnvelope<Ids> {
268
262
269
263
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
270
264
let data = B64 :: try_from ( s) . map_err ( |_| {
271
- PasswordProtectedKeyEnvelopeError :: ParsingError (
265
+ PasswordProtectedKeyEnvelopeError :: Parsing (
272
266
"Invalid PasswordProtectedKeyEnvelope Base64 encoding" . to_string ( ) ,
273
267
)
274
268
} ) ?;
275
269
Self :: try_from ( & data. into_bytes ( ) ) . map_err ( |_| {
276
- PasswordProtectedKeyEnvelopeError :: ParsingError (
270
+ PasswordProtectedKeyEnvelopeError :: Parsing (
277
271
"Failed to parse PasswordProtectedKeyEnvelope" . to_string ( ) ,
278
272
)
279
273
} )
@@ -373,7 +367,7 @@ impl TryInto<Params> for &Argon2RawSettings {
373
367
self . parallelism ,
374
368
Some ( ENVELOPE_ARGON2_OUTPUT_KEY_SIZE ) ,
375
369
)
376
- . map_err ( |_| PasswordProtectedKeyEnvelopeError :: KdfError )
370
+ . map_err ( |_| PasswordProtectedKeyEnvelopeError :: Kdf )
377
371
}
378
372
}
379
373
@@ -388,9 +382,7 @@ impl TryInto<Argon2RawSettings> for &Header {
388
382
salt : extract_bytes ( self , ARGON2_SALT , "salt" ) ?
389
383
. try_into ( )
390
384
. map_err ( |_| {
391
- PasswordProtectedKeyEnvelopeError :: ParsingError (
392
- "Invalid Argon2 salt" . to_string ( ) ,
393
- )
385
+ PasswordProtectedKeyEnvelopeError :: Parsing ( "Invalid Argon2 salt" . to_string ( ) )
394
386
} ) ?,
395
387
} )
396
388
}
@@ -415,7 +407,7 @@ fn derive_key(
415
407
argon2_settings. try_into ( ) ?,
416
408
)
417
409
. hash_password_into ( password. as_bytes ( ) , & argon2_settings. salt , & mut hash)
418
- . map_err ( |_| PasswordProtectedKeyEnvelopeError :: KdfError ) ?;
410
+ . map_err ( |_| PasswordProtectedKeyEnvelopeError :: Kdf ) ?;
419
411
420
412
Ok ( hash)
421
413
}
@@ -428,29 +420,29 @@ pub enum PasswordProtectedKeyEnvelopeError {
428
420
WrongPassword ,
429
421
/// The envelope could not be parsed correctly, or the KDF parameters are invalid
430
422
#[ error( "Parsing error {0}" ) ]
431
- ParsingError ( String ) ,
423
+ Parsing ( String ) ,
432
424
/// The KDF failed to derive a key, possibly due to invalid parameters or memory allocation
433
425
/// issues
434
426
#[ error( "Kdf error" ) ]
435
- KdfError ,
427
+ Kdf ,
436
428
/// There is no key for the provided key id in the key store
437
429
#[ error( "Key missing error" ) ]
438
- KeyMissingError ,
430
+ KeyMissing ,
439
431
/// The key store could not be written to, for example due to being read-only
440
432
#[ error( "Could not write to key store" ) ]
441
- KeyStoreError ,
433
+ KeyStore ,
442
434
}
443
435
444
436
impl From < CoseExtractError > for PasswordProtectedKeyEnvelopeError {
445
437
fn from ( err : CoseExtractError ) -> Self {
446
438
let CoseExtractError :: MissingValue ( label) = err;
447
- PasswordProtectedKeyEnvelopeError :: ParsingError ( format ! ( "Missing value for {}" , label) )
439
+ PasswordProtectedKeyEnvelopeError :: Parsing ( format ! ( "Missing value for {}" , label) )
448
440
}
449
441
}
450
442
451
443
impl From < TryFromIntError > for PasswordProtectedKeyEnvelopeError {
452
444
fn from ( err : TryFromIntError ) -> Self {
453
- PasswordProtectedKeyEnvelopeError :: ParsingError ( format ! ( "Invalid integer: {}" , err) )
445
+ PasswordProtectedKeyEnvelopeError :: Parsing ( format ! ( "Invalid integer: {}" , err) )
454
446
}
455
447
}
456
448
0 commit comments