bio_ssl.c 13 KB

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