Skip to content

Commit 8fb7e3f

Browse files
committed
spki: ensure params are encoded with Any
1 parent 0a5071d commit 8fb7e3f

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

spki/src/algorithm.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{Error, Result};
44
use core::cmp::Ordering;
55
use der::{
6-
asn1::{AnyRef, Choice, ObjectIdentifier},
6+
asn1::{AnyLike, AnyRef, Choice, ObjectIdentifier},
77
Decode, DecodeValue, DerOrd, Encode, EncodeValue, Header, Length, Reader, Sequence, ValueOrd,
88
Writer,
99
};
@@ -22,7 +22,7 @@ use der::asn1::Any;
2222
/// [RFC 5280 Section 4.1.1.2]: https://tools.ietf.org/html/rfc5280#section-4.1.1.2
2323
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
2424
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
25-
pub struct AlgorithmIdentifier<Params> {
25+
pub struct AlgorithmIdentifier<Params: AnyLike> {
2626
/// Algorithm OID, i.e. the `algorithm` field in the `AlgorithmIdentifier`
2727
/// ASN.1 schema.
2828
pub oid: ObjectIdentifier,
@@ -31,7 +31,7 @@ pub struct AlgorithmIdentifier<Params> {
3131
pub parameters: Option<Params>,
3232
}
3333

34-
impl<'a, Params> DecodeValue<'a> for AlgorithmIdentifier<Params>
34+
impl<'a, Params: AnyLike> DecodeValue<'a> for AlgorithmIdentifier<Params>
3535
where
3636
Params: Choice<'a, Error = der::Error>,
3737
{
@@ -47,7 +47,7 @@ where
4747
}
4848
}
4949

50-
impl<Params> EncodeValue for AlgorithmIdentifier<Params>
50+
impl<Params: AnyLike> EncodeValue for AlgorithmIdentifier<Params>
5151
where
5252
Params: Encode,
5353
{
@@ -62,12 +62,12 @@ where
6262
}
6363
}
6464

65-
impl<'a, Params> Sequence<'a> for AlgorithmIdentifier<Params> where
65+
impl<'a, Params: AnyLike> Sequence<'a> for AlgorithmIdentifier<Params> where
6666
Params: Choice<'a, Error = der::Error> + Encode
6767
{
6868
}
6969

70-
impl<'a, Params> TryFrom<&'a [u8]> for AlgorithmIdentifier<Params>
70+
impl<'a, Params: AnyLike> TryFrom<&'a [u8]> for AlgorithmIdentifier<Params>
7171
where
7272
Params: Choice<'a, Error = der::Error> + Encode,
7373
{
@@ -78,7 +78,7 @@ where
7878
}
7979
}
8080

