2021-05-19 16:14:00 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-25 09:09:44 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-27 12:07:39 +00:00
|
|
|
"errors"
|
2021-05-19 16:14:00 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2021-05-19 16:14:00 +00:00
|
|
|
)
|
|
|
|
|
2021-05-28 12:39:27 +00:00
|
|
|
var (
|
2022-05-18 15:20:08 +00:00
|
|
|
errWrongSessionVerb = errors.New("wrong token verb")
|
|
|
|
errWrongCID = errors.New("wrong container ID")
|
2021-05-28 12:39:27 +00:00
|
|
|
)
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
type signatureVerificationData struct {
|
|
|
|
ownerContainer user.ID
|
2021-05-19 16:14:00 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
verb session.ContainerVerb
|
2021-05-27 12:07:39 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
idContainerSet bool
|
|
|
|
idContainer cid.ID
|
2021-05-27 12:07:39 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
binTokenSession []byte
|
2021-05-27 12:07:39 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
binPublicKey []byte
|
|
|
|
|
|
|
|
signature []byte
|
2021-05-27 12:07:39 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
signedData []byte
|
2021-05-27 12:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
// verifySignature is a common method of Container service authentication. Asserts that:
|
2022-08-15 16:20:20 +00:00
|
|
|
// - for trusted parties: session is valid (*) and issued by container owner
|
|
|
|
// - operation data is signed by container owner or trusted party
|
|
|
|
// - operation data signature is correct
|
2022-05-18 15:20:08 +00:00
|
|
|
//
|
|
|
|
// (*) includes:
|
2022-08-15 16:20:20 +00:00
|
|
|
// - session token decodes correctly
|
|
|
|
// - signature is valid
|
|
|
|
// - session issued by the container owner
|
|
|
|
// - v.binPublicKey is a public session key
|
|
|
|
// - session context corresponds to the container and verb in v
|
|
|
|
// - session is "alive"
|
2022-05-18 15:20:08 +00:00
|
|
|
func (cp *Processor) verifySignature(v signatureVerificationData) error {
|
|
|
|
var err error
|
2022-12-23 17:35:35 +00:00
|
|
|
var key frostfsecdsa.PublicKeyRFC6979
|
2022-05-18 15:20:08 +00:00
|
|
|
keyProvided := v.binPublicKey != nil
|
|
|
|
|
|
|
|
if keyProvided {
|
|
|
|
err = key.Decode(v.binPublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode public key: %w", err)
|
2021-05-27 12:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:58:21 +00:00
|
|
|
if len(v.binTokenSession) > 0 {
|
2023-03-30 07:46:43 +00:00
|
|
|
return cp.verifyByTokenSession(v, &key, keyProvided)
|
2022-06-07 17:58:21 +00:00
|
|
|
}
|
2021-06-04 15:02:25 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
if keyProvided {
|
2022-06-07 17:58:21 +00:00
|
|
|
var idFromKey user.ID
|
|
|
|
user.IDFromKey(&idFromKey, (ecdsa.PublicKey)(key))
|
|
|
|
|
|
|
|
if v.ownerContainer.Equals(idFromKey) {
|
|
|
|
if key.Verify(v.signedData, v.signature) {
|
|
|
|
return nil
|
2022-05-18 15:20:08 +00:00
|
|
|
}
|
2021-05-27 12:07:39 +00:00
|
|
|
|
2022-06-07 17:58:21 +00:00
|
|
|
return errors.New("invalid signature calculated by container owner's key")
|
|
|
|
}
|
2021-05-28 12:39:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 17:58:21 +00:00
|
|
|
return errors.New("signature is invalid or calculated with the key not bound to the container owner")
|
2021-05-28 12:39:27 +00:00
|
|
|
}
|
2021-06-04 15:02:25 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
func (cp *Processor) checkTokenLifetime(token session.Container) error {
|
2021-06-04 15:02:25 +00:00
|
|
|
curEpoch, err := cp.netState.Epoch()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not read current epoch: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
if token.InvalidAt(curEpoch) {
|
|
|
|
return fmt.Errorf("token is not valid at %d", curEpoch)
|
2021-06-04 15:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-30 07:46:43 +00:00
|
|
|
|
|
|
|
func (cp *Processor) verifyByTokenSession(v signatureVerificationData, key *frostfsecdsa.PublicKeyRFC6979, keyProvided bool) error {
|
|
|
|
var tok session.Container
|
|
|
|
|
|
|
|
err := tok.Unmarshal(v.binTokenSession)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode session token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tok.VerifySignature() {
|
|
|
|
return errors.New("invalid session token signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyProvided && !tok.AssertAuthKey(key) {
|
|
|
|
return errors.New("signed with a non-session key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tok.AssertVerb(v.verb) {
|
|
|
|
return errWrongSessionVerb
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.idContainerSet && !tok.AppliedTo(v.idContainer) {
|
|
|
|
return errWrongCID
|
|
|
|
}
|
|
|
|
|
|
|
|
if !session.IssuedBy(tok, v.ownerContainer) {
|
|
|
|
return errors.New("owner differs with token owner")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cp.checkTokenLifetime(tok)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("check session lifetime: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tok.VerifySessionDataSignature(v.signedData, v.signature) {
|
|
|
|
return errors.New("invalid signature calculated with session key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|