Skip to content

Commit 0783bf4

Browse files
committed
ed25519: pkcs8 API changes
1 parent b12d5d8 commit 0783bf4

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

ed25519/src/pkcs8.rs

+16-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

@@ -128,10 +131,14 @@ impl EncodePrivateKey for KeypairBytes {
128131
private_key[1] = 0x20;
129132
private_key[2..].copy_from_slice(&self.secret_key);
130133

131-
let private_key_info = PrivateKeyInfo {
134+
let private_key_info = PrivateKeyInfoRef {
132135
algorithm: ALGORITHM_ID,
133-
private_key: &private_key,
134-
public_key: self.public_key.as_ref().map(|pk| pk.0.as_slice()),
136+
private_key: OctetStringRef::new(&private_key)?,
137+
public_key: self
138+
.public_key
139+
.as_ref()
140+
.map(|pk| BitStringRef::new(0, &pk.0))
141+
.transpose()?,
135142
};
136143

137144
let result = SecretDocument::encode_msg(&private_key_info)?;
@@ -143,10 +150,10 @@ impl EncodePrivateKey for KeypairBytes {
143150
}
144151
}
145152

146-
impl TryFrom<PrivateKeyInfo<'_>> for KeypairBytes {
153+
impl TryFrom<PrivateKeyInfoRef<'_>> for KeypairBytes {
147154
type Error = Error;
148155

149-
fn try_from(private_key: PrivateKeyInfo<'_>) -> Result<Self> {
156+
fn try_from(private_key: PrivateKeyInfoRef<'_>) -> Result<Self> {
150157
private_key.algorithm.assert_algorithm_oid(ALGORITHM_OID)?;
151158

152159
if private_key.algorithm.parameters.is_some() {
@@ -161,13 +168,14 @@ impl TryFrom<PrivateKeyInfo<'_>> for KeypairBytes {
161168
//
162169
// - 0x04: OCTET STRING tag
163170
// - 0x20: 32-byte length
164-
let secret_key = match private_key.private_key {
171+
let secret_key = match private_key.private_key.as_bytes() {
165172
[0x04, 0x20, rest @ ..] => rest.try_into().map_err(|_| Error::KeyMalformed),
166173
_ => Err(Error::KeyMalformed),
167174
}?;
168175

169176
let public_key = private_key
170177
.public_key
178+
.and_then(|bs| bs.as_bytes())
171179
.map(|bytes| bytes.try_into().map_err(|_| Error::KeyMalformed))
172180
.transpose()?
173181
.map(PublicKeyBytes);

0 commit comments

Comments
 (0)