authority.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package authority
  2. import (
  3. "crypto/sha256"
  4. "crypto/x509"
  5. "encoding/hex"
  6. "sync"
  7. "time"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "github.com/smallstep/certificates/db"
  10. "github.com/smallstep/cli/crypto/pemutil"
  11. "github.com/smallstep/cli/crypto/x509util"
  12. )
  13. const legacyAuthority = "step-certificate-authority"
  14. // Authority implements the Certificate Authority internal interface.
  15. type Authority struct {
  16. config *Config
  17. rootX509Certs []*x509.Certificate
  18. intermediateIdentity *x509util.Identity
  19. validateOnce bool
  20. certificates *sync.Map
  21. ottMap *sync.Map
  22. startTime time.Time
  23. provisioners *provisioner.Collection
  24. db db.AuthDB
  25. // Do not re-initialize
  26. initOnce bool
  27. }
  28. // New creates and initiates a new Authority type.
  29. func New(config *Config) (*Authority, error) {
  30. err := config.Validate()
  31. if err != nil {
  32. return nil, err
  33. }
  34. var a = &Authority{
  35. config: config,
  36. certificates: new(sync.Map),
  37. ottMap: new(sync.Map),
  38. provisioners: provisioner.NewCollection(config.getAudiences()),
  39. }
  40. if err := a.init(); err != nil {
  41. return nil, err
  42. }
  43. return a, nil
  44. }
  45. // init performs validation and initializes the fields of an Authority struct.
  46. func (a *Authority) init() error {
  47. // Check if handler has already been validated/initialized.
  48. if a.initOnce {
  49. return nil
  50. }
  51. var err error
  52. // Initialize step-ca Database if defined in configuration.
  53. // If a.config.DB is nil then a noopDB will be returned.
  54. if a.db, err = db.New(a.config.DB); err != nil {
  55. return err
  56. }
  57. // Load the root certificates and add them to the certificate store
  58. a.rootX509Certs = make([]*x509.Certificate, len(a.config.Root))
  59. for i, path := range a.config.Root {
  60. crt, err := pemutil.ReadCertificate(path)
  61. if err != nil {
  62. return err
  63. }
  64. // Add root certificate to the certificate map
  65. sum := sha256.Sum256(crt.Raw)
  66. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  67. a.rootX509Certs[i] = crt
  68. }
  69. // Add federated roots
  70. for _, path := range a.config.FederatedRoots {
  71. crt, err := pemutil.ReadCertificate(path)
  72. if err != nil {
  73. return err
  74. }
  75. sum := sha256.Sum256(crt.Raw)
  76. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  77. }
  78. // Decrypt and load intermediate public / private key pair.
  79. if len(a.config.Password) > 0 {
  80. a.intermediateIdentity, err = x509util.LoadIdentityFromDisk(
  81. a.config.IntermediateCert,
  82. a.config.IntermediateKey,
  83. pemutil.WithPassword([]byte(a.config.Password)),
  84. )
  85. if err != nil {
  86. return err
  87. }
  88. } else {
  89. a.intermediateIdentity, err = x509util.LoadIdentityFromDisk(a.config.IntermediateCert, a.config.IntermediateKey)
  90. if err != nil {
  91. return err
  92. }
  93. }
  94. // Store all the provisioners
  95. for _, p := range a.config.AuthorityConfig.Provisioners {
  96. if err := a.provisioners.Store(p); err != nil {
  97. return err
  98. }
  99. }
  100. // JWT numeric dates are seconds.
  101. a.startTime = time.Now().Truncate(time.Second)
  102. // Set flag indicating that initialization has been completed, and should
  103. // not be repeated.
  104. a.initOnce = true
  105. return nil
  106. }
  107. // Shutdown safely shuts down any clients, databases, etc. held by the Authority.
  108. func (a *Authority) Shutdown() error {
  109. return a.db.Shutdown()
  110. }