mirror of https://github.com/ctz/rustls
238 lines
5.3 KiB
Rust
238 lines
5.3 KiB
Rust
use crate::error::InvalidMessage;
|
|
use crate::msgs::codec;
|
|
use crate::msgs::codec::{Codec, Reader};
|
|
|
|
use alloc::vec::Vec;
|
|
use core::fmt;
|
|
use core::ops::{Deref, DerefMut};
|
|
|
|
use pki_types::CertificateDer;
|
|
use zeroize::Zeroize;
|
|
|
|
use super::codec::ReaderMut;
|
|
|
|
/// An externally length'd payload
|
|
#[derive(Clone, Eq, PartialEq)]
|
|
pub struct Payload(pub Vec<u8>);
|
|
|
|
impl Codec for Payload {
|
|
fn encode(&self, bytes: &mut Vec<u8>) {
|
|
bytes.extend_from_slice(&self.0);
|
|
}
|
|
|
|
fn read(r: &mut Reader) -> Result<Self, InvalidMessage> {
|
|
Ok(Self::read(r))
|
|
}
|
|
}
|
|
|
|
impl Payload {
|
|
pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
|
|
Self(bytes.into())
|
|
}
|
|
|
|
pub fn empty() -> Self {
|
|
Self::new(Vec::new())
|
|
}
|
|
|
|
pub fn read(r: &mut Reader) -> Self {
|
|
Self(r.rest().to_vec())
|
|
}
|
|
}
|
|
|
|
/// Non-owning version of [`Payload`]
|
|
pub struct BorrowedPayload<'a>(&'a mut [u8]);
|
|
|
|
impl Deref for BorrowedPayload<'_> {
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl<'a> DerefMut for BorrowedPayload<'a> {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl<'a> BorrowedPayload<'a> {
|
|
#[cfg(test)]
|
|
pub(crate) fn new(bytes: &'a mut [u8]) -> Self {
|
|
Self(bytes)
|
|
}
|
|
|
|
pub fn truncate(&mut self, len: usize) {
|
|
if len >= self.len() {
|
|
return;
|
|
}
|
|
|
|
self.0 = core::mem::take(&mut self.0)
|
|
.split_at_mut(len)
|
|
.0;
|
|
}
|
|
|
|
pub(crate) fn read(r: &mut ReaderMut<'a>) -> Self {
|
|
Self(r.rest())
|
|
}
|
|
|
|
pub(crate) fn into_inner(self) -> &'a mut [u8] {
|
|
self.0
|
|
}
|
|
|
|
pub(crate) fn pop(&mut self) -> Option<u8> {
|
|
if self.is_empty() {
|
|
return None;
|
|
}
|
|
|
|
let len = self.len();
|
|
let last = self[len - 1];
|
|
self.truncate(len - 1);
|
|
Some(last)
|
|
}
|
|
}
|
|
|
|
impl<'a> Codec for CertificateDer<'a> {
|
|
fn encode(&self, bytes: &mut Vec<u8>) {
|
|
codec::u24(self.as_ref().len() as u32).encode(bytes);
|
|
bytes.extend(self.as_ref());
|
|
}
|
|
|
|
fn read(r: &mut Reader) -> Result<Self, InvalidMessage> {
|
|
let len = codec::u24::read(r)?.0 as usize;
|
|
let mut sub = r.sub(len)?;
|
|
let body = sub.rest().to_vec();
|
|
Ok(Self::from(body))
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Payload {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
hex(f, &self.0)
|
|
}
|
|
}
|
|
|
|
/// An arbitrary, unknown-content, u24-length-prefixed payload
|
|
#[derive(Clone, Eq, PartialEq)]
|
|
pub(crate) struct PayloadU24(pub(crate) Vec<u8>);
|
|
|
|
impl PayloadU24 {
|
|
pub(crate) fn new(bytes: Vec<u8>) -> Self {
|
|
Self(bytes)
|
|
}
|
|
}
|
|
|
|
impl Codec for PayloadU24 {
|
|
fn encode(&self, bytes: &mut Vec<u8>) {
|
|
codec::u24(self.0.len() as u32).encode(bytes);
|
|
bytes.extend_from_slice(&self.0);
|
|
}
|
|
|
|
fn read(r: &mut Reader) -> Result<Self, InvalidMessage> {
|
|
let len = codec::u24::read(r)?.0 as usize;
|
|
let mut sub = r.sub(len)?;
|
|
let body = sub.rest().to_vec();
|
|
Ok(Self(body))
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for PayloadU24 {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
hex(f, &self.0)
|
|
}
|
|
}
|
|
|
|
/// An arbitrary, unknown-content, u16-length-prefixed payload
|
|
#[derive(Clone, Eq, PartialEq)]
|
|
pub struct PayloadU16(pub Vec<u8>);
|
|
|
|
impl PayloadU16 {
|
|
pub fn new(bytes: Vec<u8>) -> Self {
|
|
Self(bytes)
|
|
}
|
|
|
|
pub fn empty() -> Self {
|
|
Self::new(Vec::new())
|
|
}
|
|
|
|
pub fn encode_slice(slice: &[u8], bytes: &mut Vec<u8>) {
|
|
(slice.len() as u16).encode(bytes);
|
|
bytes.extend_from_slice(slice);
|
|
}
|
|
}
|
|
|
|
impl Codec for PayloadU16 {
|
|
fn encode(&self, bytes: &mut Vec<u8>) {
|
|
Self::encode_slice(&self.0, bytes);
|
|
}
|
|
|
|
fn read(r: &mut Reader) -> Result<Self, InvalidMessage> {
|
|
let len = u16::read(r)? as usize;
|
|
let mut sub = r.sub(len)?;
|
|
let body = sub.rest().to_vec();
|
|
Ok(Self(body))
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for PayloadU16 {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
hex(f, &self.0)
|
|
}
|
|
}
|
|
|
|
/// An arbitrary, unknown-content, u8-length-prefixed payload
|
|
#[derive(Clone, Eq, PartialEq)]
|
|
pub struct PayloadU8(pub(crate) Vec<u8>);
|
|
|
|
impl PayloadU8 {
|
|
pub(crate) fn encode_slice(slice: &[u8], bytes: &mut Vec<u8>) {
|
|
(slice.len() as u8).encode(bytes);
|
|
bytes.extend_from_slice(slice);
|
|
}
|
|
|
|
pub(crate) fn new(bytes: Vec<u8>) -> Self {
|
|
Self(bytes)
|
|
}
|
|
|
|
pub(crate) fn empty() -> Self {
|
|
Self(Vec::new())
|
|
}
|
|
}
|
|
|
|
impl Codec for PayloadU8 {
|
|
fn encode(&self, bytes: &mut Vec<u8>) {
|
|
(self.0.len() as u8).encode(bytes);
|
|
bytes.extend_from_slice(&self.0);
|
|
}
|
|
|
|
fn read(r: &mut Reader) -> Result<Self, InvalidMessage> {
|
|
let len = u8::read(r)? as usize;
|
|
let mut sub = r.sub(len)?;
|
|
let body = sub.rest().to_vec();
|
|
Ok(Self(body))
|
|
}
|
|
}
|
|
|
|
impl Zeroize for PayloadU8 {
|
|
fn zeroize(&mut self) {
|
|
self.0.zeroize();
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for PayloadU8 {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
hex(f, &self.0)
|
|
}
|
|
}
|
|
|
|
// Format an iterator of u8 into a hex string
|
|
pub(super) fn hex<'a>(
|
|
f: &mut fmt::Formatter<'_>,
|
|
payload: impl IntoIterator<Item = &'a u8>,
|
|
) -> fmt::Result {
|
|
for b in payload {
|
|
write!(f, "{:02x}", b)?;
|
|
}
|
|
Ok(())
|
|
}
|