Skip to content

Commit 1efe27a

Browse files
committed
spki: make SubjectPublicKeyInfo own the public key
1 parent b09ff11 commit 1efe27a

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

pkcs1/src/traits.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use {
1616
};
1717

1818
#[cfg(feature = "pkcs8")]
19-
use crate::{ALGORITHM_ID, ALGORITHM_OID};
19+
use {
20+
crate::{ALGORITHM_ID, ALGORITHM_OID},
21+
der::asn1::BitString,
22+
};
2023

2124
#[cfg(feature = "std")]
2225
use std::path::Path;
@@ -185,7 +188,7 @@ impl<T: pkcs8::DecodePublicKey> DecodeRsaPublicKey for T {
185188
fn from_pkcs1_der(public_key: &[u8]) -> Result<Self> {
186189
Ok(Self::try_from(pkcs8::SubjectPublicKeyInfoRef {
187190
algorithm: ALGORITHM_ID,
188-
subject_public_key: public_key,
191+
subject_public_key: BitString::from_bytes(public_key)?,
189192
})?)
190193
}
191194
}
@@ -208,6 +211,6 @@ impl<T: pkcs8::EncodePublicKey> EncodeRsaPublicKey for T {
208211
let doc = self.to_public_key_der()?;
209212
let spki = pkcs8::SubjectPublicKeyInfoRef::from_der(doc.as_bytes())?;
210213
spki.algorithm.assert_algorithm_oid(ALGORITHM_OID)?;
211-
RsaPublicKey::from_der(spki.subject_public_key)?.try_into()
214+
RsaPublicKey::from_der(spki.subject_public_key.raw_bytes())?.try_into()
212215
}
213216
}

spki/src/spki.rs

+21-23
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::{AnyRef, BitString, BitStringRef},
77
Choice, Decode, DecodeValue, DerOrd, Encode, Header, Reader, Sequence, ValueOrd,
88
};
99

@@ -23,7 +23,7 @@ use {
2323
use der::pem::PemLabel;
2424

2525
/// [`SubjectPublicKeyInfo`] with [`AnyRef`] algorithm parameters.
26-
pub type SubjectPublicKeyInfoRef<'a> = SubjectPublicKeyInfo<'a, AnyRef<'a>>;
26+
pub type SubjectPublicKeyInfoRef<'a> = SubjectPublicKeyInfo<AnyRef<'a>>;
2727

2828
/// X.509 `SubjectPublicKeyInfo` (SPKI) as defined in [RFC 5280 § 4.1.2.7].
2929
///
@@ -37,23 +37,23 @@ pub type SubjectPublicKeyInfoRef<'a> = SubjectPublicKeyInfo<'a, AnyRef<'a>>;
3737
/// ```
3838
///
3939
/// [RFC 5280 § 4.1.2.7]: https://tools.ietf.org/html/rfc5280#section-4.1.2.7
40-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
41-
pub struct SubjectPublicKeyInfo<'a, Params> {
40+
#[derive(Clone, Debug, Eq, PartialEq)]
41+
pub struct SubjectPublicKeyInfo<Params> {
4242
/// X.509 [`AlgorithmIdentifier`] for the public key type
4343
pub algorithm: AlgorithmIdentifier<Params>,
4444

4545
/// Public key data
46-
pub subject_public_key: &'a [u8],
46+
pub subject_public_key: BitString,
4747
}
4848

49-
impl<'a, Params> SubjectPublicKeyInfo<'a, Params> {
49+
impl<Params> SubjectPublicKeyInfo<Params> {
5050
/// Get a [`BitString`] representing the `subject_public_key`
51-
fn bitstring(&self) -> der::Result<BitStringRef<'a>> {
52-
BitStringRef::from_bytes(self.subject_public_key)
51+
fn bitstring(&self) -> BitStringRef<'_> {
52+
BitStringRef::from(&self.subject_public_key)
5353
}
5454
}
5555

56-
impl<'a, Params> SubjectPublicKeyInfo<'a, Params>
56+
impl<'a, Params> SubjectPublicKeyInfo<Params>
5757
where
5858
Params: Choice<'a> + Encode,
5959
{
@@ -84,35 +84,33 @@ where
8484
}
8585
}
8686

