mirror of
https://github.com/slackhq/nebula.git
synced 2025-12-30 10:38:28 +01:00
Support for ipv6 in the overlay with v2 certificates
--------- Co-authored-by: Jack Doan <jackdoan@rivian.com>
This commit is contained in:
@@ -2,14 +2,25 @@
|
||||
|
||||
This is a library for interacting with `nebula` style certificates and authorities.
|
||||
|
||||
A `protobuf` definition of the certificate format is also included
|
||||
There are now 2 versions of `nebula` certificates:
|
||||
|
||||
### Compiling the protobuf definition
|
||||
## v1
|
||||
|
||||
Make sure you have `protoc` installed.
|
||||
This version is deprecated.
|
||||
|
||||
A `protobuf` definition of the certificate format is included at `cert_v1.proto`
|
||||
|
||||
To compile the definition you will need `protoc` installed.
|
||||
|
||||
To compile for `go` with the same version of protobuf specified in go.mod:
|
||||
|
||||
```bash
|
||||
make
|
||||
make proto
|
||||
```
|
||||
|
||||
## v2
|
||||
|
||||
This is the latest version which uses asn.1 DER encoding. It can support ipv4 and ipv6 and tolerate
|
||||
future certificate changes better than v1.
|
||||
|
||||
`cert_v2.asn1` defines the wire format and can be used to compile marshalers.
|
||||
52
cert/asn1.go
Normal file
52
cert/asn1.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/cryptobyte"
|
||||
"golang.org/x/crypto/cryptobyte/asn1"
|
||||
)
|
||||
|
||||
// readOptionalASN1Boolean reads an asn.1 boolean with a specific tag instead of a asn.1 tag wrapping a boolean with a value
|
||||
// https://github.com/golang/go/issues/64811#issuecomment-1944446920
|
||||
func readOptionalASN1Boolean(b *cryptobyte.String, out *bool, tag asn1.Tag, defaultValue bool) bool {
|
||||
var present bool
|
||||
var child cryptobyte.String
|
||||
if !b.ReadOptionalASN1(&child, &present, tag) {
|
||||
return false
|
||||
}
|
||||
|
||||
if !present {
|
||||
*out = defaultValue
|
||||
return true
|
||||
}
|
||||
|
||||
// Ensure we have 1 byte
|
||||
if len(child) == 1 {
|
||||
*out = child[0] > 0
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// readOptionalASN1Byte reads an asn.1 uint8 with a specific tag instead of a asn.1 tag wrapping a uint8 with a value
|
||||
// Similar issue as with readOptionalASN1Boolean
|
||||
func readOptionalASN1Byte(b *cryptobyte.String, out *byte, tag asn1.Tag, defaultValue byte) bool {
|
||||
var present bool
|
||||
var child cryptobyte.String
|
||||
if !b.ReadOptionalASN1(&child, &present, tag) {
|
||||
return false
|
||||
}
|
||||
|
||||
if !present {
|
||||
*out = defaultValue
|
||||
return true
|
||||
}
|
||||
|
||||
// Ensure we have 1 byte
|
||||
if len(child) == 1 {
|
||||
*out = child[0]
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -63,31 +63,31 @@ IBNWYMep3ysx9zCgknfG5dKtwGTaqF++BWKDYdyl34KX
|
||||
|
||||
rootCA := certificateV1{
|
||||
details: detailsV1{
|
||||
Name: "nebula root ca",
|
||||
name: "nebula root ca",
|
||||
},
|
||||
}
|
||||
|
||||
rootCA01 := certificateV1{
|
||||
details: detailsV1{
|
||||
Name: "nebula root ca 01",
|
||||
name: "nebula root ca 01",
|
||||
},
|
||||
}
|
||||
|
||||
rootCAP256 := certificateV1{
|
||||
details: detailsV1{
|
||||
Name: "nebula P256 test",
|
||||
name: "nebula P256 test",
|
||||
},
|
||||
}
|
||||
|
||||
p, err := NewCAPoolFromPEM([]byte(noNewLines))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, p.CAs[string("c9bfaf7ce8e84b2eeda2e27b469f4b9617bde192efd214b68891ecda6ed49522")].Certificate.Name(), rootCA.details.Name)
|
||||
assert.Equal(t, p.CAs[string("5c9c3f23e7ee7fe97637cbd3a0a5b854154d1d9aaaf7b566a51f4a88f76b64cd")].Certificate.Name(), rootCA01.details.Name)
|
||||
assert.Equal(t, p.CAs[string("c9bfaf7ce8e84b2eeda2e27b469f4b9617bde192efd214b68891ecda6ed49522")].Certificate.Name(), rootCA.details.name)
|
||||
assert.Equal(t, p.CAs[string("5c9c3f23e7ee7fe97637cbd3a0a5b854154d1d9aaaf7b566a51f4a88f76b64cd")].Certificate.Name(), rootCA01.details.name)
|
||||
|
||||
pp, err := NewCAPoolFromPEM([]byte(withNewLines))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, pp.CAs[string("c9bfaf7ce8e84b2eeda2e27b469f4b9617bde192efd214b68891ecda6ed49522")].Certificate.Name(), rootCA.details.Name)
|
||||
assert.Equal(t, pp.CAs[string("5c9c3f23e7ee7fe97637cbd3a0a5b854154d1d9aaaf7b566a51f4a88f76b64cd")].Certificate.Name(), rootCA01.details.Name)
|
||||
assert.Equal(t, pp.CAs[string("c9bfaf7ce8e84b2eeda2e27b469f4b9617bde192efd214b68891ecda6ed49522")].Certificate.Name(), rootCA.details.name)
|
||||
assert.Equal(t, pp.CAs[string("5c9c3f23e7ee7fe97637cbd3a0a5b854154d1d9aaaf7b566a51f4a88f76b64cd")].Certificate.Name(), rootCA01.details.name)
|
||||
|
||||
// expired cert, no valid certs
|
||||
ppp, err := NewCAPoolFromPEM([]byte(expired))
|
||||
@@ -97,13 +97,13 @@ IBNWYMep3ysx9zCgknfG5dKtwGTaqF++BWKDYdyl34KX
|
||||
// expired cert, with valid certs
|
||||
pppp, err := NewCAPoolFromPEM(append([]byte(expired), noNewLines...))
|
||||
assert.Equal(t, ErrExpired, err)
|
||||
assert.Equal(t, pppp.CAs[string("c9bfaf7ce8e84b2eeda2e27b469f4b9617bde192efd214b68891ecda6ed49522")].Certificate.Name(), rootCA.details.Name)
|
||||
assert.Equal(t, pppp.CAs[string("5c9c3f23e7ee7fe97637cbd3a0a5b854154d1d9aaaf7b566a51f4a88f76b64cd")].Certificate.Name(), rootCA01.details.Name)
|
||||
assert.Equal(t, pppp.CAs[string("c9bfaf7ce8e84b2eeda2e27b469f4b9617bde192efd214b68891ecda6ed49522")].Certificate.Name(), rootCA.details.name)
|
||||
assert.Equal(t, pppp.CAs[string("5c9c3f23e7ee7fe97637cbd3a0a5b854154d1d9aaaf7b566a51f4a88f76b64cd")].Certificate.Name(), rootCA01.details.name)
|
||||
assert.Equal(t, pppp.CAs[string("152070be6bb19bc9e3bde4c2f0e7d8f4ff5448b4c9856b8eccb314fade0229b0")].Certificate.Name(), "expired")
|
||||
assert.Equal(t, len(pppp.CAs), 3)
|
||||
|
||||
ppppp, err := NewCAPoolFromPEM([]byte(p256))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, ppppp.CAs[string("a7938893ec8c4ef769b06d7f425e5e46f7a7f5ffa49c3bcf4a86b608caba9159")].Certificate.Name(), rootCAP256.details.Name)
|
||||
assert.Equal(t, ppppp.CAs[string("a7938893ec8c4ef769b06d7f425e5e46f7a7f5ffa49c3bcf4a86b608caba9159")].Certificate.Name(), rootCAP256.details.name)
|
||||
assert.Equal(t, len(ppppp.CAs), 1)
|
||||
}
|
||||
|
||||
63
cert/cert.go
63
cert/cert.go
@@ -1,15 +1,17 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Version int
|
||||
type Version uint8
|
||||
|
||||
const (
|
||||
Version1 Version = 1
|
||||
Version2 Version = 2
|
||||
VersionPre1 Version = 0
|
||||
Version1 Version = 1
|
||||
Version2 Version = 2
|
||||
)
|
||||
|
||||
type Certificate interface {
|
||||
@@ -107,23 +109,56 @@ type CachedCertificate struct {
|
||||
signerFingerprint string
|
||||
}
|
||||
|
||||
// UnmarshalCertificate will attempt to unmarshal a wire protocol level certificate.
|
||||
func UnmarshalCertificate(b []byte) (Certificate, error) {
|
||||
c, err := unmarshalCertificateV1(b, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
func (cc *CachedCertificate) String() string {
|
||||
return cc.Certificate.String()
|
||||
}
|
||||
|
||||
// UnmarshalCertificateFromHandshake will attempt to unmarshal a certificate received in a handshake.
|
||||
// RecombineAndValidate will attempt to unmarshal a certificate received in a handshake.
|
||||
// Handshakes save space by placing the peers public key in a different part of the packet, we have to
|
||||
// reassemble the actual certificate structure with that in mind.
|
||||
func UnmarshalCertificateFromHandshake(b []byte, publicKey []byte) (Certificate, error) {
|
||||
c, err := unmarshalCertificateV1(b, false)
|
||||
func RecombineAndValidate(v Version, rawCertBytes, publicKey []byte, curve Curve, caPool *CAPool) (*CachedCertificate, error) {
|
||||
if publicKey == nil {
|
||||
return nil, ErrNoPeerStaticKey
|
||||
}
|
||||
|
||||
if rawCertBytes == nil {
|
||||
return nil, ErrNoPayload
|
||||
}
|
||||
|
||||
c, err := unmarshalCertificateFromHandshake(v, rawCertBytes, publicKey, curve)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error unmarshaling cert: %w", err)
|
||||
}
|
||||
|
||||
cc, err := caPool.VerifyCertificate(time.Now(), c)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("certificate validation failed: %w", err)
|
||||
}
|
||||
|
||||
return cc, nil
|
||||
}
|
||||
|
||||
func unmarshalCertificateFromHandshake(v Version, b []byte, publicKey []byte, curve Curve) (Certificate, error) {
|
||||
var c Certificate
|
||||
var err error
|
||||
|
||||
switch v {
|
||||
case VersionPre1, Version1:
|
||||
c, err = unmarshalCertificateV1(b, publicKey)
|
||||
case Version2:
|
||||
c, err = unmarshalCertificateV2(b, publicKey, curve)
|
||||
default:
|
||||
//TODO: make a static var
|
||||
return nil, fmt.Errorf("unknown certificate version %d", v)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.details.PublicKey = publicKey
|
||||
|
||||
if c.Curve() != curve {
|
||||
return nil, fmt.Errorf("certificate curve %s does not match expected %s", c.Curve().String(), curve.String())
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
@@ -24,21 +24,21 @@ func TestMarshalingNebulaCertificate(t *testing.T) {
|
||||
|
||||
nc := certificateV1{
|
||||
details: detailsV1{
|
||||
Name: "testing",
|
||||
Ips: []netip.Prefix{
|
||||
name: "testing",
|
||||
networks: []netip.Prefix{
|
||||
mustParsePrefixUnmapped("10.1.1.1/24"),
|
||||
mustParsePrefixUnmapped("10.1.1.2/16"),
|
||||
},
|
||||
Subnets: []netip.Prefix{
|
||||
unsafeNetworks: []netip.Prefix{
|
||||
mustParsePrefixUnmapped("9.1.1.2/24"),
|
||||
mustParsePrefixUnmapped("9.1.1.3/16"),
|
||||
},
|
||||
Groups: []string{"test-group1", "test-group2", "test-group3"},
|
||||
NotBefore: before,
|
||||
NotAfter: after,
|
||||
PublicKey: pubKey,
|
||||
IsCA: false,
|
||||
Issuer: "1234567890abcedfghij1234567890ab",
|
||||
groups: []string{"test-group1", "test-group2", "test-group3"},
|
||||
notBefore: before,
|
||||
notAfter: after,
|
||||
publicKey: pubKey,
|
||||
isCA: false,
|
||||
issuer: "1234567890abcedfghij1234567890ab",
|
||||
},
|
||||
signature: []byte("1234567890abcedfghij1234567890ab"),
|
||||
}
|
||||
@@ -47,20 +47,20 @@ func TestMarshalingNebulaCertificate(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
//t.Log("Cert size:", len(b))
|
||||
|
||||
nc2, err := unmarshalCertificateV1(b, true)
|
||||
nc2, err := unmarshalCertificateV1(b, nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, nc.signature, nc2.Signature())
|
||||
assert.Equal(t, nc.details.Name, nc2.Name())
|
||||
assert.Equal(t, nc.details.NotBefore, nc2.NotBefore())
|
||||
assert.Equal(t, nc.details.NotAfter, nc2.NotAfter())
|
||||
assert.Equal(t, nc.details.PublicKey, nc2.PublicKey())
|
||||
assert.Equal(t, nc.details.IsCA, nc2.IsCA())
|
||||
assert.Equal(t, nc.details.name, nc2.Name())
|
||||
assert.Equal(t, nc.details.notBefore, nc2.NotBefore())
|
||||
assert.Equal(t, nc.details.notAfter, nc2.NotAfter())
|
||||
assert.Equal(t, nc.details.publicKey, nc2.PublicKey())
|
||||
assert.Equal(t, nc.details.isCA, nc2.IsCA())
|
||||
|
||||
assert.Equal(t, nc.details.Ips, nc2.Networks())
|
||||
assert.Equal(t, nc.details.Subnets, nc2.UnsafeNetworks())
|
||||
assert.Equal(t, nc.details.networks, nc2.Networks())
|
||||
assert.Equal(t, nc.details.unsafeNetworks, nc2.UnsafeNetworks())
|
||||
|
||||
assert.Equal(t, nc.details.Groups, nc2.Groups())
|
||||
assert.Equal(t, nc.details.groups, nc2.Groups())
|
||||
}
|
||||
|
||||
//func TestNebulaCertificate_Sign(t *testing.T) {
|
||||
@@ -150,8 +150,8 @@ func TestMarshalingNebulaCertificate(t *testing.T) {
|
||||
func TestNebulaCertificate_Expired(t *testing.T) {
|
||||
nc := certificateV1{
|
||||
details: detailsV1{
|
||||
NotBefore: time.Now().Add(time.Second * -60).Round(time.Second),
|
||||
NotAfter: time.Now().Add(time.Second * 60).Round(time.Second),
|
||||
notBefore: time.Now().Add(time.Second * -60).Round(time.Second),
|
||||
notAfter: time.Now().Add(time.Second * 60).Round(time.Second),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -166,21 +166,21 @@ func TestNebulaCertificate_MarshalJSON(t *testing.T) {
|
||||
|
||||
nc := certificateV1{
|
||||
details: detailsV1{
|
||||
Name: "testing",
|
||||
Ips: []netip.Prefix{
|
||||
name: "testing",
|
||||
networks: []netip.Prefix{
|
||||
mustParsePrefixUnmapped("10.1.1.1/24"),
|
||||
mustParsePrefixUnmapped("10.1.1.2/16"),
|
||||
},
|
||||
Subnets: []netip.Prefix{
|
||||
unsafeNetworks: []netip.Prefix{
|
||||
mustParsePrefixUnmapped("9.1.1.2/24"),
|
||||
mustParsePrefixUnmapped("9.1.1.3/16"),
|
||||
},
|
||||
Groups: []string{"test-group1", "test-group2", "test-group3"},
|
||||
NotBefore: time.Date(1, 0, 0, 1, 0, 0, 0, time.UTC),
|
||||
NotAfter: time.Date(1, 0, 0, 2, 0, 0, 0, time.UTC),
|
||||
PublicKey: pubKey,
|
||||
IsCA: false,
|
||||
Issuer: "1234567890abcedfghij1234567890ab",
|
||||
groups: []string{"test-group1", "test-group2", "test-group3"},
|
||||
notBefore: time.Date(1, 0, 0, 1, 0, 0, 0, time.UTC),
|
||||
notAfter: time.Date(1, 0, 0, 2, 0, 0, 0, time.UTC),
|
||||
publicKey: pubKey,
|
||||
isCA: false,
|
||||
issuer: "1234567890abcedfghij1234567890ab",
|
||||
},
|
||||
signature: []byte("1234567890abcedfghij1234567890ab"),
|
||||
}
|
||||
@@ -189,7 +189,7 @@ func TestNebulaCertificate_MarshalJSON(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(
|
||||
t,
|
||||
"{\"details\":{\"curve\":\"CURVE25519\",\"groups\":[\"test-group1\",\"test-group2\",\"test-group3\"],\"ips\":[\"10.1.1.1/24\",\"10.1.1.2/16\"],\"isCa\":false,\"issuer\":\"1234567890abcedfghij1234567890ab\",\"name\":\"testing\",\"notAfter\":\"0000-11-30T02:00:00Z\",\"notBefore\":\"0000-11-30T01:00:00Z\",\"publicKey\":\"313233343536373839306162636564666768696a313233343536373839306162\",\"subnets\":[\"9.1.1.2/24\",\"9.1.1.3/16\"]},\"fingerprint\":\"3944c53d4267a229295b56cb2d27d459164c010ac97d655063ba421e0670f4ba\",\"signature\":\"313233343536373839306162636564666768696a313233343536373839306162\"}",
|
||||
"{\"details\":{\"curve\":\"CURVE25519\",\"groups\":[\"test-group1\",\"test-group2\",\"test-group3\"],\"isCa\":false,\"issuer\":\"1234567890abcedfghij1234567890ab\",\"name\":\"testing\",\"networks\":[\"10.1.1.1/24\",\"10.1.1.2/16\"],\"notAfter\":\"0000-11-30T02:00:00Z\",\"notBefore\":\"0000-11-30T01:00:00Z\",\"publicKey\":\"313233343536373839306162636564666768696a313233343536373839306162\",\"unsafeNetworks\":[\"9.1.1.2/24\",\"9.1.1.3/16\"]},\"fingerprint\":\"3944c53d4267a229295b56cb2d27d459164c010ac97d655063ba421e0670f4ba\",\"signature\":\"313233343536373839306162636564666768696a313233343536373839306162\",\"version\":1}",
|
||||
string(b),
|
||||
)
|
||||
}
|
||||
@@ -526,7 +526,7 @@ func TestNebulaCertificate_Copy(t *testing.T) {
|
||||
func TestUnmarshalNebulaCertificate(t *testing.T) {
|
||||
// Test that we don't panic with an invalid certificate (#332)
|
||||
data := []byte("\x98\x00\x00")
|
||||
_, err := unmarshalCertificateV1(data, true)
|
||||
_, err := unmarshalCertificateV1(data, nil)
|
||||
assert.EqualError(t, err, "encoded Details was nil")
|
||||
}
|
||||
|
||||
|
||||
406
cert/cert_v1.go
406
cert/cert_v1.go
@@ -6,19 +6,16 @@ import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/ed25519"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/slackhq/nebula/pkclient"
|
||||
"golang.org/x/crypto/curve25519"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
@@ -31,71 +28,71 @@ type certificateV1 struct {
|
||||
}
|
||||
|
||||
type detailsV1 struct {
|
||||
Name string
|
||||
Ips []netip.Prefix
|
||||
Subnets []netip.Prefix
|
||||
Groups []string
|
||||
NotBefore time.Time
|
||||
NotAfter time.Time
|
||||
PublicKey []byte
|
||||
IsCA bool
|
||||
Issuer string
|
||||
name string
|
||||
networks []netip.Prefix
|
||||
unsafeNetworks []netip.Prefix
|
||||
groups []string
|
||||
notBefore time.Time
|
||||
notAfter time.Time
|
||||
publicKey []byte
|
||||
isCA bool
|
||||
issuer string
|
||||
|
||||
Curve Curve
|
||||
curve Curve
|
||||
}
|
||||
|
||||
type m map[string]interface{}
|
||||
|
||||
func (nc *certificateV1) Version() Version {
|
||||
func (c *certificateV1) Version() Version {
|
||||
return Version1
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Curve() Curve {
|
||||
return nc.details.Curve
|
||||
func (c *certificateV1) Curve() Curve {
|
||||
return c.details.curve
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Groups() []string {
|
||||
return nc.details.Groups
|
||||
func (c *certificateV1) Groups() []string {
|
||||
return c.details.groups
|
||||
}
|
||||
|
||||
func (nc *certificateV1) IsCA() bool {
|
||||
return nc.details.IsCA
|
||||
func (c *certificateV1) IsCA() bool {
|
||||
return c.details.isCA
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Issuer() string {
|
||||
return nc.details.Issuer
|
||||
func (c *certificateV1) Issuer() string {
|
||||
return c.details.issuer
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Name() string {
|
||||
return nc.details.Name
|
||||
func (c *certificateV1) Name() string {
|
||||
return c.details.name
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Networks() []netip.Prefix {
|
||||
return nc.details.Ips
|
||||
func (c *certificateV1) Networks() []netip.Prefix {
|
||||
return c.details.networks
|
||||
}
|
||||
|
||||
func (nc *certificateV1) NotAfter() time.Time {
|
||||
return nc.details.NotAfter
|
||||
func (c *certificateV1) NotAfter() time.Time {
|
||||
return c.details.notAfter
|
||||
}
|
||||
|
||||
func (nc *certificateV1) NotBefore() time.Time {
|
||||
return nc.details.NotBefore
|
||||
func (c *certificateV1) NotBefore() time.Time {
|
||||
return c.details.notBefore
|
||||
}
|
||||
|
||||
func (nc *certificateV1) PublicKey() []byte {
|
||||
return nc.details.PublicKey
|
||||
func (c *certificateV1) PublicKey() []byte {
|
||||
return c.details.publicKey
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Signature() []byte {
|
||||
return nc.signature
|
||||
func (c *certificateV1) Signature() []byte {
|
||||
return c.signature
|
||||
}
|
||||
|
||||
func (nc *certificateV1) UnsafeNetworks() []netip.Prefix {
|
||||
return nc.details.Subnets
|
||||
func (c *certificateV1) UnsafeNetworks() []netip.Prefix {
|
||||
return c.details.unsafeNetworks
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Fingerprint() (string, error) {
|
||||
b, err := nc.Marshal()
|
||||
func (c *certificateV1) Fingerprint() (string, error) {
|
||||
b, err := c.Marshal()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -104,33 +101,33 @@ func (nc *certificateV1) Fingerprint() (string, error) {
|
||||
return hex.EncodeToString(sum[:]), nil
|
||||
}
|
||||
|
||||
func (nc *certificateV1) CheckSignature(key []byte) bool {
|
||||
b, err := proto.Marshal(nc.getRawDetails())
|
||||
func (c *certificateV1) CheckSignature(key []byte) bool {
|
||||
b, err := proto.Marshal(c.getRawDetails())
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
switch nc.details.Curve {
|
||||
switch c.details.curve {
|
||||
case Curve_CURVE25519:
|
||||
return ed25519.Verify(key, b, nc.signature)
|
||||
return ed25519.Verify(key, b, c.signature)
|
||||
case Curve_P256:
|
||||
x, y := elliptic.Unmarshal(elliptic.P256(), key)
|
||||
pubKey := &ecdsa.PublicKey{Curve: elliptic.P256(), X: x, Y: y}
|
||||
hashed := sha256.Sum256(b)
|
||||
return ecdsa.VerifyASN1(pubKey, hashed[:], nc.signature)
|
||||
return ecdsa.VerifyASN1(pubKey, hashed[:], c.signature)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Expired(t time.Time) bool {
|
||||
return nc.details.NotBefore.After(t) || nc.details.NotAfter.Before(t)
|
||||
func (c *certificateV1) Expired(t time.Time) bool {
|
||||
return c.details.notBefore.After(t) || c.details.notAfter.Before(t)
|
||||
}
|
||||
|
||||
func (nc *certificateV1) VerifyPrivateKey(curve Curve, key []byte) error {
|
||||
if curve != nc.details.Curve {
|
||||
func (c *certificateV1) VerifyPrivateKey(curve Curve, key []byte) error {
|
||||
if curve != c.details.curve {
|
||||
return fmt.Errorf("curve in cert and private key supplied don't match")
|
||||
}
|
||||
if nc.details.IsCA {
|
||||
if c.details.isCA {
|
||||
switch curve {
|
||||
case Curve_CURVE25519:
|
||||
// the call to PublicKey below will panic slice bounds out of range otherwise
|
||||
@@ -138,7 +135,7 @@ func (nc *certificateV1) VerifyPrivateKey(curve Curve, key []byte) error {
|
||||
return fmt.Errorf("key was not 64 bytes, is invalid ed25519 private key")
|
||||
}
|
||||
|
||||
if !ed25519.PublicKey(nc.details.PublicKey).Equal(ed25519.PrivateKey(key).Public()) {
|
||||
if !ed25519.PublicKey(c.details.publicKey).Equal(ed25519.PrivateKey(key).Public()) {
|
||||
return fmt.Errorf("public key in cert and private key supplied don't match")
|
||||
}
|
||||
case Curve_P256:
|
||||
@@ -147,7 +144,7 @@ func (nc *certificateV1) VerifyPrivateKey(curve Curve, key []byte) error {
|
||||
return fmt.Errorf("cannot parse private key as P256: %w", err)
|
||||
}
|
||||
pub := privkey.PublicKey().Bytes()
|
||||
if !bytes.Equal(pub, nc.details.PublicKey) {
|
||||
if !bytes.Equal(pub, c.details.publicKey) {
|
||||
return fmt.Errorf("public key in cert and private key supplied don't match")
|
||||
}
|
||||
default:
|
||||
@@ -173,7 +170,7 @@ func (nc *certificateV1) VerifyPrivateKey(curve Curve, key []byte) error {
|
||||
default:
|
||||
return fmt.Errorf("invalid curve: %s", curve)
|
||||
}
|
||||
if !bytes.Equal(pub, nc.details.PublicKey) {
|
||||
if !bytes.Equal(pub, c.details.publicKey) {
|
||||
return fmt.Errorf("public key in cert and private key supplied don't match")
|
||||
}
|
||||
|
||||
@@ -181,173 +178,155 @@ func (nc *certificateV1) VerifyPrivateKey(curve Curve, key []byte) error {
|
||||
}
|
||||
|
||||
// getRawDetails marshals the raw details into protobuf ready struct
|
||||
func (nc *certificateV1) getRawDetails() *RawNebulaCertificateDetails {
|
||||
func (c *certificateV1) getRawDetails() *RawNebulaCertificateDetails {
|
||||
rd := &RawNebulaCertificateDetails{
|
||||
Name: nc.details.Name,
|
||||
Groups: nc.details.Groups,
|
||||
NotBefore: nc.details.NotBefore.Unix(),
|
||||
NotAfter: nc.details.NotAfter.Unix(),
|
||||
PublicKey: make([]byte, len(nc.details.PublicKey)),
|
||||
IsCA: nc.details.IsCA,
|
||||
Curve: nc.details.Curve,
|
||||
Name: c.details.name,
|
||||
Groups: c.details.groups,
|
||||
NotBefore: c.details.notBefore.Unix(),
|
||||
NotAfter: c.details.notAfter.Unix(),
|
||||
PublicKey: make([]byte, len(c.details.publicKey)),
|
||||
IsCA: c.details.isCA,
|
||||
Curve: c.details.curve,
|
||||
}
|
||||
|
||||
for _, ipNet := range nc.details.Ips {
|
||||
for _, ipNet := range c.details.networks {
|
||||
mask := net.CIDRMask(ipNet.Bits(), ipNet.Addr().BitLen())
|
||||
rd.Ips = append(rd.Ips, addr2int(ipNet.Addr()), ip2int(mask))
|
||||
}
|
||||
|
||||
for _, ipNet := range nc.details.Subnets {
|
||||
for _, ipNet := range c.details.unsafeNetworks {
|
||||
mask := net.CIDRMask(ipNet.Bits(), ipNet.Addr().BitLen())
|
||||
rd.Subnets = append(rd.Subnets, addr2int(ipNet.Addr()), ip2int(mask))
|
||||
}
|
||||
|
||||
copy(rd.PublicKey, nc.details.PublicKey[:])
|
||||
copy(rd.PublicKey, c.details.publicKey[:])
|
||||
|
||||
// I know, this is terrible
|
||||
rd.Issuer, _ = hex.DecodeString(nc.details.Issuer)
|
||||
rd.Issuer, _ = hex.DecodeString(c.details.issuer)
|
||||
|
||||
return rd
|
||||
}
|
||||
|
||||
func (nc *certificateV1) String() string {
|
||||
if nc == nil {
|
||||
return "Certificate {}\n"
|
||||
func (c *certificateV1) String() string {
|
||||
b, err := json.MarshalIndent(c.marshalJSON(), "", "\t")
|
||||
if err != nil {
|
||||
return "<error marshalling certificate>"
|
||||
}
|
||||
|
||||
s := "NebulaCertificate {\n"
|
||||
s += "\tDetails {\n"
|
||||
s += fmt.Sprintf("\t\tName: %v\n", nc.details.Name)
|
||||
|
||||
if len(nc.details.Ips) > 0 {
|
||||
s += "\t\tIps: [\n"
|
||||
for _, ip := range nc.details.Ips {
|
||||
s += fmt.Sprintf("\t\t\t%v\n", ip.String())
|
||||
}
|
||||
s += "\t\t]\n"
|
||||
} else {
|
||||
s += "\t\tIps: []\n"
|
||||
}
|
||||
|
||||
if len(nc.details.Subnets) > 0 {
|
||||
s += "\t\tSubnets: [\n"
|
||||
for _, ip := range nc.details.Subnets {
|
||||
s += fmt.Sprintf("\t\t\t%v\n", ip.String())
|
||||
}
|
||||
s += "\t\t]\n"
|
||||
} else {
|
||||
s += "\t\tSubnets: []\n"
|
||||
}
|
||||
|
||||
if len(nc.details.Groups) > 0 {
|
||||
s += "\t\tGroups: [\n"
|
||||
for _, g := range nc.details.Groups {
|
||||
s += fmt.Sprintf("\t\t\t\"%v\"\n", g)
|
||||
}
|
||||
s += "\t\t]\n"
|
||||
} else {
|
||||
s += "\t\tGroups: []\n"
|
||||
}
|
||||
|
||||
s += fmt.Sprintf("\t\tNot before: %v\n", nc.details.NotBefore)
|
||||
s += fmt.Sprintf("\t\tNot After: %v\n", nc.details.NotAfter)
|
||||
s += fmt.Sprintf("\t\tIs CA: %v\n", nc.details.IsCA)
|
||||
s += fmt.Sprintf("\t\tIssuer: %s\n", nc.details.Issuer)
|
||||
s += fmt.Sprintf("\t\tPublic key: %x\n", nc.details.PublicKey)
|
||||
s += fmt.Sprintf("\t\tCurve: %s\n", nc.details.Curve)
|
||||
s += "\t}\n"
|
||||
fp, err := nc.Fingerprint()
|
||||
if err == nil {
|
||||
s += fmt.Sprintf("\tFingerprint: %s\n", fp)
|
||||
}
|
||||
s += fmt.Sprintf("\tSignature: %x\n", nc.Signature())
|
||||
s += "}"
|
||||
|
||||
return s
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (nc *certificateV1) MarshalForHandshakes() ([]byte, error) {
|
||||
pubKey := nc.details.PublicKey
|
||||
nc.details.PublicKey = nil
|
||||
rawCertNoKey, err := nc.Marshal()
|
||||
func (c *certificateV1) MarshalForHandshakes() ([]byte, error) {
|
||||
pubKey := c.details.publicKey
|
||||
c.details.publicKey = nil
|
||||
rawCertNoKey, err := c.Marshal()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nc.details.PublicKey = pubKey
|
||||
c.details.publicKey = pubKey
|
||||
return rawCertNoKey, nil
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Marshal() ([]byte, error) {
|
||||
func (c *certificateV1) Marshal() ([]byte, error) {
|
||||
rc := RawNebulaCertificate{
|
||||
Details: nc.getRawDetails(),
|
||||
Signature: nc.signature,
|
||||
Details: c.getRawDetails(),
|
||||
Signature: c.signature,
|
||||
}
|
||||
|
||||
return proto.Marshal(&rc)
|
||||
}
|
||||
|
||||
func (nc *certificateV1) MarshalPEM() ([]byte, error) {
|
||||
b, err := nc.Marshal()
|
||||
func (c *certificateV1) MarshalPEM() ([]byte, error) {
|
||||
b, err := c.Marshal()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pem.EncodeToMemory(&pem.Block{Type: CertificateBanner, Bytes: b}), nil
|
||||
}
|
||||
|
||||
func (nc *certificateV1) MarshalJSON() ([]byte, error) {
|
||||
fp, _ := nc.Fingerprint()
|
||||
jc := m{
|
||||
"details": m{
|
||||
"name": nc.details.Name,
|
||||
"ips": nc.details.Ips,
|
||||
"subnets": nc.details.Subnets,
|
||||
"groups": nc.details.Groups,
|
||||
"notBefore": nc.details.NotBefore,
|
||||
"notAfter": nc.details.NotAfter,
|
||||
"publicKey": fmt.Sprintf("%x", nc.details.PublicKey),
|
||||
"isCa": nc.details.IsCA,
|
||||
"issuer": nc.details.Issuer,
|
||||
"curve": nc.details.Curve.String(),
|
||||
},
|
||||
"fingerprint": fp,
|
||||
"signature": fmt.Sprintf("%x", nc.Signature()),
|
||||
}
|
||||
return json.Marshal(jc)
|
||||
func (c *certificateV1) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(c.marshalJSON())
|
||||
}
|
||||
|
||||
func (nc *certificateV1) Copy() Certificate {
|
||||
c := &certificateV1{
|
||||
details: detailsV1{
|
||||
Name: nc.details.Name,
|
||||
Groups: make([]string, len(nc.details.Groups)),
|
||||
Ips: make([]netip.Prefix, len(nc.details.Ips)),
|
||||
Subnets: make([]netip.Prefix, len(nc.details.Subnets)),
|
||||
NotBefore: nc.details.NotBefore,
|
||||
NotAfter: nc.details.NotAfter,
|
||||
PublicKey: make([]byte, len(nc.details.PublicKey)),
|
||||
IsCA: nc.details.IsCA,
|
||||
Issuer: nc.details.Issuer,
|
||||
func (c *certificateV1) marshalJSON() m {
|
||||
fp, _ := c.Fingerprint()
|
||||
return m{
|
||||
"version": Version1,
|
||||
"details": m{
|
||||
"name": c.details.name,
|
||||
"networks": c.details.networks,
|
||||
"unsafeNetworks": c.details.unsafeNetworks,
|
||||
"groups": c.details.groups,
|
||||
"notBefore": c.details.notBefore,
|
||||
"notAfter": c.details.notAfter,
|
||||
"publicKey": fmt.Sprintf("%x", c.details.publicKey),
|
||||
"isCa": c.details.isCA,
|
||||
"issuer": c.details.issuer,
|
||||
"curve": c.details.curve.String(),
|
||||
},
|
||||
signature: make([]byte, len(nc.signature)),
|
||||
"fingerprint": fp,
|
||||
"signature": fmt.Sprintf("%x", c.Signature()),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *certificateV1) Copy() Certificate {
|
||||
nc := &certificateV1{
|
||||
details: detailsV1{
|
||||
name: c.details.name,
|
||||
groups: make([]string, len(c.details.groups)),
|
||||
networks: make([]netip.Prefix, len(c.details.networks)),
|
||||
unsafeNetworks: make([]netip.Prefix, len(c.details.unsafeNetworks)),
|
||||
notBefore: c.details.notBefore,
|
||||
notAfter: c.details.notAfter,
|
||||
publicKey: make([]byte, len(c.details.publicKey)),
|
||||
isCA: c.details.isCA,
|
||||
issuer: c.details.issuer,
|
||||
curve: c.details.curve,
|
||||
},
|
||||
signature: make([]byte, len(c.signature)),
|
||||
}
|
||||
|
||||
copy(c.signature, nc.signature)
|
||||
copy(c.details.Groups, nc.details.Groups)
|
||||
copy(c.details.PublicKey, nc.details.PublicKey)
|
||||
copy(nc.signature, c.signature)
|
||||
copy(nc.details.groups, c.details.groups)
|
||||
copy(nc.details.publicKey, c.details.publicKey)
|
||||
copy(nc.details.networks, c.details.networks)
|
||||
copy(nc.details.unsafeNetworks, c.details.unsafeNetworks)
|
||||
|
||||
for i, p := range nc.details.Ips {
|
||||
c.details.Ips[i] = p
|
||||
return nc
|
||||
}
|
||||
|
||||
func (c *certificateV1) fromTBSCertificate(t *TBSCertificate) error {
|
||||
c.details = detailsV1{
|
||||
name: t.Name,
|
||||
networks: t.Networks,
|
||||
unsafeNetworks: t.UnsafeNetworks,
|
||||
groups: t.Groups,
|
||||
notBefore: t.NotBefore,
|
||||
notAfter: t.NotAfter,
|
||||
publicKey: t.PublicKey,
|
||||
isCA: t.IsCA,
|
||||
curve: t.Curve,
|
||||
issuer: t.issuer,
|
||||
}
|
||||
|
||||
for i, p := range nc.details.Subnets {
|
||||
c.details.Subnets[i] = p
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return c
|
||||
func (c *certificateV1) marshalForSigning() ([]byte, error) {
|
||||
b, err := proto.Marshal(c.getRawDetails())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (c *certificateV1) setSignature(b []byte) error {
|
||||
c.signature = b
|
||||
return nil
|
||||
}
|
||||
|
||||
// unmarshalCertificateV1 will unmarshal a protobuf byte representation of a nebula cert
|
||||
func unmarshalCertificateV1(b []byte, assertPublicKey bool) (*certificateV1, error) {
|
||||
// if the publicKey is provided here then it is not required to be present in `b`
|
||||
func unmarshalCertificateV1(b []byte, publicKey []byte) (*certificateV1, error) {
|
||||
if len(b) == 0 {
|
||||
return nil, fmt.Errorf("nil byte array")
|
||||
}
|
||||
@@ -371,27 +350,28 @@ func unmarshalCertificateV1(b []byte, assertPublicKey bool) (*certificateV1, err
|
||||
|
||||
nc := certificateV1{
|
||||
details: detailsV1{
|
||||
Name: rc.Details.Name,
|
||||
Groups: make([]string, len(rc.Details.Groups)),
|
||||
Ips: make([]netip.Prefix, len(rc.Details.Ips)/2),
|
||||
Subnets: make([]netip.Prefix, len(rc.Details.Subnets)/2),
|
||||
NotBefore: time.Unix(rc.Details.NotBefore, 0),
|
||||
NotAfter: time.Unix(rc.Details.NotAfter, 0),
|
||||
PublicKey: make([]byte, len(rc.Details.PublicKey)),
|
||||
IsCA: rc.Details.IsCA,
|
||||
Curve: rc.Details.Curve,
|
||||
name: rc.Details.Name,
|
||||
groups: make([]string, len(rc.Details.Groups)),
|
||||
networks: make([]netip.Prefix, len(rc.Details.Ips)/2),
|
||||
unsafeNetworks: make([]netip.Prefix, len(rc.Details.Subnets)/2),
|
||||
notBefore: time.Unix(rc.Details.NotBefore, 0),
|
||||
notAfter: time.Unix(rc.Details.NotAfter, 0),
|
||||
publicKey: make([]byte, len(rc.Details.PublicKey)),
|
||||
isCA: rc.Details.IsCA,
|
||||
curve: rc.Details.Curve,
|
||||
},
|
||||
signature: make([]byte, len(rc.Signature)),
|
||||
}
|
||||
|
||||
copy(nc.signature, rc.Signature)
|
||||
copy(nc.details.Groups, rc.Details.Groups)
|
||||
nc.details.Issuer = hex.EncodeToString(rc.Details.Issuer)
|
||||
copy(nc.details.groups, rc.Details.Groups)
|
||||
nc.details.issuer = hex.EncodeToString(rc.Details.Issuer)
|
||||
|
||||
if len(rc.Details.PublicKey) < publicKeyLen && assertPublicKey {
|
||||
return nil, fmt.Errorf("public key was fewer than 32 bytes; %v", len(rc.Details.PublicKey))
|
||||
if len(publicKey) > 0 {
|
||||
nc.details.publicKey = publicKey
|
||||
}
|
||||
copy(nc.details.PublicKey, rc.Details.PublicKey)
|
||||
|
||||
copy(nc.details.publicKey, rc.Details.PublicKey)
|
||||
|
||||
var ip netip.Addr
|
||||
for i, rawIp := range rc.Details.Ips {
|
||||
@@ -399,7 +379,7 @@ func unmarshalCertificateV1(b []byte, assertPublicKey bool) (*certificateV1, err
|
||||
ip = int2addr(rawIp)
|
||||
} else {
|
||||
ones, _ := net.IPMask(int2ip(rawIp)).Size()
|
||||
nc.details.Ips[i/2] = netip.PrefixFrom(ip, ones)
|
||||
nc.details.networks[i/2] = netip.PrefixFrom(ip, ones)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,69 +388,15 @@ func unmarshalCertificateV1(b []byte, assertPublicKey bool) (*certificateV1, err
|
||||
ip = int2addr(rawIp)
|
||||
} else {
|
||||
ones, _ := net.IPMask(int2ip(rawIp)).Size()
|
||||
nc.details.Subnets[i/2] = netip.PrefixFrom(ip, ones)
|
||||
nc.details.unsafeNetworks[i/2] = netip.PrefixFrom(ip, ones)
|
||||
}
|
||||
}
|
||||
|
||||
//do not sort the subnets field for V1 certs
|
||||
|
||||
return &nc, nil
|
||||
}
|
||||
|
||||
func signV1(t *TBSCertificate, curve Curve, key []byte, client *pkclient.PKClient) (*certificateV1, error) {
|
||||
c := &certificateV1{
|
||||
details: detailsV1{
|
||||
Name: t.Name,
|
||||
Ips: t.Networks,
|
||||
Subnets: t.UnsafeNetworks,
|
||||
Groups: t.Groups,
|
||||
NotBefore: t.NotBefore,
|
||||
NotAfter: t.NotAfter,
|
||||
PublicKey: t.PublicKey,
|
||||
IsCA: t.IsCA,
|
||||
Curve: t.Curve,
|
||||
Issuer: t.issuer,
|
||||
},
|
||||
}
|
||||
b, err := proto.Marshal(c.getRawDetails())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var sig []byte
|
||||
|
||||
switch curve {
|
||||
case Curve_CURVE25519:
|
||||
signer := ed25519.PrivateKey(key)
|
||||
sig = ed25519.Sign(signer, b)
|
||||
case Curve_P256:
|
||||
if client != nil {
|
||||
sig, err = client.SignASN1(b)
|
||||
} else {
|
||||
signer := &ecdsa.PrivateKey{
|
||||
PublicKey: ecdsa.PublicKey{
|
||||
Curve: elliptic.P256(),
|
||||
},
|
||||
// ref: https://github.com/golang/go/blob/go1.19/src/crypto/x509/sec1.go#L95
|
||||
D: new(big.Int).SetBytes(key),
|
||||
}
|
||||
// ref: https://github.com/golang/go/blob/go1.19/src/crypto/x509/sec1.go#L119
|
||||
signer.X, signer.Y = signer.Curve.ScalarBaseMult(key)
|
||||
|
||||
// We need to hash first for ECDSA
|
||||
// - https://pkg.go.dev/crypto/ecdsa#SignASN1
|
||||
hashed := sha256.Sum256(b)
|
||||
sig, err = ecdsa.SignASN1(rand.Reader, signer, hashed[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid curve: %s", c.details.Curve)
|
||||
}
|
||||
|
||||
c.signature = sig
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func ip2int(ip []byte) uint32 {
|
||||
if len(ip) == 16 {
|
||||
return binary.BigEndian.Uint32(ip[12:16])
|
||||
|
||||
37
cert/cert_v2.asn1
Normal file
37
cert/cert_v2.asn1
Normal file
@@ -0,0 +1,37 @@
|
||||
Nebula DEFINITIONS AUTOMATIC TAGS ::= BEGIN
|
||||
|
||||
Name ::= UTF8String (SIZE (1..253))
|
||||
Time ::= INTEGER (0..18446744073709551615) -- Seconds since unix epoch, uint64 maximum
|
||||
Network ::= OCTET STRING (SIZE (5,17)) -- IP addresses are 4 or 16 bytes + 1 byte for the prefix length
|
||||
Curve ::= ENUMERATED {
|
||||
curve25519 (0),
|
||||
p256 (1)
|
||||
}
|
||||
|
||||
-- The maximum size of a certificate must not exceed 65536 bytes
|
||||
Certificate ::= SEQUENCE {
|
||||
details OCTET STRING,
|
||||
curve Curve DEFAULT curve25519,
|
||||
publicKey OCTET STRING,
|
||||
-- signature(details + curve + publicKey) using the appropriate method for curve
|
||||
signature OCTET STRING
|
||||
}
|
||||
|
||||
Details ::= SEQUENCE {
|
||||
name Name,
|
||||
|
||||
-- At least 1 ipv4 or ipv6 address must be present if isCA is false
|
||||
networks SEQUENCE OF Network OPTIONAL,
|
||||
unsafeNetworks SEQUENCE OF Network OPTIONAL,
|
||||
groups SEQUENCE OF Name OPTIONAL,
|
||||
isCA BOOLEAN DEFAULT false,
|
||||
notBefore Time,
|
||||
notAfter Time,
|
||||
|
||||
-- issuer is only required if isCA is false, if isCA is true then it must not be present
|
||||
issuer OCTET STRING OPTIONAL,
|
||||
...
|
||||
-- New fields can be added below here
|
||||
}
|
||||
|
||||
END
|
||||
621
cert/cert_v2.go
Normal file
621
cert/cert_v2.go
Normal file
@@ -0,0 +1,621 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ecdh"
|
||||
"crypto/ecdsa"
|
||||
"crypto/ed25519"
|
||||
"crypto/elliptic"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/cryptobyte"
|
||||
"golang.org/x/crypto/cryptobyte/asn1"
|
||||
"golang.org/x/crypto/curve25519"
|
||||
)
|
||||
|
||||
//TODO: should we avoid hex encoding shit on output? Just let it be base64?
|
||||
|
||||
const (
|
||||
classConstructed = 0x20
|
||||
classContextSpecific = 0x80
|
||||
|
||||
TagCertDetails = 0 | classConstructed | classContextSpecific
|
||||
TagCertCurve = 1 | classContextSpecific
|
||||
TagCertPublicKey = 2 | classContextSpecific
|
||||
TagCertSignature = 3 | classContextSpecific
|
||||
|
||||
TagDetailsName = 0 | classContextSpecific
|
||||
TagDetailsNetworks = 1 | classConstructed | classContextSpecific
|
||||
TagDetailsUnsafeNetworks = 2 | classConstructed | classContextSpecific
|
||||
TagDetailsGroups = 3 | classConstructed | classContextSpecific
|
||||
TagDetailsIsCA = 4 | classContextSpecific
|
||||
TagDetailsNotBefore = 5 | classContextSpecific
|
||||
TagDetailsNotAfter = 6 | classContextSpecific
|
||||
TagDetailsIssuer = 7 | classContextSpecific
|
||||
)
|
||||
|
||||
const (
|
||||
// MaxCertificateSize is the maximum length a valid certificate can be
|
||||
MaxCertificateSize = 65536
|
||||
|
||||
// MaxNameLength is limited to a maximum realistic DNS domain name to help facilitate DNS systems
|
||||
MaxNameLength = 253
|
||||
|
||||
// MaxNetworkLength is the maximum length a network value can be.
|
||||
// 16 bytes for an ipv6 address + 1 byte for the prefix length
|
||||
MaxNetworkLength = 17
|
||||
)
|
||||
|
||||
type certificateV2 struct {
|
||||
details detailsV2
|
||||
|
||||
// RawDetails contains the entire asn.1 DER encoded Details struct
|
||||
// This is to benefit forwards compatibility in signature checking.
|
||||
// signature(RawDetails + Curve + PublicKey) == Signature
|
||||
rawDetails []byte
|
||||
curve Curve
|
||||
publicKey []byte
|
||||
signature []byte
|
||||
}
|
||||
|
||||
type detailsV2 struct {
|
||||
name string
|
||||
networks []netip.Prefix
|
||||
unsafeNetworks []netip.Prefix
|
||||
groups []string
|
||||
isCA bool
|
||||
notBefore time.Time
|
||||
notAfter time.Time
|
||||
issuer string
|
||||
}
|
||||
|
||||
func (c *certificateV2) Version() Version {
|
||||
return Version2
|
||||
}
|
||||
|
||||
func (c *certificateV2) Curve() Curve {
|
||||
return c.curve
|
||||
}
|
||||
|
||||
func (c *certificateV2) Groups() []string {
|
||||
return c.details.groups
|
||||
}
|
||||
|
||||
func (c *certificateV2) IsCA() bool {
|
||||
return c.details.isCA
|
||||
}
|
||||
|
||||
func (c *certificateV2) Issuer() string {
|
||||
return c.details.issuer
|
||||
}
|
||||
|
||||
func (c *certificateV2) Name() string {
|
||||
return c.details.name
|
||||
}
|
||||
|
||||
func (c *certificateV2) Networks() []netip.Prefix {
|
||||
return c.details.networks
|
||||
}
|
||||
|
||||
func (c *certificateV2) NotAfter() time.Time {
|
||||
return c.details.notAfter
|
||||
}
|
||||
|
||||
func (c *certificateV2) NotBefore() time.Time {
|
||||
return c.details.notBefore
|
||||
}
|
||||
|
||||
func (c *certificateV2) PublicKey() []byte {
|
||||
return c.publicKey
|
||||
}
|
||||
|
||||
func (c *certificateV2) Signature() []byte {
|
||||
return c.signature
|
||||
}
|
||||
|
||||
func (c *certificateV2) UnsafeNetworks() []netip.Prefix {
|
||||
return c.details.unsafeNetworks
|
||||
}
|
||||
|
||||
func (c *certificateV2) Fingerprint() (string, error) {
|
||||
b := make([]byte, len(c.rawDetails)+1+len(c.publicKey))
|
||||
//TODO: double check this, panic on empty raw details
|
||||
copy(b, c.rawDetails)
|
||||
b[len(c.rawDetails)] = byte(c.curve)
|
||||
copy(b[len(c.rawDetails)+1:], c.publicKey)
|
||||
copy(b[len(c.rawDetails)+1+len(c.publicKey):], c.signature)
|
||||
sum := sha256.Sum256(b)
|
||||
return hex.EncodeToString(sum[:]), nil
|
||||
}
|
||||
|
||||
func (c *certificateV2) CheckSignature(key []byte) bool {
|
||||
b := make([]byte, len(c.rawDetails)+1+len(c.publicKey))
|
||||
//TODO: double check this, panic on empty raw details
|
||||
copy(b, c.rawDetails)
|
||||
b[len(c.rawDetails)] = byte(c.curve)
|
||||
copy(b[len(c.rawDetails)+1:], c.publicKey)
|
||||
|
||||
switch c.curve {
|
||||
case Curve_CURVE25519:
|
||||
return ed25519.Verify(key, b, c.signature)
|
||||
case Curve_P256:
|
||||
//TODO: NewPublicKey
|
||||
x, y := elliptic.Unmarshal(elliptic.P256(), key)
|
||||
pubKey := &ecdsa.PublicKey{Curve: elliptic.P256(), X: x, Y: y}
|
||||
hashed := sha256.Sum256(b)
|
||||
return ecdsa.VerifyASN1(pubKey, hashed[:], c.signature)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (c *certificateV2) Expired(t time.Time) bool {
|
||||
return c.details.notBefore.After(t) || c.details.notAfter.Before(t)
|
||||
}
|
||||
|
||||
func (c *certificateV2) VerifyPrivateKey(curve Curve, key []byte) error {
|
||||
if curve != c.curve {
|
||||
return fmt.Errorf("curve in cert and private key supplied don't match")
|
||||
}
|
||||
if c.details.isCA {
|
||||
switch curve {
|
||||
case Curve_CURVE25519:
|
||||
// the call to PublicKey below will panic slice bounds out of range otherwise
|
||||
if len(key) != ed25519.PrivateKeySize {
|
||||
return fmt.Errorf("key was not 64 bytes, is invalid ed25519 private key")
|
||||
}
|
||||
|
||||
if !ed25519.PublicKey(c.publicKey).Equal(ed25519.PrivateKey(key).Public()) {
|
||||
return fmt.Errorf("public key in cert and private key supplied don't match")
|
||||
}
|
||||
case Curve_P256:
|
||||
privkey, err := ecdh.P256().NewPrivateKey(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot parse private key as P256")
|
||||
}
|
||||
pub := privkey.PublicKey().Bytes()
|
||||
if !bytes.Equal(pub, c.publicKey) {
|
||||
return fmt.Errorf("public key in cert and private key supplied don't match")
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid curve: %s", curve)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var pub []byte
|
||||
switch curve {
|
||||
case Curve_CURVE25519:
|
||||
var err error
|
||||
pub, err = curve25519.X25519(key, curve25519.Basepoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case Curve_P256:
|
||||
privkey, err := ecdh.P256().NewPrivateKey(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pub = privkey.PublicKey().Bytes()
|
||||
default:
|
||||
return fmt.Errorf("invalid curve: %s", curve)
|
||||
}
|
||||
if !bytes.Equal(pub, c.publicKey) {
|
||||
return fmt.Errorf("public key in cert and private key supplied don't match")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *certificateV2) String() string {
|
||||
b, err := json.MarshalIndent(c.marshalJSON(), "", "\t")
|
||||
if err != nil {
|
||||
return "<error marshalling certificate>"
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (c *certificateV2) MarshalForHandshakes() ([]byte, error) {
|
||||
var b cryptobyte.Builder
|
||||
// Outermost certificate
|
||||
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
|
||||
|
||||
// Add the cert details which is already marshalled
|
||||
//TODO: panic on nil rawDetails
|
||||
b.AddBytes(c.rawDetails)
|
||||
|
||||
// Skipping the curve and public key since those come across in a different part of the handshake
|
||||
|
||||
// Add the signature
|
||||
b.AddASN1(TagCertSignature, func(b *cryptobyte.Builder) {
|
||||
b.AddBytes(c.signature)
|
||||
})
|
||||
})
|
||||
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func (c *certificateV2) Marshal() ([]byte, error) {
|
||||
var b cryptobyte.Builder
|
||||
// Outermost certificate
|
||||
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
|
||||
|
||||
// Add the cert details which is already marshalled
|
||||
b.AddBytes(c.rawDetails)
|
||||
|
||||
// Add the curve only if its not the default value
|
||||
if c.curve != Curve_CURVE25519 {
|
||||
b.AddASN1(TagCertCurve, func(b *cryptobyte.Builder) {
|
||||
b.AddBytes([]byte{byte(c.curve)})
|
||||
})
|
||||
}
|
||||
|
||||
// Add the public key if it is not empty
|
||||
if c.publicKey != nil {
|
||||
b.AddASN1(TagCertPublicKey, func(b *cryptobyte.Builder) {
|
||||
b.AddBytes(c.publicKey)
|
||||
})
|
||||
}
|
||||
|
||||
// Add the signature
|
||||
b.AddASN1(TagCertSignature, func(b *cryptobyte.Builder) {
|
||||
b.AddBytes(c.signature)
|
||||
})
|
||||
})
|
||||
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func (c *certificateV2) MarshalPEM() ([]byte, error) {
|
||||
b, err := c.Marshal()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pem.EncodeToMemory(&pem.Block{Type: CertificateV2Banner, Bytes: b}), nil
|
||||
}
|
||||
|
||||
func (c *certificateV2) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(c.marshalJSON())
|
||||
}
|
||||
|
||||
func (c *certificateV2) marshalJSON() m {
|
||||
fp, _ := c.Fingerprint()
|
||||
return m{
|
||||
"details": m{
|
||||
"name": c.details.name,
|
||||
"networks": c.details.networks,
|
||||
"unsafeNetworks": c.details.unsafeNetworks,
|
||||
"groups": c.details.groups,
|
||||
"notBefore": c.details.notBefore,
|
||||
"notAfter": c.details.notAfter,
|
||||
"isCa": c.details.isCA,
|
||||
"issuer": c.details.issuer,
|
||||
},
|
||||
"version": Version2,
|
||||
"publicKey": fmt.Sprintf("%x", c.publicKey),
|
||||
"curve": c.curve.String(),
|
||||
"fingerprint": fp,
|
||||
"signature": fmt.Sprintf("%x", c.Signature()),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *certificateV2) Copy() Certificate {
|
||||
nc := &certificateV2{
|
||||
details: detailsV2{
|
||||
name: c.details.name,
|
||||
groups: make([]string, len(c.details.groups)),
|
||||
networks: make([]netip.Prefix, len(c.details.networks)),
|
||||
unsafeNetworks: make([]netip.Prefix, len(c.details.unsafeNetworks)),
|
||||
notBefore: c.details.notBefore,
|
||||
notAfter: c.details.notAfter,
|
||||
isCA: c.details.isCA,
|
||||
issuer: c.details.issuer,
|
||||
},
|
||||
curve: c.curve,
|
||||
publicKey: make([]byte, len(c.publicKey)),
|
||||
signature: make([]byte, len(c.signature)),
|
||||
}
|
||||
|
||||
copy(nc.signature, c.signature)
|
||||
copy(nc.details.groups, c.details.groups)
|
||||
copy(nc.publicKey, c.publicKey)
|
||||
copy(nc.details.networks, c.details.networks)
|
||||
copy(nc.details.unsafeNetworks, c.details.unsafeNetworks)
|
||||
|
||||
return nc
|
||||
}
|
||||
|
||||
func (c *certificateV2) fromTBSCertificate(t *TBSCertificate) error {
|
||||
c.details = detailsV2{
|
||||
name: t.Name,
|
||||
networks: t.Networks,
|
||||
unsafeNetworks: t.UnsafeNetworks,
|
||||
groups: t.Groups,
|
||||
isCA: t.IsCA,
|
||||
notBefore: t.NotBefore,
|
||||
notAfter: t.NotAfter,
|
||||
issuer: t.issuer,
|
||||
}
|
||||
c.curve = t.Curve
|
||||
c.publicKey = t.PublicKey
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *certificateV2) marshalForSigning() ([]byte, error) {
|
||||
d, err := c.details.Marshal()
|
||||
if err != nil {
|
||||
//TODO: annotate?
|
||||
return nil, err
|
||||
}
|
||||
c.rawDetails = d
|
||||
|
||||
b := make([]byte, len(c.rawDetails)+1+len(c.publicKey))
|
||||
//TODO: double check this
|
||||
copy(b, c.rawDetails)
|
||||
b[len(c.rawDetails)] = byte(c.curve)
|
||||
copy(b[len(c.rawDetails)+1:], c.publicKey)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (c *certificateV2) setSignature(b []byte) error {
|
||||
c.signature = b
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *detailsV2) Marshal() ([]byte, error) {
|
||||
var b cryptobyte.Builder
|
||||
var err error
|
||||
|
||||
// Details are a structure
|
||||
b.AddASN1(TagCertDetails, func(b *cryptobyte.Builder) {
|
||||
|
||||
// Add the name
|
||||
b.AddASN1(TagDetailsName, func(b *cryptobyte.Builder) {
|
||||
b.AddBytes([]byte(d.name))
|
||||
})
|
||||
|
||||
// Add the networks if any exist
|
||||
if len(d.networks) > 0 {
|
||||
b.AddASN1(TagDetailsNetworks, func(b *cryptobyte.Builder) {
|
||||
for _, n := range d.networks {
|
||||
sb, innerErr := n.MarshalBinary()
|
||||
if innerErr != nil {
|
||||
// MarshalBinary never returns an error
|
||||
err = fmt.Errorf("unable to marshal network: %w", innerErr)
|
||||
return
|
||||
}
|
||||
b.AddASN1OctetString(sb)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Add the unsafe networks if any exist
|
||||
if len(d.unsafeNetworks) > 0 {
|
||||
b.AddASN1(TagDetailsUnsafeNetworks, func(b *cryptobyte.Builder) {
|
||||
for _, n := range d.unsafeNetworks {
|
||||
sb, innerErr := n.MarshalBinary()
|
||||
if innerErr != nil {
|
||||
// MarshalBinary never returns an error
|
||||
err = fmt.Errorf("unable to marshal unsafe network: %w", innerErr)
|
||||
return
|
||||
}
|
||||
b.AddASN1OctetString(sb)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Add groups if any exist
|
||||
if len(d.groups) > 0 {
|
||||
b.AddASN1(TagDetailsGroups, func(b *cryptobyte.Builder) {
|
||||
for _, group := range d.groups {
|
||||
b.AddASN1(asn1.UTF8String, func(b *cryptobyte.Builder) {
|
||||
b.AddBytes([]byte(group))
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Add IsCA only if true
|
||||
if d.isCA {
|
||||
b.AddASN1(TagDetailsIsCA, func(b *cryptobyte.Builder) {
|
||||
b.AddUint8(0xff)
|
||||
})
|
||||
}
|
||||
|
||||
// Add not before
|
||||
b.AddASN1Int64WithTag(d.notBefore.Unix(), TagDetailsNotBefore)
|
||||
|
||||
// Add not after
|
||||
b.AddASN1Int64WithTag(d.notAfter.Unix(), TagDetailsNotAfter)
|
||||
|
||||
// Add the issuer if present
|
||||
if d.issuer != "" {
|
||||
issuerBytes, innerErr := hex.DecodeString(d.issuer)
|
||||
if innerErr != nil {
|
||||
err = fmt.Errorf("failed to decode issuer: %w", innerErr)
|
||||
return
|
||||
}
|
||||
b.AddASN1(TagDetailsIssuer, func(b *cryptobyte.Builder) {
|
||||
b.AddBytes(issuerBytes)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func unmarshalCertificateV2(b []byte, publicKey []byte, curve Curve) (*certificateV2, error) {
|
||||
l := len(b)
|
||||
if l == 0 || l > MaxCertificateSize {
|
||||
return nil, ErrBadFormat
|
||||
}
|
||||
|
||||
input := cryptobyte.String(b)
|
||||
// Open the envelope
|
||||
if !input.ReadASN1(&input, asn1.SEQUENCE) || input.Empty() {
|
||||
return nil, ErrBadFormat
|
||||
}
|
||||
|
||||
// Grab the cert details, we need to preserve the tag and length
|
||||
var rawDetails cryptobyte.String
|
||||
if !input.ReadASN1Element(&rawDetails, TagCertDetails) || rawDetails.Empty() {
|
||||
return nil, ErrBadFormat
|
||||
}
|
||||
|
||||
//Maybe grab the curve
|
||||
var rawCurve byte
|
||||
if !readOptionalASN1Byte(&input, &rawCurve, TagCertCurve, byte(curve)) {
|
||||
return nil, ErrBadFormat
|
||||
}
|
||||
curve = Curve(rawCurve)
|
||||
|
||||
// Maybe grab the public key
|
||||
var rawPublicKey cryptobyte.String
|
||||
if len(publicKey) > 0 {
|
||||
rawPublicKey = publicKey
|
||||
} else if !input.ReadOptionalASN1(&rawPublicKey, nil, TagCertPublicKey) {
|
||||
return nil, ErrBadFormat
|
||||
}
|
||||
|
||||
//TODO: Assert public key length
|
||||
|
||||
// Grab the signature
|
||||
var rawSignature cryptobyte.String
|
||||
if !input.ReadASN1(&rawSignature, TagCertSignature) || rawSignature.Empty() {
|
||||
return nil, ErrBadFormat
|
||||
}
|
||||
|
||||
// Finally unmarshal the details
|
||||
details, err := unmarshalDetails(rawDetails)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &certificateV2{
|
||||
details: details,
|
||||
rawDetails: rawDetails,
|
||||
curve: curve,
|
||||
publicKey: rawPublicKey,
|
||||
signature: rawSignature,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func unmarshalDetails(b cryptobyte.String) (detailsV2, error) {
|
||||
// Open the envelope
|
||||
if !b.ReadASN1(&b, TagCertDetails) || b.Empty() {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
// Read the name
|
||||
var name cryptobyte.String
|
||||
if !b.ReadASN1(&name, TagDetailsName) || name.Empty() || len(name) > MaxNameLength {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
// Read the network addresses
|
||||
var subString cryptobyte.String
|
||||
var found bool
|
||||
|
||||
if !b.ReadOptionalASN1(&subString, &found, TagDetailsNetworks) {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
var networks []netip.Prefix
|
||||
var val cryptobyte.String
|
||||
if found {
|
||||
for !subString.Empty() {
|
||||
if !subString.ReadASN1(&val, asn1.OCTET_STRING) || val.Empty() || len(val) > MaxNetworkLength {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
var n netip.Prefix
|
||||
if err := n.UnmarshalBinary(val); err != nil {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
networks = append(networks, n)
|
||||
}
|
||||
}
|
||||
|
||||
// Read out any unsafe networks
|
||||
if !b.ReadOptionalASN1(&subString, &found, TagDetailsUnsafeNetworks) {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
var unsafeNetworks []netip.Prefix
|
||||
if found {
|
||||
for !subString.Empty() {
|
||||
if !subString.ReadASN1(&val, asn1.OCTET_STRING) || val.Empty() || len(val) > MaxNetworkLength {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
var n netip.Prefix
|
||||
if err := n.UnmarshalBinary(val); err != nil {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
unsafeNetworks = append(unsafeNetworks, n)
|
||||
}
|
||||
}
|
||||
|
||||
// Read out any groups
|
||||
if !b.ReadOptionalASN1(&subString, &found, TagDetailsGroups) {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
var groups []string
|
||||
if found {
|
||||
for !subString.Empty() {
|
||||
if !subString.ReadASN1(&val, asn1.UTF8String) || val.Empty() {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
groups = append(groups, string(val))
|
||||
}
|
||||
}
|
||||
|
||||
// Read out IsCA
|
||||
var isCa bool
|
||||
if !readOptionalASN1Boolean(&b, &isCa, TagDetailsIsCA, false) {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
// Read not before and not after
|
||||
var notBefore int64
|
||||
if !b.ReadASN1Int64WithTag(¬Before, TagDetailsNotBefore) {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
var notAfter int64
|
||||
if !b.ReadASN1Int64WithTag(¬After, TagDetailsNotAfter) {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
// Read issuer
|
||||
var issuer cryptobyte.String
|
||||
if !b.ReadOptionalASN1(&issuer, nil, TagDetailsIssuer) {
|
||||
return detailsV2{}, ErrBadFormat
|
||||
}
|
||||
|
||||
slices.SortFunc(networks, comparePrefix)
|
||||
slices.SortFunc(unsafeNetworks, comparePrefix)
|
||||
|
||||
return detailsV2{
|
||||
name: string(name),
|
||||
networks: networks,
|
||||
unsafeNetworks: unsafeNetworks,
|
||||
groups: groups,
|
||||
isCA: isCa,
|
||||
notBefore: time.Unix(notBefore, 0),
|
||||
notAfter: time.Unix(notAfter, 0),
|
||||
issuer: hex.EncodeToString(issuer),
|
||||
}, nil
|
||||
}
|
||||
@@ -24,4 +24,7 @@ var (
|
||||
ErrInvalidPEMX25519PrivateKeyBanner = errors.New("bytes did not contain a proper X25519 private key banner")
|
||||
ErrInvalidPEMEd25519PublicKeyBanner = errors.New("bytes did not contain a proper Ed25519 public key banner")
|
||||
ErrInvalidPEMEd25519PrivateKeyBanner = errors.New("bytes did not contain a proper Ed25519 private key banner")
|
||||
|
||||
ErrNoPeerStaticKey = errors.New("no peer static key was present")
|
||||
ErrNoPayload = errors.New("provided payload was empty")
|
||||
)
|
||||
|
||||
19
cert/pem.go
19
cert/pem.go
@@ -30,19 +30,24 @@ func UnmarshalCertificateFromPEM(b []byte) (Certificate, []byte, error) {
|
||||
return nil, r, ErrInvalidPEMBlock
|
||||
}
|
||||
|
||||
var c Certificate
|
||||
var err error
|
||||
|
||||
switch p.Type {
|
||||
case CertificateBanner:
|
||||
c, err := unmarshalCertificateV1(p.Bytes, true)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return c, r, nil
|
||||
c, err = unmarshalCertificateV1(p.Bytes, nil)
|
||||
case CertificateV2Banner:
|
||||
//TODO
|
||||
panic("TODO")
|
||||
c, err = unmarshalCertificateV2(p.Bytes, nil, Curve_CURVE25519)
|
||||
default:
|
||||
return nil, r, ErrInvalidPEMCertificateBanner
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, r, err
|
||||
}
|
||||
|
||||
return c, r, nil
|
||||
|
||||
}
|
||||
|
||||
func MarshalPublicKeyToPEM(curve Curve, b []byte) []byte {
|
||||
|
||||
109
cert/sign.go
109
cert/sign.go
@@ -1,11 +1,16 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/ed25519"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/slackhq/nebula/pkclient"
|
||||
)
|
||||
|
||||
// TBSCertificate represents a certificate intended to be signed.
|
||||
@@ -24,27 +29,62 @@ type TBSCertificate struct {
|
||||
issuer string
|
||||
}
|
||||
|
||||
type beingSignedCertificate interface {
|
||||
// fromTBSCertificate copies the values from the TBSCertificate to this versions internal representation
|
||||
fromTBSCertificate(*TBSCertificate) error
|
||||
|
||||
// marshalForSigning returns the bytes that should be signed
|
||||
marshalForSigning() ([]byte, error)
|
||||
|
||||
// setSignature sets the signature for the certificate that has just been signed
|
||||
setSignature([]byte) error
|
||||
}
|
||||
|
||||
type SignerLambda func(certBytes []byte) ([]byte, error)
|
||||
|
||||
// Sign will create a sealed certificate using details provided by the TBSCertificate as long as those
|
||||
// details do not violate constraints of the signing certificate.
|
||||
// If the TBSCertificate is a CA then signer must be nil.
|
||||
func (t *TBSCertificate) Sign(signer Certificate, curve Curve, key []byte) (Certificate, error) {
|
||||
return t.sign(signer, curve, key, nil)
|
||||
}
|
||||
|
||||
func (t *TBSCertificate) SignPkcs11(signer Certificate, curve Curve, client *pkclient.PKClient) (Certificate, error) {
|
||||
if curve != Curve_P256 {
|
||||
return nil, fmt.Errorf("only P256 is supported by PKCS#11")
|
||||
switch t.Curve {
|
||||
case Curve_CURVE25519:
|
||||
pk := ed25519.PrivateKey(key)
|
||||
sp := func(certBytes []byte) ([]byte, error) {
|
||||
sig := ed25519.Sign(pk, certBytes)
|
||||
return sig, nil
|
||||
}
|
||||
return t.SignWith(signer, curve, sp)
|
||||
case Curve_P256:
|
||||
pk := &ecdsa.PrivateKey{
|
||||
PublicKey: ecdsa.PublicKey{
|
||||
Curve: elliptic.P256(),
|
||||
},
|
||||
// ref: https://github.com/golang/go/blob/go1.19/src/crypto/x509/sec1.go#L95
|
||||
D: new(big.Int).SetBytes(key),
|
||||
}
|
||||
// ref: https://github.com/golang/go/blob/go1.19/src/crypto/x509/sec1.go#L119
|
||||
pk.X, pk.Y = pk.Curve.ScalarBaseMult(key)
|
||||
sp := func(certBytes []byte) ([]byte, error) {
|
||||
// We need to hash first for ECDSA
|
||||
// - https://pkg.go.dev/crypto/ecdsa#SignASN1
|
||||
hashed := sha256.Sum256(certBytes)
|
||||
return ecdsa.SignASN1(rand.Reader, pk, hashed[:])
|
||||
}
|
||||
return t.SignWith(signer, curve, sp)
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid curve: %s", t.Curve)
|
||||
}
|
||||
|
||||
return t.sign(signer, curve, nil, client)
|
||||
}
|
||||
|
||||
func (t *TBSCertificate) sign(signer Certificate, curve Curve, key []byte, client *pkclient.PKClient) (Certificate, error) {
|
||||
// SignWith does the same thing as sign, but uses the function in `sp` to calculate the signature.
|
||||
// You should only use SignWith if you do not have direct access to your private key.
|
||||
func (t *TBSCertificate) SignWith(signer Certificate, curve Curve, sp SignerLambda) (Certificate, error) {
|
||||
if curve != t.Curve {
|
||||
return nil, fmt.Errorf("curve in cert and private key supplied don't match")
|
||||
}
|
||||
|
||||
//TODO: make sure we have all minimum properties to sign, like a public key
|
||||
//TODO: we need to verify networks and unsafe networks (no duplicates, max of 1 of each version for v2 certs
|
||||
|
||||
if signer != nil {
|
||||
if t.IsCA {
|
||||
@@ -67,10 +107,55 @@ func (t *TBSCertificate) sign(signer Certificate, curve Curve, key []byte, clien
|
||||
}
|
||||
}
|
||||
|
||||
slices.SortFunc(t.Networks, comparePrefix)
|
||||
slices.SortFunc(t.UnsafeNetworks, comparePrefix)
|
||||
|
||||
var c beingSignedCertificate
|
||||
switch t.Version {
|
||||
case Version1:
|
||||
return signV1(t, curve, key, client)
|
||||
c = &certificateV1{}
|
||||
err := c.fromTBSCertificate(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case Version2:
|
||||
c = &certificateV2{}
|
||||
err := c.fromTBSCertificate(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown cert version %d", t.Version)
|
||||
}
|
||||
|
||||
certBytes, err := c.marshalForSigning()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sig, err := sp(certBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//TODO: check if we have sig bytes?
|
||||
err = c.setSignature(sig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sc, ok := c.(Certificate)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid certificate")
|
||||
}
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
func comparePrefix(a, b netip.Prefix) int {
|
||||
addr := a.Addr().Compare(b.Addr())
|
||||
if addr == 0 {
|
||||
return a.Bits() - b.Bits()
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user