Skip to content

ed25519,x25519: fix possible bad public key conversion#37

Merged
MichaelMure merged 2 commits into
masterfrom
25519-pubkey-conv
Apr 1, 2026
Merged

ed25519,x25519: fix possible bad public key conversion#37
MichaelMure merged 2 commits into
masterfrom
25519-pubkey-conv

Conversation

@MichaelMure

@MichaelMure MichaelMure commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator

Note

Medium Risk
Touches cryptographic key parsing: invalid or unexpected X.509 public keys now error instead of panicking or being accepted, which could affect callers relying on previous lax behavior.

Overview
Hardens X.509 public key decoding for ed25519 and x25519 by replacing unchecked type assertions with explicit type checks and clear errors.

Adds validation that the parsed key is the expected algorithm (and curve for X25519) and that the raw key material is the expected length before constructing the library PublicKey wrappers.

Written by Cursor Bugbot for commit f0c985a. This will update automatically on new commits. 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 X.509 DER public-key decoding for the ed25519 and x25519 key types by avoiding unsafe type assertions and returning a proper error instead of panicking when the parsed key is not of the expected type.

Changes:

  • Replace direct type assertions with checked assertions (ok idiom) in X.509 DER public key parsing.
  • Return descriptive errors when x509.ParsePKIXPublicKey yields an unexpected key type.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
crypto/x25519/public.go Makes X.509 DER parsing robust to unexpected public key types by avoiding panics.
crypto/ed25519/public.go Makes X.509 DER parsing robust to unexpected public key types by avoiding panics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crypto/x25519/public.go
Comment thread crypto/x25519/public.go
Comment on lines +107 to +110
ecdhPub, ok := pub.(*ecdh.PublicKey)
if !ok {
return nil, fmt.Errorf("invalid x25519 public key type")
}

Copilot AI Apr 1, 2026

Copy link

Choose a reason for hiding this comment

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

There’s no test exercising the new non-panicking error path when ParsePKIXPublicKey returns an unexpected type. Add a unit test that feeds a non-X25519 X.509 public key (e.g., P-256) into PublicKeyFromX509DER and asserts it returns an error (and does not panic), to prevent regressions.

Copilot uses AI. Check for mistakes.
Comment thread crypto/ed25519/public.go
Comment thread crypto/ed25519/public.go
Comment on lines +58 to +61
ed25519Pub, ok := pub.(ed25519.PublicKey)
if !ok {
return PublicKey{}, fmt.Errorf("invalid ed25519 public key type")
}

Copilot AI Apr 1, 2026

Copy link

Choose a reason for hiding this comment

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

There’s no test covering the newly added error path when ParsePKIXPublicKey returns a non-ed25519 key. Add a unit test that passes an X.509 public key of a different algorithm into PublicKeyFromX509DER and asserts it returns the new error (and does not panic).

Copilot uses AI. Check for mistakes.
@MichaelMure MichaelMure requested a review from smoyer64 April 1, 2026 14:20
smoyer64
smoyer64 previously approved these changes Apr 1, 2026

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Comment thread crypto/x25519/public.go Outdated
smoyer64
smoyer64 previously approved these changes Apr 1, 2026
@MichaelMure MichaelMure merged commit 42ad037 into master Apr 1, 2026
7 checks passed
@MichaelMure MichaelMure deleted the 25519-pubkey-conv branch April 1, 2026 15:22
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