config.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Package tlsconfig provides primitives to retrieve secure-enough TLS configurations for both clients and servers.
  2. //
  3. // As a reminder from https://golang.org/pkg/crypto/tls/#Config:
  4. // A Config structure is used to configure a TLS client or server. After one has been passed to a TLS function it must not be modified.
  5. // A Config may be reused; the tls package will also not modify it.
  6. package tlsconfig
  7. import (
  8. "crypto/tls"
  9. "crypto/x509"
  10. "fmt"
  11. "io/ioutil"
  12. "os"
  13. "github.com/Sirupsen/logrus"
  14. )
  15. // Options represents the information needed to create client and server TLS configurations.
  16. type Options struct {
  17. CAFile string
  18. // If either CertFile or KeyFile is empty, Client() will not load them
  19. // preventing the client from authenticating to the server.
  20. // However, Server() requires them and will error out if they are empty.
  21. CertFile string
  22. KeyFile string
  23. // client-only option
  24. InsecureSkipVerify bool
  25. // server-only option
  26. ClientAuth tls.ClientAuthType
  27. }
  28. // Extra (server-side) accepted CBC cipher suites - will phase out in the future
  29. var acceptedCBCCiphers = []uint16{
  30. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  31. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  32. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  33. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  34. tls.TLS_RSA_WITH_AES_256_CBC_SHA,
  35. tls.TLS_RSA_WITH_AES_128_CBC_SHA,
  36. }
  37. // DefaultServerAcceptedCiphers should be uses by code which already has a crypto/tls
  38. // options struct but wants to use a commonly accepted set of TLS cipher suites, with
  39. // known weak algorithms removed.
  40. var DefaultServerAcceptedCiphers = append(clientCipherSuites, acceptedCBCCiphers...)
  41. // ServerDefault is a secure-enough TLS configuration for the server TLS configuration.
  42. var ServerDefault = tls.Config{
  43. // Avoid fallback to SSL protocols < TLS1.0
  44. MinVersion: tls.VersionTLS10,
  45. PreferServerCipherSuites: true,
  46. CipherSuites: DefaultServerAcceptedCiphers,
  47. }
  48. // ClientDefault is a secure-enough TLS configuration for the client TLS configuration.
  49. var ClientDefault = tls.Config{
  50. // Prefer TLS1.2 as the client minimum
  51. MinVersion: tls.VersionTLS12,
  52. CipherSuites: clientCipherSuites,
  53. }
  54. // certPool returns an X.509 certificate pool from `caFile`, the certificate file.
  55. func certPool(caFile string) (*x509.CertPool, error) {
  56. // If we should verify the server, we need to load a trusted ca
  57. certPool := x509.NewCertPool()
  58. pem, err := ioutil.ReadFile(caFile)
  59. if err != nil {
  60. return nil, fmt.Errorf("Could not read CA certificate %q: %v", caFile, err)
  61. }
  62. if !certPool.AppendCertsFromPEM(pem) {
  63. return nil, fmt.Errorf("failed to append certificates from PEM file: %q", caFile)
  64. }
  65. s := certPool.Subjects()
  66. subjects := make([]string, len(s))
  67. for i, subject := range s {
  68. subjects[i] = string(subject)
  69. }
  70. logrus.Debugf("Trusting certs with subjects: %v", subjects)
  71. return certPool, nil
  72. }
  73. // Client returns a TLS configuration meant to be used by a client.
  74. func Client(options Options) (*tls.Config, error) {
  75. tlsConfig := ClientDefault
  76. tlsConfig.InsecureSkipVerify = options.InsecureSkipVerify
  77. if !options.InsecureSkipVerify && options.CAFile != "" {
  78. CAs, err := certPool(options.CAFile)
  79. if err != nil {
  80. return nil, err
  81. }
  82. tlsConfig.RootCAs = CAs
  83. }
  84. if options.CertFile != "" || options.KeyFile != "" {
  85. tlsCert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
  86. if err != nil {
  87. return nil, fmt.Errorf("Could not load X509 key pair: %v. Make sure the key is not encrypted", err)
  88. }
  89. tlsConfig.Certificates = []tls.Certificate{tlsCert}
  90. }
  91. return &tlsConfig, nil
  92. }
  93. // Server returns a TLS configuration meant to be used by a server.
  94. func Server(options Options) (*tls.Config, error) {
  95. tlsConfig := ServerDefault
  96. tlsConfig.ClientAuth = options.ClientAuth
  97. tlsCert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
  98. if err != nil {
  99. if os.IsNotExist(err) {
  100. return nil, fmt.Errorf("Could not load X509 key pair (cert: %q, key: %q): %v", options.CertFile, options.KeyFile, err)
  101. }
  102. return nil, fmt.Errorf("Error reading X509 key pair (cert: %q, key: %q): %v. Make sure the key is not encrypted.", options.CertFile, options.KeyFile, err)
  103. }
  104. tlsConfig.Certificates = []tls.Certificate{tlsCert}
  105. if options.ClientAuth >= tls.VerifyClientCertIfGiven {
  106. CAs, err := certPool(options.CAFile)
  107. if err != nil {
  108. return nil, err
  109. }
  110. tlsConfig.ClientCAs = CAs
  111. }
  112. return &tlsConfig, nil
  113. }