net_mosq.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. /*
  2. Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #define _GNU_SOURCE
  15. #include "config.h"
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #ifndef WIN32
  22. #define _GNU_SOURCE
  23. #include <netdb.h>
  24. #include <netinet/tcp.h>
  25. #include <sys/socket.h>
  26. #include <unistd.h>
  27. #else
  28. #include <winsock2.h>
  29. #include <ws2tcpip.h>
  30. #endif
  31. #ifdef __ANDROID__
  32. #include <linux/in.h>
  33. #include <linux/in6.h>
  34. #include <sys/endian.h>
  35. #endif
  36. #ifdef HAVE_NETINET_IN_H
  37. # include <netinet/in.h>
  38. #endif
  39. #ifdef WITH_UNIX_SOCKETS
  40. # include <sys/un.h>
  41. #endif
  42. #ifdef __QNX__
  43. #include <net/netbyte.h>
  44. #endif
  45. #ifdef WITH_TLS
  46. #include <openssl/conf.h>
  47. #include <openssl/engine.h>
  48. #include <openssl/err.h>
  49. #include <openssl/ui.h>
  50. #include <tls_mosq.h>
  51. #endif
  52. #ifdef WITH_BROKER
  53. # include "mosquitto_broker_internal.h"
  54. # ifdef WITH_WEBSOCKETS
  55. # include <libwebsockets.h>
  56. # endif
  57. #else
  58. # include "read_handle.h"
  59. #endif
  60. #include "logging_mosq.h"
  61. #include "memory_mosq.h"
  62. #include "mqtt_protocol.h"
  63. #include "net_mosq.h"
  64. #include "time_mosq.h"
  65. #include "util_mosq.h"
  66. #ifdef WITH_TLS
  67. int tls_ex_index_mosq = -1;
  68. UI_METHOD *_ui_method = NULL;
  69. static bool is_tls_initialized = false;
  70. /* Functions taken from OpenSSL s_server/s_client */
  71. static int ui_open(UI *ui)
  72. {
  73. return UI_method_get_opener(UI_OpenSSL())(ui);
  74. }
  75. static int ui_read(UI *ui, UI_STRING *uis)
  76. {
  77. return UI_method_get_reader(UI_OpenSSL())(ui, uis);
  78. }
  79. static int ui_write(UI *ui, UI_STRING *uis)
  80. {
  81. return UI_method_get_writer(UI_OpenSSL())(ui, uis);
  82. }
  83. static int ui_close(UI *ui)
  84. {
  85. return UI_method_get_closer(UI_OpenSSL())(ui);
  86. }
  87. static void setup_ui_method(void)
  88. {
  89. _ui_method = UI_create_method("OpenSSL application user interface");
  90. UI_method_set_opener(_ui_method, ui_open);
  91. UI_method_set_reader(_ui_method, ui_read);
  92. UI_method_set_writer(_ui_method, ui_write);
  93. UI_method_set_closer(_ui_method, ui_close);
  94. }
  95. static void cleanup_ui_method(void)
  96. {
  97. if(_ui_method){
  98. UI_destroy_method(_ui_method);
  99. _ui_method = NULL;
  100. }
  101. }
  102. UI_METHOD *net__get_ui_method(void)
  103. {
  104. return _ui_method;
  105. }
  106. #endif
  107. int net__init(void)
  108. {
  109. #ifdef WIN32
  110. WSADATA wsaData;
  111. if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
  112. return MOSQ_ERR_UNKNOWN;
  113. }
  114. #endif
  115. #ifdef WITH_SRV
  116. ares_library_init(ARES_LIB_INIT_ALL);
  117. #endif
  118. return MOSQ_ERR_SUCCESS;
  119. }
  120. void net__cleanup(void)
  121. {
  122. #ifdef WITH_TLS
  123. # if OPENSSL_VERSION_NUMBER < 0x10100000L
  124. CRYPTO_cleanup_all_ex_data();
  125. ERR_free_strings();
  126. ERR_remove_thread_state(NULL);
  127. EVP_cleanup();
  128. # if !defined(OPENSSL_NO_ENGINE)
  129. ENGINE_cleanup();
  130. # endif
  131. is_tls_initialized = false;
  132. # endif
  133. CONF_modules_unload(1);
  134. cleanup_ui_method();
  135. #endif
  136. #ifdef WITH_SRV
  137. ares_library_cleanup();
  138. #endif
  139. #ifdef WIN32
  140. WSACleanup();
  141. #endif
  142. }
  143. #ifdef WITH_TLS
  144. void net__init_tls(void)
  145. {
  146. if(is_tls_initialized) return;
  147. # if OPENSSL_VERSION_NUMBER < 0x10100000L
  148. SSL_load_error_strings();
  149. SSL_library_init();
  150. OpenSSL_add_all_algorithms();
  151. # else
  152. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS \
  153. | OPENSSL_INIT_ADD_ALL_DIGESTS \
  154. | OPENSSL_INIT_LOAD_CONFIG, NULL);
  155. # endif
  156. #if !defined(OPENSSL_NO_ENGINE)
  157. ENGINE_load_builtin_engines();
  158. #endif
  159. setup_ui_method();
  160. if(tls_ex_index_mosq == -1){
  161. tls_ex_index_mosq = SSL_get_ex_new_index(0, "client context", NULL, NULL, NULL);
  162. }
  163. is_tls_initialized = true;
  164. }
  165. #endif
  166. /* Close a socket associated with a context and set it to -1.
  167. * Returns 1 on failure (context is NULL)
  168. * Returns 0 on success.
  169. */
  170. int net__socket_close(struct mosquitto *mosq)
  171. {
  172. int rc = 0;
  173. #ifdef WITH_BROKER
  174. struct mosquitto *mosq_found;
  175. #endif
  176. assert(mosq);
  177. #ifdef WITH_TLS
  178. #ifdef WITH_WEBSOCKETS
  179. if(!mosq->wsi)
  180. #endif
  181. {
  182. if(mosq->ssl){
  183. if(!SSL_in_init(mosq->ssl)){
  184. SSL_shutdown(mosq->ssl);
  185. }
  186. SSL_free(mosq->ssl);
  187. mosq->ssl = NULL;
  188. }
  189. }
  190. #endif
  191. #ifdef WITH_WEBSOCKETS
  192. if(mosq->wsi)
  193. {
  194. if(mosq->state != mosq_cs_disconnecting){
  195. mosquitto__set_state(mosq, mosq_cs_disconnect_ws);
  196. }
  197. lws_callback_on_writable(mosq->wsi);
  198. }else
  199. #endif
  200. {
  201. if(mosq->sock != INVALID_SOCKET){
  202. #ifdef WITH_BROKER
  203. HASH_FIND(hh_sock, db.contexts_by_sock, &mosq->sock, sizeof(mosq->sock), mosq_found);
  204. if(mosq_found){
  205. HASH_DELETE(hh_sock, db.contexts_by_sock, mosq_found);
  206. }
  207. #endif
  208. rc = COMPAT_CLOSE(mosq->sock);
  209. mosq->sock = INVALID_SOCKET;
  210. }
  211. }
  212. #ifdef WITH_BROKER
  213. if(mosq->listener){
  214. mosq->listener->client_count--;
  215. mosq->listener = NULL;
  216. }
  217. #endif
  218. return rc;
  219. }
  220. #ifdef FINAL_WITH_TLS_PSK
  221. static unsigned int psk_client_callback(SSL *ssl, const char *hint,
  222. char *identity, unsigned int max_identity_len,
  223. unsigned char *psk, unsigned int max_psk_len)
  224. {
  225. struct mosquitto *mosq;
  226. int len;
  227. UNUSED(hint);
  228. mosq = SSL_get_ex_data(ssl, tls_ex_index_mosq);
  229. if(!mosq) return 0;
  230. snprintf(identity, max_identity_len, "%s", mosq->tls_psk_identity);
  231. len = mosquitto__hex2bin(mosq->tls_psk, psk, (int)max_psk_len);
  232. if (len < 0) return 0;
  233. return (unsigned int)len;
  234. }
  235. #endif
  236. #if defined(WITH_BROKER) && defined(__GLIBC__) && defined(WITH_ADNS)
  237. /* Async connect, part 1 (dns lookup) */
  238. int net__try_connect_step1(struct mosquitto *mosq, const char *host)
  239. {
  240. int s;
  241. void *sevp = NULL;
  242. struct addrinfo *hints;
  243. if(mosq->adns){
  244. gai_cancel(mosq->adns);
  245. mosquitto__free((struct addrinfo *)mosq->adns->ar_request);
  246. mosquitto__free(mosq->adns);
  247. }
  248. mosq->adns = mosquitto__calloc(1, sizeof(struct gaicb));
  249. if(!mosq->adns){
  250. return MOSQ_ERR_NOMEM;
  251. }
  252. hints = mosquitto__calloc(1, sizeof(struct addrinfo));
  253. if(!hints){
  254. mosquitto__free(mosq->adns);
  255. mosq->adns = NULL;
  256. return MOSQ_ERR_NOMEM;
  257. }
  258. hints->ai_family = AF_UNSPEC;
  259. hints->ai_socktype = SOCK_STREAM;
  260. mosq->adns->ar_name = host;
  261. mosq->adns->ar_request = hints;
  262. s = getaddrinfo_a(GAI_NOWAIT, &mosq->adns, 1, sevp);
  263. if(s){
  264. errno = s;
  265. if(mosq->adns){
  266. mosquitto__free((struct addrinfo *)mosq->adns->ar_request);
  267. mosquitto__free(mosq->adns);
  268. mosq->adns = NULL;
  269. }
  270. return MOSQ_ERR_EAI;
  271. }
  272. return MOSQ_ERR_SUCCESS;
  273. }
  274. /* Async connect part 2, the connection. */
  275. int net__try_connect_step2(struct mosquitto *mosq, uint16_t port, mosq_sock_t *sock)
  276. {
  277. struct addrinfo *ainfo, *rp;
  278. int rc;
  279. ainfo = mosq->adns->ar_result;
  280. for(rp = ainfo; rp != NULL; rp = rp->ai_next){
  281. *sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  282. if(*sock == INVALID_SOCKET) continue;
  283. if(rp->ai_family == AF_INET){
  284. ((struct sockaddr_in *)rp->ai_addr)->sin_port = htons(port);
  285. }else if(rp->ai_family == AF_INET6){
  286. ((struct sockaddr_in6 *)rp->ai_addr)->sin6_port = htons(port);
  287. }else{
  288. COMPAT_CLOSE(*sock);
  289. *sock = INVALID_SOCKET;
  290. continue;
  291. }
  292. /* Set non-blocking */
  293. if(net__socket_nonblock(sock)){
  294. continue;
  295. }
  296. rc = connect(*sock, rp->ai_addr, rp->ai_addrlen);
  297. #ifdef WIN32
  298. errno = WSAGetLastError();
  299. #endif
  300. if(rc == 0 || errno == EINPROGRESS || errno == COMPAT_EWOULDBLOCK){
  301. if(rc < 0 && (errno == EINPROGRESS || errno == COMPAT_EWOULDBLOCK)){
  302. rc = MOSQ_ERR_CONN_PENDING;
  303. }
  304. /* Set non-blocking */
  305. if(net__socket_nonblock(sock)){
  306. continue;
  307. }
  308. break;
  309. }
  310. COMPAT_CLOSE(*sock);
  311. *sock = INVALID_SOCKET;
  312. }
  313. freeaddrinfo(mosq->adns->ar_result);
  314. mosq->adns->ar_result = NULL;
  315. mosquitto__free((struct addrinfo *)mosq->adns->ar_request);
  316. mosquitto__free(mosq->adns);
  317. mosq->adns = NULL;
  318. if(!rp){
  319. return MOSQ_ERR_ERRNO;
  320. }
  321. return rc;
  322. }
  323. #endif
  324. static int net__try_connect_tcp(const char *host, uint16_t port, mosq_sock_t *sock, const char *bind_address, bool blocking)
  325. {
  326. struct addrinfo hints;
  327. struct addrinfo *ainfo, *rp;
  328. struct addrinfo *ainfo_bind, *rp_bind;
  329. int s;
  330. int rc = MOSQ_ERR_SUCCESS;
  331. ainfo_bind = NULL;
  332. *sock = INVALID_SOCKET;
  333. memset(&hints, 0, sizeof(struct addrinfo));
  334. hints.ai_family = AF_UNSPEC;
  335. hints.ai_socktype = SOCK_STREAM;
  336. s = getaddrinfo(host, NULL, &hints, &ainfo);
  337. if(s){
  338. errno = s;
  339. return MOSQ_ERR_EAI;
  340. }
  341. if(bind_address){
  342. s = getaddrinfo(bind_address, NULL, &hints, &ainfo_bind);
  343. if(s){
  344. freeaddrinfo(ainfo);
  345. errno = s;
  346. return MOSQ_ERR_EAI;
  347. }
  348. }
  349. for(rp = ainfo; rp != NULL; rp = rp->ai_next){
  350. *sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  351. if(*sock == INVALID_SOCKET) continue;
  352. if(rp->ai_family == AF_INET){
  353. ((struct sockaddr_in *)rp->ai_addr)->sin_port = htons(port);
  354. }else if(rp->ai_family == AF_INET6){
  355. ((struct sockaddr_in6 *)rp->ai_addr)->sin6_port = htons(port);
  356. }else{
  357. COMPAT_CLOSE(*sock);
  358. *sock = INVALID_SOCKET;
  359. continue;
  360. }
  361. if(bind_address){
  362. for(rp_bind = ainfo_bind; rp_bind != NULL; rp_bind = rp_bind->ai_next){
  363. if(bind(*sock, rp_bind->ai_addr, rp_bind->ai_addrlen) == 0){
  364. break;
  365. }
  366. }
  367. if(!rp_bind){
  368. COMPAT_CLOSE(*sock);
  369. *sock = INVALID_SOCKET;
  370. continue;
  371. }
  372. }
  373. if(!blocking){
  374. /* Set non-blocking */
  375. if(net__socket_nonblock(sock)){
  376. continue;
  377. }
  378. }
  379. rc = connect(*sock, rp->ai_addr, rp->ai_addrlen);
  380. #ifdef WIN32
  381. errno = WSAGetLastError();
  382. #endif
  383. if(rc == 0 || errno == EINPROGRESS || errno == COMPAT_EWOULDBLOCK){
  384. if(rc < 0 && (errno == EINPROGRESS || errno == COMPAT_EWOULDBLOCK)){
  385. rc = MOSQ_ERR_CONN_PENDING;
  386. }
  387. if(blocking){
  388. /* Set non-blocking */
  389. if(net__socket_nonblock(sock)){
  390. continue;
  391. }
  392. }
  393. break;
  394. }
  395. COMPAT_CLOSE(*sock);
  396. *sock = INVALID_SOCKET;
  397. }
  398. freeaddrinfo(ainfo);
  399. if(bind_address){
  400. freeaddrinfo(ainfo_bind);
  401. }
  402. if(!rp){
  403. return MOSQ_ERR_ERRNO;
  404. }
  405. return rc;
  406. }
  407. #ifdef WITH_UNIX_SOCKETS
  408. static int net__try_connect_unix(const char *host, mosq_sock_t *sock)
  409. {
  410. struct sockaddr_un addr;
  411. int s;
  412. int rc;
  413. if(host == NULL || strlen(host) == 0 || strlen(host) > sizeof(addr.sun_path)-1){
  414. return MOSQ_ERR_INVAL;
  415. }
  416. memset(&addr, 0, sizeof(struct sockaddr_un));
  417. addr.sun_family = AF_UNIX;
  418. strncpy(addr.sun_path, host, sizeof(addr.sun_path)-1);
  419. s = socket(AF_UNIX, SOCK_STREAM, 0);
  420. if(s < 0){
  421. return MOSQ_ERR_ERRNO;
  422. }
  423. rc = net__socket_nonblock(&s);
  424. if(rc) return rc;
  425. rc = connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un));
  426. if(rc < 0){
  427. close(s);
  428. return MOSQ_ERR_ERRNO;
  429. }
  430. *sock = s;
  431. return 0;
  432. }
  433. #endif
  434. int net__try_connect(const char *host, uint16_t port, mosq_sock_t *sock, const char *bind_address, bool blocking)
  435. {
  436. if(port == 0){
  437. #ifdef WITH_UNIX_SOCKETS
  438. return net__try_connect_unix(host, sock);
  439. #else
  440. return MOSQ_ERR_NOT_SUPPORTED;
  441. #endif
  442. }else{
  443. return net__try_connect_tcp(host, port, sock, bind_address, blocking);
  444. }
  445. }
  446. #ifdef WITH_TLS
  447. void net__print_ssl_error(struct mosquitto *mosq)
  448. {
  449. char ebuf[256];
  450. unsigned long e;
  451. int num = 0;
  452. e = ERR_get_error();
  453. while(e){
  454. log__printf(mosq, MOSQ_LOG_ERR, "OpenSSL Error[%d]: %s", num, ERR_error_string(e, ebuf));
  455. e = ERR_get_error();
  456. num++;
  457. }
  458. }
  459. int net__socket_connect_tls(struct mosquitto *mosq)
  460. {
  461. int ret, err;
  462. long res;
  463. ERR_clear_error();
  464. if (mosq->tls_ocsp_required) {
  465. /* Note: OCSP is available in all currently supported OpenSSL versions. */
  466. if ((res=SSL_set_tlsext_status_type(mosq->ssl, TLSEXT_STATUSTYPE_ocsp)) != 1) {
  467. log__printf(mosq, MOSQ_LOG_ERR, "Could not activate OCSP (error: %ld)", res);
  468. return MOSQ_ERR_OCSP;
  469. }
  470. if ((res=SSL_CTX_set_tlsext_status_cb(mosq->ssl_ctx, mosquitto__verify_ocsp_status_cb)) != 1) {
  471. log__printf(mosq, MOSQ_LOG_ERR, "Could not activate OCSP (error: %ld)", res);
  472. return MOSQ_ERR_OCSP;
  473. }
  474. if ((res=SSL_CTX_set_tlsext_status_arg(mosq->ssl_ctx, mosq)) != 1) {
  475. log__printf(mosq, MOSQ_LOG_ERR, "Could not activate OCSP (error: %ld)", res);
  476. return MOSQ_ERR_OCSP;
  477. }
  478. }
  479. ret = SSL_connect(mosq->ssl);
  480. if(ret != 1) {
  481. err = SSL_get_error(mosq->ssl, ret);
  482. if (err == SSL_ERROR_SYSCALL) {
  483. mosq->want_connect = true;
  484. return MOSQ_ERR_SUCCESS;
  485. }
  486. if(err == SSL_ERROR_WANT_READ){
  487. mosq->want_connect = true;
  488. /* We always try to read anyway */
  489. }else if(err == SSL_ERROR_WANT_WRITE){
  490. mosq->want_write = true;
  491. mosq->want_connect = true;
  492. }else{
  493. net__print_ssl_error(mosq);
  494. COMPAT_CLOSE(mosq->sock);
  495. mosq->sock = INVALID_SOCKET;
  496. net__print_ssl_error(mosq);
  497. return MOSQ_ERR_TLS;
  498. }
  499. }else{
  500. mosq->want_connect = false;
  501. }
  502. return MOSQ_ERR_SUCCESS;
  503. }
  504. #endif
  505. #ifdef WITH_TLS
  506. static int net__tls_load_ca(struct mosquitto *mosq)
  507. {
  508. int ret;
  509. if(mosq->tls_use_os_certs){
  510. SSL_CTX_set_default_verify_paths(mosq->ssl_ctx);
  511. }
  512. #if OPENSSL_VERSION_NUMBER < 0x30000000L
  513. if(mosq->tls_cafile || mosq->tls_capath){
  514. ret = SSL_CTX_load_verify_locations(mosq->ssl_ctx, mosq->tls_cafile, mosq->tls_capath);
  515. if(ret == 0){
  516. # ifdef WITH_BROKER
  517. if(mosq->tls_cafile && mosq->tls_capath){
  518. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_cafile \"%s\" and bridge_capath \"%s\".", mosq->tls_cafile, mosq->tls_capath);
  519. }else if(mosq->tls_cafile){
  520. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_cafile \"%s\".", mosq->tls_cafile);
  521. }else{
  522. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_capath \"%s\".", mosq->tls_capath);
  523. }
  524. # else
  525. if(mosq->tls_cafile && mosq->tls_capath){
  526. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check cafile \"%s\" and capath \"%s\".", mosq->tls_cafile, mosq->tls_capath);
  527. }else if(mosq->tls_cafile){
  528. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check cafile \"%s\".", mosq->tls_cafile);
  529. }else{
  530. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check capath \"%s\".", mosq->tls_capath);
  531. }
  532. # endif
  533. return MOSQ_ERR_TLS;
  534. }
  535. }
  536. #else
  537. if(mosq->tls_cafile){
  538. ret = SSL_CTX_load_verify_file(mosq->ssl_ctx, mosq->tls_cafile);
  539. if(ret == 0){
  540. # ifdef WITH_BROKER
  541. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_cafile \"%s\".", mosq->tls_cafile);
  542. # else
  543. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check cafile \"%s\".", mosq->tls_cafile);
  544. # endif
  545. return MOSQ_ERR_TLS;
  546. }
  547. }
  548. if(mosq->tls_capath){
  549. ret = SSL_CTX_load_verify_dir(mosq->ssl_ctx, mosq->tls_capath);
  550. if(ret == 0){
  551. # ifdef WITH_BROKER
  552. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check bridge_capath \"%s\".", mosq->tls_capath);
  553. # else
  554. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load CA certificates, check capath \"%s\".", mosq->tls_capath);
  555. # endif
  556. return MOSQ_ERR_TLS;
  557. }
  558. }
  559. #endif
  560. return MOSQ_ERR_SUCCESS;
  561. }
  562. static int net__init_ssl_ctx(struct mosquitto *mosq)
  563. {
  564. int ret;
  565. ENGINE *engine = NULL;
  566. uint8_t tls_alpn_wire[256];
  567. uint8_t tls_alpn_len;
  568. #if !defined(OPENSSL_NO_ENGINE)
  569. EVP_PKEY *pkey;
  570. #endif
  571. #ifndef WITH_BROKER
  572. if(mosq->user_ssl_ctx){
  573. mosq->ssl_ctx = mosq->user_ssl_ctx;
  574. if(!mosq->ssl_ctx_defaults){
  575. return MOSQ_ERR_SUCCESS;
  576. }else if(!mosq->tls_cafile && !mosq->tls_capath && !mosq->tls_psk){
  577. log__printf(mosq, MOSQ_LOG_ERR, "Error: If you use MOSQ_OPT_SSL_CTX then MOSQ_OPT_SSL_CTX_WITH_DEFAULTS must be true, or at least one of cafile, capath or psk must be specified.");
  578. return MOSQ_ERR_INVAL;
  579. }
  580. }
  581. #endif
  582. /* Apply default SSL_CTX settings. This is only used if MOSQ_OPT_SSL_CTX
  583. * has not been set, or if both of MOSQ_OPT_SSL_CTX and
  584. * MOSQ_OPT_SSL_CTX_WITH_DEFAULTS are set. */
  585. if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk || mosq->tls_use_os_certs){
  586. if(!mosq->ssl_ctx){
  587. net__init_tls();
  588. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  589. mosq->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
  590. #else
  591. mosq->ssl_ctx = SSL_CTX_new(TLS_client_method());
  592. #endif
  593. if(!mosq->ssl_ctx){
  594. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to create TLS context.");
  595. net__print_ssl_error(mosq);
  596. return MOSQ_ERR_TLS;
  597. }
  598. }
  599. #ifdef SSL_OP_NO_TLSv1_3
  600. if(mosq->tls_psk){
  601. SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_TLSv1_3);
  602. }
  603. #endif
  604. if(!mosq->tls_version){
  605. SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1);
  606. #ifdef SSL_OP_NO_TLSv1_3
  607. }else if(!strcmp(mosq->tls_version, "tlsv1.3")){
  608. SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1_2);
  609. #endif
  610. }else if(!strcmp(mosq->tls_version, "tlsv1.2")){
  611. SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
  612. }else if(!strcmp(mosq->tls_version, "tlsv1.1")){
  613. SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1);
  614. }else{
  615. log__printf(mosq, MOSQ_LOG_ERR, "Error: Protocol %s not supported.", mosq->tls_version);
  616. return MOSQ_ERR_INVAL;
  617. }
  618. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  619. /* Allow use of DHE ciphers */
  620. SSL_CTX_set_dh_auto(mosq->ssl_ctx, 1);
  621. #endif
  622. /* Disable compression */
  623. SSL_CTX_set_options(mosq->ssl_ctx, SSL_OP_NO_COMPRESSION);
  624. /* Set ALPN */
  625. if(mosq->tls_alpn) {
  626. tls_alpn_len = (uint8_t) strnlen(mosq->tls_alpn, 254);
  627. tls_alpn_wire[0] = tls_alpn_len; /* first byte is length of string */
  628. memcpy(tls_alpn_wire + 1, mosq->tls_alpn, tls_alpn_len);
  629. SSL_CTX_set_alpn_protos(mosq->ssl_ctx, tls_alpn_wire, tls_alpn_len + 1U);
  630. }
  631. #ifdef SSL_MODE_RELEASE_BUFFERS
  632. /* Use even less memory per SSL connection. */
  633. SSL_CTX_set_mode(mosq->ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
  634. #endif
  635. #if !defined(OPENSSL_NO_ENGINE)
  636. if(mosq->tls_engine){
  637. engine = ENGINE_by_id(mosq->tls_engine);
  638. if(!engine){
  639. log__printf(mosq, MOSQ_LOG_ERR, "Error loading %s engine\n", mosq->tls_engine);
  640. return MOSQ_ERR_TLS;
  641. }
  642. if(!ENGINE_init(engine)){
  643. log__printf(mosq, MOSQ_LOG_ERR, "Failed engine initialisation\n");
  644. ENGINE_free(engine);
  645. return MOSQ_ERR_TLS;
  646. }
  647. ENGINE_set_default(engine, ENGINE_METHOD_ALL);
  648. ENGINE_free(engine); /* release the structural reference from ENGINE_by_id() */
  649. }
  650. #endif
  651. if(mosq->tls_ciphers){
  652. ret = SSL_CTX_set_cipher_list(mosq->ssl_ctx, mosq->tls_ciphers);
  653. if(ret == 0){
  654. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to set TLS ciphers. Check cipher list \"%s\".", mosq->tls_ciphers);
  655. #if !defined(OPENSSL_NO_ENGINE)
  656. ENGINE_FINISH(engine);
  657. #endif
  658. net__print_ssl_error(mosq);
  659. return MOSQ_ERR_TLS;
  660. }
  661. }
  662. if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_use_os_certs){
  663. ret = net__tls_load_ca(mosq);
  664. if(ret != MOSQ_ERR_SUCCESS){
  665. # if !defined(OPENSSL_NO_ENGINE)
  666. ENGINE_FINISH(engine);
  667. # endif
  668. net__print_ssl_error(mosq);
  669. return MOSQ_ERR_TLS;
  670. }
  671. if(mosq->tls_cert_reqs == 0){
  672. SSL_CTX_set_verify(mosq->ssl_ctx, SSL_VERIFY_NONE, NULL);
  673. }else{
  674. SSL_CTX_set_verify(mosq->ssl_ctx, SSL_VERIFY_PEER, mosquitto__server_certificate_verify);
  675. }
  676. if(mosq->tls_pw_callback){
  677. SSL_CTX_set_default_passwd_cb(mosq->ssl_ctx, mosq->tls_pw_callback);
  678. SSL_CTX_set_default_passwd_cb_userdata(mosq->ssl_ctx, mosq);
  679. }
  680. if(mosq->tls_certfile){
  681. ret = SSL_CTX_use_certificate_chain_file(mosq->ssl_ctx, mosq->tls_certfile);
  682. if(ret != 1){
  683. #ifdef WITH_BROKER
  684. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load client certificate, check bridge_certfile \"%s\".", mosq->tls_certfile);
  685. #else
  686. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load client certificate \"%s\".", mosq->tls_certfile);
  687. #endif
  688. #if !defined(OPENSSL_NO_ENGINE)
  689. ENGINE_FINISH(engine);
  690. #endif
  691. net__print_ssl_error(mosq);
  692. return MOSQ_ERR_TLS;
  693. }
  694. }
  695. if(mosq->tls_keyfile){
  696. if(mosq->tls_keyform == mosq_k_engine){
  697. #if !defined(OPENSSL_NO_ENGINE)
  698. UI_METHOD *ui_method = net__get_ui_method();
  699. if(mosq->tls_engine_kpass_sha1){
  700. if(!ENGINE_ctrl_cmd(engine, ENGINE_SECRET_MODE, ENGINE_SECRET_MODE_SHA, NULL, NULL, 0)){
  701. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to set engine secret mode sha1");
  702. ENGINE_FINISH(engine);
  703. net__print_ssl_error(mosq);
  704. return MOSQ_ERR_TLS;
  705. }
  706. if(!ENGINE_ctrl_cmd(engine, ENGINE_PIN, 0, mosq->tls_engine_kpass_sha1, NULL, 0)){
  707. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to set engine pin");
  708. ENGINE_FINISH(engine);
  709. net__print_ssl_error(mosq);
  710. return MOSQ_ERR_TLS;
  711. }
  712. ui_method = NULL;
  713. }
  714. pkey = ENGINE_load_private_key(engine, mosq->tls_keyfile, ui_method, NULL);
  715. if(!pkey){
  716. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load engine private key file \"%s\".", mosq->tls_keyfile);
  717. ENGINE_FINISH(engine);
  718. net__print_ssl_error(mosq);
  719. return MOSQ_ERR_TLS;
  720. }
  721. if(SSL_CTX_use_PrivateKey(mosq->ssl_ctx, pkey) <= 0){
  722. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to use engine private key file \"%s\".", mosq->tls_keyfile);
  723. ENGINE_FINISH(engine);
  724. net__print_ssl_error(mosq);
  725. return MOSQ_ERR_TLS;
  726. }
  727. #endif
  728. }else{
  729. ret = SSL_CTX_use_PrivateKey_file(mosq->ssl_ctx, mosq->tls_keyfile, SSL_FILETYPE_PEM);
  730. if(ret != 1){
  731. #ifdef WITH_BROKER
  732. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load client key file, check bridge_keyfile \"%s\".", mosq->tls_keyfile);
  733. #else
  734. log__printf(mosq, MOSQ_LOG_ERR, "Error: Unable to load client key file \"%s\".", mosq->tls_keyfile);
  735. #endif
  736. #if !defined(OPENSSL_NO_ENGINE)
  737. ENGINE_FINISH(engine);
  738. #endif
  739. net__print_ssl_error(mosq);
  740. return MOSQ_ERR_TLS;
  741. }
  742. }
  743. ret = SSL_CTX_check_private_key(mosq->ssl_ctx);
  744. if(ret != 1){
  745. log__printf(mosq, MOSQ_LOG_ERR, "Error: Client certificate/key are inconsistent.");
  746. #if !defined(OPENSSL_NO_ENGINE)
  747. ENGINE_FINISH(engine);
  748. #endif
  749. net__print_ssl_error(mosq);
  750. return MOSQ_ERR_TLS;
  751. }
  752. }
  753. #ifdef FINAL_WITH_TLS_PSK
  754. }else if(mosq->tls_psk){
  755. SSL_CTX_set_psk_client_callback(mosq->ssl_ctx, psk_client_callback);
  756. if(mosq->tls_ciphers == NULL){
  757. SSL_CTX_set_cipher_list(mosq->ssl_ctx, "PSK");
  758. }
  759. #endif
  760. }
  761. }
  762. return MOSQ_ERR_SUCCESS;
  763. }
  764. #endif
  765. int net__socket_connect_step3(struct mosquitto *mosq, const char *host)
  766. {
  767. #ifdef WITH_TLS
  768. BIO *bio;
  769. int rc = net__init_ssl_ctx(mosq);
  770. if(rc){
  771. net__socket_close(mosq);
  772. return rc;
  773. }
  774. if(mosq->ssl_ctx){
  775. if(mosq->ssl){
  776. SSL_free(mosq->ssl);
  777. }
  778. mosq->ssl = SSL_new(mosq->ssl_ctx);
  779. if(!mosq->ssl){
  780. net__socket_close(mosq);
  781. net__print_ssl_error(mosq);
  782. return MOSQ_ERR_TLS;
  783. }
  784. SSL_set_ex_data(mosq->ssl, tls_ex_index_mosq, mosq);
  785. bio = BIO_new_socket(mosq->sock, BIO_NOCLOSE);
  786. if(!bio){
  787. net__socket_close(mosq);
  788. net__print_ssl_error(mosq);
  789. return MOSQ_ERR_TLS;
  790. }
  791. SSL_set_bio(mosq->ssl, bio, bio);
  792. /*
  793. * required for the SNI resolving
  794. */
  795. if(SSL_set_tlsext_host_name(mosq->ssl, host) != 1) {
  796. net__socket_close(mosq);
  797. return MOSQ_ERR_TLS;
  798. }
  799. if(net__socket_connect_tls(mosq)){
  800. net__socket_close(mosq);
  801. return MOSQ_ERR_TLS;
  802. }
  803. }
  804. #else
  805. UNUSED(mosq);
  806. UNUSED(host);
  807. #endif
  808. return MOSQ_ERR_SUCCESS;
  809. }
  810. /* Create a socket and connect it to 'ip' on port 'port'. */
  811. int net__socket_connect(struct mosquitto *mosq, const char *host, uint16_t port, const char *bind_address, bool blocking)
  812. {
  813. int rc, rc2;
  814. if(!mosq || !host) return MOSQ_ERR_INVAL;
  815. rc = net__try_connect(host, port, &mosq->sock, bind_address, blocking);
  816. if(rc > 0) return rc;
  817. if(mosq->tcp_nodelay){
  818. int flag = 1;
  819. if(setsockopt(mosq->sock, IPPROTO_TCP, TCP_NODELAY, (const void*)&flag, sizeof(int)) != 0){
  820. log__printf(mosq, MOSQ_LOG_WARNING, "Warning: Unable to set TCP_NODELAY.");
  821. }
  822. }
  823. #if defined(WITH_SOCKS) && !defined(WITH_BROKER)
  824. if(!mosq->socks5_host)
  825. #endif
  826. {
  827. rc2 = net__socket_connect_step3(mosq, host);
  828. if(rc2) return rc2;
  829. }
  830. return rc;
  831. }
  832. #ifdef WITH_TLS
  833. static int net__handle_ssl(struct mosquitto* mosq, int ret)
  834. {
  835. int err;
  836. err = SSL_get_error(mosq->ssl, ret);
  837. if (err == SSL_ERROR_WANT_READ) {
  838. ret = -1;
  839. errno = EAGAIN;
  840. }
  841. else if (err == SSL_ERROR_WANT_WRITE) {
  842. ret = -1;
  843. #ifdef WITH_BROKER
  844. mux__add_out(mosq);
  845. #else
  846. mosq->want_write = true;
  847. #endif
  848. errno = EAGAIN;
  849. }
  850. else {
  851. net__print_ssl_error(mosq);
  852. errno = EPROTO;
  853. }
  854. ERR_clear_error();
  855. #ifdef WIN32
  856. WSASetLastError(errno);
  857. #endif
  858. return ret;
  859. }
  860. #endif
  861. ssize_t net__read(struct mosquitto *mosq, void *buf, size_t count)
  862. {
  863. #ifdef WITH_TLS
  864. int ret;
  865. #endif
  866. assert(mosq);
  867. errno = 0;
  868. #ifdef WITH_TLS
  869. if(mosq->ssl){
  870. ret = SSL_read(mosq->ssl, buf, (int)count);
  871. if(ret <= 0){
  872. ret = net__handle_ssl(mosq, ret);
  873. }
  874. return (ssize_t )ret;
  875. }else{
  876. /* Call normal read/recv */
  877. #endif
  878. #ifndef WIN32
  879. return read(mosq->sock, buf, count);
  880. #else
  881. return recv(mosq->sock, buf, count, 0);
  882. #endif
  883. #ifdef WITH_TLS
  884. }
  885. #endif
  886. }
  887. ssize_t net__write(struct mosquitto *mosq, const void *buf, size_t count)
  888. {
  889. #ifdef WITH_TLS
  890. int ret;
  891. #endif
  892. assert(mosq);
  893. errno = 0;
  894. #ifdef WITH_TLS
  895. if(mosq->ssl){
  896. mosq->want_write = false;
  897. ret = SSL_write(mosq->ssl, buf, (int)count);
  898. if(ret < 0){
  899. ret = net__handle_ssl(mosq, ret);
  900. }
  901. return (ssize_t )ret;
  902. }else{
  903. /* Call normal write/send */
  904. #endif
  905. #ifndef WIN32
  906. return write(mosq->sock, buf, count);
  907. #else
  908. return send(mosq->sock, buf, count, 0);
  909. #endif
  910. #ifdef WITH_TLS
  911. }
  912. #endif
  913. }
  914. int net__socket_nonblock(mosq_sock_t *sock)
  915. {
  916. #ifndef WIN32
  917. int opt;
  918. /* Set non-blocking */
  919. opt = fcntl(*sock, F_GETFL, 0);
  920. if(opt == -1){
  921. COMPAT_CLOSE(*sock);
  922. *sock = INVALID_SOCKET;
  923. return MOSQ_ERR_ERRNO;
  924. }
  925. if(fcntl(*sock, F_SETFL, opt | O_NONBLOCK) == -1){
  926. /* If either fcntl fails, don't want to allow this client to connect. */
  927. COMPAT_CLOSE(*sock);
  928. *sock = INVALID_SOCKET;
  929. return MOSQ_ERR_ERRNO;
  930. }
  931. #else
  932. unsigned long opt = 1;
  933. if(ioctlsocket(*sock, FIONBIO, &opt)){
  934. COMPAT_CLOSE(*sock);
  935. *sock = INVALID_SOCKET;
  936. return MOSQ_ERR_ERRNO;
  937. }
  938. #endif
  939. return MOSQ_ERR_SUCCESS;
  940. }
  941. #ifndef WITH_BROKER
  942. int net__socketpair(mosq_sock_t *pairR, mosq_sock_t *pairW)
  943. {
  944. #ifdef WIN32
  945. int family[2] = {AF_INET, AF_INET6};
  946. int i;
  947. struct sockaddr_storage ss;
  948. struct sockaddr_in *sa = (struct sockaddr_in *)&ss;
  949. struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&ss;
  950. socklen_t ss_len;
  951. mosq_sock_t spR, spW;
  952. mosq_sock_t listensock;
  953. *pairR = INVALID_SOCKET;
  954. *pairW = INVALID_SOCKET;
  955. for(i=0; i<2; i++){
  956. memset(&ss, 0, sizeof(ss));
  957. if(family[i] == AF_INET){
  958. sa->sin_family = family[i];
  959. sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  960. sa->sin_port = 0;
  961. ss_len = sizeof(struct sockaddr_in);
  962. }else if(family[i] == AF_INET6){
  963. sa6->sin6_family = family[i];
  964. sa6->sin6_addr = in6addr_loopback;
  965. sa6->sin6_port = 0;
  966. ss_len = sizeof(struct sockaddr_in6);
  967. }else{
  968. return MOSQ_ERR_INVAL;
  969. }
  970. listensock = socket(family[i], SOCK_STREAM, IPPROTO_TCP);
  971. if(listensock == -1){
  972. continue;
  973. }
  974. if(bind(listensock, (struct sockaddr *)&ss, ss_len) == -1){
  975. COMPAT_CLOSE(listensock);
  976. continue;
  977. }
  978. if(listen(listensock, 1) == -1){
  979. COMPAT_CLOSE(listensock);
  980. continue;
  981. }
  982. memset(&ss, 0, sizeof(ss));
  983. ss_len = sizeof(ss);
  984. if(getsockname(listensock, (struct sockaddr *)&ss, &ss_len) < 0){
  985. COMPAT_CLOSE(listensock);
  986. continue;
  987. }
  988. if(family[i] == AF_INET){
  989. sa->sin_family = family[i];
  990. sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  991. ss_len = sizeof(struct sockaddr_in);
  992. }else if(family[i] == AF_INET6){
  993. sa6->sin6_family = family[i];
  994. sa6->sin6_addr = in6addr_loopback;
  995. ss_len = sizeof(struct sockaddr_in6);
  996. }
  997. spR = socket(family[i], SOCK_STREAM, IPPROTO_TCP);
  998. if(spR == -1){
  999. COMPAT_CLOSE(listensock);
  1000. continue;
  1001. }
  1002. if(net__socket_nonblock(&spR)){
  1003. COMPAT_CLOSE(listensock);
  1004. continue;
  1005. }
  1006. if(connect(spR, (struct sockaddr *)&ss, ss_len) < 0){
  1007. #ifdef WIN32
  1008. errno = WSAGetLastError();
  1009. #endif
  1010. if(errno != EINPROGRESS && errno != COMPAT_EWOULDBLOCK){
  1011. COMPAT_CLOSE(spR);
  1012. COMPAT_CLOSE(listensock);
  1013. continue;
  1014. }
  1015. }
  1016. spW = accept(listensock, NULL, 0);
  1017. if(spW == -1){
  1018. #ifdef WIN32
  1019. errno = WSAGetLastError();
  1020. #endif
  1021. if(errno != EINPROGRESS && errno != COMPAT_EWOULDBLOCK){
  1022. COMPAT_CLOSE(spR);
  1023. COMPAT_CLOSE(listensock);
  1024. continue;
  1025. }
  1026. }
  1027. if(net__socket_nonblock(&spW)){
  1028. COMPAT_CLOSE(spR);
  1029. COMPAT_CLOSE(listensock);
  1030. continue;
  1031. }
  1032. COMPAT_CLOSE(listensock);
  1033. *pairR = spR;
  1034. *pairW = spW;
  1035. return MOSQ_ERR_SUCCESS;
  1036. }
  1037. return MOSQ_ERR_UNKNOWN;
  1038. #else
  1039. int sv[2];
  1040. *pairR = INVALID_SOCKET;
  1041. *pairW = INVALID_SOCKET;
  1042. if(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1){
  1043. return MOSQ_ERR_ERRNO;
  1044. }
  1045. if(net__socket_nonblock(&sv[0])){
  1046. COMPAT_CLOSE(sv[1]);
  1047. return MOSQ_ERR_ERRNO;
  1048. }
  1049. if(net__socket_nonblock(&sv[1])){
  1050. COMPAT_CLOSE(sv[0]);
  1051. return MOSQ_ERR_ERRNO;
  1052. }
  1053. *pairR = sv[0];
  1054. *pairW = sv[1];
  1055. return MOSQ_ERR_SUCCESS;
  1056. #endif
  1057. }
  1058. #endif
  1059. #ifndef WITH_BROKER
  1060. void *mosquitto_ssl_get(struct mosquitto *mosq)
  1061. {
  1062. #ifdef WITH_TLS
  1063. return mosq->ssl;
  1064. #else
  1065. UNUSED(mosq);
  1066. return NULL;
  1067. #endif
  1068. }
  1069. #endif