console: refuse a db handle scalar only when its value overflows
PrivateKey.Bytes returns the curve's full width, so the length check refused every P-384 or P-521 key. The old FillBytes refused only a value needing more than 32 bytes, and Store.SigningKey does not restrict the curve, so a stored wide-curve key with a small scalar went from working to a panic at startup. Excess leading bytes must now be zero and are dropped, which gives the bytes FillBytes produced. P-384 fixtures pin a scalar that fits, with goldens from the original derivation, and one a bit too large that is still refused.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+21
−9
internal/console/apidbclaim.go+9 −4internal/console/apidbclaim_test.go+12 −5
CI cancelled — superseded by a newer push — run details
diff --git a/internal/console/apidbclaim.go b/internal/console/apidbclaim.go| index 38ba23005..24e97ad0d 100644 |
| --- a/internal/console/apidbclaim.go |
| +++ b/internal/console/apidbclaim.go |
| @@ -54,15 +54,20 @@ type dbFileHandle struct { |
| } |
| // 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. |
| +// at 32 bytes so handles already issued keep verifying. Bytes pads to the |
| +// curve's width, so on a wider curve the excess leading bytes are zero |
| +// whenever the value fits. A key whose value does not fit, or that cannot |
| +// be encoded, panics: NewServer calls this once, at startup. |
| func dbHandleSecret(key *ecdsa.PrivateKey) []byte { |
| 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") |
| + for len(scalar) > 32 { |
| + if scalar[0] != 0 { |
| + panic("console: database handle key: the signing key's scalar does not fit in 32 bytes") |
| + } |
| + scalar = scalar[1:] |
| } |
| padded := make([]byte, 32) |
| copy(padded[32-len(scalar):], scalar) |
diff --git a/internal/console/apidbclaim_test.go b/internal/console/apidbclaim_test.go| index 9d8108e10..0953f5738 100644 |
| --- a/internal/console/apidbclaim_test.go |
| +++ b/internal/console/apidbclaim_test.go |
| @@ -102,8 +102,9 @@ func cloneDoorClaim(c *hostclaim.Claim) *hostclaim.Claim { |
| // 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. |
| +// bytes. The scalar's value is always held in 32 bytes: a short one is padded |
| +// on the left, and only a value that needs more than 32 bytes is refused, |
| +// whatever the width of the curve it is on. |
| func TestDBHandleSecretIsStable(t *testing.T) { |
| for _, tc := range []struct { |
| name string |
| @@ -114,6 +115,8 @@ func TestDBHandleSecretIsStable(t *testing.T) { |
| {"p256", elliptic.P256(), "c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721", "8714a34444d6b53233f72d189cac8f2f38d0798f20bc93eddebfcf0785958575"}, |
| {"p256 leading zeros", elliptic.P256(), "00000000000000000000000000000000000000000000000000000000000000a7", "80d5e0a5531a5e5fb1905ca93b00476515363c9a08b53c006a2409feb7bc8933"}, |
| {"p224 padded to 32 bytes", elliptic.P224(), "f220266e1105bfe3083e03ec2a1a2fbb8dcb1d2a1f2e3d4c5b6a7988", "e1f9ace4df16f326b5a6743db65924cf2abd0eefe1387c4a574f4ca9f5038cca"}, |
| + {"p384 scalar that fits", elliptic.P384(), "00000000000000000000000000000000c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721", "8714a34444d6b53233f72d189cac8f2f38d0798f20bc93eddebfcf0785958575"}, |
| + {"p384 scalar of one", elliptic.P384(), "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", "130fa6293b53ed4f6ab2786ad4e431338c77729222cd70777da6ad4f511a9f55"}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| raw, err := hex.DecodeString(tc.scalar) |
| @@ -129,14 +132,18 @@ func TestDBHandleSecretIsStable(t *testing.T) { |
| } |
| }) |
| } |
| - t.Run("p384 refused", func(t *testing.T) { |
| - key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) |
| + t.Run("p384 scalar one bit past 32 bytes refused", func(t *testing.T) { |
| + raw, err := hex.DecodeString("00000000000000000000000000000001c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + key, err := ecdsa.ParseRawPrivateKey(elliptic.P384(), raw) |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer func() { |
| if recover() == nil { |
| - t.Fatal("a 48-byte scalar produced a secret") |
| + t.Fatal("a scalar that needs 33 bytes produced a secret") |
| } |
| }() |
| dbHandleSecret(key) |