...

Source file src/github.com/sigstore/timestamp-authority/pkg/signer/signer.go

Documentation: github.com/sigstore/timestamp-authority/pkg/signer

     1  // Copyright 2022 The Sigstore Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package signer
    16  
    17  import (
    18  	"context"
    19  	"crypto"
    20  	"crypto/elliptic"
    21  	"crypto/rand"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/sigstore/sigstore/pkg/signature"
    26  	"github.com/sigstore/sigstore/pkg/signature/kms"
    27  
    28  	// Register the provider-specific plugins
    29  	_ "github.com/sigstore/sigstore/pkg/signature/kms/aws"
    30  	_ "github.com/sigstore/sigstore/pkg/signature/kms/azure"
    31  	_ "github.com/sigstore/sigstore/pkg/signature/kms/gcp"
    32  	_ "github.com/sigstore/sigstore/pkg/signature/kms/hashivault"
    33  )
    34  
    35  const KMSScheme = "kms"
    36  const TinkScheme = "tink"
    37  const MemoryScheme = "memory"
    38  const FileScheme = "file"
    39  
    40  func NewCryptoSigner(ctx context.Context, hash crypto.Hash, signer, kmsKey, tinkKmsKey, tinkKeysetPath, hcVaultToken, fileSignerPath, fileSignerPasswd string) (crypto.Signer, error) {
    41  	switch signer {
    42  	case MemoryScheme:
    43  		sv, _, err := signature.NewECDSASignerVerifier(elliptic.P256(), rand.Reader, crypto.SHA256)
    44  		return sv, err
    45  	case FileScheme:
    46  		return NewFileSigner(fileSignerPath, fileSignerPasswd, hash)
    47  	case KMSScheme:
    48  		signer, err := kms.Get(ctx, kmsKey, hash) // hash is ignored for all KMS providers except Hashivault
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  		s, _, err := signer.CryptoSigner(ctx, func(err error) {})
    53  		return s, err
    54  	case TinkScheme:
    55  		primaryKey, err := GetPrimaryKey(ctx, tinkKmsKey, hcVaultToken)
    56  		if err != nil {
    57  			return nil, err
    58  		}
    59  		return NewTinkSigner(ctx, tinkKeysetPath, primaryKey)
    60  	default:
    61  		return nil, fmt.Errorf("unsupported signer type: %s", signer)
    62  	}
    63  }
    64  
    65  func HashToAlg(signerHashAlg string) (crypto.Hash, error) {
    66  	lowercaseAlg := strings.ToLower(signerHashAlg)
    67  	var hash crypto.Hash
    68  	switch lowercaseAlg {
    69  	case "sha256":
    70  		hash = crypto.SHA256
    71  	case "sha384":
    72  		hash = crypto.SHA384
    73  	case "sha512":
    74  		hash = crypto.SHA512
    75  	default:
    76  		return crypto.Hash(0), fmt.Errorf("unsupported hash algorithm: %s", lowercaseAlg)
    77  	}
    78  	return hash, nil
    79  }
    80  

View as plain text