| 1 |
package cert |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/netip" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/slackhq/nebula/cert/p256" |
| 9 |
) |
| 10 |
|
| 11 |
type Version uint8 |
| 12 |
|
| 13 |
const ( |
| 14 |
VersionPre1 Version = 0 |
| 15 |
Version1 Version = 1 |
| 16 |
Version2 Version = 2 |
| 17 |
) |
| 18 |
|
| 19 |
type Certificate interface { |
| 20 |
// Version defines the underlying certificate structure and wire protocol version |
| 21 |
// Version1 certificates are ipv4 only and uses protobuf serialization |
| 22 |
// Version2 certificates are ipv4 or ipv6 and uses asn.1 serialization |
| 23 |
Version() Version |
| 24 |
|
| 25 |
// Name is the human-readable name that identifies this certificate. |
| 26 |
Name() string |
| 27 |
|
| 28 |
// Networks is a list of ip addresses and network sizes assigned to this certificate. |
| 29 |
// If IsCA is true then certificates signed by this CA can only have ip addresses and |
| 30 |
// networks that are contained by an entry in this list. |
| 31 |
Networks() []netip.Prefix |
| 32 |
|
| 33 |
// UnsafeNetworks is a list of networks that this host can act as an unsafe router for. |
| 34 |
// If IsCA is true then certificates signed by this CA can only have networks that are |
| 35 |
// contained by an entry in this list. |
| 36 |
UnsafeNetworks() []netip.Prefix |
| 37 |
|
| 38 |
// Groups is a list of identities that can be used to write more general firewall rule |
| 39 |
// definitions. |
| 40 |
// If IsCA is true then certificates signed by this CA can only use groups that are |
| 41 |
// in this list. |
| 42 |
Groups() []string |
| 43 |
|
| 44 |
// IsCA signifies if this is a certificate authority (true) or a host certificate (false). |
| 45 |
// It is invalid to use a CA certificate as a host certificate. |
| 46 |
IsCA() bool |
| 47 |
|
| 48 |
// NotBefore is the time at which this certificate becomes valid. |
| 49 |
// If IsCA is true then certificate signed by this CA can not have a time before this. |
| 50 |
NotBefore() time.Time |
| 51 |
|
| 52 |
// NotAfter is the time at which this certificate becomes invalid. |
| 53 |
// If IsCA is true then certificate signed by this CA can not have a time after this. |
| 54 |
NotAfter() time.Time |
| 55 |
|
| 56 |
// Issuer is the fingerprint of the CA that signed this certificate. |
| 57 |
// If IsCA is true then this will be empty. |
| 58 |
Issuer() string |
| 59 |
|
| 60 |
// PublicKey is the raw bytes to be used in asymmetric cryptographic operations. |
| 61 |
PublicKey() []byte |
| 62 |
|
| 63 |
// MarshalPublicKeyPEM is the value of PublicKey marshalled to PEM |
| 64 |
MarshalPublicKeyPEM() []byte |
| 65 |
|
| 66 |
// Curve identifies which curve was used for the PublicKey and Signature. |
| 67 |
Curve() Curve |
| 68 |
|
| 69 |
// Signature is the cryptographic seal for all the details of this certificate. |
| 70 |
// CheckSignature can be used to verify that the details of this certificate are valid. |
| 71 |
Signature() []byte |
| 72 |
|
| 73 |
// CheckSignature will check that the certificate Signature() matches the |
| 74 |
// computed signature. A true result means this certificate has not been tampered with. |
| 75 |
CheckSignature(signingPublicKey []byte) bool |
| 76 |
|
| 77 |
// Fingerprint returns the hex encoded sha256 sum of the certificate. |
| 78 |
// This acts as a unique fingerprint and can be used to blocklist certificates. |
| 79 |
Fingerprint() (string, error) |
| 80 |
|
| 81 |
// Expired tests if the certificate is valid for the provided time. |
| 82 |
Expired(t time.Time) bool |
| 83 |
|
| 84 |
// VerifyPrivateKey returns an error if the private key is not a pair with the certificates public key. |
| 85 |
VerifyPrivateKey(curve Curve, privateKey []byte) error |
| 86 |
|
| 87 |
// Marshal will return the byte representation of this certificate |
| 88 |
// This is primarily the format transmitted on the wire. |
| 89 |
Marshal() ([]byte, error) |
| 90 |
|
| 91 |
// MarshalForHandshakes prepares the bytes needed to use directly in a handshake |
| 92 |
MarshalForHandshakes() ([]byte, error) |
| 93 |
|
| 94 |
// MarshalPEM will return a PEM encoded representation of this certificate |
| 95 |
// This is primarily the format stored on disk |
| 96 |
MarshalPEM() ([]byte, error) |
| 97 |
|
| 98 |
// MarshalJSON will return the json representation of this certificate |
| 99 |
MarshalJSON() ([]byte, error) |
| 100 |
|
| 101 |
// String will return a human-readable representation of this certificate |
| 102 |
String() string |
| 103 |
|
| 104 |
// Copy creates a copy of the certificate |
| 105 |
Copy() Certificate |
| 106 |
} |
| 107 |
|
| 108 |
// CachedCertificate represents a verified certificate with some cached fields to improve |
| 109 |
// performance. |
| 110 |
type CachedCertificate struct { |
| 111 |
Certificate Certificate |
| 112 |
InvertedGroups map[string]struct{} |
| 113 |
Fingerprint string |
| 114 |
signerFingerprint string |
| 115 |
|
| 116 |
// A place to store a 2nd fingerprint if the certificate could have one, such as with P256 |
| 117 |
fingerprint2 string |
| 118 |
} |
| 119 |
|
| 120 |
func (cc *CachedCertificate) String() string { |
| 121 |
return cc.Certificate.String() |
| 122 |
} |
| 123 |
|
| 124 |
// Recombine will attempt to unmarshal a certificate received in a handshake. |
| 125 |
// Handshakes save space by placing the peers public key in a different part of the packet, we have to |
| 126 |
// reassemble the actual certificate structure with that in mind. |
| 127 |
// Implementations MUST assert the public key is not in the raw certificate bytes if the passed in public key is not empty. |
| 128 |
func Recombine(v Version, rawCertBytes, publicKey []byte, curve Curve) (Certificate, error) { |
| 129 |
if publicKey == nil { |
| 130 |
return nil, ErrNoPeerStaticKey |
| 131 |
} |
| 132 |
|
| 133 |
if rawCertBytes == nil { |
| 134 |
return nil, ErrNoPayload |
| 135 |
} |
| 136 |
|
| 137 |
var c Certificate |
| 138 |
var err error |
| 139 |
|
| 140 |
switch v { |
| 141 |
// Implementations must ensure the result is a valid cert! |
| 142 |
case VersionPre1, Version1: |
| 143 |
c, err = unmarshalCertificateV1(rawCertBytes, publicKey) |
| 144 |
case Version2: |
| 145 |
c, err = unmarshalCertificateV2(rawCertBytes, publicKey, curve) |
| 146 |
default: |
| 147 |
return nil, ErrUnknownVersion |
| 148 |
} |
| 149 |
|
| 150 |
if err != nil { |
| 151 |
return nil, err |
| 152 |
} |
| 153 |
|
| 154 |
if c.Curve() != curve { |
| 155 |
return nil, fmt.Errorf("certificate curve %s does not match expected %s", c.Curve().String(), curve.String()) |
| 156 |
} |
| 157 |
|
| 158 |
return c, nil |
| 159 |
} |
| 160 |
|
| 161 |
// CalculateAlternateFingerprint calculates a 2nd fingerprint representation for P256 certificates |
| 162 |
// CAPool blocklist testing through `VerifyCertificate` and `VerifyCachedCertificate` automatically performs this step. |
| 163 |
func CalculateAlternateFingerprint(c Certificate) (string, error) { |
| 164 |
if c.Curve() != Curve_P256 { |
| 165 |
return "", nil |
| 166 |
} |
| 167 |
|
| 168 |
nc := c.Copy() |
| 169 |
b, err := p256.Swap(nc.Signature()) |
| 170 |
if err != nil { |
| 171 |
return "", err |
| 172 |
} |
| 173 |
|
| 174 |
switch v := nc.(type) { |
| 175 |
case *certificateV1: |
| 176 |
err = v.setSignature(b) |
| 177 |
case *certificateV2: |
| 178 |
err = v.setSignature(b) |
| 179 |
default: |
| 180 |
return "", ErrUnknownVersion |
| 181 |
} |
| 182 |
|
| 183 |
if err != nil { |
| 184 |
return "", err |
| 185 |
} |
| 186 |
return nc.Fingerprint() |
| 187 |
} |