ssl.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // #include "shim.h"
  16. import "C"
  17. import (
  18. "os"
  19. "unsafe"
  20. )
  21. type SSLTLSExtErr int
  22. const (
  23. SSLTLSExtErrOK SSLTLSExtErr = C.SSL_TLSEXT_ERR_OK
  24. SSLTLSExtErrAlertWarning SSLTLSExtErr = C.SSL_TLSEXT_ERR_ALERT_WARNING
  25. SSLTLSEXTErrAlertFatal SSLTLSExtErr = C.SSL_TLSEXT_ERR_ALERT_FATAL
  26. SSLTLSEXTErrNoAck SSLTLSExtErr = C.SSL_TLSEXT_ERR_NOACK
  27. )
  28. var (
  29. ssl_idx = C.X_SSL_new_index()
  30. )
  31. //export get_ssl_idx
  32. func get_ssl_idx() C.int {
  33. return ssl_idx
  34. }
  35. type SSL struct {
  36. ssl *C.SSL
  37. verify_cb VerifyCallback
  38. }
  39. //export go_ssl_verify_cb_thunk
  40. func go_ssl_verify_cb_thunk(p unsafe.Pointer, ok C.int, ctx *C.X509_STORE_CTX) C.int {
  41. defer func() {
  42. if err := recover(); err != nil {
  43. logger.Critf("openssl: verify callback panic'd: %v", err)
  44. os.Exit(1)
  45. }
  46. }()
  47. verify_cb := (*SSL)(p).verify_cb
  48. // set up defaults just in case verify_cb is nil
  49. if verify_cb != nil {
  50. store := &CertificateStoreCtx{ctx: ctx}
  51. if verify_cb(ok == 1, store) {
  52. ok = 1
  53. } else {
  54. ok = 0
  55. }
  56. }
  57. return ok
  58. }
  59. // Wrapper around SSL_get_servername. Returns server name according to rfc6066
  60. // http://tools.ietf.org/html/rfc6066.
  61. func (s *SSL) GetServername() string {
  62. return C.GoString(C.SSL_get_servername(s.ssl, C.TLSEXT_NAMETYPE_host_name))
  63. }
  64. // GetOptions returns SSL options. See
  65. // https://www.openssl.org/docs/ssl/SSL_CTX_set_options.html
  66. func (s *SSL) GetOptions() Options {
  67. return Options(C.X_SSL_get_options(s.ssl))
  68. }
  69. // SetOptions sets SSL options. See
  70. // https://www.openssl.org/docs/ssl/SSL_CTX_set_options.html
  71. func (s *SSL) SetOptions(options Options) Options {
  72. return Options(C.X_SSL_set_options(s.ssl, C.long(options)))
  73. }
  74. // ClearOptions clear SSL options. See
  75. // https://www.openssl.org/docs/ssl/SSL_CTX_set_options.html
  76. func (s *SSL) ClearOptions(options Options) Options {
  77. return Options(C.X_SSL_clear_options(s.ssl, C.long(options)))
  78. }
  79. // SetVerify controls peer verification settings. See
  80. // http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  81. func (s *SSL) SetVerify(options VerifyOptions, verify_cb VerifyCallback) {
  82. s.verify_cb = verify_cb
  83. if verify_cb != nil {
  84. C.SSL_set_verify(s.ssl, C.int(options), (*[0]byte)(C.X_SSL_verify_cb))
  85. } else {
  86. C.SSL_set_verify(s.ssl, C.int(options), nil)
  87. }
  88. }
  89. // SetVerifyMode controls peer verification setting. See
  90. // http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  91. func (s *SSL) SetVerifyMode(options VerifyOptions) {
  92. s.SetVerify(options, s.verify_cb)
  93. }
  94. // SetVerifyCallback controls peer verification setting. See
  95. // http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  96. func (s *SSL) SetVerifyCallback(verify_cb VerifyCallback) {
  97. s.SetVerify(s.VerifyMode(), verify_cb)
  98. }
  99. // GetVerifyCallback returns callback function. See
  100. // http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  101. func (s *SSL) GetVerifyCallback() VerifyCallback {
  102. return s.verify_cb
  103. }
  104. // VerifyMode returns peer verification setting. See
  105. // http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  106. func (s *SSL) VerifyMode() VerifyOptions {
  107. return VerifyOptions(C.SSL_get_verify_mode(s.ssl))
  108. }
  109. // SetVerifyDepth controls how many certificates deep the certificate
  110. // verification logic is willing to follow a certificate chain. See
  111. // https://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  112. func (s *SSL) SetVerifyDepth(depth int) {
  113. C.SSL_set_verify_depth(s.ssl, C.int(depth))
  114. }
  115. // GetVerifyDepth controls how many certificates deep the certificate
  116. // verification logic is willing to follow a certificate chain. See
  117. // https://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  118. func (s *SSL) GetVerifyDepth() int {
  119. return int(C.SSL_get_verify_depth(s.ssl))
  120. }
  121. // SetSSLCtx changes context to new one. Useful for Server Name Indication (SNI)
  122. // rfc6066 http://tools.ietf.org/html/rfc6066. See
  123. // http://stackoverflow.com/questions/22373332/serving-multiple-domains-in-one-box-with-sni
  124. func (s *SSL) SetSSLCtx(ctx *Ctx) {
  125. /*
  126. * SSL_set_SSL_CTX() only changes certs as of 1.0.0d
  127. * adjust other things we care about
  128. */
  129. C.SSL_set_SSL_CTX(s.ssl, ctx.ctx)
  130. }
  131. //export sni_cb_thunk
  132. func sni_cb_thunk(p unsafe.Pointer, con *C.SSL, ad unsafe.Pointer, arg unsafe.Pointer) C.int {
  133. defer func() {
  134. if err := recover(); err != nil {
  135. logger.Critf("openssl: verify callback sni panic'd: %v", err)
  136. os.Exit(1)
  137. }
  138. }()
  139. sni_cb := (*Ctx)(p).sni_cb
  140. s := &SSL{ssl: con}
  141. // This attaches a pointer to our SSL struct into the SNI callback.
  142. C.SSL_set_ex_data(s.ssl, get_ssl_idx(), unsafe.Pointer(s))
  143. // Note: this is ctx.sni_cb, not C.sni_cb
  144. return C.int(sni_cb(s))
  145. }