1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
// Copyright 2021 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package artifact
import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"math/big"
"github.com/minio/sha256-simd"
"github.com/pkg/errors"
)
// Signer is returning a signature of the provided message.
type Signer interface {
Sign(message []byte) ([]byte, error)
}
// Verifier is verifying if provided message and signature matches.
type Verifier interface {
Verify(message, sig []byte) error
}
// Crypto is an interface each specific signature algorithm must implement
// in order to be used with PKISigner.
type Crypto interface {
Sign(message []byte, key interface{}) ([]byte, error)
Verify(message, sig []byte, key interface{}) error
}
//
// RSA Crypto interface implementation
//
type RSA struct{}
func (r *RSA) Sign(message []byte, key interface{}) ([]byte, error) {
var rsaKey *rsa.PrivateKey
var ok bool
// validate key
if rsaKey, ok = key.(*rsa.PrivateKey); !ok {
return nil, errors.New("signer: invalid private key")
}
h := sha256.Sum256(message)
return rsa.SignPKCS1v15(rand.Reader, rsaKey, crypto.SHA256, h[:])
}
func (r *RSA) Verify(message, sig []byte, key interface{}) error {
var rsaKey *rsa.PublicKey
var ok bool
// validate key
if rsaKey, ok = key.(*rsa.PublicKey); !ok {
return errors.New("signer: invalid rsa public key")
}
h := sha256.Sum256(message)
return rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, h[:], sig)
}
//
// ECDSA Crypto interface implementation
//
const ecdsa256curveBits = 256
const ecdsa256keySize = 32
type ECDSA256 struct{}
func (e *ECDSA256) Sign(message []byte, key interface{}) ([]byte, error) {
var ecdsaKey *ecdsa.PrivateKey
var ok bool
// validate key
if ecdsaKey, ok = key.(*ecdsa.PrivateKey); !ok {
return nil, errors.New("signer: invalid private key")
}
// calculate the hash of the message to be signed
h := sha256.Sum256(message)
r, s, err := ecdsa.Sign(rand.Reader, ecdsaKey, h[:])
if err != nil {
return nil, errors.Wrap(err, "signer: error signing message")
}
// check if the size of the curve matches expected one;
// for now we are supporting only 256 bit ecdsa
if ecdsaKey.Curve.Params().BitSize != ecdsa256curveBits {
return nil, errors.New("signer: invalid ecdsa curve size")
}
return MarshalECDSASignature(r, s)
}
func (e *ECDSA256) Verify(message, sig []byte, key interface{}) error {
var ecdsaKey *ecdsa.PublicKey
var ok bool
// validate key
if ecdsaKey, ok = key.(*ecdsa.PublicKey); !ok {
return errors.New("signer: invalid ecdsa public key")
}
r, s, err := UnmarshalECDSASignature(sig)
if err != nil {
return err
}
h := sha256.Sum256(message)
ok = ecdsa.Verify(ecdsaKey, h[:], r, s)
if !ok {
return errors.New("signer: verification failed")
}
return nil
}
func MarshalECDSASignature(r, s *big.Int) ([]byte, error) {
// we serialize the r and s into one array where the first
// half is the r and the other one s;
// as both values are ecdsa256curveBits size we need
// 2*ecdsa256keySize size slice to store both
// MEN-1740 In some cases the size of the r and s can be different
// than expected ecdsa256keySize. In this case we need to make sure
// we are serializing those using correct offset. We can use leading
// zeros easily as this has no impact on serializing and deserializing.
rSize := len(r.Bytes())
sSize := len(s.Bytes())
if rSize > ecdsa256keySize || sSize > ecdsa256keySize {
return nil,
errors.Errorf("signer: invalid size of ecdsa keys: r: %d; s: %d",
rSize, sSize)
}
// if the keys are shorter than expected we need to use correct offset
// while serializing
rOffset := ecdsa256keySize - rSize
sOffset := ecdsa256keySize - sSize
serialized := make([]byte, 2*ecdsa256keySize)
copy(serialized[rOffset:], r.Bytes())
copy(serialized[ecdsa256keySize+sOffset:], s.Bytes())
return serialized, nil
}
func UnmarshalECDSASignature(sig []byte) (r, s *big.Int, e error) {
// check if the size of the key matches provided one
if len(sig) != 2*ecdsa256keySize {
return nil, nil, errors.Errorf("signer: invalid ecdsa key size: %d", len(sig))
}
// get the signature; see corresponding `Sign` function for more details
// about serialization
r = big.NewInt(0).SetBytes(sig[:ecdsa256keySize])
s = big.NewInt(0).SetBytes(sig[ecdsa256keySize:])
return r, s, nil
}
type SigningMethod struct {
// Key can be private or public depending if we want to sign or verify message
Key interface{}
Public []byte
Method Crypto
}
// PKISigner implements public-key encryption and supports X.509-encodded keys.
// For now both RSA and 256 bits ECDSA are supported.
type PKISigner struct {
signMethod, verifyMethod *SigningMethod
}
func NewPKISigner(privateKey []byte) (*PKISigner, error) {
if len(privateKey) == 0 {
return nil, errors.New("signer: missing key")
}
signMethod, err := GetKeyAndSignMethod(privateKey)
if err != nil {
return nil, err
}
pub, err := x509.ParsePKIXPublicKey(signMethod.Public)
if err != nil {
return nil, errors.Wrap(err, "failed to parse encoded public key")
}
return &PKISigner{
signMethod: signMethod,
verifyMethod: &SigningMethod{
Key: pub,
Method: signMethod.Method,
},
}, nil
}
func NewPKIVerifier(publicKey []byte) (*PKISigner, error) {
if len(publicKey) == 0 {
return nil, errors.New("signer: missing key")
}
verifyMethod, err := GetKeyAndVerifyMethod(publicKey)
if err != nil {
return nil, err
}
return &PKISigner{
verifyMethod: verifyMethod,
}, nil
}
func (s *PKISigner) Sign(message []byte) ([]byte, error) {
if s.signMethod == nil {
return nil, errors.New("signer: only verification allowed with this signer")
}
sig, err := s.signMethod.Method.Sign(message, s.signMethod.Key)
if err != nil {
return nil, errors.Wrap(err, "signer: error signing image")
}
enc := make([]byte, base64.StdEncoding.EncodedLen(len(sig)))
base64.StdEncoding.Encode(enc, sig)
return enc, nil
}
func (s *PKISigner) Verify(message, sig []byte) error {
dec := make([]byte, base64.StdEncoding.DecodedLen(len(sig)))
decLen, err := base64.StdEncoding.Decode(dec, sig)
if err != nil {
return errors.Wrap(err, "signer: error decoding signature")
}
if s.verifyMethod == nil {
return errors.New("verifyMethod is nil")
}
if s.verifyMethod.Method == nil {
return errors.New("verifyMethod.Method is nil")
}
return s.verifyMethod.Method.Verify(message, dec[:decLen], s.verifyMethod.Key)
}
func GetPublic(private []byte) ([]byte, error) {
sm, err := GetKeyAndSignMethod(private)
if err != nil {
return nil, errors.Wrap(err, "signer: error parsing private key")
}
return sm.Public, nil
}
func GetKeyAndVerifyMethod(keyPEM []byte) (*SigningMethod, error) {
block, _ := pem.Decode(keyPEM)
if block == nil {
return nil, errors.New("signer: failed to parse public key")
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, errors.Wrap(err, "failed to parse encoded public key")
}
switch pub := pub.(type) {
case *rsa.PublicKey:
return &SigningMethod{Key: pub, Method: new(RSA)}, nil
case *ecdsa.PublicKey:
return &SigningMethod{Key: pub, Method: new(ECDSA256)}, nil
default:
return nil, errors.Errorf("unsupported public key type: %v", pub)
}
}
func GetKeyAndSignMethod(keyPEM []byte) (*SigningMethod, error) {
block, _ := pem.Decode(keyPEM)
if block == nil {
return nil, errors.New("signer: failed to parse private key")
}
rsaKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err == nil {
pub, keyErr := x509.MarshalPKIXPublicKey(rsaKey.Public())
if keyErr != nil {
return nil, errors.Wrap(err, "signer: can not extract public RSA key")
}
return &SigningMethod{Key: rsaKey, Public: pub, Method: new(RSA)}, nil
}
ecdsaKey, err := x509.ParseECPrivateKey(block.Bytes)
if err == nil {
pub, keyErr := x509.MarshalPKIXPublicKey(ecdsaKey.Public())
if keyErr != nil {
return nil, errors.Wrap(err, "signer: can not extract public ECDSA key")
}
return &SigningMethod{Key: ecdsaKey, Public: pub, Method: new(ECDSA256)}, nil
}
return nil, errors.Wrap(err, "signer: unsupported private key type or error occured")
}
|