crl.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Package crl implements the crl command
  2. package crl
  3. import (
  4. "os"
  5. "github.com/cloudflare/cfssl/certdb/dbconf"
  6. certsql "github.com/cloudflare/cfssl/certdb/sql"
  7. "github.com/cloudflare/cfssl/cli"
  8. "github.com/cloudflare/cfssl/crl"
  9. cferr "github.com/cloudflare/cfssl/errors"
  10. "github.com/cloudflare/cfssl/helpers"
  11. "github.com/cloudflare/cfssl/log"
  12. "github.com/jmoiron/sqlx"
  13. )
  14. var crlUsageText = `cfssl crl -- generate a new Certificate Revocation List from Database
  15. Usage of crl:
  16. cfssl crl
  17. Flags:
  18. `
  19. var crlFlags = []string{"db-config", "ca", "ca-key", "expiry"}
  20. func generateCRL(c cli.Config) (crlBytes []byte, err error) {
  21. if c.CAFile == "" {
  22. log.Error("need CA certificate (provide one with -ca)")
  23. return
  24. }
  25. if c.CAKeyFile == "" {
  26. log.Error("need CA key (provide one with -ca-key)")
  27. return
  28. }
  29. var db *sqlx.DB
  30. if c.DBConfigFile != "" {
  31. db, err = dbconf.DBFromConfig(c.DBConfigFile)
  32. if err != nil {
  33. return nil, err
  34. }
  35. } else {
  36. log.Error("no Database specified!")
  37. return nil, err
  38. }
  39. dbAccessor := certsql.NewAccessor(db)
  40. log.Debug("loading CA: ", c.CAFile)
  41. ca, err := helpers.ReadBytes(c.CAFile)
  42. if err != nil {
  43. return nil, err
  44. }
  45. log.Debug("loading CA key: ", c.CAKeyFile)
  46. cakey, err := helpers.ReadBytes(c.CAKeyFile)
  47. if err != nil {
  48. return nil, cferr.Wrap(cferr.CertificateError, cferr.ReadFailed, err)
  49. }
  50. // Parse the PEM encoded certificate
  51. issuerCert, err := helpers.ParseCertificatePEM(ca)
  52. if err != nil {
  53. return nil, err
  54. }
  55. strPassword := os.Getenv("CFSSL_CA_PK_PASSWORD")
  56. password := []byte(strPassword)
  57. if strPassword == "" {
  58. password = nil
  59. }
  60. // Parse the key given
  61. key, err := helpers.ParsePrivateKeyPEMWithPassword(cakey, password)
  62. if err != nil {
  63. log.Debugf("malformed private key %v", err)
  64. return nil, err
  65. }
  66. certs, err := dbAccessor.GetRevokedAndUnexpiredCertificates()
  67. if err != nil {
  68. return nil, err
  69. }
  70. req, err := crl.NewCRLFromDB(certs, issuerCert, key, c.CRLExpiration)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return req, nil
  75. }
  76. func crlMain(args []string, c cli.Config) (err error) {
  77. req, err := generateCRL(c)
  78. if err != nil {
  79. return err
  80. }
  81. cli.PrintCRL(req)
  82. return
  83. }
  84. // Command assembles the definition of Command 'crl'
  85. var Command = &cli.Command{UsageText: crlUsageText, Flags: crlFlags, Main: crlMain}