tls.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package mint
  2. // XXX(rlb): This file is borrowed pretty much wholesale from crypto/tls
  3. import (
  4. "errors"
  5. "net"
  6. "strings"
  7. "time"
  8. )
  9. // Server returns a new TLS server side connection
  10. // using conn as the underlying transport.
  11. // The configuration config must be non-nil and must include
  12. // at least one certificate or else set GetCertificate.
  13. func Server(conn net.Conn, config *Config) *Conn {
  14. return NewConn(conn, config, false)
  15. }
  16. // Client returns a new TLS client side connection
  17. // using conn as the underlying transport.
  18. // The config cannot be nil: users must set either ServerName or
  19. // InsecureSkipVerify in the config.
  20. func Client(conn net.Conn, config *Config) *Conn {
  21. return NewConn(conn, config, true)
  22. }
  23. // A listener implements a network listener (net.Listener) for TLS connections.
  24. type Listener struct {
  25. net.Listener
  26. config *Config
  27. }
  28. // Accept waits for and returns the next incoming TLS connection.
  29. // The returned connection c is a *tls.Conn.
  30. func (l *Listener) Accept() (c net.Conn, err error) {
  31. c, err = l.Listener.Accept()
  32. if err != nil {
  33. return
  34. }
  35. server := Server(c, l.config)
  36. err = server.Handshake()
  37. if err == AlertNoAlert {
  38. err = nil
  39. }
  40. c = server
  41. return
  42. }
  43. // NewListener creates a Listener which accepts connections from an inner
  44. // Listener and wraps each connection with Server.
  45. // The configuration config must be non-nil and must include
  46. // at least one certificate or else set GetCertificate.
  47. func NewListener(inner net.Listener, config *Config) (net.Listener, error) {
  48. if config != nil && config.NonBlocking {
  49. return nil, errors.New("listening not possible in non-blocking mode")
  50. }
  51. l := new(Listener)
  52. l.Listener = inner
  53. l.config = config
  54. return l, nil
  55. }
  56. // Listen creates a TLS listener accepting connections on the
  57. // given network address using net.Listen.
  58. // The configuration config must be non-nil and must include
  59. // at least one certificate or else set GetCertificate.
  60. func Listen(network, laddr string, config *Config) (net.Listener, error) {
  61. if config == nil || !config.ValidForServer() {
  62. return nil, errors.New("tls: neither Certificates nor GetCertificate set in Config")
  63. }
  64. l, err := net.Listen(network, laddr)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return NewListener(l, config)
  69. }
  70. type TimeoutError struct{}
  71. func (TimeoutError) Error() string { return "tls: DialWithDialer timed out" }
  72. func (TimeoutError) Timeout() bool { return true }
  73. func (TimeoutError) Temporary() bool { return true }
  74. // DialWithDialer connects to the given network address using dialer.Dial and
  75. // then initiates a TLS handshake, returning the resulting TLS connection. Any
  76. // timeout or deadline given in the dialer apply to connection and TLS
  77. // handshake as a whole.
  78. //
  79. // DialWithDialer interprets a nil configuration as equivalent to the zero
  80. // configuration; see the documentation of Config for the defaults.
  81. func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*Conn, error) {
  82. if config != nil && config.NonBlocking {
  83. return nil, errors.New("dialing not possible in non-blocking mode")
  84. }
  85. // We want the Timeout and Deadline values from dialer to cover the
  86. // whole process: TCP connection and TLS handshake. This means that we
  87. // also need to start our own timers now.
  88. timeout := dialer.Timeout
  89. if !dialer.Deadline.IsZero() {
  90. deadlineTimeout := dialer.Deadline.Sub(time.Now())
  91. if timeout == 0 || deadlineTimeout < timeout {
  92. timeout = deadlineTimeout
  93. }
  94. }
  95. var errChannel chan error
  96. if timeout != 0 {
  97. errChannel = make(chan error, 2)
  98. time.AfterFunc(timeout, func() {
  99. errChannel <- TimeoutError{}
  100. })
  101. }
  102. rawConn, err := dialer.Dial(network, addr)
  103. if err != nil {
  104. return nil, err
  105. }
  106. colonPos := strings.LastIndex(addr, ":")
  107. if colonPos == -1 {
  108. colonPos = len(addr)
  109. }
  110. hostname := addr[:colonPos]
  111. if config == nil {
  112. config = &Config{}
  113. } else {
  114. config = config.Clone()
  115. }
  116. // If no ServerName is set, infer the ServerName
  117. // from the hostname we're connecting to.
  118. if config.ServerName == "" {
  119. config.ServerName = hostname
  120. }
  121. // Set up DTLS as needed.
  122. config.UseDTLS = (network == "udp")
  123. conn := Client(rawConn, config)
  124. if timeout == 0 {
  125. err = conn.Handshake()
  126. if err == AlertNoAlert {
  127. err = nil
  128. }
  129. } else {
  130. go func() {
  131. errChannel <- conn.Handshake()
  132. }()
  133. err = <-errChannel
  134. if err == AlertNoAlert {
  135. err = nil
  136. }
  137. }
  138. if err != nil {
  139. rawConn.Close()
  140. return nil, err
  141. }
  142. return conn, nil
  143. }
  144. // Dial connects to the given network address using net.Dial
  145. // and then initiates a TLS handshake, returning the resulting
  146. // TLS connection.
  147. // Dial interprets a nil configuration as equivalent to
  148. // the zero configuration; see the documentation of Config
  149. // for the defaults.
  150. func Dial(network, addr string, config *Config) (*Conn, error) {
  151. return DialWithDialer(new(net.Dialer), network, addr, config)
  152. }