| 1 |
package cert |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ed25519" |
| 5 |
"crypto/rand" |
| 6 |
"encoding/hex" |
| 7 |
"net/netip" |
| 8 |
"slices" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/slackhq/nebula/test" |
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
) |
| 16 |
|
| 17 |
func TestCertificateV2_Marshal(t *testing.T) { |
| 18 |
t.Parallel() |
| 19 |
before := time.Now().Add(time.Second * -60).Round(time.Second) |
| 20 |
after := time.Now().Add(time.Second * 60).Round(time.Second) |
| 21 |
pubKey := []byte("1234567890abcedfghij1234567890ab") |
| 22 |
|
| 23 |
nc := certificateV2{ |
| 24 |
details: detailsV2{ |
| 25 |
name: "testing", |
| 26 |
networks: []netip.Prefix{ |
| 27 |
mustParsePrefixUnmapped("10.1.1.2/16"), |
| 28 |
mustParsePrefixUnmapped("10.1.1.1/24"), |
| 29 |
}, |
| 30 |
unsafeNetworks: []netip.Prefix{ |
| 31 |
mustParsePrefixUnmapped("9.1.1.3/16"), |
| 32 |
mustParsePrefixUnmapped("9.1.1.2/24"), |
| 33 |
}, |
| 34 |
groups: []string{"test-group1", "test-group2", "test-group3"}, |
| 35 |
notBefore: before, |
| 36 |
notAfter: after, |
| 37 |
isCA: false, |
| 38 |
issuer: "1234567890abcdef1234567890abcdef", |
| 39 |
}, |
| 40 |
signature: []byte("1234567890abcdef1234567890abcdef"), |
| 41 |
publicKey: pubKey, |
| 42 |
} |
| 43 |
|
| 44 |
db, err := nc.details.Marshal() |
| 45 |
require.NoError(t, err) |
| 46 |
nc.rawDetails = db |
| 47 |
|
| 48 |
b, err := nc.Marshal() |
| 49 |
require.NoError(t, err) |
| 50 |
//t.Log("Cert size:", len(b)) |
| 51 |
|
| 52 |
nc2, err := unmarshalCertificateV2(b, nil, Curve_CURVE25519) |
| 53 |
require.NoError(t, err) |
| 54 |
|
| 55 |
assert.Equal(t, Version2, nc.Version()) |
| 56 |
assert.Equal(t, Curve_CURVE25519, nc.Curve()) |
| 57 |
assert.Equal(t, nc.Signature(), nc2.Signature()) |
| 58 |
assert.Equal(t, nc.Name(), nc2.Name()) |
| 59 |
assert.Equal(t, nc.NotBefore(), nc2.NotBefore()) |
| 60 |
assert.Equal(t, nc.NotAfter(), nc2.NotAfter()) |
| 61 |
assert.Equal(t, nc.PublicKey(), nc2.PublicKey()) |
| 62 |
assert.Equal(t, nc.IsCA(), nc2.IsCA()) |
| 63 |
assert.Equal(t, nc.Issuer(), nc2.Issuer()) |
| 64 |
|
| 65 |
// unmarshalling will sort networks and unsafeNetworks, we need to do the same |
| 66 |
// but first make sure it fails |
| 67 |
assert.NotEqual(t, nc.Networks(), nc2.Networks()) |
| 68 |
assert.NotEqual(t, nc.UnsafeNetworks(), nc2.UnsafeNetworks()) |
| 69 |
|
| 70 |
slices.SortFunc(nc.details.networks, comparePrefix) |
| 71 |
slices.SortFunc(nc.details.unsafeNetworks, comparePrefix) |
| 72 |
|
| 73 |
assert.Equal(t, nc.Networks(), nc2.Networks()) |
| 74 |
assert.Equal(t, nc.UnsafeNetworks(), nc2.UnsafeNetworks()) |
| 75 |
|
| 76 |
assert.Equal(t, nc.Groups(), nc2.Groups()) |
| 77 |
} |
| 78 |
|
| 79 |
func TestCertificateV2_Unmarshal(t *testing.T) { |
| 80 |
t.Parallel() |
| 81 |
before := time.Now().Add(time.Second * -60).Round(time.Second) |
| 82 |
after := time.Now().Add(time.Second * 60).Round(time.Second) |
| 83 |
pubKey := []byte("1234567890abcedfghij1234567890ab") |
| 84 |
|
| 85 |
nc := certificateV2{ |
| 86 |
details: detailsV2{ |
| 87 |
name: "testing", |
| 88 |
networks: []netip.Prefix{ |
| 89 |
mustParsePrefixUnmapped("10.1.1.2/16"), |
| 90 |
mustParsePrefixUnmapped("10.1.1.1/24"), |
| 91 |
}, |
| 92 |
unsafeNetworks: []netip.Prefix{ |
| 93 |
mustParsePrefixUnmapped("9.1.1.3/16"), |
| 94 |
mustParsePrefixUnmapped("9.1.1.2/24"), |
| 95 |
}, |
| 96 |
groups: []string{"test-group1", "test-group2", "test-group3"}, |
| 97 |
notBefore: before, |
| 98 |
notAfter: after, |
| 99 |
isCA: false, |
| 100 |
issuer: "1234567890abcdef1234567890abcdef", |
| 101 |
}, |
| 102 |
signature: []byte("1234567890abcdef1234567890abcdef"), |
| 103 |
publicKey: pubKey, |
| 104 |
} |
| 105 |
|
| 106 |
db, err := nc.details.Marshal() |
| 107 |
require.NoError(t, err) |
| 108 |
nc.rawDetails = db |
| 109 |
|
| 110 |
certWithPubkey, err := nc.Marshal() |
| 111 |
require.NoError(t, err) |
| 112 |
//t.Log("Cert size:", len(b)) |
| 113 |
certWithoutPubkey, err := nc.MarshalForHandshakes() |
| 114 |
require.NoError(t, err) |
| 115 |
|
| 116 |
// Cert must not have a pubkey if one is passed in as an argument |
| 117 |
_, err = unmarshalCertificateV2(certWithPubkey, pubKey, Curve_CURVE25519) |
| 118 |
require.ErrorIs(t, err, ErrCertPubkeyPresent) |
| 119 |
|
| 120 |
// Certs must have pubkeys |
| 121 |
_, err = unmarshalCertificateV2(certWithoutPubkey, nil, Curve_CURVE25519) |
| 122 |
require.ErrorIs(t, err, ErrBadFormat) |
| 123 |
|
| 124 |
// Ensure proper unmarshal if a pubkey is passed in |
| 125 |
nc2, err := unmarshalCertificateV2(certWithoutPubkey, pubKey, Curve_CURVE25519) |
| 126 |
require.NoError(t, err) |
| 127 |
|
| 128 |
assert.Equal(t, nc.PublicKey(), nc2.PublicKey()) |
| 129 |
} |
| 130 |
|
| 131 |
func TestCertificateV2_PublicKeyPem(t *testing.T) { |
| 132 |
t.Parallel() |
| 133 |
before := time.Now().Add(time.Second * -60).Round(time.Second) |
| 134 |
after := time.Now().Add(time.Second * 60).Round(time.Second) |
| 135 |
pubKey := ed25519.PublicKey("1234567890abcedfghij1234567890ab") |
| 136 |
|
| 137 |
nc := certificateV2{ |
| 138 |
details: detailsV2{ |
| 139 |
name: "testing", |
| 140 |
networks: []netip.Prefix{}, |
| 141 |
unsafeNetworks: []netip.Prefix{}, |
| 142 |
groups: []string{"test-group1", "test-group2", "test-group3"}, |
| 143 |
notBefore: before, |
| 144 |
notAfter: after, |
| 145 |
isCA: false, |
| 146 |
issuer: "1234567890abcedfghij1234567890ab", |
| 147 |
}, |
| 148 |
publicKey: pubKey, |
| 149 |
signature: []byte("1234567890abcedfghij1234567890ab"), |
| 150 |
} |
| 151 |
|
| 152 |
assert.Equal(t, Version2, nc.Version()) |
| 153 |
assert.Equal(t, Curve_CURVE25519, nc.Curve()) |
| 154 |
pubPem := "-----BEGIN NEBULA X25519 PUBLIC KEY-----\nMTIzNDU2Nzg5MGFiY2VkZmdoaWoxMjM0NTY3ODkwYWI=\n-----END NEBULA X25519 PUBLIC KEY-----\n" |
| 155 |
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), pubPem) |
| 156 |
assert.False(t, nc.IsCA()) |
| 157 |
|
| 158 |
nc.details.isCA = true |
| 159 |
assert.Equal(t, Curve_CURVE25519, nc.Curve()) |
| 160 |
pubPem = "-----BEGIN NEBULA ED25519 PUBLIC KEY-----\nMTIzNDU2Nzg5MGFiY2VkZmdoaWoxMjM0NTY3ODkwYWI=\n-----END NEBULA ED25519 PUBLIC KEY-----\n" |
| 161 |
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), pubPem) |
| 162 |
assert.True(t, nc.IsCA()) |
| 163 |
|
| 164 |
pubP256KeyPem := []byte(`-----BEGIN NEBULA P256 PUBLIC KEY----- |
| 165 |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 166 |
AAAAAAAAAAAAAAAAAAAAAAA= |
| 167 |
-----END NEBULA P256 PUBLIC KEY----- |
| 168 |
`) |
| 169 |
|
| 170 |
pubP256KeyPemCA := []byte(`-----BEGIN NEBULA ECDSA P256 PUBLIC KEY----- |
| 171 |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 172 |
AAAAAAAAAAAAAAAAAAAAAAA= |
| 173 |
-----END NEBULA ECDSA P256 PUBLIC KEY----- |
| 174 |
`) |
| 175 |
|
| 176 |
pubP256Key, _, _, err := UnmarshalPublicKeyFromPEM(pubP256KeyPem) |
| 177 |
require.NoError(t, err) |
| 178 |
nc.curve = Curve_P256 |
| 179 |
nc.publicKey = pubP256Key |
| 180 |
assert.Equal(t, Curve_P256, nc.Curve()) |
| 181 |
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), string(pubP256KeyPemCA)) |
| 182 |
assert.True(t, nc.IsCA()) |
| 183 |
|
| 184 |
nc.details.isCA = false |
| 185 |
assert.Equal(t, Curve_P256, nc.Curve()) |
| 186 |
assert.Equal(t, string(nc.MarshalPublicKeyPEM()), string(pubP256KeyPem)) |
| 187 |
assert.False(t, nc.IsCA()) |
| 188 |
} |
| 189 |
|
| 190 |
func TestCertificateV2_Expired(t *testing.T) { |
| 191 |
nc := certificateV2{ |
| 192 |
details: detailsV2{ |
| 193 |
notBefore: time.Now().Add(time.Second * -60).Round(time.Second), |
| 194 |
notAfter: time.Now().Add(time.Second * 60).Round(time.Second), |
| 195 |
}, |
| 196 |
} |
| 197 |
|
| 198 |
assert.True(t, nc.Expired(time.Now().Add(time.Hour))) |
| 199 |
assert.True(t, nc.Expired(time.Now().Add(-time.Hour))) |
| 200 |
assert.False(t, nc.Expired(time.Now())) |
| 201 |
} |
| 202 |
|
| 203 |
func TestCertificateV2_MarshalJSON(t *testing.T) { |
| 204 |
time.Local = time.UTC |
| 205 |
pubKey := []byte("1234567890abcedf1234567890abcedf") |
| 206 |
|
| 207 |
nc := certificateV2{ |
| 208 |
details: detailsV2{ |
| 209 |
name: "testing", |
| 210 |
networks: []netip.Prefix{ |
| 211 |
mustParsePrefixUnmapped("10.1.1.1/24"), |
| 212 |
mustParsePrefixUnmapped("10.1.1.2/16"), |
| 213 |
}, |
| 214 |
unsafeNetworks: []netip.Prefix{ |
| 215 |
mustParsePrefixUnmapped("9.1.1.2/24"), |
| 216 |
mustParsePrefixUnmapped("9.1.1.3/16"), |
| 217 |
}, |
| 218 |
groups: []string{"test-group1", "test-group2", "test-group3"}, |
| 219 |
notBefore: time.Date(1, 0, 0, 1, 0, 0, 0, time.UTC), |
| 220 |
notAfter: time.Date(1, 0, 0, 2, 0, 0, 0, time.UTC), |
| 221 |
isCA: false, |
| 222 |
issuer: "1234567890abcedf1234567890abcedf", |
| 223 |
}, |
| 224 |
publicKey: pubKey, |
| 225 |
signature: []byte("1234567890abcedf1234567890abcedf1234567890abcedf1234567890abcedf"), |
| 226 |
} |
| 227 |
|
| 228 |
b, err := nc.MarshalJSON() |
| 229 |
require.ErrorIs(t, err, ErrMissingDetails) |
| 230 |
|
| 231 |
rd, err := nc.details.Marshal() |
| 232 |
require.NoError(t, err) |
| 233 |
|
| 234 |
nc.rawDetails = rd |
| 235 |
b, err = nc.MarshalJSON() |
| 236 |
require.NoError(t, err) |
| 237 |
assert.JSONEq( |
| 238 |
t, |
| 239 |
"{\"curve\":\"CURVE25519\",\"details\":{\"groups\":[\"test-group1\",\"test-group2\",\"test-group3\"],\"isCa\":false,\"issuer\":\"1234567890abcedf1234567890abcedf\",\"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\",\"unsafeNetworks\":[\"9.1.1.2/24\",\"9.1.1.3/16\"]},\"fingerprint\":\"152d9a7400c1e001cb76cffd035215ebb351f69eeb797f7f847dd086e15e56dd\",\"publicKey\":\"3132333435363738393061626365646631323334353637383930616263656466\",\"signature\":\"31323334353637383930616263656466313233343536373839306162636564663132333435363738393061626365646631323334353637383930616263656466\",\"version\":2}", |
| 240 |
string(b), |
| 241 |
) |
| 242 |
} |
| 243 |
|
| 244 |
func TestCertificateV2_VerifyPrivateKey(t *testing.T) { |
| 245 |
ca, _, caKey, _ := NewTestCaCert(Version2, Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil) |
| 246 |
err := ca.VerifyPrivateKey(Curve_CURVE25519, caKey) |
| 247 |
require.NoError(t, err) |
| 248 |
|
| 249 |
err = ca.VerifyPrivateKey(Curve_CURVE25519, caKey[:16]) |
| 250 |
require.ErrorIs(t, err, ErrInvalidPrivateKey) |
| 251 |
|
| 252 |
_, caKey2, err := ed25519.GenerateKey(rand.Reader) |
| 253 |
require.NoError(t, err) |
| 254 |
err = ca.VerifyPrivateKey(Curve_CURVE25519, caKey2) |
| 255 |
require.ErrorIs(t, err, ErrPublicPrivateKeyMismatch) |
| 256 |
|
| 257 |
c, _, priv, _ := NewTestCert(Version2, Curve_CURVE25519, ca, caKey, "test", time.Time{}, time.Time{}, nil, nil, nil) |
| 258 |
rawPriv, b, curve, err := UnmarshalPrivateKeyFromPEM(priv) |
| 259 |
require.NoError(t, err) |
| 260 |
assert.Empty(t, b) |
| 261 |
assert.Equal(t, Curve_CURVE25519, curve) |
| 262 |
err = c.VerifyPrivateKey(Curve_CURVE25519, rawPriv) |
| 263 |
require.NoError(t, err) |
| 264 |
|
| 265 |
_, priv2 := X25519Keypair() |
| 266 |
err = c.VerifyPrivateKey(Curve_P256, priv2) |
| 267 |
require.ErrorIs(t, err, ErrPublicPrivateCurveMismatch) |
| 268 |
|
| 269 |
err = c.VerifyPrivateKey(Curve_CURVE25519, priv2) |
| 270 |
require.ErrorIs(t, err, ErrPublicPrivateKeyMismatch) |
| 271 |
|
| 272 |
err = c.VerifyPrivateKey(Curve_CURVE25519, priv2[:16]) |
| 273 |
require.ErrorIs(t, err, ErrInvalidPrivateKey) |
| 274 |
|
| 275 |
ac, ok := c.(*certificateV2) |
| 276 |
require.True(t, ok) |
| 277 |
ac.curve = Curve(99) |
| 278 |
err = c.VerifyPrivateKey(Curve(99), priv2) |
| 279 |
require.EqualError(t, err, "invalid curve: 99") |
| 280 |
|
| 281 |
ca2, _, caKey2, _ := NewTestCaCert(Version2, Curve_P256, time.Time{}, time.Time{}, nil, nil, nil) |
| 282 |
err = ca.VerifyPrivateKey(Curve_CURVE25519, caKey) |
| 283 |
require.NoError(t, err) |
| 284 |
|
| 285 |
err = ca2.VerifyPrivateKey(Curve_P256, caKey2[:16]) |
| 286 |
require.ErrorIs(t, err, ErrInvalidPrivateKey) |
| 287 |
|
| 288 |
c, _, priv, _ = NewTestCert(Version2, Curve_P256, ca2, caKey2, "test", time.Time{}, time.Time{}, nil, nil, nil) |
| 289 |
rawPriv, b, curve, err = UnmarshalPrivateKeyFromPEM(priv) |
| 290 |
|
| 291 |
err = c.VerifyPrivateKey(Curve_P256, priv[:16]) |
| 292 |
require.ErrorIs(t, err, ErrInvalidPrivateKey) |
| 293 |
|
| 294 |
err = c.VerifyPrivateKey(Curve_P256, priv) |
| 295 |
require.ErrorIs(t, err, ErrInvalidPrivateKey) |
| 296 |
|
| 297 |
aCa, ok := ca2.(*certificateV2) |
| 298 |
require.True(t, ok) |
| 299 |
aCa.curve = Curve(99) |
| 300 |
err = aCa.VerifyPrivateKey(Curve(99), priv2) |
| 301 |
require.EqualError(t, err, "invalid curve: 99") |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
func TestCertificateV2_VerifyPrivateKeyP256(t *testing.T) { |
| 306 |
ca, _, caKey, _ := NewTestCaCert(Version2, Curve_P256, time.Time{}, time.Time{}, nil, nil, nil) |
| 307 |
err := ca.VerifyPrivateKey(Curve_P256, caKey) |
| 308 |
require.NoError(t, err) |
| 309 |
|
| 310 |
_, _, caKey2, _ := NewTestCaCert(Version2, Curve_P256, time.Time{}, time.Time{}, nil, nil, nil) |
| 311 |
require.NoError(t, err) |
| 312 |
err = ca.VerifyPrivateKey(Curve_P256, caKey2) |
| 313 |
require.Error(t, err) |
| 314 |
|
| 315 |
c, _, priv, _ := NewTestCert(Version2, Curve_P256, ca, caKey, "test", time.Time{}, time.Time{}, nil, nil, nil) |
| 316 |
rawPriv, b, curve, err := UnmarshalPrivateKeyFromPEM(priv) |
| 317 |
require.NoError(t, err) |
| 318 |
assert.Empty(t, b) |
| 319 |
assert.Equal(t, Curve_P256, curve) |
| 320 |
err = c.VerifyPrivateKey(Curve_P256, rawPriv) |
| 321 |
require.NoError(t, err) |
| 322 |
|
| 323 |
_, priv2 := P256Keypair() |
| 324 |
err = c.VerifyPrivateKey(Curve_P256, priv2) |
| 325 |
require.Error(t, err) |
| 326 |
} |
| 327 |
|
| 328 |
func TestCertificateV2_Copy(t *testing.T) { |
| 329 |
ca, _, caKey, _ := NewTestCaCert(Version2, Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, nil) |
| 330 |
c, _, _, _ := NewTestCert(Version2, Curve_CURVE25519, ca, caKey, "test", time.Now(), time.Now().Add(5*time.Minute), nil, nil, nil) |
| 331 |
cc := c.Copy() |
| 332 |
test.AssertDeepCopyEqual(t, c, cc) |
| 333 |
} |
| 334 |
|
| 335 |
func TestUnmarshalCertificateV2(t *testing.T) { |
| 336 |
data := []byte("\x98\x00\x00") |
| 337 |
_, err := unmarshalCertificateV2(data, nil, Curve_CURVE25519) |
| 338 |
require.EqualError(t, err, "bad wire format") |
| 339 |
} |
| 340 |
|
| 341 |
func TestCertificateV2_marshalForSigningStability(t *testing.T) { |
| 342 |
before := time.Date(1996, time.May, 5, 0, 0, 0, 0, time.UTC) |
| 343 |
after := before.Add(time.Second * 60).Round(time.Second) |
| 344 |
pubKey := []byte("1234567890abcedfghij1234567890ab") |
| 345 |
|
| 346 |
nc := certificateV2{ |
| 347 |
details: detailsV2{ |
| 348 |
name: "testing", |
| 349 |
networks: []netip.Prefix{ |
| 350 |
mustParsePrefixUnmapped("10.1.1.2/16"), |
| 351 |
mustParsePrefixUnmapped("10.1.1.1/24"), |
| 352 |
}, |
| 353 |
unsafeNetworks: []netip.Prefix{ |
| 354 |
mustParsePrefixUnmapped("9.1.1.3/16"), |
| 355 |
mustParsePrefixUnmapped("9.1.1.2/24"), |
| 356 |
}, |
| 357 |
groups: []string{"test-group1", "test-group2", "test-group3"}, |
| 358 |
notBefore: before, |
| 359 |
notAfter: after, |
| 360 |
isCA: false, |
| 361 |
issuer: "1234567890abcdef1234567890abcdef", |
| 362 |
}, |
| 363 |
signature: []byte("1234567890abcdef1234567890abcdef"), |
| 364 |
publicKey: pubKey, |
| 365 |
} |
| 366 |
|
| 367 |
const expectedRawDetailsStr = "a070800774657374696e67a10e04050a0101021004050a01010118a20e0405090101031004050901010218a3270c0b746573742d67726f7570310c0b746573742d67726f7570320c0b746573742d67726f7570338504318bef808604318befbc87101234567890abcdef1234567890abcdef" |
| 368 |
expectedRawDetails, err := hex.DecodeString(expectedRawDetailsStr) |
| 369 |
require.NoError(t, err) |
| 370 |
|
| 371 |
db, err := nc.details.Marshal() |
| 372 |
require.NoError(t, err) |
| 373 |
assert.Equal(t, expectedRawDetails, db) |
| 374 |
|
| 375 |
expectedForSigning, err := hex.DecodeString(expectedRawDetailsStr + "00313233343536373839306162636564666768696a313233343536373839306162") |
| 376 |
b, err := nc.marshalForSigning() |
| 377 |
require.NoError(t, err) |
| 378 |
assert.Equal(t, expectedForSigning, b) |
| 379 |
} |