-
Notifications
You must be signed in to change notification settings - Fork 6
Support GCP Secret Manager for signer key pair #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
roger2hk
merged 11 commits into
transparency-dev:main
from
roger2hk:gcp-signer-secret-manager
Nov 8, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3af540e
Support GCP Secret Manager for signer
roger2hk 19c5877
Add terraform dependency lock file
roger2hk 3208d07
Add security warning to tls_private_key.sctfe-ecdsa-p256 resource
roger2hk 9f83e96
Rename `Signer` to `ECDSAWithSHA256Signer`
roger2hk 5ba49ca
Wrap err with key secret name
roger2hk 59fc624
Refactor `pem.Decode`
roger2hk 6576b98
Fix readme after rebase
roger2hk 7abb62b
Fix TF resources naming convention
roger2hk dd0f245
Fix `panic` when `opts` is `nil`
roger2hk 84cf5c2
Verify the correctness of the signer key pair
roger2hk f882294
Refactor `ECDSAWithSHA256Signer` to use `ecdsa.{Public,Private}Key`
roger2hk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright 2024 Google LLC. All Rights Reserved. | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"context" | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
"hash/crc32" | ||
"io" | ||
|
||
secretmanager "cloud.google.com/go/secretmanager/apiv1" | ||
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb" | ||
) | ||
|
||
// ECDSAWithSHA256Signer implements crypto.Signer using Google Cloud Secret Manager. | ||
// Only crypto.SHA256 and ECDSA are supported. | ||
type ECDSAWithSHA256Signer struct { | ||
publicKey *ecdsa.PublicKey | ||
privateKey *ecdsa.PrivateKey | ||
} | ||
|
||
// Public returns the public key stored in the Signer object. | ||
func (s *ECDSAWithSHA256Signer) Public() crypto.PublicKey { | ||
return s.publicKey | ||
} | ||
|
||
// Sign signs digest with the private key stored in Google Cloud Secret Manager. | ||
func (s *ECDSAWithSHA256Signer) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { | ||
// Verify hash function and digest bytes length. | ||
if opts == nil { | ||
return nil, errors.New("opts cannot be nil") | ||
} | ||
if opts.HashFunc() != crypto.SHA256 { | ||
return nil, fmt.Errorf("unsupported hash func: %v", opts.HashFunc()) | ||
} | ||
if len(digest) != opts.HashFunc().Size() { | ||
return nil, fmt.Errorf("digest bytes length %d does not match hash function bytes length %d", len(digest), opts.HashFunc().Size()) | ||
} | ||
|
||
return ecdsa.SignASN1(rand, s.privateKey, digest) | ||
} | ||
|
||
// NewSecretManagerSigner creates a new signer that uses the ECDSA P-256 key pair in | ||
// Google Cloud Secret Manager for signing digests. | ||
func NewSecretManagerSigner(ctx context.Context, publicKeySecretName, privateKeySecretName string) (*ECDSAWithSHA256Signer, error) { | ||
client, err := secretmanager.NewClient(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create secret manager client: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
// Public Key | ||
var publicKey crypto.PublicKey | ||
pemBlock, err := secretPEM(ctx, client, publicKeySecretName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get public key secret PEM (%s): %w", publicKeySecretName, err) | ||
} | ||
switch pemBlock.Type { | ||
case "PUBLIC KEY": | ||
publicKey, err = x509.ParsePKIXPublicKey(pemBlock.Bytes) | ||
default: | ||
return nil, fmt.Errorf("unsupported PEM type: %s", pemBlock.Type) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
var ecdsaPublicKey *ecdsa.PublicKey | ||
ecdsaPublicKey, ok := publicKey.(*ecdsa.PublicKey) | ||
if !ok { | ||
return nil, fmt.Errorf("the public key stored in Secret Manager is not an ECDSA key") | ||
} | ||
|
||
// Private Key | ||
var ecdsaPrivateKey *ecdsa.PrivateKey | ||
pemBlock, err = secretPEM(ctx, client, privateKeySecretName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get private key secret PEM (%s): %w", privateKeySecretName, err) | ||
} | ||
switch pemBlock.Type { | ||
case "EC PRIVATE KEY": | ||
ecdsaPrivateKey, err = x509.ParseECPrivateKey(pemBlock.Bytes) | ||
default: | ||
return nil, fmt.Errorf("unsupported PEM type: %s", pemBlock.Type) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Verify the correctness of the signer key pair | ||
if !ecdsaPrivateKey.PublicKey.Equal(ecdsaPublicKey) { | ||
return nil, errors.New("signer key pair doesn't match") | ||
} | ||
|
||
return &ECDSAWithSHA256Signer{ | ||
publicKey: ecdsaPublicKey, | ||
privateKey: ecdsaPrivateKey, | ||
}, nil | ||
} | ||
|
||
func secretPEM(ctx context.Context, client *secretmanager.Client, secretName string) (*pem.Block, error) { | ||
resp, err := client.AccessSecretVersion(ctx, &secretmanagerpb.AccessSecretVersionRequest{ | ||
Name: secretName, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to access secret version: %w", err) | ||
} | ||
if resp.Name != secretName { | ||
return nil, errors.New("request corrupted in-transit") | ||
} | ||
// Verify the data checksum. | ||
crc32c := crc32.MakeTable(crc32.Castagnoli) | ||
checksum := int64(crc32.Checksum(resp.Payload.Data, crc32c)) | ||
if checksum != *resp.Payload.DataCrc32C { | ||
return nil, errors.New("Data corruption detected.") | ||
} | ||
|
||
pemBlock, rest := pem.Decode([]byte(resp.Payload.Data)) | ||
if pemBlock == nil { | ||
return nil, errors.New("failed to decode PEM") | ||
} | ||
if len(rest) > 0 { | ||
return nil, fmt.Errorf("extra data after decoding PEM: %v", rest) | ||
} | ||
|
||
return pemBlock, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
phbnf marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.