2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-07-09 18:43:13 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
package peertls_test
|
2018-07-09 18:43:13 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"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"
|
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"
|
|
|
|
"github.com/zeebo/errs"
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
"storj.io/storj/internal/testpeertls"
|
|
|
|
"storj.io/storj/pkg/peertls"
|
2019-02-07 09:04:29 +00:00
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
2018-07-09 18:43:13 +01:00
|
|
|
)
|
|
|
|
|
2018-08-23 15:08:26 +01:00
|
|
|
func TestNewCert_CA(t *testing.T) {
|
2019-02-07 09:04:29 +00:00
|
|
|
caKey, err := pkcrypto.GeneratePrivateKey()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
caTemplate, err := peertls.CATemplate()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
caCert, err := peertls.NewCert(caKey, nil, caTemplate, nil)
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-07 18:02:22 +00:00
|
|
|
assert.NotEmpty(t, caKey)
|
2018-12-07 13:44:25 +00:00
|
|
|
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) {
|
2019-02-07 09:04:29 +00:00
|
|
|
caKey, err := pkcrypto.GeneratePrivateKey()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
caTemplate, err := peertls.CATemplate()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
caCert, err := peertls.NewCert(caKey, nil, caTemplate, nil)
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-02-07 09:04:29 +00:00
|
|
|
leafKey, err := pkcrypto.GeneratePrivateKey()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
leafTemplate, err := peertls.LeafTemplate()
|
2018-07-09 18:43:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
leafCert, err := peertls.NewCert(leafKey, caKey, leafTemplate, caCert)
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-07 18:02:22 +00:00
|
|
|
assert.NotEmpty(t, caKey)
|
2018-12-07 13:44:25 +00:00
|
|
|
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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
_, chain, err := testpeertls.NewCertChain(2)
|
2018-12-13 20:01:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
keys, chain, err := testpeertls.NewCertChain(2)
|
2018-12-13 20:01:43 +00:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
leafKey, leafCert, caCert := keys[1], chain[0], chain[1]
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyPeerCertChains)([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-02-07 09:04:29 +00:00
|
|
|
wrongKey, err := pkcrypto.GeneratePrivateKey()
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
leafCert, err = peertls.NewCert(leafKey, wrongKey, leafCert, caCert)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyPeerCertChains)([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
|
|
|
assert.True(t, peertls.ErrVerifyPeerCert.Has(err))
|
|
|
|
assert.True(t, peertls.ErrVerifyCertificateChain.Has(err))
|
2018-10-26 14:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyCAWhitelist(t *testing.T) {
|
2019-01-02 17:39:17 +00:00
|
|
|
_, chain2, err := testpeertls.NewCertChain(2)
|
2018-12-13 20:01:43 +00:00
|
|
|
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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyCAWhitelist(nil))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("whitelist contains ca", func(t *testing.T) {
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyCAWhitelist([]*x509.Certificate{caCert}))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
_, unrelatedChain, err := testpeertls.NewCertChain(1)
|
2018-12-13 20:01:43 +00:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
unrelatedCert := unrelatedChain[0]
|
|
|
|
|
|
|
|
t.Run("no valid signed extension, non-empty whitelist", func(t *testing.T) {
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyCAWhitelist([]*x509.Certificate{unrelatedCert}))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
|
|
|
assert.True(t, peertls.ErrVerifyCAWhitelist.Has(err))
|
2018-12-13 20:01:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("last cert in whitelist is signer", func(t *testing.T) {
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyCAWhitelist([]*x509.Certificate{unrelatedCert, caCert}))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("first cert in whitelist is signer", func(t *testing.T) {
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyCAWhitelist([]*x509.Certificate{caCert, unrelatedCert}))([][]byte{leafCert.Raw, caCert.Raw}, nil)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
_, chain3, err := testpeertls.NewCertChain(3)
|
2018-12-13 20:01:43 +00:00
|
|
|
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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyCAWhitelist([]*x509.Certificate{rootCert, unrelatedCert}))([][]byte{leaf2Cert.Raw, ca2Cert.Raw, unrelatedCert.Raw}, nil)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("length 3 chain - last cert in whitelist is signer", func(t *testing.T) {
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyCAWhitelist([]*x509.Certificate{unrelatedCert, rootCert}))([][]byte{leaf2Cert.Raw, ca2Cert.Raw, unrelatedCert.Raw}, nil)
|
2018-12-13 20:01:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddExtension(t *testing.T) {
|
2019-01-02 17:39:17 +00:00
|
|
|
_, chain, err := testpeertls.NewCertChain(1)
|
2018-12-13 20:01:43 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
keys, chain, err := testpeertls.NewCertChain(1)
|
2018-12-13 20:01:43 +00:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.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)
|
2019-01-02 17:39:17 +00:00
|
|
|
assert.Equal(t, peertls.ExtensionIDs[peertls.SignedCertExtID], chain[0].ExtraExtensions[0].Id)
|
2018-12-13 20:01:43 +00:00
|
|
|
|
|
|
|
ecKey, ok := keys[0].(*ecdsa.PrivateKey)
|
|
|
|
if !assert.True(t, ok) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-02-07 09:04:29 +00:00
|
|
|
err = pkcrypto.VerifySignature(
|
2018-12-13 20:01:43 +00:00
|
|
|
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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
keys, chain, err := testpeertls.NewCertChain(2)
|
2018-12-13 20:01:43 +00:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
caKey, leafCert := keys[0], chain[0]
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
err = peertls.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))
|
2019-01-02 17:39:17 +00:00
|
|
|
assert.True(t, peertls.ExtensionIDs[peertls.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()
|
|
|
|
}
|
|
|
|
|
2019-02-07 09:04:29 +00:00
|
|
|
err = pkcrypto.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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
keys, chain, err := testpeertls.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
|
|
|
|
2019-02-07 17:08:52 +00:00
|
|
|
leafHash := pkcrypto.SHA256Hash(leafCert.Raw)
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
rev := peertls.Revocation{
|
2018-12-13 20:01:43 +00:00
|
|
|
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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
keys, chain, err := testpeertls.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
|
|
|
|
2019-02-07 17:08:52 +00:00
|
|
|
leafHash := pkcrypto.SHA256Hash(leafCert.Raw)
|
2018-10-26 14:52:37 +01:00
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
rev := peertls.Revocation{
|
2018-12-13 20:01:43 +00:00
|
|
|
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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
keys, chain, err := testpeertls.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
|
|
|
|
2019-02-07 17:08:52 +00:00
|
|
|
leafHash := pkcrypto.SHA256Hash(leafCert.Raw)
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
rev := peertls.Revocation{
|
2018-12-13 20:01:43 +00:00
|
|
|
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
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
decodedRev := new(peertls.Revocation)
|
2018-12-13 20:01:43 +00:00
|
|
|
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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
keys, chain, err := testpeertls.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
|
|
|
|
2019-02-07 17:08:52 +00:00
|
|
|
leafHash := pkcrypto.SHA256Hash(leafCert.Raw)
|
2018-11-01 15:48:43 +00:00
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
rev := peertls.Revocation{
|
2018-12-13 20:01:43 +00:00
|
|
|
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)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
unmarshaledRev := new(peertls.Revocation)
|
2018-12-13 20:01:43 +00:00
|
|
|
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) {
|
2019-01-02 17:39:17 +00:00
|
|
|
keys, chain, err := testpeertls.NewCertChain(2)
|
2018-10-26 14:52:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
ext, err := peertls.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
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
var rev peertls.Revocation
|
2018-12-13 20:01:43 +00:00
|
|
|
err = rev.Unmarshal(ext.Value)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = rev.Verify(chain[1])
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|