tls_options.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.hasRootCA = true
  105. ctx.Config.RootCAs.AddCert(cert)
  106. ctx.mutableConfig.AddImmutableRootCACert(cert)
  107. return nil
  108. }
  109. }
  110. // AddClientCA adds to the tls.Config ClientCAs the given certificate. ClientCAs
  111. // defines the set of root certificate authorities that servers use if required
  112. // to verify a client certificate by the policy in ClientAuth.
  113. func AddClientCA(cert *x509.Certificate) TLSOption {
  114. return func(ctx *TLSOptionCtx) error {
  115. if ctx.Config.ClientCAs == nil {
  116. ctx.Config.ClientCAs = x509.NewCertPool()
  117. }
  118. ctx.hasClientCA = true
  119. ctx.Config.ClientCAs.AddCert(cert)
  120. ctx.mutableConfig.AddImmutableClientCACert(cert)
  121. return nil
  122. }
  123. }
  124. // AddRootsToRootCAs does a roots request and adds to the tls.Config RootCAs all
  125. // the certificates in the response. RootCAs defines the set of root certificate
  126. // authorities that clients use when verifying server certificates.
  127. //
  128. // BootstrapServer and BootstrapClient methods include this option by default.
  129. func AddRootsToRootCAs() TLSOption {
  130. // var once sync.Once
  131. fn := func(ctx *TLSOptionCtx) error {
  132. certs, err := ctx.Client.Roots()
  133. if err != nil {
  134. return err
  135. }
  136. ctx.hasRootCA = true
  137. ctx.mutableConfig.AddRootCAs(certs.Certificates)
  138. return nil
  139. }
  140. return func(ctx *TLSOptionCtx) error {
  141. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  142. return fn(ctx)
  143. }
  144. }
  145. // AddRootsToClientCAs does a roots request and adds to the tls.Config ClientCAs
  146. // all the certificates in the response. ClientCAs defines the set of root
  147. // certificate authorities that servers use if required to verify a client
  148. // certificate by the policy in ClientAuth.
  149. //
  150. // BootstrapServer method includes this option by default.
  151. func AddRootsToClientCAs() TLSOption {
  152. // var once sync.Once
  153. fn := func(ctx *TLSOptionCtx) error {
  154. certs, err := ctx.Client.Roots()
  155. if err != nil {
  156. return err
  157. }
  158. ctx.hasClientCA = true
  159. ctx.mutableConfig.AddClientCAs(certs.Certificates)
  160. return nil
  161. }
  162. return func(ctx *TLSOptionCtx) error {
  163. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  164. return fn(ctx)
  165. }
  166. }
  167. // AddFederationToRootCAs does a federation request and adds to the tls.Config
  168. // RootCAs all the certificates in the response. RootCAs defines the set of root
  169. // certificate authorities that clients use when verifying server certificates.
  170. func AddFederationToRootCAs() TLSOption {
  171. fn := func(ctx *TLSOptionCtx) error {
  172. certs, err := ctx.Client.Federation()
  173. if err != nil {
  174. return err
  175. }
  176. ctx.mutableConfig.AddRootCAs(certs.Certificates)
  177. return nil
  178. }
  179. return func(ctx *TLSOptionCtx) error {
  180. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  181. return fn(ctx)
  182. }
  183. }
  184. // AddFederationToClientCAs does a federation request and adds to the tls.Config
  185. // ClientCAs all the certificates in the response. ClientCAs defines the set of
  186. // root certificate authorities that servers use if required to verify a client
  187. // certificate by the policy in ClientAuth.
  188. func AddFederationToClientCAs() TLSOption {
  189. fn := func(ctx *TLSOptionCtx) error {
  190. certs, err := ctx.Client.Federation()
  191. if err != nil {
  192. return err
  193. }
  194. ctx.mutableConfig.AddClientCAs(certs.Certificates)
  195. return nil
  196. }
  197. return func(ctx *TLSOptionCtx) error {
  198. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  199. return fn(ctx)
  200. }
  201. }
  202. // AddRootsToCAs does a roots request and adds the resulting certs to the
  203. // tls.Config RootCAs and ClientCAs. Combines the functionality of
  204. // AddRootsToRootCAs and AddRootsToClientCAs.
  205. func AddRootsToCAs() TLSOption {
  206. fn := func(ctx *TLSOptionCtx) error {
  207. certs, err := ctx.Client.Roots()
  208. if err != nil {
  209. return err
  210. }
  211. ctx.hasRootCA = true
  212. ctx.hasClientCA = true
  213. ctx.mutableConfig.AddRootCAs(certs.Certificates)
  214. ctx.mutableConfig.AddClientCAs(certs.Certificates)
  215. return nil
  216. }
  217. return func(ctx *TLSOptionCtx) error {
  218. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  219. return fn(ctx)
  220. }
  221. }
  222. // AddFederationToCAs does a federation request and adds the resulting certs to the
  223. // tls.Config RootCAs and ClientCAs. Combines the functionality of
  224. // AddFederationToRootCAs and AddFederationToClientCAs.
  225. func AddFederationToCAs() TLSOption {
  226. fn := func(ctx *TLSOptionCtx) error {
  227. certs, err := ctx.Client.Federation()
  228. if err != nil {
  229. return err
  230. }
  231. if ctx.mutableConfig == nil {
  232. if ctx.Config.RootCAs == nil {
  233. ctx.Config.RootCAs = x509.NewCertPool()
  234. }
  235. if ctx.Config.ClientCAs == nil {
  236. ctx.Config.ClientCAs = x509.NewCertPool()
  237. }
  238. for _, cert := range certs.Certificates {
  239. ctx.Config.RootCAs.AddCert(cert.Certificate)
  240. ctx.Config.ClientCAs.AddCert(cert.Certificate)
  241. }
  242. } else {
  243. ctx.mutableConfig.AddRootCAs(certs.Certificates)
  244. ctx.mutableConfig.AddClientCAs(certs.Certificates)
  245. }
  246. return nil
  247. }
  248. return func(ctx *TLSOptionCtx) error {
  249. ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
  250. return fn(ctx)
  251. }
  252. }