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.
 
 
 
 
 
 

543 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. if (c->param_port != NULL) {
  144. OPENSSL_free(c->param_port);
  145. }
  146. c->param_port = BUF_strdup(p);
  147. }
  148. }
  149. if (c->param_port == NULL) {
  150. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_PORT_SPECIFIED);
  151. ERR_add_error_data(2, "host=", c->param_hostname);
  152. goto exit_loop;
  153. }
  154. if (!bio_ip_and_port_to_socket_and_addr(
  155. &bio->num, &c->them, &c->them_length, c->param_hostname,
  156. c->param_port)) {
  157. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_UNABLE_TO_CREATE_SOCKET);
  158. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  159. goto exit_loop;
  160. }
  161. memset(c->ip, 0, 4);
  162. maybe_copy_ipv4_address(c->ip, &c->them);
  163. if (c->nbio) {
  164. if (!bio_socket_nbio(bio->num, 1)) {
  165. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_ERROR_SETTING_NBIO);
  166. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  167. c->param_port);
  168. goto exit_loop;
  169. }
  170. }
  171. i = 1;
  172. ret = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
  173. sizeof(i));
  174. if (ret < 0) {
  175. OPENSSL_PUT_SYSTEM_ERROR(setsockopt);
  176. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_KEEPALIVE);
  177. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  178. goto exit_loop;
  179. }
  180. BIO_clear_retry_flags(bio);
  181. ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length);
  182. if (ret < 0) {
  183. if (bio_fd_should_retry(ret)) {
  184. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  185. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  186. bio->retry_reason = BIO_RR_CONNECT;
  187. } else {
  188. OPENSSL_PUT_SYSTEM_ERROR(connect);
  189. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_CONNECT_ERROR);
  190. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  191. c->param_port);
  192. }
  193. goto exit_loop;
  194. } else {
  195. c->state = BIO_CONN_S_OK;
  196. }
  197. break;
  198. case BIO_CONN_S_BLOCKED_CONNECT:
  199. i = bio_sock_error(bio->num);
  200. if (i) {
  201. if (bio_fd_should_retry(ret)) {
  202. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  203. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  204. bio->retry_reason = BIO_RR_CONNECT;
  205. ret = -1;
  206. } else {
  207. BIO_clear_retry_flags(bio);
  208. OPENSSL_PUT_SYSTEM_ERROR(connect);
  209. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NBIO_CONNECT_ERROR);
  210. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  211. ret = 0;
  212. }
  213. goto exit_loop;
  214. } else {
  215. c->state = BIO_CONN_S_OK;
  216. }
  217. break;
  218. case BIO_CONN_S_OK:
  219. ret = 1;
  220. goto exit_loop;
  221. default:
  222. assert(0);
  223. goto exit_loop;
  224. }
  225. if (cb != NULL) {
  226. ret = cb((BIO *)bio, c->state, ret);
  227. if (ret == 0) {
  228. goto end;
  229. }
  230. }
  231. }
  232. exit_loop:
  233. if (cb != NULL) {
  234. ret = cb((BIO *)bio, c->state, ret);
  235. }
  236. end:
  237. return ret;
  238. }
  239. static BIO_CONNECT *BIO_CONNECT_new(void) {
  240. BIO_CONNECT *ret = OPENSSL_malloc(sizeof(BIO_CONNECT));
  241. if (ret == NULL) {
  242. return NULL;
  243. }
  244. memset(ret, 0, sizeof(BIO_CONNECT));
  245. ret->state = BIO_CONN_S_BEFORE;
  246. return ret;
  247. }
  248. static void BIO_CONNECT_free(BIO_CONNECT *c) {
  249. if (c == NULL) {
  250. return;
  251. }
  252. if (c->param_hostname != NULL) {
  253. OPENSSL_free(c->param_hostname);
  254. }
  255. if (c->param_port != NULL) {
  256. OPENSSL_free(c->param_port);
  257. }
  258. OPENSSL_free(c);
  259. }
  260. static int conn_new(BIO *bio) {
  261. bio->init = 0;
  262. bio->num = -1;
  263. bio->flags = 0;
  264. bio->ptr = (char *)BIO_CONNECT_new();
  265. return bio->ptr != NULL;
  266. }
  267. static void conn_close_socket(BIO *bio) {
  268. BIO_CONNECT *c = (BIO_CONNECT *) bio->ptr;
  269. if (bio->num == -1) {
  270. return;
  271. }
  272. /* Only do a shutdown if things were established */
  273. if (c->state == BIO_CONN_S_OK) {
  274. shutdown(bio->num, 2);
  275. }
  276. closesocket(bio->num);
  277. bio->num = -1;
  278. }
  279. static int conn_free(BIO *bio) {
  280. if (bio == NULL) {
  281. return 0;
  282. }
  283. if (bio->shutdown) {
  284. conn_close_socket(bio);
  285. }
  286. BIO_CONNECT_free((BIO_CONNECT*) bio->ptr);
  287. return 1;
  288. }
  289. static int conn_read(BIO *bio, char *out, int out_len) {
  290. int ret = 0;
  291. BIO_CONNECT *data;
  292. data = (BIO_CONNECT *)bio->ptr;
  293. if (data->state != BIO_CONN_S_OK) {
  294. ret = conn_state(bio, data);
  295. if (ret <= 0) {
  296. return ret;
  297. }
  298. }
  299. bio_clear_socket_error();
  300. ret = recv(bio->num, out, out_len, 0);
  301. BIO_clear_retry_flags(bio);
  302. if (ret <= 0) {
  303. if (bio_fd_should_retry(ret)) {
  304. BIO_set_retry_read(bio);
  305. }
  306. }
  307. return ret;
  308. }
  309. static int conn_write(BIO *bio, const char *in, int in_len) {
  310. int ret;
  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 = send(bio->num, in, in_len, 0);
  321. BIO_clear_retry_flags(bio);
  322. if (ret <= 0) {
  323. if (bio_fd_should_retry(ret)) {
  324. BIO_set_retry_write(bio);
  325. }
  326. }
  327. return ret;
  328. }
  329. static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) {
  330. int *ip;
  331. const char **pptr;
  332. long ret = 1;
  333. BIO_CONNECT *data;
  334. data = (BIO_CONNECT *)bio->ptr;
  335. switch (cmd) {
  336. case BIO_CTRL_RESET:
  337. ret = 0;
  338. data->state = BIO_CONN_S_BEFORE;
  339. conn_close_socket(bio);
  340. bio->flags = 0;
  341. break;
  342. case BIO_C_DO_STATE_MACHINE:
  343. /* use this one to start the connection */
  344. if (data->state != BIO_CONN_S_OK) {
  345. ret = (long)conn_state(bio, data);
  346. } else {
  347. ret = 1;
  348. }
  349. break;
  350. case BIO_C_GET_CONNECT:
  351. /* TODO(fork): can this be removed? (Or maybe this whole file). */
  352. if (ptr != NULL) {
  353. pptr = (const char **)ptr;
  354. if (num == 0) {
  355. *pptr = data->param_hostname;
  356. } else if (num == 1) {
  357. *pptr = data->param_port;
  358. } else if (num == 2) {
  359. *pptr = (char *) &data->ip[0];
  360. } else if (num == 3) {
  361. *((int *)ptr) = data->port;
  362. }
  363. if (!bio->init) {
  364. *pptr = "not initialized";
  365. }
  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. if (data->param_hostname != NULL) {
  374. OPENSSL_free(data->param_hostname);
  375. }
  376. data->param_hostname = BUF_strdup(ptr);
  377. } else if (num == 1) {
  378. if (data->param_port != NULL) {
  379. OPENSSL_free(data->param_port);
  380. }
  381. data->param_port = BUF_strdup(ptr);
  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 = 1;
  397. } else {
  398. ret = 0;
  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, XXX, 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_nbio(BIO *bio, int on) {
  474. return BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL);
  475. }