init.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /*
  15. Package openssl is a light wrapper around OpenSSL for Go.
  16. It strives to provide a near-drop-in replacement for the Go standard library
  17. tls package, while allowing for:
  18. Performance
  19. OpenSSL is battle-tested and optimized C. While Go's built-in library shows
  20. great promise, it is still young and in some places, inefficient. This simple
  21. OpenSSL wrapper can often do at least 2x with the same cipher and protocol.
  22. On my lappytop, I get the following benchmarking speeds:
  23. BenchmarkSHA1Large_openssl 1000 2611282 ns/op 401.56 MB/s
  24. BenchmarkSHA1Large_stdlib 500 3963983 ns/op 264.53 MB/s
  25. BenchmarkSHA1Small_openssl 1000000 3476 ns/op 0.29 MB/s
  26. BenchmarkSHA1Small_stdlib 5000000 550 ns/op 1.82 MB/s
  27. BenchmarkSHA256Large_openssl 200 8085314 ns/op 129.69 MB/s
  28. BenchmarkSHA256Large_stdlib 100 18948189 ns/op 55.34 MB/s
  29. BenchmarkSHA256Small_openssl 1000000 4262 ns/op 0.23 MB/s
  30. BenchmarkSHA256Small_stdlib 1000000 1444 ns/op 0.69 MB/s
  31. BenchmarkOpenSSLThroughput 100000 21634 ns/op 47.33 MB/s
  32. BenchmarkStdlibThroughput 50000 58974 ns/op 17.36 MB/s
  33. Interoperability
  34. Many systems support OpenSSL with a variety of plugins and modules for things,
  35. such as hardware acceleration in embedded devices.
  36. Greater flexibility and configuration
  37. OpenSSL allows for far greater configuration of corner cases and backwards
  38. compatibility (such as support of SSLv2). You shouldn't be using SSLv2 if you
  39. can help but, but sometimes you can't help it.
  40. Security
  41. Yeah yeah, Heartbleed. But according to the author of the standard library's
  42. TLS implementation, Go's TLS library is vulnerable to timing attacks. And
  43. whether or not OpenSSL received the appropriate amount of scrutiny
  44. pre-Heartbleed, it sure is receiving it now.
  45. Usage
  46. Starting an HTTP server that uses OpenSSL is very easy. It's as simple as:
  47. log.Fatal(openssl.ListenAndServeTLS(
  48. ":8443", "my_server.crt", "my_server.key", myHandler))
  49. Getting a net.Listener that uses OpenSSL is also easy:
  50. ctx, err := openssl.NewCtxFromFiles("my_server.crt", "my_server.key")
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. l, err := openssl.Listen("tcp", ":7777", ctx)
  55. Making a client connection is straightforward too:
  56. ctx, err := NewCtx()
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. err = ctx.LoadVerifyLocations("/etc/ssl/certs/ca-certificates.crt", "")
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. conn, err := openssl.Dial("tcp", "localhost:7777", ctx, 0)
  65. Help wanted: To get this library to work with net/http's client, we
  66. had to fork net/http. It would be nice if an alternate http client library
  67. supported the generality needed to use OpenSSL instead of crypto/tls.
  68. */
  69. package openssl
  70. // #include "shim.h"
  71. import "C"
  72. import (
  73. "errors"
  74. "fmt"
  75. "strings"
  76. )
  77. func init() {
  78. if rc := C.X_shim_init(); rc != 0 {
  79. panic(fmt.Errorf("X_shim_init failed with %d", rc))
  80. }
  81. }
  82. // errorFromErrorQueue needs to run in the same OS thread as the operation
  83. // that caused the possible error
  84. func errorFromErrorQueue() error {
  85. var errs []string
  86. for {
  87. err := C.ERR_get_error()
  88. if err == 0 {
  89. break
  90. }
  91. errs = append(errs, fmt.Sprintf("%s:%s:%s",
  92. C.GoString(C.ERR_lib_error_string(err)),
  93. C.GoString(C.ERR_func_error_string(err)),
  94. C.GoString(C.ERR_reason_error_string(err))))
  95. }
  96. return errors.New(fmt.Sprintf("SSL errors: %s", strings.Join(errs, "\n")))
  97. }