Skip to content

Commit

Permalink
spki: implement Hash for AlgorithmIdentifier (#1414)
Browse files Browse the repository at this point in the history
This allows to build an hashmap with various hashing algorithm when
verifying objects.
  • Loading branch information
baloo authored May 31, 2024
1 parent 56d3775 commit 0fc38ef
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions der/src/asn1/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::SliceWriter;
/// and useful concept which is still extensively used in things like
/// PKI-related RFCs.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct AnyRef<'a> {
/// Tag representing the type of the encoded value.
tag: Tag,
Expand Down Expand Up @@ -169,7 +169,7 @@ mod allocating {
/// This type provides the same functionality as [`AnyRef`] but owns the
/// backing data.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Any {
/// Tag representing the type of the encoded value.
tag: Tag,
Expand Down
2 changes: 1 addition & 1 deletion der/src/bytes_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloc::{boxed::Box, vec::Vec};
use core::cmp::Ordering;

/// Byte slice newtype which respects the `Length::max()` limit.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub(crate) struct BytesOwned {
/// Precomputed `Length` (avoids possible panicking conversions)
length: Length,
Expand Down
2 changes: 1 addition & 1 deletion der/src/bytes_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::cmp::Ordering;
use crate::StrOwned;

/// Byte slice newtype which respects the `Length::max()` limit.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub(crate) struct BytesRef<'a> {
/// Precomputed `Length` (avoids possible panicking conversions)
pub length: Length,
Expand Down
2 changes: 1 addition & 1 deletion der/src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const INDEFINITE_LENGTH_OCTET: u8 = 0b10000000; // 0x80
/// ASN.1-encoded length.
///
/// Maximum length is defined by the [`Length::MAX`] constant (256 MiB).
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Length(u32);

impl Length {
Expand Down
2 changes: 1 addition & 1 deletion der/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<T: FixedTag> Tagged for T {
/// - Bit 6: primitive (0) or constructed (1)
/// - Bits 5-1: tag number
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Tag {
/// `BOOLEAN` tag: `1`.
Expand Down
2 changes: 1 addition & 1 deletion der/src/tag/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use core::fmt;
/// encoded by using a leading tag number of 31 (`0b11111`). This library
/// deliberately does not support this: tag numbers greater than 30 are
/// disallowed.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct TagNumber(pub(super) u8);

impl TagNumber {
Expand Down
2 changes: 1 addition & 1 deletion spki/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use der::asn1::Any;
///
/// [RFC 5280 Section 4.1.1.2]: https://tools.ietf.org/html/rfc5280#section-4.1.1.2
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct AlgorithmIdentifier<Params> {
/// Algorithm OID, i.e. the `algorithm` field in the `AlgorithmIdentifier`
/// ASN.1 schema.
Expand Down
22 changes: 21 additions & 1 deletion spki/tests/spki.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use hex_literal::hex;
use spki::SubjectPublicKeyInfoRef;

#[cfg(feature = "alloc")]
use der::Encode;
use {
der::Encode,
spki::{AlgorithmIdentifier, AlgorithmIdentifierOwned},
};

#[cfg(feature = "pem")]
use der::{pem::LineEnding, EncodePem};
Expand Down Expand Up @@ -159,3 +162,20 @@ fn encode_rsa_2048_pem() {
let pk_encoded = pk.to_pem(LineEnding::LF).unwrap();
assert_eq!(RSA_2048_PEM_EXAMPLE, pk_encoded);
}

#[test]
#[cfg(feature = "alloc")]
fn build_hashset_of_digests() {
const SHA1: AlgorithmIdentifierOwned = AlgorithmIdentifier {
oid: ObjectIdentifier::new_unwrap("1.3.14.3.2.26"),
parameters: None,
};
const SHA256: AlgorithmIdentifierOwned = AlgorithmIdentifier {
oid: ObjectIdentifier::new_unwrap("2.16.840.1.101.3.4.2.1"),
parameters: None,
};

let mut hashes = std::collections::HashSet::new();
hashes.insert(SHA1);
hashes.insert(SHA256);
}

0 comments on commit 0fc38ef

Please sign in to comment.