Skip to content

Commit eab0dab

Browse files
authored
x509-cert: make Certificate an owned type (#806)
As discussed in #803 let's make `Certificate` an owned type
1 parent 1c001b3 commit eab0dab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1037
-544
lines changed

der/src/asn1.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Module containing all of the various ASN.1 built-in types supported by
22
//! this library.
33
4+
#[macro_use]
5+
mod internal_macros;
6+
47
mod any;
58
mod bit_string;
69
mod boolean;
@@ -32,8 +35,9 @@ pub use const_oid::ObjectIdentifier;
3235
#[cfg(feature = "alloc")]
3336
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
3437
pub use self::{
35-
any::Any, bit_string::BitString, integer::bigint::Int, integer::bigint::Uint,
36-
octet_string::OctetString, set_of::SetOfVec,
38+
any::Any, bit_string::BitString, ia5_string::Ia5String, integer::bigint::Int,
39+
integer::bigint::Uint, octet_string::OctetString, printable_string::PrintableString,
40+
set_of::SetOfVec, teletex_string::TeletexString,
3741
};
3842
pub use self::{
3943
any::AnyRef,

der/src/asn1/any.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#![cfg_attr(feature = "arbitrary", allow(clippy::integer_arithmetic))]
33

44
use crate::{
5-
asn1::*, ByteSlice, Choice, Decode, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind,
5+
asn1::*, BytesRef, Choice, Decode, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind,
66
FixedTag, Header, Length, Reader, Result, SliceReader, Tag, Tagged, ValueOrd, Writer,
77
};
88
use core::cmp::Ordering;
99

1010
#[cfg(feature = "alloc")]
11-
use crate::Bytes;
11+
use crate::BytesOwned;
1212

1313
#[cfg(feature = "oid")]
1414
use crate::asn1::ObjectIdentifier;
@@ -31,24 +31,24 @@ pub struct AnyRef<'a> {
3131
tag: Tag,
3232

3333
/// Inner value encoded as bytes.
34-
value: ByteSlice<'a>,
34+
value: BytesRef<'a>,
3535
}
3636

3737
impl<'a> AnyRef<'a> {
3838
/// [`AnyRef`] representation of the ASN.1 `NULL` type.
3939
pub const NULL: Self = Self {
4040
tag: Tag::Null,
41-
value: ByteSlice::EMPTY,
41+
value: BytesRef::EMPTY,
4242
};
4343

4444
/// Create a new [`AnyRef`] from the provided [`Tag`] and DER bytes.
4545
pub fn new(tag: Tag, bytes: &'a [u8]) -> Result<Self> {
46-
let value = ByteSlice::new(bytes).map_err(|_| ErrorKind::Length { tag })?;
46+
let value = BytesRef::new(bytes).map_err(|_| ErrorKind::Length { tag })?;
4747
Ok(Self { tag, value })
4848
}
4949

50-
/// Infallible creation of an [`AnyRef`] from a [`ByteSlice`].
51-
pub(crate) fn from_tag_and_value(tag: Tag, value: ByteSlice<'a>) -> Self {
50+
/// Infallible creation of an [`AnyRef`] from a [`BytesRef`].
51+
pub(crate) fn from_tag_and_value(tag: Tag, value: BytesRef<'a>) -> Self {
5252
Self { tag, value }
5353
}
5454

@@ -150,7 +150,7 @@ impl<'a> Decode<'a> for AnyRef<'a> {
150150

151151
Ok(Self {
152152
tag: header.tag,
153-
value: ByteSlice::decode_value(reader, header)?,
153+
value: BytesRef::decode_value(reader, header)?,
154154
})
155155
}
156156
}
@@ -184,8 +184,8 @@ impl ValueOrd for AnyRef<'_> {
184184
}
185185
}
186186

