tls_options.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package ca
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "github.com/smallstep/certificates/api"
  6. )
  7. // TLSOption defines the type of a function that modifies a tls.Config.
  8. type TLSOption func(ctx *TLSOptionCtx) error
  9. // TLSOptionCtx is the context modified on TLSOption methods.
  10. type TLSOptionCtx struct {
  11. Client *Client
  12. Config *tls.Config
  13. Sign *api.SignResponse
  14. OnRenewFunc []TLSOption
  15. mutableConfig *mutableTLSConfig
  16. hasRootCA bool
  17. hasClientCA bool
  18. }
  19. // newTLSOptionCtx creates the TLSOption context.
  20. func newTLSOptionCtx(c *Client, config *tls.Config, sign *api.SignResponse) *TLSOptionCtx {
  21. return &TLSOptionCtx{
  22. Client: c,
  23. Config: config,
  24. Sign: sign,
  25. mutableConfig: newMutableTLSConfig(),
  26. }
  27. }
  28. func (ctx *TLSOptionCtx) apply(options []TLSOption) error {
  29. for _, fn := range options {
  30. if err := fn(ctx); err != nil {
  31. return err
  32. }
  33. }
  34. // Initialize mutable config with the fully configured tls.Config
  35. ctx.mutableConfig.Init(ctx.Config)
  36. // Build RootCAs and ClientCAs with given root certificate if necessary
  37. if root, err := RootCertificate(ctx.Sign); err == nil {
  38. if !ctx.hasRootCA {
  39. if ctx.Config.RootCAs == nil {
  40. ctx.Config.RootCAs = x509.NewCertPool()
  41. }
  42. ctx.Config.RootCAs.AddCert(root)
  43. ctx.mutableConfig.AddImmutableRootCACert(root)
  44. }
  45. if !ctx.hasClientCA && ctx.Config.ClientAuth != tls.NoClientCert {
  46. if ctx.Config.ClientCAs == nil {
  47. ctx.Config.ClientCAs = x509.NewCertPool()
  48. }
  49. ctx.Config.ClientCAs.AddCert(root)
  50. ctx.mutableConfig.AddImmutableClientCACert(root)
  51. }
  52. }
  53. // Update tls.Config with mutable data
  54. if ctx.Config.RootCAs == nil && len(ctx.mutableConfig.mutRootCerts) > 0 {
  55. ctx.Config.RootCAs = x509.NewCertPool()
  56. }
  57. if ctx.Config.ClientCAs == nil && len(ctx.mutableConfig.mutClientCerts) > 0 {
  58. ctx.Config.ClientCAs = x509.NewCertPool()
  59. }
  60. // Add mutable certificates
  61. for _, cert := range ctx.mutableConfig.mutRootCerts {
  62. ctx.Config.RootCAs.AddCert(cert)
  63. }
  64. for _, cert := range ctx.mutableConfig.mutClientCerts {
  65. ctx.Config.ClientCAs.AddCert(cert)
  66. }
  67. ctx.mutableConfig.Reload()
  68. return nil
  69. }
  70. func (ctx *TLSOptionCtx) applyRenew() error {
  71. for _, fn := range ctx.OnRenewFunc {
  72. if err := fn(ctx); err != nil {
  73. return err
  74. }
  75. }
  76. // Reload mutable config with the changes
  77. ctx.mutableConfig.Reload()
  78. return nil
  79. }
  80. // RequireAndVerifyClientCert is a tls.Config option used on servers to enforce
  81. // a valid TLS client certificate. This is the default option for mTLS servers.
  82. func RequireAndVerifyClientCert() TLSOption {
  83. return func(ctx *TLSOptionCtx) error {
  84. ctx.Config.ClientAuth = tls.RequireAndVerifyClientCert
  85. return nil
  86. }
  87. }
  88. // VerifyClientCertIfGiven is a tls.Config option used on on servers to validate
  89. // a TLS client certificate if it is provided. It does not requires a certificate.
  90. func VerifyClientCertIfGiven() TLSOption {
  91. return func(ctx *TLSOptionCtx) error {
  92. ctx.Config.ClientAuth = tls.VerifyClientCertIfGiven
  93. return nil
  94. }
  95. }
  96. // AddRootCA adds to the tls.Config RootCAs the given certificate. RootCAs
  97. // defines the set of root certificate authorities that clients use when
  98. // verifying server certificates.
  99. func AddRootCA(cert *x509.Certificate) TLSOption {
  100. return func(ctx *TLSOptionCtx) error {
  101. if ctx.Config.RootCAs == nil {
  102. ctx.Config.RootCAs = x509.NewCertPool()
  103. }
  104. ctx.Config.RootCAs.AddCert(cert)
  105. ctx.mutableConfig.AddImmutableRootCACert(cert)
  106. return nil
  107. }
  108. }
  109. // AddClientCA adds to the tls.Config ClientCAs the given certificate. ClientCAs
  110. // defines the set of root certificate authorities that servers use if required
  111. // to verify a client certificate by the policy in ClientAuth.
  112. func AddClientCA(cert *x509.Certificate) TLSOption {
  113. return func(ctx *TLSOptionCtx) error {
  114. if ctx.Config.ClientCAs == nil {
  115. ctx.Config.ClientCAs = x509.NewCertPool()
  116. }
  117. ctx.Config.ClientCAs.AddCert(cert)
  118. ctx.mutableConfig.AddImmutableClientCACert(cert)
  119. return nil
  120. }
  121. }
  122. // AddRootsToRootCAs does a roots request and adds to the tls.Config RootCAs all
  123. // the certificates in the response. RootCAs defines the set of root certificate
  124. // authorities that clients use when verifying server certificates.
  125. //
  126. // BootstrapServer and BootstrapClient methods include this option by default.
  127. func AddRootsToRootCAs() TLSOption {
  128. // var once sync.Once
  129. fn := func(ctx *TLSOptionCtx) error {
  130. certs, err := ctx.Client.Roots()
  131. if err != nil {
  132. return err
  133. }
  134. ctx.hasRootCA = true
  135. ctx.mutableConfig.AddRootCAs(certs.Certificates)
  136. return nil
  137. }
  138. return func(ctx *TLSOptionCtx) error {
  139. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  140. return fn(ctx)
  141. }
  142. }
  143. // AddRootsToClientCAs does a roots request and adds to the tls.Config ClientCAs
  144. // all the certificates in the response. ClientCAs defines the set of root
  145. // certificate authorities that servers use if required to verify a client
  146. // certificate by the policy in ClientAuth.
  147. //
  148. // BootstrapServer method includes this option by default.
  149. func AddRootsToClientCAs() TLSOption {
  150. // var once sync.Once
  151. fn := func(ctx *TLSOptionCtx) error {
  152. certs, err := ctx.Client.Roots()
  153. if err != nil {
  154. return err
  155. }
  156. ctx.hasClientCA = true
  157. ctx.mutableConfig.AddClientCAs(certs.Certificates)
  158. return nil
  159. }
  160. return func(ctx *TLSOptionCtx) error {
  161. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  162. return fn(ctx)
  163. }
  164. }
  165. // AddFederationToRootCAs does a federation request and adds to the tls.Config
  166. // RootCAs all the certificates in the response. RootCAs defines the set of root
  167. // certificate authorities that clients use when verifying server certificates.
  168. func AddFederationToRootCAs() TLSOption {
  169. fn := func(ctx *TLSOptionCtx) error {
  170. certs, err := ctx.Client.Federation()
  171. if err != nil {
  172. return err
  173. }
  174. ctx.mutableConfig.AddRootCAs(certs.Certificates)
  175. return nil
  176. }
  177. return func(ctx *TLSOptionCtx) error {
  178. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  179. return fn(ctx)
  180. }
  181. }
  182. // AddFederationToClientCAs does a federation request and adds to the tls.Config
  183. // ClientCAs all the certificates in the response. ClientCAs defines the set of
  184. // root certificate authorities that servers use if required to verify a client
  185. // certificate by the policy in ClientAuth.
  186. func AddFederationToClientCAs() TLSOption {
  187. fn := func(ctx *TLSOptionCtx) error {
  188. certs, err := ctx.Client.Federation()
  189. if err != nil {
  190. return err
  191. }
  192. ctx.mutableConfig.AddClientCAs(certs.Certificates)
  193. return nil
  194. }
  195. return func(ctx *TLSOptionCtx) error {
  196. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  197. return fn(ctx)
  198. }
  199. }
  200. // AddRootsToCAs does a roots request and adds the resulting certs to the
  201. // tls.Config RootCAs and ClientCAs. Combines the functionality of
  202. // AddRootsToRootCAs and AddRootsToClientCAs.
  203. func AddRootsToCAs() TLSOption {
  204. fn := func(ctx *TLSOptionCtx) error {
  205. certs, err := ctx.Client.Roots()
  206. if err != nil {
  207. return err
  208. }
  209. ctx.hasRootCA = true
  210. ctx.hasClientCA = true
  211. ctx.mutableConfig.AddRootCAs(certs.Certificates)
  212. ctx.mutableConfig.AddClientCAs(certs.Certificates)
  213. return nil
  214. }
  215. return func(ctx *TLSOptionCtx) error {
  216. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  217. return fn(ctx)
  218. }
  219. }
  220. // AddFederationToCAs does a federation request and adds the resulting certs to the
  221. // tls.Config RootCAs and ClientCAs. Combines the functionality of
  222. // AddFederationToRootCAs and AddFederationToClientCAs.
  223. func AddFederationToCAs() TLSOption {
  224. fn := func(ctx *TLSOptionCtx) error {
  225. certs, err := ctx.Client.Federation()
  226. if err != nil {
  227. return err
  228. }
  229. if ctx.mutableConfig == nil {
  230. if ctx.Config.RootCAs == nil {
  231. ctx.Config.RootCAs = x509.NewCertPool()
  232. }
  233. if ctx.Config.ClientCAs == nil {
  234. ctx.Config.ClientCAs = x509.NewCertPool()
  235. }
  236. for _, cert := range certs.Certificates {
  237. ctx.Config.RootCAs.AddCert(cert.Certificate)
  238. ctx.Config.ClientCAs.AddCert(cert.Certificate)
  239. }
  240. } else {
  241. ctx.mutableConfig.AddRootCAs(certs.Certificates)
  242. ctx.mutableConfig.AddClientCAs(certs.Certificates)
  243. }
  244. return nil
  245. }
  246. return func(ctx *TLSOptionCtx) error {
  247. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  248. return fn(ctx)
  249. }
  250. }