From 8c94d798c639fc3fb121599b71907c014d42b5d4 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Sat, 8 Jul 2023 21:11:25 +0100 Subject: [PATCH] fix key encodings again (#1089) --- atuin-client/Cargo.toml | 2 +- atuin-client/src/encryption.rs | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/atuin-client/Cargo.toml b/atuin-client/Cargo.toml index 6492bd15..6da679f8 100644 --- a/atuin-client/Cargo.toml +++ b/atuin-client/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "atuin-client" -edition = "2018" +edition = "2021" description = "client library for atuin" version = { workspace = true } diff --git a/atuin-client/src/encryption.rs b/atuin-client/src/encryption.rs index 86125159..8512b6d4 100644 --- a/atuin-client/src/encryption.rs +++ b/atuin-client/src/encryption.rs @@ -12,7 +12,7 @@ use std::{io::prelude::*, path::PathBuf}; use base64::prelude::{Engine, BASE64_STANDARD}; use chrono::{DateTime, Utc}; -use eyre::{bail, eyre, Context, Result}; +use eyre::{bail, ensure, eyre, Context, Result}; use fs_err as fs; use rmp::{decode::Bytes, Marker}; use serde::{Deserialize, Serialize}; @@ -58,8 +58,12 @@ pub fn load_key(settings: &Settings) -> Result { pub fn encode_key(key: &Key) -> Result { let mut buf = vec![]; - rmp::encode::write_bin(&mut buf, key.as_slice()) + rmp::encode::write_array_len(&mut buf, key.len() as u32) .wrap_err("could not encode key to message pack")?; + for b in key { + rmp::encode::write_uint(&mut buf, *b as u64) + .wrap_err("could not encode key to message pack")?; + } let buf = BASE64_STANDARD.encode(buf); Ok(buf) @@ -72,15 +76,19 @@ pub fn decode_key(key: String) -> Result { // old code wrote the key as a fixed length array of 32 bytes // new code writes the key with a length prefix - if buf.len() == 32 { - Ok(*Key::from_slice(&buf)) - } else { - let mut bytes = Bytes::new(&buf); - let key_len = rmp::decode::read_bin_len(&mut bytes).map_err(error_report)?; - if key_len != 32 || bytes.remaining_slice().len() != key_len as usize { - bail!("encryption key is not the correct size") + match <[u8; 32]>::try_from(&*buf) { + Ok(key) => Ok(key.into()), + Err(_) => { + let mut bytes = rmp::decode::Bytes::new(&buf); + let key_len = + rmp::decode::read_array_len(&mut bytes).map_err(|err| eyre!("{err:?}"))?; + ensure!(key_len == 32, "encryption key is not the correct size"); + let mut key = Key::default(); + for i in &mut key { + *i = rmp::decode::read_int(&mut bytes).map_err(|err| eyre!("{err:?}"))?; + } + Ok(key) } - Ok(*Key::from_slice(bytes.remaining_slice())) } }