certificate.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package nosql
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "encoding/json"
  6. "encoding/pem"
  7. "time"
  8. "github.com/pkg/errors"
  9. "github.com/smallstep/certificates/acme"
  10. "github.com/smallstep/nosql"
  11. )
  12. type dbCert struct {
  13. ID string `json:"id"`
  14. CreatedAt time.Time `json:"createdAt"`
  15. AccountID string `json:"accountID"`
  16. OrderID string `json:"orderID"`
  17. Leaf []byte `json:"leaf"`
  18. Intermediates []byte `json:"intermediates"`
  19. }
  20. type dbSerial struct {
  21. Serial string `json:"serial"`
  22. CertificateID string `json:"certificateID"`
  23. }
  24. // CreateCertificate creates and stores an ACME certificate type.
  25. func (db *DB) CreateCertificate(ctx context.Context, cert *acme.Certificate) error {
  26. cert.ID = randID()
  27. leaf := pem.EncodeToMemory(&pem.Block{
  28. Type: "CERTIFICATE",
  29. Bytes: cert.Leaf.Raw,
  30. })
  31. var intermediates []byte
  32. for _, cert := range cert.Intermediates {
  33. intermediates = append(intermediates, pem.EncodeToMemory(&pem.Block{
  34. Type: "CERTIFICATE",
  35. Bytes: cert.Raw,
  36. })...)
  37. }
  38. dbch := &dbCert{
  39. ID: cert.ID,
  40. AccountID: cert.AccountID,
  41. OrderID: cert.OrderID,
  42. Leaf: leaf,
  43. Intermediates: intermediates,
  44. CreatedAt: time.Now().UTC(),
  45. }
  46. if err := db.save(ctx, cert.ID, dbch, nil, "certificate", certTable); err != nil {
  47. return err
  48. }
  49. serial := cert.Leaf.SerialNumber.String()
  50. dbSerial := &dbSerial{
  51. Serial: serial,
  52. CertificateID: cert.ID,
  53. }
  54. return db.save(ctx, serial, dbSerial, nil, "serial", certBySerialTable)
  55. }
  56. // GetCertificate retrieves and unmarshals an ACME certificate type from the
  57. // datastore.
  58. func (db *DB) GetCertificate(_ context.Context, id string) (*acme.Certificate, error) {
  59. b, err := db.db.Get(certTable, []byte(id))
  60. if nosql.IsErrNotFound(err) {
  61. return nil, acme.NewError(acme.ErrorMalformedType, "certificate %s not found", id)
  62. } else if err != nil {
  63. return nil, errors.Wrapf(err, "error loading certificate %s", id)
  64. }
  65. dbC := new(dbCert)
  66. if err := json.Unmarshal(b, dbC); err != nil {
  67. return nil, errors.Wrapf(err, "error unmarshaling certificate %s", id)
  68. }
  69. certs, err := parseBundle(append(dbC.Leaf, dbC.Intermediates...))
  70. if err != nil {
  71. return nil, errors.Wrapf(err, "error parsing certificate chain for ACME certificate with ID %s", id)
  72. }
  73. return &acme.Certificate{
  74. ID: dbC.ID,
  75. AccountID: dbC.AccountID,
  76. OrderID: dbC.OrderID,
  77. Leaf: certs[0],
  78. Intermediates: certs[1:],
  79. }, nil
  80. }
  81. // GetCertificateBySerial retrieves and unmarshals an ACME certificate type from the
  82. // datastore based on a certificate serial number.
  83. func (db *DB) GetCertificateBySerial(ctx context.Context, serial string) (*acme.Certificate, error) {
  84. b, err := db.db.Get(certBySerialTable, []byte(serial))
  85. if nosql.IsErrNotFound(err) {
  86. return nil, acme.NewError(acme.ErrorMalformedType, "certificate with serial %s not found", serial)
  87. } else if err != nil {
  88. return nil, errors.Wrapf(err, "error loading certificate ID for serial %s", serial)
  89. }
  90. dbSerial := new(dbSerial)
  91. if err := json.Unmarshal(b, dbSerial); err != nil {
  92. return nil, errors.Wrapf(err, "error unmarshaling certificate with serial %s", serial)
  93. }
  94. return db.GetCertificate(ctx, dbSerial.CertificateID)
  95. }
  96. func parseBundle(b []byte) ([]*x509.Certificate, error) {
  97. var (
  98. err error
  99. block *pem.Block
  100. bundle []*x509.Certificate
  101. )
  102. for len(b) > 0 {
  103. block, b = pem.Decode(b)
  104. if block == nil {
  105. break
  106. }
  107. if block.Type != "CERTIFICATE" {
  108. return nil, errors.New("error decoding PEM: data contains block that is not a certificate")
  109. }
  110. var crt *x509.Certificate
  111. crt, err = x509.ParseCertificate(block.Bytes)
  112. if err != nil {
  113. return nil, errors.Wrapf(err, "error parsing x509 certificate")
  114. }
  115. bundle = append(bundle, crt)
  116. }
  117. if len(b) > 0 {
  118. return nil, errors.New("error decoding PEM: unexpected data")
  119. }
  120. return bundle, nil
  121. }