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

Use go-secure-stdlib's RSA key generator backed by a DRBG #29020

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3cc333b
Use DRBG based RSA key generation everywhere
sgmiller Oct 21, 2024
eaa1bae
switch to the conditional generator
sgmiller Oct 25, 2024
1d632fc
Use DRBG based RSA key generation everywhere
sgmiller Oct 21, 2024
6150e1b
switch to the conditional generator
sgmiller Oct 25, 2024
a0a63a0
Add an ENV var to disable the DRBG in a pinch
sgmiller Nov 14, 2024
5515c96
Merge branch 'sgm/prng-rsa' of github.com:/hashicorp/vault into sgm/p…
sgmiller Nov 15, 2024
e0700b3
update go.mod
sgmiller Nov 15, 2024
b756246
Use DRBG based RSA key generation everywhere
sgmiller Oct 21, 2024
4c2f9d4
switch to the conditional generator
sgmiller Oct 25, 2024
e6baaa7
Add an ENV var to disable the DRBG in a pinch
sgmiller Nov 14, 2024
5a55c89
Use DRBG based RSA key generation everywhere
sgmiller Oct 21, 2024
bd8f565
update go.mod
sgmiller Nov 15, 2024
afb7027
fix import
sgmiller Nov 25, 2024
2fd9552
Remove rsa2 alias, remove test code
sgmiller Nov 25, 2024
d05ec3c
Merge branch 'sgm/prng-rsa' of github.com:/hashicorp/vault into sgm/p…
sgmiller Nov 25, 2024
19f23bb
move cryptoutil/rsa.go to sdk
sgmiller Nov 25, 2024
1df8a42
move imports too
sgmiller Nov 25, 2024
6fff9ec
remove makefile change
sgmiller Nov 25, 2024
b56cd5d
rsa2->rsa
sgmiller Nov 25, 2024
a68bb6e
more rsa2->rsa, remove test code
sgmiller Nov 25, 2024
3c65e56
fix some overzelous search/replace
sgmiller Nov 25, 2024
b7e2af6
Update to a real tag
sgmiller Nov 25, 2024
8890ef3
changelog
sgmiller Nov 26, 2024
95aeceb
copyright
sgmiller Nov 26, 2024
c9608d7
Merge remote-tracking branch 'origin/main' into sgm/prng-rsa
sgmiller Nov 26, 2024
28e484a
work around copyright check
sgmiller Nov 26, 2024
c0dfee7
work around copyright check pt2
sgmiller Nov 26, 2024
8ba7ea6
bunch of dupe imports
sgmiller Nov 26, 2024
255c1bf
missing import
sgmiller Nov 26, 2024
54e26b5
wrong license
sgmiller Nov 26, 2024
b523448
fix go.mod conflict
sgmiller Nov 27, 2024
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
6 changes: 3 additions & 3 deletions builtin/credential/cert/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
Expand Down Expand Up @@ -39,6 +38,7 @@ import (
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/certutil"
"github.com/hashicorp/vault/sdk/helper/cryptoutil"
"github.com/hashicorp/vault/sdk/helper/tokenutil"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/vault"
Expand Down Expand Up @@ -658,7 +658,7 @@ func TestBackend_NonCAExpiry(t *testing.T) {
template.IPAddresses = []net.IP{parsedIP}

// Private key for CA cert
caPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
caPrivateKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -726,7 +726,7 @@ func TestBackend_NonCAExpiry(t *testing.T) {
template.SerialNumber = big.NewInt(5678)

template.KeyUsage = x509.KeyUsage(x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign)
issuedPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
issuedPrivateKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions builtin/logical/database/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package database
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
Expand All @@ -15,6 +14,8 @@ import (
"strings"
"time"

"github.com/hashicorp/vault/sdk/helper/cryptoutil"

"github.com/hashicorp/vault/helper/random"
"github.com/hashicorp/vault/sdk/database/dbplugin/v5"
"github.com/hashicorp/vault/sdk/helper/certutil"
Expand Down Expand Up @@ -133,7 +134,7 @@ func (kg *rsaKeyGenerator) generate(r io.Reader) ([]byte, []byte, error) {
return nil, nil, fmt.Errorf("invalid key_bits: %v", kg.KeyBits)
}

key, err := rsa.GenerateKey(reader, keyBits)
key, err := cryptoutil.GenerateRSAKey(reader, keyBits)
if err != nil {
return nil, nil, err
}
Expand Down
17 changes: 9 additions & 8 deletions builtin/logical/pki/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/hashicorp/vault/helper/testhelpers/teststorage"
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/sdk/helper/certutil"
"github.com/hashicorp/vault/sdk/helper/cryptoutil"
"github.com/hashicorp/vault/sdk/helper/testhelpers/schema"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/vault"
Expand Down Expand Up @@ -510,14 +511,14 @@ func generateURLSteps(t *testing.T, caCert, caKey string, intdata, reqdata map[s
},
}

priv1024, _ := rsa.GenerateKey(rand.Reader, 1024)
priv1024, _ := cryptoutil.GenerateRSAKey(rand.Reader, 1024)
csr1024, _ := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, priv1024)
csrPem1024 := strings.TrimSpace(string(pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csr1024,
})))

priv2048, _ := rsa.GenerateKey(rand.Reader, 2048)
priv2048, _ := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
csr2048, _ := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, priv2048)
csrPem2048 := strings.TrimSpace(string(pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE REQUEST",
Expand Down Expand Up @@ -699,7 +700,7 @@ func generateCSR(t *testing.T, csrTemplate *x509.CertificateRequest, keyType str
var err error
switch keyType {
case "rsa":
priv, err = rsa.GenerateKey(rand.Reader, keyBits)
priv, err = cryptoutil.GenerateRSAKey(rand.Reader, keyBits)
case "ec":
switch keyBits {
case 224:
Expand Down Expand Up @@ -1180,7 +1181,7 @@ func generateRoleSteps(t *testing.T, useCSRs bool) []logicaltest.TestStep {
case "rsa":
privKey, ok = generatedRSAKeys[keyBits]
if !ok {
privKey, _ = rsa.GenerateKey(rand.Reader, keyBits)
privKey, _ = cryptoutil.GenerateRSAKey(rand.Reader, keyBits)
generatedRSAKeys[keyBits] = privKey
}

Expand Down Expand Up @@ -2164,7 +2165,7 @@ func runTestSignVerbatim(t *testing.T, keyType string) {
}

// create a CSR and key
key, err := rsa.GenerateKey(rand.Reader, 2048)
key, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -2735,7 +2736,7 @@ func TestBackend_SignSelfIssued(t *testing.T) {
t.Fatal(err)
}

key, err := rsa.GenerateKey(rand.Reader, 2048)
key, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -2879,7 +2880,7 @@ func TestBackend_SignSelfIssued_DifferentTypes(t *testing.T) {
t.Fatal(err)
}

key, err := rsa.GenerateKey(rand.Reader, 2048)
key, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -3834,7 +3835,7 @@ func setCerts() {
}
ecCACert = strings.TrimSpace(string(pem.EncodeToMemory(caCertPEMBlock)))

rak, err := rsa.GenerateKey(rand.Reader, 2048)
rak, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions builtin/logical/pki/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/hashicorp/vault/api"
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/sdk/helper/certutil"
"github.com/hashicorp/vault/sdk/helper/cryptoutil"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/vault"
)
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestBackend_CA_Steps(t *testing.T) {
}
ecCACert = strings.TrimSpace(string(pem.EncodeToMemory(caCertPEMBlock)))

rak, err := rsa.GenerateKey(rand.Reader, 2048)
rak, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
Expand Down
5 changes: 3 additions & 2 deletions builtin/logical/pki/ca_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"testing"

"github.com/hashicorp/vault/sdk/helper/cryptoutil"

"github.com/hashicorp/vault/sdk/helper/certutil"
)

func TestGetKeyTypeAndBitsFromPublicKeyForRole(t *testing.T) {
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
rsaKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("error generating rsa key: %s", err)
}
Expand Down
21 changes: 11 additions & 10 deletions builtin/logical/pki/path_acme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/hashicorp/vault/helper/testhelpers"
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/sdk/helper/certutil"
"github.com/hashicorp/vault/sdk/helper/cryptoutil"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/vault"
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestAcmeBasicWorkflow(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
baseAcmeURL := "/v1/pki/" + tc.prefixUrl
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

acmeClient := getAcmeClientForCluster(t, cluster, baseAcmeURL, accountKey)
Expand Down Expand Up @@ -592,7 +593,7 @@ func TestAcmeAccountsCrossingDirectoryPath(t *testing.T) {
defer cluster.Cleanup()

baseAcmeURL := "/v1/pki/acme/"
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

testCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
Expand Down Expand Up @@ -628,7 +629,7 @@ func TestAcmeEabCrossingDirectoryPath(t *testing.T) {
require.NoError(t, err)

baseAcmeURL := "/v1/pki/acme/"
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

testCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
Expand Down Expand Up @@ -838,7 +839,7 @@ func TestAcmeTruncatesToIssuerExpiry(t *testing.T) {
require.NoError(t, err, "failed updating issuer name")

baseAcmeURL := "/v1/pki/issuer/short-ca/acme/"
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

acmeClient := getAcmeClientForCluster(t, cluster, baseAcmeURL, accountKey)
Expand Down Expand Up @@ -910,7 +911,7 @@ func TestAcmeRoleExtKeyUsage(t *testing.T) {
_, err := client.Logical().Write("pki/roles/"+roleName, roleOpt)

baseAcmeURL := "/v1/pki/roles/" + roleName + "/acme/"
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

require.NoError(t, err, "failed creating role test-role")
Expand Down Expand Up @@ -1179,7 +1180,7 @@ func TestAcmeWithCsrIncludingBasicConstraintExtension(t *testing.T) {
defer cancel()

baseAcmeURL := "/v1/pki/acme/"
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

acmeClient := getAcmeClientForCluster(t, cluster, baseAcmeURL, accountKey)
Expand Down Expand Up @@ -1511,7 +1512,7 @@ func TestAcmeValidationError(t *testing.T) {
defer cancel()

baseAcmeURL := "/v1/pki/acme/"
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

acmeClient := getAcmeClientForCluster(t, cluster, baseAcmeURL, accountKey)
Expand Down Expand Up @@ -1619,7 +1620,7 @@ func TestAcmeRevocationAcrossAccounts(t *testing.T) {
defer cancel()

baseAcmeURL := "/v1/pki/acme/"
accountKey1, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey1, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

acmeClient1 := getAcmeClientForCluster(t, cluster, baseAcmeURL, accountKey1)
Expand Down Expand Up @@ -1718,7 +1719,7 @@ func TestAcmeMaxTTL(t *testing.T) {
require.NoError(t, err, "error configuring acme")

// First Create Our Client
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")
acmeClient := getAcmeClientForCluster(t, cluster, "/v1/pki/acme/", accountKey)

Expand Down Expand Up @@ -1946,7 +1947,7 @@ func TestACMEClientRequestLimits(t *testing.T) {
for _, tc := range cases {

// First Create Our Client
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")
acmeClient := getAcmeClientForCluster(t, cluster, "/v1/pki/acme/", accountKey)

Expand Down
4 changes: 2 additions & 2 deletions builtin/logical/pki/path_config_acme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package pki
import (
"context"
"crypto/rand"
"crypto/rsa"
"testing"
"time"

"github.com/hashicorp/vault/helper/constants"
"github.com/hashicorp/vault/sdk/helper/cryptoutil"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -117,7 +117,7 @@ func TestAcmeConfig(t *testing.T) {
require.NoError(t, err)

baseAcmeURL := "/v1/pki/" + tc.prefixUrl
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

acmeClient := getAcmeClientForCluster(t, cluster, baseAcmeURL, accountKey)
Expand Down
6 changes: 3 additions & 3 deletions builtin/logical/pki/path_tidy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
Expand All @@ -23,6 +22,7 @@ import (
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/testhelpers"
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/sdk/helper/cryptoutil"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
"github.com/hashicorp/vault/sdk/helper/testhelpers/schema"
"github.com/hashicorp/vault/sdk/logical"
Expand Down Expand Up @@ -916,7 +916,7 @@ func TestTidyAcmeWithBackdate(t *testing.T) {

// Register an Account, do nothing with it
baseAcmeURL := "/v1/pki/acme/"
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

acmeClient := getAcmeClientForCluster(t, cluster, baseAcmeURL, accountKey)
Expand Down Expand Up @@ -1073,7 +1073,7 @@ func TestTidyAcmeWithSafetyBuffer(t *testing.T) {

// Register an Account, do nothing with it
baseAcmeURL := "/v1/pki/acme/"
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa key")

acmeClient := getAcmeClientForCluster(t, cluster, baseAcmeURL, accountKey)
Expand Down
7 changes: 4 additions & 3 deletions builtin/logical/pkiext/pkiext_binary/acme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
Expand All @@ -25,11 +24,13 @@ import (
"time"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-secure-stdlib/cryptoutil"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/builtin/logical/pkiext"
"github.com/hashicorp/vault/helper/testhelpers"
"github.com/hashicorp/vault/helper/testhelpers/corehelpers"
"github.com/hashicorp/vault/sdk/helper/certutil"
"github.com/hashicorp/vault/sdk/helper/cryptoutil"
hDocker "github.com/hashicorp/vault/sdk/helper/docker"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/acme"
Expand Down Expand Up @@ -704,7 +705,7 @@ func doAcmeValidationWithGoLibrary(t *testing.T, directoryUrl string, acmeOrderI
}
httpClient := &http.Client{Transport: tr}

accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa account key")
logger.Trace("Using the following url for the ACME directory", "url", directoryUrl)
acmeClient := &acme.Client{
Expand Down Expand Up @@ -957,7 +958,7 @@ func SubtestACMEStepDownNode(t *testing.T, cluster *VaultPkiCluster) {
DNSNames: []string{hostname, hostname},
}

accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
accountKey, err := cryptoutil.GenerateRSAKey(rand.Reader, 2048)
require.NoError(t, err, "failed creating rsa account key")

acmeClient := &acme.Client{
Expand Down
Loading