Skip to content

rsa: enforce key size limits on all constructors (priv+pub)#45

Merged
MichaelMure merged 1 commit into
masterfrom
rsa-enforce-limits
Jun 23, 2026
Merged

rsa: enforce key size limits on all constructors (priv+pub)#45
MichaelMure merged 1 commit into
masterfrom
rsa-enforce-limits

Conversation

@MichaelMure

@MichaelMure MichaelMure commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

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 validatePublicKey helper (modulus bit length within MinRsaKeyBits/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), and PublicKeyFromNE (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() and Precompute() before wrapping the key.

Adds TestRejectBelowPolicyRSAImports to 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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 validatePublicKey helper and applies it to PKCS#1, X.509 (DER/PEM), multibase (via X.509), and PublicKeyFromNE.
  • 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.

@MichaelMure MichaelMure merged commit d4a32b4 into master Jun 23, 2026
8 checks passed
@MichaelMure MichaelMure deleted the rsa-enforce-limits branch June 23, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants