2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-13 09:39:45 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
package identity_test
|
2018-08-13 09:39:45 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-08-23 15:08:26 +01:00
|
|
|
"context"
|
2019-04-08 19:15:19 +01:00
|
|
|
"crypto"
|
2019-04-03 16:03:53 +01:00
|
|
|
"crypto/rand"
|
2019-04-08 19:15:19 +01:00
|
|
|
"crypto/x509"
|
2019-04-03 16:03:53 +01:00
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/asn1"
|
2019-04-08 19:15:19 +01:00
|
|
|
"fmt"
|
2018-08-13 09:39:45 +01:00
|
|
|
"os"
|
2018-08-23 08:05:56 +01:00
|
|
|
"runtime"
|
2018-08-13 09:39:45 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-04-03 16:03:53 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-12-17 15:09:52 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-04-03 16:03:53 +01:00
|
|
|
"storj.io/storj/internal/testidentity"
|
2019-04-08 19:15:19 +01:00
|
|
|
"storj.io/storj/internal/testpeertls"
|
2019-01-02 10:23:25 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-08-13 09:39:45 +01:00
|
|
|
"storj.io/storj/pkg/peertls"
|
2019-04-03 16:03:53 +01:00
|
|
|
"storj.io/storj/pkg/peertls/extensions"
|
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-02-07 09:04:29 +00:00
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
2019-04-08 19:15:19 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-08-13 09:39:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPeerIdentityFromCertChain(t *testing.T) {
|
2019-02-07 09:04:29 +00:00
|
|
|
caKey, err := pkcrypto.GeneratePrivateKey()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
caTemplate, err := peertls.CATemplate()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-04-04 00:21:32 +01:00
|
|
|
caCert, err := peertls.CreateSelfSignedCertificate(caKey, caTemplate)
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
leafTemplate, err := peertls.LeafTemplate()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-02-07 09:04:29 +00:00
|
|
|
leafKey, err := pkcrypto.GeneratePrivateKey()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-23 15:08:26 +01:00
|
|
|
|
2019-04-04 00:21:32 +01:00
|
|
|
leafCert, err := peertls.CreateCertificate(pkcrypto.PublicKeyFromPrivate(leafKey), caKey, leafTemplate, caTemplate)
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
peerIdent, err := identity.PeerIdentityFromChain([]*x509.Certificate{leafCert, caCert})
|
|
|
|
require.NoError(t, err)
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.Equal(t, caCert, peerIdent.CA)
|
|
|
|
assert.Equal(t, leafCert, peerIdent.Leaf)
|
|
|
|
assert.NotEmpty(t, peerIdent.ID)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFullIdentityFromPEM(t *testing.T) {
|
2019-02-07 09:04:29 +00:00
|
|
|
caKey, err := pkcrypto.GeneratePrivateKey()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
caTemplate, err := peertls.CATemplate()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-04-04 00:21:32 +01:00
|
|
|
caCert, err := peertls.CreateSelfSignedCertificate(caKey, caTemplate)
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, caCert)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-12-07 13:44:25 +00:00
|
|
|
leafTemplate, err := peertls.LeafTemplate()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-02-07 09:04:29 +00:00
|
|
|
leafKey, err := pkcrypto.GeneratePrivateKey()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-23 15:08:26 +01:00
|
|
|
|
2019-04-04 00:21:32 +01:00
|
|
|
leafCert, err := peertls.CreateCertificate(pkcrypto.PublicKeyFromPrivate(leafKey), caKey, leafTemplate, caTemplate)
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, leafCert)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
|
|
|
chainPEM := bytes.NewBuffer([]byte{})
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, pkcrypto.WriteCertPEM(chainPEM, leafCert))
|
|
|
|
require.NoError(t, pkcrypto.WriteCertPEM(chainPEM, caCert))
|
2018-08-13 09:39:45 +01:00
|
|
|
|
|
|
|
keyPEM := bytes.NewBuffer([]byte{})
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, pkcrypto.WritePrivateKeyPEM(keyPEM, leafKey))
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
fullIdent, err := identity.FullIdentityFromPEM(chainPEM.Bytes(), keyPEM.Bytes())
|
2018-08-13 09:39:45 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-07 13:44:25 +00:00
|
|
|
assert.Equal(t, leafCert.Raw, fullIdent.Leaf.Raw)
|
|
|
|
assert.Equal(t, caCert.Raw, fullIdent.CA.Raw)
|
|
|
|
assert.Equal(t, leafKey, fullIdent.Key)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
func TestConfig_Save_with_extension(t *testing.T) {
|
2018-12-17 15:09:52 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
testidentity.CompleteIdentityVersionsTest(t, func(t *testing.T, version storj.IDVersion, ident *identity.FullIdentity) {
|
|
|
|
identCfg := &identity.Config{
|
|
|
|
CertPath: ctx.File("chain.pem"),
|
|
|
|
KeyPath: ctx.File("key.pem"),
|
|
|
|
}
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
{ // pre-save version assertions
|
|
|
|
assert.Equal(t, version.Number, ident.ID.Version().Number)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
caVersion, err := storj.IDVersionFromCert(ident.CA)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, version.Number, caVersion.Number)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
versionExt := tlsopts.NewExtensionsMap(ident.CA)[extensions.IdentityVersionExtID.String()]
|
|
|
|
if ident.ID.Version().Number == 1 {
|
|
|
|
require.NotEmpty(t, versionExt)
|
|
|
|
assert.Equal(t, ident.ID.Version().Number, storj.IDVersionNumber(versionExt.Value[0]))
|
|
|
|
} else {
|
|
|
|
assert.Empty(t, versionExt)
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
{ // test saving
|
|
|
|
err := identCfg.Save(ident)
|
|
|
|
assert.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
certInfo, err := os.Stat(identCfg.CertPath)
|
|
|
|
assert.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
keyInfo, err := os.Stat(identCfg.KeyPath)
|
|
|
|
assert.NoError(t, err)
|
2018-12-13 20:01:43 +00:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
// TODO (windows): ignoring for windows due to different default permissions
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
assert.Equal(t, os.FileMode(0644), certInfo.Mode())
|
|
|
|
assert.Equal(t, os.FileMode(0600), keyInfo.Mode())
|
|
|
|
}
|
2018-12-17 15:09:52 +00:00
|
|
|
}
|
2018-12-13 20:01:43 +00:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
{ // test loading
|
|
|
|
loadedFi, err := identCfg.Load()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, ident.Key, loadedFi.Key)
|
|
|
|
assert.Equal(t, ident.Leaf, loadedFi.Leaf)
|
|
|
|
assert.Equal(t, ident.CA, loadedFi.CA)
|
|
|
|
assert.Equal(t, ident.ID, loadedFi.ID)
|
|
|
|
|
|
|
|
versionExt := tlsopts.NewExtensionsMap(ident.CA)[extensions.IdentityVersionExtID.String()]
|
|
|
|
if ident.ID.Version().Number == 1 {
|
|
|
|
require.NotEmpty(t, versionExt)
|
|
|
|
assert.Equal(t, ident.ID.Version().Number, storj.IDVersionNumber(versionExt.Value[0]))
|
|
|
|
} else {
|
|
|
|
assert.Empty(t, versionExt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_Save(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
testidentity.IdentityVersionsTest(t, func(t *testing.T, version storj.IDVersion, ident *identity.FullIdentity) {
|
|
|
|
identCfg := &identity.Config{
|
|
|
|
CertPath: ctx.File("chain.pem"),
|
|
|
|
KeyPath: ctx.File("key.pem"),
|
|
|
|
}
|
|
|
|
|
|
|
|
chainPEM := bytes.NewBuffer([]byte{})
|
|
|
|
require.NoError(t, pkcrypto.WriteCertPEM(chainPEM, ident.Leaf))
|
|
|
|
require.NoError(t, pkcrypto.WriteCertPEM(chainPEM, ident.CA))
|
|
|
|
|
|
|
|
privateKey := ident.Key
|
|
|
|
require.NotEmpty(t, privateKey)
|
|
|
|
|
|
|
|
keyPEM := bytes.NewBuffer([]byte{})
|
|
|
|
require.NoError(t, pkcrypto.WritePrivateKeyPEM(keyPEM, privateKey))
|
|
|
|
|
|
|
|
{ // test saving
|
|
|
|
err := identCfg.Save(ident)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
certInfo, err := os.Stat(identCfg.CertPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
keyInfo, err := os.Stat(identCfg.KeyPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// TODO (windows): ignoring for windows due to different default permissions
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
assert.Equal(t, os.FileMode(0644), certInfo.Mode())
|
|
|
|
assert.Equal(t, os.FileMode(0600), keyInfo.Mode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // test loading
|
|
|
|
loadedFi, err := identCfg.Load()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ident.Key, loadedFi.Key)
|
|
|
|
assert.Equal(t, ident.Leaf, loadedFi.Leaf)
|
|
|
|
assert.Equal(t, ident.CA, loadedFi.CA)
|
|
|
|
assert.Equal(t, ident.ID, loadedFi.ID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionedNodeIDFromKey(t *testing.T) {
|
|
|
|
_, chain, err := testpeertls.NewCertChain(1, storj.LatestIDVersion().Number)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pubKey, ok := chain[peertls.LeafIndex].PublicKey.(crypto.PublicKey)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
for _, version := range storj.IDVersions {
|
|
|
|
t.Run(fmt.Sprintf("IdentityV%d", version.Number), func(t *testing.T) {
|
|
|
|
id, err := identity.NodeIDFromKey(pubKey, version)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, version.Number, id.Version().Number)
|
|
|
|
})
|
2018-12-17 15:09:52 +00:00
|
|
|
}
|
2018-12-13 20:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyPeer(t *testing.T) {
|
2019-01-02 10:23:25 +00:00
|
|
|
ca, err := identity.NewCA(context.Background(), identity.NewCAOptions{
|
2018-12-17 15:09:52 +00:00
|
|
|
Difficulty: 12,
|
|
|
|
Concurrency: 4,
|
|
|
|
})
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ca)
|
2018-12-13 20:01:43 +00:00
|
|
|
|
|
|
|
fi, err := ca.NewIdentity()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, fi)
|
2018-12-13 20:01:43 +00:00
|
|
|
|
|
|
|
err = peertls.VerifyPeerFunc(peertls.VerifyPeerCertChains)([][]byte{fi.Leaf.Raw, fi.CA.Raw}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:03:53 +01:00
|
|
|
func TestManageablePeerIdentity_AddExtension(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
manageablePeerIdentity, err := testidentity.NewTestManageablePeerIdentity(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
oldLeaf := manageablePeerIdentity.Leaf
|
|
|
|
assert.Len(t, manageablePeerIdentity.CA.Cert.ExtraExtensions, 0)
|
|
|
|
|
|
|
|
randBytes := make([]byte, 10)
|
|
|
|
_, err = rand.Read(randBytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
randExt := pkix.Extension{
|
|
|
|
Id: asn1.ObjectIdentifier{2, 999, int(randBytes[0])},
|
|
|
|
Value: randBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = manageablePeerIdentity.AddExtension(randExt)
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2019-04-03 16:03:53 +01:00
|
|
|
|
|
|
|
assert.Len(t, manageablePeerIdentity.Leaf.ExtraExtensions, 0)
|
|
|
|
assert.Len(t, manageablePeerIdentity.Leaf.Extensions, len(oldLeaf.Extensions)+1)
|
|
|
|
|
|
|
|
assert.Equal(t, oldLeaf.SerialNumber, manageablePeerIdentity.Leaf.SerialNumber)
|
|
|
|
assert.Equal(t, oldLeaf.IsCA, manageablePeerIdentity.Leaf.IsCA)
|
|
|
|
assert.Equal(t, oldLeaf.PublicKey, manageablePeerIdentity.Leaf.PublicKey)
|
2019-04-08 19:15:19 +01:00
|
|
|
ext := tlsopts.NewExtensionsMap(manageablePeerIdentity.Leaf)[randExt.Id.String()]
|
|
|
|
assert.Equal(t, randExt, ext)
|
2019-04-03 16:03:53 +01:00
|
|
|
|
|
|
|
assert.Equal(t, randExt, tlsopts.NewExtensionsMap(manageablePeerIdentity.Leaf)[randExt.Id.String()])
|
|
|
|
|
|
|
|
assert.NotEqual(t, oldLeaf.Raw, manageablePeerIdentity.Leaf.Raw)
|
|
|
|
assert.NotEqual(t, oldLeaf.RawTBSCertificate, manageablePeerIdentity.Leaf.RawTBSCertificate)
|
|
|
|
assert.NotEqual(t, oldLeaf.Signature, manageablePeerIdentity.Leaf.Signature)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManageableFullIdentity_Revoke(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
manageableFullIdentity, err := testidentity.NewTestManageableFullIdentity(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
oldLeaf := manageableFullIdentity.Leaf
|
|
|
|
assert.Len(t, manageableFullIdentity.CA.Cert.ExtraExtensions, 0)
|
|
|
|
|
|
|
|
err = manageableFullIdentity.Revoke()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2019-04-03 16:03:53 +01:00
|
|
|
|
|
|
|
assert.Len(t, manageableFullIdentity.Leaf.ExtraExtensions, 0)
|
|
|
|
assert.Len(t, manageableFullIdentity.Leaf.Extensions, len(oldLeaf.Extensions)+1)
|
|
|
|
|
|
|
|
assert.Equal(t, oldLeaf.IsCA, manageableFullIdentity.Leaf.IsCA)
|
|
|
|
|
|
|
|
assert.NotEqual(t, oldLeaf.PublicKey, manageableFullIdentity.Leaf.PublicKey)
|
|
|
|
assert.NotEqual(t, oldLeaf.SerialNumber, manageableFullIdentity.Leaf.SerialNumber)
|
|
|
|
assert.NotEqual(t, oldLeaf.Raw, manageableFullIdentity.Leaf.Raw)
|
|
|
|
assert.NotEqual(t, oldLeaf.RawTBSCertificate, manageableFullIdentity.Leaf.RawTBSCertificate)
|
|
|
|
assert.NotEqual(t, oldLeaf.Signature, manageableFullIdentity.Leaf.Signature)
|
|
|
|
|
|
|
|
revocationExt := tlsopts.NewExtensionsMap(manageableFullIdentity.Leaf)[extensions.RevocationExtID.String()]
|
|
|
|
assert.True(t, extensions.RevocationExtID.Equal(revocationExt.Id))
|
|
|
|
|
|
|
|
var rev extensions.Revocation
|
|
|
|
err = rev.Unmarshal(revocationExt.Value)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = rev.Verify(manageableFullIdentity.CA.Cert)
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|