2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
package tlsutil
|
2018-12-19 08:22:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-01-27 07:52:15 +00:00
|
|
|
"strings"
|
2021-02-05 18:32:08 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-19 08:22:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSerialNumber(t *testing.T) {
|
|
|
|
n1, err := GenerateSerialNumber()
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
n2, err := GenerateSerialNumber()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEqual(t, n1, n2)
|
|
|
|
|
|
|
|
n3, err := GenerateSerialNumber()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEqual(t, n1, n3)
|
|
|
|
require.NotEqual(t, n2, n3)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGeneratePrivateKey(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
_, p, err := GeneratePrivateKey()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEmpty(t, p)
|
|
|
|
require.Contains(t, p, "BEGIN EC PRIVATE KEY")
|
|
|
|
require.Contains(t, p, "END EC PRIVATE KEY")
|
|
|
|
|
|
|
|
block, _ := pem.Decode([]byte(p))
|
|
|
|
pk, err := x509.ParseECPrivateKey(block.Bytes)
|
|
|
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, pk)
|
|
|
|
require.Equal(t, 256, pk.Params().BitSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestSigner struct {
|
|
|
|
public interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TestSigner) Public() crypto.PublicKey {
|
|
|
|
return s.public
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TestSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateCA(t *testing.T) {
|
2021-04-13 17:31:20 +00:00
|
|
|
t.Run("no signer", func(t *testing.T) {
|
|
|
|
ca, pk, err := GenerateCA(CAOpts{Signer: &TestSigner{}})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Empty(t, ca)
|
|
|
|
require.Empty(t, pk)
|
|
|
|
})
|
2018-12-19 08:22:49 +00:00
|
|
|
|
2021-04-13 17:31:20 +00:00
|
|
|
t.Run("wrong key", func(t *testing.T) {
|
|
|
|
ca, pk, err := GenerateCA(CAOpts{Signer: &TestSigner{public: &rsa.PublicKey{}}})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Empty(t, ca)
|
|
|
|
require.Empty(t, pk)
|
|
|
|
})
|
2018-12-19 08:22:49 +00:00
|
|
|
|
2021-04-13 17:31:20 +00:00
|
|
|
t.Run("valid key", func(t *testing.T) {
|
|
|
|
ca, pk, err := GenerateCA(CAOpts{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEmpty(t, ca)
|
|
|
|
require.NotEmpty(t, pk)
|
2018-12-19 08:22:49 +00:00
|
|
|
|
2021-04-13 17:31:20 +00:00
|
|
|
cert, err := parseCert(ca)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.True(t, strings.HasPrefix(cert.Subject.CommonName, "Consul Agent CA"))
|
|
|
|
require.Equal(t, true, cert.IsCA)
|
|
|
|
require.Equal(t, true, cert.BasicConstraintsValid)
|
2021-01-20 23:25:48 +00:00
|
|
|
|
2021-04-13 17:31:20 +00:00
|
|
|
require.WithinDuration(t, cert.NotBefore, time.Now(), time.Minute)
|
|
|
|
require.WithinDuration(t, cert.NotAfter, time.Now().AddDate(0, 0, 365), time.Minute)
|
|
|
|
|
|
|
|
require.Equal(t, x509.KeyUsageCertSign|x509.KeyUsageCRLSign|x509.KeyUsageDigitalSignature, cert.KeyUsage)
|
|
|
|
})
|
2021-01-20 23:25:48 +00:00
|
|
|
|
2021-04-13 17:31:20 +00:00
|
|
|
t.Run("RSA key", func(t *testing.T) {
|
|
|
|
ca, pk, err := GenerateCA(CAOpts{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, ca)
|
|
|
|
require.NotEmpty(t, pk)
|
2021-01-20 23:25:48 +00:00
|
|
|
|
2021-04-13 17:31:20 +00:00
|
|
|
cert, err := parseCert(ca)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, strings.HasPrefix(cert.Subject.CommonName, "Consul Agent CA"))
|
|
|
|
require.Equal(t, true, cert.IsCA)
|
|
|
|
require.Equal(t, true, cert.BasicConstraintsValid)
|
2021-01-20 23:25:48 +00:00
|
|
|
|
2021-04-13 17:31:20 +00:00
|
|
|
require.WithinDuration(t, cert.NotBefore, time.Now(), time.Minute)
|
|
|
|
require.WithinDuration(t, cert.NotAfter, time.Now().AddDate(0, 0, 365), time.Minute)
|
|
|
|
|
|
|
|
require.Equal(t, x509.KeyUsageCertSign|x509.KeyUsageCRLSign|x509.KeyUsageDigitalSignature, cert.KeyUsage)
|
|
|
|
})
|
2018-12-19 08:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateCert(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
signer, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
require.Nil(t, err)
|
2021-01-27 07:52:15 +00:00
|
|
|
ca, _, err := GenerateCA(CAOpts{Signer: signer})
|
2018-12-19 08:22:49 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
DNSNames := []string{"server.dc1.consul"}
|
|
|
|
IPAddresses := []net.IP{net.ParseIP("123.234.243.213")}
|
|
|
|
extKeyUsage := []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}
|
|
|
|
name := "Cert Name"
|
2021-03-22 09:16:41 +00:00
|
|
|
certificate, pk, err := GenerateCert(CertOpts{
|
|
|
|
Signer: signer, CA: ca, Name: name, Days: 365,
|
|
|
|
DNSNames: DNSNames, IPAddresses: IPAddresses, ExtKeyUsage: extKeyUsage,
|
|
|
|
})
|
2018-12-19 08:22:49 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEmpty(t, certificate)
|
|
|
|
require.NotEmpty(t, pk)
|
|
|
|
|
|
|
|
cert, err := parseCert(certificate)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, name, cert.Subject.CommonName)
|
|
|
|
require.Equal(t, true, cert.BasicConstraintsValid)
|
|
|
|
signee, err := ParseSigner(pk)
|
|
|
|
require.Nil(t, err)
|
|
|
|
certID, err := keyID(signee.Public())
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, certID, cert.SubjectKeyId)
|
|
|
|
caID, err := keyID(signer.Public())
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, caID, cert.AuthorityKeyId)
|
|
|
|
require.Contains(t, cert.Issuer.CommonName, "Consul Agent CA")
|
|
|
|
require.Equal(t, false, cert.IsCA)
|
|
|
|
|
2019-12-13 16:42:42 +00:00
|
|
|
require.WithinDuration(t, cert.NotBefore, time.Now(), time.Minute)
|
|
|
|
require.WithinDuration(t, cert.NotAfter, time.Now().AddDate(0, 0, 365), time.Minute)
|
2018-12-19 08:22:49 +00:00
|
|
|
|
|
|
|
require.Equal(t, x509.KeyUsageDigitalSignature|x509.KeyUsageKeyEncipherment, cert.KeyUsage)
|
|
|
|
require.Equal(t, extKeyUsage, cert.ExtKeyUsage)
|
|
|
|
|
|
|
|
// https://github.com/golang/go/blob/10538a8f9e2e718a47633ac5a6e90415a2c3f5f1/src/crypto/x509/verify.go#L414
|
|
|
|
require.Equal(t, DNSNames, cert.DNSNames)
|
|
|
|
require.True(t, IPAddresses[0].Equal(cert.IPAddresses[0]))
|
|
|
|
}
|