mirror of
https://github.com/rustdesk/hbb_common.git
synced 2026-08-27 04:37:35 +00:00
fix(security): add nonce to local symmetric encryption
Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
+44
-1
@@ -710,7 +710,12 @@ impl Config {
|
||||
config.password =
|
||||
encrypt_str_or_original(&config.password, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
|
||||
}
|
||||
config.enc_id = encrypt_str_or_original(&config.id, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
|
||||
let (stored_id, encrypted, _) =
|
||||
decrypt_str_or_original(&config.enc_id, PASSWORD_ENC_VERSION);
|
||||
if !encrypted || stored_id != config.id {
|
||||
config.enc_id =
|
||||
encrypt_str_or_original(&config.id, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
|
||||
}
|
||||
config.id = "".to_owned();
|
||||
Config::store_(&config, "");
|
||||
}
|
||||
@@ -3469,6 +3474,44 @@ mod tests {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_store_keeps_existing_enc_id_when_id_is_unchanged() {
|
||||
let mut cfg = Config::default();
|
||||
cfg.id = "123456789".to_owned();
|
||||
cfg.enc_id = encrypt_str_or_original(&cfg.id, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
|
||||
let original_enc_id = cfg.enc_id.clone();
|
||||
|
||||
with_config_and_hard_settings(Config::default(), HashMap::new(), || {
|
||||
assert!(Config::set(cfg));
|
||||
|
||||
assert_eq!(Config::load().enc_id, original_enc_id);
|
||||
assert_eq!(Config::get().id, "123456789");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_store_rewrites_enc_id_when_id_changes() {
|
||||
let original_id = "123456789";
|
||||
let updated_id = "987654321";
|
||||
let mut cfg = Config::default();
|
||||
cfg.id = updated_id.to_owned();
|
||||
let original_enc_id =
|
||||
encrypt_str_or_original(original_id, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
|
||||
cfg.enc_id = original_enc_id.clone();
|
||||
|
||||
with_config_and_hard_settings(Config::default(), HashMap::new(), || {
|
||||
assert!(Config::set(cfg));
|
||||
|
||||
let stored = Config::load().enc_id;
|
||||
let (stored_id, encrypted, _) =
|
||||
decrypt_str_or_original(&stored, PASSWORD_ENC_VERSION);
|
||||
assert_ne!(stored, original_enc_id);
|
||||
assert!(encrypted);
|
||||
assert_eq!(stored_id, updated_id);
|
||||
assert_eq!(Config::get().id, updated_id);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_does_not_convert_plaintext_permanent_password_to_storage_format_in_memory() {
|
||||
let mut cfg = Config::default();
|
||||
|
||||
Reference in New Issue
Block a user