From 44b9041d0d9e854bef72aa0f6b00738f01dde082 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 23 Sep 2024 01:14:41 +0800 Subject: [PATCH] attempt to update rkyv use in TLS library @nworbnhoj would love it if you could have a look at the implementation and let me know if I've ported this correctly. I think what was going on is you were using rkyv to serialize a structure to the PDDB. I've just updated the ser/de to use the latest `alloc` API (which is much simpler than the API used in xous-ipc because it is no-alloc). I'll try and run some tests that involve the TLS crate too down the road under the theory that it would suss out any porting errors. --- libs/tls/src/lib.rs | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 8e530f560..a6febe522 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -3,19 +3,14 @@ mod danger; pub mod ota; pub mod xtls; -use std::convert::{Into, TryFrom, TryInto}; +use std::convert::{Into, TryInto}; use std::io::{Error, ErrorKind, Read, Write}; use std::net::TcpStream; use std::sync::Arc; use locales::t; use modals::Modals; -use ota::OwnedTrustAnchor; -use rkyv::{ - de::deserializers::AllocDeserializer, - ser::{serializers::WriteSerializer, Serializer}, - Deserialize, -}; +use ota::{ArchivedOwnedTrustAnchor, OwnedTrustAnchor}; use rustls::pki_types::{CertificateDer, TrustAnchor}; use rustls::{ClientConfig, ClientConnection, RootCertStore}; use x509_parser::prelude::{parse_x509_certificate, FromDer, X509Certificate}; @@ -141,20 +136,7 @@ impl Tls { None::, ) { Ok(mut pddb_key) => { - let mut buf = Vec::::new(); - // reserve 2 bytes to hold a u16 (see below) - let reserved = 2; - buf.push(0u8); - buf.push(0u8); - // serialize the trust-anchor - let mut serializer = WriteSerializer::with_pos(buf, reserved); - let pos = serializer.serialize_value(ta).unwrap(); - let mut bytes = serializer.into_inner(); - // copy pop u16 into the first 2 bytes to enable the rkyv archive to be deserialised - let pos: u16 = u16::try_from(pos).expect("data > u16"); - let pos_bytes = pos.to_be_bytes(); - bytes[0] = pos_bytes[0]; - bytes[1] = pos_bytes[1]; + let bytes = rkyv::to_bytes::(ta).unwrap(); match pddb_key.write(&bytes) { Ok(len) => { self.pddb.sync().ok(); @@ -184,12 +166,8 @@ impl Tls { let mut bytes = [0u8; ota::MAX_OTA_BYTES]; match pddb_key.read(&mut bytes) { Ok(_) => { - // extract pos u16 from the first 2 bytes - let pos: u16 = u16::from_be_bytes([bytes[0], bytes[1]]); - let pos: usize = pos.into(); - // deserialize the trust-anchor - let archive = unsafe { rkyv::archived_value::(&bytes, pos) }; - let ta = archive.deserialize(&mut AllocDeserializer {}).ok(); + let ata = unsafe { rkyv::access_unchecked::(&bytes[..]) }; + let ta = rkyv::deserialize::(ata).ok(); log::info!("get trust anchor {}", key); log::trace!("get trust anchor'{}' = '{:?}'", key, &ta); ta