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
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2021-06-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
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
|
2022-06-15 19:31:41 +00:00
|
|
|
Policy netmap.PlacementPolicy
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
// GateData represents gate tokens in AccessBox.
|
|
|
|
type GateData struct {
|
2023-10-02 09:42:02 +00:00
|
|
|
SecretKey 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
|
|
|
}
|
|
|
|
|
2024-03-01 14:11:07 +00:00
|
|
|
// SessionToken returns the first container session context.
|
|
|
|
func (g *GateData) SessionToken() *session.Container {
|
|
|
|
if len(g.SessionTokens) != 0 {
|
|
|
|
return g.SessionTokens[0]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-05-28 12:50:34 +00:00
|
|
|
case session.VerbContainerDelete, session.VerbContainerPut:
|
2022-05-04 12:29:11 +00:00
|
|
|
return tok.AssertVerb(verb)
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2022-01-26 09:09:28 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 09:42:02 +00:00
|
|
|
// Secrets represents SecretKey and the key to encrypt gate tokens.
|
2021-06-17 16:45:50 +00:00
|
|
|
type Secrets struct {
|
2023-10-02 09:42:02 +00:00
|
|
|
SecretKey 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.
|
2023-06-13 09:35:40 +00:00
|
|
|
// Secret can be nil. In such case secret will be generated.
|
2024-08-30 12:05:32 +00:00
|
|
|
func PackTokens(gatesData []*GateData, secret []byte, isCustomSecret bool) (*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 {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, nil, fmt.Errorf("create ephemeral key: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2023-10-06 11:00:37 +00:00
|
|
|
box.SeedKey = ephemeralKey.PublicKey().Bytes()
|
2024-10-14 09:07:09 +00:00
|
|
|
box.IsCustom = isCustomSecret
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2023-06-13 09:35:40 +00:00
|
|
|
if secret == nil {
|
|
|
|
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
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
secretKey := string(secret)
|
|
|
|
if !isCustomSecret {
|
|
|
|
secretKey = hex.EncodeToString(secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
return box, &Secrets{SecretKey: secretKey, EphemeralKey: 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.
|
2024-10-14 09:07:09 +00:00
|
|
|
func (x *AccessBox) GetTokens(owner *keys.PrivateKey) (*GateData, error) {
|
2023-10-06 11:00:37 +00:00
|
|
|
seedKey, err := keys.NewPublicKeyFromBytes(x.SeedKey, elliptic.P256())
|
2021-06-24 15:21:34 +00:00
|
|
|
if err != nil {
|
2023-10-06 11:00:37 +00:00
|
|
|
return nil, fmt.Errorf("couldn't unmarshal SeedKey: %w", err)
|
2021-06-24 15:21:34 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2024-10-14 09:07:09 +00:00
|
|
|
gateData, err := x.decodeGate(gate, owner, seedKey)
|
2021-06-17 16:45:50 +00:00
|
|
|
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 {
|
2022-06-15 19:31:41 +00:00
|
|
|
var cnrPolicy ContainerPolicy
|
|
|
|
if err := cnrPolicy.Policy.Unmarshal(policy.Policy); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal placement policy: %w", err)
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 19:31:41 +00:00
|
|
|
cnrPolicy.LocationConstraint = policy.LocationConstraint
|
|
|
|
|
|
|
|
result = append(result, &cnrPolicy)
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// GetBox parses AccessBox to Box.
|
2024-10-14 09:07:09 +00:00
|
|
|
func (x *AccessBox) GetBox(owner *keys.PrivateKey) (*Box, error) {
|
|
|
|
tokens, err := x.GetTokens(owner)
|
2021-07-16 12:35:07 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("get tokens: %w", err)
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := x.GetPlacementPolicy()
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("get policy: %w", err)
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-10-02 09:42:02 +00:00
|
|
|
tokens.SecretKey = secret
|
2021-06-17 16:45:50 +00:00
|
|
|
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 {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("encode gate: %w", 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
|
|
|
|
}
|
|
|
|
|
2023-10-06 11:00:37 +00:00
|
|
|
func encodeGate(ephemeralKey *keys.PrivateKey, seedKey *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 {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("encode tokens: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 11:00:37 +00:00
|
|
|
encrypted, err := encrypt(ephemeralKey, seedKey, data)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("ecrypt tokens: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-06-17 16:45:50 +00:00
|
|
|
|
|
|
|
gate := new(AccessBox_Gate)
|
2023-10-06 11:00:37 +00:00
|
|
|
gate.GatePublicKey = seedKey.Bytes()
|
2021-06-17 16:45:50 +00:00
|
|
|
gate.Tokens = encrypted
|
|
|
|
return gate, nil
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 09:07:09 +00:00
|
|
|
func (x *AccessBox) decodeGate(gate *AccessBox_Gate, owner *keys.PrivateKey, seedKey *keys.PublicKey) (*GateData, error) {
|
2023-10-06 11:00:37 +00:00
|
|
|
data, err := decrypt(owner, seedKey, gate.Tokens)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("decrypt tokens: %w", err)
|
2021-06-17 16:45:50 +00:00
|
|
|
}
|
|
|
|
tokens := new(Tokens)
|
2022-06-22 19:40:52 +00:00
|
|
|
if err = proto.Unmarshal(data, tokens); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal tokens: %w", 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 {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal bearer token: %w", err)
|
2021-06-17 16:45:50 +00:00
|
|
|
}
|
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-06-22 19:40:52 +00:00
|
|
|
if err = sessionTkn.Unmarshal(encSessionToken); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal session token: %w", err)
|
2022-01-26 09:09:28 +00:00
|
|
|
}
|
|
|
|
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
|
2024-10-14 09:07:09 +00:00
|
|
|
if x.IsCustom {
|
2024-08-30 12:05:32 +00:00
|
|
|
gateData.SecretKey = string(tokens.SecretKey)
|
|
|
|
} else {
|
|
|
|
gateData.SecretKey = hex.EncodeToString(tokens.SecretKey)
|
|
|
|
}
|
2021-06-17 16:45:50 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-06 11:00:37 +00:00
|
|
|
func encrypt(owner *keys.PrivateKey, seedKey *keys.PublicKey, data []byte) ([]byte, error) {
|
|
|
|
enc, err := getCipher(owner, seedKey)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("get chiper: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 14:50:15 +00:00
|
|
|
nonce := make([]byte, enc.NonceSize())
|
2022-06-22 19:40:52 +00:00
|
|
|
if _, err = rand.Read(nonce); err != nil {
|
|
|
|
return nil, fmt.Errorf("generate random nonce: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return enc.Seal(nonce, nonce, data, nil), nil
|
|
|
|
}
|
|
|
|
|
2023-10-06 11:00:37 +00:00
|
|
|
func decrypt(owner *keys.PrivateKey, seedKey *keys.PublicKey, data []byte) ([]byte, error) {
|
|
|
|
dec, err := getCipher(owner, seedKey)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("get chiper: %w", err)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-10-06 11:00:37 +00:00
|
|
|
func getCipher(owner *keys.PrivateKey, seedKey *keys.PublicKey) (cipher.AEAD, error) {
|
|
|
|
secret, err := generateShared256(owner, seedKey)
|
2021-06-16 14:07:31 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("generate shared key: %w", err)
|
2021-06-16 14:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key, err := deriveKey(secret)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("derive key: %w", err)
|
2021-06-16 14:07:31 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|