2020-07-15 13:48:25 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-07-15 20:16:27 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2020-07-15 13:48:25 +00:00
|
|
|
"crypto/sha256"
|
2020-07-15 20:16:27 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"io/ioutil"
|
2020-07-15 13:48:25 +00:00
|
|
|
|
|
|
|
"github.com/klauspost/compress/zstd"
|
2020-07-15 20:16:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/refs"
|
2020-07-15 13:48:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/service"
|
2020-07-15 20:16:27 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2020-07-15 13:48:25 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Center is a central app's authentication/authorization management unit.
|
|
|
|
type Center struct {
|
|
|
|
zstdEncoder *zstd.Encoder
|
|
|
|
zstdDecoder *zstd.Decoder
|
2020-07-15 20:16:27 +00:00
|
|
|
neofsKeys struct {
|
|
|
|
PrivateKey *ecdsa.PrivateKey
|
|
|
|
PublicKey *ecdsa.PublicKey
|
|
|
|
}
|
|
|
|
ownerID refs.OwnerID
|
|
|
|
wifString string
|
|
|
|
userAuthKeys struct {
|
|
|
|
PrivateKey *rsa.PrivateKey
|
|
|
|
PublicKey *rsa.PublicKey
|
|
|
|
}
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
// NewCenter creates an instance of AuthCenter.
|
2020-07-15 20:16:27 +00:00
|
|
|
func NewCenter() *Center {
|
2020-07-15 13:48:25 +00:00
|
|
|
zstdEncoder, _ := zstd.NewWriter(nil)
|
|
|
|
zstdDecoder, _ := zstd.NewReader(nil)
|
2020-07-15 20:16:27 +00:00
|
|
|
return &Center{
|
2020-07-15 15:20:45 +00:00
|
|
|
zstdEncoder: zstdEncoder,
|
|
|
|
zstdDecoder: zstdDecoder,
|
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) SetNeoFSKeys(key *ecdsa.PrivateKey) error {
|
|
|
|
publicKey := &key.PublicKey
|
|
|
|
oid, err := refs.NewOwnerID(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get OwnerID")
|
|
|
|
}
|
|
|
|
center.neofsKeys.PrivateKey = key
|
|
|
|
wif, err := crypto.WIFEncode(key)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get WIF string from given key")
|
|
|
|
}
|
|
|
|
center.neofsKeys.PublicKey = publicKey
|
|
|
|
center.ownerID = oid
|
|
|
|
center.wifString = wif
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) GetNeoFSKeyPrivateKey() *ecdsa.PrivateKey {
|
|
|
|
return center.neofsKeys.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) GetNeoFSKeyPublicKey() *ecdsa.PublicKey {
|
|
|
|
return center.neofsKeys.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) GetOwnerID() refs.OwnerID {
|
|
|
|
return center.ownerID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) GetWIFString() string {
|
|
|
|
return center.wifString
|
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) SetUserAuthKeys(key *rsa.PrivateKey) {
|
|
|
|
center.userAuthKeys.PrivateKey = key
|
|
|
|
center.userAuthKeys.PublicKey = &key.PublicKey
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) PackBearerToken(bearerToken *service.BearerTokenMsg) ([]byte, error) {
|
2020-07-15 13:48:25 +00:00
|
|
|
data, err := bearerToken.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal bearer token")
|
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
encryptedKeyID, err := encrypt(center.userAuthKeys.PublicKey, center.compress(data))
|
2020-07-15 13:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "")
|
|
|
|
}
|
|
|
|
return append(sha256Hash(data), encryptedKeyID...), nil
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) UnpackBearerToken(packedBearerToken []byte) (*service.BearerTokenMsg, error) {
|
2020-07-15 13:48:25 +00:00
|
|
|
compressedKeyID := packedBearerToken[32:]
|
2020-07-15 15:20:45 +00:00
|
|
|
encryptedKeyID, err := center.decompress(compressedKeyID)
|
2020-07-15 13:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decompress key ID")
|
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
keyID, err := decrypt(center.userAuthKeys.PrivateKey, encryptedKeyID)
|
2020-07-15 13:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decrypt key ID")
|
|
|
|
}
|
|
|
|
bearerToken := new(service.BearerTokenMsg)
|
|
|
|
if err := bearerToken.Unmarshal(keyID); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to unmarshal embedded bearer token")
|
|
|
|
}
|
|
|
|
return bearerToken, nil
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) compress(data []byte) []byte {
|
|
|
|
center.zstdEncoder.Reset(nil)
|
2020-07-15 13:48:25 +00:00
|
|
|
var compressedData []byte
|
2020-07-15 15:20:45 +00:00
|
|
|
center.zstdEncoder.EncodeAll(data, compressedData)
|
2020-07-15 13:48:25 +00:00
|
|
|
return compressedData
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) decompress(data []byte) ([]byte, error) {
|
|
|
|
center.zstdDecoder.Reset(nil)
|
2020-07-15 13:48:25 +00:00
|
|
|
var decompressedData []byte
|
2020-07-15 15:20:45 +00:00
|
|
|
if _, err := center.zstdDecoder.DecodeAll(data, decompressedData); err != nil {
|
2020-07-15 13:48:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return decompressedData, nil
|
|
|
|
}
|
|
|
|
|
2020-07-15 20:16:27 +00:00
|
|
|
func encrypt(key *rsa.PublicKey, data []byte) ([]byte, error) {
|
|
|
|
return rsa.EncryptOAEP(sha256.New(), rand.Reader, key, data, []byte{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func decrypt(key *rsa.PrivateKey, data []byte) ([]byte, error) {
|
|
|
|
return rsa.DecryptOAEP(sha256.New(), rand.Reader, key, data, []byte{})
|
|
|
|
}
|
|
|
|
|
2020-07-15 13:48:25 +00:00
|
|
|
func sha256Hash(data []byte) []byte {
|
|
|
|
hash := sha256.New()
|
|
|
|
hash.Write(data)
|
|
|
|
return hash.Sum(nil)
|
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
|
|
|
|
func ReadRSAPrivateKeyFromPEMFile(filePath string) (*rsa.PrivateKey, error) {
|
|
|
|
kbs, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to read file %s", filePath)
|
|
|
|
}
|
|
|
|
pemBlock, _ := pem.Decode(kbs)
|
|
|
|
if pemBlock == nil {
|
|
|
|
return nil, errors.Errorf("failed to decode PEM data from file %s", filePath)
|
|
|
|
}
|
|
|
|
rsaKey, err := x509.ParsePKCS1PrivateKey(pemBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to parse private key bytes from pem data from file %s", filePath)
|
|
|
|
}
|
|
|
|
return rsaKey, nil
|
|
|
|
}
|