mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-31 20:11:31 +02:00
[client, relay, management] Bump go version to 1.26 and go-quic to v0.62.0 (#7359)
* Bump go version to 1.26 and go-quic to v0.62.0 * Replace deprecated ecdsa public key assembly and add tests for jwt * Update goversioninfo * Pin go toolchain to 1.26.7
This commit is contained in:
@@ -289,36 +289,64 @@ func getPublicKey(token *jwt.Token, jwks *Jwks) (interface{}, error) {
|
||||
return nil, errKeyNotFound
|
||||
}
|
||||
|
||||
func getPublicKeyFromECDSA(jwk JSONWebKey) (publicKey *ecdsa.PublicKey, err error) {
|
||||
func curveFromName(crv string) (elliptic.Curve, error) {
|
||||
switch crv {
|
||||
case p256:
|
||||
return elliptic.P256(), nil
|
||||
case p384:
|
||||
return elliptic.P384(), nil
|
||||
case p521:
|
||||
return elliptic.P521(), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported elliptic curve %q", crv)
|
||||
}
|
||||
}
|
||||
|
||||
func getPublicKeyFromECDSA(jwk JSONWebKey) (*ecdsa.PublicKey, error) {
|
||||
if jwk.X == "" || jwk.Y == "" || jwk.Crv == "" {
|
||||
return nil, fmt.Errorf("ecdsa key incomplete")
|
||||
}
|
||||
|
||||
var xCoordinate []byte
|
||||
if xCoordinate, err = base64.RawURLEncoding.DecodeString(jwk.X); err != nil {
|
||||
curve, err := curveFromName(jwk.Crv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var yCoordinate []byte
|
||||
if yCoordinate, err = base64.RawURLEncoding.DecodeString(jwk.Y); err != nil {
|
||||
return nil, err
|
||||
xCoordinate, err := base64.RawURLEncoding.DecodeString(jwk.X)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode ecdsa x coordinate: %w", err)
|
||||
}
|
||||
|
||||
publicKey = &ecdsa.PublicKey{}
|
||||
|
||||
var curve elliptic.Curve
|
||||
switch jwk.Crv {
|
||||
case p256:
|
||||
curve = elliptic.P256()
|
||||
case p384:
|
||||
curve = elliptic.P384()
|
||||
case p521:
|
||||
curve = elliptic.P521()
|
||||
yCoordinate, err := base64.RawURLEncoding.DecodeString(jwk.Y)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode ecdsa y coordinate: %w", err)
|
||||
}
|
||||
|
||||
publicKey.Curve = curve
|
||||
publicKey.X = big.NewInt(0).SetBytes(xCoordinate)
|
||||
publicKey.Y = big.NewInt(0).SetBytes(yCoordinate)
|
||||
var x, y big.Int
|
||||
x.SetBytes(xCoordinate)
|
||||
y.SetBytes(yCoordinate)
|
||||
|
||||
bits := curve.Params().BitSize
|
||||
if x.BitLen() > bits {
|
||||
return nil, fmt.Errorf("ecdsa x coordinate is %d bits, exceeds curve %s field size of %d bits", x.BitLen(), jwk.Crv, bits)
|
||||
}
|
||||
if y.BitLen() > bits {
|
||||
return nil, fmt.Errorf("ecdsa y coordinate is %d bits, exceeds curve %s field size of %d bits", y.BitLen(), jwk.Crv, bits)
|
||||
}
|
||||
|
||||
// Round up: P-521's field is 521 bits, so a coordinate needs 66 bytes, not 65.
|
||||
size := (bits + 7) / 8
|
||||
|
||||
// Assemble the SEC 1 uncompressed point (0x04 || X || Y)
|
||||
point := make([]byte, 1+2*size)
|
||||
point[0] = 4
|
||||
x.FillBytes(point[1 : 1+size])
|
||||
y.FillBytes(point[1+size:])
|
||||
|
||||
publicKey, err := ecdsa.ParseUncompressedPublicKey(curve, point)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse ecdsa public key: %w", err)
|
||||
}
|
||||
|
||||
return publicKey, nil
|
||||
}
|
||||
|
||||
214
shared/auth/jwt/validator_test.go
Normal file
214
shared/auth/jwt/validator_test.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// ecdsaJWK builds a JWK for pub using uncompressed-point encoding
|
||||
func ecdsaJWK(t *testing.T, kid string, pub *ecdsa.PublicKey, crv string, size int) JSONWebKey {
|
||||
t.Helper()
|
||||
|
||||
point, err := pub.Bytes()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, point, 1+2*size)
|
||||
require.Equal(t, byte(4), point[0], "expected uncompressed point")
|
||||
|
||||
return JSONWebKey{
|
||||
Kty: "EC",
|
||||
Kid: kid,
|
||||
Use: "sig",
|
||||
Crv: crv,
|
||||
X: base64.RawURLEncoding.EncodeToString(point[1 : 1+size]),
|
||||
Y: base64.RawURLEncoding.EncodeToString(point[1+size:]),
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPublicKeyFromECDSA_RoundTrip(t *testing.T) {
|
||||
tests := []struct {
|
||||
crv string
|
||||
curve elliptic.Curve
|
||||
size int
|
||||
}{
|
||||
{p256, elliptic.P256(), 32},
|
||||
{p384, elliptic.P384(), 48},
|
||||
{p521, elliptic.P521(), 66},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.crv, func(t *testing.T) {
|
||||
priv, err := ecdsa.GenerateKey(tc.curve, rand.Reader)
|
||||
require.NoError(t, err)
|
||||
|
||||
got, err := getPublicKeyFromECDSA(ecdsaJWK(t, "kid", &priv.PublicKey, tc.crv, tc.size))
|
||||
require.NoError(t, err)
|
||||
assert.True(t, priv.PublicKey.Equal(got), "parsed key differs from the original")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetPublicKeyFromECDSA_ShortCoordinate covers IdPs that strip leading zero
|
||||
// bytes from a coordinate instead of padding to the curve's field size.
|
||||
func TestGetPublicKeyFromECDSA_ShortCoordinate(t *testing.T) {
|
||||
var (
|
||||
priv *ecdsa.PrivateKey
|
||||
point []byte
|
||||
)
|
||||
for i := 0; i < 10000; i++ {
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
|
||||
p, err := key.PublicKey.Bytes()
|
||||
require.NoError(t, err)
|
||||
|
||||
if p[1] == 0 || p[33] == 0 {
|
||||
priv, point = key, p
|
||||
break
|
||||
}
|
||||
}
|
||||
require.NotNil(t, priv, "no key with a leading zero coordinate byte was generated")
|
||||
|
||||
jwk := JSONWebKey{
|
||||
Kty: "EC",
|
||||
Kid: "kid",
|
||||
Crv: p256,
|
||||
X: base64.RawURLEncoding.EncodeToString(bytes.TrimLeft(point[1:33], "\x00")),
|
||||
Y: base64.RawURLEncoding.EncodeToString(bytes.TrimLeft(point[33:], "\x00")),
|
||||
}
|
||||
|
||||
got, err := getPublicKeyFromECDSA(jwk)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, priv.PublicKey.Equal(got))
|
||||
}
|
||||
|
||||
func TestGetPublicKeyFromECDSA_Invalid(t *testing.T) {
|
||||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
valid := ecdsaJWK(t, "kid", &priv.PublicKey, p256, 32)
|
||||
|
||||
offCurve := valid
|
||||
x, err := base64.RawURLEncoding.DecodeString(valid.X)
|
||||
require.NoError(t, err)
|
||||
x[31] ^= 0xff
|
||||
offCurve.X = base64.RawURLEncoding.EncodeToString(x)
|
||||
|
||||
// 33 non-zero bytes is 264 bits, past P-256's 256-bit field.
|
||||
oversized := valid
|
||||
oversized.X = base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{0xff}, 33))
|
||||
|
||||
// P-521 coordinates occupy 66 bytes but only 521 bits, so a full 66-byte
|
||||
// 0xff value (528 bits) is over the field size without being over the byte
|
||||
// length. Only a bit-length bound catches this.
|
||||
overP521 := JSONWebKey{
|
||||
Kty: "EC",
|
||||
Crv: p521,
|
||||
X: base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{0xff}, 66)),
|
||||
Y: base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{0xff}, 66)),
|
||||
}
|
||||
|
||||
zeroPoint := valid
|
||||
zeroPoint.X = base64.RawURLEncoding.EncodeToString(make([]byte, 32))
|
||||
zeroPoint.Y = base64.RawURLEncoding.EncodeToString(make([]byte, 32))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
jwk JSONWebKey
|
||||
errContains string
|
||||
}{
|
||||
{name: "missing crv", jwk: JSONWebKey{Kty: "EC", X: valid.X, Y: valid.Y}},
|
||||
{name: "missing x", jwk: JSONWebKey{Kty: "EC", Crv: p256, Y: valid.Y}},
|
||||
{name: "unsupported curve", jwk: JSONWebKey{Kty: "EC", Crv: "P-224", X: valid.X, Y: valid.Y}, errContains: "unsupported elliptic curve"},
|
||||
{name: "undecodable x", jwk: JSONWebKey{Kty: "EC", Crv: p256, X: "!!not base64!!!", Y: valid.Y}, errContains: "decode ecdsa x coordinate"},
|
||||
{name: "coordinate over field size", jwk: oversized, errContains: "exceeds curve P-256 field size of 256 bits"},
|
||||
{name: "p521 coordinate over field size", jwk: overP521, errContains: "exceeds curve P-521 field size of 521 bits"},
|
||||
{name: "off-curve point", jwk: offCurve, errContains: "parse ecdsa public key"},
|
||||
{name: "point at infinity", jwk: zeroPoint, errContains: "parse ecdsa public key"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
key, err := getPublicKeyFromECDSA(tc.jwk)
|
||||
require.Error(t, err)
|
||||
assert.Nil(t, key)
|
||||
if tc.errContains != "" {
|
||||
assert.ErrorContains(t, err, tc.errContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateAndParse_ECDSA verifies an ES256-signed token end to end, proving
|
||||
// the parsed key actually validates signatures.
|
||||
func TestValidateAndParse_ECDSA(t *testing.T) {
|
||||
const (
|
||||
kid = "es256-kid"
|
||||
issuer = "https://issuer.example.com/"
|
||||
audience = "netbird"
|
||||
)
|
||||
|
||||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
|
||||
jwks, err := json.Marshal(Jwks{Keys: []JSONWebKey{ecdsaJWK(t, kid, &priv.PublicKey, p256, 32)}})
|
||||
require.NoError(t, err)
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(jwks)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodES256, jwt.MapClaims{
|
||||
"iss": issuer,
|
||||
"aud": audience,
|
||||
"sub": "user-1",
|
||||
"iat": time.Now().Add(-time.Minute).Unix(),
|
||||
"exp": time.Now().Add(time.Hour).Unix(),
|
||||
})
|
||||
token.Header["kid"] = kid
|
||||
|
||||
signed, err := token.SignedString(priv)
|
||||
require.NoError(t, err)
|
||||
|
||||
v := NewValidator(issuer, []string{audience}, srv.URL, false)
|
||||
|
||||
parsed, err := v.ValidateAndParse(context.Background(), signed)
|
||||
require.NoError(t, err)
|
||||
require.True(t, parsed.Valid)
|
||||
|
||||
claims, ok := parsed.Claims.(jwt.MapClaims)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "user-1", claims["sub"])
|
||||
|
||||
// A token signed by a different key of the same curve must be rejected.
|
||||
other, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
|
||||
forged := jwt.NewWithClaims(jwt.SigningMethodES256, jwt.MapClaims{
|
||||
"iss": issuer,
|
||||
"aud": audience,
|
||||
"sub": "user-1",
|
||||
"iat": time.Now().Add(-time.Minute).Unix(),
|
||||
"exp": time.Now().Add(time.Hour).Unix(),
|
||||
})
|
||||
forged.Header["kid"] = kid
|
||||
|
||||
forgedSigned, err := forged.SignedString(other)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = v.ValidateAndParse(context.Background(), forgedSigned)
|
||||
require.Error(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user