bitwarden_core/mobile/
crypto.rs

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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
use std::collections::HashMap;

use base64::{engine::general_purpose::STANDARD, Engine};
use bitwarden_crypto::{
    AsymmetricCryptoKey, AsymmetricEncString, EncString, Kdf, KeyDecryptable, KeyEncryptable,
    MasterKey, SymmetricCryptoKey, UserKey,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use {tsify_next::Tsify, wasm_bindgen::prelude::*};

use crate::{
    client::{encryption_settings::EncryptionSettingsError, LoginMethod, UserLoginMethod},
    error::{Error, Result},
    Client,
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct InitUserCryptoRequest {
    /// The user's KDF parameters, as received from the prelogin request
    pub kdf_params: Kdf,
    /// The user's email address
    pub email: String,
    /// The user's encrypted private key
    pub private_key: String,
    /// The initialization method to use
    pub method: InitUserCryptoMethod,
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub enum InitUserCryptoMethod {
    Password {
        /// The user's master password
        password: String,
        /// The user's encrypted symmetric crypto key
        user_key: String,
    },
    DecryptedKey {
        /// The user's decrypted encryption key, obtained using `get_user_encryption_key`
        decrypted_user_key: String,
    },
    Pin {
        /// The user's PIN
        pin: String,
        /// The user's symmetric crypto key, encrypted with the PIN. Use `derive_pin_key` to obtain
        /// this.
        pin_protected_user_key: EncString,
    },
    AuthRequest {
        /// Private Key generated by the `crate::auth::new_auth_request`.
        request_private_key: String,

        method: AuthRequestMethod,
    },
    DeviceKey {
        /// The device's DeviceKey
        device_key: String,
        /// The Device Private Key
        protected_device_private_key: EncString,
        /// The user's symmetric crypto key, encrypted with the Device Key.
        device_protected_user_key: AsymmetricEncString,
    },
    KeyConnector {
        /// Base64 encoded master key, retrieved from the key connector.
        master_key: String,
        /// The user's encrypted symmetric crypto key
        user_key: String,
    },
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub enum AuthRequestMethod {
    UserKey {
        /// User Key protected by the private key provided in `AuthRequestResponse`.
        protected_user_key: AsymmetricEncString,
    },
    MasterKey {
        /// Master Key protected by the private key provided in `AuthRequestResponse`.
        protected_master_key: AsymmetricEncString,
        /// User Key protected by the MasterKey, provided by the auth response.
        auth_request_key: EncString,
    },
}

pub async fn initialize_user_crypto(
    client: &Client,
    req: InitUserCryptoRequest,
) -> Result<(), EncryptionSettingsError> {
    use bitwarden_crypto::{DeviceKey, PinKey};

    use crate::auth::{auth_request_decrypt_master_key, auth_request_decrypt_user_key};

    let private_key: EncString = req.private_key.parse()?;

    match req.method {
        InitUserCryptoMethod::Password { password, user_key } => {
            let user_key: EncString = user_key.parse()?;

            let master_key = MasterKey::derive(&password, &req.email, &req.kdf_params)?;
            client
                .internal
                .initialize_user_crypto_master_key(master_key, user_key, private_key)?;
        }
        InitUserCryptoMethod::DecryptedKey { decrypted_user_key } => {
            let user_key = SymmetricCryptoKey::try_from(decrypted_user_key)?;
            client
                .internal
                .initialize_user_crypto_decrypted_key(user_key, private_key)?;
        }
        InitUserCryptoMethod::Pin {
            pin,
            pin_protected_user_key,
        } => {
            let pin_key = PinKey::derive(pin.as_bytes(), req.email.as_bytes(), &req.kdf_params)?;
            client.internal.initialize_user_crypto_pin(
                pin_key,
                pin_protected_user_key,
                private_key,
            )?;
        }
        InitUserCryptoMethod::AuthRequest {
            request_private_key,
            method,
        } => {
            let user_key = match method {
                AuthRequestMethod::UserKey { protected_user_key } => {
                    auth_request_decrypt_user_key(request_private_key, protected_user_key)?
                }
                AuthRequestMethod::MasterKey {
                    protected_master_key,
                    auth_request_key,
                } => auth_request_decrypt_master_key(
                    request_private_key,
                    protected_master_key,
                    auth_request_key,
                )?,
            };
            client
                .internal
                .initialize_user_crypto_decrypted_key(user_key, private_key)?;
        }
        InitUserCryptoMethod::DeviceKey {
            device_key,
            protected_device_private_key,
            device_protected_user_key,
        } => {
            let device_key = DeviceKey::try_from(device_key)?;
            let user_key = device_key
                .decrypt_user_key(protected_device_private_key, device_protected_user_key)?;

            client
                .internal
                .initialize_user_crypto_decrypted_key(user_key, private_key)?;
        }
        InitUserCryptoMethod::KeyConnector {
            master_key,
            user_key,
        } => {
            let master_key = MasterKey::new(SymmetricCryptoKey::try_from(master_key)?);
            let user_key: EncString = user_key.parse()?;

            client
                .internal
                .initialize_user_crypto_master_key(master_key, user_key, private_key)?;
        }
    }

    client
        .internal
        .set_login_method(crate::client::LoginMethod::User(
            crate::client::UserLoginMethod::Username {
                client_id: "".to_string(),
                email: req.email,
                kdf: req.kdf_params,
            },
        ));

    Ok(())
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct InitOrgCryptoRequest {
    /// The encryption keys for all the organizations the user is a part of
    pub organization_keys: HashMap<uuid::Uuid, AsymmetricEncString>,
}

pub async fn initialize_org_crypto(
    client: &Client,
    req: InitOrgCryptoRequest,
) -> Result<(), EncryptionSettingsError> {
    let organization_keys = req.organization_keys.into_iter().collect();
    client.internal.initialize_org_crypto(organization_keys)?;
    Ok(())
}

pub async fn get_user_encryption_key(client: &Client) -> Result<String> {
    let enc = client.internal.get_encryption_settings()?;
    let user_key = enc.get_key(&None)?;

    Ok(user_key.to_base64())
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct UpdatePasswordResponse {
    /// Hash of the new password
    password_hash: String,
    /// User key, encrypted with the new password
    new_key: EncString,
}

pub fn update_password(client: &Client, new_password: String) -> Result<UpdatePasswordResponse> {
    let enc = client.internal.get_encryption_settings()?;
    let user_key = enc.get_key(&None)?;

    let login_method = client
        .internal
        .get_login_method()
        .ok_or(Error::NotAuthenticated)?;

    // Derive a new master key from password
    let new_master_key = match login_method.as_ref() {
        LoginMethod::User(
            UserLoginMethod::Username { email, kdf, .. }
            | UserLoginMethod::ApiKey { email, kdf, .. },
        ) => MasterKey::derive(&new_password, email, kdf)?,
        #[cfg(feature = "secrets")]
        LoginMethod::ServiceAccount(_) => return Err(Error::NotAuthenticated),
    };

    let new_key = new_master_key.encrypt_user_key(user_key)?;

    let password_hash = new_master_key.derive_master_key_hash(
        new_password.as_bytes(),
        bitwarden_crypto::HashPurpose::ServerAuthorization,
    )?;

    Ok(UpdatePasswordResponse {
        password_hash,
        new_key,
    })
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct DerivePinKeyResponse {
    /// [UserKey](bitwarden_crypto::UserKey) protected by PIN
    pin_protected_user_key: EncString,
    /// PIN protected by [UserKey](bitwarden_crypto::UserKey)
    encrypted_pin: EncString,
}

pub fn derive_pin_key(client: &Client, pin: String) -> Result<DerivePinKeyResponse> {
    let enc = client.internal.get_encryption_settings()?;
    let user_key = enc.get_key(&None)?;

    let login_method = client
        .internal
        .get_login_method()
        .ok_or(Error::NotAuthenticated)?;

    let pin_protected_user_key = derive_pin_protected_user_key(&pin, &login_method, user_key)?;

    Ok(DerivePinKeyResponse {
        pin_protected_user_key,
        encrypted_pin: pin.encrypt_with_key(user_key)?,
    })
}

pub fn derive_pin_user_key(client: &Client, encrypted_pin: EncString) -> Result<EncString> {
    let enc = client.internal.get_encryption_settings()?;
    let user_key = enc.get_key(&None)?;

    let pin: String = encrypted_pin.decrypt_with_key(user_key)?;
    let login_method = client
        .internal
        .get_login_method()
        .ok_or(Error::NotAuthenticated)?;

    derive_pin_protected_user_key(&pin, &login_method, user_key)
}

fn derive_pin_protected_user_key(
    pin: &str,
    login_method: &LoginMethod,
    user_key: &SymmetricCryptoKey,
) -> Result<EncString> {
    use bitwarden_crypto::PinKey;

    let derived_key = match login_method {
        LoginMethod::User(
            UserLoginMethod::Username { email, kdf, .. }
            | UserLoginMethod::ApiKey { email, kdf, .. },
        ) => PinKey::derive(pin.as_bytes(), email.as_bytes(), kdf)?,
        #[cfg(feature = "secrets")]
        LoginMethod::ServiceAccount(_) => return Err(Error::NotAuthenticated),
    };

    Ok(derived_key.encrypt_user_key(user_key)?)
}

pub(super) fn enroll_admin_password_reset(
    client: &Client,
    public_key: String,
) -> Result<AsymmetricEncString> {
    use base64::{engine::general_purpose::STANDARD, Engine};
    use bitwarden_crypto::AsymmetricPublicCryptoKey;

    let public_key = AsymmetricPublicCryptoKey::from_der(&STANDARD.decode(public_key)?)?;
    let enc = client.internal.get_encryption_settings()?;
    let key = enc.get_key(&None)?;

    Ok(AsymmetricEncString::encrypt_rsa2048_oaep_sha1(
        &key.to_vec(),
        &public_key,
    )?)
}

#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct DeriveKeyConnectorRequest {
    /// Encrypted user key, used to validate the master key
    pub user_key_encrypted: EncString,

    pub password: String,
    pub kdf: Kdf,
    pub email: String,
}

/// Derive the master key for migrating to the key connector
pub(super) fn derive_key_connector(request: DeriveKeyConnectorRequest) -> Result<String> {
    let master_key = MasterKey::derive(&request.password, &request.email, &request.kdf)?;
    master_key
        .decrypt_user_key(request.user_key_encrypted)
        .map_err(|_| "wrong password")?;

    Ok(master_key.to_base64())
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct MakeKeyPairResponse {
    /// The user's public key
    user_public_key: String,
    /// User's private key, encrypted with the user key
    user_key_encrypted_private_key: EncString,
}

pub fn make_key_pair(user_key: String) -> Result<MakeKeyPairResponse> {
    let user_key = UserKey::new(SymmetricCryptoKey::try_from(user_key)?);

    let key_pair = user_key.make_key_pair()?;

    Ok(MakeKeyPairResponse {
        user_public_key: key_pair.public,
        user_key_encrypted_private_key: key_pair.private,
    })
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct VerifyAsymmetricKeysRequest {
    /// The user's user key
    user_key: String,
    /// The user's public key
    user_public_key: String,
    /// User's private key, encrypted with the user key
    user_key_encrypted_private_key: EncString,
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct VerifyAsymmetricKeysResponse {
    /// Whether the user's private key was decryptable by the user key.
    private_key_decryptable: bool,
    /// Whether the user's private key was a valid RSA key and matched the public key provided.
    valid_private_key: bool,
}

pub fn verify_asymmetric_keys(
    request: VerifyAsymmetricKeysRequest,
) -> Result<VerifyAsymmetricKeysResponse> {
    #[derive(Debug, thiserror::Error)]
    enum VerifyError {
        #[error("Failed to decrypt private key: {0:?}")]
        DecryptFailed(bitwarden_crypto::CryptoError),
        #[error("Failed to parse decrypted private key: {0:?}")]
        ParseFailed(bitwarden_crypto::CryptoError),
        #[error("Failed to derive a public key: {0:?}")]
        PublicFailed(bitwarden_crypto::CryptoError),
        #[error("Derived public key doesn't match")]
        KeyMismatch,
    }

    fn verify_inner(
        user_key: &SymmetricCryptoKey,
        request: &VerifyAsymmetricKeysRequest,
    ) -> Result<(), VerifyError> {
        let decrypted_private_key: Vec<u8> = request
            .user_key_encrypted_private_key
            .decrypt_with_key(user_key)
            .map_err(VerifyError::DecryptFailed)?;

        let private_key = AsymmetricCryptoKey::from_der(&decrypted_private_key)
            .map_err(VerifyError::ParseFailed)?;

        let derived_public_key_vec = private_key
            .to_public_der()
            .map_err(VerifyError::PublicFailed)?;

        let derived_public_key = STANDARD.encode(&derived_public_key_vec);

        if derived_public_key != request.user_public_key {
            return Err(VerifyError::KeyMismatch);
        }
        Ok(())
    }

    let user_key = SymmetricCryptoKey::try_from(request.user_key.clone())?;

    Ok(match verify_inner(&user_key, &request) {
        Ok(_) => VerifyAsymmetricKeysResponse {
            private_key_decryptable: true,
            valid_private_key: true,
        },
        Err(e) => {
            log::debug!("User asymmetric keys verification: {}", e);

            VerifyAsymmetricKeysResponse {
                private_key_decryptable: !matches!(e, VerifyError::DecryptFailed(_)),
                valid_private_key: false,
            }
        }
    })
}

#[cfg(test)]
mod tests {
    use std::num::NonZeroU32;

    use bitwarden_crypto::RsaKeyPair;

    use super::*;
    use crate::Client;

    #[tokio::test]
    async fn test_update_password() {
        let client = Client::new(None);

        let priv_key = "2.kmLY8NJVuiKBFJtNd/ZFpA==|qOodlRXER+9ogCe3yOibRHmUcSNvjSKhdDuztLlucs10jLiNoVVVAc+9KfNErLSpx5wmUF1hBOJM8zwVPjgQTrmnNf/wuDpwiaCxNYb/0v4FygPy7ccAHK94xP1lfqq7U9+tv+/yiZSwgcT+xF0wFpoxQeNdNRFzPTuD9o4134n8bzacD9DV/WjcrXfRjbBCzzuUGj1e78+A7BWN7/5IWLz87KWk8G7O/W4+8PtEzlwkru6Wd1xO19GYU18oArCWCNoegSmcGn7w7NDEXlwD403oY8Oa7ylnbqGE28PVJx+HLPNIdSC6YKXeIOMnVs7Mctd/wXC93zGxAWD6ooTCzHSPVV50zKJmWIG2cVVUS7j35H3rGDtUHLI+ASXMEux9REZB8CdVOZMzp2wYeiOpggebJy6MKOZqPT1R3X0fqF2dHtRFPXrNsVr1Qt6bS9qTyO4ag1/BCvXF3P1uJEsI812BFAne3cYHy5bIOxuozPfipJrTb5WH35bxhElqwT3y/o/6JWOGg3HLDun31YmiZ2HScAsUAcEkA4hhoTNnqy4O2s3yVbCcR7jF7NLsbQc0MDTbnjxTdI4VnqUIn8s2c9hIJy/j80pmO9Bjxp+LQ9a2hUkfHgFhgHxZUVaeGVth8zG2kkgGdrp5VHhxMVFfvB26Ka6q6qE/UcS2lONSv+4T8niVRJz57qwctj8MNOkA3PTEfe/DP/LKMefke31YfT0xogHsLhDkx+mS8FCc01HReTjKLktk/Jh9mXwC5oKwueWWwlxI935ecn+3I2kAuOfMsgPLkoEBlwgiREC1pM7VVX1x8WmzIQVQTHd4iwnX96QewYckGRfNYWz/zwvWnjWlfcg8kRSe+68EHOGeRtC5r27fWLqRc0HNcjwpgHkI/b6czerCe8+07TWql4keJxJxhBYj3iOH7r9ZS8ck51XnOb8tGL1isimAJXodYGzakwktqHAD7MZhS+P02O+6jrg7d+yPC2ZCuS/3TOplYOCHQIhnZtR87PXTUwr83zfOwAwCyv6KP84JUQ45+DItrXLap7nOVZKQ5QxYIlbThAO6eima6Zu5XHfqGPMNWv0bLf5+vAjIa5np5DJrSwz9no/hj6CUh0iyI+SJq4RGI60lKtypMvF6MR3nHLEHOycRUQbZIyTHWl4QQLdHzuwN9lv10ouTEvNr6sFflAX2yb6w3hlCo7oBytH3rJekjb3IIOzBpeTPIejxzVlh0N9OT5MZdh4sNKYHUoWJ8mnfjdM+L4j5Q2Kgk/XiGDgEebkUxiEOQUdVpePF5uSCE+TPav/9FIRGXGiFn6NJMaU7aBsDTFBLloffFLYDpd8/bTwoSvifkj7buwLYM+h/qcnfdy5FWau1cKav+Blq/ZC0qBpo658RTC8ZtseAFDgXoQZuksM10hpP9bzD04Bx30xTGX81QbaSTNwSEEVrOtIhbDrj9OI43KH4O6zLzK+t30QxAv5zjk10RZ4+5SAdYndIlld9Y62opCfPDzRy3ubdve4ZEchpIKWTQvIxq3T5ogOhGaWBVYnkMtM2GVqvWV//46gET5SH/MdcwhACUcZ9kCpMnWH9CyyUwYvTT3UlNyV+DlS27LMPvaw7tx7qa+GfNCoCBd8S4esZpQYK/WReiS8=|pc7qpD42wxyXemdNPuwxbh8iIaryrBPu8f/DGwYdHTw=";

        let kdf = Kdf::PBKDF2 {
            iterations: 100_000.try_into().unwrap(),
        };

        initialize_user_crypto(
            & client,
            InitUserCryptoRequest {
                kdf_params: kdf.clone(),
                email: "[email protected]".into(),
                private_key: priv_key.to_owned(),
                method: InitUserCryptoMethod::Password {
                    password: "asdfasdfasdf".into(),
                    user_key: "2.u2HDQ/nH2J7f5tYHctZx6Q==|NnUKODz8TPycWJA5svexe1wJIz2VexvLbZh2RDfhj5VI3wP8ZkR0Vicvdv7oJRyLI1GyaZDBCf9CTBunRTYUk39DbZl42Rb+Xmzds02EQhc=|rwuo5wgqvTJf3rgwOUfabUyzqhguMYb3sGBjOYqjevc=".into(),
                },
            },
        )
        .await
        .unwrap();

        let new_password_response = update_password(&client, "123412341234".into()).unwrap();

        let client2 = Client::new(None);

        initialize_user_crypto(
            &client2,
            InitUserCryptoRequest {
                kdf_params: kdf.clone(),
                email: "[email protected]".into(),
                private_key: priv_key.to_owned(),
                method: InitUserCryptoMethod::Password {
                    password: "123412341234".into(),
                    user_key: new_password_response.new_key.to_string(),
                },
            },
        )
        .await
        .unwrap();

        let new_hash = client2
            .kdf()
            .hash_password(
                "[email protected]".into(),
                "123412341234".into(),
                kdf.clone(),
                bitwarden_crypto::HashPurpose::ServerAuthorization,
            )
            .await
            .unwrap();

        assert_eq!(new_hash, new_password_response.password_hash);

        assert_eq!(
            client
                .internal
                .get_encryption_settings()
                .unwrap()
                .get_key(&None)
                .unwrap()
                .to_base64(),
            client2
                .internal
                .get_encryption_settings()
                .unwrap()
                .get_key(&None)
                .unwrap()
                .to_base64()
        );
    }

    #[tokio::test]
    async fn test_initialize_user_crypto_pin() {
        let client = Client::new(None);

        let priv_key = "2.kmLY8NJVuiKBFJtNd/ZFpA==|qOodlRXER+9ogCe3yOibRHmUcSNvjSKhdDuztLlucs10jLiNoVVVAc+9KfNErLSpx5wmUF1hBOJM8zwVPjgQTrmnNf/wuDpwiaCxNYb/0v4FygPy7ccAHK94xP1lfqq7U9+tv+/yiZSwgcT+xF0wFpoxQeNdNRFzPTuD9o4134n8bzacD9DV/WjcrXfRjbBCzzuUGj1e78+A7BWN7/5IWLz87KWk8G7O/W4+8PtEzlwkru6Wd1xO19GYU18oArCWCNoegSmcGn7w7NDEXlwD403oY8Oa7ylnbqGE28PVJx+HLPNIdSC6YKXeIOMnVs7Mctd/wXC93zGxAWD6ooTCzHSPVV50zKJmWIG2cVVUS7j35H3rGDtUHLI+ASXME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jk10RZ4+5SAdYndIlld9Y62opCfPDzRy3ubdve4ZEchpIKWTQvIxq3T5ogOhGaWBVYnkMtM2GVqvWV//46gET5SH/MdcwhACUcZ9kCpMnWH9CyyUwYvTT3UlNyV+DlS27LMPvaw7tx7qa+GfNCoCBd8S4esZpQYK/WReiS8=|pc7qpD42wxyXemdNPuwxbh8iIaryrBPu8f/DGwYdHTw=";

        initialize_user_crypto(
            & client,
            InitUserCryptoRequest {
                kdf_params: Kdf::PBKDF2 {
                    iterations: 100_000.try_into().unwrap(),
                },
                email: "[email protected]".into(),
                private_key: priv_key.to_owned(),
                method: InitUserCryptoMethod::Password {
                    password: "asdfasdfasdf".into(),
                    user_key: "2.u2HDQ/nH2J7f5tYHctZx6Q==|NnUKODz8TPycWJA5svexe1wJIz2VexvLbZh2RDfhj5VI3wP8ZkR0Vicvdv7oJRyLI1GyaZDBCf9CTBunRTYUk39DbZl42Rb+Xmzds02EQhc=|rwuo5wgqvTJf3rgwOUfabUyzqhguMYb3sGBjOYqjevc=".into(),
                },
            },
        )
        .await
        .unwrap();

        let pin_key = derive_pin_key(&client, "1234".into()).unwrap();

        // Verify we can unlock with the pin
        let client2 = Client::new(None);
        initialize_user_crypto(
            &client2,
            InitUserCryptoRequest {
                kdf_params: Kdf::PBKDF2 {
                    iterations: 100_000.try_into().unwrap(),
                },
                email: "[email protected]".into(),
                private_key: priv_key.to_owned(),
                method: InitUserCryptoMethod::Pin {
                    pin: "1234".into(),
                    pin_protected_user_key: pin_key.pin_protected_user_key,
                },
            },
        )
        .await
        .unwrap();

        assert_eq!(
            client
                .internal
                .get_encryption_settings()
                .unwrap()
                .get_key(&None)
                .unwrap()
                .to_base64(),
            client2
                .internal
                .get_encryption_settings()
                .unwrap()
                .get_key(&None)
                .unwrap()
                .to_base64()
        );

        // Verify we can derive the pin protected user key from the encrypted pin
        let pin_protected_user_key = derive_pin_user_key(&client, pin_key.encrypted_pin).unwrap();

        let client3 = Client::new(None);

        initialize_user_crypto(
            &client3,
            InitUserCryptoRequest {
                kdf_params: Kdf::PBKDF2 {
                    iterations: 100_000.try_into().unwrap(),
                },
                email: "[email protected]".into(),
                private_key: priv_key.to_owned(),
                method: InitUserCryptoMethod::Pin {
                    pin: "1234".into(),
                    pin_protected_user_key,
                },
            },
        )
        .await
        .unwrap();

        assert_eq!(
            client
                .internal
                .get_encryption_settings()
                .unwrap()
                .get_key(&None)
                .unwrap()
                .to_base64(),
            client3
                .internal
                .get_encryption_settings()
                .unwrap()
                .get_key(&None)
                .unwrap()
                .to_base64()
        );
    }

    #[test]
    fn test_enroll_admin_password_reset() {
        use base64::{engine::general_purpose::STANDARD, Engine};
        use bitwarden_crypto::AsymmetricCryptoKey;

        let client = Client::new(None);

        let master_key = MasterKey::derive(
            "asdfasdfasdf",
            "[email protected]",
            &Kdf::PBKDF2 {
                iterations: NonZeroU32::new(600_000).unwrap(),
            },
        )
        .unwrap();

        let user_key = "2.Q/2PhzcC7GdeiMHhWguYAQ==|GpqzVdr0go0ug5cZh1n+uixeBC3oC90CIe0hd/HWA/pTRDZ8ane4fmsEIcuc8eMKUt55Y2q/fbNzsYu41YTZzzsJUSeqVjT8/iTQtgnNdpo=|dwI+uyvZ1h/iZ03VQ+/wrGEFYVewBUUl/syYgjsNMbE=".parse().unwrap();
        let private_key ="2.yN7l00BOlUE0Sb0M//Q53w==|EwKG/BduQRQ33Izqc/ogoBROIoI5dmgrxSo82sgzgAMIBt3A2FZ9vPRMY+GWT85JiqytDitGR3TqwnFUBhKUpRRAq4x7rA6A1arHrFp5Tp1p21O3SfjtvB3quiOKbqWk6ZaU1Np9HwqwAecddFcB0YyBEiRX3VwF2pgpAdiPbSMuvo2qIgyob0CUoC/h4Bz1be7Qa7B0Xw9/fMKkB1LpOm925lzqosyMQM62YpMGkjMsbZz0uPopu32fxzDWSPr+kekNNyLt9InGhTpxLmq1go/pXR2uw5dfpXc5yuta7DB0EGBwnQ8Vl5HPdDooqOTD9I1jE0mRyuBpWTTI3FRnu3JUh3rI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AmV7Ytu4fR3x2sF0Yfi87YhHFuCMpV/DsqxmUizyiJuD938eRcH8hzR/VO53Qo3UIsqOLcyXtTv6THjSlTopQ+JOLOnHm1w8dzYbLN44OG44rRsbihMUQp+wUZ6bsI8rrOnm9WErzkbQFbrfAINdoCiNa6cimYIjvvnMTaFWNymqY1vZxGztQiMiHiHYwTfwHTXrb9j0uPM=|09J28iXv9oWzYtzK2LBT6Yht4IT4MijEkk0fwFdrVQ4=".parse().unwrap();
        client
            .internal
            .initialize_user_crypto_master_key(master_key, user_key, private_key)
            .unwrap();

        let public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsy7RFHcX3C8Q4/OMmhhbFReYWfB45W9PDTEA8tUZwZmtOiN2RErIS2M1c+K/4HoDJ/TjpbX1f2MZcr4nWvKFuqnZXyewFc+jmvKVewYi+NAu2++vqKq2kKcmMNhwoQDQdQIVy/Uqlp4Cpi2cIwO6ogq5nHNJGR3jm+CpyrafYlbz1bPvL3hbyoGDuG2tgADhyhXUdFuef2oF3wMvn1lAJAvJnPYpMiXUFmj1ejmbwtlxZDrHgUJvUcp7nYdwUKaFoi+sOttHn3u7eZPtNvxMjhSS/X/1xBIzP/mKNLdywH5LoRxniokUk+fV3PYUxJsiU3lV0Trc/tH46jqd8ZGjmwIDAQAB";

        let encrypted = enroll_admin_password_reset(&client, public_key.to_owned()).unwrap();

        let private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCzLtEUdxfcLxDj84yaGFsVF5hZ8Hjlb08NMQDy1RnBma06I3ZESshLYzVz4r/gegMn9OOltfV/Yxlyvida8oW6qdlfJ7AVz6Oa8pV7BiL40C7b76+oqraQpyYw2HChANB1AhXL9SqWngKmLZwjA7qiCrmcc0kZHeOb4KnKtp9iVvPVs+8veFvKgYO4ba2AAOHKFdR0W55/agXfAy+fWUAkC8mc9ikyJdQWaPV6OZvC2XFkOseBQm9Rynudh3BQpoWiL6w620efe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VqHkFtIQkRSCIftzPLlpMGKha/YBV8c1fvC4LD0NPh/Ynv0gtECgYEAyOZg95/kte0jpgUEgwuMrzkhY/AaUJULFuR5MkyvReEbtSBQwV5tx60+T95PHNiFooWWVXiLMsAgyI2IbkxVR1Pzdri3gWK5CTfqb7kLuaj/B7SGvBa2Sxo478KS5K8tBBBWkITqo+wLC0mn3uZi1dyMWO1zopTA+KtEGF2dtGQ=";
        let private_key =
            AsymmetricCryptoKey::from_der(&STANDARD.decode(private_key).unwrap()).unwrap();
        let decrypted: Vec<u8> = encrypted.decrypt_with_key(&private_key).unwrap();

        let enc = client.internal.get_encryption_settings().unwrap();
        let expected = enc.get_key(&None).unwrap();
        assert_eq!(&decrypted, &expected.to_vec());
    }

    #[test]
    fn test_derive_key_connector() {
        let request = DeriveKeyConnectorRequest {
            password: "asdfasdfasdf".to_string(),
            email: "[email protected]".to_string(),
            kdf: Kdf::PBKDF2 {
                iterations: NonZeroU32::new(600_000).unwrap(),
            },
            user_key_encrypted: "2.Q/2PhzcC7GdeiMHhWguYAQ==|GpqzVdr0go0ug5cZh1n+uixeBC3oC90CIe0hd/HWA/pTRDZ8ane4fmsEIcuc8eMKUt55Y2q/fbNzsYu41YTZzzsJUSeqVjT8/iTQtgnNdpo=|dwI+uyvZ1h/iZ03VQ+/wrGEFYVewBUUl/syYgjsNMbE=".parse().unwrap(),
        };

        let result = derive_key_connector(request).unwrap();

        assert_eq!(result, "ySXq1RVLKEaV1eoQE/ui9aFKIvXTl9PAXwp1MljfF50=");
    }

    fn setup_asymmetric_keys_test() -> (UserKey, RsaKeyPair) {
        let master_key = MasterKey::derive(
            "asdfasdfasdf",
            "[email protected]",
            &Kdf::PBKDF2 {
                iterations: NonZeroU32::new(600_000).unwrap(),
            },
        )
        .unwrap();
        let user_key = (master_key.make_user_key().unwrap()).0;
        let key_pair = user_key.make_key_pair().unwrap();

        (user_key, key_pair)
    }

    #[test]
    fn test_make_key_pair() {
        let (user_key, _) = setup_asymmetric_keys_test();

        let response = make_key_pair(user_key.0.to_base64()).unwrap();

        assert!(!response.user_public_key.is_empty());
        let encrypted_private_key = response.user_key_encrypted_private_key;
        let private_key: Vec<u8> = encrypted_private_key.decrypt_with_key(&user_key.0).unwrap();
        assert!(!private_key.is_empty());
    }

    #[test]
    fn test_verify_asymmetric_keys_success() {
        let (user_key, key_pair) = setup_asymmetric_keys_test();

        let request = VerifyAsymmetricKeysRequest {
            user_key: user_key.0.to_base64(),
            user_public_key: key_pair.public,
            user_key_encrypted_private_key: key_pair.private,
        };
        let response = verify_asymmetric_keys(request).unwrap();

        assert!(response.private_key_decryptable);
        assert!(response.valid_private_key);
    }

    #[test]
    fn test_verify_asymmetric_keys_decrypt_failed() {
        let (user_key, key_pair) = setup_asymmetric_keys_test();
        let undecryptable_private_key = "2.cqD39M4erPZ3tWaz2Fng9w==|+Bsp/xvM30oo+HThKN12qirK0A63EjMadcwethCX7kEgfL5nEXgAFsSgRBMpByc1djgpGDMXzUTLOE+FejXRsrEHH/ICZ7jPMgSR+lV64Mlvw3fgvDPQdJ6w3MCmjPueGQtrlPj1K78BkRomN3vQwwRBFUIJhLAnLshTOIFrSghoyG78na7McqVMMD0gmC0zmRaSs2YWu/46ES+2Rp8V5OC4qdeeoJM9MQfaOtmaqv7NRVDeDM3DwoyTJAOcon8eovMKE4jbFPUboiXjNQBkBgjvLhco3lVJnFcQuYgmjqrwuUQRsfAtZjxFXg/RQSH2D+SI5uRaTNQwkL4iJqIw7BIKtI0gxDz6eCVdq/+DLhpImgCV/aaIhF/jkpGqLCceFsYMbuqdULMM1VYKgV+IAuyC65R+wxOaKS+1IevvPnNp7tgKAvT5+shFg8piusj+rQ49daX2SmV2OImwdWMmmX93bcVV0xJ/WYB1yrqmyRUcTwyvX3RQF25P5okIIzFasRp8jXFZe8C6f93yzkn1TPQbp95zF4OsWjfPFVH4hzca07ACt2HjbAB75JakWbFA5MbCF8aOIwIfeLVhVlquQXCldOHCsl22U/f3HTGLB9OS8F83CDAy7qZq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erifyAsymmetricKeysRequest {
            user_key: user_key.0.to_base64(),
            user_public_key: key_pair.public,
            user_key_encrypted_private_key: undecryptable_private_key,
        };
        let response = verify_asymmetric_keys(request).unwrap();

        assert!(!response.private_key_decryptable);
        assert!(!response.valid_private_key);
    }

    #[test]
    fn test_verify_asymmetric_keys_parse_failed() {
        let (user_key, key_pair) = setup_asymmetric_keys_test();

        let invalid_private_key = "bad_key"
            .to_string()
            .into_bytes()
            .encrypt_with_key(&user_key.0)
            .unwrap();

        let request = VerifyAsymmetricKeysRequest {
            user_key: user_key.0.to_base64(),
            user_public_key: key_pair.public,
            user_key_encrypted_private_key: invalid_private_key,
        };
        let response = verify_asymmetric_keys(request).unwrap();

        assert!(response.private_key_decryptable);
        assert!(!response.valid_private_key);
    }

    #[test]
    fn test_verify_asymmetric_keys_key_mismatch() {
        let (user_key, key_pair) = setup_asymmetric_keys_test();
        let new_key_pair = user_key.make_key_pair().unwrap();

        let request = VerifyAsymmetricKeysRequest {
            user_key: user_key.0.to_base64(),
            user_public_key: key_pair.public,
            user_key_encrypted_private_key: new_key_pair.private,
        };
        let response = verify_asymmetric_keys(request).unwrap();

        assert!(response.private_key_decryptable);
        assert!(!response.valid_private_key);
    }
}