fix key encodings again (#1089)

This commit is contained in:
Conrad Ludgate 2023-07-08 21:11:25 +01:00 committed by GitHub
parent eb5e1c213c
commit 8c94d798c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "atuin-client" name = "atuin-client"
edition = "2018" edition = "2021"
description = "client library for atuin" description = "client library for atuin"
version = { workspace = true } version = { workspace = true }

View file

@ -12,7 +12,7 @@ use std::{io::prelude::*, path::PathBuf};
use base64::prelude::{Engine, BASE64_STANDARD}; use base64::prelude::{Engine, BASE64_STANDARD};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use eyre::{bail, eyre, Context, Result}; use eyre::{bail, ensure, eyre, Context, Result};
use fs_err as fs; use fs_err as fs;
use rmp::{decode::Bytes, Marker}; use rmp::{decode::Bytes, Marker};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -58,8 +58,12 @@ pub fn load_key(settings: &Settings) -> Result<Key> {
pub fn encode_key(key: &Key) -> Result<String> { pub fn encode_key(key: &Key) -> Result<String> {
let mut buf = vec![]; 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")?; .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); let buf = BASE64_STANDARD.encode(buf);
Ok(buf) Ok(buf)
@ -72,15 +76,19 @@ pub fn decode_key(key: String) -> Result<Key> {
// old code wrote the key as a fixed length array of 32 bytes // old code wrote the key as a fixed length array of 32 bytes
// new code writes the key with a length prefix // new code writes the key with a length prefix
if buf.len() == 32 { match <[u8; 32]>::try_from(&*buf) {
Ok(*Key::from_slice(&buf)) Ok(key) => Ok(key.into()),
} else { Err(_) => {
let mut bytes = Bytes::new(&buf); let mut bytes = rmp::decode::Bytes::new(&buf);
let key_len = rmp::decode::read_bin_len(&mut bytes).map_err(error_report)?; let key_len =
if key_len != 32 || bytes.remaining_slice().len() != key_len as usize { rmp::decode::read_array_len(&mut bytes).map_err(|err| eyre!("{err:?}"))?;
bail!("encryption key is not the correct size") 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()))
} }
} }