fix(sm2): reject non-uncompressed public key encodings

NewPublicKey documented that it takes an uncompressed point, but it delegated the check to the internal parser, which accepts compressed SEC 1 encodings. A 33-byte 02/03 key therefore came back as an ecdsa.PublicKey with Y = 0: an unusable key that nothing validated, because the on-curve check runs over the encoding rather than the decoded point.

Check the length and the 0x04 prefix before decoding, and return errInvalidPublicKey for everything else. Compressed, hybrid (06/07) and infinity encodings now fail cleanly; 65-byte inputs still go through the same on-curve check as before. All in-repo callers (cfca/pkcs10, smx509/cfca_csr, the examples) already pass 04||X||Y.

TestParseUncompressedPublicKey now asserts errInvalidPublicKey and covers the four rejected prefixes.
This commit is contained in:
Sun Yimin committed 2026-10-21 10:35:44 +08:00
1 parent b46911628f
commit 5b2d5c13a8
2 files changed
+21 -6

No files matched your search

+7
View File
@@ -196,7 +196,14 @@ func NewPrivateKeyFromInt(key *big.Int) (*PrivateKey, error) {
//
// The key parameter is a byte slice representing the public key in uncompressed format.
// According to GB/T 32918.1-2016, the public key must be in the correct format and on the curve.
// Compressed, hybrid and infinity encodings are rejected: they cannot be represented by an
// [ecdsa.PublicKey] without a second decoding step, and accepting them here used to yield a
// key with a zero Y coordinate.
func NewPublicKey(key []byte) (*ecdsa.PublicKey, error) {
byteLen := (P256().Params().BitSize + 7) / 8
if len(key) != 1+2*byteLen || key[0] != uncompressed {
return nil, errInvalidPublicKey
}
k, err := sm2.NewPublicKey(key)
if err != nil {
return nil, err
+14 -6
View File
@@ -8,6 +8,7 @@ import (
"crypto/elliptic"
"crypto/rand"
"encoding/hex"
"errors"
"math/big"
"testing"
@@ -91,21 +92,28 @@ func TestNewPrivateKeyFromInt(t *testing.T) {
func TestParseUncompressedPublicKey(t *testing.T) {
// test nil
_, err := ParseUncompressedPublicKey(nil)
if err == nil || err.Error() != "sm2: invalid public key encoding" {
t.Errorf("should throw sm2: invalid public key encoding")
if !errors.Is(err, errInvalidPublicKey) {
t.Errorf("nil: got %v, want %v", err, errInvalidPublicKey)
}
// test without point format prefix byte
keypoints, _ := hex.DecodeString("8356e642a40ebd18d29ba3532fbd9f3bbee8f027c3f6f39a5ba2f870369f9988981f5efe55d1c5cdf6c0ef2b070847a14f7fdf4272a8df09c442f3058af94ba1")
_, err = ParseUncompressedPublicKey(keypoints)
if err == nil || err.Error() != "invalid P256 point encoding" {
t.Errorf("invalid P256 point encoding")
if !errors.Is(err, errInvalidPublicKey) {
t.Errorf("missing prefix: got %v, want %v", err, errInvalidPublicKey)
}
// test correct point
keypoints, _ = hex.DecodeString("048356e642a40ebd18d29ba3532fbd9f3bbee8f027c3f6f39a5ba2f870369f9988981f5efe55d1c5cdf6c0ef2b070847a14f7fdf4272a8df09c442f3058af94ba1")
_, err = ParseUncompressedPublicKey(keypoints)
if err != nil {
if _, err := ParseUncompressedPublicKey(keypoints); err != nil {
t.Fatal(err)
}
// test compressed and hybrid prefixes: they used to be decoded into a key
// with a zero Y coordinate instead of being rejected
for _, prefix := range []byte{2, 3, 6, 7} {
encoded := append([]byte{prefix}, keypoints[1:]...)
if _, err := ParseUncompressedPublicKey(encoded); !errors.Is(err, errInvalidPublicKey) {
t.Errorf("prefix %#x: got %v, want %v", prefix, err, errInvalidPublicKey)
}
}
// test point not on curve
keypoints, _ = hex.DecodeString("048356e642a40ebd18d29ba3532fbd9f3bbee8f027c3f6f39a5ba2f870369f9988981f5efe55d1c5cdf6c0ef2b070847a14f7fdf4272a8df09c442f3058af94ba2")
_, err = ParseUncompressedPublicKey(keypoints)