Skip to content

Commit 8191fb6

Browse files
committed
pkcs8: use OctetString/BitString
Signed-off-by: Arthur Gautier <[email protected]>
1 parent d799384 commit 8191fb6

File tree

2 files changed

+93
-65
lines changed

2 files changed

+93
-65
lines changed

pkcs8/src/private_key_info.rs

+87-59
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use crate::{Error, Result, Version};
44
use core::fmt;
55
use der::{
66
asn1::{AnyRef, BitStringRef, ContextSpecific, OctetStringRef},
7-
Decode, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, TagMode, TagNumber,
8-
Writer,
7+
Decode, DecodeValue, Encode, EncodeValue, FixedTag, Header, Length, Reader, Sequence, TagMode,
8+
TagNumber, Writer,
99
};
1010
use spki::AlgorithmIdentifier;
1111

1212
#[cfg(feature = "alloc")]
13-
use {
14-
alloc::boxed::Box,
15-
der::{asn1::Any, SecretDocument},
13+
use der::{
14+
asn1::{Any, BitString, OctetString},
15+
SecretDocument,
1616
};
1717

1818
#[cfg(feature = "encryption")]
@@ -92,18 +92,18 @@ const PUBLIC_KEY_TAG: TagNumber = TagNumber::N1;
9292
/// [RFC 5208 Section 5]: https://tools.ietf.org/html/rfc5208#section-5
9393
/// [RFC 5958 Section 2]: https://datatracker.ietf.org/doc/html/rfc5958#section-2
9494
#[derive(Clone)]
95-
pub struct PrivateKeyInfoInner<Params, Key> {
95+
pub struct PrivateKeyInfoInner<Params, Key, PubKey> {
9696
/// X.509 `AlgorithmIdentifier` for the private key type.
9797
pub algorithm: AlgorithmIdentifier<Params>,
9898

9999
/// Private key data.
100100
pub private_key: Key,
101101

102102
/// Public key data, optionally available if version is V2.
103-
pub public_key: Option<Key>,
103+
pub public_key: Option<PubKey>,
104104
}
105105

106-
impl<Params, Key> PrivateKeyInfoInner<Params, Key> {
106+
impl<Params, Key, PubKey> PrivateKeyInfoInner<Params, Key, PubKey> {
107107
/// Create a new PKCS#8 [`PrivateKeyInfoInner`] message.
108108
///
109109
/// This is a helper method which initializes `attributes` and `public_key`
@@ -127,10 +127,14 @@ impl<Params, Key> PrivateKeyInfoInner<Params, Key> {
127127
}
128128
}
129129
}
130-
impl<'a, Params, Key> PrivateKeyInfoInner<Params, Key>
130+
131+
impl<'a, Params, Key, PubKey> PrivateKeyInfoInner<Params, Key, PubKey>
131132
where
132133
Params: der::Choice<'a, Error = der::Error> + Encode,
133-
Key: From<&'a [u8]> + AsRef<[u8]> + 'a,
134+
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
135+
Key: EncodeValue,
136+
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
137+
PubKey: BitStringLike,
134138
{
135139
/// Encrypt this private key using a symmetric encryption key derived
136140
/// from the provided password.
@@ -164,30 +168,29 @@ where
164168
}
165169
}
166170

167-
impl<'a, Params, Key> PrivateKeyInfoInner<Params, Key>
171+
impl<'a, Params, Key, PubKey> PrivateKeyInfoInner<Params, Key, PubKey>
168172
where
169173
Params: der::Choice<'a> + Encode,
170-
Key: AsRef<[u8]>,
174+
PubKey: BitStringLike,
171175
{
172176
/// Get a `BIT STRING` representation of the public key, if present.
173-
fn public_key_bit_string(&self) -> der::Result<Option<ContextSpecific<BitStringRef<'_>>>> {
174-
self.public_key
175-
.as_ref()
176-
.map(|pk| {
177-
BitStringRef::from_bytes(pk.as_ref()).map(|value| ContextSpecific {
178-
tag_number: PUBLIC_KEY_TAG,
179-
tag_mode: TagMode::Implicit,
180-
value,
181-
})
182-
})
183-
.transpose()
177+
fn public_key_bit_string(&self) -> Option<ContextSpecific<BitStringRef<'_>>> {
178+
self.public_key.as_ref().map(|pk| {
179+
let value = pk.as_bit_string();
180+
ContextSpecific {
181+
tag_number: PUBLIC_KEY_TAG,
182+
tag_mode: TagMode::Implicit,
183+
value,
184+
}
185+
})
184186
}
185187
}
186188

