bio_ssl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright 1995-2021 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_local.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. ERR_raise(ERR_LIB_BIO, 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 (BIO_get_shutdown(a)) {
  71. if (bs->ssl != NULL)
  72. SSL_shutdown(bs->ssl);
  73. if (BIO_get_init(a))
  74. SSL_free(bs->ssl);
  75. BIO_clear_flags(a, ~0); /* Clear all flags */
  76. BIO_set_init(a, 0);
  77. }
  78. OPENSSL_free(bs);
  79. return 1;
  80. }
  81. static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes)
  82. {
  83. int ret = 1;
  84. BIO_SSL *sb;
  85. SSL *ssl;
  86. int retry_reason = 0;
  87. int r = 0;
  88. if (buf == NULL)
  89. return 0;
  90. sb = BIO_get_data(b);
  91. ssl = sb->ssl;
  92. BIO_clear_retry_flags(b);
  93. ret = ssl_read_internal(ssl, buf, size, readbytes);
  94. switch (SSL_get_error(ssl, ret)) {
  95. case SSL_ERROR_NONE:
  96. if (sb->renegotiate_count > 0) {
  97. sb->byte_count += *readbytes;
  98. if (sb->byte_count > sb->renegotiate_count) {
  99. sb->byte_count = 0;
  100. sb->num_renegotiates++;
  101. SSL_renegotiate(ssl);
  102. r = 1;
  103. }
  104. }
  105. if ((sb->renegotiate_timeout > 0) && (!r)) {
  106. unsigned long tm;
  107. tm = (unsigned long)time(NULL);
  108. if (tm > sb->last_time + sb->renegotiate_timeout) {
  109. sb->last_time = tm;
  110. sb->num_renegotiates++;
  111. SSL_renegotiate(ssl);
  112. }
  113. }
  114. break;
  115. case SSL_ERROR_WANT_READ:
  116. BIO_set_retry_read(b);
  117. break;
  118. case SSL_ERROR_WANT_WRITE:
  119. BIO_set_retry_write(b);
  120. break;
  121. case SSL_ERROR_WANT_X509_LOOKUP:
  122. BIO_set_retry_special(b);
  123. retry_reason = BIO_RR_SSL_X509_LOOKUP;
  124. break;
  125. case SSL_ERROR_WANT_ACCEPT:
  126. BIO_set_retry_special(b);
  127. retry_reason = BIO_RR_ACCEPT;
  128. break;
  129. case SSL_ERROR_WANT_CONNECT:
  130. BIO_set_retry_special(b);
  131. retry_reason = BIO_RR_CONNECT;
  132. break;
  133. case SSL_ERROR_SYSCALL:
  134. case SSL_ERROR_SSL:
  135. case SSL_ERROR_ZERO_RETURN:
  136. default:
  137. break;
  138. }
  139. BIO_set_retry_reason(b, retry_reason);
  140. return ret;
  141. }
  142. static int ssl_write(BIO *b, const char *buf, size_t size, size_t *written)
  143. {
  144. int ret, r = 0;
  145. int retry_reason = 0;
  146. SSL *ssl;
  147. BIO_SSL *bs;
  148. if (buf == NULL)
  149. return 0;
  150. bs = BIO_get_data(b);
  151. ssl = bs->ssl;
  152. BIO_clear_retry_flags(b);
  153. ret = ssl_write_internal(ssl, buf, size, written);
  154. switch (SSL_get_error(ssl, ret)) {
  155. case SSL_ERROR_NONE:
  156. if (bs->renegotiate_count > 0) {
  157. bs->byte_count += *written;
  158. if (bs->byte_count > bs->renegotiate_count) {
  159. bs->byte_count = 0;
  160. bs->num_renegotiates++;
  161. SSL_renegotiate(ssl);
  162. r = 1;
  163. }
  164. }
  165. if ((bs->renegotiate_timeout > 0) && (!r)) {
  166. unsigned long tm;
  167. tm = (unsigned long)time(NULL);
  168. if (tm > bs->last_time + bs->renegotiate_timeout) {
  169. bs->last_time = tm;
  170. bs->num_renegotiates++;
  171. SSL_renegotiate(ssl);
  172. }
  173. }
  174. break;
  175. case SSL_ERROR_WANT_WRITE:
  176. BIO_set_retry_write(b);
  177. break;
  178. case SSL_ERROR_WANT_READ:
  179. BIO_set_retry_read(b);
  180. break;
  181. case SSL_ERROR_WANT_X509_LOOKUP:
  182. BIO_set_retry_special(b);
  183. retry_reason = BIO_RR_SSL_X509_LOOKUP;
  184. break;
  185. case SSL_ERROR_WANT_CONNECT:
  186. BIO_set_retry_special(b);
  187. retry_reason = BIO_RR_CONNECT;
  188. case SSL_ERROR_SYSCALL:
  189. case SSL_ERROR_SSL:
  190. default:
  191. break;
  192. }
  193. BIO_set_retry_reason(b, retry_reason);
  194. return ret;
  195. }
  196. static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
  197. {
  198. SSL **sslp, *ssl;
  199. BIO_SSL *bs, *dbs;
  200. BIO *dbio, *bio;
  201. long ret = 1;
  202. BIO *next;
  203. SSL_CONNECTION *sc = NULL;
  204. bs = BIO_get_data(b);
  205. next = BIO_next(b);
  206. ssl = bs->ssl;
  207. if ((ssl == NULL
  208. || (sc = SSL_CONNECTION_FROM_SSL(ssl)) == NULL)
  209. && cmd != BIO_C_SET_SSL)
  210. return 0;
  211. /* TODO(QUIC): The rbio/wbio might be from QUIC_CONNECTION instead */
  212. switch (cmd) {
  213. case BIO_CTRL_RESET:
  214. SSL_shutdown(ssl);
  215. if (sc->handshake_func == ssl->method->ssl_connect)
  216. SSL_set_connect_state(ssl);
  217. else if (sc->handshake_func == ssl->method->ssl_accept)
  218. SSL_set_accept_state(ssl);
  219. if (!SSL_clear(ssl)) {
  220. ret = 0;
  221. break;
  222. }
  223. if (next != NULL)
  224. ret = BIO_ctrl(next, cmd, num, ptr);
  225. else if (sc->rbio != NULL)
  226. ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
  227. else
  228. ret = 1;
  229. break;
  230. case BIO_CTRL_INFO:
  231. ret = 0;
  232. break;
  233. case BIO_C_SSL_MODE:
  234. if (num) /* client mode */
  235. SSL_set_connect_state(ssl);
  236. else
  237. SSL_set_accept_state(ssl);
  238. break;
  239. case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
  240. ret = bs->renegotiate_timeout;
  241. if (num < 60)
  242. num = 5;
  243. bs->renegotiate_timeout = (unsigned long)num;
  244. bs->last_time = (unsigned long)time(NULL);
  245. break;
  246. case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
  247. ret = bs->renegotiate_count;
  248. if ((long)num >= 512)
  249. bs->renegotiate_count = (unsigned long)num;
  250. break;
  251. case BIO_C_GET_SSL_NUM_RENEGOTIATES:
  252. ret = bs->num_renegotiates;
  253. break;
  254. case BIO_C_SET_SSL:
  255. if (ssl != NULL) {
  256. ssl_free(b);
  257. if (!ssl_new(b))
  258. return 0;
  259. bs = BIO_get_data(b);
  260. }
  261. BIO_set_shutdown(b, num);
  262. ssl = (SSL *)ptr;
  263. bs->ssl = ssl;
  264. bio = SSL_get_rbio(ssl);
  265. if (bio != NULL) {
  266. if (next != NULL)
  267. BIO_push(bio, next);
  268. BIO_set_next(b, bio);
  269. BIO_up_ref(bio);
  270. }
  271. BIO_set_init(b, 1);
  272. break;
  273. case BIO_C_GET_SSL:
  274. if (ptr != NULL) {
  275. sslp = (SSL **)ptr;
  276. *sslp = ssl;
  277. } else
  278. ret = 0;
  279. break;
  280. case BIO_CTRL_GET_CLOSE:
  281. ret = BIO_get_shutdown(b);
  282. break;
  283. case BIO_CTRL_SET_CLOSE:
  284. BIO_set_shutdown(b, (int)num);
  285. break;
  286. case BIO_CTRL_WPENDING:
  287. ret = BIO_ctrl(sc->wbio, cmd, num, ptr);
  288. break;
  289. case BIO_CTRL_PENDING:
  290. ret = SSL_pending(ssl);
  291. if (ret == 0)
  292. ret = BIO_pending(sc->rbio);
  293. break;
  294. case BIO_CTRL_FLUSH:
  295. BIO_clear_retry_flags(b);
  296. ret = BIO_ctrl(sc->wbio, cmd, num, ptr);
  297. BIO_copy_next_retry(b);
  298. break;
  299. case BIO_CTRL_PUSH:
  300. if ((next != NULL) && (next != sc->rbio)) {
  301. /*
  302. * We are going to pass ownership of next to the SSL object...but
  303. * we don't own a reference to pass yet - so up ref
  304. */
  305. BIO_up_ref(next);
  306. SSL_set_bio(ssl, next, next);
  307. }
  308. break;
  309. case BIO_CTRL_POP:
  310. /* Only detach if we are the BIO explicitly being popped */
  311. if (b == ptr) {
  312. /* This will clear the reference we obtained during push */
  313. SSL_set_bio(ssl, NULL, NULL);
  314. }
  315. break;
  316. case BIO_C_DO_STATE_MACHINE:
  317. BIO_clear_retry_flags(b);
  318. BIO_set_retry_reason(b, 0);
  319. ret = (int)SSL_do_handshake(ssl);
  320. switch (SSL_get_error(ssl, (int)ret)) {
  321. case SSL_ERROR_WANT_READ:
  322. BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
  323. break;
  324. case SSL_ERROR_WANT_WRITE:
  325. BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
  326. break;
  327. case SSL_ERROR_WANT_CONNECT:
  328. BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
  329. BIO_set_retry_reason(b, BIO_get_retry_reason(next));
  330. break;
  331. case SSL_ERROR_WANT_X509_LOOKUP:
  332. BIO_set_retry_special(b);
  333. BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
  334. break;
  335. default:
  336. break;
  337. }
  338. break;
  339. case BIO_CTRL_DUP:
  340. dbio = (BIO *)ptr;
  341. dbs = BIO_get_data(dbio);
  342. SSL_free(dbs->ssl);
  343. dbs->ssl = SSL_dup(ssl);
  344. dbs->num_renegotiates = bs->num_renegotiates;
  345. dbs->renegotiate_count = bs->renegotiate_count;
  346. dbs->byte_count = bs->byte_count;
  347. dbs->renegotiate_timeout = bs->renegotiate_timeout;
  348. dbs->last_time = bs->last_time;
  349. ret = (dbs->ssl != NULL);
  350. break;
  351. case BIO_C_GET_FD:
  352. ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
  353. break;
  354. case BIO_CTRL_SET_CALLBACK:
  355. ret = 0; /* use callback ctrl */
  356. break;
  357. default:
  358. ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
  359. break;
  360. }
  361. return ret;
  362. }
  363. static long ssl_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  364. {
  365. SSL *ssl;
  366. BIO_SSL *bs;
  367. long ret = 1;
  368. bs = BIO_get_data(b);
  369. ssl = bs->ssl;
  370. switch (cmd) {
  371. case BIO_CTRL_SET_CALLBACK:
  372. ret = BIO_callback_ctrl(SSL_get_rbio(ssl), cmd, fp);
  373. break;
  374. default:
  375. ret = 0;
  376. break;
  377. }
  378. return ret;
  379. }
  380. static int ssl_puts(BIO *bp, const char *str)
  381. {
  382. int n, ret;
  383. n = strlen(str);
  384. ret = BIO_write(bp, str, n);
  385. return ret;
  386. }
  387. BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
  388. {
  389. #ifndef OPENSSL_NO_SOCK
  390. BIO *ret = NULL, *buf = NULL, *ssl = NULL;
  391. if ((buf = BIO_new(BIO_f_buffer())) == NULL)
  392. return NULL;
  393. if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
  394. goto err;
  395. if ((ret = BIO_push(buf, ssl)) == NULL)
  396. goto err;
  397. return ret;
  398. err:
  399. BIO_free(buf);
  400. BIO_free(ssl);
  401. #endif
  402. return NULL;
  403. }
  404. BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
  405. {
  406. #ifndef OPENSSL_NO_SOCK
  407. BIO *ret = NULL, *con = NULL, *ssl = NULL;
  408. if ((con = BIO_new(BIO_s_connect())) == NULL)
  409. return NULL;
  410. if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
  411. goto err;
  412. if ((ret = BIO_push(ssl, con)) == NULL)
  413. goto err;
  414. return ret;
  415. err:
  416. BIO_free(ssl);
  417. BIO_free(con);
  418. #endif
  419. return NULL;
  420. }
  421. BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
  422. {
  423. BIO *ret;
  424. SSL *ssl;
  425. if ((ret = BIO_new(BIO_f_ssl())) == NULL)
  426. return NULL;
  427. if ((ssl = SSL_new(ctx)) == NULL) {
  428. BIO_free(ret);
  429. return NULL;
  430. }
  431. if (client)
  432. SSL_set_connect_state(ssl);
  433. else
  434. SSL_set_accept_state(ssl);
  435. BIO_set_ssl(ret, ssl, BIO_CLOSE);
  436. return ret;
  437. }
  438. int BIO_ssl_copy_session_id(BIO *t, BIO *f)
  439. {
  440. BIO_SSL *tdata, *fdata;
  441. t = BIO_find_type(t, BIO_TYPE_SSL);
  442. f = BIO_find_type(f, BIO_TYPE_SSL);
  443. if ((t == NULL) || (f == NULL))
  444. return 0;
  445. tdata = BIO_get_data(t);
  446. fdata = BIO_get_data(f);
  447. if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
  448. return 0;
  449. if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
  450. return 0;
  451. return 1;
  452. }
  453. void BIO_ssl_shutdown(BIO *b)
  454. {
  455. BIO_SSL *bdata;
  456. for (; b != NULL; b = BIO_next(b)) {
  457. if (BIO_method_type(b) != BIO_TYPE_SSL)
  458. continue;
  459. bdata = BIO_get_data(b);
  460. if (bdata != NULL && bdata->ssl != NULL)
  461. SSL_shutdown(bdata->ssl);
  462. }
  463. }