-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcrypto.rs
More file actions
116 lines (100 loc) · 5.2 KB
/
Copy pathcrypto.rs
File metadata and controls
116 lines (100 loc) · 5.2 KB
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
use bitwarden_core::key_management::{
V2UpgradeToken,
crypto::{
DeriveKeyConnectorRequest, DerivePinKeyResponse, EnrollPinResponse, InitOrgCryptoRequest,
InitUserCryptoRequest, ReinitUserCryptoRequest, UpdateKdfResponse, UpdatePasswordResponse,
},
};
use bitwarden_crypto::{EncString, Kdf, RotateableKeySet, UnsignedSharedKey};
use bitwarden_encoding::B64;
use crate::error::Result;
#[allow(missing_docs)]
#[derive(uniffi::Object)]
pub struct CryptoClient(pub(crate) bitwarden_core::key_management::CryptoClient);
#[uniffi::export(async_runtime = "tokio")]
impl CryptoClient {
/// Initialization method for the user crypto. Needs to be called before any other crypto
/// operations.
pub async fn initialize_user_crypto(&self, req: InitUserCryptoRequest) -> Result<()> {
Ok(self.0.initialize_user_crypto(req).await?)
}
/// Initialization method for the organization crypto. Needs to be called after
/// `initialize_user_crypto` but before any other crypto operations.
pub async fn initialize_org_crypto(&self, req: InitOrgCryptoRequest) -> Result<()> {
Ok(self.0.initialize_org_crypto(req).await?)
}
/// Re-initialize the user's cryptographic state during an unlock session for handling a synced
/// v2 upgrade token. Requires the SDK to be unlocked. See
/// [`bitwarden_core::key_management::CryptoClient::reinit_user_crypto`].
pub async fn reinit_user_crypto(&self, req: ReinitUserCryptoRequest) -> Result<()> {
Ok(self.0.reinit_user_crypto(req).await?)
}
/// Get the uses's decrypted encryption key. Note: It's very important
/// to keep this key safe, as it can be used to decrypt all of the user's data
pub async fn get_user_encryption_key(&self) -> Result<B64> {
Ok(self.0.get_user_encryption_key().await?)
}
/// Create the data necessary to update the user's password. The user's encryption key is
/// re-encrypted with the new password. This returns the new encrypted user key and the new
/// password hash but does not update sdk state.
pub async fn make_update_password(
&self,
new_password: String,
) -> Result<UpdatePasswordResponse> {
Ok(self.0.make_update_password(new_password).await?)
}
/// Generates a PIN protected user key from the provided PIN. The result can be stored and later
/// used to initialize another client instance by using the PIN and the PIN key with
/// `initialize_user_crypto`.
pub async fn derive_pin_key(&self, pin: String) -> Result<DerivePinKeyResponse> {
Ok(self.0.derive_pin_key(pin).await?)
}
/// Derives the pin protected user key from encrypted pin. Used when pin requires master
/// password on first unlock.
pub async fn derive_pin_user_key(&self, encrypted_pin: EncString) -> Result<EncString> {
Ok(self.0.derive_pin_user_key(encrypted_pin).await?)
}
/// Protects the current user key with the provided PIN. The result can be stored and later
/// used to initialize another client instance by using the PIN and the PIN key with
/// `initialize_user_crypto`.
pub fn enroll_pin(&self, pin: String) -> Result<EnrollPinResponse> {
Ok(self.0.enroll_pin(pin)?)
}
/// Protects the current user key with the provided PIN. The result can be stored and later
/// used to initialize another client instance by using the PIN and the PIN key with
/// `initialize_user_crypto`. The provided pin is encrypted with the user key.
pub fn enroll_pin_with_encrypted_pin(
&self,
encrypted_pin: EncString,
) -> Result<EnrollPinResponse> {
Ok(self
.0
.enroll_pin_with_encrypted_pin(encrypted_pin.to_string())?)
}
pub fn enroll_admin_password_reset(&self, public_key: B64) -> Result<UnsignedSharedKey> {
Ok(self.0.enroll_admin_password_reset(public_key)?)
}
/// Derive the master key for migrating to the key connector
pub fn derive_key_connector(&self, request: DeriveKeyConnectorRequest) -> Result<B64> {
Ok(self.0.derive_key_connector(request)?)
}
/// Creates the a new rotateable key set for the current user key protected
/// by a key derived from the given PRF.
pub fn make_prf_user_key_set(&self, prf: B64) -> Result<RotateableKeySet> {
Ok(self.0.make_prf_user_key_set(prf)?)
}
/// Create the data necessary to update the user's kdf settings. The user's encryption key is
/// re-encrypted for the password under the new kdf settings. This returns the new encrypted
/// user key and the new password hash but does not update sdk state.
/// Note: This is deprecated. Please use the user-crypto-management client instead.
pub async fn make_update_kdf(&self, password: String, kdf: Kdf) -> Result<UpdateKdfResponse> {
#[allow(deprecated)]
Ok(self.0.make_update_kdf(password, kdf).await?)
}
/// Gets the upgraded V2 user key using an upgrade token.
/// If the current key is already V2, returns it directly.
/// If the current key is V1 and a token is provided, extracts the V2 key.
pub fn get_upgraded_user_key(&self, upgrade_token: Option<V2UpgradeToken>) -> Result<B64> {
Ok(self.0.get_upgraded_user_key(upgrade_token)?)
}
}