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) {
|
|
|
|
t.Parallel()
|
2021-01-27 07:52:15 +00:00
|
|
|
ca, pk, err := GenerateCA(CAOpts{Signer: &TestSigner{}})
|
2018-12-19 08:22:49 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Empty(t, ca)
|
2021-01-27 07:52:15 +00:00
|
|
|
require.Empty(t, pk)
|
2018-12-19 08:22:49 +00:00
|
|
|
|
|
|
|
// test what happens with wrong key
|
2021-01-27 07:52:15 +00:00
|
|
|
ca, pk, err = GenerateCA(CAOpts{Signer: &TestSigner{public: &rsa.PublicKey{}}})
|
2018-12-19 08:22:49 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Empty(t, ca)
|
2021-01-27 07:52:15 +00:00
|
|
|
require.Empty(t, pk)
|
2018-12-19 08:22:49 +00:00
|
|
|
|
|
|
|
// test what happens with correct key
|
2021-01-27 07:52:15 +00:00
|
|
|
ca, pk, err = GenerateCA(CAOpts{})
|
2018-12-19 08:22:49 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEmpty(t, ca)
|
2021-01-27 07:52:15 +00:00
|
|
|
require.NotEmpty(t, pk)
|
2018-12-19 08:22:49 +00:00
|
|
|
|
|
|
|
cert, err := parseCert(ca)
|
|
|
|
require.Nil(t, err)
|
2021-01-27 07:52:15 +00:00
|
|
|
require.True(t, strings.HasPrefix(cert.Subject.CommonName, "Consul Agent CA"))
|
2018-12-19 08:22:49 +00:00
|
|
|
require.Equal(t, true, cert.IsCA)
|
|
|
|
require.Equal(t, true, cert.BasicConstraintsValid)
|
|
|
|
|
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.KeyUsageCertSign|x509.KeyUsageCRLSign|x509.KeyUsageDigitalSignature, cert.KeyUsage)
|
2021-01-20 23:25:48 +00:00
|
|
|
|
|
|
|
// Test what happens with a correct RSA Key
|
2021-02-05 18:32:08 +00:00
|
|
|
s, err := rsa.GenerateKey(rand.Reader, 2048)
|
2021-01-20 23:25:48 +00:00
|
|
|
require.Nil(t, err)
|
2021-02-05 18:32:08 +00:00
|
|
|
ca, _, err = GenerateCA(CAOpts{Signer: &TestSigner{public: s.Public()}})
|
|
|
|
require.NoError(t, err)
|
2021-01-20 23:25:48 +00:00
|
|
|
require.NotEmpty(t, ca)
|
|
|
|
|
|
|
|
cert, err = parseCert(ca)
|
2021-02-05 18:32:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, strings.HasPrefix(cert.Subject.CommonName, "Consul Agent CA"))
|
2021-01-20 23:25:48 +00:00
|
|
|
require.Equal(t, true, cert.IsCA)
|
|
|
|
require.Equal(t, true, cert.BasicConstraintsValid)
|
|
|
|
|
|
|
|
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]))
|
|
|
|
}
|