Skip to content

Commit

Permalink
Make RSA PrivateKey conversion symmetric
Browse files Browse the repository at this point in the history
  • Loading branch information
maximebedard committed Jun 19, 2024
1 parent 6d5eae7 commit 35f6422
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
13 changes: 10 additions & 3 deletions jose-jwk/src/crypto/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use jose_jwa::{Algorithm, Algorithm::Signing, Signing::*};

use super::Error;
use super::KeyInfo;
use crate::{Rsa, RsaPrivate};
use crate::{Rsa, RsaOptional, RsaPrivate};

impl KeyInfo for RsaPublicKey {
fn strength(&self) -> usize {
Expand Down Expand Up @@ -95,15 +95,22 @@ impl TryFrom<Rsa> for RsaPublicKey {
}
}

// TODO: patch rsa crate to export the optional values
impl From<&RsaPrivateKey> for Rsa {
fn from(pk: &RsaPrivateKey) -> Self {
let opt = Some(RsaOptional {
p: pk.primes()[0].to_bytes_be().into(),
q: pk.primes()[1].to_bytes_be().into(),
dp: pk.dp().expect("unreachable").to_bytes_be().into(),
dq: pk.dq().expect("unreachable").to_bytes_be().into(),
qi: pk.qinv().expect("unreachable").to_bytes_be().1.into(),
oth: alloc::vec![],
});
Self {
n: pk.n().to_bytes_be().into(),
e: pk.e().to_bytes_be().into(),
prv: Some(RsaPrivate {
d: pk.d().to_bytes_be().into(),
opt: None,
opt,
}),
}
}
Expand Down
7 changes: 2 additions & 5 deletions jose-jwk/tests/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod rfc7517 {
"qi":"GyM_p6JrXySiz1toFgKbWV-JdI3jQ4ypu9rbMWx3rQJBfmt0FoYzgUIZEVFEcOqwemRN81zoDAaa-Bk0KWNGDjJHZDdDmFhW3AN7lI-puxk_mHZGJ11rxyR8O55XLSe3SPmRfKwZI6yU24ZxvQKFYItdldUKGzO6Ia6zTKhAVRU",
"alg":"RS256",
"kid":"2011-04-29"
}
},
]
});

Expand Down Expand Up @@ -306,10 +306,7 @@ mod rfc7517 {
#[cfg(feature = "rsa")]
if let Key::Rsa(key) = &jwk.keys[1].key {
let pk = ::rsa::RsaPrivateKey::try_from(key).unwrap();
// FIXME: work around the serialization asymmetry.
let mut k: Rsa = pk.into();
k.prv.as_mut().unwrap().opt = key.prv.as_ref().unwrap().opt.clone();
assert_eq!(key, &k);
assert_eq!(key, &pk.into());
} else {
unreachable!()
}
Expand Down

0 comments on commit 35f6422

Please sign in to comment.