2020-05-04 15:52:56 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2020-05-04 16:33:18 +00:00
|
|
|
"crypto/ecdsa"
|
2020-05-04 15:52:56 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
"github.com/nspcc-dev/neofs-crypto/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testSignedDataSrc struct {
|
2020-05-06 08:44:55 +00:00
|
|
|
err error
|
|
|
|
data []byte
|
|
|
|
sig []byte
|
|
|
|
key *ecdsa.PublicKey
|
|
|
|
token SessionToken
|
2020-06-18 12:24:17 +00:00
|
|
|
|
|
|
|
bearer BearerToken
|
2020-05-04 15:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testSignedDataReader struct {
|
2020-05-06 08:44:55 +00:00
|
|
|
*testSignedDataSrc
|
2020-05-05 10:16:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 08:44:55 +00:00
|
|
|
func (s testSignedDataSrc) GetSignature() []byte {
|
2020-05-05 10:16:21 +00:00
|
|
|
return s.sig
|
|
|
|
}
|
|
|
|
|
2020-05-06 08:44:55 +00:00
|
|
|
func (s testSignedDataSrc) GetSignKeyPairs() []SignKeyPair {
|
2020-05-05 10:16:21 +00:00
|
|
|
return []SignKeyPair{
|
|
|
|
newSignatureKeyPair(s.key, s.sig),
|
|
|
|
}
|
2020-05-04 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 08:44:55 +00:00
|
|
|
func (s testSignedDataSrc) SignedData() ([]byte, error) {
|
|
|
|
return s.data, s.err
|
2020-05-04 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 08:44:55 +00:00
|
|
|
func (s *testSignedDataSrc) AddSignKey(sig []byte, key *ecdsa.PublicKey) {
|
2020-05-05 10:16:21 +00:00
|
|
|
s.key = key
|
|
|
|
s.sig = sig
|
2020-05-04 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 15:52:56 +00:00
|
|
|
func testData(t *testing.T, sz int) []byte {
|
|
|
|
d := make([]byte, sz)
|
|
|
|
_, err := rand.Read(d)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2020-05-06 08:44:55 +00:00
|
|
|
func (s testSignedDataSrc) GetSessionToken() SessionToken {
|
|
|
|
return s.token
|
|
|
|
}
|
|
|
|
|
2020-06-18 12:24:17 +00:00
|
|
|
func (s testSignedDataSrc) GetBearerToken() BearerToken {
|
|
|
|
return s.bearer
|
|
|
|
}
|
|
|
|
|
2020-05-04 15:52:56 +00:00
|
|
|
func (s testSignedDataReader) SignedDataSize() int {
|
2020-05-06 08:44:55 +00:00
|
|
|
return len(s.data)
|
2020-05-04 15:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s testSignedDataReader) ReadSignedData(buf []byte) (int, error) {
|
2020-05-06 08:44:55 +00:00
|
|
|
if s.err != nil {
|
|
|
|
return 0, s.err
|
2020-05-04 15:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2020-05-06 08:44:55 +00:00
|
|
|
if len(buf) < len(s.data) {
|
2020-05-04 15:52:56 +00:00
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2020-05-06 08:44:55 +00:00
|
|
|
return copy(buf, s.data), err
|
2020-05-04 15:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDataSignature(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// nil private key
|
2020-05-05 10:16:21 +00:00
|
|
|
_, err = DataSignature(nil, nil)
|
2020-05-04 15:52:56 +00:00
|
|
|
require.EqualError(t, err, crypto.ErrEmptyPrivateKey.Error())
|
|
|
|
|
|
|
|
// create test private key
|
|
|
|
sk := test.DecodeKey(0)
|
|
|
|
|
2020-05-05 10:16:21 +00:00
|
|
|
// nil private key
|
|
|
|
_, err = DataSignature(sk, nil)
|
|
|
|
require.EqualError(t, err, ErrNilSignedDataSource.Error())
|
|
|
|
|
2020-05-04 15:52:56 +00:00
|
|
|
t.Run("common signed data source", func(t *testing.T) {
|
|
|
|
// create test data source
|
|
|
|
src := &testSignedDataSrc{
|
2020-05-06 08:44:55 +00:00
|
|
|
data: testData(t, 10),
|
2020-05-04 15:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create custom error for data source
|
2020-05-06 08:44:55 +00:00
|
|
|
src.err = errors.New("test error for data source")
|
2020-05-04 15:52:56 +00:00
|
|
|
|
2020-05-05 10:16:21 +00:00
|
|
|
_, err = DataSignature(sk, src)
|
2020-05-06 08:44:55 +00:00
|
|
|
require.EqualError(t, err, src.err.Error())
|
2020-05-04 15:52:56 +00:00
|
|
|
|
|
|
|
// reset error to nil
|
2020-05-06 08:44:55 +00:00
|
|
|
src.err = nil
|
2020-05-04 15:52:56 +00:00
|
|
|
|
|
|
|
// calculate data signature
|
2020-05-05 10:16:21 +00:00
|
|
|
sig, err := DataSignature(sk, src)
|
2020-05-04 15:52:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ascertain that the signature passes verification
|
2020-05-06 08:44:55 +00:00
|
|
|
require.NoError(t, crypto.Verify(&sk.PublicKey, src.data, sig))
|
2020-05-04 15:52:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("signed data reader", func(t *testing.T) {
|
|
|
|
// create test signed data reader
|
2020-05-06 08:44:55 +00:00
|
|
|
src := &testSignedDataSrc{
|
|
|
|
data: testData(t, 10),
|
2020-05-04 15:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create custom error for signed data reader
|
2020-05-06 08:44:55 +00:00
|
|
|
src.err = errors.New("test error for signed data reader")
|
2020-05-04 15:52:56 +00:00
|
|
|
|
2020-05-05 10:16:21 +00:00
|
|
|
sig, err := DataSignature(sk, src)
|
2020-05-06 08:44:55 +00:00
|
|
|
require.EqualError(t, err, src.err.Error())
|
2020-05-04 15:52:56 +00:00
|
|
|
|
|
|
|
// reset error to nil
|
2020-05-06 08:44:55 +00:00
|
|
|
src.err = nil
|
2020-05-04 15:52:56 +00:00
|
|
|
|
|
|
|
// calculate data signature
|
2020-05-05 10:16:21 +00:00
|
|
|
sig, err = DataSignature(sk, src)
|
2020-05-04 15:52:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ascertain that the signature passes verification
|
2020-05-06 08:44:55 +00:00
|
|
|
require.NoError(t, crypto.Verify(&sk.PublicKey, src.data, sig))
|
2020-05-04 15:52:56 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-04 16:33:18 +00:00
|
|
|
|
|
|
|
func TestAddSignatureWithKey(t *testing.T) {
|
2020-05-06 08:44:55 +00:00
|
|
|
require.NoError(t,
|
|
|
|
AddSignatureWithKey(
|
|
|
|
test.DecodeKey(0),
|
|
|
|
&testSignedDataSrc{
|
|
|
|
data: testData(t, 10),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
2020-05-04 16:33:18 +00:00
|
|
|
}
|
2020-05-05 10:16:21 +00:00
|
|
|
|
|
|
|
func TestVerifySignatures(t *testing.T) {
|
|
|
|
// empty signatures
|
|
|
|
require.NoError(t, VerifySignatures(nil))
|
|
|
|
|
|
|
|
// create test signature source
|
|
|
|
src := &testSignedDataSrc{
|
2020-05-06 08:44:55 +00:00
|
|
|
data: testData(t, 10),
|
2020-05-05 10:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create private key for test
|
|
|
|
sk := test.DecodeKey(0)
|
|
|
|
|
|
|
|
// calculate a signature of the data
|
2020-05-06 08:44:55 +00:00
|
|
|
sig, err := crypto.Sign(sk, src.data)
|
2020-05-05 10:16:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ascertain that verification is passed
|
|
|
|
require.NoError(t,
|
|
|
|
VerifySignatures(
|
|
|
|
src,
|
|
|
|
newSignatureKeyPair(&sk.PublicKey, sig),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// break the signature
|
|
|
|
sig[0]++
|
|
|
|
|
|
|
|
require.Error(t,
|
|
|
|
VerifySignatures(
|
|
|
|
src,
|
|
|
|
newSignatureKeyPair(&sk.PublicKey, sig),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// restore the signature
|
|
|
|
sig[0]--
|
|
|
|
|
|
|
|
// empty data source
|
|
|
|
require.EqualError(t,
|
|
|
|
VerifySignatures(nil, nil),
|
|
|
|
ErrNilSignedDataSource.Error(),
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyAccumulatedSignatures(t *testing.T) {
|
|
|
|
// nil signature source
|
|
|
|
require.EqualError(t,
|
|
|
|
VerifyAccumulatedSignatures(nil),
|
|
|
|
ErrNilSignatureKeySource.Error(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// create test private key
|
|
|
|
sk := test.DecodeKey(0)
|
|
|
|
|
|
|
|
// create signature source
|
2020-05-06 08:44:55 +00:00
|
|
|
src := &testSignedDataSrc{
|
2020-05-05 10:16:21 +00:00
|
|
|
data: testData(t, 10),
|
|
|
|
key: &sk.PublicKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// calculate a signature
|
|
|
|
src.sig, err = crypto.Sign(sk, src.data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ascertain that verification is passed
|
|
|
|
require.NoError(t, VerifyAccumulatedSignatures(src))
|
|
|
|
|
|
|
|
// break the signature
|
|
|
|
src.sig[0]++
|
|
|
|
|
|
|
|
// ascertain that verification is failed
|
|
|
|
require.Error(t, VerifyAccumulatedSignatures(src))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifySignatureWithKey(t *testing.T) {
|
|
|
|
// nil signature source
|
|
|
|
require.EqualError(t,
|
|
|
|
VerifySignatureWithKey(nil, nil),
|
|
|
|
ErrEmptyDataWithSignature.Error(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// create test signature source
|
2020-05-06 08:44:55 +00:00
|
|
|
src := &testSignedDataSrc{
|
2020-05-05 10:16:21 +00:00
|
|
|
data: testData(t, 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
// nil public key
|
|
|
|
require.EqualError(t,
|
2020-05-06 08:44:55 +00:00
|
|
|
VerifySignatureWithKey(nil, src),
|
2020-05-05 10:16:21 +00:00
|
|
|
crypto.ErrEmptyPublicKey.Error(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// create test private key
|
|
|
|
sk := test.DecodeKey(0)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// calculate a signature
|
|
|
|
src.sig, err = crypto.Sign(sk, src.data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ascertain that verification is passed
|
2020-05-06 08:44:55 +00:00
|
|
|
require.NoError(t, VerifySignatureWithKey(&sk.PublicKey, src))
|
2020-05-05 10:16:21 +00:00
|
|
|
|
|
|
|
// break the signature
|
|
|
|
src.sig[0]++
|
|
|
|
|
|
|
|
// ascertain that verification is failed
|
2020-05-06 08:44:55 +00:00
|
|
|
require.Error(t, VerifySignatureWithKey(&sk.PublicKey, src))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSignVerifyDataWithSessionToken(t *testing.T) {
|
2020-06-10 17:22:34 +00:00
|
|
|
// sign with empty RequestSignedData
|
2020-05-06 08:44:55 +00:00
|
|
|
require.EqualError(t,
|
2020-06-10 17:22:34 +00:00
|
|
|
SignRequestData(nil, nil),
|
|
|
|
ErrNilRequestSignedData.Error(),
|
2020-05-06 08:44:55 +00:00
|
|
|
)
|
|
|
|
|
2020-06-10 17:22:34 +00:00
|
|
|
// verify with empty RequestVerifyData
|
2020-05-06 08:44:55 +00:00
|
|
|
require.EqualError(t,
|
2020-06-10 17:22:34 +00:00
|
|
|
VerifyRequestData(nil),
|
|
|
|
ErrNilRequestVerifyData.Error(),
|
2020-05-06 08:44:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// create test session token
|
|
|
|
var (
|
|
|
|
token = new(Token)
|
|
|
|
initVerb = Token_Info_Verb(1)
|
2020-06-18 12:26:56 +00:00
|
|
|
|
|
|
|
bearer = wrapBearerTokenMsg(new(BearerTokenMsg))
|
|
|
|
bearerEpoch = uint64(8)
|
2020-05-06 08:44:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
token.SetVerb(initVerb)
|
|
|
|
|
2020-06-18 12:26:56 +00:00
|
|
|
bearer.SetExpirationEpoch(bearerEpoch)
|
|
|
|
|
2020-05-06 08:44:55 +00:00
|
|
|
// create test data with token
|
|
|
|
src := &testSignedDataSrc{
|
|
|
|
data: testData(t, 10),
|
|
|
|
token: token,
|
2020-06-18 12:26:56 +00:00
|
|
|
|
|
|
|
bearer: bearer,
|
2020-05-06 08:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create test private key
|
|
|
|
sk := test.DecodeKey(0)
|
|
|
|
|
|
|
|
// sign with private key
|
2020-06-10 17:22:34 +00:00
|
|
|
require.NoError(t, SignRequestData(sk, src))
|
2020-05-06 08:44:55 +00:00
|
|
|
|
|
|
|
// ascertain that verification is passed
|
2020-06-10 17:22:34 +00:00
|
|
|
require.NoError(t, VerifyRequestData(src))
|
2020-05-06 08:44:55 +00:00
|
|
|
|
|
|
|
// break the data
|
|
|
|
src.data[0]++
|
|
|
|
|
|
|
|
// ascertain that verification is failed
|
2020-06-10 17:22:34 +00:00
|
|
|
require.Error(t, VerifyRequestData(src))
|
2020-05-06 08:44:55 +00:00
|
|
|
|
|
|
|
// restore the data
|
|
|
|
src.data[0]--
|
|
|
|
|
|
|
|
// break the token
|
|
|
|
token.SetVerb(initVerb + 1)
|
|
|
|
|
|
|
|
// ascertain that verification is failed
|
2020-06-10 17:22:34 +00:00
|
|
|
require.Error(t, VerifyRequestData(src))
|
2020-05-06 08:44:55 +00:00
|
|
|
|
|
|
|
// restore the token
|
|
|
|
token.SetVerb(initVerb)
|
|
|
|
|
|
|
|
// ascertain that verification is passed
|
2020-06-10 17:22:34 +00:00
|
|
|
require.NoError(t, VerifyRequestData(src))
|
2020-05-06 08:44:55 +00:00
|
|
|
|
2020-06-18 12:26:56 +00:00
|
|
|
// break the Bearer token
|
|
|
|
bearer.SetExpirationEpoch(bearerEpoch + 1)
|
|
|
|
|
|
|
|
// ascertain that verification is failed
|
|
|
|
require.Error(t, VerifyRequestData(src))
|
|
|
|
|
|
|
|
// restore the Bearer token
|
|
|
|
bearer.SetExpirationEpoch(bearerEpoch)
|
|
|
|
|
|
|
|
// ascertain that verification is passed
|
|
|
|
require.NoError(t, VerifyRequestData(src))
|
|
|
|
|
2020-05-06 08:44:55 +00:00
|
|
|
// wrap to data reader
|
|
|
|
rdr := &testSignedDataReader{
|
|
|
|
testSignedDataSrc: src,
|
|
|
|
}
|
|
|
|
|
|
|
|
// sign with private key
|
2020-06-10 17:22:34 +00:00
|
|
|
require.NoError(t, SignRequestData(sk, rdr))
|
2020-05-06 08:44:55 +00:00
|
|
|
|
|
|
|
// ascertain that verification is passed
|
2020-06-10 17:22:34 +00:00
|
|
|
require.NoError(t, VerifyRequestData(rdr))
|
2020-05-05 10:16:21 +00:00
|
|
|
}
|