2019-03-25 21:52:12 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-12-18 15:08:54 +00:00
|
|
|
package revocation_test
|
2019-03-25 21:52:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/peertls"
|
|
|
|
"storj.io/common/peertls/extensions"
|
|
|
|
"storj.io/common/peertls/testpeertls"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
2023-04-06 14:54:41 +01:00
|
|
|
"storj.io/storj/private/kvstore"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testrevocation"
|
2019-03-25 21:52:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRevocationCheckHandler(t *testing.T) {
|
2019-12-18 15:08:54 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
testrevocation.RunDBs(t, func(t *testing.T, revDB extensions.RevocationDB, _ kvstore.Store) {
|
2019-04-08 19:15:19 +01:00
|
|
|
keys, chain, err := testpeertls.NewCertChain(2, storj.LatestIDVersion().Number)
|
2019-03-25 21:52:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-20 16:04:17 +01:00
|
|
|
opts := &extensions.Options{RevocationDB: revDB}
|
2019-03-25 21:52:12 +00:00
|
|
|
revocationChecker := extensions.RevocationCheckHandler.NewHandlerFunc(opts)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
revokingChain, leafRevocationExt, err := testpeertls.RevokeLeaf(keys[peertls.CAIndex], chain)
|
2019-03-25 21:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-03 16:03:53 +01:00
|
|
|
assert.Equal(t, chain[peertls.CAIndex].Raw, revokingChain[peertls.CAIndex].Raw)
|
|
|
|
|
|
|
|
{
|
|
|
|
t.Log("revoked leaf success (original chain)")
|
|
|
|
err := revocationChecker(pkix.Extension{}, identity.ToChains(chain))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-03-25 21:52:12 +00:00
|
|
|
|
|
|
|
{
|
2019-04-03 16:03:53 +01:00
|
|
|
t.Log("revoked leaf success (revoking chain)")
|
|
|
|
err := revocationChecker(pkix.Extension{}, identity.ToChains(revokingChain))
|
2019-03-25 21:52:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: add leaf revocation to revocation DB
|
2019-04-03 16:03:53 +01:00
|
|
|
t.Log("revocation DB put leaf revocation")
|
2019-06-04 12:36:27 +01:00
|
|
|
err = revDB.Put(ctx, revokingChain, leafRevocationExt)
|
2019-03-25 21:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
{
|
2019-04-03 16:03:53 +01:00
|
|
|
t.Log("revoked leaf success (revoking chain)")
|
|
|
|
err := revocationChecker(pkix.Extension{}, identity.ToChains(revokingChain))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
t.Log("revoked leaf error (original chain)")
|
2019-03-25 21:52:12 +00:00
|
|
|
err := revocationChecker(pkix.Extension{}, identity.ToChains(chain))
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2019-04-03 16:03:53 +01:00
|
|
|
})
|
2019-03-25 21:52:12 +00:00
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
testrevocation.RunDBs(t, func(t *testing.T, revDB extensions.RevocationDB, _ kvstore.Store) {
|
2019-04-08 19:15:19 +01:00
|
|
|
t.Log("new revocation DB")
|
|
|
|
keys, chain, err := testpeertls.NewCertChain(2, storj.LatestIDVersion().Number)
|
2019-04-03 16:03:53 +01:00
|
|
|
assert.NoError(t, err)
|
2019-03-25 21:52:12 +00:00
|
|
|
|
2019-08-20 16:04:17 +01:00
|
|
|
opts := &extensions.Options{RevocationDB: revDB}
|
2019-04-03 16:03:53 +01:00
|
|
|
revocationChecker := extensions.RevocationCheckHandler.NewHandlerFunc(opts)
|
2019-04-08 19:15:19 +01:00
|
|
|
revokingChain, caRevocationExt, err := testpeertls.RevokeCA(keys[peertls.CAIndex], chain)
|
2019-03-25 21:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-03 16:03:53 +01:00
|
|
|
assert.NotEqual(t, chain[peertls.CAIndex].Raw, revokingChain[peertls.CAIndex].Raw)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
chainID, err := identity.NodeIDFromCert(chain[peertls.CAIndex])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
revokingChainID, err := identity.NodeIDFromCert(revokingChain[peertls.CAIndex])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, chainID, revokingChainID)
|
|
|
|
|
2019-04-03 16:03:53 +01:00
|
|
|
{
|
|
|
|
t.Log("revoked CA error (original chain)")
|
|
|
|
err := revocationChecker(pkix.Extension{}, identity.ToChains(chain))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:52:12 +00:00
|
|
|
{
|
2019-04-03 16:03:53 +01:00
|
|
|
t.Log("revoked CA success (revokingChain)")
|
|
|
|
err := revocationChecker(pkix.Extension{}, identity.ToChains(revokingChain))
|
2019-03-25 21:52:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: add CA revocation to revocation DB
|
2019-04-03 16:03:53 +01:00
|
|
|
t.Log("revocation DB put CA revocation")
|
2019-06-04 12:36:27 +01:00
|
|
|
err = revDB.Put(ctx, revokingChain, caRevocationExt)
|
2019-03-25 21:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
{
|
2019-04-03 16:03:53 +01:00
|
|
|
t.Log("revoked CA error (revoking CA chain)")
|
|
|
|
err := revocationChecker(pkix.Extension{}, identity.ToChains(revokingChain))
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
t.Log("revoked CA error (original chain)")
|
|
|
|
err := revocationChecker(pkix.Extension{}, identity.ToChains(chain))
|
2019-03-25 21:52:12 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRevocationUpdateHandler(t *testing.T) {
|
2019-12-18 15:08:54 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
testrevocation.RunDBs(t, func(t *testing.T, revDB extensions.RevocationDB, _ kvstore.Store) {
|
2019-04-08 19:15:19 +01:00
|
|
|
keys, chain, err := testpeertls.NewCertChain(2, storj.LatestIDVersion().Number)
|
2019-03-25 21:52:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
olderRevokedChain, olderRevocation, err := testpeertls.RevokeLeaf(keys[peertls.CAIndex], chain)
|
2019-03-25 21:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
2019-04-08 19:15:19 +01:00
|
|
|
revokedLeafChain, newerRevocation, err := testpeertls.RevokeLeaf(keys[peertls.CAIndex], chain)
|
2019-03-25 21:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
2019-04-08 19:15:19 +01:00
|
|
|
newestRevokedChain, newestRevocation, err := testpeertls.RevokeLeaf(keys[peertls.CAIndex], revokedLeafChain)
|
2019-03-25 21:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-08-20 16:04:17 +01:00
|
|
|
opts := &extensions.Options{RevocationDB: revDB}
|
2019-03-25 21:52:12 +00:00
|
|
|
revocationChecker := extensions.RevocationUpdateHandler.NewHandlerFunc(opts)
|
|
|
|
|
|
|
|
{
|
|
|
|
t.Log("first revocation")
|
|
|
|
err := revocationChecker(newerRevocation, identity.ToChains(revokedLeafChain))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
t.Log("older revocation error")
|
|
|
|
err = revocationChecker(olderRevocation, identity.ToChains(olderRevokedChain))
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
t.Log("newer revocation")
|
|
|
|
err = revocationChecker(newestRevocation, identity.ToChains(newestRevokedChain))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-08-20 15:15:43 +01:00
|
|
|
|
|
|
|
func TestWithOptions_NilRevocationDB(t *testing.T) {
|
|
|
|
_, chain, err := testpeertls.NewCertChain(2, storj.LatestIDVersion().Number)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-08-20 16:04:17 +01:00
|
|
|
opts := &extensions.Options{RevocationDB: nil}
|
2019-08-20 15:15:43 +01:00
|
|
|
handlerFuncMap := extensions.DefaultHandlers.WithOptions(opts)
|
|
|
|
|
|
|
|
extMap := tlsopts.NewExtensionsMap(chain[peertls.LeafIndex])
|
|
|
|
err = extMap.HandleExtensions(handlerFuncMap, identity.ToChains(chain))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|