certdb.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package certdb
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "time"
  6. "github.com/jmoiron/sqlx/types"
  7. )
  8. // CertificateRecord encodes a certificate and its metadata
  9. // that will be recorded in a database.
  10. type CertificateRecord struct {
  11. Serial string `db:"serial_number"`
  12. AKI string `db:"authority_key_identifier"`
  13. CALabel string `db:"ca_label"`
  14. Status string `db:"status"`
  15. Reason int `db:"reason"`
  16. Expiry time.Time `db:"expiry"`
  17. RevokedAt time.Time `db:"revoked_at"`
  18. PEM string `db:"pem"`
  19. // the following fields will be empty for data inserted before migrate 002 has been run.
  20. IssuedAt *time.Time `db:"issued_at"`
  21. NotBefore *time.Time `db:"not_before"`
  22. MetadataJSON types.JSONText `db:"metadata"`
  23. SANsJSON types.JSONText `db:"sans"`
  24. CommonName sql.NullString `db:"common_name"`
  25. }
  26. // SetMetadata sets the metadata json
  27. func (c *CertificateRecord) SetMetadata(meta map[string]interface{}) error {
  28. marshaled, err := json.Marshal(meta)
  29. if err != nil {
  30. return err
  31. }
  32. c.MetadataJSON = types.JSONText(marshaled)
  33. return nil
  34. }
  35. // GetMetadata returns the json metadata
  36. func (c *CertificateRecord) GetMetadata() (map[string]interface{}, error) {
  37. var meta map[string]interface{}
  38. err := c.MetadataJSON.Unmarshal(&meta)
  39. return meta, err
  40. }
  41. // SetSANs sets the list of sans
  42. func (c *CertificateRecord) SetSANs(meta []string) error {
  43. marshaled, err := json.Marshal(meta)
  44. if err != nil {
  45. return err
  46. }
  47. c.SANsJSON = types.JSONText(marshaled)
  48. return nil
  49. }
  50. // GetSANs returns the json SANs
  51. func (c *CertificateRecord) GetSANs() ([]string, error) {
  52. var sans []string
  53. err := c.SANsJSON.Unmarshal(&sans)
  54. return sans, err
  55. }
  56. // OCSPRecord encodes a OCSP response body and its metadata
  57. // that will be recorded in a database.
  58. type OCSPRecord struct {
  59. Serial string `db:"serial_number"`
  60. AKI string `db:"authority_key_identifier"`
  61. Body string `db:"body"`
  62. Expiry time.Time `db:"expiry"`
  63. }
  64. // Accessor abstracts the CRUD of certdb objects from a DB.
  65. type Accessor interface {
  66. InsertCertificate(cr CertificateRecord) error
  67. GetCertificate(serial, aki string) ([]CertificateRecord, error)
  68. GetUnexpiredCertificates() ([]CertificateRecord, error)
  69. GetRevokedAndUnexpiredCertificates() ([]CertificateRecord, error)
  70. GetUnexpiredCertificatesByLabel(labels []string) (crs []CertificateRecord, err error)
  71. GetRevokedAndUnexpiredCertificatesByLabel(label string) ([]CertificateRecord, error)
  72. GetRevokedAndUnexpiredCertificatesByLabelSelectColumns(label string) ([]CertificateRecord, error)
  73. RevokeCertificate(serial, aki string, reasonCode int) error
  74. InsertOCSP(rr OCSPRecord) error
  75. GetOCSP(serial, aki string) ([]OCSPRecord, error)
  76. GetUnexpiredOCSPs() ([]OCSPRecord, error)
  77. UpdateOCSP(serial, aki, body string, expiry time.Time) error
  78. UpsertOCSP(serial, aki, body string, expiry time.Time) error
  79. }