authority.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package authority
  2. import (
  3. "crypto/sha256"
  4. realx509 "crypto/x509"
  5. "encoding/hex"
  6. "fmt"
  7. "net"
  8. "sync"
  9. "time"
  10. "github.com/pkg/errors"
  11. "github.com/smallstep/cli/crypto/pemutil"
  12. "github.com/smallstep/cli/crypto/x509util"
  13. )
  14. const legacyAuthority = "step-certificate-authority"
  15. // Authority implements the Certificate Authority internal interface.
  16. type Authority struct {
  17. config *Config
  18. rootX509Crt *realx509.Certificate
  19. intermediateIdentity *x509util.Identity
  20. validateOnce bool
  21. certificates *sync.Map
  22. ottMap *sync.Map
  23. startTime time.Time
  24. provisionerIDIndex *sync.Map
  25. encryptedKeyIndex *sync.Map
  26. provisionerKeySetIndex *sync.Map
  27. sortedProvisioners provisionerSlice
  28. audiences []string
  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
  47. _, port, err := net.SplitHostPort(config.Address)
  48. if err != nil {
  49. return nil, errors.Wrapf(err, "error parsing %s", config.Address)
  50. }
  51. audiences := []string{legacyAuthority}
  52. for _, name := range config.DNSNames {
  53. if port == "443" {
  54. audiences = append(audiences, fmt.Sprintf("https://%s/sign", name), fmt.Sprintf("https://%s/1.0/sign", name))
  55. }
  56. audiences = append(audiences, fmt.Sprintf("https://%s:%s/sign", name, port), fmt.Sprintf("https://%s:%s/1.0/sign", name, port))
  57. }
  58. var a = &Authority{
  59. config: config,
  60. certificates: new(sync.Map),
  61. ottMap: new(sync.Map),
  62. provisionerIDIndex: new(sync.Map),
  63. encryptedKeyIndex: new(sync.Map),
  64. provisionerKeySetIndex: new(sync.Map),
  65. sortedProvisioners: sorted,
  66. audiences: audiences,
  67. }
  68. if err := a.init(); err != nil {
  69. return nil, err
  70. }
  71. return a, nil
  72. }
  73. // init performs validation and initializes the fields of an Authority struct.
  74. func (a *Authority) init() error {
  75. // Check if handler has already been validated/initialized.
  76. if a.initOnce {
  77. return nil
  78. }
  79. var err error
  80. // First load the root using our modified pem/x509 package.
  81. a.rootX509Crt, err = pemutil.ReadCertificate(a.config.Root)
  82. if err != nil {
  83. return err
  84. }
  85. // Add root certificate to the certificate map
  86. sum := sha256.Sum256(a.rootX509Crt.Raw)
  87. a.certificates.Store(hex.EncodeToString(sum[:]), a.rootX509Crt)
  88. // Decrypt and load intermediate public / private key pair.
  89. if len(a.config.Password) > 0 {
  90. a.intermediateIdentity, err = x509util.LoadIdentityFromDisk(
  91. a.config.IntermediateCert,
  92. a.config.IntermediateKey,
  93. pemutil.WithPassword([]byte(a.config.Password)),
  94. )
  95. if err != nil {
  96. return err
  97. }
  98. } else {
  99. a.intermediateIdentity, err = x509util.LoadIdentityFromDisk(a.config.IntermediateCert, a.config.IntermediateKey)
  100. if err != nil {
  101. return err
  102. }
  103. }
  104. for _, p := range a.config.AuthorityConfig.Provisioners {
  105. a.provisionerIDIndex.Store(p.ID(), p)
  106. if len(p.EncryptedKey) != 0 {
  107. a.encryptedKeyIndex.Store(p.Key.KeyID, p.EncryptedKey)
  108. }
  109. }
  110. a.startTime = time.Now()
  111. // Set flag indicating that initialization has been completed, and should
  112. // not be repeated.
  113. a.initOnce = true
  114. return nil
  115. }