Skip to content

Commit 4dd6f4a

Browse files
committed
pkcs8: provide PrivateKeyInfo type alias with older API
Signed-off-by: Arthur Gautier <[email protected]>
1 parent 07cc02b commit 4dd6f4a

File tree

6 files changed

+56
-59
lines changed

6 files changed

+56
-59
lines changed

pkcs8/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub(crate) mod encrypted_private_key_info;
8585

8686
pub use crate::{
8787
error::{Error, Result},
88-
private_key_info::{PrivateKeyInfo, PrivateKeyInfoRef},
88+
private_key_info::{PrivateKeyInfo, PrivateKeyInfoInner},
8989
traits::DecodePrivateKey,
9090
version::Version,
9191
};

pkcs8/src/private_key_info.rs

+26-29
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const PUBLIC_KEY_TAG: TagNumber = TagNumber::N1;
9494
/// [RFC 5208 Section 5]: https://tools.ietf.org/html/rfc5208#section-5
9595
/// [RFC 5958 Section 2]: https://datatracker.ietf.org/doc/html/rfc5958#section-2
9696
#[derive(Clone)]
97-
pub struct PrivateKeyInfo<Params, Key> {
97+
pub struct PrivateKeyInfoInner<Params, Key> {
9898
/// X.509 `AlgorithmIdentifier` for the private key type.
9999
pub algorithm: AlgorithmIdentifier<Params>,
100100

@@ -105,8 +105,8 @@ pub struct PrivateKeyInfo<Params, Key> {
105105
pub public_key: Option<Key>,
106106
}
107107

108-
impl<Params, Key> PrivateKeyInfo<Params, Key> {
109-
/// Create a new PKCS#8 [`PrivateKeyInfo`] message.
108+
impl<Params, Key> PrivateKeyInfoInner<Params, Key> {
109+
/// Create a new PKCS#8 [`PrivateKeyInfoInner`] message.
110110
///
111111
/// This is a helper method which initializes `attributes` and `public_key`
112112
/// to `None`, helpful if you aren't using those.
@@ -129,7 +129,7 @@ impl<Params, Key> PrivateKeyInfo<Params, Key> {
129129
}
130130
}
131131
}
132-
impl<'a, Params, Key> PrivateKeyInfo<Params, Key>
132+
impl<'a, Params, Key> PrivateKeyInfoInner<Params, Key>
133133
where
134134
Params: der::Choice<'a> + Encode,
135135
Key: From<&'a [u8]> + AsRef<[u8]>,
@@ -166,7 +166,7 @@ where
166166
}
167167
}
168168

169-
impl<'a, Params, Key> PrivateKeyInfo<Params, Key>
169+
impl<'a, Params, Key> PrivateKeyInfoInner<Params, Key>
170170
where
171171
Params: der::Choice<'a> + Encode,
172172
Key: AsRef<[u8]>,
@@ -186,15 +186,12 @@ where
186186
}
187187
}
188188

189-
impl<'a, Params, Key> DecodeValue<'a> for PrivateKeyInfo<Params, Key>
189+
impl<'a, Params, Key> DecodeValue<'a> for PrivateKeyInfoInner<Params, Key>
190190
where
191191
Params: der::Choice<'a> + Encode,
192192
Key: From<&'a [u8]>,
193193
{
194-
fn decode_value<R: Reader<'a>>(
195-
reader: &mut R,
196-
header: Header,
197-
) -> der::Result<PrivateKeyInfo<Params, Key>> {
194+
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> {
198195
reader.read_nested(header.length, |reader| {
199196
// Parse and validate `version` INTEGER.
200197
let version = Version::decode(reader)?;
@@ -235,7 +232,7 @@ where
235232
}
236233
}
237234

238-
impl<'a, Params, Key> EncodeValue for PrivateKeyInfo<Params, Key>
235+
impl<'a, Params, Key> EncodeValue for PrivateKeyInfoInner<Params, Key>
239236
where
240237
Params: der::Choice<'a> + Encode,
241238
Key: AsRef<[u8]>,
@@ -256,14 +253,14 @@ where
256253
}
257254
}
258255

