| 1 |
|
| /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ |
| 2 |
|
| /* |
| 3 |
|
| * Portions created by Alan Antonuk are Copyright (c) 2012-2014 Alan Antonuk. |
| 4 |
|
| * All Rights Reserved. |
| 5 |
|
| * |
| 6 |
|
| * Portions created by Michael Steinert are Copyright (c) 2012-2014 Michael |
| 7 |
|
| * Steinert. All Rights Reserved. |
| 8 |
|
| * |
| 9 |
|
| * Permission is hereby granted, free of charge, to any person obtaining a |
| 10 |
|
| * copy of this software and associated documentation files (the "Software"), |
| 11 |
|
| * to deal in the Software without restriction, including without limitation |
| 12 |
|
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 |
|
| * and/or sell copies of the Software, and to permit persons to whom the |
| 14 |
|
| * Software is furnished to do so, subject to the following conditions: |
| 15 |
|
| * |
| 16 |
|
| * The above copyright notice and this permission notice shall be included in |
| 17 |
|
| * all copies or substantial portions of the Software. |
| 18 |
|
| * |
| 19 |
|
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
|
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
|
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
|
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
|
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 |
|
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 25 |
|
| * DEALINGS IN THE SOFTWARE. |
| 26 |
|
| */ |
| 27 |
|
| |
| 28 |
|
| #ifdef HAVE_CONFIG_H |
| 29 |
|
| #include "config.h" |
| 30 |
|
| #endif |
| 31 |
|
| |
| 32 |
|
| #include "amqp_ssl_socket.h" |
| 33 |
|
| #include "amqp_socket.h" |
| 34 |
|
| #include "amqp_hostcheck.h" |
| 35 |
|
| #include "amqp_private.h" |
| 36 |
|
| #include "amqp_time.h" |
| 37 |
|
| #include "threads.h" |
| 38 |
|
| |
| 39 |
|
| #include <ctype.h> |
| 40 |
|
| #include <openssl/conf.h> |
| 41 |
|
| #include <openssl/err.h> |
| 42 |
|
| #include <openssl/ssl.h> |
| 43 |
|
| #include <openssl/x509v3.h> |
| 44 |
|
| #include <stdlib.h> |
| 45 |
|
| #include <string.h> |
| 46 |
|
| |
| 47 |
|
| |
| 48 |
|
| static int initialize_openssl(void); |
| 49 |
|
| static int destroy_openssl(void); |
| 50 |
|
| |
| 51 |
|
| static int open_ssl_connections = 0; |
| 52 |
|
| static amqp_boolean_t do_initialize_openssl = 1; |
| 53 |
|
| static amqp_boolean_t openssl_initialized = 0; |
| 54 |
|
| |
| 55 |
|
| #ifdef ENABLE_THREAD_SAFETY |
| 56 |
|
| static unsigned long amqp_ssl_threadid_callback(void); |
| 57 |
|
| static void amqp_ssl_locking_callback(int mode, int n, const char *file, int line); |
| 58 |
|
| |
| 59 |
|
| #ifdef _WIN32 |
| 60 |
|
| static long win32_create_mutex = 0; |
| 61 |
|
| static pthread_mutex_t openssl_init_mutex = NULL; |
| 62 |
|
| #else |
| 63 |
|
| static pthread_mutex_t openssl_init_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 64 |
|
| #endif |
| 65 |
|
| static pthread_mutex_t *amqp_openssl_lockarray = NULL; |
| 66 |
|
| #endif /* ENABLE_THREAD_SAFETY */ |
| 67 |
|
| |
| 68 |
|
| struct amqp_ssl_socket_t { |
| 69 |
|
| const struct amqp_socket_class_t *klass; |
| 70 |
|
| SSL_CTX *ctx; |
| 71 |
|
| int sockfd; |
| 72 |
|
| SSL *ssl; |
| 73 |
|
| amqp_boolean_t verify; |
| 74 |
|
| int internal_error; |
| 75 |
|
| }; |
| 76 |
|
| |
| 77 |
1 |
| static ssize_t amqp_ssl_socket_send(void *base, const void *buf, size_t len, |
| 78 |
|
| AMQP_UNUSED int flags) { |
| 79 |
1 |
| struct amqp_ssl_socket_t *self = (struct amqp_ssl_socket_t *)base; |
| 80 |
|
| ssize_t res; |
| 81 |
1 |
▶ | if (-1 == self->sockfd) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (-1 == self->sockfd) { |
1 |
0 |
1 |
|
| 82 |
1 |
| return AMQP_STATUS_SOCKET_CLOSED; |
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
0 |
| ERR_clear_error(); |
| 86 |
0 |
| self->internal_error = 0; |
| 87 |
|
| |
| 88 |
|
| /* This will only return on error, or once the whole buffer has been |
| 89 |
|
| * written to the SSL stream. See SSL_MODE_ENABLE_PARTIAL_WRITE */ |
| 90 |
0 |
| res = SSL_write(self->ssl, buf, len); |
| 91 |
0 |
▶ | if (0 >= res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 >= res) { |
0 |
0 |
0 |
|
| 92 |
0 |
| self->internal_error = SSL_get_error(self->ssl, res); |
| 93 |
|
| /* TODO: Close connection if it isn't already? */ |
| 94 |
|
| /* TODO: Possibly be more intelligent in reporting WHAT went wrong */ |
| 95 |
0 |
| switch (self->internal_error) { |
| 96 |
0 |
| case SSL_ERROR_WANT_READ: |
| 97 |
0 |
| res = AMQP_PRIVATE_STATUS_SOCKET_NEEDREAD; |
| 98 |
0 |
| break; |
| 99 |
0 |
| case SSL_ERROR_WANT_WRITE: |
| 100 |
0 |
| res = AMQP_PRIVATE_STATUS_SOCKET_NEEDWRITE; |
| 101 |
0 |
| break; |
| 102 |
0 |
| case SSL_ERROR_ZERO_RETURN: |
| 103 |
0 |
| res = AMQP_STATUS_CONNECTION_CLOSED; |
| 104 |
0 |
| break; |
| 105 |
0 |
| default: |
| 106 |
0 |
| res = AMQP_STATUS_SSL_ERROR; |
| 107 |
0 |
| break; |
| 108 |
|
| } |
| 109 |
|
| } else { |
| 110 |
0 |
| self->internal_error = 0; |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
0 |
| return res; |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
|
| static ssize_t |
| 117 |
0 |
| amqp_ssl_socket_recv(void *base, |
| 118 |
|
| void *buf, |
| 119 |
|
| size_t len, |
| 120 |
|
| AMQP_UNUSED int flags) |
| 121 |
|
| { |
| 122 |
0 |
| struct amqp_ssl_socket_t *self = (struct amqp_ssl_socket_t *)base; |
| 123 |
|
| ssize_t received; |
| 124 |
0 |
▶ | if (-1 == self->sockfd) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (-1 == self->sockfd) { |
0 |
0 |
0 |
|
| 125 |
0 |
| return AMQP_STATUS_SOCKET_CLOSED; |
| 126 |
|
| } |
| 127 |
0 |
| ERR_clear_error(); |
| 128 |
0 |
| self->internal_error = 0; |
| 129 |
|
| |
| 130 |
0 |
| received = SSL_read(self->ssl, buf, len); |
| 131 |
0 |
▶ | if (0 >= received) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 >= received) { |
0 |
0 |
0 |
|
| 132 |
0 |
| self->internal_error = SSL_get_error(self->ssl, received); |
| 133 |
0 |
| switch (self->internal_error) { |
| 134 |
0 |
| case SSL_ERROR_WANT_READ: |
| 135 |
0 |
| received = AMQP_PRIVATE_STATUS_SOCKET_NEEDREAD; |
| 136 |
0 |
| break; |
| 137 |
0 |
| case SSL_ERROR_WANT_WRITE: |
| 138 |
0 |
| received = AMQP_PRIVATE_STATUS_SOCKET_NEEDWRITE; |
| 139 |
0 |
| break; |
| 140 |
0 |
| case SSL_ERROR_ZERO_RETURN: |
| 141 |
0 |
| received = AMQP_STATUS_CONNECTION_CLOSED; |
| 142 |
0 |
| break; |
| 143 |
0 |
| default: |
| 144 |
0 |
| received = AMQP_STATUS_SSL_ERROR; |
| 145 |
0 |
| break; |
| 146 |
|
| } |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
0 |
| return received; |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
0 |
| static int match(ASN1_STRING *entry_string, const char *string) |
| 153 |
|
| { |
| 154 |
0 |
| unsigned char *utf8_value = NULL, *cp, ch; |
| 155 |
0 |
| int utf8_length, status = 1; |
| 156 |
0 |
| utf8_length = ASN1_STRING_to_UTF8(&utf8_value, entry_string); |
| 157 |
0 |
▶ | if (0 > utf8_length) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 > utf8_length) { |
0 |
0 |
0 |
|
| 158 |
0 |
| goto error; |
| 159 |
|
| } |
| 160 |
0 |
▶ | while (utf8_length > 0 && utf8_value[utf8_length - 1] == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (utf8_length > 0 && utf8_value[utf8_length - 1] == 0) { |
0 |
0 |
0 |
| while (utf8_length > 0 && utf8_value[utf8_length - 1] == 0) { |
0 |
0 |
0 |
|
| 161 |
0 |
| --utf8_length; |
| 162 |
|
| } |
| 163 |
0 |
▶ | if (utf8_length >= 256) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (utf8_length >= 256) { |
0 |
0 |
0 |
|
| 164 |
0 |
| goto error; |
| 165 |
|
| } |
| 166 |
0 |
▶ | if ((size_t)utf8_length != strlen((char *)utf8_value)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)utf8_length != strlen((char *)utf8_value)) { |
0 |
0 |
0 |
|
| 167 |
0 |
| goto error; |
| 168 |
|
| } |
| 169 |
0 |
▶ | for (cp = utf8_value; (ch = *cp) != '\0'; ++cp) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (cp = utf8_value; (ch = *cp) != '\0'; ++cp) { |
0 |
0 |
0 |
|
| 170 |
0 |
▶ | if (isascii(ch) && !isprint(ch)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (isascii(ch) && !isprint(ch)) { |
0 |
0 |
0 |
| if (isascii(ch) && !isprint(ch)) { |
0 |
0 |
0 |
|
| 171 |
0 |
| goto error; |
| 172 |
|
| } |
| 173 |
|
| } |
| 174 |
0 |
▶ | if (!amqp_hostcheck((char *)utf8_value, string)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!amqp_hostcheck((char *)utf8_value, string)) { |
0 |
0 |
0 |
|
| 175 |
0 |
| goto error; |
| 176 |
|
| } |
| 177 |
0 |
| exit: |
| 178 |
0 |
| OPENSSL_free(utf8_value); |
| 179 |
0 |
| return status; |
| 180 |
0 |
| error: |
| 181 |
0 |
| status = 0; |
| 182 |
0 |
| goto exit; |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
|
| /* Does this hostname match an entry in the subjectAltName extension? |
| 186 |
|
| * returns: 0 if no, 1 if yes, -1 if no subjectAltName entries were found. |
| 187 |
|
| */ |
| 188 |
0 |
| static int hostname_matches_subject_alt_name(const char *hostname, X509 *cert) |
| 189 |
|
| { |
| 190 |
0 |
| int found_any_entries = 0; |
| 191 |
|
| int found_match; |
| 192 |
0 |
| GENERAL_NAME *namePart = NULL; |
| 193 |
|
| STACK_OF(GENERAL_NAME) *san = |
| 194 |
0 |
| (STACK_OF(GENERAL_NAME)*) X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 195 |
|
| |
| 196 |
0 |
▶ | while (sk_GENERAL_NAME_num(san) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while (sk_GENERAL_NAME_num(san) > 0) |
0 |
0 |
0 |
|
| 197 |
|
| { |
| 198 |
0 |
| namePart = sk_GENERAL_NAME_pop(san); |
| 199 |
|
| |
| 200 |
0 |
▶ | if (namePart->type == GEN_DNS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (namePart->type == GEN_DNS) { |
0 |
0 |
0 |
|
| 201 |
0 |
| found_any_entries = 1; |
| 202 |
0 |
| found_match = match(namePart->d.uniformResourceIdentifier, hostname); |
| 203 |
0 |
▶ | if (found_match) |
Branch: 1 branch
| Branch | True | False | Total |
| if (found_match) |
0 |
0 |
0 |
|
| 204 |
0 |
| return 1; |
| 205 |
|
| } |
| 206 |
|
| } |
| 207 |
|
| |
| 208 |
0 |
▶ | return (found_any_entries ? 0 : -1); |
Branch: 1 branch
| Branch | True | False | Total |
| return (found_any_entries ? 0 : -1); |
0 |
0 |
0 |
|
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
0 |
| static int hostname_matches_subject_common_name(const char *hostname, X509 *cert) |
| 212 |
|
| { |
| 213 |
|
| X509_NAME *name; |
| 214 |
|
| X509_NAME_ENTRY *name_entry; |
| 215 |
|
| int position; |
| 216 |
|
| ASN1_STRING *entry_string; |
| 217 |
|
| |
| 218 |
0 |
| name = X509_get_subject_name(cert); |
| 219 |
0 |
| position = -1; |
| 220 |
|
| for (;;) { |
| 221 |
0 |
| position = X509_NAME_get_index_by_NID(name, NID_commonName, position); |
| 222 |
0 |
▶ | if (position == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (position == -1) |
0 |
0 |
0 |
|
| 223 |
0 |
| break; |
| 224 |
0 |
| name_entry = X509_NAME_get_entry(name, position); |
| 225 |
0 |
| entry_string = X509_NAME_ENTRY_get_data(name_entry); |
| 226 |
0 |
▶ | if (match(entry_string, hostname)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (match(entry_string, hostname)) |
0 |
0 |
0 |
|
| 227 |
0 |
| return 1; |
| 228 |
|
| } |
| 229 |
0 |
| return 0; |
| 230 |
|
| } |
| 231 |
|
| |
| 232 |
|
| static int |
| 233 |
0 |
| amqp_ssl_socket_verify_hostname(void *base, const char *host) |
| 234 |
|
| { |
| 235 |
0 |
| struct amqp_ssl_socket_t *self = (struct amqp_ssl_socket_t *)base; |
| 236 |
0 |
| int status = 0; |
| 237 |
|
| X509 *cert; |
| 238 |
|
| int res; |
| 239 |
0 |
| cert = SSL_get_peer_certificate(self->ssl); |
| 240 |
0 |
▶ | if (!cert) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cert) { |
0 |
0 |
0 |
|
| 241 |
0 |
| goto error; |
| 242 |
|
| } |
| 243 |
0 |
| res = hostname_matches_subject_alt_name(host, cert); |
| 244 |
0 |
▶ | if (res != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res != 1) { |
0 |
0 |
0 |
|
| 245 |
0 |
| res = hostname_matches_subject_common_name(host, cert); |
| 246 |
0 |
▶ | if (!res) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) |
0 |
0 |
0 |
|
| 247 |
0 |
| goto error; |
| 248 |
|
| } |
| 249 |
0 |
| exit: |
| 250 |
0 |
| return status; |
| 251 |
0 |
| error: |
| 252 |
0 |
| status = -1; |
| 253 |
0 |
| goto exit; |
| 254 |
|
| } |
| 255 |
|
| |
| 256 |
|
| static int |
| 257 |
1 |
| amqp_ssl_socket_open(void *base, const char *host, int port, struct timeval *timeout) |
| 258 |
|
| { |
| 259 |
1 |
| struct amqp_ssl_socket_t *self = (struct amqp_ssl_socket_t *)base; |
| 260 |
|
| long result; |
| 261 |
|
| int status; |
| 262 |
|
| amqp_time_t deadline; |
| 263 |
1 |
▶ | if (-1 != self->sockfd) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (-1 != self->sockfd) { |
0 |
1 |
1 |
|
| 264 |
0 |
| return AMQP_STATUS_SOCKET_INUSE; |
| 265 |
|
| } |
| 266 |
1 |
| ERR_clear_error(); |
| 267 |
|
| |
| 268 |
1 |
| self->ssl = SSL_new(self->ctx); |
| 269 |
1 |
▶ | if (!self->ssl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ssl) { |
0 |
1 |
1 |
|
| 270 |
0 |
| self->internal_error = ERR_peek_error(); |
| 271 |
0 |
| status = AMQP_STATUS_SSL_ERROR; |
| 272 |
0 |
| goto exit; |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
1 |
| status = amqp_time_from_now(&deadline, timeout); |
| 276 |
1 |
▶ | if (AMQP_STATUS_OK != status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (AMQP_STATUS_OK != status) { |
0 |
1 |
1 |
|
| 277 |
0 |
| return status; |
| 278 |
|
| } |
| 279 |
|
| |
| 280 |
1 |
| self->sockfd = amqp_open_socket_inner(host, port, deadline); |
| 281 |
1 |
▶ | if (0 > self->sockfd) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 > self->sockfd) { |
1 |
0 |
1 |
|
| 282 |
1 |
| status = self->sockfd; |
| 283 |
1 |
| self->internal_error = amqp_os_socket_error(); |
| 284 |
1 |
| self->sockfd = -1; |
| 285 |
1 |
| goto error_out1; |
| 286 |
|
| } |
| 287 |
|
| |
| 288 |
0 |
| status = SSL_set_fd(self->ssl, self->sockfd); |
| 289 |
0 |
▶ | if (!status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!status) { |
0 |
0 |
0 |
|
| 290 |
0 |
| self->internal_error = SSL_get_error(self->ssl, status); |
| 291 |
0 |
| status = AMQP_STATUS_SSL_ERROR; |
| 292 |
0 |
| goto error_out2; |
| 293 |
|
| } |
| 294 |
|
| |
| 295 |
0 |
| start_connect: |
| 296 |
0 |
| status = SSL_connect(self->ssl); |
| 297 |
0 |
▶ | if (status != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (status != 1) { |
0 |
0 |
0 |
|
| 298 |
0 |
| self->internal_error = SSL_get_error(self->ssl, status); |
| 299 |
0 |
| switch (self->internal_error) { |
| 300 |
0 |
| case SSL_ERROR_WANT_READ: |
| 301 |
0 |
| status = amqp_poll(self->sockfd, AMQP_SF_POLLIN, deadline); |
| 302 |
0 |
| break; |
| 303 |
0 |
| case SSL_ERROR_WANT_WRITE: |
| 304 |
0 |
| status = amqp_poll(self->sockfd, AMQP_SF_POLLOUT, deadline); |
| 305 |
0 |
| break; |
| 306 |
0 |
| default: |
| 307 |
0 |
| status = AMQP_STATUS_SSL_CONNECTION_FAILED; |
| 308 |
|
| } |
| 309 |
0 |
▶ | if (AMQP_STATUS_OK == status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (AMQP_STATUS_OK == status) { |
0 |
0 |
0 |
|
| 310 |
0 |
| goto start_connect; |
| 311 |
|
| } |
| 312 |
0 |
| goto error_out2; |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
0 |
| result = SSL_get_verify_result(self->ssl); |
| 316 |
0 |
▶ | if (X509_V_OK != result) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (X509_V_OK != result) { |
0 |
0 |
0 |
|
| 317 |
0 |
| self->internal_error = result; |
| 318 |
0 |
| status = AMQP_STATUS_SSL_PEER_VERIFY_FAILED; |
| 319 |
0 |
| goto error_out3; |
| 320 |
|
| } |
| 321 |
0 |
▶ | if (self->verify) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->verify) { |
0 |
0 |
0 |
|
| 322 |
0 |
| int verify_status = amqp_ssl_socket_verify_hostname(self, host); |
| 323 |
0 |
▶ | if (verify_status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (verify_status) { |
0 |
0 |
0 |
|
| 324 |
0 |
| self->internal_error = 0; |
| 325 |
0 |
| status = AMQP_STATUS_SSL_HOSTNAME_VERIFY_FAILED; |
| 326 |
0 |
| goto error_out3; |
| 327 |
|
| } |
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
0 |
| self->internal_error = 0; |
| 331 |
0 |
| status = AMQP_STATUS_OK; |
| 332 |
|
| |
| 333 |
1 |
| exit: |
| 334 |
1 |
| return status; |
| 335 |
|
| |
| 336 |
0 |
| error_out3: |
| 337 |
0 |
| SSL_shutdown(self->ssl); |
| 338 |
0 |
| error_out2: |
| 339 |
0 |
| amqp_os_socket_close(self->sockfd); |
| 340 |
0 |
| self->sockfd = -1; |
| 341 |
1 |
| error_out1: |
| 342 |
1 |
| SSL_free(self->ssl); |
| 343 |
1 |
| self->ssl = NULL; |
| 344 |
1 |
| goto exit; |
| 345 |
|
| } |
| 346 |
|
| |
| 347 |
|
| static int |
| 348 |
1 |
| amqp_ssl_socket_close(void *base) |
| 349 |
|
| { |
| 350 |
|
| int res; |
| 351 |
1 |
| struct amqp_ssl_socket_t *self = (struct amqp_ssl_socket_t *)base; |
| 352 |
|
| |
| 353 |
1 |
▶ | if (-1 == self->sockfd) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (-1 == self->sockfd) { |
0 |
1 |
1 |
|
| 354 |
1 |
| return AMQP_STATUS_SOCKET_CLOSED; |
| 355 |
|
| } |
| 356 |
|
| |
| 357 |
0 |
| start_shutdown: |
| 358 |
0 |
| res = SSL_shutdown(self->ssl); |
| 359 |
0 |
▶ | if (0 == res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == res) { |
0 |
0 |
0 |
|
| 360 |
0 |
| goto start_shutdown; |
| 361 |
0 |
▶ | } else if (-1 == res) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (-1 == res) { |
0 |
0 |
0 |
|
| 362 |
0 |
| self->internal_error = SSL_get_error(self->ssl, res); |
| 363 |
0 |
| switch (self->internal_error) { |
| 364 |
0 |
| case SSL_ERROR_WANT_READ: |
| 365 |
0 |
| res = amqp_poll(self->sockfd, AMQP_SF_POLLIN, amqp_time_infinite()); |
| 366 |
0 |
| break; |
| 367 |
0 |
| case SSL_ERROR_WANT_WRITE: |
| 368 |
0 |
| res = amqp_poll(self->sockfd, AMQP_SF_POLLOUT, amqp_time_infinite()); |
| 369 |
0 |
| break; |
| 370 |
|
| } |
| 371 |
0 |
▶ | if (AMQP_STATUS_OK == res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (AMQP_STATUS_OK == res) { |
0 |
0 |
0 |
|
| 372 |
0 |
| goto start_shutdown; |
| 373 |
|
| } |
| 374 |
|
| /* Swallow errors in poll, just consider the connection dead */ |
| 375 |
|
| } |
| 376 |
0 |
| SSL_free(self->ssl); |
| 377 |
0 |
| self->ssl = NULL; |
| 378 |
|
| |
| 379 |
0 |
▶ | if (amqp_os_socket_close(self->sockfd)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (amqp_os_socket_close(self->sockfd)) { |
0 |
0 |
0 |
|
| 380 |
0 |
| return AMQP_STATUS_SOCKET_ERROR; |
| 381 |
|
| } |
| 382 |
0 |
| self->sockfd = -1; |
| 383 |
|
| |
| 384 |
0 |
| return AMQP_STATUS_OK; |
| 385 |
|
| } |
| 386 |
|
| |
| 387 |
|
| static int |
| 388 |
10 |
| amqp_ssl_socket_get_sockfd(void *base) |
| 389 |
|
| { |
| 390 |
10 |
| struct amqp_ssl_socket_t *self = (struct amqp_ssl_socket_t *)base; |
| 391 |
10 |
| return self->sockfd; |
| 392 |
|
| } |
| 393 |
|
| |
| 394 |
|
| static void |
| 395 |
1 |
| amqp_ssl_socket_delete(void *base) |
| 396 |
|
| { |
| 397 |
1 |
| struct amqp_ssl_socket_t *self = (struct amqp_ssl_socket_t *)base; |
| 398 |
|
| |
| 399 |
1 |
▶ | if (self) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self) { |
1 |
0 |
1 |
|
| 400 |
1 |
| amqp_ssl_socket_close(self); |
| 401 |
|
| |
| 402 |
1 |
| SSL_CTX_free(self->ctx); |
| 403 |
1 |
| free(self); |
| 404 |
|
| } |
| 405 |
1 |
| destroy_openssl(); |
| 406 |
1 |
| } |
| 407 |
|
| |
| 408 |
|
| static const struct amqp_socket_class_t amqp_ssl_socket_class = { |
| 409 |
|
| amqp_ssl_socket_send, /* send */ |
| 410 |
|
| amqp_ssl_socket_recv, /* recv */ |
| 411 |
|
| amqp_ssl_socket_open, /* open */ |
| 412 |
|
| amqp_ssl_socket_close, /* close */ |
| 413 |
|
| amqp_ssl_socket_get_sockfd, /* get_sockfd */ |
| 414 |
|
| amqp_ssl_socket_delete /* delete */ |
| 415 |
|
| }; |
| 416 |
|
| |
| 417 |
|
| amqp_socket_t * |
| 418 |
1 |
| amqp_ssl_socket_new(amqp_connection_state_t state) |
| 419 |
|
| { |
| 420 |
1 |
| struct amqp_ssl_socket_t *self = calloc(1, sizeof(*self)); |
| 421 |
|
| int status; |
| 422 |
1 |
▶ | if (!self) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self) { |
0 |
1 |
1 |
|
| 423 |
0 |
| return NULL; |
| 424 |
|
| } |
| 425 |
|
| |
| 426 |
1 |
| self->sockfd = -1; |
| 427 |
1 |
| self->klass = &amqp_ssl_socket_class; |
| 428 |
1 |
| self->verify = 1; |
| 429 |
|
| |
| 430 |
1 |
| status = initialize_openssl(); |
| 431 |
1 |
▶ | if (status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (status) { |
0 |
1 |
1 |
|
| 432 |
0 |
| goto error; |
| 433 |
|
| } |
| 434 |
|
| |
| 435 |
1 |
| self->ctx = SSL_CTX_new(SSLv23_client_method()); |
| 436 |
1 |
▶ | if (!self->ctx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ctx) { |
0 |
1 |
1 |
|
| 437 |
0 |
| goto error; |
| 438 |
|
| } |
| 439 |
|
| |
| 440 |
1 |
| amqp_set_socket(state, (amqp_socket_t *)self); |
| 441 |
|
| |
| 442 |
1 |
| return (amqp_socket_t *)self; |
| 443 |
0 |
| error: |
| 444 |
0 |
| amqp_ssl_socket_delete((amqp_socket_t *)self); |
| 445 |
0 |
| return NULL; |
| 446 |
|
| } |
| 447 |
|
| |
| 448 |
|
| int |
| 449 |
1 |
| amqp_ssl_socket_set_cacert(amqp_socket_t *base, |
| 450 |
|
| const char *cacert) |
| 451 |
|
| { |
| 452 |
|
| int status; |
| 453 |
|
| struct amqp_ssl_socket_t *self; |
| 454 |
1 |
▶ | if (base->klass != &amqp_ssl_socket_class) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base->klass != &amqp_ssl_socket_class) { |
0 |
1 |
1 |
|
| 455 |
0 |
| amqp_abort("<%p> is not of type amqp_ssl_socket_t", base); |
| 456 |
|
| } |
| 457 |
1 |
| self = (struct amqp_ssl_socket_t *)base; |
| 458 |
1 |
| status = SSL_CTX_load_verify_locations(self->ctx, cacert, NULL); |
| 459 |
1 |
▶ | if (1 != status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (1 != status) { |
0 |
1 |
1 |
|
| 460 |
0 |
| return AMQP_STATUS_SSL_ERROR; |
| 461 |
|
| } |
| 462 |
1 |
| return AMQP_STATUS_OK; |
| 463 |
|
| } |
| 464 |
|
| |
| 465 |
|
| int |
| 466 |
0 |
| amqp_ssl_socket_set_key(amqp_socket_t *base, |
| 467 |
|
| const char *cert, |
| 468 |
|
| const char *key) |
| 469 |
|
| { |
| 470 |
|
| int status; |
| 471 |
|
| struct amqp_ssl_socket_t *self; |
| 472 |
0 |
▶ | if (base->klass != &amqp_ssl_socket_class) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base->klass != &amqp_ssl_socket_class) { |
0 |
0 |
0 |
|
| 473 |
0 |
| amqp_abort("<%p> is not of type amqp_ssl_socket_t", base); |
| 474 |
|
| } |
| 475 |
0 |
| self = (struct amqp_ssl_socket_t *)base; |
| 476 |
0 |
| status = SSL_CTX_use_certificate_chain_file(self->ctx, cert); |
| 477 |
0 |
▶ | if (1 != status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (1 != status) { |
0 |
0 |
0 |
|
| 478 |
0 |
| return AMQP_STATUS_SSL_ERROR; |
| 479 |
|
| } |
| 480 |
0 |
| status = SSL_CTX_use_PrivateKey_file(self->ctx, key, |
| 481 |
|
| SSL_FILETYPE_PEM); |
| 482 |
0 |
▶ | if (1 != status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (1 != status) { |
0 |
0 |
0 |
|
| 483 |
0 |
| return AMQP_STATUS_SSL_ERROR; |
| 484 |
|
| } |
| 485 |
0 |
| return AMQP_STATUS_OK; |
| 486 |
|
| } |
| 487 |
|
| |
| 488 |
|
| static int |
| 489 |
0 |
| password_cb(AMQP_UNUSED char *buffer, |
| 490 |
|
| AMQP_UNUSED int length, |
| 491 |
|
| AMQP_UNUSED int rwflag, |
| 492 |
|
| AMQP_UNUSED void *user_data) |
| 493 |
|
| { |
| 494 |
0 |
| amqp_abort("rabbitmq-c does not support password protected keys"); |
| 495 |
|
| return 0; |
| 496 |
|
| } |
| 497 |
|
| |
| 498 |
|
| int |
| 499 |
0 |
| amqp_ssl_socket_set_key_buffer(amqp_socket_t *base, |
| 500 |
|
| const char *cert, |
| 501 |
|
| const void *key, |
| 502 |
|
| size_t n) |
| 503 |
|
| { |
| 504 |
0 |
| int status = AMQP_STATUS_OK; |
| 505 |
0 |
| BIO *buf = NULL; |
| 506 |
0 |
| RSA *rsa = NULL; |
| 507 |
|
| struct amqp_ssl_socket_t *self; |
| 508 |
0 |
▶ | if (base->klass != &amqp_ssl_socket_class) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base->klass != &amqp_ssl_socket_class) { |
0 |
0 |
0 |
|
| 509 |
0 |
| amqp_abort("<%p> is not of type amqp_ssl_socket_t", base); |
| 510 |
|
| } |
| 511 |
0 |
| self = (struct amqp_ssl_socket_t *)base; |
| 512 |
0 |
| status = SSL_CTX_use_certificate_chain_file(self->ctx, cert); |
| 513 |
0 |
▶ | if (1 != status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (1 != status) { |
0 |
0 |
0 |
|
| 514 |
0 |
| return AMQP_STATUS_SSL_ERROR; |
| 515 |
|
| } |
| 516 |
0 |
| buf = BIO_new_mem_buf((void *)key, n); |
| 517 |
0 |
▶ | if (!buf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) { |
0 |
0 |
0 |
|
| 518 |
0 |
| goto error; |
| 519 |
|
| } |
| 520 |
0 |
| rsa = PEM_read_bio_RSAPrivateKey(buf, NULL, password_cb, NULL); |
| 521 |
0 |
▶ | if (!rsa) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rsa) { |
0 |
0 |
0 |
|
| 522 |
0 |
| goto error; |
| 523 |
|
| } |
| 524 |
0 |
| status = SSL_CTX_use_RSAPrivateKey(self->ctx, rsa); |
| 525 |
0 |
▶ | if (1 != status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (1 != status) { |
0 |
0 |
0 |
|
| 526 |
0 |
| goto error; |
| 527 |
|
| } |
| 528 |
0 |
| exit: |
| 529 |
0 |
| BIO_vfree(buf); |
| 530 |
0 |
| RSA_free(rsa); |
| 531 |
0 |
| return status; |
| 532 |
0 |
| error: |
| 533 |
0 |
| status = AMQP_STATUS_SSL_ERROR; |
| 534 |
0 |
| goto exit; |
| 535 |
|
| } |
| 536 |
|
| |
| 537 |
|
| int |
| 538 |
0 |
| amqp_ssl_socket_set_cert(amqp_socket_t *base, |
| 539 |
|
| const char *cert) |
| 540 |
|
| { |
| 541 |
|
| int status; |
| 542 |
|
| struct amqp_ssl_socket_t *self; |
| 543 |
0 |
▶ | if (base->klass != &amqp_ssl_socket_class) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base->klass != &amqp_ssl_socket_class) { |
0 |
0 |
0 |
|
| 544 |
0 |
| amqp_abort("<%p> is not of type amqp_ssl_socket_t", base); |
| 545 |
|
| } |
| 546 |
0 |
| self = (struct amqp_ssl_socket_t *)base; |
| 547 |
0 |
| status = SSL_CTX_use_certificate_chain_file(self->ctx, cert); |
| 548 |
0 |
▶ | if (1 != status) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (1 != status) { |
0 |
0 |
0 |
|
| 549 |
0 |
| return AMQP_STATUS_SSL_ERROR; |
| 550 |
|
| } |
| 551 |
0 |
| return AMQP_STATUS_OK; |
| 552 |
|
| } |
| 553 |
|
| |
| 554 |
|
| void |
| 555 |
1 |
| amqp_ssl_socket_set_verify(amqp_socket_t *base, |
| 556 |
|
| amqp_boolean_t verify) |
| 557 |
|
| { |
| 558 |
|
| struct amqp_ssl_socket_t *self; |
| 559 |
1 |
▶ | if (base->klass != &amqp_ssl_socket_class) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base->klass != &amqp_ssl_socket_class) { |
0 |
1 |
1 |
|
| 560 |
0 |
| amqp_abort("<%p> is not of type amqp_ssl_socket_t", base); |
| 561 |
|
| } |
| 562 |
1 |
| self = (struct amqp_ssl_socket_t *)base; |
| 563 |
1 |
| self->verify = verify; |
| 564 |
1 |
| } |
| 565 |
|
| |
| 566 |
|
| void |
| 567 |
1 |
| amqp_set_initialize_ssl_library(amqp_boolean_t do_initialize) |
| 568 |
|
| { |
| 569 |
1 |
▶ | if (!openssl_initialized) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!openssl_initialized) { |
1 |
0 |
1 |
|
| 570 |
1 |
| do_initialize_openssl = do_initialize; |
| 571 |
|
| } |
| 572 |
1 |
| } |
| 573 |
|
| |
| 574 |
|
| #ifdef ENABLE_THREAD_SAFETY |
| 575 |
|
| unsigned long |
| 576 |
|
| amqp_ssl_threadid_callback(void) |
| 577 |
|
| { |
| 578 |
|
| return (unsigned long)pthread_self(); |
| 579 |
|
| } |
| 580 |
|
| |
| 581 |
|
| void |
| 582 |
|
| amqp_ssl_locking_callback(int mode, int n, |
| 583 |
|
| AMQP_UNUSED const char *file, |
| 584 |
|
| AMQP_UNUSED int line) |
| 585 |
|
| { |
| 586 |
|
| if (mode & CRYPTO_LOCK) { |
| 587 |
|
| if (pthread_mutex_lock(&amqp_openssl_lockarray[n])) { |
| 588 |
|
| amqp_abort("Runtime error: Failure in trying to lock OpenSSL mutex"); |
| 589 |
|
| } |
| 590 |
|
| } else { |
| 591 |
|
| if (pthread_mutex_unlock(&amqp_openssl_lockarray[n])) { |
| 592 |
|
| amqp_abort("Runtime error: Failure in trying to unlock OpenSSL mutex"); |
| 593 |
|
| } |
| 594 |
|
| } |
| 595 |
|
| } |
| 596 |
|
| #endif /* ENABLE_THREAD_SAFETY */ |
| 597 |
|
| |
| 598 |
|
| static int |
| 599 |
1 |
| initialize_openssl(void) |
| 600 |
|
| { |
| 601 |
|
| #ifdef ENABLE_THREAD_SAFETY |
| 602 |
|
| #ifdef _WIN32 |
| 603 |
|
| /* No such thing as PTHREAD_INITIALIZE_MUTEX macro on Win32, so we use this */ |
| 604 |
|
| if (NULL == openssl_init_mutex) { |
| 605 |
|
| while (InterlockedExchange(&win32_create_mutex, 1) == 1) |
| 606 |
|
| /* Loop, someone else is holding this lock */ ; |
| 607 |
|
| |
| 608 |
|
| if (NULL == openssl_init_mutex) { |
| 609 |
|
| if (pthread_mutex_init(&openssl_init_mutex, NULL)) { |
| 610 |
|
| return -1; |
| 611 |
|
| } |
| 612 |
|
| } |
| 613 |
|
| InterlockedExchange(&win32_create_mutex, 0); |
| 614 |
|
| } |
| 615 |
|
| #endif /* _WIN32 */ |
| 616 |
|
| |
| 617 |
|
| if (pthread_mutex_lock(&openssl_init_mutex)) { |
| 618 |
|
| return -1; |
| 619 |
|
| } |
| 620 |
|
| #endif /* ENABLE_THREAD_SAFETY */ |
| 621 |
1 |
▶ | if (do_initialize_openssl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (do_initialize_openssl) { |
1 |
0 |
1 |
|
| 622 |
|
| #ifdef ENABLE_THREAD_SAFETY |
| 623 |
|
| if (NULL == amqp_openssl_lockarray) { |
| 624 |
|
| int i = 0; |
| 625 |
|
| amqp_openssl_lockarray = calloc(CRYPTO_num_locks(), sizeof(pthread_mutex_t)); |
| 626 |
|
| if (!amqp_openssl_lockarray) { |
| 627 |
|
| pthread_mutex_unlock(&openssl_init_mutex); |
| 628 |
|
| return -1; |
| 629 |
|
| } |
| 630 |
|
| for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 631 |
|
| if (pthread_mutex_init(&amqp_openssl_lockarray[i], NULL)) { |
| 632 |
|
| free(amqp_openssl_lockarray); |
| 633 |
|
| amqp_openssl_lockarray = NULL; |
| 634 |
|
| pthread_mutex_unlock(&openssl_init_mutex); |
| 635 |
|
| return -1; |
| 636 |
|
| } |
| 637 |
|
| } |
| 638 |
|
| } |
| 639 |
|
| |
| 640 |
|
| if (0 == open_ssl_connections) { |
| 641 |
|
| CRYPTO_set_id_callback(amqp_ssl_threadid_callback); |
| 642 |
|
| CRYPTO_set_locking_callback(amqp_ssl_locking_callback); |
| 643 |
|
| } |
| 644 |
|
| #endif /* ENABLE_THREAD_SAFETY */ |
| 645 |
|
| |
| 646 |
1 |
▶ | if (!openssl_initialized) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!openssl_initialized) { |
1 |
0 |
1 |
|
| 647 |
1 |
| OPENSSL_config(NULL); |
| 648 |
|
| |
| 649 |
1 |
| SSL_library_init(); |
| 650 |
1 |
| SSL_load_error_strings(); |
| 651 |
|
| |
| 652 |
1 |
| openssl_initialized = 1; |
| 653 |
|
| } |
| 654 |
|
| } |
| 655 |
|
| |
| 656 |
1 |
| ++open_ssl_connections; |
| 657 |
|
| |
| 658 |
|
| #ifdef ENABLE_THREAD_SAFETY |
| 659 |
|
| pthread_mutex_unlock(&openssl_init_mutex); |
| 660 |
|
| #endif /* ENABLE_THREAD_SAFETY */ |
| 661 |
1 |
| return 0; |
| 662 |
|
| } |
| 663 |
|
| |
| 664 |
|
| static int |
| 665 |
1 |
| destroy_openssl(void) |
| 666 |
|
| { |
| 667 |
|
| #ifdef ENABLE_THREAD_SAFETY |
| 668 |
|
| if (pthread_mutex_lock(&openssl_init_mutex)) { |
| 669 |
|
| return -1; |
| 670 |
|
| } |
| 671 |
|
| #endif /* ENABLE_THREAD_SAFETY */ |
| 672 |
|
| |
| 673 |
1 |
▶ | if (open_ssl_connections > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (open_ssl_connections > 0) { |
1 |
0 |
1 |
|
| 674 |
1 |
| --open_ssl_connections; |
| 675 |
|
| } |
| 676 |
|
| |
| 677 |
|
| #ifdef ENABLE_THREAD_SAFETY |
| 678 |
|
| if (0 == open_ssl_connections && do_initialize_openssl) { |
| 679 |
|
| /* Unsetting these allows the rabbitmq-c library to be unloaded |
| 680 |
|
| * safely. We do leak the amqp_openssl_lockarray. Which is only |
| 681 |
|
| * an issue if you repeatedly unload and load the library |
| 682 |
|
| */ |
| 683 |
|
| CRYPTO_set_locking_callback(NULL); |
| 684 |
|
| CRYPTO_set_id_callback(NULL); |
| 685 |
|
| } |
| 686 |
|
| |
| 687 |
|
| pthread_mutex_unlock(&openssl_init_mutex); |
| 688 |
|
| #endif /* ENABLE_THREAD_SAFETY */ |
| 689 |
1 |
| return 0; |
| 690 |
|
| } |