Skip to content

Commit

Permalink
Use the system rand reader for CA root and intermediate generation (#…
Browse files Browse the repository at this point in the history
…12559)

* Use the system rand reader for CA root and intermediate generation

* changelog
  • Loading branch information
sgmiller committed Sep 15, 2021
1 parent b86d300 commit 8478615
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
10 changes: 6 additions & 4 deletions builtin/logical/pki/cert_util.go
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/base64"
"encoding/pem"
"fmt"
"io"
"net"
"net/url"
"regexp"
Expand Down Expand Up @@ -449,7 +450,8 @@ func generateCert(ctx context.Context,
b *backend,
input *inputBundle,
caSign *certutil.CAInfoBundle,
isCA bool) (*certutil.ParsedCertBundle, error) {
isCA bool,
randomSource io.Reader) (*certutil.ParsedCertBundle, error) {

if input.role == nil {
return nil, errutil.InternalError{Err: "no role found in data bundle"}
Expand Down Expand Up @@ -494,7 +496,7 @@ func generateCert(ctx context.Context,
}
}

parsedBundle, err := certutil.CreateCertificate(data)
parsedBundle, err := certutil.CreateCertificateWithRandomSource(data, randomSource)
if err != nil {
return nil, err
}
Expand All @@ -504,7 +506,7 @@ func generateCert(ctx context.Context,

// N.B.: This is only meant to be used for generating intermediate CAs.
// It skips some sanity checks.
func generateIntermediateCSR(b *backend, input *inputBundle) (*certutil.ParsedCSRBundle, error) {
func generateIntermediateCSR(b *backend, input *inputBundle, randomSource io.Reader) (*certutil.ParsedCSRBundle, error) {
creation, err := generateCreationBundle(b, input, nil, nil)
if err != nil {
return nil, err
Expand All @@ -514,7 +516,7 @@ func generateIntermediateCSR(b *backend, input *inputBundle) (*certutil.ParsedCS
}

addBasicConstraints := input.apiData != nil && input.apiData.Get("add_basic_constraints").(bool)
parsedBundle, err := certutil.CreateCSR(creation, addBasicConstraints)
parsedBundle, err := certutil.CreateCSRWithRandomSource(creation, addBasicConstraints, randomSource)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/logical/pki/path_intermediate.go
Expand Up @@ -75,7 +75,7 @@ func (b *backend) pathGenerateIntermediate(ctx context.Context, req *logical.Req
req: req,
apiData: data,
}
parsedBundle, err := generateIntermediateCSR(b, input)
parsedBundle, err := generateIntermediateCSR(b, input, b.Backend.GetRandomReader())
if err != nil {
switch err.(type) {
case errutil.UserError:
Expand Down
3 changes: 2 additions & 1 deletion builtin/logical/pki/path_issue_sign.go
Expand Up @@ -2,6 +2,7 @@ package pki

import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"time"
Expand Down Expand Up @@ -219,7 +220,7 @@ func (b *backend) pathIssueSignCert(ctx context.Context, req *logical.Request, d
if useCSR {
parsedBundle, err = signCert(b, input, signingBundle, false, useCSRValues)
} else {
parsedBundle, err = generateCert(ctx, b, input, signingBundle, false)
parsedBundle, err = generateCert(ctx, b, input, signingBundle, false, rand.Reader)
}
if err != nil {
switch err.(type) {
Expand Down
2 changes: 1 addition & 1 deletion builtin/logical/pki/path_root.go
Expand Up @@ -155,7 +155,7 @@ func (b *backend) pathCAGenerateRoot(ctx context.Context, req *logical.Request,
apiData: data,
role: role,
}
parsedBundle, err := generateCert(ctx, b, input, nil, true)
parsedBundle, err := generateCert(ctx, b, input, nil, true, b.Backend.GetRandomReader())
if err != nil {
switch err.(type) {
case errutil.UserError:
Expand Down
3 changes: 3 additions & 0 deletions changelog/12559.txt
@@ -0,0 +1,3 @@
```release-note:improvement
secrets/pki: Use entropy augmentation when available when generating root and intermediate CA key material.
```

0 comments on commit 8478615

Please sign in to comment.