rec_layer_s3.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * Copyright 1995-2022 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 <limits.h>
  11. #include <errno.h>
  12. #include "../ssl_local.h"
  13. #include <openssl/evp.h>
  14. #include <openssl/buffer.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/core_names.h>
  17. #include "record_local.h"
  18. #include "internal/packet.h"
  19. void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s)
  20. {
  21. rl->s = s;
  22. }
  23. void RECORD_LAYER_clear(RECORD_LAYER *rl)
  24. {
  25. rl->wnum = 0;
  26. memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment));
  27. rl->handshake_fragment_len = 0;
  28. rl->wpend_tot = 0;
  29. rl->wpend_type = 0;
  30. rl->wpend_ret = 0;
  31. rl->wpend_buf = NULL;
  32. if (rl->rrlmethod != NULL)
  33. rl->rrlmethod->free(rl->rrl); /* Ignore return value */
  34. if (rl->wrlmethod != NULL)
  35. rl->wrlmethod->free(rl->wrl); /* Ignore return value */
  36. BIO_free(rl->rrlnext);
  37. rl->rrlmethod = NULL;
  38. rl->wrlmethod = NULL;
  39. rl->rrlnext = NULL;
  40. rl->rrl = NULL;
  41. rl->wrl = NULL;
  42. if (rl->d)
  43. DTLS_RECORD_LAYER_clear(rl);
  44. }
  45. /* Checks if we have unprocessed read ahead data pending */
  46. int RECORD_LAYER_read_pending(const RECORD_LAYER *rl)
  47. {
  48. return rl->rrlmethod->unprocessed_read_pending(rl->rrl);
  49. }
  50. /* Checks if we have decrypted unread record data pending */
  51. int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl)
  52. {
  53. return (rl->curr_rec < rl->num_recs)
  54. || rl->rrlmethod->processed_read_pending(rl->rrl);
  55. }
  56. int RECORD_LAYER_write_pending(const RECORD_LAYER *rl)
  57. {
  58. return rl->wpend_tot > 0;
  59. }
  60. static uint32_t ossl_get_max_early_data(SSL_CONNECTION *s)
  61. {
  62. uint32_t max_early_data;
  63. SSL_SESSION *sess = s->session;
  64. /*
  65. * If we are a client then we always use the max_early_data from the
  66. * session/psksession. Otherwise we go with the lowest out of the max early
  67. * data set in the session and the configured max_early_data.
  68. */
  69. if (!s->server && sess->ext.max_early_data == 0) {
  70. if (!ossl_assert(s->psksession != NULL
  71. && s->psksession->ext.max_early_data > 0)) {
  72. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  73. return 0;
  74. }
  75. sess = s->psksession;
  76. }
  77. if (!s->server)
  78. max_early_data = sess->ext.max_early_data;
  79. else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
  80. max_early_data = s->recv_max_early_data;
  81. else
  82. max_early_data = s->recv_max_early_data < sess->ext.max_early_data
  83. ? s->recv_max_early_data : sess->ext.max_early_data;
  84. return max_early_data;
  85. }
  86. static int ossl_early_data_count_ok(SSL_CONNECTION *s, size_t length,
  87. size_t overhead, int send)
  88. {
  89. uint32_t max_early_data;
  90. max_early_data = ossl_get_max_early_data(s);
  91. if (max_early_data == 0) {
  92. SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
  93. SSL_R_TOO_MUCH_EARLY_DATA);
  94. return 0;
  95. }
  96. /* If we are dealing with ciphertext we need to allow for the overhead */
  97. max_early_data += overhead;
  98. if (s->early_data_count + length > max_early_data) {
  99. SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
  100. SSL_R_TOO_MUCH_EARLY_DATA);
  101. return 0;
  102. }
  103. s->early_data_count += length;
  104. return 1;
  105. }
  106. size_t ssl3_pending(const SSL *s)
  107. {
  108. size_t i, num = 0;
  109. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  110. if (sc == NULL)
  111. return 0;
  112. if (SSL_CONNECTION_IS_DTLS(sc)) {
  113. TLS_RECORD *rdata;
  114. pitem *item, *iter;
  115. iter = pqueue_iterator(sc->rlayer.d->buffered_app_data.q);
  116. while ((item = pqueue_next(&iter)) != NULL) {
  117. rdata = item->data;
  118. num += rdata->length;
  119. }
  120. }
  121. for (i = 0; i < sc->rlayer.num_recs; i++) {
  122. if (sc->rlayer.tlsrecs[i].type != SSL3_RT_APPLICATION_DATA)
  123. return num;
  124. num += sc->rlayer.tlsrecs[i].length;
  125. }
  126. num += sc->rlayer.rrlmethod->app_data_pending(sc->rlayer.rrl);
  127. return num;
  128. }
  129. void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len)
  130. {
  131. ctx->default_read_buf_len = len;
  132. }
  133. void SSL_set_default_read_buffer_len(SSL *s, size_t len)
  134. {
  135. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  136. if (sc == NULL)
  137. return;
  138. sc->rlayer.default_read_buf_len = len;
  139. }
  140. const char *SSL_rstate_string_long(const SSL *s)
  141. {
  142. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  143. const char *lng;
  144. if (sc == NULL)
  145. return NULL;
  146. if (sc->rlayer.rrlmethod == NULL || sc->rlayer.rrl == NULL)
  147. return "unknown";
  148. sc->rlayer.rrlmethod->get_state(sc->rlayer.rrl, NULL, &lng);
  149. return lng;
  150. }
  151. const char *SSL_rstate_string(const SSL *s)
  152. {
  153. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  154. const char *shrt;
  155. if (sc == NULL)
  156. return NULL;
  157. if (sc->rlayer.rrlmethod == NULL || sc->rlayer.rrl == NULL)
  158. return "unknown";
  159. sc->rlayer.rrlmethod->get_state(sc->rlayer.rrl, &shrt, NULL);
  160. return shrt;
  161. }
  162. static int tls_write_check_pending(SSL_CONNECTION *s, int type,
  163. const unsigned char *buf, size_t len)
  164. {
  165. if (s->rlayer.wpend_tot == 0)
  166. return 0;
  167. /* We have pending data, so do some sanity checks */
  168. if ((s->rlayer.wpend_tot > len)
  169. || (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)
  170. && (s->rlayer.wpend_buf != buf))
  171. || (s->rlayer.wpend_type != type)) {
  172. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_WRITE_RETRY);
  173. return -1;
  174. }
  175. return 1;
  176. }
  177. /*
  178. * Call this to write data in records of type 'type' It will return <= 0 if
  179. * not all data has been sent or non-blocking IO.
  180. */
  181. int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, size_t len,
  182. size_t *written)
  183. {
  184. const unsigned char *buf = buf_;
  185. size_t tot;
  186. size_t n, max_send_fragment, split_send_fragment, maxpipes;
  187. int i;
  188. SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
  189. OSSL_RECORD_TEMPLATE tmpls[SSL_MAX_PIPELINES];
  190. unsigned int recversion;
  191. if (s == NULL)
  192. return -1;
  193. s->rwstate = SSL_NOTHING;
  194. tot = s->rlayer.wnum;
  195. /*
  196. * ensure that if we end up with a smaller value of data to write out
  197. * than the original len from a write which didn't complete for
  198. * non-blocking I/O and also somehow ended up avoiding the check for
  199. * this in tls_write_check_pending/SSL_R_BAD_WRITE_RETRY as it must never be
  200. * possible to end up with (len-tot) as a large number that will then
  201. * promptly send beyond the end of the users buffer ... so we trap and
  202. * report the error in a way the user will notice
  203. */
  204. if ((len < s->rlayer.wnum)
  205. || ((s->rlayer.wpend_tot != 0)
  206. && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {
  207. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
  208. return -1;
  209. }
  210. if (s->early_data_state == SSL_EARLY_DATA_WRITING
  211. && !ossl_early_data_count_ok(s, len, 0, 1)) {
  212. /* SSLfatal() already called */
  213. return -1;
  214. }
  215. s->rlayer.wnum = 0;
  216. /*
  217. * If we are supposed to be sending a KeyUpdate or NewSessionTicket then go
  218. * into init unless we have writes pending - in which case we should finish
  219. * doing that first.
  220. */
  221. if (s->rlayer.wpend_tot == 0 && (s->key_update != SSL_KEY_UPDATE_NONE
  222. || s->ext.extra_tickets_expected > 0))
  223. ossl_statem_set_in_init(s, 1);
  224. /*
  225. * When writing early data on the server side we could be "in_init" in
  226. * between receiving the EoED and the CF - but we don't want to handle those
  227. * messages yet.
  228. */
  229. if (SSL_in_init(ssl) && !ossl_statem_get_in_handshake(s)
  230. && s->early_data_state != SSL_EARLY_DATA_UNAUTH_WRITING) {
  231. i = s->handshake_func(ssl);
  232. /* SSLfatal() already called */
  233. if (i < 0)
  234. return i;
  235. if (i == 0) {
  236. return -1;
  237. }
  238. }
  239. i = tls_write_check_pending(s, type, buf, len);
  240. if (i < 0) {
  241. /* SSLfatal() already called */
  242. return i;
  243. } else if (i > 0) {
  244. /* Retry needed */
  245. i = HANDLE_RLAYER_WRITE_RETURN(s,
  246. s->rlayer.wrlmethod->retry_write_records(s->rlayer.wrl));
  247. if (i <= 0)
  248. return i;
  249. tot += s->rlayer.wpend_tot;
  250. s->rlayer.wpend_tot = 0;
  251. } /* else no retry required */
  252. if (tot == 0) {
  253. /*
  254. * We've not previously sent any data for this write so memorize
  255. * arguments so that we can detect bad write retries later
  256. */
  257. s->rlayer.wpend_tot = 0;
  258. s->rlayer.wpend_type = type;
  259. s->rlayer.wpend_buf = buf;
  260. s->rlayer.wpend_ret = len;
  261. }
  262. if (tot == len) { /* done? */
  263. *written = tot;
  264. return 1;
  265. }
  266. /* If we have an alert to send, lets send it */
  267. if (s->s3.alert_dispatch > 0) {
  268. i = ssl->method->ssl_dispatch_alert(ssl);
  269. if (i <= 0) {
  270. /* SSLfatal() already called if appropriate */
  271. return i;
  272. }
  273. /* if it went, fall through and send more stuff */
  274. }
  275. n = (len - tot);
  276. max_send_fragment = ssl_get_max_send_fragment(s);
  277. split_send_fragment = ssl_get_split_send_fragment(s);
  278. if (max_send_fragment == 0
  279. || split_send_fragment == 0
  280. || split_send_fragment > max_send_fragment) {
  281. /*
  282. * We should have prevented this when we set/get the split and max send
  283. * fragments so we shouldn't get here
  284. */
  285. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  286. return -1;
  287. }
  288. /*
  289. * Some servers hang if initial client hello is larger than 256 bytes
  290. * and record version number > TLS 1.0
  291. */
  292. recversion = (s->version == TLS1_3_VERSION) ? TLS1_2_VERSION : s->version;
  293. if (SSL_get_state(ssl) == TLS_ST_CW_CLNT_HELLO
  294. && !s->renegotiate
  295. && TLS1_get_version(ssl) > TLS1_VERSION
  296. && s->hello_retry_request == SSL_HRR_NONE)
  297. recversion = TLS1_VERSION;
  298. for (;;) {
  299. size_t tmppipelen, remain;
  300. size_t j, lensofar = 0;
  301. /*
  302. * Ask the record layer how it would like to split the amount of data
  303. * that we have, and how many of those records it would like in one go.
  304. */
  305. maxpipes = s->rlayer.wrlmethod->get_max_records(s->rlayer.wrl, type, n,
  306. max_send_fragment,
  307. &split_send_fragment);
  308. /*
  309. * If max_pipelines is 0 then this means "undefined" and we default to
  310. * whatever the record layer wants to do. Otherwise we use the smallest
  311. * value from the number requested by the record layer, and max number
  312. * configured by the user.
  313. */
  314. if (s->max_pipelines > 0 && maxpipes > s->max_pipelines)
  315. maxpipes = s->max_pipelines;
  316. if (maxpipes > SSL_MAX_PIPELINES)
  317. maxpipes = SSL_MAX_PIPELINES;
  318. if (split_send_fragment > max_send_fragment) {
  319. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  320. return -1;
  321. }
  322. if (n / maxpipes >= split_send_fragment) {
  323. /*
  324. * We have enough data to completely fill all available
  325. * pipelines
  326. */
  327. for (j = 0; j < maxpipes; j++) {
  328. tmpls[j].type = type;
  329. tmpls[j].version = recversion;
  330. tmpls[j].buf = &(buf[tot]) + (j * split_send_fragment);
  331. tmpls[j].buflen = split_send_fragment;
  332. }
  333. /* Remember how much data we are going to be sending */
  334. s->rlayer.wpend_tot = maxpipes * split_send_fragment;
  335. } else {
  336. /* We can partially fill all available pipelines */
  337. tmppipelen = n / maxpipes;
  338. remain = n % maxpipes;
  339. /*
  340. * If there is a remainder we add an extra byte to the first few
  341. * pipelines
  342. */
  343. if (remain > 0)
  344. tmppipelen++;
  345. for (j = 0; j < maxpipes; j++) {
  346. tmpls[j].type = type;
  347. tmpls[j].version = recversion;
  348. tmpls[j].buf = &(buf[tot]) + lensofar;
  349. tmpls[j].buflen = tmppipelen;
  350. lensofar += tmppipelen;
  351. if (j + 1 == remain)
  352. tmppipelen--;
  353. }
  354. /* Remember how much data we are going to be sending */
  355. s->rlayer.wpend_tot = n;
  356. }
  357. i = HANDLE_RLAYER_WRITE_RETURN(s,
  358. s->rlayer.wrlmethod->write_records(s->rlayer.wrl, tmpls, maxpipes));
  359. if (i <= 0) {
  360. /* SSLfatal() already called if appropriate */
  361. s->rlayer.wnum = tot;
  362. return i;
  363. }
  364. if (s->rlayer.wpend_tot == n
  365. || (type == SSL3_RT_APPLICATION_DATA
  366. && (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0)) {
  367. *written = tot + s->rlayer.wpend_tot;
  368. s->rlayer.wpend_tot = 0;
  369. return 1;
  370. }
  371. n -= s->rlayer.wpend_tot;
  372. tot += s->rlayer.wpend_tot;
  373. }
  374. }
  375. int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret,
  376. char *file, int line)
  377. {
  378. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  379. if (ret == OSSL_RECORD_RETURN_RETRY) {
  380. s->rwstate = writing ? SSL_WRITING : SSL_READING;
  381. ret = -1;
  382. } else {
  383. s->rwstate = SSL_NOTHING;
  384. if (ret == OSSL_RECORD_RETURN_EOF) {
  385. if (writing) {
  386. /*
  387. * This shouldn't happen with a writing operation. We treat it
  388. * as fatal.
  389. */
  390. ERR_new();
  391. ERR_set_debug(file, line, 0);
  392. ossl_statem_fatal(s, SSL_AD_INTERNAL_ERROR,
  393. ERR_R_INTERNAL_ERROR, NULL);
  394. ret = OSSL_RECORD_RETURN_FATAL;
  395. } else if ((s->options & SSL_OP_IGNORE_UNEXPECTED_EOF) != 0) {
  396. SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN);
  397. s->s3.warn_alert = SSL_AD_CLOSE_NOTIFY;
  398. } else {
  399. ERR_new();
  400. ERR_set_debug(file, line, 0);
  401. ossl_statem_fatal(s, SSL_AD_DECODE_ERROR,
  402. SSL_R_UNEXPECTED_EOF_WHILE_READING, NULL);
  403. }
  404. } else if (ret == OSSL_RECORD_RETURN_FATAL) {
  405. int al = s->rlayer.rrlmethod->get_alert_code(s->rlayer.rrl);
  406. if (al != SSL_AD_NO_ALERT) {
  407. ERR_new();
  408. ERR_set_debug(file, line, 0);
  409. ossl_statem_fatal(s, al, SSL_R_RECORD_LAYER_FAILURE, NULL);
  410. }
  411. /*
  412. * else some failure but there is no alert code. We don't log an
  413. * error for this. The record layer should have logged an error
  414. * already or, if not, its due to some sys call error which will be
  415. * reported via SSL_ERROR_SYSCALL and errno.
  416. */
  417. }
  418. /*
  419. * The record layer distinguishes the cases of EOF, non-fatal
  420. * err and retry. Upper layers do not.
  421. * If we got a retry or success then *ret is already correct,
  422. * otherwise we need to convert the return value.
  423. */
  424. if (ret == OSSL_RECORD_RETURN_NON_FATAL_ERR || ret == OSSL_RECORD_RETURN_EOF)
  425. ret = 0;
  426. else if (ret < OSSL_RECORD_RETURN_NON_FATAL_ERR)
  427. ret = -1;
  428. }
  429. return ret;
  430. }
  431. void ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr)
  432. {
  433. if (rr->rechandle != NULL) {
  434. /* The record layer allocated the buffers for this record */
  435. s->rlayer.rrlmethod->release_record(s->rlayer.rrl, rr->rechandle);
  436. } else {
  437. /* We allocated the buffers for this record (only happens with DTLS) */
  438. OPENSSL_free(rr->data);
  439. }
  440. s->rlayer.curr_rec++;
  441. }
  442. /*-
  443. * Return up to 'len' payload bytes received in 'type' records.
  444. * 'type' is one of the following:
  445. *
  446. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  447. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  448. * - 0 (during a shutdown, no data has to be returned)
  449. *
  450. * If we don't have stored data to work from, read a SSL/TLS record first
  451. * (possibly multiple records if we still don't have anything to return).
  452. *
  453. * This function must handle any surprises the peer may have for us, such as
  454. * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
  455. * messages are treated as if they were handshake messages *if* the |recvd_type|
  456. * argument is non NULL.
  457. * Also if record payloads contain fragments too small to process, we store
  458. * them until there is enough for the respective protocol (the record protocol
  459. * may use arbitrary fragmentation and even interleaving):
  460. * Change cipher spec protocol
  461. * just 1 byte needed, no need for keeping anything stored
  462. * Alert protocol
  463. * 2 bytes needed (AlertLevel, AlertDescription)
  464. * Handshake protocol
  465. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  466. * to detect unexpected Client Hello and Hello Request messages
  467. * here, anything else is handled by higher layers
  468. * Application data protocol
  469. * none of our business
  470. */
  471. int ssl3_read_bytes(SSL *ssl, int type, int *recvd_type, unsigned char *buf,
  472. size_t len, int peek, size_t *readbytes)
  473. {
  474. int i, j, ret;
  475. size_t n, curr_rec, totalbytes;
  476. TLS_RECORD *rr;
  477. void (*cb) (const SSL *ssl, int type2, int val) = NULL;
  478. int is_tls13;
  479. SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
  480. is_tls13 = SSL_CONNECTION_IS_TLS13(s);
  481. if ((type != 0
  482. && (type != SSL3_RT_APPLICATION_DATA)
  483. && (type != SSL3_RT_HANDSHAKE))
  484. || (peek && (type != SSL3_RT_APPLICATION_DATA))) {
  485. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  486. return -1;
  487. }
  488. if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0))
  489. /* (partially) satisfy request from storage */
  490. {
  491. unsigned char *src = s->rlayer.handshake_fragment;
  492. unsigned char *dst = buf;
  493. unsigned int k;
  494. /* peek == 0 */
  495. n = 0;
  496. while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) {
  497. *dst++ = *src++;
  498. len--;
  499. s->rlayer.handshake_fragment_len--;
  500. n++;
  501. }
  502. /* move any remaining fragment bytes: */
  503. for (k = 0; k < s->rlayer.handshake_fragment_len; k++)
  504. s->rlayer.handshake_fragment[k] = *src++;
  505. if (recvd_type != NULL)
  506. *recvd_type = SSL3_RT_HANDSHAKE;
  507. *readbytes = n;
  508. return 1;
  509. }
  510. /*
  511. * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
  512. */
  513. if (!ossl_statem_get_in_handshake(s) && SSL_in_init(ssl)) {
  514. /* type == SSL3_RT_APPLICATION_DATA */
  515. i = s->handshake_func(ssl);
  516. /* SSLfatal() already called */
  517. if (i < 0)
  518. return i;
  519. if (i == 0)
  520. return -1;
  521. }
  522. start:
  523. s->rwstate = SSL_NOTHING;
  524. /*-
  525. * For each record 'i' up to |num_recs]
  526. * rr[i].type - is the type of record
  527. * rr[i].data, - data
  528. * rr[i].off, - offset into 'data' for next read
  529. * rr[i].length, - number of bytes.
  530. */
  531. /* get new records if necessary */
  532. if (s->rlayer.curr_rec >= s->rlayer.num_recs) {
  533. s->rlayer.curr_rec = s->rlayer.num_recs = 0;
  534. do {
  535. rr = &s->rlayer.tlsrecs[s->rlayer.num_recs];
  536. ret = HANDLE_RLAYER_READ_RETURN(s,
  537. s->rlayer.rrlmethod->read_record(s->rlayer.rrl,
  538. &rr->rechandle,
  539. &rr->version, &rr->type,
  540. &rr->data, &rr->length,
  541. NULL, NULL));
  542. if (ret <= 0) {
  543. /* SSLfatal() already called if appropriate */
  544. return ret;
  545. }
  546. rr->off = 0;
  547. s->rlayer.num_recs++;
  548. } while (s->rlayer.rrlmethod->processed_read_pending(s->rlayer.rrl)
  549. && s->rlayer.num_recs < SSL_MAX_PIPELINES);
  550. }
  551. rr = &s->rlayer.tlsrecs[s->rlayer.curr_rec];
  552. if (s->rlayer.handshake_fragment_len > 0
  553. && rr->type != SSL3_RT_HANDSHAKE
  554. && SSL_CONNECTION_IS_TLS13(s)) {
  555. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  556. SSL_R_MIXED_HANDSHAKE_AND_NON_HANDSHAKE_DATA);
  557. return -1;
  558. }
  559. /*
  560. * Reset the count of consecutive warning alerts if we've got a non-empty
  561. * record that isn't an alert.
  562. */
  563. if (rr->type != SSL3_RT_ALERT && rr->length != 0)
  564. s->rlayer.alert_count = 0;
  565. /* we now have a packet which can be read and processed */
  566. if (s->s3.change_cipher_spec /* set when we receive ChangeCipherSpec,
  567. * reset by ssl3_get_finished */
  568. && (rr->type != SSL3_RT_HANDSHAKE)) {
  569. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  570. SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
  571. return -1;
  572. }
  573. /*
  574. * If the other end has shut down, throw anything we read away (even in
  575. * 'peek' mode)
  576. */
  577. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  578. s->rlayer.curr_rec++;
  579. s->rwstate = SSL_NOTHING;
  580. return 0;
  581. }
  582. if (type == rr->type
  583. || (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC
  584. && type == SSL3_RT_HANDSHAKE && recvd_type != NULL
  585. && !is_tls13)) {
  586. /*
  587. * SSL3_RT_APPLICATION_DATA or
  588. * SSL3_RT_HANDSHAKE or
  589. * SSL3_RT_CHANGE_CIPHER_SPEC
  590. */
  591. /*
  592. * make sure that we are not getting application data when we are
  593. * doing a handshake for the first time
  594. */
  595. if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA
  596. && SSL_IS_FIRST_HANDSHAKE(s)) {
  597. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_APP_DATA_IN_HANDSHAKE);
  598. return -1;
  599. }
  600. if (type == SSL3_RT_HANDSHAKE
  601. && rr->type == SSL3_RT_CHANGE_CIPHER_SPEC
  602. && s->rlayer.handshake_fragment_len > 0) {
  603. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
  604. return -1;
  605. }
  606. if (recvd_type != NULL)
  607. *recvd_type = rr->type;
  608. if (len == 0) {
  609. /*
  610. * Skip a zero length record. This ensures multiple calls to
  611. * SSL_read() with a zero length buffer will eventually cause
  612. * SSL_pending() to report data as being available.
  613. */
  614. if (rr->length == 0)
  615. ssl_release_record(s, rr);
  616. return 0;
  617. }
  618. totalbytes = 0;
  619. curr_rec = s->rlayer.curr_rec;
  620. do {
  621. if (len - totalbytes > rr->length)
  622. n = rr->length;
  623. else
  624. n = len - totalbytes;
  625. memcpy(buf, &(rr->data[rr->off]), n);
  626. buf += n;
  627. if (peek) {
  628. /* Mark any zero length record as consumed CVE-2016-6305 */
  629. if (rr->length == 0)
  630. ssl_release_record(s, rr);
  631. } else {
  632. if (s->options & SSL_OP_CLEANSE_PLAINTEXT)
  633. OPENSSL_cleanse(&(rr->data[rr->off]), n);
  634. rr->length -= n;
  635. rr->off += n;
  636. if (rr->length == 0)
  637. ssl_release_record(s, rr);
  638. }
  639. if (rr->length == 0
  640. || (peek && n == rr->length)) {
  641. rr++;
  642. curr_rec++;
  643. }
  644. totalbytes += n;
  645. } while (type == SSL3_RT_APPLICATION_DATA
  646. && curr_rec < s->rlayer.num_recs
  647. && totalbytes < len);
  648. if (totalbytes == 0) {
  649. /* We must have read empty records. Get more data */
  650. goto start;
  651. }
  652. *readbytes = totalbytes;
  653. return 1;
  654. }
  655. /*
  656. * If we get here, then type != rr->type; if we have a handshake message,
  657. * then it was unexpected (Hello Request or Client Hello) or invalid (we
  658. * were actually expecting a CCS).
  659. */
  660. /*
  661. * Lets just double check that we've not got an SSLv2 record
  662. */
  663. if (rr->version == SSL2_VERSION) {
  664. /*
  665. * Should never happen. ssl3_get_record() should only give us an SSLv2
  666. * record back if this is the first packet and we are looking for an
  667. * initial ClientHello. Therefore |type| should always be equal to
  668. * |rr->type|. If not then something has gone horribly wrong
  669. */
  670. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  671. return -1;
  672. }
  673. if (ssl->method->version == TLS_ANY_VERSION
  674. && (s->server || rr->type != SSL3_RT_ALERT)) {
  675. /*
  676. * If we've got this far and still haven't decided on what version
  677. * we're using then this must be a client side alert we're dealing
  678. * with. We shouldn't be receiving anything other than a ClientHello
  679. * if we are a server.
  680. */
  681. s->version = rr->version;
  682. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
  683. return -1;
  684. }
  685. /*-
  686. * s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
  687. * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
  688. */
  689. if (rr->type == SSL3_RT_ALERT) {
  690. unsigned int alert_level, alert_descr;
  691. unsigned char *alert_bytes = rr->data
  692. + rr->off;
  693. PACKET alert;
  694. if (!PACKET_buf_init(&alert, alert_bytes, rr->length)
  695. || !PACKET_get_1(&alert, &alert_level)
  696. || !PACKET_get_1(&alert, &alert_descr)
  697. || PACKET_remaining(&alert) != 0) {
  698. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT);
  699. return -1;
  700. }
  701. if (s->msg_callback)
  702. s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, ssl,
  703. s->msg_callback_arg);
  704. if (s->info_callback != NULL)
  705. cb = s->info_callback;
  706. else if (ssl->ctx->info_callback != NULL)
  707. cb = ssl->ctx->info_callback;
  708. if (cb != NULL) {
  709. j = (alert_level << 8) | alert_descr;
  710. cb(ssl, SSL_CB_READ_ALERT, j);
  711. }
  712. if ((!is_tls13 && alert_level == SSL3_AL_WARNING)
  713. || (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED)) {
  714. s->s3.warn_alert = alert_descr;
  715. ssl_release_record(s, rr);
  716. s->rlayer.alert_count++;
  717. if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
  718. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  719. SSL_R_TOO_MANY_WARN_ALERTS);
  720. return -1;
  721. }
  722. }
  723. /*
  724. * Apart from close_notify the only other warning alert in TLSv1.3
  725. * is user_cancelled - which we just ignore.
  726. */
  727. if (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED) {
  728. goto start;
  729. } else if (alert_descr == SSL_AD_CLOSE_NOTIFY
  730. && (is_tls13 || alert_level == SSL3_AL_WARNING)) {
  731. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  732. return 0;
  733. } else if (alert_level == SSL3_AL_FATAL || is_tls13) {
  734. s->rwstate = SSL_NOTHING;
  735. s->s3.fatal_alert = alert_descr;
  736. SSLfatal_data(s, SSL_AD_NO_ALERT,
  737. SSL_AD_REASON_OFFSET + alert_descr,
  738. "SSL alert number %d", alert_descr);
  739. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  740. ssl_release_record(s, rr);
  741. SSL_CTX_remove_session(s->session_ctx, s->session);
  742. return 0;
  743. } else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  744. /*
  745. * This is a warning but we receive it if we requested
  746. * renegotiation and the peer denied it. Terminate with a fatal
  747. * alert because if application tried to renegotiate it
  748. * presumably had a good reason and expects it to succeed. In
  749. * future we might have a renegotiation where we don't care if
  750. * the peer refused it where we carry on.
  751. */
  752. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_RENEGOTIATION);
  753. return -1;
  754. } else if (alert_level == SSL3_AL_WARNING) {
  755. /* We ignore any other warning alert in TLSv1.2 and below */
  756. goto start;
  757. }
  758. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE);
  759. return -1;
  760. }
  761. if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
  762. if (rr->type == SSL3_RT_HANDSHAKE) {
  763. BIO *rbio;
  764. /*
  765. * We ignore any handshake messages sent to us unless they are
  766. * TLSv1.3 in which case we want to process them. For all other
  767. * handshake messages we can't do anything reasonable with them
  768. * because we are unable to write any response due to having already
  769. * sent close_notify.
  770. */
  771. if (!SSL_CONNECTION_IS_TLS13(s)) {
  772. ssl_release_record(s, rr);
  773. if ((s->mode & SSL_MODE_AUTO_RETRY) != 0)
  774. goto start;
  775. s->rwstate = SSL_READING;
  776. rbio = SSL_get_rbio(ssl);
  777. BIO_clear_retry_flags(rbio);
  778. BIO_set_retry_read(rbio);
  779. return -1;
  780. }
  781. } else {
  782. /*
  783. * The peer is continuing to send application data, but we have
  784. * already sent close_notify. If this was expected we should have
  785. * been called via SSL_read() and this would have been handled
  786. * above.
  787. * No alert sent because we already sent close_notify
  788. */
  789. ssl_release_record(s, rr);
  790. SSLfatal(s, SSL_AD_NO_ALERT,
  791. SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY);
  792. return -1;
  793. }
  794. }
  795. /*
  796. * For handshake data we have 'fragment' storage, so fill that so that we
  797. * can process the header at a fixed place. This is done after the
  798. * "SHUTDOWN" code above to avoid filling the fragment storage with data
  799. * that we're just going to discard.
  800. */
  801. if (rr->type == SSL3_RT_HANDSHAKE) {
  802. size_t dest_maxlen = sizeof(s->rlayer.handshake_fragment);
  803. unsigned char *dest = s->rlayer.handshake_fragment;
  804. size_t *dest_len = &s->rlayer.handshake_fragment_len;
  805. n = dest_maxlen - *dest_len; /* available space in 'dest' */
  806. if (rr->length < n)
  807. n = rr->length; /* available bytes */
  808. /* now move 'n' bytes: */
  809. memcpy(dest + *dest_len, rr->data + rr->off, n);
  810. rr->off += n;
  811. rr->length -= n;
  812. *dest_len += n;
  813. if (rr->length == 0)
  814. ssl_release_record(s, rr);
  815. if (*dest_len < dest_maxlen)
  816. goto start; /* fragment was too small */
  817. }
  818. if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  819. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
  820. return -1;
  821. }
  822. /*
  823. * Unexpected handshake message (ClientHello, NewSessionTicket (TLS1.3) or
  824. * protocol violation)
  825. */
  826. if ((s->rlayer.handshake_fragment_len >= 4)
  827. && !ossl_statem_get_in_handshake(s)) {
  828. int ined = (s->early_data_state == SSL_EARLY_DATA_READING);
  829. /* We found handshake data, so we're going back into init */
  830. ossl_statem_set_in_init(s, 1);
  831. i = s->handshake_func(ssl);
  832. /* SSLfatal() already called if appropriate */
  833. if (i < 0)
  834. return i;
  835. if (i == 0) {
  836. return -1;
  837. }
  838. /*
  839. * If we were actually trying to read early data and we found a
  840. * handshake message, then we don't want to continue to try and read
  841. * the application data any more. It won't be "early" now.
  842. */
  843. if (ined)
  844. return -1;
  845. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  846. if (!RECORD_LAYER_read_pending(&s->rlayer)) {
  847. BIO *bio;
  848. /*
  849. * In the case where we try to read application data, but we
  850. * trigger an SSL handshake, we return -1 with the retry
  851. * option set. Otherwise renegotiation may cause nasty
  852. * problems in the blocking world
  853. */
  854. s->rwstate = SSL_READING;
  855. bio = SSL_get_rbio(ssl);
  856. BIO_clear_retry_flags(bio);
  857. BIO_set_retry_read(bio);
  858. return -1;
  859. }
  860. }
  861. goto start;
  862. }
  863. switch (rr->type) {
  864. default:
  865. /*
  866. * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but
  867. * TLS 1.2 says you MUST send an unexpected message alert. We use the
  868. * TLS 1.2 behaviour for all protocol versions to prevent issues where
  869. * no progress is being made and the peer continually sends unrecognised
  870. * record types, using up resources processing them.
  871. */
  872. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
  873. return -1;
  874. case SSL3_RT_CHANGE_CIPHER_SPEC:
  875. case SSL3_RT_ALERT:
  876. case SSL3_RT_HANDSHAKE:
  877. /*
  878. * we already handled all of these, with the possible exception of
  879. * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
  880. * that should not happen when type != rr->type
  881. */
  882. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR);
  883. return -1;
  884. case SSL3_RT_APPLICATION_DATA:
  885. /*
  886. * At this point, we were expecting handshake data, but have
  887. * application data. If the library was running inside ssl3_read()
  888. * (i.e. in_read_app_data is set) and it makes sense to read
  889. * application data at this point (session renegotiation not yet
  890. * started), we will indulge it.
  891. */
  892. if (ossl_statem_app_data_allowed(s)) {
  893. s->s3.in_read_app_data = 2;
  894. return -1;
  895. } else if (ossl_statem_skip_early_data(s)) {
  896. /*
  897. * This can happen after a client sends a CH followed by early_data,
  898. * but the server responds with a HelloRetryRequest. The server
  899. * reads the next record from the client expecting to find a
  900. * plaintext ClientHello but gets a record which appears to be
  901. * application data. The trial decrypt "works" because null
  902. * decryption was applied. We just skip it and move on to the next
  903. * record.
  904. */
  905. if (!ossl_early_data_count_ok(s, rr->length,
  906. EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
  907. /* SSLfatal() already called */
  908. return -1;
  909. }
  910. ssl_release_record(s, rr);
  911. goto start;
  912. } else {
  913. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
  914. return -1;
  915. }
  916. }
  917. }
  918. /*
  919. * Returns true if the current rrec was sent in SSLv2 backwards compatible
  920. * format and false otherwise.
  921. */
  922. int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl)
  923. {
  924. if (SSL_CONNECTION_IS_DTLS(rl->s))
  925. return 0;
  926. return rl->tlsrecs[0].version == SSL2_VERSION;
  927. }
  928. static OSSL_FUNC_rlayer_msg_callback_fn rlayer_msg_callback_wrapper;
  929. static void rlayer_msg_callback_wrapper(int write_p, int version,
  930. int content_type, const void *buf,
  931. size_t len, void *cbarg)
  932. {
  933. SSL_CONNECTION *s = cbarg;
  934. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  935. if (s->msg_callback != NULL)
  936. s->msg_callback(write_p, version, content_type, buf, len, ssl,
  937. s->msg_callback_arg);
  938. }
  939. static OSSL_FUNC_rlayer_security_fn rlayer_security_wrapper;
  940. static int rlayer_security_wrapper(void *cbarg, int op, int bits, int nid,
  941. void *other)
  942. {
  943. SSL_CONNECTION *s = cbarg;
  944. return ssl_security(s, op, bits, nid, other);
  945. }
  946. static OSSL_FUNC_rlayer_padding_fn rlayer_padding_wrapper;
  947. static size_t rlayer_padding_wrapper(void *cbarg, int type, size_t len)
  948. {
  949. SSL_CONNECTION *s = cbarg;
  950. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  951. return s->rlayer.record_padding_cb(ssl, type, len,
  952. s->rlayer.record_padding_arg);
  953. }
  954. static const OSSL_DISPATCH rlayer_dispatch[] = {
  955. { OSSL_FUNC_RLAYER_SKIP_EARLY_DATA, (void (*)(void))ossl_statem_skip_early_data },
  956. { OSSL_FUNC_RLAYER_MSG_CALLBACK, (void (*)(void))rlayer_msg_callback_wrapper },
  957. { OSSL_FUNC_RLAYER_SECURITY, (void (*)(void))rlayer_security_wrapper },
  958. { OSSL_FUNC_RLAYER_PADDING, (void (*)(void))rlayer_padding_wrapper },
  959. { 0, NULL }
  960. };
  961. void ossl_ssl_set_custom_record_layer(SSL_CONNECTION *s,
  962. const OSSL_RECORD_METHOD *meth,
  963. void *rlarg)
  964. {
  965. s->rlayer.custom_rlmethod = meth;
  966. s->rlayer.rlarg = rlarg;
  967. }
  968. static const OSSL_RECORD_METHOD *ssl_select_next_record_layer(SSL_CONNECTION *s,
  969. int direction,
  970. int level)
  971. {
  972. if (s->rlayer.custom_rlmethod != NULL)
  973. return s->rlayer.custom_rlmethod;
  974. if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE) {
  975. if (SSL_CONNECTION_IS_DTLS(s))
  976. return &ossl_dtls_record_method;
  977. return &ossl_tls_record_method;
  978. }
  979. #ifndef OPENSSL_NO_KTLS
  980. /* KTLS does not support renegotiation */
  981. if (level == OSSL_RECORD_PROTECTION_LEVEL_APPLICATION
  982. && (s->options & SSL_OP_ENABLE_KTLS) != 0
  983. && (SSL_CONNECTION_IS_TLS13(s) || SSL_IS_FIRST_HANDSHAKE(s)))
  984. return &ossl_ktls_record_method;
  985. #endif
  986. /* Default to the current OSSL_RECORD_METHOD */
  987. return direction == OSSL_RECORD_DIRECTION_READ ? s->rlayer.rrlmethod
  988. : s->rlayer.wrlmethod;
  989. }
  990. static int ssl_post_record_layer_select(SSL_CONNECTION *s, int direction)
  991. {
  992. const OSSL_RECORD_METHOD *thismethod;
  993. OSSL_RECORD_LAYER *thisrl;
  994. if (direction == OSSL_RECORD_DIRECTION_READ) {
  995. thismethod = s->rlayer.rrlmethod;
  996. thisrl = s->rlayer.rrl;
  997. } else {
  998. thismethod = s->rlayer.wrlmethod;
  999. thisrl = s->rlayer.wrl;
  1000. }
  1001. #ifndef OPENSSL_NO_KTLS
  1002. {
  1003. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  1004. if (s->rlayer.rrlmethod == &ossl_ktls_record_method) {
  1005. /* KTLS does not support renegotiation so disallow it */
  1006. SSL_set_options(ssl, SSL_OP_NO_RENEGOTIATION);
  1007. }
  1008. }
  1009. #endif
  1010. if (SSL_IS_FIRST_HANDSHAKE(s) && thismethod->set_first_handshake != NULL)
  1011. thismethod->set_first_handshake(thisrl, 1);
  1012. if (s->max_pipelines != 0 && thismethod->set_max_pipelines != NULL)
  1013. thismethod->set_max_pipelines(thisrl, s->max_pipelines);
  1014. return 1;
  1015. }
  1016. int ssl_set_new_record_layer(SSL_CONNECTION *s, int version,
  1017. int direction, int level,
  1018. unsigned char *secret, size_t secretlen,
  1019. unsigned char *key, size_t keylen,
  1020. unsigned char *iv, size_t ivlen,
  1021. unsigned char *mackey, size_t mackeylen,
  1022. const EVP_CIPHER *ciph, size_t taglen,
  1023. int mactype, const EVP_MD *md,
  1024. const SSL_COMP *comp, const EVP_MD *kdfdigest)
  1025. {
  1026. OSSL_PARAM options[5], *opts = options;
  1027. OSSL_PARAM settings[6], *set = settings;
  1028. const OSSL_RECORD_METHOD **thismethod;
  1029. OSSL_RECORD_LAYER **thisrl, *newrl = NULL;
  1030. BIO *thisbio;
  1031. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1032. const OSSL_RECORD_METHOD *meth;
  1033. int use_etm, stream_mac = 0, tlstree = 0;
  1034. unsigned int maxfrag = (direction == OSSL_RECORD_DIRECTION_WRITE)
  1035. ? ssl_get_max_send_fragment(s)
  1036. : SSL3_RT_MAX_PLAIN_LENGTH;
  1037. int use_early_data = 0;
  1038. uint32_t max_early_data;
  1039. COMP_METHOD *compm = (comp == NULL) ? NULL : comp->method;
  1040. meth = ssl_select_next_record_layer(s, direction, level);
  1041. if (direction == OSSL_RECORD_DIRECTION_READ) {
  1042. thismethod = &s->rlayer.rrlmethod;
  1043. thisrl = &s->rlayer.rrl;
  1044. thisbio = s->rbio;
  1045. } else {
  1046. thismethod = &s->rlayer.wrlmethod;
  1047. thisrl = &s->rlayer.wrl;
  1048. thisbio = s->wbio;
  1049. }
  1050. if (meth == NULL)
  1051. meth = *thismethod;
  1052. if (!ossl_assert(meth != NULL)) {
  1053. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  1054. return 0;
  1055. }
  1056. /* Parameters that *may* be supported by a record layer if passed */
  1057. *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
  1058. &s->options);
  1059. *opts++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE,
  1060. &s->mode);
  1061. if (direction == OSSL_RECORD_DIRECTION_READ) {
  1062. *opts++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_READ_BUFFER_LEN,
  1063. &s->rlayer.default_read_buf_len);
  1064. *opts++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD,
  1065. &s->rlayer.read_ahead);
  1066. } else {
  1067. *opts++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_PARAM_BLOCK_PADDING,
  1068. &s->rlayer.block_padding);
  1069. }
  1070. *opts = OSSL_PARAM_construct_end();
  1071. /* Parameters that *must* be supported by a record layer if passed */
  1072. if (direction == OSSL_RECORD_DIRECTION_READ) {
  1073. use_etm = SSL_READ_ETM(s) ? 1 : 0;
  1074. if ((s->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM) != 0)
  1075. stream_mac = 1;
  1076. if ((s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE) != 0)
  1077. tlstree = 1;
  1078. } else {
  1079. use_etm = SSL_WRITE_ETM(s) ? 1 : 0;
  1080. if ((s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM) != 0)
  1081. stream_mac = 1;
  1082. if ((s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE) != 0)
  1083. tlstree = 1;
  1084. }
  1085. if (use_etm)
  1086. *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_USE_ETM,
  1087. &use_etm);
  1088. if (stream_mac)
  1089. *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_STREAM_MAC,
  1090. &stream_mac);
  1091. if (tlstree)
  1092. *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_TLSTREE,
  1093. &tlstree);
  1094. /*
  1095. * We only need to do this for the read side. The write side should already
  1096. * have the correct value due to the ssl_get_max_send_fragment() call above
  1097. */
  1098. if (direction == OSSL_RECORD_DIRECTION_READ
  1099. && s->session != NULL
  1100. && USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
  1101. maxfrag = GET_MAX_FRAGMENT_LENGTH(s->session);
  1102. if (maxfrag != SSL3_RT_MAX_PLAIN_LENGTH)
  1103. *set++ = OSSL_PARAM_construct_uint(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN,
  1104. &maxfrag);
  1105. /*
  1106. * The record layer must check the amount of early data sent or received
  1107. * using the early keys. A server also needs to worry about rejected early
  1108. * data that might arrive when the handshake keys are in force.
  1109. */
  1110. if (s->server && direction == OSSL_RECORD_DIRECTION_READ) {
  1111. use_early_data = (level == OSSL_RECORD_PROTECTION_LEVEL_EARLY
  1112. || level == OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE);
  1113. } else if (!s->server && direction == OSSL_RECORD_DIRECTION_WRITE) {
  1114. use_early_data = (level == OSSL_RECORD_PROTECTION_LEVEL_EARLY);
  1115. }
  1116. if (use_early_data) {
  1117. max_early_data = ossl_get_max_early_data(s);
  1118. if (max_early_data != 0)
  1119. *set++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA,
  1120. &max_early_data);
  1121. }
  1122. *set = OSSL_PARAM_construct_end();
  1123. for (;;) {
  1124. int rlret;
  1125. BIO *prev = NULL;
  1126. BIO *next = NULL;
  1127. unsigned int epoch = 0;
  1128. OSSL_DISPATCH rlayer_dispatch_tmp[OSSL_NELEM(rlayer_dispatch)];
  1129. size_t i, j;
  1130. if (direction == OSSL_RECORD_DIRECTION_READ) {
  1131. prev = s->rlayer.rrlnext;
  1132. if (SSL_CONNECTION_IS_DTLS(s)
  1133. && level != OSSL_RECORD_PROTECTION_LEVEL_NONE)
  1134. epoch = DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer) + 1; /* new epoch */
  1135. #ifndef OPENSSL_NO_DGRAM
  1136. if (SSL_CONNECTION_IS_DTLS(s))
  1137. next = BIO_new(BIO_s_dgram_mem());
  1138. else
  1139. #endif
  1140. next = BIO_new(BIO_s_mem());
  1141. if (next == NULL) {
  1142. BIO_free(prev);
  1143. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1144. return 0;
  1145. }
  1146. s->rlayer.rrlnext = next;
  1147. } else {
  1148. if (SSL_CONNECTION_IS_DTLS(s)
  1149. && level != OSSL_RECORD_PROTECTION_LEVEL_NONE)
  1150. epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) + 1; /* new epoch */
  1151. }
  1152. /*
  1153. * Create a copy of the dispatch array, missing out wrappers for
  1154. * callbacks that we don't need.
  1155. */
  1156. for (i = 0, j = 0; i < OSSL_NELEM(rlayer_dispatch); i++) {
  1157. switch (rlayer_dispatch[i].function_id) {
  1158. case OSSL_FUNC_RLAYER_MSG_CALLBACK:
  1159. if (s->msg_callback == NULL)
  1160. continue;
  1161. break;
  1162. case OSSL_FUNC_RLAYER_PADDING:
  1163. if (s->rlayer.record_padding_cb == NULL)
  1164. continue;
  1165. break;
  1166. default:
  1167. break;
  1168. }
  1169. rlayer_dispatch_tmp[j++] = rlayer_dispatch[i];
  1170. }
  1171. rlret = meth->new_record_layer(sctx->libctx, sctx->propq, version,
  1172. s->server, direction, level, epoch,
  1173. secret, secretlen, key, keylen, iv,
  1174. ivlen, mackey, mackeylen, ciph, taglen,
  1175. mactype, md, compm, kdfdigest, prev,
  1176. thisbio, next, NULL, NULL, settings,
  1177. options, rlayer_dispatch_tmp, s,
  1178. s->rlayer.rlarg, &newrl);
  1179. BIO_free(prev);
  1180. switch (rlret) {
  1181. case OSSL_RECORD_RETURN_FATAL:
  1182. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_RECORD_LAYER_FAILURE);
  1183. return 0;
  1184. case OSSL_RECORD_RETURN_NON_FATAL_ERR:
  1185. if (*thismethod != meth && *thismethod != NULL) {
  1186. /*
  1187. * We tried a new record layer method, but it didn't work out,
  1188. * so we fallback to the original method and try again
  1189. */
  1190. meth = *thismethod;
  1191. continue;
  1192. }
  1193. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_RECORD_LAYER);
  1194. return 0;
  1195. case OSSL_RECORD_RETURN_SUCCESS:
  1196. break;
  1197. default:
  1198. /* Should not happen */
  1199. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1200. return 0;
  1201. }
  1202. break;
  1203. }
  1204. /*
  1205. * Free the old record layer if we have one except in the case of DTLS when
  1206. * writing and there are still buffered sent messages in our queue. In that
  1207. * case the record layer is still referenced by those buffered messages for
  1208. * potential retransmit. Only when those buffered messages get freed do we
  1209. * free the record layer object (see dtls1_hm_fragment_free)
  1210. */
  1211. if (!SSL_CONNECTION_IS_DTLS(s)
  1212. || direction == OSSL_RECORD_DIRECTION_READ
  1213. || pqueue_peek(s->d1->sent_messages) == NULL) {
  1214. if (*thismethod != NULL && !(*thismethod)->free(*thisrl)) {
  1215. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1216. return 0;
  1217. }
  1218. }
  1219. *thisrl = newrl;
  1220. *thismethod = meth;
  1221. return ssl_post_record_layer_select(s, direction);
  1222. }
  1223. int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers)
  1224. {
  1225. if (!ossl_assert(s->rlayer.rrlmethod != NULL)
  1226. || !ossl_assert(s->rlayer.wrlmethod != NULL))
  1227. return 0;
  1228. s->rlayer.rrlmethod->set_protocol_version(s->rlayer.rrl, s->version);
  1229. s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, s->version);
  1230. return 1;
  1231. }