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::{KeyIds, SymmetricKeyId};
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
60#[allow(missing_docs)]
61pub struct EncryptionSettings {}
62
63impl EncryptionSettings {
64 #[cfg(feature = "secrets")]
67 pub(crate) fn new_single_org_key(
68 organization_id: OrganizationId,
69 key: SymmetricCryptoKey,
70 store: &KeyStore<KeyIds>,
71 ) {
72 #[allow(deprecated)]
74 store
75 .context_mut()
76 .set_symmetric_key(SymmetricKeyId::Organization(organization_id), key)
77 .expect("Mutable context");
78 }
79
80 #[cfg(feature = "internal")]
81 #[instrument(err, skip_all)]
82 pub(crate) fn set_org_keys(
83 org_enc_keys: Vec<(OrganizationId, UnsignedSharedKey)>,
84 store: &KeyStore<KeyIds>,
85 ) -> Result<(), EncryptionSettingsError> {
86 use crate::key_management::PrivateKeyId;
87
88 let mut ctx = store.context_mut();
89
90 if org_enc_keys.is_empty() {
92 info!("No organization keys to set");
93 return Ok(());
94 }
95
96 if !ctx.has_private_key(PrivateKeyId::UserPrivateKey) {
97 info!("User private key is missing, cannot set organization keys");
98 return Err(MissingPrivateKeyError.into());
99 }
100
101 ctx.retain_symmetric_keys(|key_ref| !matches!(key_ref, SymmetricKeyId::Organization(_)));
104
105 info!("Decrypting organization keys");
106 for (org_id, org_enc_key) in org_enc_keys {
108 let _span =
109 tracing::span!(tracing::Level::INFO, "decapsulate_org_key", org_id = %org_id)
110 .entered();
111 match org_enc_key.decapsulate(PrivateKeyId::UserPrivateKey, &mut ctx) {
112 Err(e) => {
113 tracing::error!("Failed to decapsulate organization key: {}", e);
114 return Err(e.into());
115 }
116 Ok(org_symmetric_key) => {
117 tracing::info!(
118 org_id = %org_id,
119 "Successfully decapsulated organization key for org",
120 );
121 ctx.persist_symmetric_key(
122 org_symmetric_key,
123 SymmetricKeyId::Organization(org_id),
124 )?;
125 }
126 }
127 }
128
129 Ok(())
130 }
131}