259-
impl<'a, Params, Key> Sequence<'a> for PrivateKeyInfo<Params, Key>
256+
impl<'a, Params, Key> Sequence<'a> for PrivateKeyInfoInner<Params, Key>
260257
where
261258
Params: der::Choice<'a> + Encode,
262259
Key: From<&'a [u8]> + AsRef<[u8]>,
263260
{
264261
}
265262

266-
impl<'a, Params, Key> TryFrom<&'a [u8]> for PrivateKeyInfo<Params, Key>
263+
impl<'a, Params, Key> TryFrom<&'a [u8]> for PrivateKeyInfoInner<Params, Key>
267264
where
268265
Params: der::Choice<'a> + Encode,
269266
Key: From<&'a [u8]> + AsRef<[u8]>,
@@ -275,13 +272,13 @@ where
275272
}
276273
}
277274

278-
impl<Params, Key> fmt::Debug for PrivateKeyInfo<Params, Key>
275+
impl<Params, Key> fmt::Debug for PrivateKeyInfoInner<Params, Key>
279276
where
280277
Params: fmt::Debug,
281278
Key: fmt::Debug,
282279
{
283280
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
284-
f.debug_struct("PrivateKeyInfo")
281+
f.debug_struct("PrivateKeyInfoInner")
285282
.field("version", &self.version())
286283
.field("algorithm", &self.algorithm)
287284
.field("public_key", &self.public_key)
@@ -290,38 +287,38 @@ where
290287
}
291288

292289
#[cfg(feature = "alloc")]
293-
impl<'a, Params, Key> TryFrom<PrivateKeyInfo<Params, Key>> for SecretDocument
290+
impl<'a, Params, Key> TryFrom<PrivateKeyInfoInner<Params, Key>> for SecretDocument
294291
where
295292
Params: der::Choice<'a> + Encode,
296293
Key: From<&'a [u8]> + AsRef<[u8]>,
297294
{
298295
type Error = Error;
299296

300-
fn try_from(private_key: PrivateKeyInfo<Params, Key>) -> Result<SecretDocument> {
297+
fn try_from(private_key: PrivateKeyInfoInner<Params, Key>) -> Result<SecretDocument> {
301298
SecretDocument::try_from(&private_key)
302299
}
303300
}
304301

305302
#[cfg(feature = "alloc")]
306-
impl<'a, Params, Key> TryFrom<&PrivateKeyInfo<Params, Key>> for SecretDocument
303+
impl<'a, Params, Key> TryFrom<&PrivateKeyInfoInner<Params, Key>> for SecretDocument
307304
where
308305
Params: der::Choice<'a> + Encode,
309306
Key: From<&'a [u8]> + AsRef<[u8]>,
310307
{
311308
type Error = Error;
312309

313-
fn try_from(private_key: &PrivateKeyInfo<Params, Key>) -> Result<SecretDocument> {
310+
fn try_from(private_key: &PrivateKeyInfoInner<Params, Key>) -> Result<SecretDocument> {
314311
Ok(Self::encode_msg(private_key)?)
315312
}
316313
}
317314

318315
#[cfg(feature = "pem")]
319-
impl<Params, Key> PemLabel for PrivateKeyInfo<Params, Key> {
316+
impl<Params, Key> PemLabel for PrivateKeyInfoInner<Params, Key> {
320317
const PEM_LABEL: &'static str = "PRIVATE KEY";
321318
}
322319

323320
#[cfg(feature = "subtle")]
324-
impl<Params, Key> ConstantTimeEq for PrivateKeyInfo<Params, Key>
321+
impl<Params, Key> ConstantTimeEq for PrivateKeyInfoInner<Params, Key>
325322
where
326323
Params: Eq,
327324
Key: PartialEq + AsRef<[u8]>,
@@ -337,15 +334,15 @@ where
337334
}
338335

339336
#[cfg(feature = "subtle")]
340-
impl<Params, Key> Eq for PrivateKeyInfo<Params, Key>
337+
impl<Params, Key> Eq for PrivateKeyInfoInner<Params, Key>
341338
where
342339
Params: Eq,
343340
Key: AsRef<[u8]> + Eq,
344341
{
345342
}
346343

347344
#[cfg(feature = "subtle")]
348-
impl<Params, Key> PartialEq for PrivateKeyInfo<Params, Key>
345+
impl<Params, Key> PartialEq for PrivateKeyInfoInner<Params, Key>
349346
where
350347
Params: Eq,
351348
Key: PartialEq + AsRef<[u8]>,
@@ -355,19 +352,19 @@ where
355352
}
356353
}
357354

