config.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package authority
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "os"
  7. "time"
  8. "github.com/pkg/errors"
  9. "github.com/smallstep/certificates/authority/provisioner"
  10. "github.com/smallstep/certificates/ct"
  11. "github.com/smallstep/certificates/db"
  12. "github.com/smallstep/cli/crypto/tlsutil"
  13. "github.com/smallstep/cli/crypto/x509util"
  14. )
  15. var (
  16. // DefaultTLSOptions represents the default TLS version as well as the cipher
  17. // suites used in the TLS certificates.
  18. DefaultTLSOptions = tlsutil.TLSOptions{
  19. CipherSuites: x509util.CipherSuites{
  20. "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
  21. "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  22. "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  23. },
  24. MinVersion: 1.2,
  25. MaxVersion: 1.2,
  26. Renegotiation: false,
  27. }
  28. defaultDisableRenewal = false
  29. defaultEnableSSHCA = false
  30. globalProvisionerClaims = provisioner.Claims{
  31. MinTLSDur: &provisioner.Duration{Duration: 5 * time.Minute}, // TLS certs
  32. MaxTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
  33. DefaultTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
  34. DisableRenewal: &defaultDisableRenewal,
  35. MinUserSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // User SSH certs
  36. MaxUserSSHDur: &provisioner.Duration{Duration: 24 * time.Hour},
  37. DefaultUserSSHDur: &provisioner.Duration{Duration: 4 * time.Hour},
  38. MinHostSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // Host SSH certs
  39. MaxHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
  40. DefaultHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
  41. EnableSSHCA: &defaultEnableSSHCA,
  42. }
  43. )
  44. // Config represents the CA configuration and it's mapped to a JSON object.
  45. type Config struct {
  46. Root multiString `json:"root"`
  47. FederatedRoots []string `json:"federatedRoots"`
  48. IntermediateCert string `json:"crt"`
  49. IntermediateKey string `json:"key"`
  50. Address string `json:"address"`
  51. DNSNames []string `json:"dnsNames"`
  52. SSH *SSHConfig `json:"ssh,omitempty"`
  53. Logger json.RawMessage `json:"logger,omitempty"`
  54. DB *db.Config `json:"db,omitempty"`
  55. Monitoring json.RawMessage `json:"monitoring,omitempty"`
  56. AuthorityConfig *AuthConfig `json:"authority,omitempty"`
  57. TLS *tlsutil.TLSOptions `json:"tls,omitempty"`
  58. Password string `json:"password,omitempty"`
  59. CTs []ct.Config `json:"cts"`
  60. }
  61. // AuthConfig represents the configuration options for the authority.
  62. type AuthConfig struct {
  63. Provisioners provisioner.List `json:"provisioners"`
  64. Template *x509util.ASN1DN `json:"template,omitempty"`
  65. Claims *provisioner.Claims `json:"claims,omitempty"`
  66. DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
  67. }
  68. // Validate validates the authority configuration.
  69. func (c *AuthConfig) Validate(audiences provisioner.Audiences) error {
  70. if c == nil {
  71. return errors.New("authority cannot be undefined")
  72. }
  73. if len(c.Provisioners) == 0 {
  74. return errors.New("authority.provisioners cannot be empty")
  75. }
  76. // Merge global and configuration claims
  77. claimer, err := provisioner.NewClaimer(c.Claims, globalProvisionerClaims)
  78. if err != nil {
  79. return err
  80. }
  81. // Initialize provisioners
  82. config := provisioner.Config{
  83. Claims: claimer.Claims(),
  84. Audiences: audiences,
  85. }
  86. for _, p := range c.Provisioners {
  87. if err := p.Init(config); err != nil {
  88. return err
  89. }
  90. }
  91. if c.Template == nil {
  92. c.Template = &x509util.ASN1DN{}
  93. }
  94. return nil
  95. }
  96. // SSHConfig contains the user and host keys.
  97. type SSHConfig struct {
  98. HostKey string `json:"hostKey"`
  99. UserKey string `json:"userKey"`
  100. AddUserPrincipal string `json:"addUserPrincipal"`
  101. AddUserCommand string `json:"addUserCommand"`
  102. }
  103. // LoadConfiguration parses the given filename in JSON format and returns the
  104. // configuration struct.
  105. func LoadConfiguration(filename string) (*Config, error) {
  106. f, err := os.Open(filename)
  107. if err != nil {
  108. return nil, errors.Wrapf(err, "error opening %s", filename)
  109. }
  110. defer f.Close()
  111. var c Config
  112. if err := json.NewDecoder(f).Decode(&c); err != nil {
  113. return nil, errors.Wrapf(err, "error parsing %s", filename)
  114. }
  115. return &c, nil
  116. }
  117. // Save saves the configuration to the given filename.
  118. func (c *Config) Save(filename string) error {
  119. f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  120. if err != nil {
  121. return errors.Wrapf(err, "error opening %s", filename)
  122. }
  123. defer f.Close()
  124. enc := json.NewEncoder(f)
  125. enc.SetIndent("", "\t")
  126. return errors.Wrapf(enc.Encode(c), "error writing %s", filename)
  127. }
  128. // Validate validates the configuration.
  129. func (c *Config) Validate() error {
  130. switch {
  131. case c.Address == "":
  132. return errors.New("address cannot be empty")
  133. case c.Root.HasEmpties():
  134. return errors.New("root cannot be empty")
  135. case c.IntermediateCert == "":
  136. return errors.New("crt cannot be empty")
  137. case c.IntermediateKey == "":
  138. return errors.New("key cannot be empty")
  139. case len(c.DNSNames) == 0:
  140. return errors.New("dnsNames cannot be empty")
  141. }
  142. // Validate address (a port is required)
  143. if _, _, err := net.SplitHostPort(c.Address); err != nil {
  144. return errors.Errorf("invalid address %s", c.Address)
  145. }
  146. if c.TLS == nil {
  147. c.TLS = &DefaultTLSOptions
  148. } else {
  149. if len(c.TLS.CipherSuites) == 0 {
  150. c.TLS.CipherSuites = DefaultTLSOptions.CipherSuites
  151. }
  152. if c.TLS.MaxVersion == 0 {
  153. c.TLS.MaxVersion = DefaultTLSOptions.MaxVersion
  154. }
  155. if c.TLS.MinVersion == 0 {
  156. c.TLS.MinVersion = c.TLS.MaxVersion
  157. }
  158. if c.TLS.MinVersion > c.TLS.MaxVersion {
  159. return errors.New("tls minVersion cannot exceed tls maxVersion")
  160. }
  161. c.TLS.Renegotiation = c.TLS.Renegotiation || DefaultTLSOptions.Renegotiation
  162. }
  163. if len(c.CTs) > 0 {
  164. for _, ct := range c.CTs {
  165. if err := ct.Validate(); err != nil {
  166. return err
  167. }
  168. }
  169. }
  170. return c.AuthorityConfig.Validate(c.getAudiences())
  171. }
  172. // getAudiences returns the legacy and possible urls without the ports that will
  173. // be used as the default provisioner audiences. The CA might have proxies in
  174. // front so we cannot rely on the port.
  175. func (c *Config) getAudiences() provisioner.Audiences {
  176. audiences := provisioner.Audiences{
  177. Sign: []string{legacyAuthority},
  178. Revoke: []string{legacyAuthority},
  179. }
  180. for _, name := range c.DNSNames {
  181. audiences.Sign = append(audiences.Sign,
  182. fmt.Sprintf("https://%s/sign", name), fmt.Sprintf("https://%s/1.0/sign", name))
  183. audiences.Revoke = append(audiences.Revoke,
  184. fmt.Sprintf("https://%s/revoke", name), fmt.Sprintf("https://%s/1.0/revoke", name))
  185. }
  186. return audiences
  187. }