Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e183b64
der: s/StrSlice/StrRef/
baloo Dec 18, 2022
f79c847
der: s/ByteSlice/BytesRef/
baloo Dec 18, 2022
5f53908
der: s/Bytes/BytesOwned/
baloo Dec 18, 2022
4630f91
x509-cert: Make extensions owned
baloo Nov 28, 2022
d7ad56c
x509-cert: make OtherName an owned type
baloo Nov 28, 2022
4dadf4b
der: adds a StrOwned type owning the data
baloo Dec 18, 2022
9e2a1a3
der: export common string implementation in a macro
baloo Dec 18, 2022
c3e5ba7
der: adds a `Ia5String` owned equivalent to `IaStringRef`
baloo Dec 30, 2022
ffc0c4c
der: implement Arbitrary for OctetString
baloo Dec 30, 2022
e2881f8
x509-cert: use Ia5String and use owned types
baloo Nov 28, 2022
5b28139
x509-cert: Use owned String as backend for Utf8String
baloo Nov 28, 2022
7dd4292
der: provide `TeletexString` as owned alternative to `TeletexStringRef`
baloo Nov 28, 2022
e1bc8d1
der: provide `PrintableString` as owned alternative to `PrintableStri…
baloo Nov 28, 2022
573bca2
x509-cert: make `DirectoryString` and `EdiPartyName` owned types
baloo Nov 28, 2022
a113ec1
x509-cert: make `SubjectKeyIdentifier` an owned type
baloo Nov 28, 2022
8d7389e
x509-cert: make `GeneralName` an owned type
baloo Nov 28, 2022
2b18e99
x509-cert: make `DisplayText` an owned type
baloo Nov 28, 2022
0385432
x509-cert: make `PolicyQualifierInfo` an owned type
baloo Nov 28, 2022
b2ed2a4
x509-cert: use `BitString` for various fields
baloo Nov 28, 2022
777d73a
x509-cert: Crl are now owned types
baloo Nov 28, 2022
0eed9ae
x509-cert: AKI is now an owned type
baloo Nov 28, 2022
29b04fb
x509-cert: NoticeReference is now an owned type
baloo Nov 28, 2022
c05e4ba
x509-cert: CertReq is now using an owned signature
baloo Nov 28, 2022
20be9c5
x509-cert: TrustAnchorInfo is now an owned type
baloo Nov 28, 2022
66b419f
x509-cert: make Certificate an owned type
baloo Dec 18, 2022
052c9e5
x509-cert: missing features from spki -> x509-cert
baloo Dec 18, 2022
2109a1b
pkcs7: follow new api
baloo Dec 30, 2022
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
8 changes: 6 additions & 2 deletions der/src/asn1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Module containing all of the various ASN.1 built-in types supported by
//! this library.

#[macro_use]
mod internal_macros;

mod any;
mod bit_string;
mod boolean;
Expand Down Expand Up @@ -32,8 +35,9 @@ pub use const_oid::ObjectIdentifier;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use self::{
any::Any, bit_string::BitString, integer::bigint::Int, integer::bigint::Uint,
octet_string::OctetString, set_of::SetOfVec,
any::Any, bit_string::BitString, ia5_string::Ia5String, integer::bigint::Int,
integer::bigint::Uint, octet_string::OctetString, printable_string::PrintableString,
set_of::SetOfVec, teletex_string::TeletexString,
};
pub use self::{
any::AnyRef,
Expand Down
35 changes: 21 additions & 14 deletions der/src/asn1/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#![cfg_attr(feature = "arbitrary", allow(clippy::integer_arithmetic))]

use crate::{
asn1::*, ByteSlice, Choice, Decode, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind,
asn1::*, BytesRef, Choice, Decode, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind,
FixedTag, Header, Length, Reader, Result, SliceReader, Tag, Tagged, ValueOrd, Writer,
};
use core::cmp::Ordering;

#[cfg(feature = "alloc")]
use crate::Bytes;
use crate::BytesOwned;

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

/// Inner value encoded as bytes.
value: ByteSlice<'a>,
value: BytesRef<'a>,
}

impl<'a> AnyRef<'a> {
/// [`AnyRef`] representation of the ASN.1 `NULL` type.
pub const NULL: Self = Self {
tag: Tag::Null,
value: ByteSlice::EMPTY,
value: BytesRef::EMPTY,
};

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

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

Expand Down Expand Up @@ -150,7 +150,7 @@ impl<'a> Decode<'a> for AnyRef<'a> {

Ok(Self {
tag: header.tag,
value: ByteSlice::decode_value(reader, header)?,
value: BytesRef::decode_value(reader, header)?,
})
}
}
Expand Down Expand Up @@ -184,8 +184,8 @@ impl ValueOrd for AnyRef<'_> {
}
}

impl<'a> From<AnyRef<'a>> for ByteSlice<'a> {
fn from(any: AnyRef<'a>) -> ByteSlice<'a> {
impl<'a> From<AnyRef<'a>> for BytesRef<'a> {
fn from(any: AnyRef<'a>) -> BytesRef<'a> {
any.value
}
}
Expand All @@ -211,14 +211,14 @@ pub struct Any {
tag: Tag,

/// Inner value encoded as bytes.
value: Bytes,
value: BytesOwned,
}

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

// Ensure the tag and value are a valid `AnyRef`.
AnyRef::new(tag, value.as_slice())?;
Expand Down Expand Up @@ -293,7 +293,7 @@ where
let anyref: AnyRef<'a> = input.into();
Self {
tag: anyref.tag(),
value: Bytes::from(anyref.value),
value: BytesOwned::from(anyref.value),
}
}
}
Expand All @@ -308,7 +308,7 @@ mod allocating {
fn to_owned(&self) -> Self::Owned {
Any {
tag: self.tag(),
value: Bytes::from(self.value),
value: BytesOwned::from(self.value),
}
}
}
Expand All @@ -319,4 +319,11 @@ mod allocating {
self.into()
}
}

impl Any {
/// Is this value an ASN.1 `NULL` value?
pub fn is_null(&self) -> bool {
self.to_ref() == AnyRef::NULL
}
}
}
29 changes: 23 additions & 6 deletions der/src/asn1/bit_string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! ASN.1 `BIT STRING` support.

use crate::{
asn1::AnyRef, ByteSlice, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind, FixedTag, Header,
asn1::AnyRef, BytesRef, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind, FixedTag, Header,
Length, Reader, Result, Tag, ValueOrd, Writer,
};
use core::{cmp::Ordering, iter::FusedIterator};
Expand All @@ -24,7 +24,7 @@ pub struct BitStringRef<'a> {
bit_length: usize,

/// Bitstring represented as a slice of bytes.
inner: ByteSlice<'a>,
inner: BytesRef<'a>,
}

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

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

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

let unused_bits = reader.read_byte()?;
let inner = ByteSlice::decode_value(reader, header)?;
let inner = BytesRef::decode_value(reader, header)?;
Self::new(unused_bits, inner.as_slice())
}
}
Expand Down Expand Up @@ -205,13 +205,13 @@ impl<'a> arbitrary::Arbitrary<'a> for BitStringRef<'a> {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Self::new(
u.int_in_range(0..=Self::MAX_UNUSED_BITS)?,
ByteSlice::arbitrary(u)?.as_slice(),
BytesRef::arbitrary(u)?.as_slice(),
)
.map_err(|_| arbitrary::Error::IncorrectFormat)
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and(u8::size_hint(depth), ByteSlice::size_hint(depth))
arbitrary::size_hint::and(u8::size_hint(depth), BytesRef::size_hint(depth))
}
}

Expand Down Expand Up @@ -354,6 +354,23 @@ impl ValueOrd for BitString {
}
}

// Implement by hand because the derive would create invalid values.
// Use the constructor to create a valid value.
#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for BitString {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Self::new(
u.int_in_range(0..=Self::MAX_UNUSED_BITS)?,
BytesRef::arbitrary(u)?.as_slice(),
)
.map_err(|_| arbitrary::Error::IncorrectFormat)
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and(u8::size_hint(depth), BytesRef::size_hint(depth))
}
}

#[cfg(feature = "alloc")]
mod allocating {
use super::*;
Expand Down
Loading