tlsTunnel.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2023, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package server
  20. import (
  21. "net"
  22. tls "github.com/Psiphon-Labs/psiphon-tls"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/obfuscator"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/values"
  29. )
  30. // TLSTunnelServer tunnels TCP traffic (in the case of Psiphon, Obfuscated SSH
  31. // traffic) over TLS.
  32. type TLSTunnelServer struct {
  33. support *SupportServices
  34. listener net.Listener
  35. listenerTunnelProtocol string
  36. listenerPort int
  37. passthroughAddress string
  38. tlsConfig *tls.Config
  39. obfuscatorSeedHistory *obfuscator.SeedHistory
  40. }
  41. // ListenTLSTunnel returns the listener of a new TLSTunnelServer.
  42. // Note: the first Read or Write call on a connection returned by the listener
  43. // will trigger the underlying TLS handshake.
  44. func ListenTLSTunnel(
  45. support *SupportServices,
  46. listener net.Listener,
  47. listenerTunnelProtocol string,
  48. listenerPort int,
  49. ) (net.Listener, error) {
  50. server, err := NewTLSTunnelServer(support, listener, listenerTunnelProtocol, listenerPort)
  51. if err != nil {
  52. return nil, errors.Trace(err)
  53. }
  54. listener = tls.NewListener(server.listener, server.tlsConfig)
  55. return NewTLSTunnelListener(listener, server), nil
  56. }
  57. // NewTLSTunnelServer initializes a new TLSTunnelServer.
  58. func NewTLSTunnelServer(
  59. support *SupportServices,
  60. listener net.Listener,
  61. listenerTunnelProtocol string,
  62. listenerPort int) (*TLSTunnelServer, error) {
  63. passthroughAddress := support.Config.TunnelProtocolPassthroughAddresses[listenerTunnelProtocol]
  64. tlsServer := &TLSTunnelServer{
  65. support: support,
  66. listener: listener,
  67. listenerTunnelProtocol: listenerTunnelProtocol,
  68. listenerPort: listenerPort,
  69. passthroughAddress: passthroughAddress,
  70. obfuscatorSeedHistory: obfuscator.NewSeedHistory(nil),
  71. }
  72. tlsConfig, err := tlsServer.makeTLSTunnelConfig()
  73. if err != nil {
  74. return nil, errors.Trace(err)
  75. }
  76. tlsServer.tlsConfig = tlsConfig
  77. return tlsServer, nil
  78. }
  79. // makeTLSTunnelConfig creates a TLS config for a TLSTunnelServer listener.
  80. func (server *TLSTunnelServer) makeTLSTunnelConfig() (*tls.Config, error) {
  81. // Limitation: certificate value changes on restart.
  82. certificate, privateKey, err := common.GenerateWebServerCertificate(values.GetHostName())
  83. if err != nil {
  84. return nil, errors.Trace(err)
  85. }
  86. tlsCertificate, err := tls.X509KeyPair(
  87. []byte(certificate), []byte(privateKey))
  88. if err != nil {
  89. return nil, errors.Trace(err)
  90. }
  91. var minVersion uint16
  92. if protocol.TunnelProtocolUsesTLSOSSH(server.listenerTunnelProtocol) {
  93. // Use min TLS 1.3 so cert is not plaintext on the wire.
  94. minVersion = tls.VersionTLS13
  95. } else {
  96. // Need to support older TLS versions for backwards compatibility.
  97. // Vary the minimum version to frustrate scanning/fingerprinting of unfronted servers.
  98. // Limitation: like the certificate, this value changes on restart.
  99. minVersionCandidates := []uint16{tls.VersionTLS10, tls.VersionTLS11, tls.VersionTLS12}
  100. minVersion = minVersionCandidates[prng.Intn(len(minVersionCandidates))]
  101. }
  102. config := &tls.Config{
  103. Certificates: []tls.Certificate{tlsCertificate},
  104. NextProtos: []string{"http/1.1"},
  105. MinVersion: minVersion,
  106. }
  107. // When configured, initialize passthrough mode, an anti-probing defense.
  108. // Clients must prove knowledge of the obfuscated key via a message sent in
  109. // the TLS ClientHello random field.
  110. //
  111. // When clients fail to provide a valid message, the client connection is
  112. // relayed to the designated passthrough address, typically another web site.
  113. // The entire flow is relayed, including the original ClientHello, so the
  114. // client will perform a TLS handshake with the passthrough target.
  115. //
  116. // Irregular events are logged for invalid client activity.
  117. if server.passthroughAddress != "" {
  118. config.PassthroughAddress = server.passthroughAddress
  119. config.PassthroughVerifyMessage = func(
  120. message []byte) bool {
  121. return obfuscator.VerifyTLSPassthroughMessage(
  122. true,
  123. // Meek obfuscated key used for legacy reasons. See comment for
  124. // MeekObfuscatedKey.
  125. server.support.Config.MeekObfuscatedKey,
  126. message)
  127. }
  128. config.PassthroughLogInvalidMessage = func(
  129. clientIP string) {
  130. logIrregularTunnel(
  131. server.support,
  132. server.listenerTunnelProtocol,
  133. server.listenerPort,
  134. clientIP,
  135. errors.TraceNew("invalid passthrough message"),
  136. nil)
  137. }
  138. config.PassthroughHistoryAddNew = func(
  139. clientIP string,
  140. clientRandom []byte) bool {
  141. // Use a custom, shorter TTL based on the validity period of the
  142. // passthrough message.
  143. TTL := obfuscator.TLS_PASSTHROUGH_TIME_PERIOD
  144. // strictMode is true as legitimate clients never retry TLS
  145. // connections using a previous random value.
  146. ok, logFields := server.obfuscatorSeedHistory.AddNewWithTTL(
  147. true,
  148. clientIP,
  149. "client-random",
  150. clientRandom,
  151. TTL)
  152. if logFields != nil {
  153. logIrregularTunnel(
  154. server.support,
  155. server.listenerTunnelProtocol,
  156. server.listenerPort,
  157. clientIP,
  158. errors.TraceNew("duplicate passthrough message"),
  159. LogFields(*logFields))
  160. }
  161. return ok
  162. }
  163. }
  164. return config, nil
  165. }
  166. // TLSTunnelListener implements the net.Listener interface. Accept returns a
  167. // net.Conn which implements the common.MetricsSource interface.
  168. type TLSTunnelListener struct {
  169. net.Listener
  170. server *TLSTunnelServer
  171. }
  172. // NewTLSTunnelListener initializes a new TLSTunnelListener.
  173. func NewTLSTunnelListener(listener net.Listener, server *TLSTunnelServer) *TLSTunnelListener {
  174. return &TLSTunnelListener{
  175. Listener: listener,
  176. server: server,
  177. }
  178. }
  179. func (l *TLSTunnelListener) Accept() (net.Conn, error) {
  180. conn, err := l.Listener.Accept()
  181. if err != nil {
  182. return nil, errors.Trace(err)
  183. }
  184. return NewTLSTunnelConn(conn, l.server), nil
  185. }
  186. // TLSTunnelConn implements the net.Conn and common.MetricsSource interfaces.
  187. type TLSTunnelConn struct {
  188. net.Conn
  189. server *TLSTunnelServer
  190. }
  191. // NewTLSTunnelConn initializes a new TLSTunnelConn.
  192. func NewTLSTunnelConn(conn net.Conn, server *TLSTunnelServer) *TLSTunnelConn {
  193. return &TLSTunnelConn{
  194. Conn: conn,
  195. server: server,
  196. }
  197. }
  198. // GetMetrics implements the common.MetricsSource interface.
  199. func (conn *TLSTunnelConn) GetMetrics() common.LogFields {
  200. var logFields common.LogFields
  201. // Relay any metrics from the underlying conn.
  202. if m, ok := conn.Conn.(common.MetricsSource); ok {
  203. logFields = m.GetMetrics()
  204. } else {
  205. logFields = make(common.LogFields)
  206. }
  207. if conn.server.passthroughAddress != "" {
  208. logFields["passthrough_address"] = conn.server.passthroughAddress
  209. }
  210. return logFields
  211. }