2021-05-25 19:59:21 +00:00
|
|
|
package accessbox
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-16 14:07:31 +00:00
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2021-06-14 13:39:25 +00:00
|
|
|
"crypto/rand"
|
2021-06-16 14:07:31 +00:00
|
|
|
"crypto/sha256"
|
2021-06-14 13:39:25 +00:00
|
|
|
"fmt"
|
2021-06-16 14:07:31 +00:00
|
|
|
"io"
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2021-06-16 14:07:31 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2021-06-14 13:39:25 +00:00
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
2021-06-16 14:07:31 +00:00
|
|
|
"golang.org/x/crypto/hkdf"
|
2021-06-14 13:39:25 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tokenInterface interface {
|
|
|
|
Marshal(bs ...[]byte) ([]byte, error)
|
|
|
|
Unmarshal([]byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal returns the wire-format of AccessBox.
|
|
|
|
func (x *AccessBox) Marshal() ([]byte, error) {
|
|
|
|
return proto.Marshal(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal parses the wire-format message and put data to x.
|
|
|
|
func (x *AccessBox) Unmarshal(data []byte) error {
|
|
|
|
return proto.Unmarshal(data, x)
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
// PackTokens adds a bearer and session tokens to BearerTokens and SessionToken lists respectively.
|
|
|
|
// Session token can be nil.
|
|
|
|
func PackTokens(bearer *token.BearerToken, sess *session.Token, keys ...*ecdsa.PublicKey) (*AccessBox, *ecdsa.PrivateKey, error) {
|
|
|
|
box := &AccessBox{}
|
|
|
|
ephemeralKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-06-16 14:07:31 +00:00
|
|
|
box.OwnerPublicKey = crypto.MarshalPublicKey(&ephemeralKey.PublicKey)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
if err := box.addToken(bearer, &box.BearerTokens, ephemeralKey, keys...); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to add bearer token to accessbox: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-06-16 14:07:31 +00:00
|
|
|
if sess != nil {
|
|
|
|
if err := box.addToken(sess, &box.SessionTokens, ephemeralKey, keys...); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to add session token to accessbox: %w", err)
|
|
|
|
}
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
2021-06-14 13:39:25 +00:00
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
return box, ephemeralKey, err
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
// GetBearerToken returns bearer token from AccessBox.
|
2021-06-16 14:07:31 +00:00
|
|
|
func (x *AccessBox) GetBearerToken(owner *ecdsa.PrivateKey) (*token.BearerToken, error) {
|
|
|
|
sender := crypto.UnmarshalPublicKey(x.OwnerPublicKey)
|
|
|
|
ownerKey := crypto.MarshalPublicKey(&owner.PublicKey)
|
2021-06-14 13:39:25 +00:00
|
|
|
for _, data := range x.BearerTokens {
|
2021-06-16 14:07:31 +00:00
|
|
|
if !bytes.Equal(data.GatePublicKey, ownerKey) {
|
2021-06-14 13:39:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
tkn := token.NewBearerToken()
|
|
|
|
if err := decodeToken(data.Token, tkn, owner, sender); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode bearer token: %w", err)
|
|
|
|
}
|
|
|
|
return tkn, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
return nil, fmt.Errorf("no bearer token for key %x was found", ownerKey)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
// GetSessionToken returns session token from AccessBox.
|
2021-06-16 14:07:31 +00:00
|
|
|
func (x *AccessBox) GetSessionToken(owner *ecdsa.PrivateKey) (*session.Token, error) {
|
|
|
|
sender := crypto.UnmarshalPublicKey(x.OwnerPublicKey)
|
|
|
|
ownerKey := crypto.MarshalPublicKey(&owner.PublicKey)
|
2021-06-14 13:39:25 +00:00
|
|
|
for _, data := range x.SessionTokens {
|
2021-06-16 14:07:31 +00:00
|
|
|
if !bytes.Equal(data.GatePublicKey, ownerKey) {
|
2021-06-14 13:39:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
tkn := session.NewToken()
|
|
|
|
|
|
|
|
if err := decodeToken(data.Token, tkn, owner, sender); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode session token: %w", err)
|
|
|
|
}
|
|
|
|
return tkn, nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
return nil, fmt.Errorf("no session token for key %x was found", ownerKey)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
func (x *AccessBox) addToken(tkn tokenInterface, list *[]*AccessBox_Token, owner *ecdsa.PrivateKey, keys ...*ecdsa.PublicKey) error {
|
2021-06-14 13:39:25 +00:00
|
|
|
for i, sender := range keys {
|
|
|
|
data, err := encodeToken(tkn, owner, sender)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w, sender = %d", err, i)
|
|
|
|
}
|
2021-06-16 14:07:31 +00:00
|
|
|
*list = append(*list, newToken(data, crypto.MarshalPublicKey(sender)))
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newToken(data []byte, key []byte) *AccessBox_Token {
|
|
|
|
res := new(AccessBox_Token)
|
|
|
|
res.Token = data
|
|
|
|
res.GatePublicKey = key
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
func encodeToken(tkn tokenInterface, owner *ecdsa.PrivateKey, sender *ecdsa.PublicKey) ([]byte, error) {
|
2021-06-14 13:39:25 +00:00
|
|
|
data, err := tkn.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
encrypted, err := encrypt(owner, sender, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return encrypted, nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
func decodeToken(data []byte, tkn tokenInterface, owner *ecdsa.PrivateKey, sender *ecdsa.PublicKey) error {
|
2021-06-14 13:39:25 +00:00
|
|
|
decoded, err := decrypt(owner, sender, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tkn.Unmarshal(decoded)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
func generateShared256(prv *ecdsa.PrivateKey, pub *ecdsa.PublicKey) (sk []byte, err error) {
|
|
|
|
if prv.PublicKey.Curve != pub.Curve {
|
|
|
|
return nil, fmt.Errorf("not equal curves")
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
x, _ := pub.Curve.ScalarMult(pub.X, pub.Y, prv.D.Bytes())
|
|
|
|
if x == nil {
|
|
|
|
return nil, fmt.Errorf("shared key is point at infinity")
|
|
|
|
}
|
|
|
|
|
|
|
|
sk = make([]byte, 32)
|
|
|
|
skBytes := x.Bytes()
|
|
|
|
copy(sk[len(sk)-len(skBytes):], skBytes)
|
|
|
|
return sk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deriveKey(secret []byte) ([]byte, error) {
|
|
|
|
hash := sha256.New
|
|
|
|
kdf := hkdf.New(hash, secret, nil, nil)
|
|
|
|
key := make([]byte, 32)
|
|
|
|
_, err := io.ReadFull(kdf, key)
|
|
|
|
return key, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func encrypt(owner *ecdsa.PrivateKey, sender *ecdsa.PublicKey, data []byte) ([]byte, error) {
|
|
|
|
enc, err := getCipher(owner, sender)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce := make([]byte, enc.NonceSize(), enc.NonceSize()+len(data)+enc.Overhead())
|
|
|
|
if _, err := rand.Read(nonce); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc.Seal(nonce, nonce, data, nil), nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
func decrypt(owner *ecdsa.PrivateKey, sender *ecdsa.PublicKey, data []byte) ([]byte, error) {
|
|
|
|
dec, err := getCipher(owner, sender)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ld, ns := len(data), dec.NonceSize(); ld < ns {
|
|
|
|
return nil, fmt.Errorf("wrong data size (%d), should be greater than %d", ld, ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce, cypher := data[:dec.NonceSize()], data[dec.NonceSize():]
|
|
|
|
return dec.Open(nil, nonce, cypher, nil)
|
|
|
|
}
|
2021-06-16 14:07:31 +00:00
|
|
|
|
|
|
|
func getCipher(owner *ecdsa.PrivateKey, sender *ecdsa.PublicKey) (cipher.AEAD, error) {
|
|
|
|
secret, err := generateShared256(owner, sender)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := deriveKey(secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
aead, err := chacha20poly1305.NewX(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return aead, nil
|
|
|
|
}
|