Skip to content

Commit

Permalink
elliptic-curve: pkcs8 API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed Aug 19, 2024
1 parent dff9996 commit df6f7ab
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions elliptic-curve/src/secret_key/pkcs8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
use super::SecretKey;
use crate::{
pkcs8::{der::Decode, AssociatedOid},
pkcs8::{
der::{asn1::OctetStringRef, Decode},
AssociatedOid,
},
sec1::{ModulusSize, ValidatePublicKey},
Curve, FieldBytesSize, ALGORITHM_OID,
};
Expand Down Expand Up @@ -39,19 +42,19 @@ where
};
}

impl<C> TryFrom<pkcs8::PrivateKeyInfo<'_>> for SecretKey<C>
impl<C> TryFrom<pkcs8::PrivateKeyInfoRef<'_>> for SecretKey<C>
where
C: AssociatedOid + Curve + ValidatePublicKey,
FieldBytesSize<C>: ModulusSize,
{
type Error = pkcs8::Error;

fn try_from(private_key_info: pkcs8::PrivateKeyInfo<'_>) -> pkcs8::Result<Self> {
fn try_from(private_key_info: pkcs8::PrivateKeyInfoRef<'_>) -> pkcs8::Result<Self> {
private_key_info
.algorithm
.assert_oids(ALGORITHM_OID, C::OID)?;

let ec_private_key = EcPrivateKey::from_der(private_key_info.private_key)?;
let ec_private_key = EcPrivateKey::from_der(private_key_info.private_key.as_bytes())?;
Ok(Self::try_from(ec_private_key)?)
}
}
Expand All @@ -64,14 +67,17 @@ where
FieldBytesSize<C>: ModulusSize,
{
fn to_pkcs8_der(&self) -> pkcs8::Result<der::SecretDocument> {
// TODO(tarcieri): make `PrivateKeyInfo` generic around `Params`
// TODO(tarcieri): make `PrivateKeyInfoRef` generic around `Params`
let algorithm_identifier = pkcs8::AlgorithmIdentifierRef {
oid: ALGORITHM_OID,
parameters: Some((&C::OID).into()),
};

let ec_private_key = self.to_sec1_der()?;
let pkcs8_key = pkcs8::PrivateKeyInfo::new(algorithm_identifier, &ec_private_key);
let pkcs8_key = pkcs8::PrivateKeyInfoRef::new(
algorithm_identifier,
OctetStringRef::new(&ec_private_key)?,
);
Ok(der::SecretDocument::encode_msg(&pkcs8_key)?)
}
}
Expand Down

0 comments on commit df6f7ab

Please sign in to comment.