187-
impl<'a, Params, Key> DecodeValue<'a> for PrivateKeyInfoInner<Params, Key>
189+
impl<'a, Params, Key, PubKey> DecodeValue<'a> for PrivateKeyInfoInner<Params, Key, PubKey>
188190
where
189191
Params: der::Choice<'a, Error = der::Error> + Encode,
190-
Key: From<&'a [u8]>,
192+
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
193+
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
191194
{
192195
type Error = der::Error;
193196

@@ -196,16 +199,9 @@ where
196199
// Parse and validate `version` INTEGER.
197200
let version = Version::decode(reader)?;
198201
let algorithm = reader.decode()?;
199-
let private_key: &[u8] = OctetStringRef::decode(reader)?.into();
200-
let private_key = Key::try_from(private_key)?;
201-
let public_key = reader
202-
.context_specific::<BitStringRef<'_>>(PUBLIC_KEY_TAG, TagMode::Implicit)?
203-
.map(|bs| {
204-
bs.as_bytes()
205-
.ok_or_else(|| der::Tag::BitString.value_error())
206-
.map(Key::from)
207-
})
208-
.transpose()?;
202+
let private_key = Key::decode(reader)?;
203+
let public_key =
204+
reader.context_specific::<PubKey>(PUBLIC_KEY_TAG, TagMode::Implicit)?;
209205

210206
if version.has_public_key() != public_key.is_some() {
211207
return Err(reader.error(
@@ -232,38 +228,45 @@ where
232228
}
233229
}
234230

235-
impl<'a, Params, Key> EncodeValue for PrivateKeyInfoInner<Params, Key>
231+
impl<'a, Params, Key, PubKey> EncodeValue for PrivateKeyInfoInner<Params, Key, PubKey>
236232
where
237233
Params: der::Choice<'a, Error = der::Error> + Encode,
238-
Key: AsRef<[u8]>,
234+
Key: EncodeValue + FixedTag,
235+
PubKey: BitStringLike,
239236
{
240237
fn value_len(&self) -> der::Result<Length> {
241238
self.version().encoded_len()?
242239
+ self.algorithm.encoded_len()?
243-
+ OctetStringRef::new(self.private_key.as_ref())?.encoded_len()?
244-
+ self.public_key_bit_string()?.encoded_len()?
240+
+ self.private_key.encoded_len()?
241+
+ self.public_key_bit_string().encoded_len()?
245242
}
246243

247244
fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> {
248245
self.version().encode(writer)?;
249246
self.algorithm.encode(writer)?;
250-
OctetStringRef::new(self.private_key.as_ref())?.encode(writer)?;
251-
self.public_key_bit_string()?.encode(writer)?;
247+
self.private_key.encode(writer)?;
248+
self.public_key_bit_string().encode(writer)?;
252249
Ok(())
253250
}
254251
}
255252

256-
impl<'a, Params, Key> Sequence<'a> for PrivateKeyInfoInner<Params, Key>
253+
impl<'a, Params, Key, PubKey> Sequence<'a> for PrivateKeyInfoInner<Params, Key, PubKey>
257254
where
258255
Params: der::Choice<'a, Error = der::Error> + Encode,
259-
Key: From<&'a [u8]> + AsRef<[u8]>,
256+
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
257+
Key: EncodeValue,
258+
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
259+
PubKey: BitStringLike,
260260
{
261261
}
262262

263-
impl<'a, Params, Key> TryFrom<&'a [u8]> for PrivateKeyInfoInner<Params, Key>
263+
impl<'a, Params, Key, PubKey> TryFrom<&'a [u8]> for PrivateKeyInfoInner<Params, Key, PubKey>
264264
where
265265
Params: der::Choice<'a, Error = der::Error> + Encode,
266-
Key: From<&'a [u8]> + AsRef<[u8]> + 'a,
266+
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
267+
Key: EncodeValue,
268+
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
269+
PubKey: BitStringLike,
267270
{
268271
type Error = Error;
269272

@@ -272,10 +275,10 @@ where
272275
}
273276
}
274277

275-
impl<Params, Key> fmt::Debug for PrivateKeyInfoInner<Params, Key>
278+
impl<Params, Key, PubKey> fmt::Debug for PrivateKeyInfoInner<Params, Key, PubKey>
276279
where
277280
Params: fmt::Debug,
278-
Key: fmt::Debug,
281+
PubKey: fmt::Debug,
279282
{
280283
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
281284
f.debug_struct("PrivateKeyInfoInner")
@@ -287,41 +290,48 @@ where
287290
}
288291

289292
#[cfg(feature = "alloc")]
290-
impl<'a, Params, Key> TryFrom<PrivateKeyInfoInner<Params, Key>> for SecretDocument
293+
impl<'a, Params, Key, PubKey> TryFrom<PrivateKeyInfoInner<Params, Key, PubKey>> for SecretDocument
291294
where
292295
Params: der::Choice<'a, Error = der::Error> + Encode,
293-
Key: From<&'a [u8]> + AsRef<[u8]>,
296+
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
297+
Key: EncodeValue,
298+
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
299+
PubKey: BitStringLike,
294300
{
295301
type Error = Error;
296302

297-
fn try_from(private_key: PrivateKeyInfoInner<Params, Key>) -> Result<SecretDocument> {
303+
fn try_from(private_key: PrivateKeyInfoInner<Params, Key, PubKey>) -> Result<SecretDocument> {
298304
SecretDocument::try_from(&private_key)
299305
}
300306
}
301307

302308
#[cfg(feature = "alloc")]
303-
impl<'a, Params, Key> TryFrom<&PrivateKeyInfoInner<Params, Key>> for SecretDocument
309+
impl<'a, Params, Key, PubKey> TryFrom<&PrivateKeyInfoInner<Params, Key, PubKey>> for SecretDocument
304310
where
305311
Params: der::Choice<'a, Error = der::Error> + Encode,
306-
Key: From<&'a [u8]> + AsRef<[u8]>,
312+
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
313+
Key: EncodeValue,
314+
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
315+
PubKey: BitStringLike,
307316
{
308317
type Error = Error;
309318

310-
fn try_from(private_key: &PrivateKeyInfoInner<Params, Key>) -> Result<SecretDocument> {
319+
fn try_from(private_key: &PrivateKeyInfoInner<Params, Key, PubKey>) -> Result<SecretDocument> {
311320
Ok(Self::encode_msg(private_key)?)
312321
}
313322
}
314323

315324
#[cfg(feature = "pem")]
316-
impl<Params, Key> PemLabel for PrivateKeyInfoInner<Params, Key> {
325+
impl<Params, Key, PubKey> PemLabel for PrivateKeyInfoInner<Params, Key, PubKey> {
317326
const PEM_LABEL: &'static str = "PRIVATE KEY";
318327
}
319328

320329
#[cfg(feature = "subtle")]
321-
impl<Params, Key> ConstantTimeEq for PrivateKeyInfoInner<Params, Key>
330+
impl<Params, Key, PubKey> ConstantTimeEq for PrivateKeyInfoInner<Params, Key, PubKey>
322331
where
323332
Params: Eq,
324333
Key: PartialEq + AsRef<[u8]>,
334+
PubKey: PartialEq,
325335
{
326336
fn ct_eq(&self, other: &Self) -> Choice {
327337
// NOTE: public fields are not compared in constant time
@@ -334,36 +344,54 @@ where
334344
}
335345

336346
#[cfg(feature = "subtle")]
337-
impl<Params, Key> Eq for PrivateKeyInfoInner<Params, Key>
347+
impl<Params, Key, PubKey> Eq for PrivateKeyInfoInner<Params, Key, PubKey>
338348
where
339349
Params: Eq,
340350
Key: AsRef<[u8]> + Eq,
351+
PubKey: Eq,
341352
{
342353
}
343354

344355
#[cfg(feature = "subtle")]
345-
impl<Params, Key> PartialEq for PrivateKeyInfoInner<Params, Key>
356+
impl<Params, Key, PubKey> PartialEq for PrivateKeyInfoInner<Params, Key, PubKey>
346357
where
347358
Params: Eq,
348359
Key: PartialEq + AsRef<[u8]>,
360+
PubKey: PartialEq,
349361
{
350362
fn eq(&self, other: &Self) -> bool {
351363
self.ct_eq(other).into()
352364
}
353365
}
354366

355367
/// [`PrivateKeyInfoInner`] with [`AnyRef`] algorithm parameters, and `&[u8]` key.
356-
pub type PrivateKeyInfo<'a> = PrivateKeyInfoInner<AnyRef<'a>, &'a [u8]>;
368+
pub type PrivateKeyInfo<'a> = PrivateKeyInfoInner<AnyRef<'a>, OctetStringRef<'a>, BitStringRef<'a>>;
357369

358370
/// [`PrivateKeyInfo`] with [`Any`] algorithm parameters, and `Box<[u8]>` key.
359371
#[cfg(feature = "alloc")]
360-
pub type PrivateKeyInfoOwned = PrivateKeyInfoInner<Any, Box<[u8]>>;
372+
pub type PrivateKeyInfoOwned = PrivateKeyInfoInner<Any, OctetString, BitString>;
373+
374+
pub trait BitStringLike {
375+
fn as_bit_string(&self) -> BitStringRef<'_>;
376+
}
377+
378+
impl<'a> BitStringLike for BitStringRef<'a> {
379+
fn as_bit_string(&self) -> BitStringRef<'_> {
380+
BitStringRef::from(self)
381+
}
382+
}
361383

362384
#[cfg(feature = "alloc")]
363385
mod allocating {
364386
use super::*;
365387
use der::referenced::*;
366388

389+
impl BitStringLike for BitString {
390+
fn as_bit_string(&self) -> BitStringRef<'_> {
391+
BitStringRef::from(self)
392+
}
393+
}
394+
367395
impl<'a> RefToOwned<'a> for PrivateKeyInfo<'a> {
368396
type Owned = PrivateKeyInfoOwned;
369397
fn ref_to_owned(&self) -> Self::Owned {

pkcs8/tests/private_key.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! PKCS#8 private key tests
22
3-
use der::asn1::ObjectIdentifier;
3+
use der::asn1::{ObjectIdentifier, OctetStringRef};
44
use hex_literal::hex;
55
use pkcs8::{PrivateKeyInfo, Version};
66

@@ -90,7 +90,10 @@ fn decode_ec_bignp256_der() {
9090
// $ openssl asn1parse -inform der -in tests/examples/bign256-priv.der
9191
assert_eq!(
9292
pk.private_key,
93-
&hex!("1F66B5B84B7339674533F0329C74F21834281FED0732429E0C79235FC273E269")
93+
OctetStringRef::new(&hex!(
94+
"1F66B5B84B7339674533F0329C74F21834281FED0732429E0C79235FC273E269"
95+
))
96+
.unwrap()
9497
)
9598
}
9699

@@ -127,10 +130,7 @@ fn decode_ed25519_der_v2() {
127130
assert_eq!(pk.algorithm.oid, "1.3.101.112".parse().unwrap());
128131
assert_eq!(pk.algorithm.parameters, None);
129132
assert_eq!(pk.private_key.as_ref(), PRIV_KEY);
130-
assert_eq!(
131-
pk.public_key.as_ref().map(|p| p.as_ref()),
132-
Some(&PUB_KEY[..])
133-
);
133+
assert_eq!(pk.public_key.and_then(|p| p.as_bytes()), Some(&PUB_KEY[..]));
134134
}
135135

136136
#[test]

0 commit comments

Comments
 (0)