connect.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/bio.h>
  57. #include <assert.h>
  58. #include <errno.h>
  59. #include <string.h>
  60. #if !defined(OPENSSL_WINDOWS)
  61. #include <sys/socket.h>
  62. #include <netinet/in.h>
  63. #include <arpa/inet.h>
  64. #include <unistd.h>
  65. #else
  66. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  67. #include <winsock2.h>
  68. #include <ws2tcpip.h>
  69. OPENSSL_MSVC_PRAGMA(warning(pop))
  70. #endif
  71. #include <openssl/buf.h>
  72. #include <openssl/err.h>
  73. #include <openssl/mem.h>
  74. #include "internal.h"
  75. #include "../internal.h"
  76. enum {
  77. BIO_CONN_S_BEFORE,
  78. BIO_CONN_S_BLOCKED_CONNECT,
  79. BIO_CONN_S_OK,
  80. };
  81. typedef struct bio_connect_st {
  82. int state;
  83. char *param_hostname;
  84. char *param_port;
  85. int nbio;
  86. unsigned short port;
  87. struct sockaddr_storage them;
  88. socklen_t them_length;
  89. // the file descriptor is kept in bio->num in order to match the socket
  90. // BIO.
  91. // info_callback is called when the connection is initially made
  92. // callback(BIO,state,ret); The callback should return 'ret', state is for
  93. // compatibility with the SSL info_callback.
  94. int (*info_callback)(const BIO *bio, int state, int ret);
  95. } BIO_CONNECT;
  96. #if !defined(OPENSSL_WINDOWS)
  97. static int closesocket(int sock) {
  98. return close(sock);
  99. }
  100. #endif
  101. // split_host_and_port sets |*out_host| and |*out_port| to the host and port
  102. // parsed from |name|. It returns one on success or zero on error. Even when
  103. // successful, |*out_port| may be NULL on return if no port was specified.
  104. static int split_host_and_port(char **out_host, char **out_port, const char *name) {
  105. const char *host, *port = NULL;
  106. size_t host_len = 0;
  107. *out_host = NULL;
  108. *out_port = NULL;
  109. if (name[0] == '[') { // bracketed IPv6 address
  110. const char *close = strchr(name, ']');
  111. if (close == NULL) {
  112. return 0;
  113. }
  114. host = name + 1;
  115. host_len = close - host;
  116. if (close[1] == ':') { // [IP]:port
  117. port = close + 2;
  118. } else if (close[1] != 0) {
  119. return 0;
  120. }
  121. } else {
  122. const char *colon = strchr(name, ':');
  123. if (colon == NULL || strchr(colon + 1, ':') != NULL) { // IPv6 address
  124. host = name;
  125. host_len = strlen(name);
  126. } else { // host:port
  127. host = name;
  128. host_len = colon - name;
  129. port = colon + 1;
  130. }
  131. }
  132. *out_host = BUF_strndup(host, host_len);
  133. if (*out_host == NULL) {
  134. return 0;
  135. }
  136. if (port == NULL) {
  137. *out_port = NULL;
  138. return 1;
  139. }
  140. *out_port = OPENSSL_strdup(port);
  141. if (*out_port == NULL) {
  142. OPENSSL_free(*out_host);
  143. *out_host = NULL;
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. static int conn_state(BIO *bio, BIO_CONNECT *c) {
  149. int ret = -1, i;
  150. int (*cb)(const BIO *, int, int) = NULL;
  151. if (c->info_callback != NULL) {
  152. cb = c->info_callback;
  153. }
  154. for (;;) {
  155. switch (c->state) {
  156. case BIO_CONN_S_BEFORE:
  157. // If there's a hostname and a port, assume that both are
  158. // exactly what they say. If there is only a hostname, try
  159. // (just once) to split it into a hostname and port.
  160. if (c->param_hostname == NULL) {
  161. OPENSSL_PUT_ERROR(BIO, BIO_R_NO_HOSTNAME_SPECIFIED);
  162. goto exit_loop;
  163. }
  164. if (c->param_port == NULL) {
  165. char *host, *port;
  166. if (!split_host_and_port(&host, &port, c->param_hostname) ||
  167. port == NULL) {
  168. OPENSSL_free(host);
  169. OPENSSL_free(port);
  170. OPENSSL_PUT_ERROR(BIO, BIO_R_NO_PORT_SPECIFIED);
  171. ERR_add_error_data(2, "host=", c->param_hostname);
  172. goto exit_loop;
  173. }
  174. OPENSSL_free(c->param_port);
  175. c->param_port = port;
  176. OPENSSL_free(c->param_hostname);
  177. c->param_hostname = host;
  178. }
  179. if (!bio_ip_and_port_to_socket_and_addr(
  180. &bio->num, &c->them, &c->them_length, c->param_hostname,
  181. c->param_port)) {
  182. OPENSSL_PUT_ERROR(BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
  183. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  184. goto exit_loop;
  185. }
  186. if (c->nbio) {
  187. if (!bio_socket_nbio(bio->num, 1)) {
  188. OPENSSL_PUT_ERROR(BIO, BIO_R_ERROR_SETTING_NBIO);
  189. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  190. c->param_port);
  191. goto exit_loop;
  192. }
  193. }
  194. i = 1;
  195. ret = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
  196. sizeof(i));
  197. if (ret < 0) {
  198. OPENSSL_PUT_SYSTEM_ERROR();
  199. OPENSSL_PUT_ERROR(BIO, BIO_R_KEEPALIVE);
  200. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  201. goto exit_loop;
  202. }
  203. BIO_clear_retry_flags(bio);
  204. ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length);
  205. if (ret < 0) {
  206. if (bio_fd_should_retry(ret)) {
  207. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  208. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  209. bio->retry_reason = BIO_RR_CONNECT;
  210. } else {
  211. OPENSSL_PUT_SYSTEM_ERROR();
  212. OPENSSL_PUT_ERROR(BIO, BIO_R_CONNECT_ERROR);
  213. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  214. c->param_port);
  215. }
  216. goto exit_loop;
  217. } else {
  218. c->state = BIO_CONN_S_OK;
  219. }
  220. break;
  221. case BIO_CONN_S_BLOCKED_CONNECT:
  222. i = bio_sock_error(bio->num);
  223. if (i) {
  224. if (bio_fd_should_retry(ret)) {
  225. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  226. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  227. bio->retry_reason = BIO_RR_CONNECT;
  228. ret = -1;
  229. } else {
  230. BIO_clear_retry_flags(bio);
  231. OPENSSL_PUT_SYSTEM_ERROR();
  232. OPENSSL_PUT_ERROR(BIO, BIO_R_NBIO_CONNECT_ERROR);
  233. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  234. ret = 0;
  235. }
  236. goto exit_loop;
  237. } else {
  238. c->state = BIO_CONN_S_OK;
  239. }
  240. break;
  241. case BIO_CONN_S_OK:
  242. ret = 1;
  243. goto exit_loop;
  244. default:
  245. assert(0);
  246. goto exit_loop;
  247. }
  248. if (cb != NULL) {
  249. ret = cb((BIO *)bio, c->state, ret);
  250. if (ret == 0) {
  251. goto end;
  252. }
  253. }
  254. }
  255. exit_loop:
  256. if (cb != NULL) {
  257. ret = cb((BIO *)bio, c->state, ret);
  258. }
  259. end:
  260. return ret;
  261. }
  262. static BIO_CONNECT *BIO_CONNECT_new(void) {
  263. BIO_CONNECT *ret = OPENSSL_malloc(sizeof(BIO_CONNECT));
  264. if (ret == NULL) {
  265. return NULL;
  266. }
  267. OPENSSL_memset(ret, 0, sizeof(BIO_CONNECT));
  268. ret->state = BIO_CONN_S_BEFORE;
  269. return ret;
  270. }
  271. static void BIO_CONNECT_free(BIO_CONNECT *c) {
  272. if (c == NULL) {
  273. return;
  274. }
  275. OPENSSL_free(c->param_hostname);
  276. OPENSSL_free(c->param_port);
  277. OPENSSL_free(c);
  278. }
  279. static int conn_new(BIO *bio) {
  280. bio->init = 0;
  281. bio->num = -1;
  282. bio->flags = 0;
  283. bio->ptr = (char *)BIO_CONNECT_new();
  284. return bio->ptr != NULL;
  285. }
  286. static void conn_close_socket(BIO *bio) {
  287. BIO_CONNECT *c = (BIO_CONNECT *) bio->ptr;
  288. if (bio->num == -1) {
  289. return;
  290. }
  291. // Only do a shutdown if things were established
  292. if (c->state == BIO_CONN_S_OK) {
  293. shutdown(bio->num, 2);
  294. }
  295. closesocket(bio->num);
  296. bio->num = -1;
  297. }
  298. static int conn_free(BIO *bio) {
  299. if (bio == NULL) {
  300. return 0;
  301. }
  302. if (bio->shutdown) {
  303. conn_close_socket(bio);
  304. }
  305. BIO_CONNECT_free((BIO_CONNECT*) bio->ptr);
  306. return 1;
  307. }
  308. static int conn_read(BIO *bio, char *out, int out_len) {
  309. int ret = 0;
  310. BIO_CONNECT *data;
  311. data = (BIO_CONNECT *)bio->ptr;
  312. if (data->state != BIO_CONN_S_OK) {
  313. ret = conn_state(bio, data);
  314. if (ret <= 0) {
  315. return ret;
  316. }
  317. }
  318. bio_clear_socket_error();
  319. ret = recv(bio->num, out, out_len, 0);
  320. BIO_clear_retry_flags(bio);
  321. if (ret <= 0) {
  322. if (bio_fd_should_retry(ret)) {
  323. BIO_set_retry_read(bio);
  324. }
  325. }
  326. return ret;
  327. }
  328. static int conn_write(BIO *bio, const char *in, int in_len) {
  329. int ret;
  330. BIO_CONNECT *data;
  331. data = (BIO_CONNECT *)bio->ptr;
  332. if (data->state != BIO_CONN_S_OK) {
  333. ret = conn_state(bio, data);
  334. if (ret <= 0) {
  335. return ret;
  336. }
  337. }
  338. bio_clear_socket_error();
  339. ret = send(bio->num, in, in_len, 0);
  340. BIO_clear_retry_flags(bio);
  341. if (ret <= 0) {
  342. if (bio_fd_should_retry(ret)) {
  343. BIO_set_retry_write(bio);
  344. }
  345. }
  346. return ret;
  347. }
  348. static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) {
  349. int *ip;
  350. long ret = 1;
  351. BIO_CONNECT *data;
  352. data = (BIO_CONNECT *)bio->ptr;
  353. switch (cmd) {
  354. case BIO_CTRL_RESET:
  355. ret = 0;
  356. data->state = BIO_CONN_S_BEFORE;
  357. conn_close_socket(bio);
  358. bio->flags = 0;
  359. break;
  360. case BIO_C_DO_STATE_MACHINE:
  361. // use this one to start the connection
  362. if (data->state != BIO_CONN_S_OK) {
  363. ret = (long)conn_state(bio, data);
  364. } else {
  365. ret = 1;
  366. }
  367. break;
  368. case BIO_C_SET_CONNECT:
  369. if (ptr != NULL) {
  370. bio->init = 1;
  371. if (num == 0) {
  372. OPENSSL_free(data->param_hostname);
  373. data->param_hostname = BUF_strdup(ptr);
  374. if (data->param_hostname == NULL) {
  375. ret = 0;
  376. }
  377. } else if (num == 1) {
  378. OPENSSL_free(data->param_port);
  379. data->param_port = BUF_strdup(ptr);
  380. if (data->param_port == NULL) {
  381. ret = 0;
  382. }
  383. } else {
  384. ret = 0;
  385. }
  386. }
  387. break;
  388. case BIO_C_SET_NBIO:
  389. data->nbio = (int)num;
  390. break;
  391. case BIO_C_GET_FD:
  392. if (bio->init) {
  393. ip = (int *)ptr;
  394. if (ip != NULL) {
  395. *ip = bio->num;
  396. }
  397. ret = bio->num;
  398. } else {
  399. ret = -1;
  400. }
  401. break;
  402. case BIO_CTRL_GET_CLOSE:
  403. ret = bio->shutdown;
  404. break;
  405. case BIO_CTRL_SET_CLOSE:
  406. bio->shutdown = (int)num;
  407. break;
  408. case BIO_CTRL_PENDING:
  409. case BIO_CTRL_WPENDING:
  410. ret = 0;
  411. break;
  412. case BIO_CTRL_FLUSH:
  413. break;
  414. case BIO_CTRL_GET_CALLBACK: {
  415. int (**fptr)(const BIO *bio, int state, int xret);
  416. fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
  417. *fptr = data->info_callback;
  418. } break;
  419. default:
  420. ret = 0;
  421. break;
  422. }
  423. return ret;
  424. }
  425. static long conn_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  426. long ret = 1;
  427. BIO_CONNECT *data;
  428. data = (BIO_CONNECT *)bio->ptr;
  429. switch (cmd) {
  430. case BIO_CTRL_SET_CALLBACK:
  431. data->info_callback = (int (*)(const struct bio_st *, int, int))fp;
  432. break;
  433. default:
  434. ret = 0;
  435. break;
  436. }
  437. return ret;
  438. }
  439. BIO *BIO_new_connect(const char *hostname) {
  440. BIO *ret;
  441. ret = BIO_new(BIO_s_connect());
  442. if (ret == NULL) {
  443. return NULL;
  444. }
  445. if (!BIO_set_conn_hostname(ret, hostname)) {
  446. BIO_free(ret);
  447. return NULL;
  448. }
  449. return ret;
  450. }
  451. static const BIO_METHOD methods_connectp = {
  452. BIO_TYPE_CONNECT, "socket connect", conn_write, conn_read,
  453. NULL /* puts */, NULL /* gets */, conn_ctrl, conn_new,
  454. conn_free, conn_callback_ctrl,
  455. };
  456. const BIO_METHOD *BIO_s_connect(void) { return &methods_connectp; }
  457. int BIO_set_conn_hostname(BIO *bio, const char *name) {
  458. return BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, (void*) name);
  459. }
  460. int BIO_set_conn_port(BIO *bio, const char *port_str) {
  461. return BIO_ctrl(bio, BIO_C_SET_CONNECT, 1, (void*) port_str);
  462. }
  463. int BIO_set_conn_int_port(BIO *bio, const int *port) {
  464. char buf[DECIMAL_SIZE(int) + 1];
  465. BIO_snprintf(buf, sizeof(buf), "%d", *port);
  466. return BIO_set_conn_port(bio, buf);
  467. }
  468. int BIO_set_nbio(BIO *bio, int on) {
  469. return BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL);
  470. }
  471. int BIO_do_connect(BIO *bio) {
  472. return BIO_ctrl(bio, BIO_C_DO_STATE_MACHINE, 0, NULL);
  473. }