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

554 lines
15 KiB

  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. enum {
  76. BIO_CONN_S_BEFORE,
  77. BIO_CONN_S_BLOCKED_CONNECT,
  78. BIO_CONN_S_OK,
  79. };
  80. typedef struct bio_connect_st {
  81. int state;
  82. char *param_hostname;
  83. char *param_port;
  84. int nbio;
  85. unsigned short port;
  86. struct sockaddr_storage them;
  87. socklen_t them_length;
  88. /* the file descriptor is kept in bio->num in order to match the socket
  89. * BIO. */
  90. /* info_callback is called when the connection is initially made
  91. * callback(BIO,state,ret); The callback should return 'ret', state is for
  92. * compatibility with the SSL info_callback. */
  93. int (*info_callback)(const BIO *bio, int state, int ret);
  94. } BIO_CONNECT;
  95. #if !defined(OPENSSL_WINDOWS)
  96. static int closesocket(int sock) {
  97. return close(sock);
  98. }
  99. #endif
  100. /* split_host_and_port sets |*out_host| and |*out_port| to the host and port
  101. * parsed from |name|. It returns one on success or zero on error. Even when
  102. * successful, |*out_port| may be NULL on return if no port was specified. */
  103. static int split_host_and_port(char **out_host, char **out_port, const char *name) {
  104. const char *host, *port = NULL;
  105. size_t host_len = 0;
  106. *out_host = NULL;
  107. *out_port = NULL;
  108. if (name[0] == '[') { /* bracketed IPv6 address */
  109. const char *close = strchr(name, ']');
  110. if (close == NULL) {
  111. return 0;
  112. }
  113. host = name + 1;
  114. host_len = close - host;
  115. if (close[1] == ':') { /* [IP]:port */
  116. port = close + 2;
  117. } else if (close[1] != 0) {
  118. return 0;
  119. }
  120. } else {
  121. const char *colon = strchr(name, ':');
  122. if (colon == NULL || strchr(colon + 1, ':') != NULL) { /* IPv6 address */
  123. host = name;
  124. host_len = strlen(name);
  125. } else { /* host:port */
  126. host = name;
  127. host_len = colon - name;
  128. port = colon + 1;
  129. }
  130. }
  131. *out_host = BUF_strndup(host, host_len);
  132. if (*out_host == NULL) {
  133. return 0;
  134. }
  135. if (port == NULL) {
  136. *out_port = NULL;
  137. return 1;
  138. }
  139. *out_port = OPENSSL_strdup(port);
  140. if (*out_port == NULL) {
  141. OPENSSL_free(*out_host);
  142. *out_host = NULL;
  143. return 0;
  144. }
  145. return 1;
  146. }
  147. static int conn_state(BIO *bio, BIO_CONNECT *c) {
  148. int ret = -1, i;
  149. int (*cb)(const BIO *, int, int) = NULL;
  150. if (c->info_callback != NULL) {
  151. cb = c->info_callback;
  152. }
  153. for (;;) {
  154. switch (c->state) {
  155. case BIO_CONN_S_BEFORE:
  156. /* If there's a hostname and a port, assume that both are
  157. * exactly what they say. If there is only a hostname, try
  158. * (just once) to split it into a hostname and port. */
  159. if (c->param_hostname == NULL) {
  160. OPENSSL_PUT_ERROR(BIO, BIO_R_NO_HOSTNAME_SPECIFIED);
  161. goto exit_loop;
  162. }
  163. if (c->param_port == NULL) {
  164. char *host, *port;
  165. if (!split_host_and_port(&host, &port, c->param_hostname) ||
  166. port == NULL) {
  167. OPENSSL_free(host);
  168. OPENSSL_free(port);
  169. OPENSSL_PUT_ERROR(BIO, BIO_R_NO_PORT_SPECIFIED);
  170. ERR_add_error_data(2, "host=", c->param_hostname);
  171. goto exit_loop;
  172. }
  173. OPENSSL_free(c->param_port);
  174. c->param_port = port;
  175. OPENSSL_free(c->param_hostname);
  176. c->param_hostname = host;
  177. }
  178. if (!bio_ip_and_port_to_socket_and_addr(
  179. &bio->num, &c->them, &c->them_length, c->param_hostname,
  180. c->param_port)) {
  181. OPENSSL_PUT_ERROR(BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
  182. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  183. goto exit_loop;
  184. }
  185. if (c->nbio) {
  186. if (!bio_socket_nbio(bio->num, 1)) {
  187. OPENSSL_PUT_ERROR(BIO, BIO_R_ERROR_SETTING_NBIO);
  188. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  189. c->param_port);
  190. goto exit_loop;
  191. }
  192. }
  193. i = 1;
  194. ret = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
  195. sizeof(i));
  196. if (ret < 0) {
  197. OPENSSL_PUT_SYSTEM_ERROR();
  198. OPENSSL_PUT_ERROR(BIO, BIO_R_KEEPALIVE);
  199. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  200. goto exit_loop;
  201. }
  202. BIO_clear_retry_flags(bio);
  203. ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length);
  204. if (ret < 0) {
  205. if (bio_fd_should_retry(ret)) {
  206. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  207. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  208. bio->retry_reason = BIO_RR_CONNECT;
  209. } else {
  210. OPENSSL_PUT_SYSTEM_ERROR();
  211. OPENSSL_PUT_ERROR(BIO, BIO_R_CONNECT_ERROR);
  212. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  213. c->param_port);
  214. }
  215. goto exit_loop;
  216. } else {
  217. c->state = BIO_CONN_S_OK;
  218. }
  219. break;
  220. case BIO_CONN_S_BLOCKED_CONNECT:
  221. i = bio_sock_error(bio->num);
  222. if (i) {
  223. if (bio_fd_should_retry(ret)) {
  224. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  225. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  226. bio->retry_reason = BIO_RR_CONNECT;
  227. ret = -1;
  228. } else {
  229. BIO_clear_retry_flags(bio);
  230. OPENSSL_PUT_SYSTEM_ERROR();
  231. OPENSSL_PUT_ERROR(BIO, BIO_R_NBIO_CONNECT_ERROR);
  232. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  233. ret = 0;
  234. }
  235. goto exit_loop;
  236. } else {
  237. c->state = BIO_CONN_S_OK;
  238. }
  239. break;
  240. case BIO_CONN_S_OK:
  241. ret = 1;
  242. goto exit_loop;
  243. default:
  244. assert(0);
  245. goto exit_loop;
  246. }
  247. if (cb != NULL) {
  248. ret = cb((BIO *)bio, c->state, ret);
  249. if (ret == 0) {
  250. goto end;
  251. }
  252. }
  253. }
  254. exit_loop:
  255. if (cb != NULL) {
  256. ret = cb((BIO *)bio, c->state, ret);
  257. }
  258. end:
  259. return ret;
  260. }
  261. static BIO_CONNECT *BIO_CONNECT_new(void) {
  262. BIO_CONNECT *ret = OPENSSL_malloc(sizeof(BIO_CONNECT));
  263. if (ret == NULL) {
  264. return NULL;
  265. }
  266. memset(ret, 0, sizeof(BIO_CONNECT));
  267. ret->state = BIO_CONN_S_BEFORE;
  268. return ret;
  269. }
  270. static void BIO_CONNECT_free(BIO_CONNECT *c) {
  271. if (c == NULL) {
  272. return;
  273. }
  274. OPENSSL_free(c->param_hostname);
  275. OPENSSL_free(c->param_port);
  276. OPENSSL_free(c);
  277. }
  278. static int conn_new(BIO *bio) {
  279. bio->init = 0;
  280. bio->num = -1;
  281. bio->flags = 0;
  282. bio->ptr = (char *)BIO_CONNECT_new();
  283. return bio->ptr != NULL;
  284. }
  285. static void conn_close_socket(BIO *bio) {
  286. BIO_CONNECT *c = (BIO_CONNECT *) bio->ptr;
  287. if (bio->num == -1) {
  288. return;
  289. }
  290. /* Only do a shutdown if things were established */
  291. if (c->state == BIO_CONN_S_OK) {
  292. shutdown(bio->num, 2);
  293. }
  294. closesocket(bio->num);
  295. bio->num = -1;
  296. }
  297. static int conn_free(BIO *bio) {
  298. if (bio == NULL) {
  299. return 0;
  300. }
  301. if (bio->shutdown) {
  302. conn_close_socket(bio);
  303. }
  304. BIO_CONNECT_free((BIO_CONNECT*) bio->ptr);
  305. return 1;
  306. }
  307. static int conn_read(BIO *bio, char *out, int out_len) {
  308. int ret = 0;
  309. BIO_CONNECT *data;
  310. data = (BIO_CONNECT *)bio->ptr;
  311. if (data->state != BIO_CONN_S_OK) {
  312. ret = conn_state(bio, data);
  313. if (ret <= 0) {
  314. return ret;
  315. }
  316. }
  317. bio_clear_socket_error();
  318. ret = recv(bio->num, out, out_len, 0);
  319. BIO_clear_retry_flags(bio);
  320. if (ret <= 0) {
  321. if (bio_fd_should_retry(ret)) {
  322. BIO_set_retry_read(bio);
  323. }
  324. }
  325. return ret;
  326. }
  327. static int conn_write(BIO *bio, const char *in, int in_len) {
  328. int ret;
  329. BIO_CONNECT *data;
  330. data = (BIO_CONNECT *)bio->ptr;
  331. if (data->state != BIO_CONN_S_OK) {
  332. ret = conn_state(bio, data);
  333. if (ret <= 0) {
  334. return ret;
  335. }
  336. }
  337. bio_clear_socket_error();
  338. ret = send(bio->num, in, in_len, 0);
  339. BIO_clear_retry_flags(bio);
  340. if (ret <= 0) {
  341. if (bio_fd_should_retry(ret)) {
  342. BIO_set_retry_write(bio);
  343. }
  344. }
  345. return ret;
  346. }
  347. static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) {
  348. int *ip;
  349. long ret = 1;
  350. BIO_CONNECT *data;
  351. data = (BIO_CONNECT *)bio->ptr;
  352. switch (cmd) {
  353. case BIO_CTRL_RESET:
  354. ret = 0;
  355. data->state = BIO_CONN_S_BEFORE;
  356. conn_close_socket(bio);
  357. bio->flags = 0;
  358. break;
  359. case BIO_C_DO_STATE_MACHINE:
  360. /* use this one to start the connection */
  361. if (data->state != BIO_CONN_S_OK) {
  362. ret = (long)conn_state(bio, data);
  363. } else {
  364. ret = 1;
  365. }
  366. break;
  367. case BIO_C_SET_CONNECT:
  368. if (ptr != NULL) {
  369. bio->init = 1;
  370. if (num == 0) {
  371. OPENSSL_free(data->param_hostname);
  372. data->param_hostname = BUF_strdup(ptr);
  373. if (data->param_hostname == NULL) {
  374. ret = 0;
  375. }
  376. } else if (num == 1) {
  377. OPENSSL_free(data->param_port);
  378. data->param_port = BUF_strdup(ptr);
  379. if (data->param_port == NULL) {
  380. ret = 0;
  381. }
  382. } else {
  383. ret = 0;
  384. }
  385. }
  386. break;
  387. case BIO_C_SET_NBIO:
  388. data->nbio = (int)num;
  389. break;
  390. case BIO_C_GET_FD:
  391. if (bio->init) {
  392. ip = (int *)ptr;
  393. if (ip != NULL) {
  394. *ip = bio->num;
  395. }
  396. ret = bio->num;
  397. } else {
  398. ret = -1;
  399. }
  400. break;
  401. case BIO_CTRL_GET_CLOSE:
  402. ret = bio->shutdown;
  403. break;
  404. case BIO_CTRL_SET_CLOSE:
  405. bio->shutdown = (int)num;
  406. break;
  407. case BIO_CTRL_PENDING:
  408. case BIO_CTRL_WPENDING:
  409. ret = 0;
  410. break;
  411. case BIO_CTRL_FLUSH:
  412. break;
  413. case BIO_CTRL_SET_CALLBACK: {
  414. #if 0 /* FIXME: Should this be used? -- Richard Levitte */
  415. OPENSSL_PUT_ERROR(BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  416. ret = -1;
  417. #else
  418. ret = 0;
  419. #endif
  420. } break;
  421. case BIO_CTRL_GET_CALLBACK: {
  422. int (**fptr)(const BIO *bio, int state, int xret);
  423. fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
  424. *fptr = data->info_callback;
  425. } break;
  426. default:
  427. ret = 0;
  428. break;
  429. }
  430. return (ret);
  431. }
  432. static long conn_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  433. long ret = 1;
  434. BIO_CONNECT *data;
  435. data = (BIO_CONNECT *)bio->ptr;
  436. switch (cmd) {
  437. case BIO_CTRL_SET_CALLBACK: {
  438. data->info_callback = (int (*)(const struct bio_st *, int, int))fp;
  439. } break;
  440. default:
  441. ret = 0;
  442. break;
  443. }
  444. return ret;
  445. }
  446. static int conn_puts(BIO *bp, const char *str) {
  447. return conn_write(bp, str, strlen(str));
  448. }
  449. BIO *BIO_new_connect(const char *hostname) {
  450. BIO *ret;
  451. ret = BIO_new(BIO_s_connect());
  452. if (ret == NULL) {
  453. return NULL;
  454. }
  455. if (!BIO_set_conn_hostname(ret, hostname)) {
  456. BIO_free(ret);
  457. return NULL;
  458. }
  459. return ret;
  460. }
  461. static const BIO_METHOD methods_connectp = {
  462. BIO_TYPE_CONNECT, "socket connect", conn_write, conn_read,
  463. conn_puts, NULL /* connect_gets, */, conn_ctrl, conn_new,
  464. conn_free, conn_callback_ctrl,
  465. };
  466. const BIO_METHOD *BIO_s_connect(void) { return &methods_connectp; }
  467. int BIO_set_conn_hostname(BIO *bio, const char *name) {
  468. return BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, (void*) name);
  469. }
  470. int BIO_set_conn_port(BIO *bio, const char *port_str) {
  471. return BIO_ctrl(bio, BIO_C_SET_CONNECT, 1, (void*) port_str);
  472. }
  473. int BIO_set_conn_int_port(BIO *bio, const int *port) {
  474. char buf[DECIMAL_SIZE(int) + 1];
  475. BIO_snprintf(buf, sizeof(buf), "%d", *port);
  476. return BIO_set_conn_port(bio, buf);
  477. }
  478. int BIO_set_nbio(BIO *bio, int on) {
  479. return BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL);
  480. }
  481. int BIO_do_connect(BIO *bio) {
  482. return BIO_ctrl(bio, BIO_C_DO_STATE_MACHINE, 0, NULL);
  483. }