2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-11 15:35:55 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2019-01-30 20:47:21 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-10-11 15:35:55 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-07 09:04:29 +00:00
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
2018-10-11 15:35:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GenerateSignature creates signature from identity id
|
2019-01-30 20:47:21 +00:00
|
|
|
func GenerateSignature(data []byte, identity *identity.FullIdentity) ([]byte, error) {
|
2019-02-07 20:39:20 +00:00
|
|
|
return pkcrypto.HashAndSign(identity.Key, data)
|
2018-10-11 15:35:55 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 12:40:11 +01:00
|
|
|
// NewSignedMessage creates instance of signed message
|
2019-01-30 20:47:21 +00:00
|
|
|
func NewSignedMessage(signature []byte, identity *identity.FullIdentity) (*pb.SignedMessage, error) {
|
2019-02-07 20:39:20 +00:00
|
|
|
encodedKey, err := pkcrypto.PublicKeyToPKIX(identity.Leaf.PublicKey)
|
2018-10-11 15:35:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-17 12:40:11 +01:00
|
|
|
return &pb.SignedMessage{
|
2018-10-11 15:35:55 +01:00
|
|
|
Data: identity.ID.Bytes(),
|
|
|
|
Signature: signature,
|
|
|
|
PublicKey: encodedKey,
|
|
|
|
}, nil
|
|
|
|
}
|
2018-10-17 12:40:11 +01:00
|
|
|
|
|
|
|
// SignedMessageVerifier checks if provided signed message can be verified
|
|
|
|
type SignedMessageVerifier func(signature *pb.SignedMessage) error
|
|
|
|
|
|
|
|
// NewSignedMessageVerifier creates default implementation of SignedMessageVerifier
|
|
|
|
func NewSignedMessageVerifier() SignedMessageVerifier {
|
|
|
|
return func(signedMessage *pb.SignedMessage) error {
|
|
|
|
if signedMessage == nil {
|
|
|
|
return Error.New("no message to verify")
|
|
|
|
}
|
|
|
|
if signedMessage.Signature == nil {
|
|
|
|
return Error.New("missing signature for verification")
|
|
|
|
}
|
|
|
|
if signedMessage.Data == nil {
|
|
|
|
return Error.New("missing data for verification")
|
|
|
|
}
|
|
|
|
if signedMessage.PublicKey == nil {
|
|
|
|
return Error.New("missing public key for verification")
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:39:20 +00:00
|
|
|
k, err := pkcrypto.PublicKeyFromPKIX(signedMessage.GetPublicKey())
|
2018-10-17 12:40:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2019-02-07 20:39:20 +00:00
|
|
|
return pkcrypto.HashAndVerifySignature(k, signedMessage.GetData(), signedMessage.GetSignature())
|
2018-10-17 12:40:11 +01:00
|
|
|
}
|
|
|
|
}
|