openssl_ssl_util.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_SSL_OPENSSL_SSL_UTIL_H_
  5. #define NET_SSL_OPENSSL_SSL_UTIL_H_
  6. #include <stdint.h>
  7. #include "net/base/net_export.h"
  8. #include "net/cert/x509_certificate.h"
  9. #include "net/log/net_log_event_type.h"
  10. #include "third_party/boringssl/src/include/openssl/base.h"
  11. namespace crypto {
  12. class OpenSSLErrStackTracer;
  13. }
  14. namespace base {
  15. class Location;
  16. }
  17. namespace net {
  18. class NetLogWithSource;
  19. // Puts a net error, |err|, on the error stack in OpenSSL. The file and line are
  20. // extracted from |posted_from|. The function code of the error is left as 0.
  21. void OpenSSLPutNetError(const base::Location& posted_from, int err);
  22. // Utility to construct the appropriate set & clear masks for use the OpenSSL
  23. // options and mode configuration functions. (SSL_set_options etc)
  24. struct SslSetClearMask {
  25. SslSetClearMask();
  26. void ConfigureFlag(long flag, bool state);
  27. long set_mask = 0;
  28. long clear_mask = 0;
  29. };
  30. // Converts an OpenSSL error code into a net error code, walking the OpenSSL
  31. // error stack if needed.
  32. //
  33. // Note that |tracer| is not currently used in the implementation, but is passed
  34. // in anyway as this ensures the caller will clear any residual codes left on
  35. // the error stack.
  36. NET_EXPORT_PRIVATE int MapOpenSSLError(
  37. int err,
  38. const crypto::OpenSSLErrStackTracer& tracer);
  39. // Helper struct to store information about an OpenSSL error stack entry.
  40. struct OpenSSLErrorInfo {
  41. OpenSSLErrorInfo() = default;
  42. uint32_t error_code = 0;
  43. const char* file = nullptr;
  44. int line = 0;
  45. };
  46. // Converts an OpenSSL error code into a net error code, walking the OpenSSL
  47. // error stack if needed. If a value on the stack is used, the error code and
  48. // associated information are returned in |*out_error_info|. Otherwise its
  49. // fields are set to 0 and NULL. This function will never return OK, so
  50. // SSL_ERROR_ZERO_RETURN must be handled externally.
  51. //
  52. // Note that |tracer| is not currently used in the implementation, but is passed
  53. // in anyway as this ensures the caller will clear any residual codes left on
  54. // the error stack.
  55. int MapOpenSSLErrorWithDetails(int err,
  56. const crypto::OpenSSLErrStackTracer& tracer,
  57. OpenSSLErrorInfo* out_error_info);
  58. // Logs an OpenSSL error to the NetLog.
  59. void NetLogOpenSSLError(const NetLogWithSource& net_log,
  60. NetLogEventType type,
  61. int net_error,
  62. int ssl_error,
  63. const OpenSSLErrorInfo& error_info);
  64. // Returns the net SSL version number (see ssl_connection_status_flags.h) for
  65. // this SSL connection.
  66. int GetNetSSLVersion(SSL* ssl);
  67. // Configures |ssl| to send the specified certificate and either |pkey| or
  68. // |custom_key|. This is a wrapper over |SSL_set_chain_and_key|.
  69. bool SetSSLChainAndKey(SSL* ssl,
  70. X509Certificate* cert,
  71. EVP_PKEY* pkey,
  72. const SSL_PRIVATE_KEY_METHOD* custom_key);
  73. } // namespace net
  74. #endif // NET_SSL_OPENSSL_SSL_UTIL_H_