4
0

ca.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. ca.renewer.Stop()
  106. return ca.srv.Shutdown()
  107. }
  108. // Reload reloads the configuration of the CA and calls to the server Reload
  109. // method.
  110. func (ca *CA) Reload() error {
  111. if ca.opts.configFile == "" {
  112. return errors.New("error reloading ca: configuration file is not set")
  113. }
  114. config, err := authority.LoadConfiguration(ca.opts.configFile)
  115. if err != nil {
  116. return errors.Wrap(err, "error reloading ca")
  117. }
  118. newCA, err := New(config, WithPassword(ca.opts.password), WithConfigFile(ca.opts.configFile))
  119. if err != nil {
  120. return errors.Wrap(err, "error reloading ca")
  121. }
  122. return ca.srv.Reload(newCA.srv)
  123. }
  124. // getTLSConfig returns a TLSConfig for the CA server with a self-renewing
  125. // server certificate.
  126. func (ca *CA) getTLSConfig(auth *authority.Authority) (*tls.Config, error) {
  127. // Create initial TLS certificate
  128. tlsCrt, err := auth.GetTLSCertificate()
  129. if err != nil {
  130. return nil, err
  131. }
  132. // Start tls renewer with the new certificate.
  133. // If a renewer was started, attempt to stop it before.
  134. if ca.renewer != nil {
  135. ca.renewer.Stop()
  136. }
  137. ca.renewer, err = NewTLSRenewer(tlsCrt, auth.GetTLSCertificate)
  138. if err != nil {
  139. return nil, err
  140. }
  141. ca.renewer.Run()
  142. var tlsConfig *tls.Config
  143. if ca.config.TLS != nil {
  144. tlsConfig = ca.config.TLS.TLSConfig()
  145. } else {
  146. tlsConfig = &tls.Config{
  147. MinVersion: tls.VersionTLS12,
  148. }
  149. }
  150. certPool := x509.NewCertPool()
  151. for _, crt := range auth.GetRootCertificates() {
  152. certPool.AddCert(crt)
  153. }
  154. // GetCertificate will only be called if the client supplies SNI
  155. // information or if tlsConfig.Certificates is empty.
  156. // When client requests are made using an IP address (as opposed to a domain
  157. // name) the server does not receive any SNI and may fallback to using the
  158. // first entry in the Certificates attribute; by setting the attribute to
  159. // empty we are implicitly forcing GetCertificate to be the only mechanism
  160. // by which the server can find it's own leaf Certificate.
  161. tlsConfig.Certificates = []tls.Certificate{}
  162. tlsConfig.GetCertificate = ca.renewer.GetCertificateForCA
  163. // Add support for mutual tls to renew certificates
  164. tlsConfig.ClientAuth = tls.VerifyClientCertIfGiven
  165. tlsConfig.ClientCAs = certPool
  166. // Use server's most preferred ciphersuite
  167. tlsConfig.PreferServerCipherSuites = true
  168. return tlsConfig, nil
  169. }