87-
impl<'a, Params> DecodeValue<'a> for SubjectPublicKeyInfo<'a, Params>
87+
impl<'a, Params> DecodeValue<'a> for SubjectPublicKeyInfo<Params>
8888
where
8989
Params: Choice<'a> + Encode,
9090
{
9191
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> {
9292
reader.read_nested(header.length, |reader| {
9393
Ok(Self {
9494
algorithm: reader.decode()?,
95-
subject_public_key: BitStringRef::decode(reader)?
96-
.as_bytes()
97-
.ok_or_else(|| der::Tag::BitString.value_error())?,
95+
subject_public_key: BitString::decode(reader)?,
9896
})
9997
})
10098
}
10199
}
102100

103-
impl<'a, Params> Sequence<'a> for SubjectPublicKeyInfo<'a, Params>
101+
impl<'a, Params> Sequence<'a> for SubjectPublicKeyInfo<Params>
104102
where
105103
Params: Choice<'a> + Encode,
106104
{
107105
fn fields<F, T>(&self, f: F) -> der::Result<T>
108106
where
109107
F: FnOnce(&[&dyn Encode]) -> der::Result<T>,
110108
{
111-
f(&[&self.algorithm, &self.bitstring()?])
109+
f(&[&self.algorithm, &self.bitstring()])
112110
}
113111
}
114112

115-
impl<'a, Params> TryFrom<&'a [u8]> for SubjectPublicKeyInfo<'a, Params>
113+
impl<'a, Params> TryFrom<&'a [u8]> for SubjectPublicKeyInfo<Params>
116114
where
117115
Params: Choice<'a> + Encode,
118116
{
@@ -123,46 +121,46 @@ where
123121
}
124122
}
125123

126-
impl<'a, Params> ValueOrd for SubjectPublicKeyInfo<'a, Params>
124+
impl<'a, Params> ValueOrd for SubjectPublicKeyInfo<Params>
127125
where
128126
Params: Choice<'a> + DerOrd + Encode,
129127
{
130128
fn value_cmp(&self, other: &Self) -> der::Result<Ordering> {
131129
match self.algorithm.der_cmp(&other.algorithm)? {
132-
Ordering::Equal => self.bitstring()?.der_cmp(&other.bitstring()?),
130+
Ordering::Equal => self.bitstring().der_cmp(&other.bitstring()),
133131
other => Ok(other),
134132
}
135133
}
136134
}
137135

138136
#[cfg(feature = "alloc")]
139137
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
140-
impl<'a, Params> TryFrom<SubjectPublicKeyInfo<'a, Params>> for Document
138+
impl<'a, Params> TryFrom<SubjectPublicKeyInfo<Params>> for Document
141139
where
142140
Params: Choice<'a> + Encode,
143141
{
144142
type Error = Error;
145143

146-
fn try_from(spki: SubjectPublicKeyInfo<'a, Params>) -> Result<Document> {
144+
fn try_from(spki: SubjectPublicKeyInfo<Params>) -> Result<Document> {
147145
Self::try_from(&spki)
148146
}
149147
}
150148

151149
#[cfg(feature = "alloc")]
152150
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
153-
impl<'a, Params> TryFrom<&SubjectPublicKeyInfo<'a, Params>> for Document
151+
impl<'a, Params> TryFrom<&SubjectPublicKeyInfo<Params>> for Document
154152
where
155153
Params: Choice<'a> + Encode,
156154
{
157155
type Error = Error;
158156

159-
fn try_from(spki: &SubjectPublicKeyInfo<'a, Params>) -> Result<Document> {
157+
fn try_from(spki: &SubjectPublicKeyInfo<Params>) -> Result<Document> {
160158
Ok(Self::encode_msg(spki)?)
161159
}
162160
}
163161

164162
#[cfg(feature = "pem")]
165163
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
166-
impl<Params> PemLabel for SubjectPublicKeyInfo<'_, Params> {
164+
impl<Params> PemLabel for SubjectPublicKeyInfo<Params> {
167165
const PEM_LABEL: &'static str = "PUBLIC KEY";
168166
}

x509-cert/tests/certreq.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn decode_rsa_2048_der() {
5252
let alg = cr.info.public_key.algorithm;
5353
assert_eq!(alg.oid, "1.2.840.113549.1.1.1".parse().unwrap());
5454
assert!(alg.parameters.unwrap().is_null());
55-
assert_eq!(cr.info.public_key.subject_public_key, RSA_KEY);
55+
assert_eq!(cr.info.public_key.subject_public_key.raw_bytes(), RSA_KEY);
5656

5757
// Check the attributes (just one; contains extensions).
5858
assert_eq!(cr.info.attributes.len(), 1);

0 commit comments

Comments
 (0)