Skip to content
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

Improved keyvaultsigner #8

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions internal/azure/keyvaultsigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import (
"context"
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"math/big"
"time"

"github.com/Azure/azure-sdk-for-go/services/keyvault/v7.0/keyvault"
"github.com/pkg/errors"
)

// Timeout for all calls to Azure Key Vault
const KeyVaultRequestTimeout = 20 * time.Second

// KeyVaultSigner an Azure Key Vault signer
type KeyVaultSigner struct {
crypto.Signer
Expand All @@ -39,7 +42,9 @@ func NewKeyVaultSigner(client *keyvault.BaseClient, keyVaultName string, key str
// Public returns the PublicKey from an Azure Key Vault Key
func (s *KeyVaultSigner) Public() crypto.PublicKey {
// Get the key from Azure Key Vault
keyBundle, err := s.client.GetKey(context.Background(), s.url, s.key, "")
ctx, cancel := context.WithTimeout(context.Background(), KeyVaultRequestTimeout)
keyBundle, err := s.client.GetKey(ctx, s.url, s.key, "")
cancel()
if err != nil {
return nil
}
Expand Down Expand Up @@ -78,15 +83,7 @@ func (s *KeyVaultSigner) Public() crypto.PublicKey {
}

// Create a new PublicKey using our computed values
key := rsa.PublicKey{N: n, E: int(e)}

// Convert into the rigth format
publicKey, err := x509.ParsePKCS1PublicKey(x509.MarshalPKCS1PublicKey(&key))
if err != nil {
return nil
}

return publicKey
return &rsa.PublicKey{N: n, E: int(e)}
}

// Sign a digest with the private key in Azure Key Vault
Expand All @@ -95,8 +92,9 @@ func (s *KeyVaultSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerO
encodedDigest := base64.RawURLEncoding.EncodeToString(digest)

// Attempt to sign using the KeyVault client
ctx, cancel := context.WithTimeout(context.Background(), KeyVaultRequestTimeout)
response, err := s.client.Sign(
context.Background(),
ctx,
s.url,
s.key,
"",
Expand All @@ -105,6 +103,10 @@ func (s *KeyVaultSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerO
Value: &encodedDigest,
},
)
cancel()
if err != nil {
return nil, err
}

// Parse the result, decoding from base64
signature, err := base64.RawURLEncoding.DecodeString(*response.Result)
Expand Down