Merge pull request #82 from nspcc-dev/token-add-sign-key-change

Change AddSignKey of SessionToken
This commit is contained in:
Leonard Lyubich 2020-05-18 12:58:32 +03:00 committed by GitHub
commit d391421e5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 34 deletions

View file

@ -6,6 +6,7 @@ import (
"io" "io"
"github.com/nspcc-dev/neofs-api-go/refs" "github.com/nspcc-dev/neofs-api-go/refs"
crypto "github.com/nspcc-dev/neofs-crypto"
) )
type signAccumWithToken struct { type signAccumWithToken struct {
@ -125,10 +126,14 @@ func (x Token_Info_Verb) Bytes() []byte {
return data return data
} }
// AddSignKey calls a Signature field setter of token with passed signature. // AddSignKey calls a Signature field setter and an OwnerKey field setter with corresponding arguments.
func (s signedSessionToken) AddSignKey(sig []byte, _ *ecdsa.PublicKey) { func (s signedSessionToken) AddSignKey(sig []byte, key *ecdsa.PublicKey) {
if s.SessionToken != nil { if s.SessionToken != nil {
s.SessionToken.SetSignature(sig) s.SessionToken.SetSignature(sig)
s.SessionToken.SetOwnerKey(
crypto.MarshalPublicKey(key),
)
} }
} }
@ -174,11 +179,11 @@ func NewVerifiedSessionToken(token SessionToken) DataWithSignature {
} }
} }
func tokenInfoSize(v SessionTokenInfo) int { func tokenInfoSize(v SessionKeySource) int {
if v == nil { if v == nil {
return 0 return 0
} }
return fixedTokenDataSize + len(v.GetSessionKey()) + len(v.GetOwnerKey()) return fixedTokenDataSize + len(v.GetSessionKey())
} }
// Fills passed buffer with signing token information bytes. // Fills passed buffer with signing token information bytes.
@ -208,9 +213,7 @@ func copyTokenSignedData(buf []byte, token SessionTokenInfo) {
tokenEndianness.PutUint64(buf[off:], token.ExpirationEpoch()) tokenEndianness.PutUint64(buf[off:], token.ExpirationEpoch())
off += 8 off += 8
off += copy(buf[off:], token.GetSessionKey()) copy(buf[off:], token.GetSessionKey())
copy(buf[off:], token.GetOwnerKey())
} }
// SignedData concatenates signed data with session token information. Returns concatenation result. // SignedData concatenates signed data with session token information. Returns concatenation result.

View file

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neofs-api-go/refs" "github.com/nspcc-dev/neofs-api-go/refs"
crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-crypto/test" "github.com/nspcc-dev/neofs-crypto/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -77,16 +78,6 @@ func TestTokenGettersSetters(t *testing.T) {
require.Equal(t, key, tok.GetSessionKey()) require.Equal(t, key, tok.GetSessionKey())
} }
{
key := make([]byte, 10)
_, err := rand.Read(key)
require.NoError(t, err)
tok.SetOwnerKey(key)
require.Equal(t, key, tok.GetOwnerKey())
}
{ // Signature { // Signature
sig := make([]byte, 10) sig := make([]byte, 10)
_, err := rand.Read(sig) _, err := rand.Read(sig)
@ -136,11 +127,6 @@ func TestSignToken(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
token.SetSessionKey(sessionKey) token.SetSessionKey(sessionKey)
ownerKey := make([]byte, 10)
_, err = rand.Read(ownerKey[:])
require.NoError(t, err)
token.SetOwnerKey(ownerKey)
signedToken := NewSignedSessionToken(token) signedToken := NewSignedSessionToken(token)
verifiedToken := NewVerifiedSessionToken(token) verifiedToken := NewVerifiedSessionToken(token)
@ -226,18 +212,6 @@ func TestSignToken(t *testing.T) {
token.SetSessionKey(sessionKey) token.SetSessionKey(sessionKey)
}, },
}, },
{ // Owner key
corrupt: func() {
ownerKey := token.GetOwnerKey()
ownerKey[0]++
token.SetOwnerKey(ownerKey)
},
restore: func() {
ownerKey := token.GetOwnerKey()
ownerKey[0]--
token.SetOwnerKey(ownerKey)
},
},
} }
for _, v := range items { for _, v := range items {
@ -247,3 +221,28 @@ func TestSignToken(t *testing.T) {
require.NoError(t, VerifySignatureWithKey(pk, verifiedToken)) require.NoError(t, VerifySignatureWithKey(pk, verifiedToken))
} }
} }
func TestSignedSessionToken_AddSignKey(t *testing.T) {
// nil SessionToken
s := new(signedSessionToken)
require.NotPanics(t, func() {
s.AddSignKey(nil, nil)
})
// create test public key and signature
pk := &test.DecodeKey(0).PublicKey
sig := []byte{1, 2, 3}
s.SessionToken = new(Token)
// add key-signature pair to SessionToken
s.AddSignKey(sig, pk)
require.Equal(t, sig, s.GetSignature())
require.Equal(t,
crypto.MarshalPublicKey(pk),
s.GetOwnerKey(),
)
}