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.
 
 
 
 
 
 

539 lines
14 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 <stdio.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. #pragma warning(push, 3)
  68. #include <winsock2.h>
  69. #include <ws2tcpip.h>
  70. #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. 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. uint8_t ip[4];
  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. /* maybe_copy_ipv4_address sets |*ipv4| to the IPv4 address from |ss| (in
  103. * big-endian order), if |ss| contains an IPv4 socket address. */
  104. static void maybe_copy_ipv4_address(uint8_t *ipv4,
  105. const struct sockaddr_storage *ss) {
  106. const struct sockaddr_in *sin;
  107. if (ss->ss_family != AF_INET) {
  108. return;
  109. }
  110. sin = (const struct sockaddr_in*) ss;
  111. memcpy(ipv4, &sin->sin_addr, 4);
  112. }
  113. static int conn_state(BIO *bio, BIO_CONNECT *c) {
  114. int ret = -1, i;
  115. char *p, *q;
  116. int (*cb)(const BIO *, int, int) = NULL;
  117. if (c->info_callback != NULL) {
  118. cb = c->info_callback;
  119. }
  120. for (;;) {
  121. switch (c->state) {
  122. case BIO_CONN_S_BEFORE:
  123. p = c->param_hostname;
  124. if (p == NULL) {
  125. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_HOSTNAME_SPECIFIED);
  126. goto exit_loop;
  127. }
  128. for (; *p != 0; p++) {
  129. if (*p == ':' || *p == '/') {
  130. break;
  131. }
  132. }
  133. i = *p;
  134. if (i == ':' || i == '/') {
  135. *(p++) = 0;
  136. if (i == ':') {
  137. for (q = p; *q; q++) {
  138. if (*q == '/') {
  139. *q = 0;
  140. break;
  141. }
  142. }
  143. OPENSSL_free(c->param_port);
  144. c->param_port = BUF_strdup(p);
  145. }
  146. }
  147. if (c->param_port == NULL) {
  148. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_PORT_SPECIFIED);
  149. ERR_add_error_data(2, "host=", c->param_hostname);
  150. goto exit_loop;
  151. }
  152. if (!bio_ip_and_port_to_socket_and_addr(
  153. &bio->num, &c->them, &c->them_length, c->param_hostname,
  154. c->param_port)) {
  155. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_UNABLE_TO_CREATE_SOCKET);
  156. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  157. goto exit_loop;
  158. }
  159. memset(c->ip, 0, 4);
  160. maybe_copy_ipv4_address(c->ip, &c->them);
  161. if (c->nbio) {
  162. if (!bio_socket_nbio(bio->num, 1)) {
  163. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_ERROR_SETTING_NBIO);
  164. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  165. c->param_port);
  166. goto exit_loop;
  167. }
  168. }
  169. i = 1;
  170. ret = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
  171. sizeof(i));
  172. if (ret < 0) {
  173. OPENSSL_PUT_SYSTEM_ERROR(setsockopt);
  174. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_KEEPALIVE);
  175. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  176. goto exit_loop;
  177. }
  178. BIO_clear_retry_flags(bio);
  179. ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length);
  180. if (ret < 0) {
  181. if (bio_fd_should_retry(ret)) {
  182. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  183. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  184. bio->retry_reason = BIO_RR_CONNECT;
  185. } else {
  186. OPENSSL_PUT_SYSTEM_ERROR(connect);
  187. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_CONNECT_ERROR);
  188. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  189. c->param_port);
  190. }
  191. goto exit_loop;
  192. } else {
  193. c->state = BIO_CONN_S_OK;
  194. }
  195. break;
  196. case BIO_CONN_S_BLOCKED_CONNECT:
  197. i = bio_sock_error(bio->num);
  198. if (i) {
  199. if (bio_fd_should_retry(ret)) {
  200. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  201. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  202. bio->retry_reason = BIO_RR_CONNECT;
  203. ret = -1;
  204. } else {
  205. BIO_clear_retry_flags(bio);
  206. OPENSSL_PUT_SYSTEM_ERROR(connect);
  207. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NBIO_CONNECT_ERROR);
  208. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  209. ret = 0;
  210. }
  211. goto exit_loop;
  212. } else {
  213. c->state = BIO_CONN_S_OK;
  214. }
  215. break;
  216. case BIO_CONN_S_OK:
  217. ret = 1;
  218. goto exit_loop;
  219. default:
  220. assert(0);
  221. goto exit_loop;
  222. }
  223. if (cb != NULL) {
  224. ret = cb((BIO *)bio, c->state, ret);
  225. if (ret == 0) {
  226. goto end;
  227. }
  228. }
  229. }
  230. exit_loop:
  231. if (cb != NULL) {
  232. ret = cb((BIO *)bio, c->state, ret);
  233. }
  234. end:
  235. return ret;
  236. }
  237. static BIO_CONNECT *BIO_CONNECT_new(void) {
  238. BIO_CONNECT *ret = OPENSSL_malloc(sizeof(BIO_CONNECT));
  239. if (ret == NULL) {
  240. return NULL;
  241. }
  242. memset(ret, 0, sizeof(BIO_CONNECT));
  243. ret->state = BIO_CONN_S_BEFORE;
  244. return ret;
  245. }
  246. static void BIO_CONNECT_free(BIO_CONNECT *c) {
  247. if (c == NULL) {
  248. return;
  249. }
  250. OPENSSL_free(c->param_hostname);
  251. OPENSSL_free(c->param_port);
  252. OPENSSL_free(c);
  253. }
  254. static int conn_new(BIO *bio) {
  255. bio->init = 0;
  256. bio->num = -1;
  257. bio->flags = 0;
  258. bio->ptr = (char *)BIO_CONNECT_new();
  259. return bio->ptr != NULL;
  260. }
  261. static void conn_close_socket(BIO *bio) {
  262. BIO_CONNECT *c = (BIO_CONNECT *) bio->ptr;
  263. if (bio->num == -1) {
  264. return;
  265. }
  266. /* Only do a shutdown if things were established */
  267. if (c->state == BIO_CONN_S_OK) {
  268. shutdown(bio->num, 2);
  269. }
  270. closesocket(bio->num);
  271. bio->num = -1;
  272. }
  273. static int conn_free(BIO *bio) {
  274. if (bio == NULL) {
  275. return 0;
  276. }
  277. if (bio->shutdown) {
  278. conn_close_socket(bio);
  279. }
  280. BIO_CONNECT_free((BIO_CONNECT*) bio->ptr);
  281. return 1;
  282. }
  283. static int conn_read(BIO *bio, char *out, int out_len) {
  284. int ret = 0;
  285. BIO_CONNECT *data;
  286. data = (BIO_CONNECT *)bio->ptr;
  287. if (data->state != BIO_CONN_S_OK) {
  288. ret = conn_state(bio, data);
  289. if (ret <= 0) {
  290. return ret;
  291. }
  292. }
  293. bio_clear_socket_error();
  294. ret = recv(bio->num, out, out_len, 0);
  295. BIO_clear_retry_flags(bio);
  296. if (ret <= 0) {
  297. if (bio_fd_should_retry(ret)) {
  298. BIO_set_retry_read(bio);
  299. }
  300. }
  301. return ret;
  302. }
  303. static int conn_write(BIO *bio, const char *in, int in_len) {
  304. int ret;
  305. BIO_CONNECT *data;
  306. data = (BIO_CONNECT *)bio->ptr;
  307. if (data->state != BIO_CONN_S_OK) {
  308. ret = conn_state(bio, data);
  309. if (ret <= 0) {
  310. return ret;
  311. }
  312. }
  313. bio_clear_socket_error();
  314. ret = send(bio->num, in, in_len, 0);
  315. BIO_clear_retry_flags(bio);
  316. if (ret <= 0) {
  317. if (bio_fd_should_retry(ret)) {
  318. BIO_set_retry_write(bio);
  319. }
  320. }
  321. return ret;
  322. }
  323. static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) {
  324. int *ip;
  325. const char **pptr;
  326. long ret = 1;
  327. BIO_CONNECT *data;
  328. data = (BIO_CONNECT *)bio->ptr;
  329. switch (cmd) {
  330. case BIO_CTRL_RESET:
  331. ret = 0;
  332. data->state = BIO_CONN_S_BEFORE;
  333. conn_close_socket(bio);
  334. bio->flags = 0;
  335. break;
  336. case BIO_C_DO_STATE_MACHINE:
  337. /* use this one to start the connection */
  338. if (data->state != BIO_CONN_S_OK) {
  339. ret = (long)conn_state(bio, data);
  340. } else {
  341. ret = 1;
  342. }
  343. break;
  344. case BIO_C_GET_CONNECT:
  345. /* TODO(fork): can this be removed? (Or maybe this whole file). */
  346. if (ptr != NULL) {
  347. pptr = (const char **)ptr;
  348. if (num == 0) {
  349. *pptr = data->param_hostname;
  350. } else if (num == 1) {
  351. *pptr = data->param_port;
  352. } else if (num == 2) {
  353. *pptr = (char *) &data->ip[0];
  354. } else if (num == 3) {
  355. *((int *)ptr) = data->port;
  356. }
  357. if (!bio->init) {
  358. *pptr = "not initialized";
  359. }
  360. ret = 1;
  361. }
  362. break;
  363. case BIO_C_SET_CONNECT:
  364. if (ptr != NULL) {
  365. bio->init = 1;
  366. if (num == 0) {
  367. OPENSSL_free(data->param_hostname);
  368. data->param_hostname = BUF_strdup(ptr);
  369. if (data->param_hostname == NULL) {
  370. ret = 0;
  371. }
  372. } else if (num == 1) {
  373. OPENSSL_free(data->param_port);
  374. data->param_port = BUF_strdup(ptr);
  375. if (data->param_port == NULL) {
  376. ret = 0;
  377. }
  378. } else {
  379. ret = 0;
  380. }
  381. }
  382. break;
  383. case BIO_C_SET_NBIO:
  384. data->nbio = (int)num;
  385. break;
  386. case BIO_C_GET_FD:
  387. if (bio->init) {
  388. ip = (int *)ptr;
  389. if (ip != NULL) {
  390. *ip = bio->num;
  391. }
  392. ret = 1;
  393. } else {
  394. ret = 0;
  395. }
  396. break;
  397. case BIO_CTRL_GET_CLOSE:
  398. ret = bio->shutdown;
  399. break;
  400. case BIO_CTRL_SET_CLOSE:
  401. bio->shutdown = (int)num;
  402. break;
  403. case BIO_CTRL_PENDING:
  404. case BIO_CTRL_WPENDING:
  405. ret = 0;
  406. break;
  407. case BIO_CTRL_FLUSH:
  408. break;
  409. case BIO_CTRL_SET_CALLBACK: {
  410. #if 0 /* FIXME: Should this be used? -- Richard Levitte */
  411. OPENSSL_PUT_ERROR(BIO, XXX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  412. ret = -1;
  413. #else
  414. ret = 0;
  415. #endif
  416. } break;
  417. case BIO_CTRL_GET_CALLBACK: {
  418. int (**fptr)(const BIO *bio, int state, int xret);
  419. fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
  420. *fptr = data->info_callback;
  421. } break;
  422. default:
  423. ret = 0;
  424. break;
  425. }
  426. return (ret);
  427. }
  428. static long conn_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  429. long ret = 1;
  430. BIO_CONNECT *data;
  431. data = (BIO_CONNECT *)bio->ptr;
  432. switch (cmd) {
  433. case BIO_CTRL_SET_CALLBACK: {
  434. data->info_callback = (int (*)(const struct bio_st *, int, int))fp;
  435. } break;
  436. default:
  437. ret = 0;
  438. break;
  439. }
  440. return ret;
  441. }
  442. static int conn_puts(BIO *bp, const char *str) {
  443. return conn_write(bp, str, strlen(str));
  444. }
  445. BIO *BIO_new_connect(const char *hostname) {
  446. BIO *ret;
  447. ret = BIO_new(BIO_s_connect());
  448. if (ret == NULL) {
  449. return NULL;
  450. }
  451. if (!BIO_set_conn_hostname(ret, hostname)) {
  452. BIO_free(ret);
  453. return NULL;
  454. }
  455. return ret;
  456. }
  457. static const BIO_METHOD methods_connectp = {
  458. BIO_TYPE_CONNECT, "socket connect", conn_write, conn_read,
  459. conn_puts, NULL /* connect_gets, */, conn_ctrl, conn_new,
  460. conn_free, conn_callback_ctrl,
  461. };
  462. const BIO_METHOD *BIO_s_connect(void) { return &methods_connectp; }
  463. int BIO_set_conn_hostname(BIO *bio, const char *name) {
  464. return BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, (void*) name);
  465. }
  466. int BIO_set_conn_port(BIO *bio, const char *port_str) {
  467. return BIO_ctrl(bio, BIO_C_SET_CONNECT, 1, (void*) port_str);
  468. }
  469. int BIO_set_nbio(BIO *bio, int on) {
  470. return BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL);
  471. }