certinfo.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Package certinfo implements the certinfo command
  2. package certinfo
  3. import (
  4. "crypto/x509"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/cloudflare/cfssl/certdb/dbconf"
  9. "github.com/cloudflare/cfssl/certdb/sql"
  10. "github.com/cloudflare/cfssl/certinfo"
  11. "github.com/cloudflare/cfssl/cli"
  12. "github.com/jmoiron/sqlx"
  13. )
  14. // Usage text of 'cfssl certinfo'
  15. var dataUsageText = `cfssl certinfo -- output certinfo about the given cert
  16. Usage of certinfo:
  17. - Data from local certificate files
  18. cfssl certinfo -cert file
  19. - Data from local CSR file
  20. cfssl certinfo -csr file
  21. - Data from certificate from remote server.
  22. cfssl certinfo -domain domain_name
  23. - Data from CA storage
  24. cfssl certinfo -sn serial (requires -db-config and -aki)
  25. Flags:
  26. `
  27. // flags used by 'cfssl certinfo'
  28. var certinfoFlags = []string{"aki", "cert", "csr", "db-config", "domain", "serial"}
  29. // certinfoMain is the main CLI of certinfo functionality
  30. func certinfoMain(args []string, c cli.Config) (err error) {
  31. var cert *certinfo.Certificate
  32. var csr *x509.CertificateRequest
  33. if c.CertFile != "" {
  34. if c.CertFile == "-" {
  35. var certPEM []byte
  36. if certPEM, err = cli.ReadStdin(c.CertFile); err != nil {
  37. return
  38. }
  39. if cert, err = certinfo.ParseCertificatePEM(certPEM); err != nil {
  40. return
  41. }
  42. } else {
  43. if cert, err = certinfo.ParseCertificateFile(c.CertFile); err != nil {
  44. return
  45. }
  46. }
  47. } else if c.CSRFile != "" {
  48. if c.CSRFile == "-" {
  49. var csrPEM []byte
  50. if csrPEM, err = cli.ReadStdin(c.CSRFile); err != nil {
  51. return
  52. }
  53. if csr, err = certinfo.ParseCSRPEM(csrPEM); err != nil {
  54. return
  55. }
  56. } else {
  57. if csr, err = certinfo.ParseCSRFile(c.CSRFile); err != nil {
  58. return
  59. }
  60. }
  61. } else if c.Domain != "" {
  62. if cert, err = certinfo.ParseCertificateDomain(c.Domain); err != nil {
  63. return
  64. }
  65. } else if c.Serial != "" && c.AKI != "" {
  66. if c.DBConfigFile == "" {
  67. return errors.New("need DB config file (provide with -db-config)")
  68. }
  69. var db *sqlx.DB
  70. db, err = dbconf.DBFromConfig(c.DBConfigFile)
  71. if err != nil {
  72. return
  73. }
  74. dbAccessor := sql.NewAccessor(db)
  75. if cert, err = certinfo.ParseSerialNumber(c.Serial, c.AKI, dbAccessor); err != nil {
  76. return
  77. }
  78. } else {
  79. return errors.New("Must specify certinfo target through -cert, -csr, -domain or -serial + -aki")
  80. }
  81. var b []byte
  82. if cert != nil {
  83. b, err = json.MarshalIndent(cert, "", " ")
  84. } else if csr != nil {
  85. b, err = json.MarshalIndent(csr, "", " ")
  86. }
  87. if err != nil {
  88. return
  89. }
  90. fmt.Println(string(b))
  91. return
  92. }
  93. // Command assembles the definition of Command 'certinfo'
  94. var Command = &cli.Command{UsageText: dataUsageText, Flags: certinfoFlags, Main: certinfoMain}