From 0d40054e24bb55996356e455e68d88256d776b38 Mon Sep 17 00:00:00 2001 From: Hinton Date: Tue, 23 Apr 2024 18:27:18 +0200 Subject: [PATCH] Shore up uri checksum --- .../src/sensitive/sensitive.rs | 7 ++++ crates/bitwarden/src/vault/cipher/login.rs | 34 +++++++++++-------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/crates/bitwarden-crypto/src/sensitive/sensitive.rs b/crates/bitwarden-crypto/src/sensitive/sensitive.rs index dac688f46..9673c47e5 100644 --- a/crates/bitwarden-crypto/src/sensitive/sensitive.rs +++ b/crates/bitwarden-crypto/src/sensitive/sensitive.rs @@ -3,6 +3,7 @@ use std::{ fmt::{self, Formatter}, }; +use generic_array::{ArrayLength, GenericArray}; use schemars::JsonSchema; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use zeroize::{Zeroize, ZeroizeOnDrop}; @@ -93,6 +94,12 @@ impl From for SensitiveVec { } } +impl> From>> for SensitiveVec { + fn from(val: Sensitive>) -> Self { + SensitiveVec::new(Box::new(val.value.to_vec())) + } +} + impl SensitiveString { pub fn decode_base64(self, engine: T) -> Result { // Prevent accidental copies by allocating the full size diff --git a/crates/bitwarden/src/vault/cipher/login.rs b/crates/bitwarden/src/vault/cipher/login.rs index 1612d869d..bc474c573 100644 --- a/crates/bitwarden/src/vault/cipher/login.rs +++ b/crates/bitwarden/src/vault/cipher/login.rs @@ -1,13 +1,15 @@ -use base64::{engine::general_purpose::STANDARD, Engine}; +use base64::engine::general_purpose::STANDARD; use bitwarden_api_api::models::{CipherLoginModel, CipherLoginUriModel}; use bitwarden_crypto::{ - CryptoError, DecryptedString, EncString, KeyDecryptable, KeyEncryptable, SensitiveString, - SymmetricCryptoKey, + CryptoError, DecryptedString, EncString, KeyDecryptable, KeyEncryptable, Sensitive, + SensitiveVec, SymmetricCryptoKey, }; use chrono::{DateTime, Utc}; +use hmac::digest::generic_array::GenericArray; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; +use sha2::Digest; use crate::error::{require, Error, Result}; @@ -50,26 +52,28 @@ impl LoginUriView { let Some(cs) = &self.uri_checksum else { return false; }; - let Ok(cs) = STANDARD.decode(cs.expose()) else { + let Ok(cs) = cs.clone().decode_base64(STANDARD) else { return false; }; - use sha2::Digest; - let uri_hash = sha2::Sha256::new() - .chain_update(uri.expose().as_bytes()) - .finalize(); + let uri_hash: Sensitive> = Sensitive::new(Box::new( + sha2::Sha256::new() + .chain_update(uri.expose().as_bytes()) + .finalize(), + )); - uri_hash.as_slice() == cs + uri_hash.expose().as_slice() == cs.expose() } pub(crate) fn generate_checksum(&mut self) { if let Some(uri) = &self.uri { - use sha2::Digest; - let uri_hash = sha2::Sha256::new() - .chain_update(uri.expose().as_bytes()) - .finalize(); - let uri_hash = STANDARD.encode(uri_hash.as_slice()); - self.uri_checksum = Some(SensitiveString::new(Box::new(uri_hash))); + let uri_hash: SensitiveVec = Sensitive::new(Box::new( + sha2::Sha256::new() + .chain_update(uri.expose().as_bytes()) + .finalize(), + )) + .into(); + self.uri_checksum = Some(uri_hash.encode_base64(STANDARD)) } } }