Skip to content

Commit

Permalink
adds rsa helpers to read public keys (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo authored Dec 1, 2023
1 parent c2bc88f commit c875799
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/asymmetric/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ impl Algorithm {
Algorithm::EcBp512 => 64,
}
}

/// Returns true if the algorithm is RSA
pub fn is_rsa(self) -> bool {
matches!(
self,
Algorithm::Rsa2048 | Algorithm::Rsa3072 | Algorithm::Rsa4096
)
}
}

impl_algorithm_serializers!(Algorithm);
16 changes: 16 additions & 0 deletions src/asymmetric/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use ::ecdsa::elliptic_curve::{
bigint::Integer, generic_array::GenericArray, point::PointCompression, sec1, FieldBytesSize,
PrimeCurve,
};
use num_traits::FromPrimitive;
use rsa::{BigUint, RsaPublicKey};
use serde::{Deserialize, Serialize};

/// Response from `command::get_public_key`
Expand Down Expand Up @@ -74,6 +76,20 @@ impl PublicKey {
None
}
}

/// Return the RSA public key
pub fn rsa(&self) -> Option<RsaPublicKey> {
if !self.algorithm.is_rsa() {
return None;
}

const EXP: u64 = 65537;

let modulus = BigUint::from_bytes_be(&self.bytes);
let exp = BigUint::from_u64(EXP).expect("invalid static exponent");

RsaPublicKey::new(modulus, exp).ok()
}
}

impl AsRef<[u8]> for PublicKey {
Expand Down

0 comments on commit c875799

Please sign in to comment.