tls_options.go 6.7 KB

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