Skip to content

Commit

Permalink
WIP: pkcs1: adopt pkcs8 API changes
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Gautier <[email protected]>
  • Loading branch information
baloo committed Aug 19, 2024
1 parent 8191fb6 commit 5c7ed62
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 10 additions & 2 deletions pkcs1/src/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub(crate) mod other_prime_info;
use crate::{Error, Result, RsaPublicKey, Version};
use core::fmt;
use der::{
asn1::UintRef, Decode, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, Tag,
Writer,
asn1::{OctetStringRef, UintRef},
Decode, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, Tag, Writer,
};

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -172,6 +172,14 @@ impl<'a> TryFrom<&'a [u8]> for RsaPrivateKey<'a> {
}
}

impl<'a> TryFrom<OctetStringRef<'a>> for RsaPrivateKey<'a> {
type Error = Error;

fn try_from(bytes: OctetStringRef<'a>) -> Result<Self> {
Ok(Self::from_der(bytes.as_bytes())?)
}
}

impl fmt::Debug for RsaPrivateKey<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RsaPrivateKey")
Expand Down
7 changes: 5 additions & 2 deletions pkcs1/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
#[cfg(feature = "pkcs8")]
use {
crate::{ALGORITHM_ID, ALGORITHM_OID},
der::asn1::BitStringRef,
der::asn1::{BitStringRef, OctetStringRef},
};

#[cfg(feature = "std")]
Expand Down Expand Up @@ -160,6 +160,8 @@ where
T: for<'a> TryFrom<pkcs8::PrivateKeyInfo<'a>, Error = pkcs8::Error>,
{
fn from_pkcs1_der(private_key: &[u8]) -> Result<Self> {
// TODO: UNTESTED
let private_key = OctetStringRef::new(private_key)?;
Ok(Self::try_from(pkcs8::PrivateKeyInfo {
algorithm: ALGORITHM_ID,
private_key,
Expand All @@ -185,9 +187,10 @@ where
impl<T: pkcs8::EncodePrivateKey> EncodeRsaPrivateKey for T {
fn to_pkcs1_der(&self) -> Result<SecretDocument> {
let pkcs8_doc = self.to_pkcs8_der()?;
// TODO: UNTESTED
let pkcs8_key = pkcs8::PrivateKeyInfo::from_der(pkcs8_doc.as_bytes())?;
pkcs8_key.algorithm.assert_algorithm_oid(ALGORITHM_OID)?;
RsaPrivateKey::from_der(pkcs8_key.private_key)?.try_into()
RsaPrivateKey::from_der(pkcs8_key.private_key.as_bytes())?.try_into()
}
}

Expand Down

0 comments on commit 5c7ed62

Please sign in to comment.