81-
impl<Params> ValueOrd for AlgorithmIdentifier<Params>
81+
impl<Params: AnyLike> ValueOrd for AlgorithmIdentifier<Params>
8282
where
8383
Params: DerOrd,
8484
{
@@ -100,7 +100,7 @@ pub type AlgorithmIdentifierWithOid = AlgorithmIdentifier<ObjectIdentifier>;
100100
#[cfg(feature = "alloc")]
101101
pub type AlgorithmIdentifierOwned = AlgorithmIdentifier<Any>;
102102

103-
impl<Params> AlgorithmIdentifier<Params> {
103+
impl<Params: AnyLike> AlgorithmIdentifier<Params> {
104104
/// Assert the `algorithm` OID is an expected value.
105105
pub fn assert_algorithm_oid(&self, expected_oid: ObjectIdentifier) -> Result<ObjectIdentifier> {
106106
if self.oid == expected_oid {

spki/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
//! Borrow the [`ObjectIdentifier`] first then use [`der::AnyRef::from`] or `.into()`:
2222
//!
2323
//! ```
24+
//! use der::asn1::Any;
2425
//! use spki::{AlgorithmIdentifier, ObjectIdentifier};
2526
//!
2627
//! let alg_oid = "1.2.840.10045.2.1".parse::<ObjectIdentifier>().unwrap();
2728
//! let params_oid = "1.2.840.10045.3.1.7".parse::<ObjectIdentifier>().unwrap();
2829
//!
2930
//! let alg_id = AlgorithmIdentifier {
3031
//! oid: alg_oid,
31-
//! parameters: Some(params_oid)
32+
//! parameters: Some(Any::encode_from(&params_oid).unwrap())
3233
//! };
3334
//! ```
3435

spki/src/spki.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{AlgorithmIdentifier, Error, Result};
44
use core::cmp::Ordering;
55
use der::{
6-
asn1::{AnyRef, BitStringRef},
6+
asn1::{AnyLike, AnyRef, BitStringRef},
77
Choice, Decode, DecodeValue, DerOrd, Encode, EncodeValue, FixedTag, Header, Length, Reader,
88
Sequence, ValueOrd, Writer,
99
};
@@ -41,7 +41,7 @@ pub type SubjectPublicKeyInfoOwned = SubjectPublicKeyInfo<Any, BitString>;
4141
/// [RFC 5280 § 4.1.2.7]: https://tools.ietf.org/html/rfc5280#section-4.1.2.7
4242
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
4343
#[derive(Clone, Debug, Eq, PartialEq)]
44-
pub struct SubjectPublicKeyInfo<Params, Key> {
44+
pub struct SubjectPublicKeyInfo<Params: AnyLike, Key> {
4545
/// X.509 [`AlgorithmIdentifier`] for the public key type
4646
pub algorithm: AlgorithmIdentifier<Params>,
4747

@@ -51,7 +51,7 @@ pub struct SubjectPublicKeyInfo<Params, Key> {
5151

5252
impl<'a, Params, Key> SubjectPublicKeyInfo<Params, Key>
5353
where
54-
Params: Choice<'a, Error = der::Error> + Encode,
54+
Params: AnyLike + Choice<'a, Error = der::Error> + Encode,
5555
// TODO: replace FixedTag with FixedTag<TAG = { Tag::BitString }> once
5656
// https://github.com/rust-lang/rust/issues/92827 is fixed
5757
Key: Decode<'a, Error = der::Error> + Encode + FixedTag,
@@ -84,7 +84,7 @@ where
8484

8585
impl<'a: 'k, 'k, Params, Key: 'k> DecodeValue<'a> for SubjectPublicKeyInfo<Params, Key>
8686
where
87-
Params: Choice<'a, Error = der::Error> + Encode,
87+
Params: AnyLike + Choice<'a, Error = der::Error> + Encode,
8888
Key: Decode<'a, Error = der::Error>,
8989
{
9090
type Error = der::Error;
@@ -101,7 +101,7 @@ where
101101

102102
impl<'a, Params, Key> EncodeValue for SubjectPublicKeyInfo<Params, Key>
103103
where
104-
Params: Choice<'a, Error = der::Error> + Encode,
104+
Params: AnyLike + Choice<'a, Error = der::Error> + Encode,
105105
Key: Encode,
106106
{
107107
fn value_len(&self) -> der::Result<Length> {
@@ -117,14 +117,14 @@ where
117117

118118
impl<'a, Params, Key> Sequence<'a> for SubjectPublicKeyInfo<Params, Key>
119119
where
120-
Params: Choice<'a, Error = der::Error> + Encode,
120+
Params: AnyLike + Choice<'a, Error = der::Error> + Encode,
121121
Key: Decode<'a, Error = der::Error> + Encode + FixedTag,
122122
{
123123
}
124124

125125
impl<'a, Params, Key> TryFrom<&'a [u8]> for SubjectPublicKeyInfo<Params, Key>
126126
where
127-
Params: Choice<'a, Error = der::Error> + Encode,
127+
Params: AnyLike + Choice<'a, Error = der::Error> + Encode,
128128
Key: Decode<'a, Error = der::Error> + Encode + FixedTag,
129129
{
130130
type Error = Error;
@@ -136,7 +136,7 @@ where
136136

137137
impl<'a, Params, Key> ValueOrd for SubjectPublicKeyInfo<Params, Key>
138138
where
139-
Params: Choice<'a, Error = der::Error> + DerOrd + Encode,
139+
Params: AnyLike + Choice<'a, Error = der::Error> + DerOrd + Encode,
140140
Key: ValueOrd,
141141
{
142142
fn value_cmp(&self, other: &Self) -> der::Result<Ordering> {
@@ -150,7 +150,7 @@ where
150150
#[cfg(feature = "alloc")]
151151
impl<'a: 'k, 'k, Params, Key: 'k> TryFrom<SubjectPublicKeyInfo<Params, Key>> for Document
152152
where
153-
Params: Choice<'a, Error = der::Error> + Encode,
153+
Params: AnyLike + Choice<'a, Error = der::Error> + Encode,
154154
Key: Decode<'a, Error = der::Error> + Encode + FixedTag,
155155
BitStringRef<'a>: From<&'k Key>,
156156
{
@@ -164,7 +164,7 @@ where
164164
#[cfg(feature = "alloc")]
165165
impl<'a: 'k, 'k, Params, Key: 'k> TryFrom<&SubjectPublicKeyInfo<Params, Key>> for Document
166166
where
167-
Params: Choice<'a, Error = der::Error> + Encode,
167+
Params: AnyLike + Choice<'a, Error = der::Error> + Encode,
168168
Key: Decode<'a, Error = der::Error> + Encode + FixedTag,
169169
BitStringRef<'a>: From<&'k Key>,
170170
{
@@ -176,7 +176,10 @@ where
176176
}
177177

178178
#[cfg(feature = "pem")]
179-
impl<Params, Key> PemLabel for SubjectPublicKeyInfo<Params, Key> {
179+
impl<Params, Key> PemLabel for SubjectPublicKeyInfo<Params, Key>
180+
where
181+
Params: AnyLike,
182+
{
180183
const PEM_LABEL: &'static str = "PUBLIC KEY";
181184
}
182185

spki/src/traits.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Traits for encoding/decoding SPKI public keys.
22
33
use crate::{AlgorithmIdentifier, Error, Result, SubjectPublicKeyInfoRef};
4-
use der::{EncodeValue, Tagged};
4+
use der::{asn1::AnyLike, EncodeValue, Tagged};
55

66
#[cfg(feature = "alloc")]
77
use {
@@ -103,7 +103,7 @@ pub trait EncodePublicKey {
103103
/// This is useful for e.g. keys for digital signature algorithms.
104104
pub trait AssociatedAlgorithmIdentifier {
105105
/// Algorithm parameters.
106-
type Params: Tagged + EncodeValue;
106+
type Params: Tagged + EncodeValue + AnyLike;
107107

108108
/// `AlgorithmIdentifier` for this structure.
109109
const ALGORITHM_IDENTIFIER: AlgorithmIdentifier<Self::Params>;
@@ -141,7 +141,7 @@ where
141141
/// private keys.
142142
pub trait SignatureAlgorithmIdentifier {
143143
/// Algorithm parameters.
144-
type Params: Tagged + EncodeValue;
144+
type Params: Tagged + EncodeValue + AnyLike;
145145

146146
/// `AlgorithmIdentifier` for the corresponding singature system.
147147
const SIGNATURE_ALGORITHM_IDENTIFIER: AlgorithmIdentifier<Self::Params>;

spki/tests/spki.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! `SubjectPublicKeyInfo` tests.
22
3-
use der::asn1::ObjectIdentifier;
3+
use der::asn1::{AnyLike, ObjectIdentifier};
44
use hex_literal::hex;
55
use spki::SubjectPublicKeyInfoRef;
66

0 commit comments

Comments
 (0)