You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ssl_util.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef SRC_LIBUTIL_SSL_UTIL_H_
  17. #define SRC_LIBUTIL_SSL_UTIL_H_
  18. #include "config.h"
  19. #include "libutil/addr.h"
  20. #include "libutil/libev_helper.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. struct rspamd_ssl_connection;
  25. typedef void (*rspamd_ssl_handler_t)(int fd, short what, gpointer d);
  26. typedef void (*rspamd_ssl_error_handler_t)(gpointer d, GError *err);
  27. /**
  28. * Creates a new ssl connection data structure
  29. * @param ssl_ctx initialized SSL_CTX structure
  30. * @return opaque connection data
  31. */
  32. struct rspamd_ssl_connection *rspamd_ssl_connection_new(gpointer ssl_ctx,
  33. struct ev_loop *ev_base,
  34. gboolean verify_peer,
  35. const char *log_tag);
  36. /**
  37. * Connects SSL session using the specified (connected) FD
  38. * @param conn connection
  39. * @param fd fd to use
  40. * @param hostname hostname for SNI
  41. * @param ev event to use
  42. * @param tv timeout for connection
  43. * @param handler connected session handler
  44. * @param handler_data opaque data
  45. * @return TRUE if a session has been connected
  46. */
  47. gboolean rspamd_ssl_connect_fd(struct rspamd_ssl_connection *conn, int fd,
  48. const char *hostname, struct rspamd_io_ev *ev, ev_tstamp timeout,
  49. rspamd_ssl_handler_t handler, rspamd_ssl_error_handler_t err_handler,
  50. gpointer handler_data);
  51. /**
  52. * Restores SSL handlers for the existing ssl connection (e.g. after keepalive)
  53. * @param conn
  54. * @param handler
  55. * @param err_handler
  56. * @param handler_data
  57. */
  58. void rspamd_ssl_connection_restore_handlers(struct rspamd_ssl_connection *conn,
  59. rspamd_ssl_handler_t handler,
  60. rspamd_ssl_error_handler_t err_handler,
  61. gpointer handler_data,
  62. short ev_what);
  63. /**
  64. * Perform async read from SSL socket
  65. * @param conn
  66. * @param buf
  67. * @param buflen
  68. * @return
  69. */
  70. gssize rspamd_ssl_read(struct rspamd_ssl_connection *conn, gpointer buf,
  71. gsize buflen);
  72. /**
  73. * Perform async write to ssl buffer
  74. * @param conn
  75. * @param buf
  76. * @param buflen
  77. * @param ev
  78. * @param tv
  79. * @return
  80. */
  81. gssize rspamd_ssl_write(struct rspamd_ssl_connection *conn, gconstpointer buf,
  82. gsize buflen);
  83. /**
  84. * Emulate writev by copying iovec to a temporary buffer
  85. * @param conn
  86. * @param buf
  87. * @param buflen
  88. * @return
  89. */
  90. gssize rspamd_ssl_writev(struct rspamd_ssl_connection *conn, struct iovec *iov,
  91. gsize iovlen);
  92. /**
  93. * Removes connection data
  94. * @param conn
  95. */
  96. void rspamd_ssl_connection_free(struct rspamd_ssl_connection *conn);
  97. gpointer rspamd_init_ssl_ctx(void);
  98. gpointer rspamd_init_ssl_ctx_noverify(void);
  99. void rspamd_ssl_ctx_config(struct rspamd_config *cfg, gpointer ssl_ctx);
  100. void rspamd_ssl_ctx_free(gpointer ssl_ctx);
  101. void rspamd_openssl_maybe_init(void);
  102. #ifdef __cplusplus
  103. }
  104. #endif
  105. #endif /* SRC_LIBUTIL_SSL_UTIL_H_ */