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/elliptic"
|
2021-06-14 13:39:25 +00:00
|
|
|
"crypto/rand"
|
2021-06-16 14:07:31 +00:00
|
|
|
"crypto/sha256"
|
2021-06-17 16:45:50 +00:00
|
|
|
"encoding/hex"
|
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-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-04-25 09:57:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
2021-11-15 12:56:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
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"
|
|
|
|
)
|
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
// Box represents friendly AccessBox.
|
|
|
|
type Box struct {
|
|
|
|
Gate *GateData
|
|
|
|
Policies []*ContainerPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerPolicy represents friendly AccessBox_ContainerPolicy.
|
|
|
|
type ContainerPolicy struct {
|
|
|
|
LocationConstraint string
|
|
|
|
Policy *netmap.PlacementPolicy
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
// GateData represents gate tokens in AccessBox.
|
|
|
|
type GateData struct {
|
2022-01-26 09:09:28 +00:00
|
|
|
AccessKey string
|
2022-04-25 09:57:58 +00:00
|
|
|
BearerToken *bearer.Token
|
2022-05-04 12:29:11 +00:00
|
|
|
SessionTokens []*session.Container
|
2022-01-26 09:09:28 +00:00
|
|
|
GateKey *keys.PublicKey
|
2021-06-17 16:45:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// NewGateData returns GateData from the provided bearer token and the public gate key.
|
2022-04-25 09:57:58 +00:00
|
|
|
func NewGateData(gateKey *keys.PublicKey, bearerTkn *bearer.Token) *GateData {
|
2021-06-18 15:15:58 +00:00
|
|
|
return &GateData{GateKey: gateKey, BearerToken: bearerTkn}
|
2021-06-17 16:45:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// SessionTokenForPut returns the first suitable container session context for PUT operation.
|
2022-05-04 12:29:11 +00:00
|
|
|
func (g *GateData) SessionTokenForPut() *session.Container {
|
|
|
|
return g.containerSessionToken(session.VerbContainerPut)
|
2022-01-26 09:09:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// SessionTokenForDelete returns the first suitable container session context for DELETE operation.
|
2022-05-04 12:29:11 +00:00
|
|
|
func (g *GateData) SessionTokenForDelete() *session.Container {
|
|
|
|
return g.containerSessionToken(session.VerbContainerDelete)
|
2022-01-26 09:09:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// SessionTokenForSetEACL returns the first suitable container session context for SetEACL operation.
|
2022-05-04 12:29:11 +00:00
|
|
|
func (g *GateData) SessionTokenForSetEACL() *session.Container {
|
|
|
|
return g.containerSessionToken(session.VerbContainerSetEACL)
|
2022-01-26 09:09:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 12:29:11 +00:00
|
|
|
func (g *GateData) containerSessionToken(verb session.ContainerVerb) *session.Container {
|
2022-01-26 09:09:28 +00:00
|
|
|
for _, sessionToken := range g.SessionTokens {
|
2022-05-04 12:29:11 +00:00
|
|
|
if isAppropriateContainerContext(sessionToken, verb) {
|
|
|
|
return sessionToken
|
2022-01-26 09:09:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:29:11 +00:00
|
|
|
func isAppropriateContainerContext(tok *session.Container, verb session.ContainerVerb) bool {
|
|
|
|
switch verb {
|
|
|
|
case session.VerbContainerSetEACL, session.VerbContainerDelete, session.VerbContainerPut:
|
|
|
|
return tok.AssertVerb(verb)
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2022-01-26 09:09:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Secrets represents AccessKey and the key to encrypt gate tokens.
|
2021-06-17 16:45:50 +00:00
|
|
|
type Secrets struct {
|
|
|
|
AccessKey string
|
2021-06-24 15:21:34 +00:00
|
|
|
EphemeralKey *keys.PrivateKey
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// PackTokens adds bearer and session tokens to BearerTokens and SessionToken lists respectively.
|
2021-06-16 14:07:31 +00:00
|
|
|
// Session token can be nil.
|
2021-06-18 15:15:58 +00:00
|
|
|
func PackTokens(gatesData []*GateData) (*AccessBox, *Secrets, error) {
|
2021-06-16 14:07:31 +00:00
|
|
|
box := &AccessBox{}
|
2021-06-24 15:21:34 +00:00
|
|
|
ephemeralKey, err := keys.NewPrivateKey()
|
2021-06-16 14:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-06-24 15:21:34 +00:00
|
|
|
box.OwnerPublicKey = ephemeralKey.PublicKey().Bytes()
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
secret, err := generateSecret()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to generate accessKey as hex: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-06-17 16:45:50 +00:00
|
|
|
|
2021-06-18 15:15:58 +00:00
|
|
|
if err := box.addTokens(gatesData, ephemeralKey, secret); err != nil {
|
2021-06-17 16:45:50 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to add tokens to accessbox: %w", err)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
2021-06-14 13:39:25 +00:00
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
return box, &Secrets{hex.EncodeToString(secret), ephemeralKey}, err
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
// GetTokens returns gate tokens from AccessBox.
|
2021-06-24 15:21:34 +00:00
|
|
|
func (x *AccessBox) GetTokens(owner *keys.PrivateKey) (*GateData, error) {
|
|
|
|
sender, err := keys.NewPublicKeyFromBytes(x.OwnerPublicKey, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't unmarshal OwnerPublicKey: %w", err)
|
|
|
|
}
|
|
|
|
ownerKey := owner.PublicKey().Bytes()
|
2021-06-17 16:45:50 +00:00
|
|
|
for _, gate := range x.Gates {
|
|
|
|
if !bytes.Equal(gate.GatePublicKey, ownerKey) {
|
2021-06-14 13:39:25 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-06-17 16:45:50 +00:00
|
|
|
|
|
|
|
gateData, err := decodeGate(gate, owner, sender)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode gate: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-06-17 16:45:50 +00:00
|
|
|
return gateData, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
return nil, fmt.Errorf("no gate data for key %x was found", ownerKey)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
// GetPlacementPolicy returns ContainerPolicy from AccessBox.
|
|
|
|
func (x *AccessBox) GetPlacementPolicy() ([]*ContainerPolicy, error) {
|
|
|
|
var result []*ContainerPolicy
|
|
|
|
for _, policy := range x.ContainerPolicy {
|
|
|
|
placementPolicy := netmap.NewPlacementPolicy()
|
|
|
|
if err := placementPolicy.Unmarshal(policy.Policy); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, &ContainerPolicy{
|
|
|
|
LocationConstraint: policy.LocationConstraint,
|
|
|
|
Policy: placementPolicy,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// GetBox parses AccessBox to Box.
|
2021-07-16 12:35:07 +00:00
|
|
|
func (x *AccessBox) GetBox(owner *keys.PrivateKey) (*Box, error) {
|
|
|
|
tokens, err := x.GetTokens(owner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := x.GetPlacementPolicy()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Box{
|
|
|
|
Gate: tokens,
|
|
|
|
Policies: policy,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
func (x *AccessBox) addTokens(gatesData []*GateData, ephemeralKey *keys.PrivateKey, secret []byte) error {
|
2022-04-25 09:57:58 +00:00
|
|
|
for _, gate := range gatesData {
|
|
|
|
encBearer := gate.BearerToken.Marshal()
|
2022-01-26 09:09:28 +00:00
|
|
|
encSessions := make([][]byte, len(gate.SessionTokens))
|
|
|
|
for i, sessionToken := range gate.SessionTokens {
|
2022-05-04 12:29:11 +00:00
|
|
|
encSessions[i] = sessionToken.Marshal()
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
tokens := new(Tokens)
|
|
|
|
tokens.AccessKey = secret
|
|
|
|
tokens.BearerToken = encBearer
|
2022-01-26 09:09:28 +00:00
|
|
|
tokens.SessionTokens = encSessions
|
2021-06-14 13:39:25 +00:00
|
|
|
|
2021-06-18 15:15:58 +00:00
|
|
|
boxGate, err := encodeGate(ephemeralKey, gate.GateKey, tokens)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
2021-06-17 16:45:50 +00:00
|
|
|
return err
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-06-17 16:45:50 +00:00
|
|
|
x.Gates = append(x.Gates, boxGate)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
func encodeGate(ephemeralKey *keys.PrivateKey, ownerKey *keys.PublicKey, tokens *Tokens) (*AccessBox_Gate, error) {
|
2021-06-17 16:45:50 +00:00
|
|
|
data, err := proto.Marshal(tokens)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
encrypted, err := encrypt(ephemeralKey, ownerKey, data)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-17 16:45:50 +00:00
|
|
|
|
|
|
|
gate := new(AccessBox_Gate)
|
2021-06-24 15:21:34 +00:00
|
|
|
gate.GatePublicKey = ownerKey.Bytes()
|
2021-06-17 16:45:50 +00:00
|
|
|
gate.Tokens = encrypted
|
|
|
|
return gate, nil
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
func decodeGate(gate *AccessBox_Gate, owner *keys.PrivateKey, sender *keys.PublicKey) (*GateData, error) {
|
2021-06-17 16:45:50 +00:00
|
|
|
data, err := decrypt(owner, sender, gate.Tokens)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
2021-06-17 16:45:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tokens := new(Tokens)
|
|
|
|
if err := proto.Unmarshal(data, tokens); err != nil {
|
|
|
|
return nil, err
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
var bearerTkn bearer.Token
|
|
|
|
if err = bearerTkn.Unmarshal(tokens.BearerToken); err != nil {
|
2021-06-17 16:45:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-26 09:09:28 +00:00
|
|
|
|
2022-05-04 12:29:11 +00:00
|
|
|
sessionTkns := make([]*session.Container, len(tokens.SessionTokens))
|
2022-01-26 09:09:28 +00:00
|
|
|
for i, encSessionToken := range tokens.SessionTokens {
|
2022-05-04 12:29:11 +00:00
|
|
|
sessionTkn := new(session.Container)
|
2022-01-26 09:09:28 +00:00
|
|
|
if err := sessionTkn.Unmarshal(encSessionToken); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sessionTkns[i] = sessionTkn
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
gateData := NewGateData(owner.PublicKey(), &bearerTkn)
|
2022-01-26 09:09:28 +00:00
|
|
|
gateData.SessionTokens = sessionTkns
|
2021-06-17 16:45:50 +00:00
|
|
|
gateData.AccessKey = hex.EncodeToString(tokens.AccessKey)
|
|
|
|
return gateData, nil
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
func generateShared256(prv *keys.PrivateKey, pub *keys.PublicKey) (sk []byte, err error) {
|
|
|
|
if prv.PublicKey().Curve != pub.Curve {
|
2021-06-16 14:07:31 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
func encrypt(owner *keys.PrivateKey, sender *keys.PublicKey, data []byte) ([]byte, error) {
|
2021-06-16 14:07:31 +00:00
|
|
|
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-24 15:21:34 +00:00
|
|
|
func decrypt(owner *keys.PrivateKey, sender *keys.PublicKey, data []byte) ([]byte, error) {
|
2021-06-16 14:07:31 +00:00
|
|
|
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
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
func getCipher(owner *keys.PrivateKey, sender *keys.PublicKey) (cipher.AEAD, error) {
|
2021-06-16 14:07:31 +00:00
|
|
|
secret, err := generateShared256(owner, sender)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := deriveKey(secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
return chacha20poly1305.NewX(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSecret() ([]byte, error) {
|
|
|
|
b := make([]byte, 32)
|
|
|
|
_, err := io.ReadFull(rand.Reader, b)
|
|
|
|
return b, err
|
2021-06-16 14:07:31 +00:00
|
|
|
}
|