ca.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package ca
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "net/http"
  6. "github.com/go-chi/chi"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/api"
  9. "github.com/smallstep/certificates/authority"
  10. "github.com/smallstep/certificates/logging"
  11. "github.com/smallstep/certificates/monitoring"
  12. "github.com/smallstep/certificates/server"
  13. )
  14. type options struct {
  15. configFile string
  16. password []byte
  17. }
  18. func (o *options) apply(opts []Option) {
  19. for _, fn := range opts {
  20. fn(o)
  21. }
  22. }
  23. // Option is the type of options passed to the CA constructor.
  24. type Option func(o *options)
  25. // WithConfigFile sets the given name as the configuration file name in the CA
  26. // options.
  27. func WithConfigFile(name string) Option {
  28. return func(o *options) {
  29. o.configFile = name
  30. }
  31. }
  32. // WithPassword sets the given password as the configured password in the CA
  33. // options.
  34. func WithPassword(password []byte) Option {
  35. return func(o *options) {
  36. o.password = password
  37. }
  38. }
  39. // CA is the type used to build the complete certificate authority. It builds
  40. // the HTTP server, set ups the middlewares and the HTTP handlers.
  41. type CA struct {
  42. auth *authority.Authority
  43. config *authority.Config
  44. srv *server.Server
  45. opts *options
  46. renewer *TLSRenewer
  47. }
  48. // New creates and initializes the CA with the given configuration and options.
  49. func New(config *authority.Config, opts ...Option) (*CA, error) {
  50. ca := &CA{
  51. config: config,
  52. opts: new(options),
  53. }
  54. ca.opts.apply(opts)
  55. return ca.Init(config)
  56. }
  57. // Init initializes the CA with the given configuration.
  58. func (ca *CA) Init(config *authority.Config) (*CA, error) {
  59. if l := len(ca.opts.password); l > 0 {
  60. ca.config.Password = string(ca.opts.password)
  61. }
  62. auth, err := authority.New(config)
  63. if err != nil {
  64. return nil, err
  65. }
  66. tlsConfig, err := ca.getTLSConfig(auth)
  67. if err != nil {
  68. return nil, err
  69. }
  70. // Using chi as the main router
  71. mux := chi.NewRouter()
  72. handler := http.Handler(mux)
  73. // Add api endpoints in / and /1.0
  74. routerHandler := api.New(auth)
  75. routerHandler.Route(mux)
  76. mux.Route("/1.0", func(r chi.Router) {
  77. routerHandler.Route(r)
  78. })
  79. // Add monitoring if configured
  80. if len(config.Monitoring) > 0 {
  81. m, err := monitoring.New(config.Monitoring)
  82. if err != nil {
  83. return nil, err
  84. }
  85. handler = m.Middleware(handler)
  86. }
  87. // Add logger if configured
  88. if len(config.Logger) > 0 {
  89. logger, err := logging.New("ca", config.Logger)
  90. if err != nil {
  91. return nil, err
  92. }
  93. handler = logger.Middleware(handler)
  94. }
  95. ca.auth = auth
  96. ca.srv = server.New(config.Address, handler, tlsConfig)
  97. return ca, nil
  98. }
  99. // Run starts the CA calling to the server ListenAndServe method.
  100. func (ca *CA) Run() error {
  101. return ca.srv.ListenAndServe()
  102. }
  103. // Stop stops the CA calling to the server Shutdown method.
  104. func (ca *CA) Stop() error {
  105. return ca.srv.Shutdown()
  106. }
  107. // Reload reloads the configuration of the CA and calls to the server Reload
  108. // method.
  109. func (ca *CA) Reload() error {
  110. if ca.opts.configFile == "" {
  111. return errors.New("error reloading ca: configuration file is not set")
  112. }
  113. config, err := authority.LoadConfiguration(ca.opts.configFile)
  114. if err != nil {
  115. return errors.Wrap(err, "error reloading ca")
  116. }
  117. newCA, err := New(config, WithPassword(ca.opts.password), WithConfigFile(ca.opts.configFile))
  118. if err != nil {
  119. return errors.Wrap(err, "error reloading ca")
  120. }
  121. return ca.srv.Reload(newCA.srv)
  122. }
  123. // getTLSConfig returns a TLSConfig for the CA server with a self-renewing
  124. // server certificate.
  125. func (ca *CA) getTLSConfig(auth *authority.Authority) (*tls.Config, error) {
  126. // Create initial TLS certificate
  127. tlsCrt, err := auth.GetTLSCertificate()
  128. if err != nil {
  129. return nil, err
  130. }
  131. // Start tls renewer with the new certificate.
  132. // If a renewer was started, attempt to stop it before.
  133. if ca.renewer != nil {
  134. ca.renewer.Stop()
  135. }
  136. ca.renewer, err = NewTLSRenewer(tlsCrt, auth.GetTLSCertificate)
  137. if err != nil {
  138. return nil, err
  139. }
  140. ca.renewer.Run()
  141. var tlsConfig *tls.Config
  142. if ca.config.TLS != nil {
  143. tlsConfig = ca.config.TLS.TLSConfig()
  144. } else {
  145. tlsConfig = &tls.Config{
  146. MinVersion: tls.VersionTLS12,
  147. }
  148. }
  149. certPool := x509.NewCertPool()
  150. certPool.AddCert(auth.GetRootCertificate())
  151. // GetCertificate will only be called if the client supplies SNI
  152. // information or if tlsConfig.Certificates is empty.
  153. // When client requests are made using an IP address (as opposed to a domain
  154. // name) the server does not receive any SNI and may fallback to using the
  155. // first entry in the Certificates attribute; by setting the attribute to
  156. // empty we are implicitly forcing GetCertificate to be the only mechanism
  157. // by which the server can find it's own leaf Certificate.
  158. tlsConfig.Certificates = []tls.Certificate{}
  159. tlsConfig.GetCertificate = ca.renewer.GetCertificate
  160. // Add support for mutual tls to renew certificates
  161. tlsConfig.ClientAuth = tls.VerifyClientCertIfGiven
  162. tlsConfig.ClientCAs = certPool
  163. // Use server's most preferred ciphersuite
  164. tlsConfig.PreferServerCipherSuites = true
  165. return tlsConfig, nil
  166. }