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.
 
 
 
 
 
 

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