gencert.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Package gencert implements the gencert command.
  2. package gencert
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "github.com/cloudflare/cfssl/api/generator"
  7. "github.com/cloudflare/cfssl/cli"
  8. "github.com/cloudflare/cfssl/cli/genkey"
  9. "github.com/cloudflare/cfssl/cli/sign"
  10. "github.com/cloudflare/cfssl/csr"
  11. "github.com/cloudflare/cfssl/initca"
  12. "github.com/cloudflare/cfssl/log"
  13. "github.com/cloudflare/cfssl/signer"
  14. )
  15. var gencertUsageText = `cfssl gencert -- generate a new key and signed certificate
  16. Usage of gencert:
  17. Generate a new key and cert from CSR:
  18. cfssl gencert -initca CSRJSON
  19. cfssl gencert -ca cert -ca-key key [-config config] [-profile profile] [-hostname hostname] CSRJSON
  20. cfssl gencert -remote remote_host [-config config] [-profile profile] [-label label] [-hostname hostname] CSRJSON
  21. Re-generate a CA cert with the CA key and CSR:
  22. cfssl gencert -initca -ca-key key CSRJSON
  23. Re-generate a CA cert with the CA key and certificate:
  24. cfssl gencert -renewca -ca cert -ca-key key
  25. Arguments:
  26. CSRJSON: JSON file containing the request, use '-' for reading JSON from stdin
  27. Flags:
  28. `
  29. var gencertFlags = []string{"initca", "remote", "ca", "ca-key", "config", "cn", "hostname", "profile", "label"}
  30. func gencertMain(args []string, c cli.Config) error {
  31. if c.RenewCA {
  32. log.Infof("re-generate a CA certificate from CA cert and key")
  33. cert, err := initca.RenewFromPEM(c.CAFile, c.CAKeyFile)
  34. if err != nil {
  35. log.Errorf("%v\n", err)
  36. return err
  37. }
  38. cli.PrintCert(nil, nil, cert)
  39. return nil
  40. }
  41. csrJSONFile, args, err := cli.PopFirstArgument(args)
  42. if err != nil {
  43. return err
  44. }
  45. if len(args) > 0 {
  46. return errors.New("only one argument is accepted, please check with usage")
  47. }
  48. csrJSONFileBytes, err := cli.ReadStdin(csrJSONFile)
  49. if err != nil {
  50. return err
  51. }
  52. req := csr.CertificateRequest{
  53. KeyRequest: csr.NewKeyRequest(),
  54. }
  55. err = json.Unmarshal(csrJSONFileBytes, &req)
  56. if err != nil {
  57. return err
  58. }
  59. if c.CNOverride != "" {
  60. req.CN = c.CNOverride
  61. }
  62. switch {
  63. case c.IsCA:
  64. var key, csrPEM, cert []byte
  65. if c.CAKeyFile != "" {
  66. log.Infof("re-generate a CA certificate from CSR and CA key")
  67. cert, csrPEM, err = initca.NewFromPEM(&req, c.CAKeyFile)
  68. if err != nil {
  69. log.Errorf("%v\n", err)
  70. return err
  71. }
  72. } else {
  73. log.Infof("generating a new CA key and certificate from CSR")
  74. cert, csrPEM, key, err = initca.New(&req)
  75. if err != nil {
  76. return err
  77. }
  78. }
  79. cli.PrintCert(key, csrPEM, cert)
  80. default:
  81. if req.CA != nil {
  82. err = errors.New("ca section only permitted in initca")
  83. return err
  84. }
  85. if c.Hostname != "" {
  86. req.Hosts = signer.SplitHosts(c.Hostname)
  87. }
  88. // Remote can be forced on the command line or in the config
  89. if c.Remote == "" && c.CFG == nil {
  90. if c.CAFile == "" {
  91. log.Error("need a CA certificate (provide one with -ca)")
  92. return nil
  93. }
  94. if c.CAKeyFile == "" {
  95. log.Error("need a CA key (provide one with -ca-key)")
  96. return nil
  97. }
  98. }
  99. var key, csrBytes []byte
  100. g := &csr.Generator{Validator: genkey.Validator}
  101. csrBytes, key, err = g.ProcessRequest(&req)
  102. if err != nil {
  103. key = nil
  104. return err
  105. }
  106. s, err := sign.SignerFromConfig(c)
  107. if err != nil {
  108. return err
  109. }
  110. var cert []byte
  111. signReq := signer.SignRequest{
  112. Request: string(csrBytes),
  113. Hosts: signer.SplitHosts(c.Hostname),
  114. Profile: c.Profile,
  115. Label: c.Label,
  116. }
  117. if c.CRL != "" {
  118. signReq.CRLOverride = c.CRL
  119. }
  120. cert, err = s.Sign(signReq)
  121. if err != nil {
  122. return err
  123. }
  124. // This follows the Baseline Requirements for the Issuance and
  125. // Management of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser
  126. // Forum (https://cabforum.org). Specifically, section 10.2.3 ("Information
  127. // Requirements"), states:
  128. //
  129. // "Applicant information MUST include, but not be limited to, at least one
  130. // Fully-Qualified Domain Name or IP address to be included in the Certificate’s
  131. // SubjectAltName extension."
  132. if len(signReq.Hosts) == 0 && len(req.Hosts) == 0 {
  133. log.Warning(generator.CSRNoHostMessage)
  134. }
  135. cli.PrintCert(key, csrBytes, cert)
  136. }
  137. return nil
  138. }
  139. // Command assembles the definition of Command 'gencert'
  140. var Command = &cli.Command{UsageText: gencertUsageText, Flags: gencertFlags, Main: gencertMain}