187-
impl<'a> From<AnyRef<'a>> for ByteSlice<'a> {
188-
fn from(any: AnyRef<'a>) -> ByteSlice<'a> {
187+
impl<'a> From<AnyRef<'a>> for BytesRef<'a> {
188+
fn from(any: AnyRef<'a>) -> BytesRef<'a> {
189189
any.value
190190
}
191191
}
@@ -211,14 +211,14 @@ pub struct Any {
211211
tag: Tag,
212212

213213
/// Inner value encoded as bytes.
214-
value: Bytes,
214+
value: BytesOwned,
215215
}
216216

217217
#[cfg(feature = "alloc")]
218218
impl Any {
219219
/// Create a new [`Any`] from the provided [`Tag`] and DER bytes.
220220
pub fn new(tag: Tag, bytes: &[u8]) -> Result<Self> {
221-
let value = Bytes::new(bytes)?;
221+
let value = BytesOwned::new(bytes)?;
222222

223223
// Ensure the tag and value are a valid `AnyRef`.
224224
AnyRef::new(tag, value.as_slice())?;
@@ -293,7 +293,7 @@ where
293293
let anyref: AnyRef<'a> = input.into();
294294
Self {
295295
tag: anyref.tag(),
296-
value: Bytes::from(anyref.value),
296+
value: BytesOwned::from(anyref.value),
297297
}
298298
}
299299
}
@@ -308,7 +308,7 @@ mod allocating {
308308
fn to_owned(&self) -> Self::Owned {
309309
Any {
310310
tag: self.tag(),
311-
value: Bytes::from(self.value),
311+
value: BytesOwned::from(self.value),
312312
}
313313
}
314314
}
@@ -319,4 +319,11 @@ mod allocating {
319319
self.into()
320320
}
321321
}
322+
323+
impl Any {
324+
/// Is this value an ASN.1 `NULL` value?
325+
pub fn is_null(&self) -> bool {
326+
self.to_ref() == AnyRef::NULL
327+
}
328+
}
322329
}

der/src/asn1/bit_string.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! ASN.1 `BIT STRING` support.
22
33
use crate::{
4-
asn1::AnyRef, ByteSlice, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind, FixedTag, Header,
4+
asn1::AnyRef, BytesRef, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind, FixedTag, Header,
55
Length, Reader, Result, Tag, ValueOrd, Writer,
66
};
77
use core::{cmp::Ordering, iter::FusedIterator};
@@ -24,7 +24,7 @@ pub struct BitStringRef<'a> {
2424
bit_length: usize,
2525

2626
/// Bitstring represented as a slice of bytes.
27-
inner: ByteSlice<'a>,
27+
inner: BytesRef<'a>,
2828
}
2929

3030
impl<'a> BitStringRef<'a> {
@@ -40,7 +40,7 @@ impl<'a> BitStringRef<'a> {
4040
return Err(Self::TAG.value_error());
4141
}
4242

43-
let inner = ByteSlice::new(bytes).map_err(|_| Self::TAG.length_error())?;
43+
let inner = BytesRef::new(bytes).map_err(|_| Self::TAG.length_error())?;
4444

4545
let bit_length = usize::try_from(inner.len())?
4646
.checked_mul(8)
@@ -128,7 +128,7 @@ impl<'a> DecodeValue<'a> for BitStringRef<'a> {
128128
};
129129

130130
let unused_bits = reader.read_byte()?;
131-
let inner = ByteSlice::decode_value(reader, header)?;
131+
let inner = BytesRef::decode_value(reader, header)?;
132132
Self::new(unused_bits, inner.as_slice())
133133
}
134134
}
@@ -205,13 +205,13 @@ impl<'a> arbitrary::Arbitrary<'a> for BitStringRef<'a> {
205205
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
206206
Self::new(
207207
u.int_in_range(0..=Self::MAX_UNUSED_BITS)?,
208-
ByteSlice::arbitrary(u)?.as_slice(),
208+
BytesRef::arbitrary(u)?.as_slice(),
209209
)
210210
.map_err(|_| arbitrary::Error::IncorrectFormat)
211211
}
212212

213213
fn size_hint(depth: usize) -> (usize, Option<usize>) {
214-
arbitrary::size_hint::and(u8::size_hint(depth), ByteSlice::size_hint(depth))
214+
arbitrary::size_hint::and(u8::size_hint(depth), BytesRef::size_hint(depth))
215215
}
216216
}
217217

@@ -354,6 +354,23 @@ impl ValueOrd for BitString {
354354
}
355355
}
356356

357+
// Implement by hand because the derive would create invalid values.
358+
// Use the constructor to create a valid value.
359+
#[cfg(feature = "arbitrary")]
360+
impl<'a> arbitrary::Arbitrary<'a> for BitString {
361+
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
362+
Self::new(
363+
u.int_in_range(0..=Self::MAX_UNUSED_BITS)?,
364+
BytesRef::arbitrary(u)?.as_slice(),
365+
)
366+
.map_err(|_| arbitrary::Error::IncorrectFormat)
367+
}
368+
369+
fn size_hint(depth: usize) -> (usize, Option<usize>) {
370+
arbitrary::size_hint::and(u8::size_hint(depth), BytesRef::size_hint(depth))
371+
}
372+
}
373+
357374
#[cfg(feature = "alloc")]
358375
mod allocating {
359376
use super::*;

0 commit comments

Comments
 (0)