certificate.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. var err error
  27. cert.ID, err = randID()
  28. if err != nil {
  29. return err
  30. }
  31. leaf := pem.EncodeToMemory(&pem.Block{
  32. Type: "CERTIFICATE",
  33. Bytes: cert.Leaf.Raw,
  34. })
  35. var intermediates []byte
  36. for _, cert := range cert.Intermediates {
  37. intermediates = append(intermediates, pem.EncodeToMemory(&pem.Block{
  38. Type: "CERTIFICATE",
  39. Bytes: cert.Raw,
  40. })...)
  41. }
  42. dbch := &dbCert{
  43. ID: cert.ID,
  44. AccountID: cert.AccountID,
  45. OrderID: cert.OrderID,
  46. Leaf: leaf,
  47. Intermediates: intermediates,
  48. CreatedAt: time.Now().UTC(),
  49. }
  50. err = db.save(ctx, cert.ID, dbch, nil, "certificate", certTable)
  51. if err != nil {
  52. return err
  53. }
  54. serial := cert.Leaf.SerialNumber.String()
  55. dbSerial := &dbSerial{
  56. Serial: serial,
  57. CertificateID: cert.ID,
  58. }
  59. return db.save(ctx, serial, dbSerial, nil, "serial", certBySerialTable)
  60. }
  61. // GetCertificate retrieves and unmarshals an ACME certificate type from the
  62. // datastore.
  63. func (db *DB) GetCertificate(_ context.Context, id string) (*acme.Certificate, error) {
  64. b, err := db.db.Get(certTable, []byte(id))
  65. if nosql.IsErrNotFound(err) {
  66. return nil, acme.NewError(acme.ErrorMalformedType, "certificate %s not found", id)
  67. } else if err != nil {
  68. return nil, errors.Wrapf(err, "error loading certificate %s", id)
  69. }
  70. dbC := new(dbCert)
  71. if err := json.Unmarshal(b, dbC); err != nil {
  72. return nil, errors.Wrapf(err, "error unmarshaling certificate %s", id)
  73. }
  74. certs, err := parseBundle(append(dbC.Leaf, dbC.Intermediates...))
  75. if err != nil {
  76. return nil, errors.Wrapf(err, "error parsing certificate chain for ACME certificate with ID %s", id)
  77. }
  78. return &acme.Certificate{
  79. ID: dbC.ID,
  80. AccountID: dbC.AccountID,
  81. OrderID: dbC.OrderID,
  82. Leaf: certs[0],
  83. Intermediates: certs[1:],
  84. }, nil
  85. }
  86. // GetCertificateBySerial retrieves and unmarshals an ACME certificate type from the
  87. // datastore based on a certificate serial number.
  88. func (db *DB) GetCertificateBySerial(ctx context.Context, serial string) (*acme.Certificate, error) {
  89. b, err := db.db.Get(certBySerialTable, []byte(serial))
  90. if nosql.IsErrNotFound(err) {
  91. return nil, acme.NewError(acme.ErrorMalformedType, "certificate with serial %s not found", serial)
  92. } else if err != nil {
  93. return nil, errors.Wrapf(err, "error loading certificate ID for serial %s", serial)
  94. }
  95. dbSerial := new(dbSerial)
  96. if err := json.Unmarshal(b, dbSerial); err != nil {
  97. return nil, errors.Wrapf(err, "error unmarshaling certificate with serial %s", serial)
  98. }
  99. return db.GetCertificate(ctx, dbSerial.CertificateID)
  100. }
  101. func parseBundle(b []byte) ([]*x509.Certificate, error) {
  102. var (
  103. err error
  104. block *pem.Block
  105. bundle []*x509.Certificate
  106. )
  107. for len(b) > 0 {
  108. block, b = pem.Decode(b)
  109. if block == nil {
  110. break
  111. }
  112. if block.Type != "CERTIFICATE" {
  113. return nil, errors.New("error decoding PEM: data contains block that is not a certificate")
  114. }
  115. var crt *x509.Certificate
  116. crt, err = x509.ParseCertificate(block.Bytes)
  117. if err != nil {
  118. return nil, errors.Wrapf(err, "error parsing x509 certificate")
  119. }
  120. bundle = append(bundle, crt)
  121. }
  122. if len(b) > 0 {
  123. return nil, errors.New("error decoding PEM: unexpected data")
  124. }
  125. return bundle, nil
  126. }