-
Notifications
You must be signed in to change notification settings - Fork 357
refactor(rust_crypto): move RSA backend to rsa 0.10.0-rc.17 #502
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
Open
EffortlessSteven
wants to merge
3
commits into
Keats:master
Choose a base branch
from
EffortlessSteven:codex/rust-crypto-rsa-010
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
365fcb4
refactor(rust_crypto): move RSA signing onto low-level key operations
EffortlessSteven c6605f2
chore(rust_crypto): lift RSA backend to rsa 0.10.0-rc.17
EffortlessSteven 0ed6873
fix(rust_crypto): preserve RSA component extraction behavior
EffortlessSteven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,24 +1,32 @@ | ||||||
| //! Implementations of the [`JwtSigner`] and [`JwtVerifier`] traits for the | ||||||
| //! RSA family of algorithms using RustCrypto. | ||||||
|
|
||||||
| use hmac::digest::FixedOutputReset; | ||||||
| // Use the hash types re-exported by `rsa` so this backend stays in the same | ||||||
| // digest ecosystem as the 0.10 RC line without a crate-wide sha2 migration. | ||||||
| use rsa::{ | ||||||
| BigUint, Pkcs1v15Sign, Pss, RsaPublicKey, | ||||||
| BoxedUint, Pkcs1v15Sign, Pss, RsaPrivateKey, RsaPublicKey, | ||||||
| pkcs1::{DecodeRsaPrivateKey, DecodeRsaPublicKey}, | ||||||
| pkcs1v15::SigningKey, | ||||||
| pkcs8::AssociatedOid, | ||||||
| pss::BlindedSigningKey, | ||||||
| sha2::{ | ||||||
| Sha256, Sha384, Sha512, | ||||||
| digest::{Digest, FixedOutputReset}, | ||||||
| }, | ||||||
| traits::SignatureScheme, | ||||||
| }; | ||||||
| use sha2::{Digest, Sha256, Sha384, Sha512}; | ||||||
| use signature::{RandomizedSigner, SignatureEncoding, Signer, Verifier}; | ||||||
| use signature::{Signer, Verifier}; | ||||||
|
|
||||||
| use crate::algorithms::AlgorithmFamily; | ||||||
| use crate::crypto::{JwtSigner, JwtVerifier}; | ||||||
| use crate::decoding::DecodingKeyKind; | ||||||
| use crate::errors::{ErrorKind, Result, new_error}; | ||||||
| use crate::{Algorithm, DecodingKey, EncodingKey}; | ||||||
|
|
||||||
| fn to_boxed_uint(bytes: &[u8]) -> std::result::Result<BoxedUint, signature::Error> { | ||||||
| let bits_precision = | ||||||
| u32::try_from(bytes.len().saturating_mul(8)).map_err(signature::Error::from_source)?; | ||||||
| BoxedUint::from_be_slice(bytes, bits_precision).map_err(signature::Error::from_source) | ||||||
| } | ||||||
|
|
||||||
| fn try_sign_rsa<H>( | ||||||
| encoding_key: &EncodingKey, | ||||||
| msg: &[u8], | ||||||
|
|
@@ -27,21 +35,22 @@ fn try_sign_rsa<H>( | |||||
| where | ||||||
| H: Digest + AssociatedOid + FixedOutputReset, | ||||||
| { | ||||||
| let mut rng = rand::thread_rng(); | ||||||
| if pss { | ||||||
| let private_key = rsa::RsaPrivateKey::from_pkcs1_der(encoding_key.inner()) | ||||||
| .map_err(signature::Error::from_source)?; | ||||||
| let signing_key = BlindedSigningKey::<H>::new(private_key); | ||||||
| Ok(signing_key.sign_with_rng(&mut rng, msg).to_vec()) | ||||||
| let mut rng = rand10::rng(); | ||||||
| let private_key = RsaPrivateKey::from_pkcs1_der(encoding_key.inner()) | ||||||
| .map_err(signature::Error::from_source)?; | ||||||
| let digest = H::digest(msg); | ||||||
|
|
||||||
| let signature = if pss { | ||||||
| private_key.sign_with_rng(&mut rng, Pss::<H>::new(), digest.as_ref()) | ||||||
| } else { | ||||||
| let private_key = rsa::RsaPrivateKey::from_pkcs1_der(encoding_key.inner()) | ||||||
| .map_err(signature::Error::from_source)?; | ||||||
| let signing_key = SigningKey::<H>::new(private_key); | ||||||
| Ok(signing_key.sign_with_rng(&mut rng, msg).to_vec()) | ||||||
| private_key.sign_with_rng(&mut rng, Pkcs1v15Sign::new::<H>(), digest.as_ref()) | ||||||
| } | ||||||
| .map_err(signature::Error::from_source)?; | ||||||
|
|
||||||
| Ok(signature) | ||||||
| } | ||||||
|
|
||||||
| fn verify_rsa<S: SignatureScheme, H: Digest + AssociatedOid>( | ||||||
| fn verify_rsa<S: SignatureScheme, H: Digest>( | ||||||
| scheme: S, | ||||||
| decoding_key: &DecodingKey, | ||||||
| msg: &[u8], | ||||||
|
|
@@ -53,12 +62,13 @@ fn verify_rsa<S: SignatureScheme, H: Digest + AssociatedOid>( | |||||
| DecodingKeyKind::SecretOrDer(bytes) => { | ||||||
| RsaPublicKey::from_pkcs1_der(bytes) | ||||||
| .map_err(signature::Error::from_source)? | ||||||
| .verify(scheme, &digest, signature) | ||||||
| .verify(scheme, digest.as_ref(), signature) | ||||||
| .map_err(signature::Error::from_source)?; | ||||||
| } | ||||||
| DecodingKeyKind::RsaModulusExponent { n, e } => { | ||||||
| RsaPublicKey::new(BigUint::from_bytes_be(n), BigUint::from_bytes_be(e))? | ||||||
| .verify(scheme, &digest, signature) | ||||||
| RsaPublicKey::new(to_boxed_uint(n)?, to_boxed_uint(e)?) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend the use of BoxedUint::from_be_slice_vartime:
Suggested change
NOTE(security): Now this is a variable time function (and inherently dangerous), but here this is only used on a public key and no secret is involved. |
||||||
| .map_err(signature::Error::from_source)? | ||||||
| .verify(scheme, digest.as_ref(), signature) | ||||||
| .map_err(signature::Error::from_source)?; | ||||||
| } | ||||||
| }; | ||||||
|
|
@@ -115,7 +125,7 @@ macro_rules! define_rsa_verifier { | |||||
| signature: &Vec<u8>, | ||||||
| ) -> std::result::Result<(), signature::Error> { | ||||||
| if $pss { | ||||||
| verify_rsa::<Pss, $hash>(Pss::new::<$hash>(), &self.0, msg, signature) | ||||||
| verify_rsa::<_, $hash>(Pss::<$hash>::new(), &self.0, msg, signature) | ||||||
| } else { | ||||||
| verify_rsa::<_, $hash>(Pkcs1v15Sign::new::<$hash>(), &self.0, msg, signature) | ||||||
| } | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BoxedUint::to_be_bytes_trimmed_vartime is the function you're looking for.
NOTE(security): variable timed function, only to be used on public components.