console: derive the db handle key without ecdsa's raw scalar
ecdsa.PrivateKey.D and PublicKey.X/Y are deprecated since Go 1.26. dbHandleSecret now reads the scalar with PrivateKey.Bytes and holds it at 32 bytes, so handles issued before a deploy keep verifying. TestDBHandleSecretIsStable pins exact secrets, taken from the old code and passing against it, for P-256 and for a shorter P-224 scalar. A scalar too wide for 32 bytes still panics at startup, as FillBytes did. The signing key is always P-256 and parsed or generated by the standard library, so PrivateKey.Bytes has no valid input to refuse here. Two test helpers build their public keys with ParseUncompressedPublicKey instead of raw coordinates.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+65
−10
internal/console/apidbclaim.go+13 −1internal/console/apidbclaim_test.go+44 −0internal/console/keymail_test.go+3 −4internal/edge/certsync_e2e_test.go+5 −5
diff --git a/internal/console/apidbclaim.go b/internal/console/apidbclaim.go| index 99ca385ff..38ba23005 100644 |
| --- a/internal/console/apidbclaim.go |
| +++ b/internal/console/apidbclaim.go |
| @@ -53,8 +53,20 @@ type dbFileHandle struct { |
| SnapshotCRC64 *uint64 `json:"snapshot_crc64,omitempty"` |
| } |
| +// dbHandleSecret derives the handle key from the signing key's scalar, held |
| +// at 32 bytes so handles already issued keep verifying. A key that cannot |
| +// give one panics: NewServer calls this once, at startup. |
| func dbHandleSecret(key *ecdsa.PrivateKey) []byte { |
| - mac := hmac.New(sha256.New, key.D.FillBytes(make([]byte, 32))) |
| + scalar, err := key.Bytes() |
| + if err != nil { |
| + panic("console: database handle key: " + err.Error()) |
| + } |
| + if len(scalar) > 32 { |
| + panic("console: database handle key: the signing key's scalar is wider than 32 bytes") |
| + } |
| + padded := make([]byte, 32) |
| + copy(padded[32-len(scalar):], scalar) |
| + mac := hmac.New(sha256.New, padded) |
| mac.Write([]byte("carlos-db-handle-key-v1\x00")) |
| return mac.Sum(nil) |
| } |
diff --git a/internal/console/apidbclaim_test.go b/internal/console/apidbclaim_test.go| index 898057e82..9d8108e10 100644 |
| --- a/internal/console/apidbclaim_test.go |
| +++ b/internal/console/apidbclaim_test.go |
| @@ -5,6 +5,7 @@ import ( |
| "crypto/ecdsa" |
| "crypto/elliptic" |
| "crypto/rand" |
| + "encoding/hex" |
| "encoding/json" |
| "errors" |
| "io" |
| @@ -99,6 +100,49 @@ func cloneDoorClaim(c *hostclaim.Claim) *hostclaim.Claim { |
| return &out |
| } |
| +// TestDBHandleSecretIsStable: a handle signed before a deploy has to verify |
| +// after it, so the secret derived from the signing key is pinned to exact |
| +// bytes. The scalar is always 32 bytes wide: a short one is padded on the |
| +// left, and a key too wide for that is refused. |
| +func TestDBHandleSecretIsStable(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + curve elliptic.Curve |
| + scalar string |
| + want string |
| + }{ |
| + {"p256", elliptic.P256(), "c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721", "8714a34444d6b53233f72d189cac8f2f38d0798f20bc93eddebfcf0785958575"}, |
| + {"p256 leading zeros", elliptic.P256(), "00000000000000000000000000000000000000000000000000000000000000a7", "80d5e0a5531a5e5fb1905ca93b00476515363c9a08b53c006a2409feb7bc8933"}, |
| + {"p224 padded to 32 bytes", elliptic.P224(), "f220266e1105bfe3083e03ec2a1a2fbb8dcb1d2a1f2e3d4c5b6a7988", "e1f9ace4df16f326b5a6743db65924cf2abd0eefe1387c4a574f4ca9f5038cca"}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + raw, err := hex.DecodeString(tc.scalar) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + key, err := ecdsa.ParseRawPrivateKey(tc.curve, raw) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := hex.EncodeToString(dbHandleSecret(key)); got != tc.want { |
| + t.Fatalf("secret = %s, want %s", got, tc.want) |
| + } |
| + }) |
| + } |
| + t.Run("p384 refused", func(t *testing.T) { |
| + key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer func() { |
| + if recover() == nil { |
| + t.Fatal("a 48-byte scalar produced a secret") |
| + } |
| + }() |
| + dbHandleSecret(key) |
| + }) |
| +} |
| + |
| func TestDoorPlanHandles(t *testing.T) { |
| key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| if err != nil { |
diff --git a/internal/console/keymail_test.go b/internal/console/keymail_test.go| index efab8f993..8a6a5f592 100644 |
| --- a/internal/console/keymail_test.go |
| +++ b/internal/console/keymail_test.go |
| @@ -473,10 +473,9 @@ func testVerify(b64pub string, msg []byte, b64sig string) bool { |
| if err != nil || len(raw) != 65 || raw[0] != 4 { |
| return false |
| } |
| - pub := &ecdsa.PublicKey{ |
| - Curve: elliptic.P256(), |
| - X: new(big.Int).SetBytes(raw[1:33]), |
| - Y: new(big.Int).SetBytes(raw[33:65]), |
| + pub, err := ecdsa.ParseUncompressedPublicKey(elliptic.P256(), raw) |
| + if err != nil { |
| + return false |
| } |
| sig, err := base64.StdEncoding.DecodeString(b64sig) |
| if err != nil || len(sig) != 64 { |
diff --git a/internal/edge/certsync_e2e_test.go b/internal/edge/certsync_e2e_test.go| index 03de19a88..3dedbe096 100644 |
| --- a/internal/edge/certsync_e2e_test.go |
| +++ b/internal/edge/certsync_e2e_test.go |
| @@ -388,11 +388,11 @@ func jwkThumbprintFromJSON(raw json.RawMessage) (string, error) { |
| if err != nil { |
| return "", fmt.Errorf("jwk y: %w", err) |
| } |
| - return acme.JWKThumbprint(&ecdsa.PublicKey{ |
| - Curve: elliptic.P256(), |
| - X: new(big.Int).SetBytes(x), |
| - Y: new(big.Int).SetBytes(y), |
| - }) |
| + pub, err := ecdsa.ParseUncompressedPublicKey(elliptic.P256(), append(append([]byte{4}, x...), y...)) |
| + if err != nil { |
| + return "", fmt.Errorf("jwk point: %w", err) |
| + } |
| + return acme.JWKThumbprint(pub) |
| } |
| // ---- two edges ----------------------------------------------------------- |