package certificate import ( "bytes" "crypto" "crypto/ecdsa" "crypto/ed25519" "crypto/rsa" "crypto/x509" "encoding/pem" "fmt" "strconv" "time" "github.com/pkg/errors" "github.com/smallstep/cli/errs" "github.com/smallstep/cli/flags" "github.com/smallstep/cli/ui" "github.com/smallstep/cli/utils" "github.com/urfave/cli" "go.step.sm/crypto/pemutil" "go.step.sm/crypto/x509util" ) const customIntermediateTemplate = `{ "subject": {{ toJson .Subject }}, "keyUsage": ["certSign", "crlSign"], "basicConstraints": { "isCA": true, "maxPathLen": {{ .MaxPathLen }} } }` func signCommand() cli.Command { return cli.Command{ Name: "sign", Action: cli.ActionFunc(signAction), Usage: "sign a certificate signing request (CSR)", UsageText: `**step certificate sign** [**--profile**=] [**--template**=] [**--password-file**=] [**--path-len**=] [**--not-before**=] [**--not-after**=] [**--bundle**]`, Description: `**step certificate sign** generates a signed certificate from a certificate signing request (CSR). ## POSITIONAL ARGUMENTS : The path to a certificate signing request (CSR) to be signed. : The path to an issuing certificate. : The path to a private key for signing the CSR. ## EXIT CODES This command returns 0 on success and \>0 if any error occurs. ## EXAMPLES Sign a certificate signing request using the leaf profile: ''' $ step certificate sign leaf.csr issuer.crt issuer.key # or $ step certificate sign --profile leaf leaf.csr issuer.crt issuer.key ''' Sign a CSR and bundle the new certificate with the issuer: ''' $ step certificate sign --bundle leaf.csr issuer.crt issuer.key ''' Sign a CSR with custom validity and bundle the new certificate with the issuer: ''' $ step certificate sign --bundle --not-before -1m --not-after 16h leaf.csr issuer.crt issuer.key ''' Sign an intermediate ca: ''' $ step certificate sign --profile intermediate-ca intermediate.csr issuer.crt issuer.key ''' Sign an intermediate ca that can sign other intermediates; in this example, the issuer must set the pathLenConstraint at least to 2 or without a limit: ''' $ step certificate sign --profile intermediate-ca --path-len 1 intermediate.csr issuer.crt issuer.key ''' Sign a CSR but only use information present in it, it doesn't add any key or extended key usages if they are not in the CSR. ''' $ step certificate sign --profile csr test.csr issuer.crt issuer.key ''' Sign a CSR with only clientAuth as key usage using a template: ''' $ cat coyote.tpl { "subject": { "country": "US", "organization": "Coyote Corporation", "commonName": "{{ .Subject.CommonName }}" }, "emailAddresses": {{ toJson .Insecure.CR.EmailAddresses }}, "keyUsage": ["digitalSignature"], "extKeyUsage": ["clientAuth"] } $ step certificate create --csr coyote@acme.corp coyote.csr coyote.key $ step certificate sign --template coyote.tpl coyote.csr issuer.crt issuer.key '''`, Flags: []cli.Flag{ cli.StringFlag{ Name: "profile", Value: profileLeaf, Usage: `The certificate profile sets various certificate details such as certificate use and expiration. The default profile is 'leaf' which is suitable for a client or server using TLS. : is a case-sensitive string and must be one of: **leaf** : Signs a leaf x.509 certificate suitable for use with TLS. **intermediate-ca** : Signs a certificate that can be used to sign additional leaf certificates. **csr** : Signs a x.509 certificate without modifying the CSR.`, }, cli.StringFlag{ Name: "template", Usage: `The certificate template , a JSON representation of the certificate to create.`, }, flags.PasswordFile, cli.StringFlag{ Name: "not-before", Usage: `The set in the NotBefore property of the certificate. If a