|
| 1 | +// Copyright 2025 Google LLC. All Rights Reserved. |
| 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 main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "crypto" |
| 20 | + "crypto/ecdsa" |
| 21 | + "crypto/elliptic" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + |
| 27 | + tinkUtils "github.com/sigstore/sigstore/pkg/signature/tink" |
| 28 | + "github.com/tink-crypto/tink-go-gcpkms/v2/integration/gcpkms" |
| 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 | +const TinkScheme = "tink" |
| 35 | + |
| 36 | +// NewTinkSignerVerifier returns a crypto.Signer. Only ECDSA P-256 is supported. |
| 37 | +// Provide a path to the encrypted keyset and GCP KMS key URI for decryption. |
| 38 | +func NewTinkSignerVerifier(ctx context.Context, kekURI, keysetPath string) (crypto.Signer, error) { |
| 39 | + if kekURI == "" || keysetPath == "" { |
| 40 | + return nil, fmt.Errorf("key encryption key URI or keyset path unset") |
| 41 | + } |
| 42 | + kek, err := getKeyEncryptionKey(ctx, kekURI) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + f, err := os.Open(filepath.Clean(keysetPath)) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + defer f.Close() |
| 52 | + |
| 53 | + kh, err := keyset.Read(keyset.NewJSONReader(f), kek) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + signer, _, err := tinkUtils.KeyHandleToSigner(kh) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + // validate that key is ECDSA P-256 |
| 63 | + pub, ok := signer.Public().(*ecdsa.PublicKey) |
| 64 | + if !ok { |
| 65 | + return nil, fmt.Errorf("key must be ECDSA") |
| 66 | + } |
| 67 | + if pub.Curve != elliptic.P256() { |
| 68 | + return nil, fmt.Errorf("elliptic curve must be P-256, was %s", pub.Curve.Params().Name) |
| 69 | + } |
| 70 | + |
| 71 | + return signer, err |
| 72 | +} |
| 73 | + |
| 74 | +// getKeyEncryptionKey returns a Tink AEAD encryption key from KMS |
| 75 | +func getKeyEncryptionKey(ctx context.Context, kmsKey string) (tink.AEAD, error) { |
| 76 | + switch { |
| 77 | + case strings.HasPrefix(kmsKey, "gcp-kms://"): |
| 78 | + gcpClient, err := gcpkms.NewClientWithOptions(ctx, kmsKey) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + registry.RegisterKMSClient(gcpClient) |
| 83 | + return gcpClient.GetAEAD(kmsKey) |
| 84 | + default: |
| 85 | + return nil, fmt.Errorf("unsupported KMS key type for key %s", kmsKey) |
| 86 | + } |
| 87 | +} |
0 commit comments