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
11 changes: 5 additions & 6 deletions der/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{Header, Length, Result, SliceWriter, Tagged, Writer};
use core::marker::PhantomData;

#[cfg(feature = "alloc")]
use {alloc::boxed::Box, alloc::vec::Vec, core::iter};
use {alloc::boxed::Box, alloc::vec::Vec};

#[cfg(feature = "pem")]
use {
Expand Down Expand Up @@ -40,12 +40,11 @@ pub trait Encode {
#[cfg(feature = "alloc")]
fn encode_to_vec(&self, buf: &mut Vec<u8>) -> Result<Length> {
let expected_len = usize::try_from(self.encoded_len()?)?;
buf.reserve(expected_len);
buf.extend(iter::repeat(0).take(expected_len));
let initial_len = buf.len();
buf.resize(initial_len + expected_len, 0u8);

let mut writer = SliceWriter::new(buf);
self.encode(&mut writer)?;
let actual_len = writer.finish()?.len();
let buf_slice = &mut buf[initial_len..];
let actual_len = self.encode_to_slice(buf_slice)?.len();

if expected_len != actual_len {
return Err(ErrorKind::Incomplete {
Expand Down
24 changes: 23 additions & 1 deletion der/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ mod sequence {
// ```
//
// [RFC 5280 Section 5.2.5]: https://datatracker.ietf.org/doc/html/rfc5280#section-5.2.5
#[derive(Sequence)]
#[derive(Sequence, Default)]
pub struct IssuingDistributionPointExample {
// Omit distributionPoint and only_some_reasons because corresponding structs are not
// available here and are not germane to the example
Expand Down Expand Up @@ -601,6 +601,28 @@ mod sequence {
assert_eq!(idp.only_contains_attribute_certs, true);
}

#[test]
fn idp_encode_twice() {
let mut vec_buf = Vec::new();

IssuingDistributionPointExample {
only_contains_user_certs: true,
..Default::default()
}
.encode_to_vec(&mut vec_buf)
.unwrap();

// encode to the same vec by appending
IssuingDistributionPointExample {
only_contains_cacerts: true,
..Default::default()
}
.encode_to_vec(&mut vec_buf)
.unwrap();

assert_eq!(vec_buf, hex!("30038101FF 30038201FF"));
}

// demonstrates default field that is not context specific
#[test]
fn extension_test() {
Expand Down