| 1 |
package cert |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/ecdh" |
| 6 |
"crypto/ecdsa" |
| 7 |
"crypto/ed25519" |
| 8 |
"crypto/elliptic" |
| 9 |
"crypto/sha256" |
| 10 |
"encoding/binary" |
| 11 |
"encoding/hex" |
| 12 |
"encoding/json" |
| 13 |
"encoding/pem" |
| 14 |
"fmt" |
| 15 |
"net" |
| 16 |
"net/netip" |
| 17 |
"time" |
| 18 |
|
| 19 |
"golang.org/x/crypto/curve25519" |
| 20 |
"google.golang.org/protobuf/proto" |
| 21 |
) |
| 22 |
|
| 23 |
const publicKeyLen = 32 |
| 24 |
|
| 25 |
type certificateV1 struct { |
| 26 |
details detailsV1 |
| 27 |
signature []byte |
| 28 |
} |
| 29 |
|
| 30 |
type detailsV1 struct { |
| 31 |
name string |
| 32 |
networks []netip.Prefix |
| 33 |
unsafeNetworks []netip.Prefix |
| 34 |
groups []string |
| 35 |
notBefore time.Time |
| 36 |
notAfter time.Time |
| 37 |
publicKey []byte |
| 38 |
isCA bool |
| 39 |
issuer string |
| 40 |
|
| 41 |
curve Curve |
| 42 |
} |
| 43 |
|
| 44 |
type m = map[string]any |
| 45 |
|
| 46 |
func (c *certificateV1) Version() Version { |
| 47 |
return Version1 |
| 48 |
} |
| 49 |
|
| 50 |
func (c *certificateV1) Curve() Curve { |
| 51 |
return c.details.curve |
| 52 |
} |
| 53 |
|
| 54 |
func (c *certificateV1) Groups() []string { |
| 55 |
return c.details.groups |
| 56 |
} |
| 57 |
|
| 58 |
func (c *certificateV1) IsCA() bool { |
| 59 |
return c.details.isCA |
| 60 |
} |
| 61 |
|
| 62 |
func (c *certificateV1) Issuer() string { |
| 63 |
return c.details.issuer |
| 64 |
} |
| 65 |
|
| 66 |
func (c *certificateV1) Name() string { |
| 67 |
return c.details.name |
| 68 |
} |
| 69 |
|
| 70 |
func (c *certificateV1) Networks() []netip.Prefix { |
| 71 |
return c.details.networks |
| 72 |
} |
| 73 |
|
| 74 |
func (c *certificateV1) NotAfter() time.Time { |
| 75 |
return c.details.notAfter |
| 76 |
} |
| 77 |
|
| 78 |
func (c *certificateV1) NotBefore() time.Time { |
| 79 |
return c.details.notBefore |
| 80 |
} |
| 81 |
|
| 82 |
func (c *certificateV1) PublicKey() []byte { |
| 83 |
return c.details.publicKey |
| 84 |
} |
| 85 |
|
| 86 |
func (c *certificateV1) MarshalPublicKeyPEM() []byte { |
| 87 |
return marshalCertPublicKeyToPEM(c) |
| 88 |
} |
| 89 |
|
| 90 |
func (c *certificateV1) Signature() []byte { |
| 91 |
return c.signature |
| 92 |
} |
| 93 |
|
| 94 |
func (c *certificateV1) UnsafeNetworks() []netip.Prefix { |
| 95 |
return c.details.unsafeNetworks |
| 96 |
} |
| 97 |
|
| 98 |
func (c *certificateV1) Fingerprint() (string, error) { |
| 99 |
b, err := c.Marshal() |
| 100 |
if err != nil { |
| 101 |
return "", err |
| 102 |
} |
| 103 |
|
| 104 |
sum := sha256.Sum256(b) |
| 105 |
return hex.EncodeToString(sum[:]), nil |
| 106 |
} |
| 107 |
|
| 108 |
func (c *certificateV1) CheckSignature(key []byte) bool { |
| 109 |
b, err := proto.Marshal(c.getRawDetails()) |
| 110 |
if err != nil { |
| 111 |
return false |
| 112 |
} |
| 113 |
switch c.details.curve { |
| 114 |
case Curve_CURVE25519: |
| 115 |
if len(key) != ed25519.PublicKeySize { |
| 116 |
return false //avoids a panic internal to ed25519 |
| 117 |
} |
| 118 |
return ed25519.Verify(key, b, c.signature) |
| 119 |
case Curve_P256: |
| 120 |
pubKey, err := ecdsa.ParseUncompressedPublicKey(elliptic.P256(), key) |
| 121 |
if err != nil { |
| 122 |
return false |
| 123 |
} |
| 124 |
hashed := sha256.Sum256(b) |
| 125 |
return ecdsa.VerifyASN1(pubKey, hashed[:], c.signature) |
| 126 |
default: |
| 127 |
return false |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
func (c *certificateV1) Expired(t time.Time) bool { |
| 132 |
return c.details.notBefore.After(t) || c.details.notAfter.Before(t) |
| 133 |
} |
| 134 |
|
| 135 |
func (c *certificateV1) VerifyPrivateKey(curve Curve, key []byte) error { |
| 136 |
if curve != c.details.curve { |
| 137 |
return fmt.Errorf("curve in cert and private key supplied don't match") |
| 138 |
} |
| 139 |
if c.details.isCA { |
| 140 |
switch curve { |
| 141 |
case Curve_CURVE25519: |
| 142 |
// the call to PublicKey below will panic slice bounds out of range otherwise |
| 143 |
if len(key) != ed25519.PrivateKeySize { |
| 144 |
return fmt.Errorf("key was not 64 bytes, is invalid ed25519 private key") |
| 145 |
} |
| 146 |
|
| 147 |
if !ed25519.PublicKey(c.details.publicKey).Equal(ed25519.PrivateKey(key).Public()) { |
| 148 |
return fmt.Errorf("public key in cert and private key supplied don't match") |
| 149 |
} |
| 150 |
case Curve_P256: |
| 151 |
privkey, err := ecdh.P256().NewPrivateKey(key) |
| 152 |
if err != nil { |
| 153 |
return fmt.Errorf("cannot parse private key as P256: %w", err) |
| 154 |
} |
| 155 |
pub := privkey.PublicKey().Bytes() |
| 156 |
if !bytes.Equal(pub, c.details.publicKey) { |
| 157 |
return fmt.Errorf("public key in cert and private key supplied don't match") |
| 158 |
} |
| 159 |
default: |
| 160 |
return fmt.Errorf("invalid curve: %s", curve) |
| 161 |
} |
| 162 |
return nil |
| 163 |
} |
| 164 |
|
| 165 |
var pub []byte |
| 166 |
switch curve { |
| 167 |
case Curve_CURVE25519: |
| 168 |
var err error |
| 169 |
pub, err = curve25519.X25519(key, curve25519.Basepoint) |
| 170 |
if err != nil { |
| 171 |
return err |
| 172 |
} |
| 173 |
case Curve_P256: |
| 174 |
privkey, err := ecdh.P256().NewPrivateKey(key) |
| 175 |
if err != nil { |
| 176 |
return err |
| 177 |
} |
| 178 |
pub = privkey.PublicKey().Bytes() |
| 179 |
default: |
| 180 |
return fmt.Errorf("invalid curve: %s", curve) |
| 181 |
} |
| 182 |
if !bytes.Equal(pub, c.details.publicKey) { |
| 183 |
return fmt.Errorf("public key in cert and private key supplied don't match") |
| 184 |
} |
| 185 |
|
| 186 |
return nil |
| 187 |
} |
| 188 |
|
| 189 |
// getRawDetails marshals the raw details into protobuf ready struct |
| 190 |
func (c *certificateV1) getRawDetails() *RawNebulaCertificateDetails { |
| 191 |
rd := &RawNebulaCertificateDetails{ |
| 192 |
Name: c.details.name, |
| 193 |
Groups: c.details.groups, |
| 194 |
NotBefore: c.details.notBefore.Unix(), |
| 195 |
NotAfter: c.details.notAfter.Unix(), |
| 196 |
PublicKey: make([]byte, len(c.details.publicKey)), |
| 197 |
IsCA: c.details.isCA, |
| 198 |
Curve: c.details.curve, |
| 199 |
} |
| 200 |
|
| 201 |
for _, ipNet := range c.details.networks { |
| 202 |
mask := net.CIDRMask(ipNet.Bits(), ipNet.Addr().BitLen()) |
| 203 |
rd.Ips = append(rd.Ips, addr2int(ipNet.Addr()), ip2int(mask)) |
| 204 |
} |
| 205 |
|
| 206 |
for _, ipNet := range c.details.unsafeNetworks { |
| 207 |
mask := net.CIDRMask(ipNet.Bits(), ipNet.Addr().BitLen()) |
| 208 |
rd.Subnets = append(rd.Subnets, addr2int(ipNet.Addr()), ip2int(mask)) |
| 209 |
} |
| 210 |
|
| 211 |
copy(rd.PublicKey, c.details.publicKey[:]) |
| 212 |
|
| 213 |
// I know, this is terrible |
| 214 |
rd.Issuer, _ = hex.DecodeString(c.details.issuer) |
| 215 |
|
| 216 |
return rd |
| 217 |
} |
| 218 |
|
| 219 |
func (c *certificateV1) String() string { |
| 220 |
b, err := json.MarshalIndent(c.marshalJSON(), "", "\t") |
| 221 |
if err != nil { |
| 222 |
return fmt.Sprintf("<error marshalling certificate: %v>", err) |
| 223 |
} |
| 224 |
return string(b) |
| 225 |
} |
| 226 |
|
| 227 |
func (c *certificateV1) MarshalForHandshakes() ([]byte, error) { |
| 228 |
pubKey := c.details.publicKey |
| 229 |
c.details.publicKey = nil |
| 230 |
rawCertNoKey, err := c.Marshal() |
| 231 |
if err != nil { |
| 232 |
return nil, err |
| 233 |
} |
| 234 |
c.details.publicKey = pubKey |
| 235 |
return rawCertNoKey, nil |
| 236 |
} |
| 237 |
|
| 238 |
func (c *certificateV1) Marshal() ([]byte, error) { |
| 239 |
rc := RawNebulaCertificate{ |
| 240 |
Details: c.getRawDetails(), |
| 241 |
Signature: c.signature, |
| 242 |
} |
| 243 |
|
| 244 |
return proto.Marshal(&rc) |
| 245 |
} |
| 246 |
|
| 247 |
func (c *certificateV1) MarshalPEM() ([]byte, error) { |
| 248 |
b, err := c.Marshal() |
| 249 |
if err != nil { |
| 250 |
return nil, err |
| 251 |
} |
| 252 |
return pem.EncodeToMemory(&pem.Block{Type: CertificateBanner, Bytes: b}), nil |
| 253 |
} |
| 254 |
|
| 255 |
func (c *certificateV1) MarshalJSON() ([]byte, error) { |
| 256 |
return json.Marshal(c.marshalJSON()) |
| 257 |
} |
| 258 |
|
| 259 |
func (c *certificateV1) marshalJSON() m { |
| 260 |
fp, _ := c.Fingerprint() |
| 261 |
return m{ |
| 262 |
"version": Version1, |
| 263 |
"details": m{ |
| 264 |
"name": c.details.name, |
| 265 |
"networks": c.details.networks, |
| 266 |
"unsafeNetworks": c.details.unsafeNetworks, |
| 267 |
"groups": c.details.groups, |
| 268 |
"notBefore": c.details.notBefore, |
| 269 |
"notAfter": c.details.notAfter, |
| 270 |
"publicKey": fmt.Sprintf("%x", c.details.publicKey), |
| 271 |
"isCa": c.details.isCA, |
| 272 |
"issuer": c.details.issuer, |
| 273 |
"curve": c.details.curve.String(), |
| 274 |
}, |
| 275 |
"fingerprint": fp, |
| 276 |
"signature": fmt.Sprintf("%x", c.Signature()), |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
func (c *certificateV1) Copy() Certificate { |
| 281 |
nc := &certificateV1{ |
| 282 |
details: detailsV1{ |
| 283 |
name: c.details.name, |
| 284 |
notBefore: c.details.notBefore, |
| 285 |
notAfter: c.details.notAfter, |
| 286 |
publicKey: make([]byte, len(c.details.publicKey)), |
| 287 |
isCA: c.details.isCA, |
| 288 |
issuer: c.details.issuer, |
| 289 |
curve: c.details.curve, |
| 290 |
}, |
| 291 |
signature: make([]byte, len(c.signature)), |
| 292 |
} |
| 293 |
|
| 294 |
if c.details.groups != nil { |
| 295 |
nc.details.groups = make([]string, len(c.details.groups)) |
| 296 |
copy(nc.details.groups, c.details.groups) |
| 297 |
} |
| 298 |
|
| 299 |
if c.details.networks != nil { |
| 300 |
nc.details.networks = make([]netip.Prefix, len(c.details.networks)) |
| 301 |
copy(nc.details.networks, c.details.networks) |
| 302 |
} |
| 303 |
|
| 304 |
if c.details.unsafeNetworks != nil { |
| 305 |
nc.details.unsafeNetworks = make([]netip.Prefix, len(c.details.unsafeNetworks)) |
| 306 |
copy(nc.details.unsafeNetworks, c.details.unsafeNetworks) |
| 307 |
} |
| 308 |
|
| 309 |
copy(nc.signature, c.signature) |
| 310 |
copy(nc.details.publicKey, c.details.publicKey) |
| 311 |
|
| 312 |
return nc |
| 313 |
} |
| 314 |
|
| 315 |
func (c *certificateV1) fromTBSCertificate(t *TBSCertificate) error { |
| 316 |
c.details = detailsV1{ |
| 317 |
name: t.Name, |
| 318 |
networks: t.Networks, |
| 319 |
unsafeNetworks: t.UnsafeNetworks, |
| 320 |
groups: t.Groups, |
| 321 |
notBefore: t.NotBefore, |
| 322 |
notAfter: t.NotAfter, |
| 323 |
publicKey: t.PublicKey, |
| 324 |
isCA: t.IsCA, |
| 325 |
curve: t.Curve, |
| 326 |
issuer: t.issuer, |
| 327 |
} |
| 328 |
|
| 329 |
return c.validate() |
| 330 |
} |
| 331 |
|
| 332 |
func (c *certificateV1) validate() error { |
| 333 |
// Empty names are allowed |
| 334 |
|
| 335 |
if len(c.details.publicKey) == 0 { |
| 336 |
return ErrInvalidPublicKey |
| 337 |
} |
| 338 |
|
| 339 |
// Original v1 rules allowed multiple networks to be present but ignored all but the first one. |
| 340 |
// Continue to allow this behavior |
| 341 |
if !c.details.isCA && len(c.details.networks) == 0 { |
| 342 |
return NewErrInvalidCertificateProperties("non-CA certificates must contain exactly one network") |
| 343 |
} |
| 344 |
|
| 345 |
for _, network := range c.details.networks { |
| 346 |
if !network.IsValid() || !network.Addr().IsValid() { |
| 347 |
return NewErrInvalidCertificateProperties("invalid network: %s", network) |
| 348 |
} |
| 349 |
|
| 350 |
if network.Addr().Is6() { |
| 351 |
return NewErrInvalidCertificateProperties("certificate may not contain IPv6 networks: %v", network) |
| 352 |
} |
| 353 |
|
| 354 |
if network.Addr().IsUnspecified() { |
| 355 |
return NewErrInvalidCertificateProperties("non-CA certificates must not use the zero address as a network: %s", network) |
| 356 |
} |
| 357 |
|
| 358 |
if network.Addr().Zone() != "" { |
| 359 |
return NewErrInvalidCertificateProperties("networks may not contain zones: %s", network) |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
for _, network := range c.details.unsafeNetworks { |
| 364 |
if !network.IsValid() || !network.Addr().IsValid() { |
| 365 |
return NewErrInvalidCertificateProperties("invalid unsafe network: %s", network) |
| 366 |
} |
| 367 |
|
| 368 |
if network.Addr().Is6() { |
| 369 |
return NewErrInvalidCertificateProperties("certificate may not contain IPv6 unsafe networks: %v", network) |
| 370 |
} |
| 371 |
|
| 372 |
if network.Addr().Zone() != "" { |
| 373 |
return NewErrInvalidCertificateProperties("unsafe networks may not contain zones: %s", network) |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
// v1 doesn't bother with sort order or uniqueness of networks or unsafe networks. |
| 378 |
// We can't modify the unmarshalled data because verification requires re-marshalling and a re-ordered |
| 379 |
// unsafe networks would result in a different signature. |
| 380 |
|
| 381 |
return nil |
| 382 |
} |
| 383 |
|
| 384 |
func (c *certificateV1) marshalForSigning() ([]byte, error) { |
| 385 |
b, err := proto.Marshal(c.getRawDetails()) |
| 386 |
if err != nil { |
| 387 |
return nil, err |
| 388 |
} |
| 389 |
return b, nil |
| 390 |
} |
| 391 |
|
| 392 |
func (c *certificateV1) setSignature(b []byte) error { |
| 393 |
if len(b) == 0 { |
| 394 |
return ErrEmptySignature |
| 395 |
} |
| 396 |
c.signature = b |
| 397 |
return nil |
| 398 |
} |
| 399 |
|
| 400 |
// unmarshalCertificateV1 will unmarshal a protobuf byte representation of a nebula cert |
| 401 |
// if the publicKey is provided here then it is not required to be present in `b` |
| 402 |
func unmarshalCertificateV1(b []byte, publicKey []byte) (*certificateV1, error) { |
| 403 |
if len(b) == 0 { |
| 404 |
return nil, fmt.Errorf("nil byte array") |
| 405 |
} |
| 406 |
var rc RawNebulaCertificate |
| 407 |
err := proto.Unmarshal(b, &rc) |
| 408 |
if err != nil { |
| 409 |
return nil, err |
| 410 |
} |
| 411 |
|
| 412 |
if rc.Details == nil { |
| 413 |
return nil, fmt.Errorf("encoded Details was nil") |
| 414 |
} |
| 415 |
|
| 416 |
if len(rc.Details.Ips)%2 != 0 { |
| 417 |
return nil, fmt.Errorf("encoded IPs should be in pairs, an odd number was found") |
| 418 |
} |
| 419 |
|
| 420 |
if len(rc.Details.Subnets)%2 != 0 { |
| 421 |
return nil, fmt.Errorf("encoded Subnets should be in pairs, an odd number was found") |
| 422 |
} |
| 423 |
|
| 424 |
nc := certificateV1{ |
| 425 |
details: detailsV1{ |
| 426 |
name: rc.Details.Name, |
| 427 |
groups: make([]string, len(rc.Details.Groups)), |
| 428 |
networks: make([]netip.Prefix, len(rc.Details.Ips)/2), |
| 429 |
unsafeNetworks: make([]netip.Prefix, len(rc.Details.Subnets)/2), |
| 430 |
notBefore: time.Unix(rc.Details.NotBefore, 0), |
| 431 |
notAfter: time.Unix(rc.Details.NotAfter, 0), |
| 432 |
publicKey: nil, |
| 433 |
isCA: rc.Details.IsCA, |
| 434 |
curve: rc.Details.Curve, |
| 435 |
}, |
| 436 |
signature: make([]byte, len(rc.Signature)), |
| 437 |
} |
| 438 |
|
| 439 |
copy(nc.signature, rc.Signature) |
| 440 |
copy(nc.details.groups, rc.Details.Groups) |
| 441 |
nc.details.issuer = hex.EncodeToString(rc.Details.Issuer) |
| 442 |
|
| 443 |
// If a public key is passed in as an argument, the certificate pubkey must be empty |
| 444 |
// and the passed-in pubkey copied into the cert. |
| 445 |
if len(publicKey) > 0 { |
| 446 |
if len(rc.Details.PublicKey) != 0 { |
| 447 |
return nil, ErrCertPubkeyPresent |
| 448 |
} |
| 449 |
nc.details.publicKey = make([]byte, len(publicKey)) |
| 450 |
copy(nc.details.publicKey, publicKey) |
| 451 |
} else { |
| 452 |
nc.details.publicKey = make([]byte, len(rc.Details.PublicKey)) |
| 453 |
copy(nc.details.publicKey, rc.Details.PublicKey) |
| 454 |
} |
| 455 |
|
| 456 |
var ip netip.Addr |
| 457 |
for i, rawIp := range rc.Details.Ips { |
| 458 |
if i%2 == 0 { |
| 459 |
ip = int2addr(rawIp) |
| 460 |
} else { |
| 461 |
ones, _ := net.IPMask(int2ip(rawIp)).Size() |
| 462 |
nc.details.networks[i/2] = netip.PrefixFrom(ip, ones) |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
for i, rawIp := range rc.Details.Subnets { |
| 467 |
if i%2 == 0 { |
| 468 |
ip = int2addr(rawIp) |
| 469 |
} else { |
| 470 |
ones, _ := net.IPMask(int2ip(rawIp)).Size() |
| 471 |
nc.details.unsafeNetworks[i/2] = netip.PrefixFrom(ip, ones) |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
err = nc.validate() |
| 476 |
if err != nil { |
| 477 |
return nil, err |
| 478 |
} |
| 479 |
|
| 480 |
return &nc, nil |
| 481 |
} |
| 482 |
|
| 483 |
func ip2int(ip []byte) uint32 { |
| 484 |
if len(ip) == 16 { |
| 485 |
return binary.BigEndian.Uint32(ip[12:16]) |
| 486 |
} |
| 487 |
return binary.BigEndian.Uint32(ip) |
| 488 |
} |
| 489 |
|
| 490 |
func int2ip(nn uint32) net.IP { |
| 491 |
ip := make(net.IP, net.IPv4len) |
| 492 |
binary.BigEndian.PutUint32(ip, nn) |
| 493 |
return ip |
| 494 |
} |
| 495 |
|
| 496 |
func addr2int(addr netip.Addr) uint32 { |
| 497 |
b := addr.Unmap().As4() |
| 498 |
return binary.BigEndian.Uint32(b[:]) |
| 499 |
} |
| 500 |
|
| 501 |
func int2addr(nn uint32) netip.Addr { |
| 502 |
ip := [4]byte{} |
| 503 |
binary.BigEndian.PutUint32(ip[:], nn) |
| 504 |
return netip.AddrFrom4(ip).Unmap() |
| 505 |
} |