| 1 | // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors. |
| 2 | // SPDX-License-Identifier: mit |
| 3 | |
| 4 | /** \file */ |
| 5 | |
| 6 | #ifndef RABBITMQ_C_SSL_SOCKET_H |
| 7 | #define RABBITMQ_C_SSL_SOCKET_H |
| 8 | |
| 9 | #include <rabbitmq-c/amqp.h> |
| 10 | #include <rabbitmq-c/export.h> |
| 11 | |
| 12 | AMQP_BEGIN_DECLS |
| 13 | |
| 14 | /** |
| 15 | * Create a new SSL/TLS socket object. |
| 16 | * |
| 17 | * The returned socket object is owned by the \ref amqp_connection_state_t |
| 18 | * object and will be destroyed when the state object is destroyed or a new |
| 19 | * socket object is created. |
| 20 | * |
| 21 | * If the socket object creation fails, the \ref amqp_connection_state_t object |
| 22 | * will not be changed. |
| 23 | * |
| 24 | * The object returned by this function can be retrieved from the |
| 25 | * amqp_connection_state_t object later using the amqp_get_socket() function. |
| 26 | * |
| 27 | * Calling this function may result in the underlying SSL library being |
| 28 | * initialized. |
| 29 | * \sa amqp_set_initialize_ssl_library() |
| 30 | * |
| 31 | * \param [in,out] state The connection object that owns the SSL/TLS socket |
| 32 | * \return A new socket object or NULL if an error occurred. |
| 33 | * |
| 34 | * \since v0.4.0 |
| 35 | */ |
| 36 | AMQP_EXPORT |
| 37 | amqp_socket_t *AMQP_CALL amqp_ssl_socket_new(amqp_connection_state_t state); |
| 38 | |
| 39 | /** |
| 40 | * Get the internal OpenSSL context. Caveat emptor. |
| 41 | * |
| 42 | * \param [in,out] self An SSL/TLS socket object. |
| 43 | * |
| 44 | * \return A pointer to the internal OpenSSL context. This should be cast to |
| 45 | * <tt>SSL_CTX*</tt>. |
| 46 | * |
| 47 | * \since v0.9.0 |
| 48 | */ |
| 49 | AMQP_EXPORT |
| 50 | void *AMQP_CALL amqp_ssl_socket_get_context(amqp_socket_t *self); |
| 51 | |
| 52 | /** |
| 53 | * Enable loading of the CA certificates from the default location. |
| 54 | * |
| 55 | * \param [in,out] self An SSL/TLS socket object. |
| 56 | * |
| 57 | * \return \ref AMQP_STATUS_OK on success an \ref amqp_status_enum value on |
| 58 | * failure. |
| 59 | * |
| 60 | * \since v0.14.0 |
| 61 | */ |
| 62 | AMQP_EXPORT |
| 63 | int AMQP_CALL amqp_ssl_socket_enable_default_verify_paths(amqp_socket_t *self); |
| 64 | |
| 65 | /** |
| 66 | * Set the CA certificate. |
| 67 | * |
| 68 | * \param [in,out] self An SSL/TLS socket object. |
| 69 | * \param [in] cacert Path to the CA cert file in PEM format. |
| 70 | * |
| 71 | * \return \ref AMQP_STATUS_OK on success an \ref amqp_status_enum value on |
| 72 | * failure. |
| 73 | * |
| 74 | * \since v0.4.0 |
| 75 | */ |
| 76 | AMQP_EXPORT |
| 77 | int AMQP_CALL amqp_ssl_socket_set_cacert(amqp_socket_t *self, |
| 78 | const char *cacert); |
| 79 | |
| 80 | /** |
| 81 | * Set the password of key in PEM format. |
| 82 | * |
| 83 | * \param [in,out] self An SSL/TLS socket object. |
| 84 | * \param [in] passwd The password of key in PEM format. |
| 85 | * |
| 86 | * \since v0.11.0 |
| 87 | */ |
| 88 | AMQP_EXPORT |
| 89 | void AMQP_CALL amqp_ssl_socket_set_key_passwd(amqp_socket_t *self, |
| 90 | const char *passwd); |
| 91 | |
| 92 | /** |
| 93 | * Set the client key. |
| 94 | * |
| 95 | * \param [in,out] self An SSL/TLS socket object. |
| 96 | * \param [in] cert Path to the client certificate in PEM foramt. |
| 97 | * \param [in] key Path to the client key in PEM format. |
| 98 | * |
| 99 | * \return \ref AMQP_STATUS_OK on success an \ref amqp_status_enum value on |
| 100 | * failure. |
| 101 | * |
| 102 | * \since v0.4.0 |
| 103 | */ |
| 104 | AMQP_EXPORT |
| 105 | int AMQP_CALL amqp_ssl_socket_set_key(amqp_socket_t *self, const char *cert, |
| 106 | const char *key); |
| 107 | |
| 108 | /** |
| 109 | * Set the client key use the engine. |
| 110 | * |
| 111 | * This function requires amqp_set_ssl_engine() has been called. |
| 112 | * |
| 113 | * \param [in,out] self An SSL/TLS socket object. |
| 114 | * \param [in] cert Path to the client certificate in PEM foramt. |
| 115 | * \param [in] the key ID. |
| 116 | * |
| 117 | * \return \ref AMQP_STATUS_OK on success an \ref amqp_status_enum value on |
| 118 | * failure. May return \ref AMQP_STATUS_SSL_UNIMPLEMENTED if OpenSSL does |
| 119 | * not support the ENGINE API. |
| 120 | * |
| 121 | * \since v0.11.0 |
| 122 | */ |
| 123 | AMQP_EXPORT |
| 124 | int AMQP_CALL amqp_ssl_socket_set_key_engine(amqp_socket_t *self, |
| 125 | const char *cert, const char *key); |
| 126 | |
| 127 | /** |
| 128 | * Set the client key from a buffer. |
| 129 | * |
| 130 | * \param [in,out] self An SSL/TLS socket object. |
| 131 | * \param [in] cert Path to the client certificate in PEM foramt. |
| 132 | * \param [in] key A buffer containing client key in PEM format. |
| 133 | * \param [in] n The length of the buffer. |
| 134 | * |
| 135 | * \return \ref AMQP_STATUS_OK on success an \ref amqp_status_enum value on |
| 136 | * failure. |
| 137 | * |
| 138 | * \since v0.4.0 |
| 139 | */ |
| 140 | AMQP_EXPORT |
| 141 | int AMQP_CALL amqp_ssl_socket_set_key_buffer(amqp_socket_t *self, |
| 142 | const char *cert, const void *key, |
| 143 | size_t n); |
| 144 | |
| 145 | /** |
| 146 | * Enable or disable peer verification. |
| 147 | * |
| 148 | * \deprecated use \amqp_ssl_socket_set_verify_peer and |
| 149 | * \amqp_ssl_socket_set_verify_hostname instead. |
| 150 | * |
| 151 | * If peer verification is enabled then the common name in the server |
| 152 | * certificate must match the server name. Peer verification is enabled by |
| 153 | * default. |
| 154 | * |
| 155 | * \param [in,out] self An SSL/TLS socket object. |
| 156 | * \param [in] verify Enable or disable peer verification. |
| 157 | * |
| 158 | * \since v0.4.0 |
| 159 | */ |
| 160 | AMQP_DEPRECATED_EXPORT void AMQP_CALL |
| 161 | amqp_ssl_socket_set_verify(amqp_socket_t *self, amqp_boolean_t verify); |
| 162 | |
| 163 | /** |
| 164 | * Enable or disable peer verification. |
| 165 | * |
| 166 | * Peer verification validates the certificate chain that is sent by the broker. |
| 167 | * Hostname validation is controlled by \amqp_ssl_socket_set_verify_peer. |
| 168 | * |
| 169 | * \param [in,out] self An SSL/TLS socket object. |
| 170 | * \param [in] verify enable or disable peer validation |
| 171 | * |
| 172 | * \since v0.8.0 |
| 173 | */ |
| 174 | AMQP_EXPORT |
| 175 | void AMQP_CALL amqp_ssl_socket_set_verify_peer(amqp_socket_t *self, |
| 176 | amqp_boolean_t verify); |
| 177 | |
| 178 | /** |
| 179 | * Enable or disable hostname verification. |
| 180 | * |
| 181 | * Hostname verification checks the broker cert for a CN or SAN that matches the |
| 182 | * hostname that amqp_socket_open() is presented. Peer verification is |
| 183 | * controlled by \amqp_ssl_socket_set_verify_peer |
| 184 | * |
| 185 | * \since v0.8.0 |
| 186 | */ |
| 187 | AMQP_EXPORT |
| 188 | void AMQP_CALL amqp_ssl_socket_set_verify_hostname(amqp_socket_t *self, |
| 189 | amqp_boolean_t verify); |
| 190 | |
| 191 | typedef enum { |
| 192 | AMQP_TLSv1 = 1, |
| 193 | AMQP_TLSv1_1 = 2, |
| 194 | AMQP_TLSv1_2 = 3, |
| 195 | AMQP_TLSv1_3 = 4, |
| 196 | AMQP_TLSvLATEST = 0xFFFF |
| 197 | } amqp_tls_version_t; |
| 198 | |
| 199 | /** |
| 200 | * Set min and max TLS versions. |
| 201 | * |
| 202 | * Set the oldest and newest acceptable TLS versions that are acceptable when |
| 203 | * connecting to the broker. Set min == max to restrict to just that |
| 204 | * version. |
| 205 | * |
| 206 | * As of v0.14.0 the defaults are TLS v1.2 and TLS v1.3. TLS v1.1 and lower are |
| 207 | * no longer supported. |
| 208 | * |
| 209 | * \param [in,out] self An SSL/TLS socket object. |
| 210 | * \param [in] min the minimum acceptable TLS version |
| 211 | * \param [in] max the maxmium acceptable TLS version |
| 212 | * \returns AMQP_STATUS_OK on success, AMQP_STATUS_UNSUPPORTED if OpenSSL does |
| 213 | * not support the requested TLS version, AMQP_STATUS_INVALID_PARAMETER if an |
| 214 | * invalid combination of parameters is passed. |
| 215 | * |
| 216 | * \since v0.8.0 |
| 217 | */ |
| 218 | AMQP_EXPORT |
| 219 | int AMQP_CALL amqp_ssl_socket_set_ssl_versions(amqp_socket_t *self, |
| 220 | amqp_tls_version_t min, |
| 221 | amqp_tls_version_t max); |
| 222 | |
| 223 | /** |
| 224 | * Sets whether rabbitmq-c will initialize OpenSSL. |
| 225 | * |
| 226 | * \deprecated Since v0.13.0 this is a no-op. OpenSSL automatically manages |
| 227 | * library initialization and uninitialization. |
| 228 | * |
| 229 | * OpenSSL requires a one-time initialization across a whole program, this sets |
| 230 | * whether or not rabbitmq-c will initialize the SSL library when the first call |
| 231 | * to amqp_ssl_socket_new() is made. You should call this function with |
| 232 | * do_init = 0 if the underlying SSL library is initialized somewhere else |
| 233 | * the program. |
| 234 | * |
| 235 | * Failing to initialize or double initialization of the SSL library will |
| 236 | * result in undefined behavior |
| 237 | * |
| 238 | * By default rabbitmq-c will initialize the underlying SSL library. |
| 239 | * |
| 240 | * NOTE: calling this function after the first socket has been opened with |
| 241 | * amqp_open_socket() will not have any effect. |
| 242 | * |
| 243 | * \param [in] do_initialize If 0 rabbitmq-c will not initialize the SSL |
| 244 | * library, otherwise rabbitmq-c will initialize the |
| 245 | * SSL library |
| 246 | * |
| 247 | * \since v0.4.0 |
| 248 | */ |
| 249 | AMQP_DEPRECATED_EXPORT |
| 250 | void AMQP_CALL amqp_set_initialize_ssl_library(amqp_boolean_t do_initialize); |
| 251 | |
| 252 | /** |
| 253 | * Initialize the underlying SSL/TLS library. |
| 254 | * |
| 255 | * \deprecated Since v0.13.0 this is a no-op. OpenSSL automatically manages |
| 256 | * library initialization and uninitialization. |
| 257 | * |
| 258 | * The OpenSSL library requires a one-time initialization across the whole |
| 259 | * program. |
| 260 | * |
| 261 | * This function unconditionally initializes OpenSSL so that rabbitmq-c may |
| 262 | * use it. |
| 263 | * |
| 264 | * This function is thread-safe, and may be called more than once. |
| 265 | * |
| 266 | * \return AMQP_STATUS_OK on success. |
| 267 | * |
| 268 | * \since v0.9.0 |
| 269 | */ |
| 270 | AMQP_DEPRECATED_EXPORT |
| 271 | int AMQP_CALL amqp_initialize_ssl_library(void); |
| 272 | |
| 273 | /** |
| 274 | * Set the engine for underlying SSL/TLS library. |
| 275 | * |
| 276 | * This function is thread-safe, and may be called more than once. |
| 277 | * |
| 278 | * This function requires amqp_initialize_ssl_library() or amqp_ssl_socket_new() |
| 279 | * has been called. |
| 280 | * |
| 281 | * \param [in] engine the engine ID |
| 282 | * \return AMQP_STATUS_OK on success. May return \ref AMQP_STATUS_SSL_UNIMPLEMENTED |
| 283 | * if OpenSSL does not support the ENGINE API. |
| 284 | * |
| 285 | * \since v0.11.0 |
| 286 | */ |
| 287 | AMQP_EXPORT |
| 288 | int amqp_set_ssl_engine(const char *engine); |
| 289 | |
| 290 | /** |
| 291 | * Uninitialize the underlying SSL/TLS library. |
| 292 | * |
| 293 | * \deprecated Since v0.13.0 this is a no-op. OpenSSL automatically manages |
| 294 | * library initialization and uninitialization. |
| 295 | * |
| 296 | * \return AMQP_STATUS_OK on success. |
| 297 | * |
| 298 | * \since v0.9.0 |
| 299 | */ |
| 300 | AMQP_DEPRECATED_EXPORT |
| 301 | int AMQP_CALL amqp_uninitialize_ssl_library(void); |
| 302 | |
| 303 | AMQP_END_DECLS |
| 304 | |
| 305 | #endif /* RABBITMQ_C_SSL_SOCKET_H */ |