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.
This commit is contained in:
bunnie 2024-09-23 01:14:41 +08:00
parent 89bc7c1b68
commit 44b9041d0d

View file

@ -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::<fn()>,
) {
Ok(mut pddb_key) => {
let mut buf = Vec::<u8>::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::<rkyv::rancor::Error>(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::<OwnedTrustAnchor>(&bytes, pos) };
let ta = archive.deserialize(&mut AllocDeserializer {}).ok();
let ata = unsafe { rkyv::access_unchecked::<ArchivedOwnedTrustAnchor>(&bytes[..]) };
let ta = rkyv::deserialize::<OwnedTrustAnchor, rkyv::rancor::Error>(ata).ok();
log::info!("get trust anchor {}", key);
log::trace!("get trust anchor'{}' = '{:?}'", key, &ta);
ta