bio_ssl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 (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. bs = BIO_get_data(b);
  257. }
  258. BIO_set_shutdown(b, num);
  259. ssl = (SSL *)ptr;
  260. bs->ssl = ssl;
  261. bio = SSL_get_rbio(ssl);
  262. if (bio != NULL) {
  263. if (next != NULL)
  264. BIO_push(bio, next);
  265. BIO_set_next(b, bio);
  266. BIO_up_ref(bio);
  267. }
  268. BIO_set_init(b, 1);
  269. break;
  270. case BIO_C_GET_SSL:
  271. if (ptr != NULL) {
  272. sslp = (SSL **)ptr;
  273. *sslp = ssl;
  274. } else
  275. ret = 0;
  276. break;
  277. case BIO_CTRL_GET_CLOSE:
  278. ret = BIO_get_shutdown(b);
  279. break;
  280. case BIO_CTRL_SET_CLOSE:
  281. BIO_set_shutdown(b, (int)num);
  282. break;
  283. case BIO_CTRL_WPENDING:
  284. ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
  285. break;
  286. case BIO_CTRL_PENDING:
  287. ret = SSL_pending(ssl);
  288. if (ret == 0)
  289. ret = BIO_pending(ssl->rbio);
  290. break;
  291. case BIO_CTRL_FLUSH:
  292. BIO_clear_retry_flags(b);
  293. ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
  294. BIO_copy_next_retry(b);
  295. break;
  296. case BIO_CTRL_PUSH:
  297. if ((next != NULL) && (next != ssl->rbio)) {
  298. /*
  299. * We are going to pass ownership of next to the SSL object...but
  300. * we don't own a reference to pass yet - so up ref
  301. */
  302. BIO_up_ref(next);
  303. SSL_set_bio(ssl, next, next);
  304. }
  305. break;
  306. case BIO_CTRL_POP:
  307. /* Only detach if we are the BIO explicitly being popped */
  308. if (b == ptr) {
  309. /* This will clear the reference we obtained during push */
  310. SSL_set_bio(ssl, NULL, NULL);
  311. }
  312. break;
  313. case BIO_C_DO_STATE_MACHINE:
  314. BIO_clear_retry_flags(b);
  315. BIO_set_retry_reason(b, 0);
  316. ret = (int)SSL_do_handshake(ssl);
  317. switch (SSL_get_error(ssl, (int)ret)) {
  318. case SSL_ERROR_WANT_READ:
  319. BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
  320. break;
  321. case SSL_ERROR_WANT_WRITE:
  322. BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
  323. break;
  324. case SSL_ERROR_WANT_CONNECT:
  325. BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
  326. BIO_set_retry_reason(b, BIO_get_retry_reason(next));
  327. break;
  328. case SSL_ERROR_WANT_X509_LOOKUP:
  329. BIO_set_retry_special(b);
  330. BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
  331. break;
  332. default:
  333. break;
  334. }
  335. break;
  336. case BIO_CTRL_DUP:
  337. dbio = (BIO *)ptr;
  338. dbs = BIO_get_data(dbio);
  339. SSL_free(dbs->ssl);
  340. dbs->ssl = SSL_dup(ssl);
  341. dbs->num_renegotiates = bs->num_renegotiates;
  342. dbs->renegotiate_count = bs->renegotiate_count;
  343. dbs->byte_count = bs->byte_count;
  344. dbs->renegotiate_timeout = bs->renegotiate_timeout;
  345. dbs->last_time = bs->last_time;
  346. ret = (dbs->ssl != NULL);
  347. break;
  348. case BIO_C_GET_FD:
  349. ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
  350. break;
  351. case BIO_CTRL_SET_CALLBACK:
  352. ret = 0; /* use callback ctrl */
  353. break;
  354. default:
  355. ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
  356. break;
  357. }
  358. return ret;
  359. }
  360. static long ssl_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  361. {
  362. SSL *ssl;
  363. BIO_SSL *bs;
  364. long ret = 1;
  365. bs = BIO_get_data(b);
  366. ssl = bs->ssl;
  367. switch (cmd) {
  368. case BIO_CTRL_SET_CALLBACK:
  369. ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
  370. break;
  371. default:
  372. ret = 0;
  373. break;
  374. }
  375. return ret;
  376. }
  377. static int ssl_puts(BIO *bp, const char *str)
  378. {
  379. int n, ret;
  380. n = strlen(str);
  381. ret = BIO_write(bp, str, n);
  382. return ret;
  383. }
  384. BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
  385. {
  386. #ifndef OPENSSL_NO_SOCK
  387. BIO *ret = NULL, *buf = NULL, *ssl = NULL;
  388. if ((buf = BIO_new(BIO_f_buffer())) == NULL)
  389. return NULL;
  390. if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
  391. goto err;
  392. if ((ret = BIO_push(buf, ssl)) == NULL)
  393. goto err;
  394. return ret;
  395. err:
  396. BIO_free(buf);
  397. BIO_free(ssl);
  398. #endif
  399. return NULL;
  400. }
  401. BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
  402. {
  403. #ifndef OPENSSL_NO_SOCK
  404. BIO *ret = NULL, *con = NULL, *ssl = NULL;
  405. if ((con = BIO_new(BIO_s_connect())) == NULL)
  406. return NULL;
  407. if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
  408. goto err;
  409. if ((ret = BIO_push(ssl, con)) == NULL)
  410. goto err;
  411. return ret;
  412. err:
  413. BIO_free(ssl);
  414. BIO_free(con);
  415. #endif
  416. return NULL;
  417. }
  418. BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
  419. {
  420. BIO *ret;
  421. SSL *ssl;
  422. if ((ret = BIO_new(BIO_f_ssl())) == NULL)
  423. return NULL;
  424. if ((ssl = SSL_new(ctx)) == NULL) {
  425. BIO_free(ret);
  426. return NULL;
  427. }
  428. if (client)
  429. SSL_set_connect_state(ssl);
  430. else
  431. SSL_set_accept_state(ssl);
  432. BIO_set_ssl(ret, ssl, BIO_CLOSE);
  433. return ret;
  434. }
  435. int BIO_ssl_copy_session_id(BIO *t, BIO *f)
  436. {
  437. BIO_SSL *tdata, *fdata;
  438. t = BIO_find_type(t, BIO_TYPE_SSL);
  439. f = BIO_find_type(f, BIO_TYPE_SSL);
  440. if ((t == NULL) || (f == NULL))
  441. return 0;
  442. tdata = BIO_get_data(t);
  443. fdata = BIO_get_data(f);
  444. if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
  445. return 0;
  446. if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
  447. return 0;
  448. return 1;
  449. }
  450. void BIO_ssl_shutdown(BIO *b)
  451. {
  452. BIO_SSL *bdata;
  453. for (; b != NULL; b = BIO_next(b)) {
  454. if (BIO_method_type(b) != BIO_TYPE_SSL)
  455. continue;
  456. bdata = BIO_get_data(b);
  457. if (bdata != NULL && bdata->ssl != NULL)
  458. SSL_shutdown(bdata->ssl);
  459. }
  460. }