Skip to main content

bitwarden_vault/cipher/
drivers_license.rs

1use bitwarden_api_api::models::CipherDriversLicenseModel;
2use bitwarden_core::key_management::{KeySlotIds, SymmetricKeySlotId};
3use bitwarden_crypto::{
4    CompositeEncryptable, CryptoError, Decryptable, EncString, KeyStoreContext,
5    PrimitiveEncryptable,
6};
7use serde::{Deserialize, Serialize};
8#[cfg(feature = "wasm")]
9use tsify::Tsify;
10
11use super::cipher::CipherKind;
12use crate::{Cipher, VaultParseError, cipher::cipher::CopyableCipherFields};
13
14#[derive(Serialize, Deserialize, Debug, Clone)]
15#[serde(rename_all = "camelCase")]
16#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
17#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
18pub struct DriversLicense {
19    pub first_name: Option<EncString>,
20    pub middle_name: Option<EncString>,
21    pub last_name: Option<EncString>,
22    pub date_of_birth: Option<EncString>,
23    pub license_number: Option<EncString>,
24    pub issuing_country: Option<EncString>,
25    pub issuing_state: Option<EncString>,
26    pub issue_date: Option<EncString>,
27    pub expiration_date: Option<EncString>,
28    pub issuing_authority: Option<EncString>,
29    pub license_class: Option<EncString>,
30}
31
32#[allow(missing_docs)]
33#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
34#[serde(rename_all = "camelCase", deny_unknown_fields)]
35#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
36#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
37pub struct DriversLicenseView {
38    pub first_name: Option<String>,
39    pub middle_name: Option<String>,
40    pub last_name: Option<String>,
41    pub date_of_birth: Option<String>,
42    pub license_number: Option<String>,
43    pub issuing_country: Option<String>,
44    pub issuing_state: Option<String>,
45    pub issue_date: Option<String>,
46    pub expiration_date: Option<String>,
47    pub issuing_authority: Option<String>,
48    pub license_class: Option<String>,
49}
50
51impl CompositeEncryptable<KeySlotIds, SymmetricKeySlotId, DriversLicense> for DriversLicenseView {
52    fn encrypt_composite(
53        &self,
54        ctx: &mut KeyStoreContext<KeySlotIds>,
55        key: SymmetricKeySlotId,
56    ) -> Result<DriversLicense, CryptoError> {
57        Ok(DriversLicense {
58            first_name: self.first_name.encrypt(ctx, key)?,
59            middle_name: self.middle_name.encrypt(ctx, key)?,
60            last_name: self.last_name.encrypt(ctx, key)?,
61            date_of_birth: self.date_of_birth.encrypt(ctx, key)?,
62            license_number: self.license_number.encrypt(ctx, key)?,
63            issuing_country: self.issuing_country.encrypt(ctx, key)?,
64            issuing_state: self.issuing_state.encrypt(ctx, key)?,
65            issue_date: self.issue_date.encrypt(ctx, key)?,
66            expiration_date: self.expiration_date.encrypt(ctx, key)?,
67            issuing_authority: self.issuing_authority.encrypt(ctx, key)?,
68            license_class: self.license_class.encrypt(ctx, key)?,
69        })
70    }
71}
72
73impl Decryptable<KeySlotIds, SymmetricKeySlotId, DriversLicenseView> for DriversLicense {
74    fn decrypt(
75        &self,
76        ctx: &mut KeyStoreContext<KeySlotIds>,
77        key: SymmetricKeySlotId,
78    ) -> Result<DriversLicenseView, CryptoError> {
79        Ok(DriversLicenseView {
80            first_name: self.first_name.decrypt(ctx, key).ok().flatten(),
81            middle_name: self.middle_name.decrypt(ctx, key).ok().flatten(),
82            last_name: self.last_name.decrypt(ctx, key).ok().flatten(),
83            date_of_birth: self.date_of_birth.decrypt(ctx, key).ok().flatten(),
84            license_number: self.license_number.decrypt(ctx, key).ok().flatten(),
85            issuing_country: self.issuing_country.decrypt(ctx, key).ok().flatten(),
86            issuing_state: self.issuing_state.decrypt(ctx, key).ok().flatten(),
87            issue_date: self.issue_date.decrypt(ctx, key).ok().flatten(),
88            expiration_date: self.expiration_date.decrypt(ctx, key).ok().flatten(),
89            issuing_authority: self.issuing_authority.decrypt(ctx, key).ok().flatten(),
90            license_class: self.license_class.decrypt(ctx, key).ok().flatten(),
91        })
92    }
93}
94
95impl CipherKind for DriversLicense {
96    fn decrypt_subtitle(
97        &self,
98        ctx: &mut KeyStoreContext<KeySlotIds>,
99        key: SymmetricKeySlotId,
100    ) -> Result<String, CryptoError> {
101        let first_name: Option<String> = self
102            .first_name
103            .as_ref()
104            .map(|f| f.decrypt(ctx, key))
105            .transpose()?;
106        let last_name: Option<String> = self
107            .last_name
108            .as_ref()
109            .map(|l| l.decrypt(ctx, key))
110            .transpose()?;
111        let parts: Vec<String> = [first_name, last_name]
112            .into_iter()
113            .flatten()
114            .filter(|s| !s.is_empty())
115            .collect();
116        Ok(parts.join(" "))
117    }
118
119    fn get_copyable_fields(&self, _: Option<&Cipher>) -> Vec<CopyableCipherFields> {
120        [
121            self.first_name
122                .as_ref()
123                .map(|_| CopyableCipherFields::DriversLicenseFirstName),
124            self.middle_name
125                .as_ref()
126                .map(|_| CopyableCipherFields::DriversLicenseMiddleName),
127            self.last_name
128                .as_ref()
129                .map(|_| CopyableCipherFields::DriversLicenseLastName),
130            self.license_number
131                .as_ref()
132                .map(|_| CopyableCipherFields::DriversLicenseLicenseNumber),
133        ]
134        .into_iter()
135        .flatten()
136        .collect()
137    }
138}
139
140impl TryFrom<CipherDriversLicenseModel> for DriversLicense {
141    type Error = VaultParseError;
142
143    fn try_from(dl: CipherDriversLicenseModel) -> Result<Self, Self::Error> {
144        Ok(Self {
145            first_name: EncString::try_from_optional(dl.first_name)?,
146            middle_name: EncString::try_from_optional(dl.middle_name)?,
147            last_name: EncString::try_from_optional(dl.last_name)?,
148            date_of_birth: EncString::try_from_optional(dl.date_of_birth)?,
149            license_number: EncString::try_from_optional(dl.license_number)?,
150            issuing_country: EncString::try_from_optional(dl.issuing_country)?,
151            issuing_state: EncString::try_from_optional(dl.issuing_state)?,
152            issue_date: EncString::try_from_optional(dl.issue_date)?,
153            expiration_date: EncString::try_from_optional(dl.expiration_date)?,
154            issuing_authority: EncString::try_from_optional(dl.issuing_authority)?,
155            license_class: EncString::try_from_optional(dl.license_class)?,
156        })
157    }
158}
159
160impl From<DriversLicense> for CipherDriversLicenseModel {
161    fn from(dl: DriversLicense) -> Self {
162        Self {
163            first_name: dl.first_name.map(|n| n.to_string()),
164            middle_name: dl.middle_name.map(|n| n.to_string()),
165            last_name: dl.last_name.map(|n| n.to_string()),
166            date_of_birth: dl.date_of_birth.map(|n| n.to_string()),
167            license_number: dl.license_number.map(|n| n.to_string()),
168            issuing_country: dl.issuing_country.map(|n| n.to_string()),
169            issuing_state: dl.issuing_state.map(|n| n.to_string()),
170            issue_date: dl.issue_date.map(|n| n.to_string()),
171            expiration_date: dl.expiration_date.map(|n| n.to_string()),
172            issuing_authority: dl.issuing_authority.map(|n| n.to_string()),
173            license_class: dl.license_class.map(|n| n.to_string()),
174        }
175    }
176}
177
178#[cfg(test)]
179mod tests {
180    use bitwarden_core::key_management::create_test_crypto_with_user_key;
181    use bitwarden_crypto::SymmetricCryptoKey;
182
183    use super::*;
184    use crate::cipher::cipher::CopyableCipherFields;
185
186    const TEST_VECTOR_DL_KEY: &str =
187        "taqu8EG0R01PCl/p0mM8q2Pz3OmCcw66AEoXF82dwhsIUgSR7Fw7yZNXkjtWNC3qxtjkKsFn8xMg1zwUQplD3Q==";
188    const TEST_VECTOR_DL_JSON: &str = r#"{"firstName":"2.knZfEnxppJSnCv2K1JLJZQ==|WdifZ8QIUkFuSeVk8WBlSQ==|OX4LNsv+l0Z2EhqNWMgemTZgMwLs5o8T6Osra9nzmU4=","middleName":"2.QrbWBvz1v1139ab0PXCE0g==|qjpNmAzfm5thbkfsb+inmA==|FVmBwCVB+VCKPGKSTLqBCpQWfYeomO/9K4M80i4Hz74=","lastName":"2.kLvD+H8AvuZ26sZSVXCwJw==|hOpCZQ1pSmRxU+10Mb6itg==|890LMSpvyTPumcaBZV2Q/sa0aU0xWSxHGn6Oz/aUvcY=","dateOfBirth":"2.tz5PMtlTQlGiyhrmtFpkfQ==|q7aKh0RO/3UpuzxkWJj/lw==|W+dL85zWGf6hmZby1rkekwFSiAe3Nlf8JcQ/r8aRvC8=","licenseNumber":"2.tqNWH0mhqCqVkGuylbGJPQ==|d60Z0GfOZrQdnDDRSQSYig==|bVQ9kEO13+pGFr5CnA2AcsXHlKdntsB7dWXxu9dPViQ=","issuingCountry":"2.4c/os2TnGc4lV48zTXtcrA==|48WLHeewxx53cR0oAJrT9Q==|DbdhHEl+ZjFJsAwCJqdx6smENOJ6aa6prOSSzrIaxsw=","issuingState":"2.sXVmW8/M1Dt9of6UR8bOFQ==|Se5KFBLQ0EiUywa3Hll6eg==|pIfsxpZrXh1Z3+VG2HX2sXpQfJ1GrlFq8DyunOr/vk0=","issueDate":"2.NiEamcsCLptp7ZGR5yv+Kw==|iS0jlJFbscygj+8q/E3FWA==|M0Iq6DqgDTI3l/OArBeqtdR4dHXLi87QexEK1H7XwsE=","expirationDate":"2.oEePzQ/7a8bC8y93Wf1cog==|QtbxhibvRGdBctqETfYqgQ==|zQflEdAhXKxZelF7qLbAdJNqhZXG0v331XwdGEzr10Q=","issuingAuthority":"2.prE7jFCIfr0+DU0XnOSXWw==|fyISTE3sQFp1GnmVpaTRGg==|a+i1vTOoPtj0bkvFjRUXdXxkVq2RtOkv6zuMxS+BOQc=","licenseClass":"2.Yk070ToPNCnbxxQ2CPe20w==|4eb1WaAOXenbQcotMhgaCw==|yGkq0dg6b65Nf6WxbOPV/r7MRDKFplcWLQ7sZNmOlCY="}"#;
189
190    fn test_drivers_license_view() -> DriversLicenseView {
191        DriversLicenseView {
192            first_name: Some("John".to_string()),
193            middle_name: Some("Michael".to_string()),
194            last_name: Some("Doe".to_string()),
195            date_of_birth: Some("1985-06-15".to_string()),
196            license_number: Some("DL-987654".to_string()),
197            issuing_country: Some("US".to_string()),
198            issuing_state: Some("NY".to_string()),
199            issue_date: Some("2020-01-01".to_string()),
200            expiration_date: Some("2028-01-01".to_string()),
201            issuing_authority: Some("NY DMV".to_string()),
202            license_class: Some("D".to_string()),
203        }
204    }
205
206    #[test]
207    #[ignore]
208    fn generate_test_vector() {
209        let key = SymmetricCryptoKey::make_aes256_cbc_hmac_key();
210        let key_b64 = key.to_base64();
211        let key_store = create_test_crypto_with_user_key(key);
212        let key_slot = SymmetricKeySlotId::User;
213        let mut ctx = key_store.context();
214
215        let encrypted = test_drivers_license_view()
216            .encrypt_composite(&mut ctx, key_slot)
217            .unwrap();
218        let json = serde_json::to_string(&encrypted).unwrap();
219
220        println!("const TEST_VECTOR_DL_KEY: &str = \"{key_b64}\";");
221        println!("const TEST_VECTOR_DL_JSON: &str = r#\"{json}\"#;");
222    }
223
224    #[test]
225    fn test_recorded_drivers_license_test_vector() {
226        let key =
227            SymmetricCryptoKey::try_from(TEST_VECTOR_DL_KEY.to_string()).expect("valid test key");
228        let key_store = create_test_crypto_with_user_key(key);
229        let key_slot = SymmetricKeySlotId::User;
230        let mut ctx = key_store.context();
231
232        let encrypted: DriversLicense =
233            serde_json::from_str(TEST_VECTOR_DL_JSON).expect("valid test vector JSON");
234        let decrypted: DriversLicenseView = encrypted
235            .decrypt(&mut ctx, key_slot)
236            .expect("DriversLicense has changed in a backwards-incompatible way. Existing encrypted data must remain decryptable. If a new format is needed, create a new version instead of modifying the existing one.");
237
238        assert_eq!(decrypted, test_drivers_license_view());
239    }
240
241    #[test]
242    fn test_subtitle_drivers_license() {
243        let key = SymmetricCryptoKey::try_from("hvBMMb1t79YssFZkpetYsM3deyVuQv4r88Uj9gvYe0+G8EwxvW3v1iywVmSl61iwzd17JW5C/ivzxSP2C9h7Tw==".to_string()).unwrap();
244        let key_store = create_test_crypto_with_user_key(key);
245        let key = SymmetricKeySlotId::User;
246        let mut ctx = key_store.context();
247
248        let first_name_encrypted = "John".to_owned().encrypt(&mut ctx, key).unwrap();
249        let last_name_encrypted = "Doe".to_owned().encrypt(&mut ctx, key).unwrap();
250
251        let dl = DriversLicense {
252            first_name: Some(first_name_encrypted),
253            middle_name: None,
254            last_name: Some(last_name_encrypted),
255            date_of_birth: None,
256            license_number: None,
257            issuing_country: None,
258            issuing_state: None,
259            issue_date: None,
260            expiration_date: None,
261            issuing_authority: None,
262            license_class: None,
263        };
264
265        assert_eq!(
266            dl.decrypt_subtitle(&mut ctx, key).unwrap(),
267            "John Doe".to_string()
268        );
269    }
270
271    #[test]
272    fn test_get_copyable_fields_drivers_license() {
273        let enc_str: EncString = "2.tMIugb6zQOL+EuOizna1wQ==|W5dDLoNJtajN68yeOjrr6w==|qS4hwJB0B0gNLI0o+jxn+sKMBmvtVgJCRYNEXBZoGeE=".parse().unwrap();
274
275        let dl = DriversLicense {
276            first_name: Some(enc_str.clone()),
277            middle_name: Some(enc_str.clone()),
278            last_name: Some(enc_str.clone()),
279            date_of_birth: Some(enc_str.clone()),
280            license_number: Some(enc_str.clone()),
281            issuing_country: Some(enc_str.clone()),
282            issuing_state: Some(enc_str.clone()),
283            issue_date: Some(enc_str.clone()),
284            expiration_date: Some(enc_str.clone()),
285            issuing_authority: Some(enc_str.clone()),
286            license_class: Some(enc_str),
287        };
288
289        let copyable_fields = dl.get_copyable_fields(None);
290        assert_eq!(
291            copyable_fields,
292            vec![
293                CopyableCipherFields::DriversLicenseFirstName,
294                CopyableCipherFields::DriversLicenseMiddleName,
295                CopyableCipherFields::DriversLicenseLastName,
296                CopyableCipherFields::DriversLicenseLicenseNumber,
297            ]
298        );
299    }
300}