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

Add --ca-kms flag to step certificate create #942

Merged
merged 19 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Detect OIDC tokens issued by Kubernetes (smallstep/cli#953).
- Add support for Smallstep Managed Endpoint X509 extension
(smallstep/cli#989).
- Support signing a certificate for a private key that can only be used for encryption with the `--skip-csr-signature` flag in `step certificate create`. Some KMSs restrict key usage to a single type of cryptographic operation. This blocks RSA decryption keys from being used to sign a CSR for their public key. Using the `--skip-csr-signature` flag, the public key is used directly with a certificate template, removing the need for the CSR signature.

### Changed

- Increase PBKDF2 iterations to 600k (smallstep/cli#949).
- `--kms` flag is no longer used for the CA (signing) key for `step certificate create`. It was replaced by the `--ca-kms` flag (smallstep/cli#942).

### Fixed

Expand Down
89 changes: 68 additions & 21 deletions command/certificate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
[**--kms**=<uri>] [**--csr**] [**--profile**=<profile>]
[**--template**=<file>] [**--set**=<key=value>] [**--set-file**=<file>]
[**--not-before**=<duration>] [**--not-after**=<duration>]
[**--password-file**=<file>] [**--ca**=<issuer-cert>]
[**--password-file**=<file>] [**--ca**=<issuer-cert>]
[**--ca-key**=<issuer-key>] [**--ca-password-file**=<file>]
[**--san**=<SAN>] [**--bundle**] [**--key**=<file>]
[**--ca-kms**=<uri>] [**--san**=<SAN>] [**--bundle**] [**--key**=<file>]
[**--kty**=<type>] [**--curve**=<curve>] [**--size**=<size>]
[**--no-password**] [**--insecure**]`,
[**--skip-csr-signature**] [**--no-password**] [**--insecure**]`,
Description: `**step certificate create** generates a certificate or a
certificate signing request (CSR) that can be signed later using 'step
certificate sign' (or some other tool) to produce a certificate.
Expand Down Expand Up @@ -345,11 +345,34 @@
'pkcs11:id=4001;object=intermediate-key'
$ step certificate create \
--profile intermediate-ca \
--kms 'pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=smallstep?pin-value=password' \
--ca-kms 'pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=smallstep?pin-value=password'
--ca root_ca.crt --ca-key 'pkcs11:id=4000' \
--kms 'pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=smallstep?pin-value=password' \
--key 'pkcs11:id=4001' \
'My KMS Intermediate' intermediate_ca.crt
'''

Create an intermediate certificate for an RSA decryption key in Google Cloud KMS, signed by a root stored on disk, using <step-kms-plugin>:
'''
$ step certificate create \
--profile intermediate-ca \
--ca root_ca.crt --ca-key root_ca_key \
--kms cloudkms: \
--key 'projects/myProjectID/locations/global/keyRings/myKeyRing/cryptoKeys/myKey/cryptoKeyVersions/1' \
--skip-csr-signature \
'My RSA Intermediate' intermediate_rsa_ca.crt
'''

Create an intermediate certificate for an RSA signing key in Google Cloud KMS, signed by a root stored in an HSM, using <step-kms-plugin>:
'''
$ step certificate create \
--profile intermediate-ca \
--ca-kms 'pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=smallstep?pin-value=password' \
--ca root_ca.crt --ca-key 'pkcs11:id=4000' \
--kms cloudkms: \
--key 'projects/myProjectID/locations/global/keyRings/myKeyRing/cryptoKeys/myKey/cryptoKeyVersions/1' \
'My RSA Intermediate' intermediate_rsa_ca.crt
'''
`,
Flags: []cli.Flag{
flags.KMSUri,
Expand Down Expand Up @@ -446,6 +469,14 @@
Name: "insecure",
Hidden: true,
},
cli.StringFlag{
Name: "ca-kms",
Usage: "The <uri> to configure the KMS used for signing the certificate",
},
cli.BoolFlag{
Name: "skip-csr-signature",
Usage: "Skip creating and signing a CSR",
},
},
}
}
Expand Down Expand Up @@ -485,17 +516,22 @@
}

var (
sans = ctx.StringSlice("san")
profile = ctx.String("profile")
templateFile = ctx.String("template")
bundle = ctx.Bool("bundle")
subtle = ctx.Bool("subtle")
sans = ctx.StringSlice("san")
profile = ctx.String("profile")
templateFile = ctx.String("template")
bundle = ctx.Bool("bundle")
subtle = ctx.Bool("subtle")
skipCSRSignature = ctx.Bool("skip-csr-signature")

Check warning on line 524 in command/certificate/create.go

View check run for this annotation

Codecov / codecov/patch

command/certificate/create.go#L519-L524

Added lines #L519 - L524 were not covered by tests
)

if ctx.IsSet("profile") && templateFile != "" {
return errs.IncompatibleFlagWithFlag(ctx, "profile", "template")
}

if ctx.Bool("csr") && skipCSRSignature {
return errs.IncompatibleFlagWithFlag(ctx, "csr", "skip-csr-signature")
}

Check warning on line 533 in command/certificate/create.go

View check run for this annotation

Codecov / codecov/patch

