bio_ssl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <openssl/crypto.h>
  14. #include "internal/bio.h"
  15. #include <openssl/err.h>
  16. #include "ssl_locl.h"
  17. static int ssl_write(BIO *h, const char *buf, size_t size, size_t *written);
  18. static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes);
  19. static int ssl_puts(BIO *h, const char *str);
  20. static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  21. static int ssl_new(BIO *h);
  22. static int ssl_free(BIO *data);
  23. static long ssl_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  24. typedef struct bio_ssl_st {
  25. SSL *ssl; /* The ssl handle :-) */
  26. /* re-negotiate every time the total number of bytes is this size */
  27. int num_renegotiates;
  28. unsigned long renegotiate_count;
  29. size_t byte_count;
  30. unsigned long renegotiate_timeout;
  31. unsigned long last_time;
  32. } BIO_SSL;
  33. static const BIO_METHOD methods_sslp = {
  34. BIO_TYPE_SSL,
  35. "ssl",
  36. ssl_write,
  37. NULL, /* ssl_write_old, */
  38. ssl_read,
  39. NULL, /* ssl_read_old, */
  40. ssl_puts,
  41. NULL, /* ssl_gets, */
  42. ssl_ctrl,
  43. ssl_new,
  44. ssl_free,
  45. ssl_callback_ctrl,
  46. };
  47. const BIO_METHOD *BIO_f_ssl(void)
  48. {
  49. return &methods_sslp;
  50. }
  51. static int ssl_new(BIO *bi)
  52. {
  53. BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs));
  54. if (bs == NULL) {
  55. BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
  56. return 0;
  57. }
  58. BIO_set_init(bi, 0);
  59. BIO_set_data(bi, bs);
  60. /* Clear all flags */
  61. BIO_clear_flags(bi, ~0);
  62. return 1;
  63. }
  64. static int ssl_free(BIO *a)
  65. {
  66. BIO_SSL *bs;
  67. if (a == NULL)
  68. return 0;
  69. bs = BIO_get_data(a);
  70. if (bs->ssl != NULL)
  71. SSL_shutdown(bs->ssl);
  72. if (BIO_get_shutdown(a)) {
  73. if (BIO_get_init(a))
  74. SSL_free(bs->ssl);
  75. /* Clear all flags */
  76. BIO_clear_flags(a, ~0);
  77. BIO_set_init(a, 0);
  78. }
  79. OPENSSL_free(bs);
  80. return 1;
  81. }
  82. static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes)
  83. {
  84. int ret = 1;
  85. BIO_SSL *sb;
  86. SSL *ssl;
  87. int retry_reason = 0;
  88. int r = 0;
  89. if (buf == NULL)
  90. return 0;
  91. sb = BIO_get_data(b);
  92. ssl = sb->ssl;
  93. BIO_clear_retry_flags(b);
  94. ret = ssl_read_internal(ssl, buf, size, readbytes);
  95. switch (SSL_get_error(ssl, ret)) {
  96. case SSL_ERROR_NONE:
  97. if (sb->renegotiate_count > 0) {
  98. sb->byte_count += *readbytes;
  99. if (sb->byte_count > sb->renegotiate_count) {
  100. sb->byte_count = 0;
  101. sb->num_renegotiates++;
  102. SSL_renegotiate(ssl);
  103. r = 1;
  104. }
  105. }
  106. if ((sb->renegotiate_timeout > 0) && (!r)) {
  107. unsigned long tm;
  108. tm = (unsigned long)time(NULL);
  109. if (tm > sb->last_time + sb->renegotiate_timeout) {
  110. sb->last_time = tm;
  111. sb->num_renegotiates++;
  112. SSL_renegotiate(ssl);
  113. }
  114. }
  115. break;
  116. case SSL_ERROR_WANT_READ:
  117. BIO_set_retry_read(b);
  118. break;
  119. case SSL_ERROR_WANT_WRITE:
  120. BIO_set_retry_write(b);
  121. break;
  122. case SSL_ERROR_WANT_X509_LOOKUP:
  123. BIO_set_retry_special(b);
  124. retry_reason = BIO_RR_SSL_X509_LOOKUP;
  125. break;
  126. case SSL_ERROR_WANT_ACCEPT:
  127. BIO_set_retry_special(b);
  128. retry_reason = BIO_RR_ACCEPT;
  129. break;
  130. case SSL_ERROR_WANT_CONNECT:
  131. BIO_set_retry_special(b);
  132. retry_reason = BIO_RR_CONNECT;
  133. break;
  134. case SSL_ERROR_SYSCALL:
  135. case SSL_ERROR_SSL:
  136. case SSL_ERROR_ZERO_RETURN:
  137. default:
  138. break;
  139. }
  140. BIO_set_retry_reason(b, retry_reason);
  141. return ret;
  142. }
  143. static int ssl_write(BIO *b, const char *buf, size_t size, size_t *written)
  144. {
  145. int ret, r = 0;
  146. int retry_reason = 0;
  147. SSL *ssl;
  148. BIO_SSL *bs;
  149. if (buf == NULL)
  150. return 0;
  151. bs = BIO_get_data(b);
  152. ssl = bs->ssl;
  153. BIO_clear_retry_flags(b);
  154. ret = ssl_write_internal(ssl, buf, size, written);
  155. switch (SSL_get_error(ssl, ret)) {
  156. case SSL_ERROR_NONE:
  157. if (bs->renegotiate_count > 0) {
  158. bs->byte_count += *written;
  159. if (bs->byte_count > bs->renegotiate_count) {
  160. bs->byte_count = 0;
  161. bs->num_renegotiates++;
  162. SSL_renegotiate(ssl);
  163. r = 1;
  164. }
  165. }
  166. if ((bs->renegotiate_timeout > 0) && (!r)) {
  167. unsigned long tm;
  168. tm = (unsigned long)time(NULL);
  169. if (tm > bs->last_time + bs->renegotiate_timeout) {
  170. bs->last_time = tm;
  171. bs->num_renegotiates++;
  172. SSL_renegotiate(ssl);
  173. }
  174. }
  175. break;
  176. case SSL_ERROR_WANT_WRITE:
  177. BIO_set_retry_write(b);
  178. break;
  179. case SSL_ERROR_WANT_READ:
  180. BIO_set_retry_read(b);
  181. break;
  182. case SSL_ERROR_WANT_X509_LOOKUP:
  183. BIO_set_retry_special(b);
  184. retry_reason = BIO_RR_SSL_X509_LOOKUP;
  185. break;
  186. case SSL_ERROR_WANT_CONNECT:
  187. BIO_set_retry_special(b);
  188. retry_reason = BIO_RR_CONNECT;
  189. case SSL_ERROR_SYSCALL:
  190. case SSL_ERROR_SSL:
  191. default:
  192. break;
  193. }
  194. BIO_set_retry_reason(b, retry_reason);
  195. return ret;
  196. }
  197. static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
  198. {
  199. SSL **sslp, *ssl;
  200. BIO_SSL *bs, *dbs;
  201. BIO *dbio, *bio;
  202. long ret = 1;
  203. BIO *next;
  204. bs = BIO_get_data(b);
  205. next = BIO_next(b);
  206. ssl = bs->ssl;
  207. if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
  208. return 0;
  209. switch (cmd) {
  210. case BIO_CTRL_RESET:
  211. SSL_shutdown(ssl);
  212. if (ssl->handshake_func == ssl->method->ssl_connect)
  213. SSL_set_connect_state(ssl);
  214. else if (ssl->handshake_func == ssl->method->ssl_accept)
  215. SSL_set_accept_state(ssl);
  216. if (!SSL_clear(ssl)) {
  217. ret = 0;
  218. break;
  219. }
  220. if (next != NULL)
  221. ret = BIO_ctrl(next, cmd, num, ptr);
  222. else if (ssl->rbio != NULL)
  223. ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
  224. else
  225. ret = 1;
  226. break;
  227. case BIO_CTRL_INFO:
  228. ret = 0;
  229. break;
  230. case BIO_C_SSL_MODE:
  231. if (num) /* client mode */
  232. SSL_set_connect_state(ssl);
  233. else
  234. SSL_set_accept_state(ssl);
  235. break;
  236. case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
  237. ret = bs->renegotiate_timeout;
  238. if (num < 60)
  239. num = 5;
  240. bs->renegotiate_timeout = (unsigned long)num;
  241. bs->last_time = (unsigned long)time(NULL);
  242. break;
  243. case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
  244. ret = bs->renegotiate_count;
  245. if ((long)num >= 512)
  246. bs->renegotiate_count = (unsigned long)num;
  247. break;
  248. case BIO_C_GET_SSL_NUM_RENEGOTIATES:
  249. ret = bs->num_renegotiates;
  250. break;
  251. case BIO_C_SET_SSL:
  252. if (ssl != NULL) {
  253. ssl_free(b);
  254. if (!ssl_new(b))
  255. return 0;
  256. }
  257. BIO_set_shutdown(b, num);
  258. ssl = (SSL *)ptr;
  259. bs->ssl = ssl;
  260. bio = SSL_get_rbio(ssl);
  261. if (bio != NULL) {
  262. if (next != NULL)
  263. BIO_push(bio, next);
  264. BIO_set_next(b, bio);
  265. BIO_up_ref(bio);
  266. }
  267. BIO_set_init(b, 1);
  268. break;
  269. case BIO_C_GET_SSL:
  270. if (ptr != NULL) {
  271. sslp = (SSL **)ptr;
  272. *sslp = ssl;
  273. } else
  274. ret = 0;
  275. break;
  276. case BIO_CTRL_GET_CLOSE:
  277. ret = BIO_get_shutdown(b);
  278. break;
  279. case BIO_CTRL_SET_CLOSE:
  280. BIO_set_shutdown(b, (int)num);
  281. break;
  282. case BIO_CTRL_WPENDING:
  283. ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
  284. break;
  285. case BIO_CTRL_PENDING:
  286. ret = SSL_pending(ssl);
  287. if (ret == 0)
  288. ret = BIO_pending(ssl->rbio);
  289. break;
  290. case BIO_CTRL_FLUSH:
  291. BIO_clear_retry_flags(b);
  292. ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
  293. BIO_copy_next_retry(b);
  294. break;
  295. case BIO_CTRL_PUSH:
  296. if ((next != NULL) && (next != ssl->rbio)) {
  297. /*
  298. * We are going to pass ownership of next to the SSL object...but
  299. * we don't own a reference to pass yet - so up ref
  300. */
  301. BIO_up_ref(next);
  302. SSL_set_bio(ssl, next, next);
  303. }
  304. break;
  305. case BIO_CTRL_POP:
  306. /* Only detach if we are the BIO explicitly being popped */
  307. if (b == ptr) {
  308. /* This will clear the reference we obtained during push */
  309. SSL_set_bio(ssl, NULL, NULL);
  310. }
  311. break;
  312. case BIO_C_DO_STATE_MACHINE:
  313. BIO_clear_retry_flags(b);
  314. BIO_set_retry_reason(b, 0);
  315. ret = (int)SSL_do_handshake(ssl);
  316. switch (SSL_get_error(ssl, (int)ret)) {
  317. case SSL_ERROR_WANT_READ:
  318. BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
  319. break;
  320. case SSL_ERROR_WANT_WRITE:
  321. BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
  322. break;
  323. case SSL_ERROR_WANT_CONNECT:
  324. BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
  325. BIO_set_retry_reason(b, BIO_get_retry_reason(next));
  326. break;
  327. case SSL_ERROR_WANT_X509_LOOKUP:
  328. BIO_set_retry_special(b);
  329. BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
  330. break;
  331. default:
  332. break;
  333. }
  334. break;
  335. case BIO_CTRL_DUP:
  336. dbio = (BIO *)ptr;
  337. dbs = BIO_get_data(dbio);
  338. SSL_free(dbs->ssl);
  339. dbs->ssl = SSL_dup(ssl);
  340. dbs->num_renegotiates = bs->num_renegotiates;
  341. dbs->renegotiate_count = bs->renegotiate_count;
  342. dbs->byte_count = bs->byte_count;
  343. dbs->renegotiate_timeout = bs->renegotiate_timeout;
  344. dbs->last_time = bs->last_time;
  345. ret = (dbs->ssl != NULL);
  346. break;
  347. case BIO_C_GET_FD:
  348. ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
  349. break;
  350. case BIO_CTRL_SET_CALLBACK:
  351. ret = 0; /* use callback ctrl */
  352. break;
  353. default:
  354. ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
  355. break;
  356. }
  357. return ret;
  358. }
  359. static long ssl_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  360. {
  361. SSL *ssl;
  362. BIO_SSL *bs;
  363. long ret = 1;
  364. bs = BIO_get_data(b);
  365. ssl = bs->ssl;
  366. switch (cmd) {
  367. case BIO_CTRL_SET_CALLBACK:
  368. ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
  369. break;
  370. default:
  371. ret = 0;
  372. break;
  373. }
  374. return ret;
  375. }
  376. static int ssl_puts(BIO *bp, const char *str)
  377. {
  378. int n, ret;
  379. n = strlen(str);
  380. ret = BIO_write(bp, str, n);
  381. return ret;
  382. }
  383. BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
  384. {
  385. #ifndef OPENSSL_NO_SOCK
  386. BIO *ret = NULL, *buf = NULL, *ssl = NULL;
  387. if ((buf = BIO_new(BIO_f_buffer())) == NULL)
  388. return NULL;
  389. if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
  390. goto err;
  391. if ((ret = BIO_push(buf, ssl)) == NULL)
  392. goto err;
  393. return ret;
  394. err:
  395. BIO_free(buf);
  396. BIO_free(ssl);
  397. #endif
  398. return NULL;
  399. }
  400. BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
  401. {
  402. #ifndef OPENSSL_NO_SOCK
  403. BIO *ret = NULL, *con = NULL, *ssl = NULL;
  404. if ((con = BIO_new(BIO_s_connect())) == NULL)
  405. return NULL;
  406. if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
  407. goto err;
  408. if ((ret = BIO_push(ssl, con)) == NULL)
  409. goto err;
  410. return ret;
  411. err:
  412. BIO_free(con);
  413. #endif
  414. return NULL;
  415. }
  416. BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
  417. {
  418. BIO *ret;
  419. SSL *ssl;
  420. if ((ret = BIO_new(BIO_f_ssl())) == NULL)
  421. return NULL;
  422. if ((ssl = SSL_new(ctx)) == NULL) {
  423. BIO_free(ret);
  424. return NULL;
  425. }
  426. if (client)
  427. SSL_set_connect_state(ssl);
  428. else
  429. SSL_set_accept_state(ssl);
  430. BIO_set_ssl(ret, ssl, BIO_CLOSE);
  431. return ret;
  432. }
  433. int BIO_ssl_copy_session_id(BIO *t, BIO *f)
  434. {
  435. BIO_SSL *tdata, *fdata;
  436. t = BIO_find_type(t, BIO_TYPE_SSL);
  437. f = BIO_find_type(f, BIO_TYPE_SSL);
  438. if ((t == NULL) || (f == NULL))
  439. return 0;
  440. tdata = BIO_get_data(t);
  441. fdata = BIO_get_data(f);
  442. if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
  443. return 0;
  444. if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
  445. return 0;
  446. return 1;
  447. }
  448. void BIO_ssl_shutdown(BIO *b)
  449. {
  450. BIO_SSL *bdata;
  451. for (; b != NULL; b = BIO_next(b)) {
  452. if (BIO_method_type(b) != BIO_TYPE_SSL)
  453. continue;
  454. bdata = BIO_get_data(b);
  455. if (bdata != NULL && bdata->ssl != NULL)
  456. SSL_shutdown(bdata->ssl);
  457. }
  458. }