bio_ssl.c 13 KB

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