command/certificate/create.go#L531-L533

Added lines #L531 - L533 were not covered by tests

// Read template if passed
var template string
if templateFile != "" {
Expand Down Expand Up @@ -631,20 +667,31 @@
defaultValidity = defaultTemplatevalidity
}

// Create X.509 certificate used as base for the certificate
cr, err := x509util.CreateCertificateRequest(subject, sans, signer)
if err != nil {
return err
}

// Create X.509 certificate
templateData := x509util.CreateTemplateData(subject, sans)
templateData.SetUserData(userData)
certificate, err := x509util.NewCertificate(cr, x509util.WithTemplate(template, templateData))
if err != nil {
return err

var certTemplate = &x509.Certificate{}
if skipCSRSignature {
certTemplate.PublicKey = pub
certificate, err := x509util.NewCertificateFromX509(certTemplate, x509util.WithTemplate(template, templateData))
if err != nil {
return err
}
certTemplate = certificate.GetCertificate()
} else {
// Create X.509 certificate used as base for the certificate
cr, err := x509util.CreateCertificateRequest(subject, sans, priv)
if err != nil {
return err
}
certificate, err := x509util.NewCertificate(cr, x509util.WithTemplate(template, templateData))
if err != nil {
return err
}
certTemplate = certificate.GetCertificate()

Check warning on line 692 in command/certificate/create.go

View check run for this annotation

Codecov / codecov/patch

command/certificate/create.go#L673-L692

Added lines #L673 - L692 were not covered by tests
}
certTemplate := certificate.GetCertificate()

if parent == nil {
parent = certTemplate
}
Expand Down Expand Up @@ -766,9 +813,9 @@
var (
caCert = ctx.String("ca")
caKey = ctx.String("ca-key")
caKMS = ctx.String("ca-kms")

Check warning on line 816 in command/certificate/create.go

View check run for this annotation

Codecov / codecov/patch

command/certificate/create.go#L816

Added line #L816 was not covered by tests
profile = ctx.String("profile")
template = ctx.String("template")
kms = ctx.String("kms")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes the command incompatible with at least one example in the help:

# Create an intermediate certificate using step-kms-plugin:

$ step kms create \
  --kms 'pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=smallstep?pin-value=password' \
  'pkcs11:id=4001;object=intermediate-key'
$ step certificate create \
  --profile intermediate-ca \
  --kms 'pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=smallstep?pin-value=password' \
  --ca root_ca.crt --ca-key 'pkcs11:id=4000' \
  --key 'pkcs11:id=4001' \
  'My KMS Intermediate' intermediate_ca.crt

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 290f81d

)

// Check required flags when profile is used.
Expand Down Expand Up @@ -819,7 +866,7 @@
opts = append(opts, pemutil.WithPasswordFile(passFile))
}

signer, err := cryptoutil.CreateSigner(kms, caKey, opts...)
signer, err := cryptoutil.CreateSigner(caKMS, caKey, opts...)

Check warning on line 869 in command/certificate/create.go

View check run for this annotation

Codecov / codecov/patch

command/certificate/create.go#L869

Added line #L869 was not covered by tests
if err != nil {
return nil, nil, err
}
Expand Down
12 changes: 10 additions & 2 deletions internal/cryptoutil/cryptoutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// CreateSigner reads a key from a file with a given name or creates a signer
// with the given kms and name uri.
func CreateSigner(kms, name string, opts ...pemutil.Options) (crypto.Signer, error) {
if kms == "" {
if kms == "" || isSoftKMS(kms) {

Check warning on line 32 in internal/cryptoutil/cryptoutil.go

View check run for this annotation

Codecov / codecov/patch

internal/cryptoutil/cryptoutil.go#L32

Added line #L32 was not covered by tests
s, err := pemutil.Read(name, opts...)
if err != nil {
return nil, err
Expand All @@ -43,6 +43,10 @@
return newKMSSigner(kms, name)
}

func isSoftKMS(kms string) bool {
return strings.HasPrefix(strings.ToLower(strings.TrimSpace(kms)), "softkms")

Check warning on line 47 in internal/cryptoutil/cryptoutil.go

View check run for this annotation

Codecov / codecov/patch

internal/cryptoutil/cryptoutil.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

// LoadCertificate returns a x509.Certificate from a kms or file
func LoadCertificate(kms, certPath string) ([]*x509.Certificate, error) {
if kms == "" {
Expand Down Expand Up @@ -138,9 +142,13 @@
}

// IsX509Signer returns true if the given signer is supported by Go's
// crypto/x509 package to sign sign X509 certificates. This methods returns true
// crypto/x509 package to sign X509 certificates. This methods returns true
// for ECDSA, RSA and Ed25519 keys, but if the kms is `sshagentkms:` it will
// only return true for Ed25519 keys.
// TODO(hs): introspect the KMS key to verify that it can actually be
// used for signing? E.g. for Google Cloud KMS RSA keys can be used for
// signing or decryption, but only one of those at a time. Trying to use
// a signing key to decrypt data will result in an error from Cloud KMS.
func IsX509Signer(signer crypto.Signer) bool {
pub := signer.Public()
if ks, ok := signer.(*kmsSigner); ok {
Expand Down