bio_ssl.c 14 KB

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