net.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (C) 2017. See AUTHORS.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package openssl
  15. import (
  16. "errors"
  17. "net"
  18. )
  19. type listener struct {
  20. net.Listener
  21. ctx *Ctx
  22. }
  23. func (l *listener) Accept() (c net.Conn, err error) {
  24. c, err = l.Listener.Accept()
  25. if err != nil {
  26. return nil, err
  27. }
  28. ssl_c, err := Server(c, l.ctx)
  29. if err != nil {
  30. c.Close()
  31. return nil, err
  32. }
  33. return ssl_c, nil
  34. }
  35. // NewListener wraps an existing net.Listener such that all accepted
  36. // connections are wrapped as OpenSSL server connections using the provided
  37. // context ctx.
  38. func NewListener(inner net.Listener, ctx *Ctx) net.Listener {
  39. return &listener{
  40. Listener: inner,
  41. ctx: ctx}
  42. }
  43. // Listen is a wrapper around net.Listen that wraps incoming connections with
  44. // an OpenSSL server connection using the provided context ctx.
  45. func Listen(network, laddr string, ctx *Ctx) (net.Listener, error) {
  46. if ctx == nil {
  47. return nil, errors.New("no ssl context provided")
  48. }
  49. l, err := net.Listen(network, laddr)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return NewListener(l, ctx), nil
  54. }
  55. type DialFlags int
  56. const (
  57. InsecureSkipHostVerification DialFlags = 1 << iota
  58. DisableSNI
  59. )
  60. // Dial will connect to network/address and then wrap the corresponding
  61. // underlying connection with an OpenSSL client connection using context ctx.
  62. // If flags includes InsecureSkipHostVerification, the server certificate's
  63. // hostname will not be checked to match the hostname in addr. Otherwise, flags
  64. // should be 0.
  65. //
  66. // Dial probably won't work for you unless you set a verify location or add
  67. // some certs to the certificate store of the client context you're using.
  68. // This library is not nice enough to use the system certificate store by
  69. // default for you yet.
  70. func Dial(network, addr string, ctx *Ctx, flags DialFlags) (*Conn, error) {
  71. return DialSession(network, addr, ctx, flags, nil)
  72. }
  73. // DialSession will connect to network/address and then wrap the corresponding
  74. // underlying connection with an OpenSSL client connection using context ctx.
  75. // If flags includes InsecureSkipHostVerification, the server certificate's
  76. // hostname will not be checked to match the hostname in addr. Otherwise, flags
  77. // should be 0.
  78. //
  79. // Dial probably won't work for you unless you set a verify location or add
  80. // some certs to the certificate store of the client context you're using.
  81. // This library is not nice enough to use the system certificate store by
  82. // default for you yet.
  83. //
  84. // If session is not nil it will be used to resume the tls state. The session
  85. // can be retrieved from the GetSession method on the Conn.
  86. func DialSession(network, addr string, ctx *Ctx, flags DialFlags,
  87. session []byte) (*Conn, error) {
  88. host, _, err := net.SplitHostPort(addr)
  89. if err != nil {
  90. return nil, err
  91. }
  92. if ctx == nil {
  93. var err error
  94. ctx, err = NewCtx()
  95. if err != nil {
  96. return nil, err
  97. }
  98. // TODO: use operating system default certificate chain?
  99. }
  100. c, err := net.Dial(network, addr)
  101. if err != nil {
  102. return nil, err
  103. }
  104. conn, err := Client(c, ctx)
  105. if err != nil {
  106. c.Close()
  107. return nil, err
  108. }
  109. if session != nil {
  110. err := conn.setSession(session)
  111. if err != nil {
  112. c.Close()
  113. return nil, err
  114. }
  115. }
  116. if flags&DisableSNI == 0 {
  117. err = conn.SetTlsExtHostName(host)
  118. if err != nil {
  119. conn.Close()
  120. return nil, err
  121. }
  122. }
  123. err = conn.Handshake()
  124. if err != nil {
  125. conn.Close()
  126. return nil, err
  127. }
  128. if flags&InsecureSkipHostVerification == 0 {
  129. err = conn.VerifyHostname(host)
  130. if err != nil {
  131. conn.Close()
  132. return nil, err
  133. }
  134. }
  135. return conn, nil
  136. }