Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b12d5d8

Browse files
committedSep 5, 2024
ed448: pkcs8 API changes
1 parent c739591 commit b12d5d8

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed
 

‎ed448/src/pkcs8.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
//! breaking changes when using this module.
1616
1717
pub use pkcs8::{
18-
spki, DecodePrivateKey, DecodePublicKey, Error, ObjectIdentifier, PrivateKeyInfo, Result,
18+
spki, DecodePrivateKey, DecodePublicKey, Error, ObjectIdentifier, PrivateKeyInfoRef, Result,
1919
};
2020

2121
#[cfg(feature = "alloc")]
2222
pub use pkcs8::{spki::EncodePublicKey, EncodePrivateKey};
2323

2424
#[cfg(feature = "alloc")]
25-
pub use pkcs8::der::{asn1::BitStringRef, Document, SecretDocument};
25+
pub use pkcs8::der::{
26+
asn1::{BitStringRef, OctetStringRef},
27+
Document, SecretDocument,
28+
};
2629

2730
use core::fmt;
2831

@@ -112,21 +115,26 @@ impl EncodePrivateKey for KeypairBytes {
112115
private_key[0] = 0x04;
113116
private_key[1] = 0x39;
114117
private_key[2..].copy_from_slice(&self.secret_key);
118+
let private_key = OctetStringRef::new(&private_key)?;
115119

116-
let private_key_info = PrivateKeyInfo {
120+
let private_key_info = PrivateKeyInfoRef {
117121
algorithm: ALGORITHM_ID,
118-
private_key: &private_key,
119-
public_key: self.public_key.as_ref().map(|pk| pk.0.as_slice()),
122+
private_key: private_key,
123+
public_key: self
124+
.public_key
125+
.as_ref()
126+
.map(|pk| BitStringRef::new(0, &pk.0))
127+
.transpose()?,
120128
};
121129

122130
Ok(SecretDocument::encode_msg(&private_key_info)?)
123131
}
124132
}
125133

126-
impl TryFrom<PrivateKeyInfo<'_>> for KeypairBytes {
134+
impl TryFrom<PrivateKeyInfoRef<'_>> for KeypairBytes {
127135
type Error = Error;
128136

129-
fn try_from(private_key: PrivateKeyInfo<'_>) -> Result<Self> {
137+
fn try_from(private_key: PrivateKeyInfoRef<'_>) -> Result<Self> {
130138
private_key.algorithm.assert_algorithm_oid(ALGORITHM_OID)?;
131139

132140
if private_key.algorithm.parameters.is_some() {
@@ -141,13 +149,14 @@ impl TryFrom<PrivateKeyInfo<'_>> for KeypairBytes {
141149
//
142150
// - 0x04: OCTET STRING tag
143151
// - 0x39: 57-byte length
144-
let secret_key = match private_key.private_key {
152+
let secret_key = match private_key.private_key.as_bytes() {
145153
[0x04, 0x39, rest @ ..] => rest.try_into().map_err(|_| Error::KeyMalformed),
146154
_ => Err(Error::KeyMalformed),
147155
}?;
148156

149157
let public_key = private_key
150158
.public_key
159+
.and_then(|bs| bs.as_bytes())
151160
.map(|bytes| bytes.try_into().map_err(|_| Error::KeyMalformed))
152161
.transpose()?
153162
.map(PublicKeyBytes);

0 commit comments

Comments
 (0)
Please sign in to comment.