2018-07-09 18:43:13 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package peertls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-12-13 20:01:43 +00:00
|
|
|
"crypto"
|
2018-07-09 18:43:13 +01:00
|
|
|
"crypto/ecdsa"
|
2018-12-13 20:01:43 +00:00
|
|
|
"crypto/rand"
|
2018-07-09 18:43:13 +01:00
|
|
|
"crypto/x509"
|
2018-12-13 20:01:43 +00:00
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/asn1"
|
|
|
|
"encoding/gob"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-07-09 18:43:13 +01:00
|
|
|
"testing"
|
2018-12-13 20:01:43 +00:00
|
|
|
"time"
|
2018-07-09 18:43:13 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-12-13 20:01:43 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2018-07-09 18:43:13 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
)
|
|
|
|
|
2018-08-23 15:08:26 +01:00
|
|
|
func TestNewCert_CA(t *testing.T) {
|
2018-12-07 13:44:25 +00:00
|
|
|
caKey, err := NewKey()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
caTemplate, err := CATemplate()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
caCert, err := NewCert(caKey, nil, caTemplate, nil)
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NotEmpty(t, caKey.(*ecdsa.PrivateKey))
|
|
|
|
assert.NotEmpty(t, caCert)
|
|
|
|
assert.NotEmpty(t, caCert.PublicKey.(*ecdsa.PublicKey))
|
2018-07-09 18:43:13 +01:00
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
err = caCert.CheckSignatureFrom(caCert)
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:08:26 +01:00
|
|
|
func TestNewCert_Leaf(t *testing.T) {
|
2018-12-07 13:44:25 +00:00
|
|
|
caKey, err := NewKey()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
caTemplate, err := CATemplate()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
caCert, err := NewCert(caKey, nil, caTemplate, nil)
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
leafKey, err := NewKey()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
leafTemplate, err := LeafTemplate()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
leafCert, err := NewCert(leafKey, caKey, leafTemplate, caCert)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotEmpty(t, caKey.(*ecdsa.PrivateKey))
|
|
|
|
assert.NotEmpty(t, leafCert)
|
|
|
|
assert.NotEmpty(t, leafCert.PublicKey.(*ecdsa.PublicKey))
|
2018-07-09 18:43:13 +01:00
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
err = caCert.CheckSignatureFrom(caCert)
|
2018-08-23 15:08:26 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-07 13:44:25 +00:00
|
|
|
err = leafCert.CheckSignatureFrom(caCert)
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
func TestVerifyPeerFunc(t *testing.T) {
|
2018-12-13 20:01:43 +00:00
|
|
|
_, chain, err := newCertChain(2)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
leafCert, caCert := chain[0], chain[1]
|
2018-07-09 18:43:13 +01:00
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
testFunc := func(chain [][]byte, parsedChains [][]*x509.Certificate) error {
|
|
|
|
switch {
|
2018-12-07 13:44:25 +00:00
|
|
|
case !bytes.Equal(chain[1], caCert.Raw):
|
2018-08-13 09:39:45 +01:00
|
|
|
return errs.New("CA cert doesn't match")
|
2018-12-07 13:44:25 +00:00
|
|
|
case !bytes.Equal(chain[0], leafCert.Raw):
|
2018-08-13 09:39:45 +01:00
|
|
|
return errs.New("leaf's CA cert doesn't match")
|
2018-12-07 13:44:25 +00:00
|
|
|
case leafCert.PublicKey.(*ecdsa.PublicKey).Curve != parsedChains[0][0].PublicKey.(*ecdsa.PublicKey).Curve:
|
2018-08-13 09:39:45 +01:00
|
|
|
return errs.New("leaf public key doesn't match")
|
2018-12-07 13:44:25 +00:00
|
|
|
case leafCert.PublicKey.(*ecdsa.PublicKey).X.Cmp(parsedChains[0][0].PublicKey.(*ecdsa.PublicKey).X) != 0:
|
2018-08-13 09:39:45 +01:00
|
|
|
return errs.New("leaf public key doesn't match")
|
2018-12-07 13:44:25 +00:00
|
|
|
case leafCert.PublicKey.(*ecdsa.PublicKey).Y.Cmp(parsedChains[0][0].PublicKey.(*ecdsa.PublicKey).Y) != 0:
|
2018-08-13 09:39:45 +01:00
|
|
|
return errs.New("leaf public key doesn't match")
|
2018-12-07 13:44:25 +00:00
|
|
|
case !bytes.Equal(parsedChains[0][1].Raw, caCert.Raw):
|
2018-08-13 09:39:45 +01:00
|
|
|
return errs.New("parsed CA cert doesn't match")
|
2018-12-07 13:44:25 +00:00
|
|
|
case !bytes.Equal(parsedChains[0][0].Raw, leafCert.Raw):
|
2018-08-13 09:39:45 +01:00
|
|
|
return errs.New("parsed leaf cert doesn't match")
|
|
|
|
}
|
|
|
|
return nil
|
2018-07-09 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
err = VerifyPeerFunc(testFunc)([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
2018-08-13 09:39:45 +01:00
|
|
|
assert.NoError(t, err)
|
2018-07-09 18:43:13 +01:00
|
|
|
}
|
2018-08-23 15:08:26 +01:00
|
|
|
|
|
|
|
func TestVerifyPeerCertChains(t *testing.T) {
|
2018-12-13 20:01:43 +00:00
|
|
|
keys, chain, err := newCertChain(2)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
leafKey, leafCert, caCert := keys[1], chain[0], chain[1]
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
err = VerifyPeerFunc(VerifyPeerCertChains)([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
wrongKey, err := NewKey()
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
leafCert, err = NewCert(leafKey, wrongKey, leafCert, caCert)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
err = VerifyPeerFunc(VerifyPeerCertChains)([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.True(t, ErrVerifyPeerCert.Has(err))
|
|
|
|
assert.True(t, ErrVerifyCertificateChain.Has(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyCAWhitelist(t *testing.T) {
|
2018-12-13 20:01:43 +00:00
|
|
|
_, chain2, err := newCertChain(2)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
leafCert, caCert := chain2[0], chain2[1]
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
t.Run("empty whitelist", func(t *testing.T) {
|
|
|
|
err = VerifyPeerFunc(VerifyCAWhitelist(nil))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("whitelist contains ca", func(t *testing.T) {
|
|
|
|
err = VerifyPeerFunc(VerifyCAWhitelist([]*x509.Certificate{caCert}))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
_, unrelatedChain, err := newCertChain(1)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
unrelatedCert := unrelatedChain[0]
|
|
|
|
|
|
|
|
t.Run("no valid signed extension, non-empty whitelist", func(t *testing.T) {
|
|
|
|
err = VerifyPeerFunc(VerifyCAWhitelist([]*x509.Certificate{unrelatedCert}))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
|
|
|
assert.True(t, ErrVerifyCAWhitelist.Has(err))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("last cert in whitelist is signer", func(t *testing.T) {
|
|
|
|
err = VerifyPeerFunc(VerifyCAWhitelist([]*x509.Certificate{unrelatedCert, caCert}))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("first cert in whitelist is signer", func(t *testing.T) {
|
|
|
|
err = VerifyPeerFunc(VerifyCAWhitelist([]*x509.Certificate{caCert, unrelatedCert}))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
_, chain3, err := newCertChain(3)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
leaf2Cert, ca2Cert, rootCert := chain3[0], chain3[1], chain3[2]
|
|
|
|
|
|
|
|
t.Run("length 3 chain - first cert in whitelist is signer", func(t *testing.T) {
|
|
|
|
err = VerifyPeerFunc(VerifyCAWhitelist([]*x509.Certificate{rootCert, unrelatedCert}))([][]byte{leaf2Cert.Raw, ca2Cert.Raw, unrelatedCert.Raw}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("length 3 chain - last cert in whitelist is signer", func(t *testing.T) {
|
|
|
|
err = VerifyPeerFunc(VerifyCAWhitelist([]*x509.Certificate{unrelatedCert, rootCert}))([][]byte{leaf2Cert.Raw, ca2Cert.Raw, unrelatedCert.Raw}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddExtension(t *testing.T) {
|
|
|
|
_, chain, err := newCertChain(1)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: there's nothing special about length 32
|
|
|
|
randBytes := make([]byte, 32)
|
|
|
|
exampleID := asn1.ObjectIdentifier{2, 999}
|
|
|
|
i, err := rand.Read(randBytes)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
assert.Equal(t, 32, i)
|
|
|
|
|
|
|
|
ext := pkix.Extension{
|
|
|
|
Id: exampleID,
|
|
|
|
Value: randBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = AddExtension(chain[0], ext)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.Len(t, chain[0].ExtraExtensions, 1)
|
|
|
|
assert.Equal(t, ext, chain[0].ExtraExtensions[0])
|
|
|
|
}
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
func TestAddSignedCertExt(t *testing.T) {
|
|
|
|
keys, chain, err := newCertChain(1)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = AddSignedCertExt(keys[0], chain[0])
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.Len(t, chain[0].ExtraExtensions, 1)
|
|
|
|
assert.Equal(t, ExtensionIDs[SignedCertExtID], chain[0].ExtraExtensions[0].Id)
|
|
|
|
|
|
|
|
ecKey, ok := keys[0].(*ecdsa.PrivateKey)
|
|
|
|
if !assert.True(t, ok) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = VerifySignature(
|
|
|
|
chain[0].ExtraExtensions[0].Value,
|
|
|
|
chain[0].RawTBSCertificate,
|
|
|
|
&ecKey.PublicKey,
|
|
|
|
)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
}
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
func TestSignLeafExt(t *testing.T) {
|
|
|
|
keys, chain, err := newCertChain(2)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
caKey, leafCert := keys[0], chain[0]
|
|
|
|
|
|
|
|
err = AddSignedCertExt(caKey, leafCert)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.Equal(t, 1, len(leafCert.ExtraExtensions))
|
|
|
|
assert.True(t, ExtensionIDs[SignedCertExtID].Equal(leafCert.ExtraExtensions[0].Id))
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
caECKey, ok := caKey.(*ecdsa.PrivateKey)
|
|
|
|
if !assert.True(t, ok) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = VerifySignature(leafCert.ExtraExtensions[0].Value, leafCert.RawTBSCertificate, &caECKey.PublicKey)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
}
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
func TestRevocation_Sign(t *testing.T) {
|
|
|
|
keys, chain, err := newCertChain(2)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
leafCert, caKey := chain[0], keys[0]
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2018-12-18 11:55:55 +00:00
|
|
|
leafHash, err := SHA256Hash(leafCert.Raw)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
rev := Revocation{
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
CertHash: make([]byte, len(leafHash)),
|
|
|
|
}
|
|
|
|
copy(rev.CertHash, leafHash)
|
|
|
|
err = rev.Sign(caKey)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NotEmpty(t, rev.Signature)
|
|
|
|
}
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
func TestRevocation_Verify(t *testing.T) {
|
|
|
|
keys, chain, err := newCertChain(2)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
leafCert, caCert, caKey := chain[0], chain[1], keys[0]
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2018-12-18 11:55:55 +00:00
|
|
|
leafHash, err := SHA256Hash(leafCert.Raw)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
rev := Revocation{
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
CertHash: make([]byte, len(leafHash)),
|
|
|
|
}
|
|
|
|
copy(rev.CertHash, leafHash)
|
|
|
|
err = rev.Sign(caKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, rev.Signature)
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
err = rev.Verify(caCert)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
}
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
func TestRevocation_Marshal(t *testing.T) {
|
|
|
|
keys, chain, err := newCertChain(2)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
leafCert, caKey := chain[0], keys[0]
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2018-12-18 11:55:55 +00:00
|
|
|
leafHash, err := SHA256Hash(leafCert.Raw)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
rev := Revocation{
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
CertHash: make([]byte, len(leafHash)),
|
|
|
|
}
|
|
|
|
copy(rev.CertHash, leafHash)
|
|
|
|
err = rev.Sign(caKey)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NotEmpty(t, rev.Signature)
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
revBytes, err := rev.Marshal()
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NotEmpty(t, revBytes)
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
decodedRev := new(Revocation)
|
|
|
|
decoder := gob.NewDecoder(bytes.NewBuffer(revBytes))
|
|
|
|
err = decoder.Decode(decodedRev)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.Equal(t, rev, *decodedRev)
|
2018-12-07 13:44:25 +00:00
|
|
|
}
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
func TestRevocation_Unmarshal(t *testing.T) {
|
|
|
|
keys, chain, err := newCertChain(2)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
leafCert, caKey := chain[0], keys[0]
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2018-12-18 11:55:55 +00:00
|
|
|
leafHash, err := SHA256Hash(leafCert.Raw)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
rev := Revocation{
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
CertHash: make([]byte, len(leafHash)),
|
|
|
|
}
|
|
|
|
copy(rev.CertHash, leafHash)
|
|
|
|
err = rev.Sign(caKey)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NotEmpty(t, rev.Signature)
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
encodedRev := new(bytes.Buffer)
|
|
|
|
encoder := gob.NewEncoder(encodedRev)
|
|
|
|
err = encoder.Encode(rev)
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
unmarshaledRev := new(Revocation)
|
|
|
|
err = unmarshaledRev.Unmarshal(encodedRev.Bytes())
|
2018-11-01 15:48:43 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NotNil(t, rev)
|
|
|
|
assert.Equal(t, rev, *unmarshaledRev)
|
|
|
|
}
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
func TestNewRevocationExt(t *testing.T) {
|
|
|
|
keys, chain, err := newCertChain(2)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
ext, err := NewRevocationExt(keys[0], chain[0])
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-07 13:44:25 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
var rev Revocation
|
|
|
|
err = rev.Unmarshal(ext.Value)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = rev.Verify(chain[1])
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRevocationDB_Get(t *testing.T) {
|
2018-12-14 02:24:35 +00:00
|
|
|
tmp, err := ioutil.TempDir("", "TestRevocationDB_Get")
|
2018-12-13 20:01:43 +00:00
|
|
|
defer func() { _ = os.RemoveAll(tmp) }()
|
|
|
|
|
|
|
|
keys, chain, err := newCertChain(2)
|
|
|
|
if !assert.NoError(t, err) {
|
2018-12-07 13:44:25 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
ext, err := NewRevocationExt(keys[0], chain[0])
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
revDB, err := NewRevocationDBBolt(filepath.Join(tmp, "revocations.db"))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
var rev *Revocation
|
|
|
|
t.Run("missing key", func(t *testing.T) {
|
|
|
|
rev, err = revDB.Get(chain)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, rev)
|
|
|
|
})
|
|
|
|
|
2018-12-18 11:55:55 +00:00
|
|
|
caHash, err := SHA256Hash(chain[1].Raw)
|
2018-12-13 20:01:43 +00:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = revDB.DB.Put(caHash, ext.Value)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("existing key", func(t *testing.T) {
|
|
|
|
rev, err = revDB.Get(chain)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
revBytes, err := rev.Marshal()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, bytes.Equal(ext.Value, revBytes))
|
|
|
|
})
|
2018-12-07 13:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
func TestRevocationDB_Put(t *testing.T) {
|
2018-12-14 02:24:35 +00:00
|
|
|
tmp, err := ioutil.TempDir("", "TestRevocationDB_Put")
|
2018-12-13 20:01:43 +00:00
|
|
|
defer func() { _ = os.RemoveAll(tmp) }()
|
|
|
|
|
|
|
|
keys, chain, err := newCertChain(2)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
2018-12-07 13:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
olderExt, err := NewRevocationExt(keys[0], chain[0])
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
ext, err := NewRevocationExt(keys[0], chain[0])
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-12-07 13:44:25 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
newerExt, err := NewRevocationExt(keys[0], chain[0])
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
revDB, err := NewRevocationDBBolt(filepath.Join(tmp, "revocations.db"))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-12-07 13:44:25 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
ext pkix.Extension
|
|
|
|
errClass *errs.Class
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"new key",
|
|
|
|
ext,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"existing key - older timestamp",
|
|
|
|
olderExt,
|
|
|
|
&ErrExtension,
|
|
|
|
ErrRevocationTimestamp,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"existing key - newer timestamp",
|
|
|
|
newerExt,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t2 *testing.T) {
|
|
|
|
if !assert.NotNil(t, c.ext) {
|
|
|
|
t2.Fail()
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
err = revDB.Put(chain, c.ext)
|
|
|
|
if c.errClass != nil {
|
|
|
|
assert.True(t, c.errClass.Has(err))
|
|
|
|
}
|
|
|
|
if c.err != nil {
|
|
|
|
assert.Equal(t, c.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.err == nil && c.errClass == nil {
|
|
|
|
if !assert.NoError(t2, err) {
|
|
|
|
t2.Fail()
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
func(t2 *testing.T, ext pkix.Extension) {
|
2018-12-18 11:55:55 +00:00
|
|
|
caHash, err := SHA256Hash(chain[1].Raw)
|
2018-12-13 20:01:43 +00:00
|
|
|
if !assert.NoError(t2, err) {
|
|
|
|
t2.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
revBytes, err := revDB.DB.Get(caHash)
|
|
|
|
if !assert.NoError(t2, err) {
|
|
|
|
t2.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
rev := new(Revocation)
|
|
|
|
err = rev.Unmarshal(revBytes)
|
|
|
|
assert.NoError(t2, err)
|
|
|
|
assert.True(t2, bytes.Equal(ext.Value, revBytes))
|
|
|
|
}(t2, c.ext)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type extensionHandlerMock struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *extensionHandlerMock) verify(ext pkix.Extension, chain [][]*x509.Certificate) error {
|
|
|
|
args := m.Called(ext, chain)
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtensionHandlers_VerifyFunc(t *testing.T) {
|
|
|
|
keys, chain, err := newRevokedLeafChain()
|
|
|
|
chains := [][]*x509.Certificate{chain}
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
err = AddSignedCertExt(keys[0], chain[0])
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
extMock := new(extensionHandlerMock)
|
|
|
|
verify := func(ext pkix.Extension, chain [][]*x509.Certificate) error {
|
|
|
|
return extMock.verify(ext, chain)
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers := ExtensionHandlers{
|
|
|
|
{
|
|
|
|
id: ExtensionIDs[RevocationExtID],
|
|
|
|
verify: verify,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: ExtensionIDs[SignedCertExtID],
|
|
|
|
verify: verify,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
extMock.On("verify", chains[0][LeafIndex].ExtraExtensions[0], chains).Return(nil)
|
|
|
|
extMock.On("verify", chains[0][LeafIndex].ExtraExtensions[1], chains).Return(nil)
|
|
|
|
|
|
|
|
err = handlers.VerifyFunc()(nil, chains)
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
extMock.AssertCalled(t, "verify", chains[0][LeafIndex].ExtraExtensions[0], chains)
|
|
|
|
extMock.AssertCalled(t, "verify", chains[0][LeafIndex].ExtraExtensions[1], chains)
|
|
|
|
extMock.AssertExpectations(t)
|
2018-12-07 13:44:25 +00:00
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
// TODO: test error scenario(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseExtensions(t *testing.T) {
|
|
|
|
revokedLeafKeys, revokedLeafChain, err := newRevokedLeafChain()
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
whitelistSignedKeys, whitelistSignedChain, err := newCertChain(3)
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
err = AddSignedCertExt(whitelistSignedKeys[0], whitelistSignedChain[0])
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-13 20:01:43 +00:00
|
|
|
_, unrelatedChain, err := newCertChain(1)
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-14 02:24:35 +00:00
|
|
|
tmp, err := ioutil.TempDir("", "TestParseExtensions")
|
2018-12-13 20:01:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = os.RemoveAll(tmp) }()
|
|
|
|
revDB, err := NewRevocationDBBolt(filepath.Join(tmp, "revocations.db"))
|
|
|
|
assert.NoError(t, err)
|
2018-12-07 13:44:25 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
config TLSExtConfig
|
2018-12-13 20:01:43 +00:00
|
|
|
extLen int
|
|
|
|
certChain []*x509.Certificate
|
2018-12-07 13:44:25 +00:00
|
|
|
whitelist []*x509.Certificate
|
2018-12-13 20:01:43 +00:00
|
|
|
errClass *errs.Class
|
|
|
|
err error
|
2018-12-07 13:44:25 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-12-13 20:01:43 +00:00
|
|
|
"leaf whitelist signature - success",
|
2018-12-07 13:44:25 +00:00
|
|
|
TLSExtConfig{WhitelistSignedLeaf: true},
|
2018-12-13 20:01:43 +00:00
|
|
|
1,
|
|
|
|
whitelistSignedChain,
|
|
|
|
[]*x509.Certificate{whitelistSignedChain[2]},
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"leaf whitelist signature - failure (empty whitelist)",
|
|
|
|
TLSExtConfig{WhitelistSignedLeaf: true},
|
|
|
|
1,
|
|
|
|
whitelistSignedChain,
|
|
|
|
nil,
|
|
|
|
&ErrVerifyCAWhitelist,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"leaf whitelist signature - failure",
|
|
|
|
TLSExtConfig{WhitelistSignedLeaf: true},
|
|
|
|
1,
|
|
|
|
whitelistSignedChain,
|
|
|
|
unrelatedChain,
|
|
|
|
&ErrVerifyCAWhitelist,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"certificate revocation - single revocation ",
|
|
|
|
TLSExtConfig{Revocation: true},
|
|
|
|
1,
|
|
|
|
revokedLeafChain,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"certificate revocation - serial revocations",
|
|
|
|
TLSExtConfig{Revocation: true},
|
|
|
|
1,
|
|
|
|
func() []*x509.Certificate {
|
|
|
|
rev := new(Revocation)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
_, chain, err := revokeLeaf(revokedLeafKeys, revokedLeafChain)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = rev.Unmarshal(chain[0].ExtraExtensions[0].Value)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return chain
|
|
|
|
}(),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"certificate revocation - serial revocations error (older timestamp)",
|
|
|
|
TLSExtConfig{Revocation: true},
|
|
|
|
1,
|
|
|
|
func() []*x509.Certificate {
|
|
|
|
keys, chain, err := newRevokedLeafChain()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rev := new(Revocation)
|
|
|
|
err = rev.Unmarshal(chain[0].ExtraExtensions[0].Value)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rev.Timestamp = rev.Timestamp + 300
|
|
|
|
err = rev.Sign(keys[0])
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
revBytes, err := rev.Marshal()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = revDB.Put(chain, pkix.Extension{
|
|
|
|
Id: ExtensionIDs[RevocationExtID],
|
|
|
|
Value: revBytes,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
return chain
|
|
|
|
}(),
|
|
|
|
nil,
|
|
|
|
&ErrExtension,
|
|
|
|
ErrRevocationTimestamp,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"certificate revocation and leaf whitelist signature",
|
|
|
|
TLSExtConfig{Revocation: true, WhitelistSignedLeaf: true},
|
|
|
|
2,
|
|
|
|
func() []*x509.Certificate {
|
|
|
|
_, chain, err := newRevokedLeafChain()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = AddSignedCertExt(whitelistSignedKeys[0], chain[0])
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return chain
|
|
|
|
}(),
|
|
|
|
[]*x509.Certificate{whitelistSignedChain[2]},
|
|
|
|
nil,
|
|
|
|
nil,
|
2018-12-07 13:44:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
2018-12-13 20:01:43 +00:00
|
|
|
opts := ParseExtOptions{
|
|
|
|
CAWhitelist: c.whitelist,
|
|
|
|
RevDB: revDB,
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers := ParseExtensions(c.config, opts)
|
|
|
|
assert.Equal(t, c.extLen, len(handlers))
|
|
|
|
err := handlers.VerifyFunc()(nil, [][]*x509.Certificate{c.certChain})
|
|
|
|
if c.errClass != nil {
|
|
|
|
assert.True(t, c.errClass.Has(err))
|
|
|
|
}
|
|
|
|
if c.err != nil {
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
if c.errClass == nil && c.err == nil {
|
|
|
|
assert.NoError(t, err)
|
2018-12-07 13:44:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-08-23 15:08:26 +01:00
|
|
|
}
|
2018-12-13 20:01:43 +00:00
|
|
|
|
|
|
|
// NB: keys are in the reverse order compared to certs (i.e. first key belongs to last cert)!
|
|
|
|
func newCertChain(length int) (keys []crypto.PrivateKey, certs []*x509.Certificate, _ error) {
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
key, err := NewKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
keys = append(keys, key)
|
|
|
|
|
|
|
|
var template *x509.Certificate
|
|
|
|
if i == length-1 {
|
|
|
|
template, err = CATemplate()
|
|
|
|
} else {
|
|
|
|
template, err = LeafTemplate()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cert *x509.Certificate
|
|
|
|
if i == 0 {
|
|
|
|
cert, err = NewCert(key, nil, template, nil)
|
|
|
|
} else {
|
|
|
|
cert, err = NewCert(key, keys[i-1], template, certs[i-1:][0])
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
certs = append([]*x509.Certificate{cert}, certs...)
|
|
|
|
}
|
|
|
|
return keys, certs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func revokeLeaf(keys []crypto.PrivateKey, chain []*x509.Certificate) ([]crypto.PrivateKey, []*x509.Certificate, error) {
|
|
|
|
revokingKey, err := NewKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
revokingTemplate, err := LeafTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
revokingCert, err := NewCert(revokingKey, keys[0], revokingTemplate, chain[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = AddRevocationExt(keys[0], chain[0], revokingCert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys, append([]*x509.Certificate{revokingCert}, chain[1:]...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRevokedLeafChain() ([]crypto.PrivateKey, []*x509.Certificate, error) {
|
|
|
|
keys2, certs2, err := newCertChain(2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return revokeLeaf(keys2, certs2)
|
|
|
|
}
|