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.
 
 
 
 
 
 

536 rivejä
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. #if !defined(OPENSSL_WINDOWS)
  61. #include <sys/socket.h>
  62. #include <netinet/in.h>
  63. #include <arpa/inet.h>
  64. #include <unistd.h>
  65. #endif
  66. #include <openssl/buf.h>
  67. #include <openssl/err.h>
  68. #include <openssl/mem.h>
  69. #include "internal.h"
  70. enum {
  71. BIO_CONN_S_BEFORE,
  72. BIO_CONN_S_BLOCKED_CONNECT,
  73. BIO_CONN_S_OK,
  74. };
  75. typedef struct bio_connect_st {
  76. int state;
  77. char *param_hostname;
  78. char *param_port;
  79. int nbio;
  80. uint8_t ip[4];
  81. unsigned short port;
  82. struct sockaddr_storage them;
  83. socklen_t them_length;
  84. /* the file descriptor is kept in bio->num in order to match the socket
  85. * BIO. */
  86. /* info_callback is called when the connection is initially made
  87. * callback(BIO,state,ret); The callback should return 'ret', state is for
  88. * compatibility with the SSL info_callback. */
  89. int (*info_callback)(const BIO *bio, int state, int ret);
  90. } BIO_CONNECT;
  91. #if !defined(OPENSSL_WINDOWS)
  92. static int closesocket(int sock) {
  93. return close(sock);
  94. }
  95. #endif
  96. /* maybe_copy_ipv4_address sets |*ipv4| to the IPv4 address from |ss| (in
  97. * big-endian order), if |ss| contains an IPv4 socket address. */
  98. static void maybe_copy_ipv4_address(uint8_t *ipv4,
  99. const struct sockaddr_storage *ss) {
  100. const struct sockaddr_in *sin;
  101. if (ss->ss_family != AF_INET) {
  102. return;
  103. }
  104. sin = (const struct sockaddr_in*) ss;
  105. memcpy(ipv4, &sin->sin_addr, 4);
  106. }
  107. static int conn_state(BIO *bio, BIO_CONNECT *c) {
  108. int ret = -1, i;
  109. char *p, *q;
  110. int (*cb)(const BIO *, int, int) = NULL;
  111. if (c->info_callback != NULL) {
  112. cb = c->info_callback;
  113. }
  114. for (;;) {
  115. switch (c->state) {
  116. case BIO_CONN_S_BEFORE:
  117. p = c->param_hostname;
  118. if (p == NULL) {
  119. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_HOSTNAME_SPECIFIED);
  120. goto exit_loop;
  121. }
  122. for (; *p != 0; p++) {
  123. if (*p == ':' || *p == '/') {
  124. break;
  125. }
  126. }
  127. i = *p;
  128. if (i == ':' || i == '/') {
  129. *(p++) = 0;
  130. if (i == ':') {
  131. for (q = p; *q; q++) {
  132. if (*q == '/') {
  133. *q = 0;
  134. break;
  135. }
  136. }
  137. if (c->param_port != NULL) {
  138. OPENSSL_free(c->param_port);
  139. }
  140. c->param_port = BUF_strdup(p);
  141. }
  142. }
  143. if (c->param_port == NULL) {
  144. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_PORT_SPECIFIED);
  145. ERR_add_error_data(2, "host=", c->param_hostname);
  146. goto exit_loop;
  147. }
  148. if (!bio_ip_and_port_to_socket_and_addr(
  149. &bio->num, &c->them, &c->them_length, c->param_hostname,
  150. c->param_port)) {
  151. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_UNABLE_TO_CREATE_SOCKET);
  152. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  153. goto exit_loop;
  154. }
  155. memset(c->ip, 0, 4);
  156. maybe_copy_ipv4_address(c->ip, &c->them);
  157. if (c->nbio) {
  158. if (!bio_socket_nbio(bio->num, 1)) {
  159. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_ERROR_SETTING_NBIO);
  160. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  161. c->param_port);
  162. goto exit_loop;
  163. }
  164. }
  165. i = 1;
  166. ret = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
  167. sizeof(i));
  168. if (ret < 0) {
  169. OPENSSL_PUT_SYSTEM_ERROR(setsockopt);
  170. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_KEEPALIVE);
  171. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  172. goto exit_loop;
  173. }
  174. BIO_clear_retry_flags(bio);
  175. ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length);
  176. if (ret < 0) {
  177. if (bio_fd_should_retry(ret)) {
  178. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  179. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  180. bio->retry_reason = BIO_RR_CONNECT;
  181. } else {
  182. OPENSSL_PUT_SYSTEM_ERROR(connect);
  183. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_CONNECT_ERROR);
  184. ERR_add_error_data(4, "host=", c->param_hostname, ":",
  185. c->param_port);
  186. }
  187. goto exit_loop;
  188. } else {
  189. c->state = BIO_CONN_S_OK;
  190. }
  191. break;
  192. case BIO_CONN_S_BLOCKED_CONNECT:
  193. i = bio_sock_error(bio->num);
  194. if (i) {
  195. if (bio_fd_should_retry(ret)) {
  196. BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
  197. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  198. bio->retry_reason = BIO_RR_CONNECT;
  199. ret = -1;
  200. } else {
  201. BIO_clear_retry_flags(bio);
  202. OPENSSL_PUT_SYSTEM_ERROR(connect);
  203. OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NBIO_CONNECT_ERROR);
  204. ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
  205. ret = 0;
  206. }
  207. goto exit_loop;
  208. } else {
  209. c->state = BIO_CONN_S_OK;
  210. }
  211. break;
  212. case BIO_CONN_S_OK:
  213. ret = 1;
  214. goto exit_loop;
  215. default:
  216. assert(0);
  217. goto exit_loop;
  218. }
  219. if (cb != NULL) {
  220. ret = cb((BIO *)bio, c->state, ret);
  221. if (ret == 0) {
  222. goto end;
  223. }
  224. }
  225. }
  226. exit_loop:
  227. if (cb != NULL) {
  228. ret = cb((BIO *)bio, c->state, ret);
  229. }
  230. end:
  231. return ret;
  232. }
  233. static BIO_CONNECT *BIO_CONNECT_new(void) {
  234. BIO_CONNECT *ret = OPENSSL_malloc(sizeof(BIO_CONNECT));
  235. if (ret == NULL) {
  236. return NULL;
  237. }
  238. memset(ret, 0, sizeof(BIO_CONNECT));
  239. ret->state = BIO_CONN_S_BEFORE;
  240. return ret;
  241. }
  242. static void BIO_CONNECT_free(BIO_CONNECT *c) {
  243. if (c == NULL) {
  244. return;
  245. }
  246. if (c->param_hostname != NULL) {
  247. OPENSSL_free(c->param_hostname);
  248. }
  249. if (c->param_port != NULL) {
  250. OPENSSL_free(c->param_port);
  251. }
  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. break;
  343. case BIO_C_GET_CONNECT:
  344. /* TODO(fork): can this be removed? (Or maybe this whole file). */
  345. if (ptr != NULL) {
  346. pptr = (const char **)ptr;
  347. if (num == 0) {
  348. *pptr = data->param_hostname;
  349. } else if (num == 1) {
  350. *pptr = data->param_port;
  351. } else if (num == 2) {
  352. *pptr = (char *) &data->ip[0];
  353. } else if (num == 3) {
  354. *((int *)ptr) = data->port;
  355. }
  356. if (!bio->init) {
  357. *pptr = "not initialized";
  358. }
  359. ret = 1;
  360. }
  361. break;
  362. case BIO_C_SET_CONNECT:
  363. if (ptr != NULL) {
  364. bio->init = 1;
  365. if (num == 0) {
  366. if (data->param_hostname != NULL) {
  367. OPENSSL_free(data->param_hostname);
  368. }
  369. data->param_hostname = BUF_strdup(ptr);
  370. } else if (num == 1) {
  371. if (data->param_port != NULL) {
  372. OPENSSL_free(data->param_port);
  373. }
  374. data->param_port = BUF_strdup(ptr);
  375. } else {
  376. ret = 0;
  377. }
  378. }
  379. break;
  380. case BIO_C_SET_NBIO:
  381. data->nbio = (int)num;
  382. break;
  383. case BIO_C_GET_FD:
  384. if (bio->init) {
  385. ip = (int *)ptr;
  386. if (ip != NULL) {
  387. *ip = bio->num;
  388. }
  389. ret = 1;
  390. } else {
  391. ret = 0;
  392. }
  393. break;
  394. case BIO_CTRL_GET_CLOSE:
  395. ret = bio->shutdown;
  396. break;
  397. case BIO_CTRL_SET_CLOSE:
  398. bio->shutdown = (int)num;
  399. break;
  400. case BIO_CTRL_PENDING:
  401. case BIO_CTRL_WPENDING:
  402. ret = 0;
  403. break;
  404. case BIO_CTRL_FLUSH:
  405. break;
  406. case BIO_CTRL_SET_CALLBACK: {
  407. #if 0 /* FIXME: Should this be used? -- Richard Levitte */
  408. OPENSSL_PUT_ERROR(BIO, XXX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  409. ret = -1;
  410. #else
  411. ret = 0;
  412. #endif
  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. static int conn_puts(BIO *bp, const char *str) {
  440. return conn_write(bp, str, strlen(str));
  441. }
  442. BIO *BIO_new_connect(const char *hostname) {
  443. BIO *ret;
  444. ret = BIO_new(BIO_s_connect());
  445. if (ret == NULL) {
  446. return NULL;
  447. }
  448. if (!BIO_set_conn_hostname(ret, hostname)) {
  449. BIO_free(ret);
  450. return NULL;
  451. }
  452. return ret;
  453. }
  454. static const BIO_METHOD methods_connectp = {
  455. BIO_TYPE_CONNECT, "socket connect", conn_write, conn_read,
  456. conn_puts, NULL /* connect_gets, */, conn_ctrl, conn_new,
  457. conn_free, conn_callback_ctrl,
  458. };
  459. const BIO_METHOD *BIO_s_connect(void) { return &methods_connectp; }
  460. int BIO_set_conn_hostname(BIO *bio, const char *name) {
  461. return BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, (void*) name);
  462. }
  463. int BIO_set_conn_port(BIO *bio, const char *port_str) {
  464. return BIO_ctrl(bio, BIO_C_SET_CONNECT, 1, (void*) port_str);
  465. }
  466. int BIO_set_nbio(BIO *bio, int on) {
  467. return BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL);
  468. }