authority.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package authority
  2. import (
  3. "crypto/sha256"
  4. "crypto/x509"
  5. "encoding/hex"
  6. "fmt"
  7. "sync"
  8. "time"
  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. provisionerIDIndex *sync.Map
  23. encryptedKeyIndex *sync.Map
  24. provisionerKeySetIndex *sync.Map
  25. sortedProvisioners provisionerSlice
  26. audiences []string
  27. // Do not re-initialize
  28. initOnce bool
  29. }
  30. // New creates and initiates a new Authority type.
  31. func New(config *Config) (*Authority, error) {
  32. err := config.Validate()
  33. if err != nil {
  34. return nil, err
  35. }
  36. // Get sorted provisioners
  37. var sorted provisionerSlice
  38. if config.AuthorityConfig != nil {
  39. sorted, err = newSortedProvisioners(config.AuthorityConfig.Provisioners)
  40. if err != nil {
  41. return nil, err
  42. }
  43. }
  44. // Define audiences: legacy + possible urls without the ports.
  45. // The CA might have proxies in front so we cannot rely on the port.
  46. audiences := []string{legacyAuthority}
  47. for _, name := range config.DNSNames {
  48. audiences = append(audiences, fmt.Sprintf("https://%s/sign", name), fmt.Sprintf("https://%s/1.0/sign", name))
  49. }
  50. var a = &Authority{
  51. config: config,
  52. certificates: new(sync.Map),
  53. ottMap: new(sync.Map),
  54. provisionerIDIndex: new(sync.Map),
  55. encryptedKeyIndex: new(sync.Map),
  56. provisionerKeySetIndex: new(sync.Map),
  57. sortedProvisioners: sorted,
  58. audiences: audiences,
  59. }
  60. if err := a.init(); err != nil {
  61. return nil, err
  62. }
  63. return a, nil
  64. }
  65. // init performs validation and initializes the fields of an Authority struct.
  66. func (a *Authority) init() error {
  67. // Check if handler has already been validated/initialized.
  68. if a.initOnce {
  69. return nil
  70. }
  71. var err error
  72. // Load the root certificates and add them to the certificate store
  73. a.rootX509Certs = make([]*x509.Certificate, len(a.config.Root))
  74. for i, path := range a.config.Root {
  75. crt, err := pemutil.ReadCertificate(path)
  76. if err != nil {
  77. return err
  78. }
  79. // Add root certificate to the certificate map
  80. sum := sha256.Sum256(crt.Raw)
  81. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  82. a.rootX509Certs[i] = crt
  83. }
  84. // Add federated roots
  85. for _, path := range a.config.FederatedRoots {
  86. crt, err := pemutil.ReadCertificate(path)
  87. if err != nil {
  88. return err
  89. }
  90. sum := sha256.Sum256(crt.Raw)
  91. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  92. }
  93. // Decrypt and load intermediate public / private key pair.
  94. if len(a.config.Password) > 0 {
  95. a.intermediateIdentity, err = x509util.LoadIdentityFromDisk(
  96. a.config.IntermediateCert,
  97. a.config.IntermediateKey,
  98. pemutil.WithPassword([]byte(a.config.Password)),
  99. )
  100. if err != nil {
  101. return err
  102. }
  103. } else {
  104. a.intermediateIdentity, err = x509util.LoadIdentityFromDisk(a.config.IntermediateCert, a.config.IntermediateKey)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. for _, p := range a.config.AuthorityConfig.Provisioners {
  110. a.provisionerIDIndex.Store(p.ID(), p)
  111. if len(p.EncryptedKey) != 0 {
  112. a.encryptedKeyIndex.Store(p.Key.KeyID, p.EncryptedKey)
  113. }
  114. }
  115. a.startTime = time.Now()
  116. // Set flag indicating that initialization has been completed, and should
  117. // not be repeated.
  118. a.initOnce = true
  119. return nil
  120. }