mutable_tls_config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package ca
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "sync"
  6. "github.com/smallstep/certificates/api"
  7. )
  8. // mutableTLSConfig allows to use a tls.Config with mutable cert pools.
  9. type mutableTLSConfig struct {
  10. sync.RWMutex
  11. config *tls.Config
  12. clientCerts []*x509.Certificate
  13. rootCerts []*x509.Certificate
  14. mutClientCerts []*x509.Certificate
  15. mutRootCerts []*x509.Certificate
  16. }
  17. // newMutableTLSConfig creates a new mutableTLSConfig that will be later
  18. // initialized with a tls.Config.
  19. func newMutableTLSConfig() *mutableTLSConfig {
  20. return &mutableTLSConfig{
  21. clientCerts: []*x509.Certificate{},
  22. rootCerts: []*x509.Certificate{},
  23. mutClientCerts: []*x509.Certificate{},
  24. mutRootCerts: []*x509.Certificate{},
  25. }
  26. }
  27. // Init initializes the mutable tls.Config with the given tls.Config.
  28. func (c *mutableTLSConfig) Init(base *tls.Config) {
  29. c.Lock()
  30. c.config = base.Clone()
  31. c.Unlock()
  32. }
  33. // TLSConfig returns the updated tls.Config it it has changed. It's used in the
  34. // tls.Config GetConfigForClient.
  35. func (c *mutableTLSConfig) TLSConfig() (config *tls.Config) {
  36. c.RLock()
  37. config = c.config.Clone()
  38. c.RUnlock()
  39. return
  40. }
  41. // Reload reloads the tls.Config with the new CAs.
  42. func (c *mutableTLSConfig) Reload() {
  43. // Prepare new pools
  44. c.RLock()
  45. rootCAs := x509.NewCertPool()
  46. clientCAs := x509.NewCertPool()
  47. // Fixed certs
  48. for _, cert := range c.rootCerts {
  49. rootCAs.AddCert(cert)
  50. }
  51. for _, cert := range c.clientCerts {
  52. clientCAs.AddCert(cert)
  53. }
  54. // Mutable certs
  55. for _, cert := range c.mutRootCerts {
  56. rootCAs.AddCert(cert)
  57. }
  58. for _, cert := range c.mutClientCerts {
  59. clientCAs.AddCert(cert)
  60. }
  61. c.RUnlock()
  62. // Set new pool
  63. c.Lock()
  64. c.config.RootCAs = rootCAs
  65. c.config.ClientCAs = clientCAs
  66. c.mutRootCerts = []*x509.Certificate{}
  67. c.mutClientCerts = []*x509.Certificate{}
  68. c.Unlock()
  69. }
  70. // AddImmutableClientCACert add an immutable cert to ClientCAs.
  71. func (c *mutableTLSConfig) AddImmutableClientCACert(cert *x509.Certificate) {
  72. c.Lock()
  73. c.clientCerts = append(c.clientCerts, cert)
  74. c.Unlock()
  75. }
  76. // AddImmutableRootCACert add an immutable cert to RootCas.
  77. func (c *mutableTLSConfig) AddImmutableRootCACert(cert *x509.Certificate) {
  78. c.Lock()
  79. c.rootCerts = append(c.rootCerts, cert)
  80. c.Unlock()
  81. }
  82. // AddClientCAs add mutable certs to ClientCAs.
  83. func (c *mutableTLSConfig) AddClientCAs(certs []api.Certificate) {
  84. c.Lock()
  85. for _, cert := range certs {
  86. c.mutClientCerts = append(c.mutClientCerts, cert.Certificate)
  87. }
  88. c.Unlock()
  89. }
  90. // AddRootCAs add mutable certs to RootCAs.
  91. func (c *mutableTLSConfig) AddRootCAs(certs []api.Certificate) {
  92. c.Lock()
  93. for _, cert := range certs {
  94. c.mutRootCerts = append(c.mutRootCerts, cert.Certificate)
  95. }
  96. c.Unlock()
  97. }