certificate.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // CreateCertificate creates and stores an ACME certificate type.
  21. func (db *DB) CreateCertificate(ctx context.Context, cert *acme.Certificate) error {
  22. var err error
  23. cert.ID, err = randID()
  24. if err != nil {
  25. return err
  26. }
  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. return db.save(ctx, cert.ID, dbch, nil, "certificate", certTable)
  47. }
  48. // GetCertificate retrieves and unmarshals an ACME certificate type from the
  49. // datastore.
  50. func (db *DB) GetCertificate(ctx context.Context, id string) (*acme.Certificate, error) {
  51. b, err := db.db.Get(certTable, []byte(id))
  52. if nosql.IsErrNotFound(err) {
  53. return nil, acme.NewError(acme.ErrorMalformedType, "certificate %s not found", id)
  54. } else if err != nil {
  55. return nil, errors.Wrapf(err, "error loading certificate %s", id)
  56. }
  57. dbC := new(dbCert)
  58. if err := json.Unmarshal(b, dbC); err != nil {
  59. return nil, errors.Wrapf(err, "error unmarshaling certificate %s", id)
  60. }
  61. certs, err := parseBundle(append(dbC.Leaf, dbC.Intermediates...))
  62. if err != nil {
  63. return nil, errors.Wrapf(err, "error parsing certificate chain for ACME certificate with ID %s", id)
  64. }
  65. return &acme.Certificate{
  66. ID: dbC.ID,
  67. AccountID: dbC.AccountID,
  68. OrderID: dbC.OrderID,
  69. Leaf: certs[0],
  70. Intermediates: certs[1:],
  71. }, nil
  72. }
  73. func parseBundle(b []byte) ([]*x509.Certificate, error) {
  74. var (
  75. err error
  76. block *pem.Block
  77. bundle []*x509.Certificate
  78. )
  79. for len(b) > 0 {
  80. block, b = pem.Decode(b)
  81. if block == nil {
  82. break
  83. }
  84. if block.Type != "CERTIFICATE" {
  85. return nil, errors.New("error decoding PEM: data contains block that is not a certificate")
  86. }
  87. var crt *x509.Certificate
  88. crt, err = x509.ParseCertificate(block.Bytes)
  89. if err != nil {
  90. return nil, errors.Wrapf(err, "error parsing x509 certificate")
  91. }
  92. bundle = append(bundle, crt)
  93. }
  94. if len(b) > 0 {
  95. return nil, errors.New("error decoding PEM: unexpected data")
  96. }
  97. return bundle, nil
  98. }