Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: short-hand API to export keys into PKCS1 PEM #284

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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