Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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