Skip to content

Commit

Permalink
feat: short-hand API to export keys into PKCS1 PEM
Browse files Browse the repository at this point in the history
  • Loading branch information
CBenoit committed Jun 10, 2024
1 parent 5af9a96 commit 5b75703
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 40 deletions.
30 changes: 30 additions & 0 deletions ffi/dotnet/Devolutions.Picky/Generated/PrivateKey.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions ffi/dotnet/Devolutions.Picky/Generated/PublicKey.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions ffi/dotnet/Devolutions.Picky/Generated/RawPrivateKey.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions ffi/dotnet/Devolutions.Picky/Generated/RawPublicKey.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ffi/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ pub mod ffi {
Ok(Box::new(Pem(pem)))
}

/// Exports the private key into a PEM object using the PKCS 1 format
///
/// This format can only be used for RSA keys.
pub fn to_pkcs1_pem(&self) -> Result<Box<Pem>, Box<PickyError>> {
let pem = self.0.to_pkcs1_pem()?;
Ok(Box::new(Pem(pem)))
}

/// Extracts the public part of this private key
pub fn to_public_key(&self) -> Result<Box<PublicKey>, Box<PickyError>> {
let key = self.0.to_public_key()?;
Expand Down Expand Up @@ -186,6 +194,14 @@ pub mod ffi {
Ok(Box::new(Pem(pem)))
}

/// Exports the public key into a PEM object using the PKCS 1 format
///
/// This format can only be used for RSA keys.
pub fn to_pkcs1_pem(&self) -> Result<Box<Pem>, Box<PickyError>> {
let pem = self.0.to_pkcs1_pem()?;
Ok(Box::new(Pem(pem)))
}

/// Retrieves the key kind for this public key.
pub fn get_kind(&self) -> KeyKind {
self.0.kind().into()
Expand Down
16 changes: 16 additions & 0 deletions ffi/wasm/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ impl PrivateKey {
Ok(Pem(pem))
}

/// Exports the private key into a PEM object using the PKCS 1 format
///
/// This format can only be used for RSA keys.
pub fn to_pkcs1_pem(&self) -> Result<Pem, KeyError> {
let pem = self.0.to_pkcs1_pem()?;
Ok(Pem(pem))
}

/// Extracts the public part of this private key
pub fn to_public_key(&self) -> Result<PublicKey, KeyError> {
Ok(PublicKey(self.0.to_public_key()?))
Expand Down Expand Up @@ -137,4 +145,12 @@ impl PublicKey {
let pem = self.0.to_pem()?;
Ok(Pem(pem))
}

/// Exports the public key into a PEM object using the PKCS 1 format
///
/// This format can only be used for RSA keys.
pub fn to_pkcs1_pem(&self) -> Result<Pem, KeyError> {
let pem = self.0.to_pkcs1_pem()?;
Ok(Pem(pem))
}
}
9 changes: 4 additions & 5 deletions picky-asn1-x509/src/private_key_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl PrivateKeyInfo {
exponents: (IntegerAsn1, IntegerAsn1),
coefficient: IntegerAsn1,
) -> Self {
let private_key = PrivateKeyValue::RSA(
let private_key = PrivateKeyValue::Rsa(
RsaPrivateKey {
version: vec![0].into(),
modulus,
Expand Down Expand Up @@ -212,7 +212,7 @@ impl<'de> de::Deserialize<'de> for PrivateKeyInfo {
seq_next_element!(seq, PrivateKeyInfo, "private key algorithm");

let private_key = if private_key_algorithm.is_a(oids::rsa_encryption()) {
PrivateKeyValue::RSA(seq_next_element!(seq, PrivateKeyInfo, "rsa oid"))
PrivateKeyValue::Rsa(seq_next_element!(seq, PrivateKeyInfo, "rsa oid"))
} else if matches!(private_key_algorithm.parameters(), AlgorithmIdentifierParameters::Ec(_)) {
PrivateKeyValue::EC(seq_next_element!(seq, PrivateKeyInfo, "ec private key"))
} else if private_key_algorithm.is_one_of([oids::ed25519(), oids::x25519()]) {
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<'de> de::Deserialize<'de> for PrivateKeyInfo {

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum PrivateKeyValue {
RSA(OctetStringAsn1Container<RsaPrivateKey>),
Rsa(OctetStringAsn1Container<RsaPrivateKey>),
EC(OctetStringAsn1Container<ECPrivateKey>),
// Used by Ed25519, Ed448, X25519, and X448 keys
ED(OctetStringAsn1Container<OctetStringAsn1>),
Expand All @@ -274,7 +274,7 @@ impl ser::Serialize for PrivateKeyValue {
S: ser::Serializer,
{
match self {
PrivateKeyValue::RSA(rsa) => rsa.serialize(serializer),
PrivateKeyValue::Rsa(rsa) => rsa.serialize(serializer),
PrivateKeyValue::EC(ec) => ec.serialize(serializer),
PrivateKeyValue::ED(ed) => ed.serialize(serializer),
}
Expand Down Expand Up @@ -488,7 +488,6 @@ impl RsaPrivateKey {
/// publicKey [1] BIT STRING OPTIONAL
/// }
/// ```

#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
pub struct ECPrivateKey {
pub version: IntegerAsn1,
Expand Down
Loading

0 comments on commit 5b75703

Please sign in to comment.