Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tls_codec/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#2322](https://github.com/RustCrypto/formats/pull/2322): Add `VLByteVec` and `SecretVLByteVec`, which are `#[serde(transparent)]` wrappers serializing via `serde_bytes`. They produce a much more compact representation in `serde` formats that distinguish byte arrays from sequences of `u8` (e.g. CBOR, MessagePack, bincode). Their `serde` output is not compatible with `VLBytes` / `SecretVLBytes`, but their `Deserialize` impls are backwards-compatible: in self-describing `serde` formats they also accept the legacy `VLBytes` / `SecretVLBytes` encoding (a struct with a `vec` field containing a sequence of `u8`). Deprecate `VLBytes` and `SecretVLBytes` in favour of `VLByteVec` and `SecretVLByteVec`.
- [#1656](https://github.com/RustCrypto/formats/pull/1656) Add `TlsVarInt` type for variable-length integers.

### Fixed
- [#2348](https://github.com/RustCrypto/formats/pull/2348) Use `write_all` everywhere instead of write to prevent partial writes from going undetected. The `Error::InvalidWriteLength` variant is deprecated as it is no longer returned.

## 0.4.2

- [#1628](https://github.com/RustCrypto/formats/pull/1628) Bump MSRV to 1.74
Expand Down
10 changes: 2 additions & 8 deletions tls_codec/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ impl<const LEN: usize> Serialize for [u8; LEN] {
#[cfg(feature = "std")]
#[inline]
fn tls_serialize<W: Write>(&self, writer: &mut W) -> Result<usize, Error> {
let written = writer.write(self)?;
if written == LEN {
Ok(written)
} else {
Err(Error::InvalidWriteLength(format!(
"Expected to write {LEN} bytes but only {written} were written."
)))
}
writer.write_all(self)?;
Ok(LEN)
}
}

Expand Down
3 changes: 3 additions & 0 deletions tls_codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ pub enum Error {
InvalidVectorLength,

/// Error writing everything out.
///
/// **Deprecated:** This error variant is not returned anymore and only kept to avoid breaking
/// existing code.
InvalidWriteLength(String),

/// Invalid input when trying to decode a primitive integer.
Expand Down
9 changes: 4 additions & 5 deletions tls_codec/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ impl<T: Serialize> Serialize for Option<T> {
fn tls_serialize<W: Write>(&self, writer: &mut W) -> Result<usize, Error> {
match self {
Some(e) => {
let written = writer.write(&[1])?;
debug_assert_eq!(written, 1);
writer.write_all(&[1])?;
e.tls_serialize(writer).map(|l| l + 1)
}
None => {
Expand Down Expand Up @@ -156,9 +155,9 @@ macro_rules! impl_unsigned {
#[cfg(feature = "std")]
#[inline]
fn tls_serialize<W: Write>(&self, writer: &mut W) -> Result<usize, Error> {
let written = writer.write(&self.to_be_bytes())?;
debug_assert_eq!(written, $bytes);
Ok(written)
let bytes = &self.to_be_bytes();
writer.write_all(bytes)?;
Ok($bytes)
}
}

Expand Down
4 changes: 3 additions & 1 deletion tls_codec/src/tls_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ macro_rules! impl_byte_serialize {
let mut written = <$size as Serialize>::tls_serialize(&<$size>::try_from(byte_length).unwrap(), writer)?;

// Now serialize the elements
written += writer.write($self.as_slice())?;
let bytes = $self.as_slice();
writer.write_all(bytes)?;
written += bytes.len();

$self.assert_written_bytes(tls_serialized_len, written)?;
Ok(written)
Expand Down
27 changes: 27 additions & 0 deletions tls_codec/tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,30 @@ fn test_matching_vl_bytes_serialization() {
SerializeBytes::tls_serialize(&byte_vec).expect("Error encoding byte vector")
);
}

struct SingleByteWriter<W>(W);

impl<W: std::io::Write> std::io::Write for SingleByteWriter<W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if buf.is_empty() {
return Ok(0);
}

self.0.write(&buf[..1])
}

fn flush(&mut self) -> std::io::Result<()> {
self.0.flush()
}
}

#[test]
fn serialize_into_short_writer() {
let mut short_writer = SingleByteWriter(vec![0u8; 32]);

let first = [0u8, 1, 2];

first.tls_serialize(&mut short_writer).unwrap();
Comment thread
franziskuskiefer marked this conversation as resolved.
assert_eq!(short_writer.0.len(), 35);
assert_eq!(&short_writer.0[short_writer.0.len() - 3..], &[0, 1, 2])
}