bitwarden_core/client/
encryption_settings.rs1#[cfg(any(feature = "internal", feature = "secrets"))]
2use bitwarden_crypto::KeyStore;
3#[cfg(feature = "secrets")]
4use bitwarden_crypto::SymmetricCryptoKey;
5#[cfg(feature = "internal")]
6use bitwarden_crypto::UnsignedSharedKey;
7use bitwarden_error::bitwarden_error;
8use thiserror::Error;
9#[cfg(feature = "internal")]
10use tracing::{info, instrument};
11
12#[cfg(any(feature = "secrets", feature = "internal"))]
13use crate::OrganizationId;
14#[cfg(any(feature = "internal", feature = "secrets"))]
15use crate::key_management::{KeySlotIds, SymmetricKeySlotId};
16use crate::{MissingPrivateKeyError, error::UserIdAlreadySetError};
17
18#[allow(missing_docs)]
19#[bitwarden_error(flat)]
20#[derive(Debug, Error)]
21pub enum EncryptionSettingsError {
22 #[error("Cryptography error, {0}")]
23 Crypto(#[from] bitwarden_crypto::CryptoError),
24
25 #[error("Cryptography Initialization error")]
26 CryptoInitialization,
27
28 #[error(transparent)]
29 MissingPrivateKey(#[from] MissingPrivateKeyError),
30
31 #[error(transparent)]
32 UserIdAlreadySet(#[from] UserIdAlreadySetError),
33
34 #[error("Wrong Pin")]
35 WrongPin,
36
37 #[error("Unable to set user-key to state")]
39 UserKeyStateUpdateFailed,
40
41 #[error("Unable to retrieve user-key from state")]
42 UserKeyStateRetrievalFailed,
43
44 #[error("Invalid upgrade token")]
45 InvalidUpgradeToken,
46
47 #[error("Key connector retrieval failed")]
49 KeyConnectorRetrievalFailed,
50
51 #[error("Unable to initialize local user data key")]
53 LocalUserDataKeyInitFailed,
54
55 #[error("Unable to load local user data key into key store")]
57 LocalUserDataKeyLoadFailed,
58
59 #[error("Unable to migrate local user data key after user key upgrade")]
61 LocalUserDataMigrationFailed,
62}
63
64#[allow(missing_docs)]
65pub struct EncryptionSettings {}
66
67impl EncryptionSettings {
68 #[cfg(feature = "secrets")]
71 pub(crate) fn new_single_org_key(
72 organization_id: OrganizationId,
73 key: SymmetricCryptoKey,
74 store: &KeyStore<KeySlotIds>,
75 ) {
76 #[allow(deprecated)]
78 store
79 .context_mut()
80 .set_symmetric_key(SymmetricKeySlotId::Organization(organization_id), key)
81 .expect("Mutable context");
82 }
83
84 #[cfg(feature = "internal")]
85 #[instrument(err, skip_all)]
86 pub(crate) fn set_org_keys(
87 org_enc_keys: Vec<(OrganizationId, UnsignedSharedKey)>,
88 store: &KeyStore<KeySlotIds>,
89 ) -> Result<(), EncryptionSettingsError> {
90 use crate::key_management::PrivateKeySlotId;
91
92 let mut ctx = store.context_mut();
93
94 if org_enc_keys.is_empty() {
96 info!("No organization keys to set");
97 return Ok(());
98 }
99
100 if !ctx.has_private_key(PrivateKeySlotId::UserPrivateKey) {
101 info!("User private key is missing, cannot set organization keys");
102 return Err(MissingPrivateKeyError.into());
103 }
104
105 ctx.retain_symmetric_keys(|key_ref| {
108 !matches!(key_ref, SymmetricKeySlotId::Organization(_))
109 });
110
111 info!("Decrypting organization keys");
112 for (org_id, org_enc_key) in org_enc_keys {
114 let _span =
115 tracing::span!(tracing::Level::INFO, "decapsulate_org_key", org_id = %org_id)
116 .entered();
117 match org_enc_key.decapsulate(PrivateKeySlotId::UserPrivateKey, &mut ctx) {
118 Err(e) => {
119 tracing::error!("Failed to decapsulate organization key: {}", e);
120 return Err(e.into());
121 }
122 Ok(org_symmetric_key) => {
123 tracing::info!(
124 org_id = %org_id,
125 "Successfully decapsulated organization key for org",
126 );
127 ctx.persist_symmetric_key(
128 org_symmetric_key,
129 SymmetricKeySlotId::Organization(org_id),
130 )?;
131 }
132 }
133 }
134
135 Ok(())
136 }
137}