| 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 |
"errors" |
| 21 |
"os" |
| 22 |
"path/filepath" |
| 23 |
"strings" |
| 24 |
|
| 25 |
tinkUtils "github.com/sigstore/sigstore/pkg/signature/tink" |
| 26 |
"github.com/tink-crypto/tink-go-awskms/v3/integration/awskms" |
| 27 |
"github.com/tink-crypto/tink-go-gcpkms/v2/integration/gcpkms" |
| 28 |
"github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault" |
| 29 |
"github.com/tink-crypto/tink-go/v2/core/registry" |
| 30 |
"github.com/tink-crypto/tink-go/v2/keyset" |
| 31 |
"github.com/tink-crypto/tink-go/v2/tink" |
| 32 |
) |
| 33 |
|
| 34 |
// NewTinkSigner creates a signer by decrypting a local Tink keyset with a remote KMS encryption key |
| 35 |
func NewTinkSigner(tinkKeysetPath string, primaryKey tink.AEAD) (crypto.Signer, error) { |
| 36 |
f, err := os.Open(filepath.Clean(tinkKeysetPath)) |
| 37 |
if err != nil { |
| 38 |
return nil, err |
| 39 |
} |
| 40 |
defer f.Close() |
| 41 |
|
| 42 |
kh, err := keyset.Read(keyset.NewJSONReader(f), primaryKey) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
signer, err := tinkUtils.KeyHandleToSigner(kh) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
return signer, nil |
| 51 |
} |
| 52 |
|
| 53 |
// GetPrimaryKey returns a Tink AEAD encryption key from KMS |
| 54 |
// Supports GCP, AWS, and Vault |
| 55 |
func GetPrimaryKey(ctx context.Context, kmsKey, hcVaultToken string) (tink.AEAD, error) { |
| 56 |
switch { |
| 57 |
case strings.HasPrefix(kmsKey, "gcp-kms://"): |
| 58 |
gcpClient, err := gcpkms.NewClientWithOptions(ctx, kmsKey) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
registry.RegisterKMSClient(gcpClient) |
| 63 |
return gcpClient.GetAEAD(kmsKey) |
| 64 |
case strings.HasPrefix(kmsKey, "aws-kms://"): |
| 65 |
awsClient, err := awskms.NewClientWithOptions(ctx, kmsKey) |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
registry.RegisterKMSClient(awsClient) |
| 70 |
return awsClient.GetAEAD(kmsKey) |
| 71 |
case strings.HasPrefix(kmsKey, "hcvault://"): |
| 72 |
hcVaultClient, err := hcvault.NewClient(kmsKey, nil, hcVaultToken) |
| 73 |
if err != nil { |
| 74 |
return nil, err |
| 75 |
} |
| 76 |
registry.RegisterKMSClient(hcVaultClient) |
| 77 |
return hcVaultClient.GetAEAD(kmsKey) |
| 78 |
default: |
| 79 |
return nil, errors.New("unsupported Tink KMS key type") |
| 80 |
} |
| 81 |
} |