@@ -2,9 +2,13 @@ package provisioner
22
33import (
44 "context"
5+ "crypto/ecdh"
6+ "crypto/ecdsa"
57 "crypto/ed25519"
8+ "crypto/elliptic"
69 "crypto/x509"
710 "encoding/base64"
11+ "math/big"
812 "net"
913 "time"
1014
@@ -337,10 +341,23 @@ func (p *Nebula) authorizeToken(token string, audiences []string) (*nebula.Nebul
337341 return nil , nil , errs .Unauthorized ("token is not valid: failed to verify certificate against configured CA" )
338342 }
339343
340- var pub interface {}
341- if c .Details .IsCA {
344+ var pub any
345+ switch {
346+ case c .Details .Curve == nebula .Curve_P256 :
347+ // When Nebula is used with ECDSA P-256 keys, both CAs and clients use the same type.
348+ ecdhPub , err := ecdh .P256 ().NewPublicKey (c .Details .PublicKey )
349+ if err != nil {
350+ return nil , nil , errs .UnauthorizedErr (err , errs .WithMessage ("failed to parse nebula public key" ))
351+ }
352+ publicKeyBytes := ecdhPub .Bytes ()
353+ pub = & ecdsa.PublicKey { // convert back to *ecdsa.PublicKey, because our jose package nor go-jose supports *ecdh.PublicKey
354+ Curve : elliptic .P256 (),
355+ X : big .NewInt (0 ).SetBytes (publicKeyBytes [1 :33 ]),
356+ Y : big .NewInt (0 ).SetBytes (publicKeyBytes [33 :]),
357+ }
358+ case c .Details .IsCA :
342359 pub = ed25519 .PublicKey (c .Details .PublicKey )
343- } else {
360+ default :
344361 pub = x25519 .PublicKey (c .Details .PublicKey )
345362 }
346363
@@ -358,6 +375,7 @@ func (p *Nebula) authorizeToken(token string, audiences []string) (*nebula.Nebul
358375 }, time .Minute ); err != nil {
359376 return nil , nil , errs .UnauthorizedErr (err , errs .WithMessage ("token is not valid: invalid claims" ))
360377 }
378+
361379 // Validate token and subject too.
362380 if ! matchesAudience (claims .Audience , audiences ) {
363381 return nil , nil , errs .Unauthorized ("token is not valid: invalid claims" )
0 commit comments