4
0

authority.go 3.8 KB

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