bio_ssl.c 14 KB

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