358-
/// [`PrivateKeyInfo`] with [`AnyRef`] algorithm parameters, and `&[u8]` key.
359-
pub type PrivateKeyInfoRef<'a> = PrivateKeyInfo<AnyRef<'a>, &'a [u8]>;
355+
/// [`PrivateKeyInfoInner`] with [`AnyRef`] algorithm parameters, and `&[u8]` key.
356+
pub type PrivateKeyInfo<'a> = PrivateKeyInfoInner<AnyRef<'a>, &'a [u8]>;
360357

361358
/// [`PrivateKeyInfo`] with [`Any`] algorithm parameters, and `Box<[u8]>` key.
362359
#[cfg(feature = "alloc")]
363-
pub type PrivateKeyInfoOwned = PrivateKeyInfo<Any, Box<[u8]>>;
360+
pub type PrivateKeyInfoOwned = PrivateKeyInfoInner<Any, Box<[u8]>>;
364361

365362
#[cfg(feature = "alloc")]
366363
mod allocating {
367364
use super::*;
368365
use der::referenced::*;
369366

370-
impl<'a> RefToOwned<'a> for PrivateKeyInfoRef<'a> {
367+
impl<'a> RefToOwned<'a> for PrivateKeyInfo<'a> {
371368
type Owned = PrivateKeyInfoOwned;
372369
fn ref_to_owned(&self) -> Self::Owned {
373370
PrivateKeyInfoOwned {
@@ -379,9 +376,9 @@ mod allocating {
379376
}
380377

381378
impl OwnedToRef for PrivateKeyInfoOwned {
382-
type Borrowed<'a> = PrivateKeyInfoRef<'a>;
379+
type Borrowed<'a> = PrivateKeyInfo<'a>;
383380
fn owned_to_ref(&self) -> Self::Borrowed<'_> {
384-
PrivateKeyInfoRef {
381+
PrivateKeyInfo {
385382
algorithm: self.algorithm.owned_to_ref(),
386383
private_key: self.private_key.owned_to_ref(),
387384
public_key: self.public_key.owned_to_ref(),

pkcs8/src/traits.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Traits for parsing objects from PKCS#8 encoded documents
22
3-
use crate::{Error, PrivateKeyInfoRef, Result};
3+
use crate::{Error, PrivateKeyInfo, Result};
44

55
#[cfg(feature = "alloc")]
66
use der::SecretDocument;
@@ -49,7 +49,7 @@ pub trait DecodePrivateKey: Sized {
4949
fn from_pkcs8_pem(s: &str) -> Result<Self> {
5050
// Validate PEM label
5151
let label = pem::decode_label(s.as_bytes())?;
52-
PrivateKeyInfoRef::validate_pem_label(label)?;
52+
PrivateKeyInfo::validate_pem_label(label)?;
5353

5454
let doc = SecretDocument::from_pem(s)?.1;
5555
Self::from_pkcs8_der(doc.as_bytes())
@@ -81,17 +81,17 @@ pub trait DecodePrivateKey: Sized {
8181
#[cfg(all(feature = "pem", feature = "std"))]
8282
fn read_pkcs8_pem_file(path: impl AsRef<Path>) -> Result<Self> {
8383
let (label, doc) = SecretDocument::read_pem_file(path)?;
84-
PrivateKeyInfoRef::validate_pem_label(&label)?;
84+
PrivateKeyInfo::validate_pem_label(&label)?;
8585
Self::from_pkcs8_der(doc.as_bytes())
8686
}
8787
}
8888

8989
impl<T> DecodePrivateKey for T
9090
where
91-
T: for<'a> TryFrom<PrivateKeyInfoRef<'a>, Error = Error>,
91+
T: for<'a> TryFrom<PrivateKeyInfo<'a>, Error = Error>,
9292
{
9393
fn from_pkcs8_der(bytes: &[u8]) -> Result<Self> {
94-
Self::try_from(PrivateKeyInfoRef::try_from(bytes)?)
94+
Self::try_from(PrivateKeyInfo::try_from(bytes)?)
9595
}
9696
}
9797

@@ -116,7 +116,7 @@ pub trait EncodePrivateKey {
116116
#[cfg(feature = "pem")]
117117
fn to_pkcs8_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> {
118118
let doc = self.to_pkcs8_der()?;
119-
Ok(doc.to_pem(PrivateKeyInfoRef::PEM_LABEL, line_ending)?)
119+
Ok(doc.to_pem(PrivateKeyInfo::PEM_LABEL, line_ending)?)
120120
}
121121

122122
/// Serialize this private key as an encrypted PEM-encoded PKCS#8 private
@@ -142,6 +142,6 @@ pub trait EncodePrivateKey {
142142
#[cfg(all(feature = "pem", feature = "std"))]
143143
fn write_pkcs8_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
144144
let doc = self.to_pkcs8_der()?;
145-
Ok(doc.write_pem_file(path, PrivateKeyInfoRef::PEM_LABEL, line_ending)?)
145+
Ok(doc.write_pem_file(path, PrivateKeyInfo::PEM_LABEL, line_ending)?)
146146
}
147147
}

pkcs8/tests/encrypted_private_key.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![cfg(feature = "pkcs5")]
44

55
use hex_literal::hex;
6-
use pkcs8::{pkcs5::pbes2, EncryptedPrivateKeyInfo, PrivateKeyInfoRef};
6+
use pkcs8::{pkcs5::pbes2, EncryptedPrivateKeyInfo, PrivateKeyInfo};
77

88
#[cfg(feature = "alloc")]
99
use der::Encode;
@@ -168,7 +168,7 @@ fn encrypt_ed25519_der_encpriv_aes256_pbkdf2_sha256() {
168168
)
169169
.unwrap();
170170

171-
let pk_plaintext = PrivateKeyInfoRef::try_from(ED25519_DER_PLAINTEXT_EXAMPLE).unwrap();
171+
let pk_plaintext = PrivateKeyInfo::try_from(ED25519_DER_PLAINTEXT_EXAMPLE).unwrap();
172172
let pk_encrypted = pk_plaintext
173173
.encrypt_with_params(pbes2_params, PASSWORD)
174174
.unwrap();
@@ -189,7 +189,7 @@ fn encrypt_ed25519_der_encpriv_aes256_scrypt() {
189189
)
190190
.unwrap();
191191

192-
let pk_plaintext = PrivateKeyInfoRef::try_from(ED25519_DER_PLAINTEXT_EXAMPLE).unwrap();
192+
let pk_plaintext = PrivateKeyInfo::try_from(ED25519_DER_PLAINTEXT_EXAMPLE).unwrap();
193193
let pk_encrypted = pk_plaintext
194194
.encrypt_with_params(scrypt_params, PASSWORD)
195195
.unwrap();

0 commit comments

Comments
 (0)