authority.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/cli/crypto/pemutil"
  10. "github.com/smallstep/cli/crypto/x509util"
  11. )
  12. const legacyAuthority = "step-certificate-authority"
  13. // Authority implements the Certificate Authority internal interface.
  14. type Authority struct {
  15. config *Config
  16. rootX509Certs []*x509.Certificate
  17. intermediateIdentity *x509util.Identity
  18. validateOnce bool
  19. certificates *sync.Map
  20. ottMap *sync.Map
  21. startTime time.Time
  22. provisioners *provisioner.Collection
  23. // Do not re-initialize
  24. initOnce bool
  25. }
  26. // New creates and initiates a new Authority type.
  27. func New(config *Config) (*Authority, error) {
  28. err := config.Validate()
  29. if err != nil {
  30. return nil, err
  31. }
  32. var a = &Authority{
  33. config: config,
  34. certificates: new(sync.Map),
  35. ottMap: new(sync.Map),
  36. provisioners: provisioner.NewCollection(config.getAudiences()),
  37. }
  38. if err := a.init(); err != nil {
  39. return nil, err
  40. }
  41. return a, nil
  42. }
  43. // init performs validation and initializes the fields of an Authority struct.
  44. func (a *Authority) init() error {
  45. // Check if handler has already been validated/initialized.
  46. if a.initOnce {
  47. return nil
  48. }
  49. var err error
  50. // Load the root certificates and add them to the certificate store
  51. a.rootX509Certs = make([]*x509.Certificate, len(a.config.Root))
  52. for i, path := range a.config.Root {
  53. crt, err := pemutil.ReadCertificate(path)
  54. if err != nil {
  55. return err
  56. }
  57. // Add root certificate to the certificate map
  58. sum := sha256.Sum256(crt.Raw)
  59. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  60. a.rootX509Certs[i] = crt
  61. }
  62. // Add federated roots
  63. for _, path := range a.config.FederatedRoots {
  64. crt, err := pemutil.ReadCertificate(path)
  65. if err != nil {
  66. return err
  67. }
  68. sum := sha256.Sum256(crt.Raw)
  69. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  70. }
  71. // Decrypt and load intermediate public / private key pair.
  72. if len(a.config.Password) > 0 {
  73. a.intermediateIdentity, err = x509util.LoadIdentityFromDisk(
  74. a.config.IntermediateCert,
  75. a.config.IntermediateKey,
  76. pemutil.WithPassword([]byte(a.config.Password)),
  77. )
  78. if err != nil {
  79. return err
  80. }
  81. } else {
  82. a.intermediateIdentity, err = x509util.LoadIdentityFromDisk(a.config.IntermediateCert, a.config.IntermediateKey)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. // Store all the provisioners
  88. for _, p := range a.config.AuthorityConfig.Provisioners {
  89. if err := a.provisioners.Store(p); err != nil {
  90. return err
  91. }
  92. }
  93. a.startTime = time.Now()
  94. // Set flag indicating that initialization has been completed, and should
  95. // not be repeated.
  96. a.initOnce = true
  97. return nil
  98. }