rsa: enforce key size limits on all constructors (priv+pub)#45
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens RSA key handling by enforcing a consistent public-key policy (minimum/maximum modulus size plus basic modulus/exponent validity) across all RSA import/constructor paths, and strengthens private key imports by validating and precomputing the parsed RSA private key material.
Changes:
- Centralizes RSA public-key validation in a new
validatePublicKeyhelper and applies it to PKCS#1, X.509 (DER/PEM), multibase (via X.509), andPublicKeyFromNE. - Tightens PKCS#8 private key imports by validating the embedded public key and calling
rsa.PrivateKey.Validate()+Precompute()before returning. - Adds a regression test asserting 1024-bit RSA material is rejected across generation and all relevant public/private constructors.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| crypto/rsa/public.go | Adds shared RSA public-key validation and applies it to all public-key constructors. |
| crypto/rsa/private.go | Enforces policy and strengthens PKCS#8 private key import by validating and precomputing parsed keys. |
| crypto/rsa/key_test.go | Adds coverage ensuring sub-policy RSA keys are rejected across all constructors/import paths. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
dovydas55
approved these changes
Jun 23, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Note
Medium Risk
Tightens cryptographic key acceptance on all import paths; behavior change for callers that relied on sub-2048-bit or malformed keys, but it hardens security rather than widening attack surface.
Overview
Centralizes RSA public-key policy in a new
validatePublicKeyhelper (modulus bit length withinMinRsaKeyBits/MaxRsaKeyBits, odd modulus, valid exponent) and runs it on every public import path that previously only parsed DER/PEM or NE bytes—PKCS#1, PKIX/X509, multibase (via X509), andPublicKeyFromNE(inline checks replaced by the shared validator).Private PKCS#8 imports now reject weak keys by validating the embedded public half, then call
rsa.PrivateKey.Validate()andPrecompute()before wrapping the key.Adds
TestRejectBelowPolicyRSAImportsto assert 1024-bit material fails across generation and all listed public/private constructors.Reviewed by Cursor Bugbot for commit 24414ae. Bugbot is set up for automated code reviews on this repo. Configure here.