rec_layer_s3.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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. static const OSSL_RECORD_METHOD *ssl_select_next_record_layer(SSL_CONNECTION *s,
  962. int direction,
  963. int level)
  964. {
  965. if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE) {
  966. if (SSL_CONNECTION_IS_DTLS(s))
  967. return &ossl_dtls_record_method;
  968. return &ossl_tls_record_method;
  969. }
  970. #ifndef OPENSSL_NO_KTLS
  971. /* KTLS does not support renegotiation */
  972. if (level == OSSL_RECORD_PROTECTION_LEVEL_APPLICATION
  973. && (s->options & SSL_OP_ENABLE_KTLS) != 0
  974. && (SSL_CONNECTION_IS_TLS13(s) || SSL_IS_FIRST_HANDSHAKE(s)))
  975. return &ossl_ktls_record_method;
  976. #endif
  977. /* Default to the current OSSL_RECORD_METHOD */
  978. return direction == OSSL_RECORD_DIRECTION_READ ? s->rlayer.rrlmethod
  979. : s->rlayer.wrlmethod;
  980. }
  981. static int ssl_post_record_layer_select(SSL_CONNECTION *s, int direction)
  982. {
  983. const OSSL_RECORD_METHOD *thismethod;
  984. OSSL_RECORD_LAYER *thisrl;
  985. if (direction == OSSL_RECORD_DIRECTION_READ) {
  986. thismethod = s->rlayer.rrlmethod;
  987. thisrl = s->rlayer.rrl;
  988. } else {
  989. thismethod = s->rlayer.wrlmethod;
  990. thisrl = s->rlayer.wrl;
  991. }
  992. #ifndef OPENSSL_NO_KTLS
  993. {
  994. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  995. if (s->rlayer.rrlmethod == &ossl_ktls_record_method) {
  996. /* KTLS does not support renegotiation so disallow it */
  997. SSL_set_options(ssl, SSL_OP_NO_RENEGOTIATION);
  998. }
  999. }
  1000. #endif
  1001. if (SSL_IS_FIRST_HANDSHAKE(s) && thismethod->set_first_handshake != NULL)
  1002. thismethod->set_first_handshake(thisrl, 1);
  1003. if (s->max_pipelines != 0 && thismethod->set_max_pipelines != NULL)
  1004. thismethod->set_max_pipelines(thisrl, s->max_pipelines);
  1005. return 1;
  1006. }
  1007. int ssl_set_new_record_layer(SSL_CONNECTION *s, int version,
  1008. int direction, int level,
  1009. unsigned char *key, size_t keylen,
  1010. unsigned char *iv, size_t ivlen,
  1011. unsigned char *mackey, size_t mackeylen,
  1012. const EVP_CIPHER *ciph, size_t taglen,
  1013. int mactype, const EVP_MD *md,
  1014. const SSL_COMP *comp)
  1015. {
  1016. OSSL_PARAM options[5], *opts = options;
  1017. OSSL_PARAM settings[6], *set = settings;
  1018. const OSSL_RECORD_METHOD **thismethod;
  1019. OSSL_RECORD_LAYER **thisrl, *newrl = NULL;
  1020. BIO *thisbio;
  1021. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  1022. const OSSL_RECORD_METHOD *meth;
  1023. int use_etm, stream_mac = 0, tlstree = 0;
  1024. unsigned int maxfrag = (direction == OSSL_RECORD_DIRECTION_WRITE)
  1025. ? ssl_get_max_send_fragment(s)
  1026. : SSL3_RT_MAX_PLAIN_LENGTH;
  1027. int use_early_data = 0;
  1028. uint32_t max_early_data;
  1029. COMP_METHOD *compm = (comp == NULL) ? NULL : comp->method;
  1030. meth = ssl_select_next_record_layer(s, direction, level);
  1031. if (direction == OSSL_RECORD_DIRECTION_READ) {
  1032. thismethod = &s->rlayer.rrlmethod;
  1033. thisrl = &s->rlayer.rrl;
  1034. thisbio = s->rbio;
  1035. } else {
  1036. thismethod = &s->rlayer.wrlmethod;
  1037. thisrl = &s->rlayer.wrl;
  1038. thisbio = s->wbio;
  1039. }
  1040. if (meth == NULL)
  1041. meth = *thismethod;
  1042. if (!ossl_assert(meth != NULL)) {
  1043. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  1044. return 0;
  1045. }
  1046. /* Parameters that *may* be supported by a record layer if passed */
  1047. *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
  1048. &s->options);
  1049. *opts++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE,
  1050. &s->mode);
  1051. if (direction == OSSL_RECORD_DIRECTION_READ) {
  1052. *opts++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_READ_BUFFER_LEN,
  1053. &s->rlayer.default_read_buf_len);
  1054. *opts++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD,
  1055. &s->rlayer.read_ahead);
  1056. } else {
  1057. *opts++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_PARAM_BLOCK_PADDING,
  1058. &s->rlayer.block_padding);
  1059. }
  1060. *opts = OSSL_PARAM_construct_end();
  1061. /* Parameters that *must* be supported by a record layer if passed */
  1062. if (direction == OSSL_RECORD_DIRECTION_READ) {
  1063. use_etm = SSL_READ_ETM(s) ? 1 : 0;
  1064. if ((s->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM) != 0)
  1065. stream_mac = 1;
  1066. if ((s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE) != 0)
  1067. tlstree = 1;
  1068. } else {
  1069. use_etm = SSL_WRITE_ETM(s) ? 1 : 0;
  1070. if ((s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM) != 0)
  1071. stream_mac = 1;
  1072. if ((s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE) != 0)
  1073. tlstree = 1;
  1074. }
  1075. if (use_etm)
  1076. *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_USE_ETM,
  1077. &use_etm);
  1078. if (stream_mac)
  1079. *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_STREAM_MAC,
  1080. &stream_mac);
  1081. if (tlstree)
  1082. *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_TLSTREE,
  1083. &tlstree);
  1084. /*
  1085. * We only need to do this for the read side. The write side should already
  1086. * have the correct value due to the ssl_get_max_send_fragment() call above
  1087. */
  1088. if (direction == OSSL_RECORD_DIRECTION_READ
  1089. && s->session != NULL
  1090. && USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
  1091. maxfrag = GET_MAX_FRAGMENT_LENGTH(s->session);
  1092. if (maxfrag != SSL3_RT_MAX_PLAIN_LENGTH)
  1093. *set++ = OSSL_PARAM_construct_uint(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN,
  1094. &maxfrag);
  1095. /*
  1096. * The record layer must check the amount of early data sent or received
  1097. * using the early keys. A server also needs to worry about rejected early
  1098. * data that might arrive when the handshake keys are in force.
  1099. */
  1100. if (s->server && direction == OSSL_RECORD_DIRECTION_READ) {
  1101. use_early_data = (level == OSSL_RECORD_PROTECTION_LEVEL_EARLY
  1102. || level == OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE);
  1103. } else if (!s->server && direction == OSSL_RECORD_DIRECTION_WRITE) {
  1104. use_early_data = (level == OSSL_RECORD_PROTECTION_LEVEL_EARLY);
  1105. }
  1106. if (use_early_data) {
  1107. max_early_data = ossl_get_max_early_data(s);
  1108. if (max_early_data != 0)
  1109. *set++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA,
  1110. &max_early_data);
  1111. }
  1112. *set = OSSL_PARAM_construct_end();
  1113. for (;;) {
  1114. int rlret;
  1115. BIO *prev = NULL;
  1116. BIO *next = NULL;
  1117. unsigned int epoch = 0;
  1118. OSSL_DISPATCH rlayer_dispatch_tmp[OSSL_NELEM(rlayer_dispatch)];
  1119. size_t i, j;
  1120. if (direction == OSSL_RECORD_DIRECTION_READ) {
  1121. prev = s->rlayer.rrlnext;
  1122. if (SSL_CONNECTION_IS_DTLS(s)
  1123. && level != OSSL_RECORD_PROTECTION_LEVEL_NONE)
  1124. epoch = DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer) + 1; /* new epoch */
  1125. if (SSL_CONNECTION_IS_DTLS(s))
  1126. next = BIO_new(BIO_s_dgram_mem());
  1127. else
  1128. next = BIO_new(BIO_s_mem());
  1129. if (next == NULL) {
  1130. BIO_free(prev);
  1131. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1132. return 0;
  1133. }
  1134. s->rlayer.rrlnext = next;
  1135. } else {
  1136. if (SSL_CONNECTION_IS_DTLS(s)
  1137. && level != OSSL_RECORD_PROTECTION_LEVEL_NONE)
  1138. epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) + 1; /* new epoch */
  1139. }
  1140. /*
  1141. * Create a copy of the dispatch array, missing out wrappers for
  1142. * callbacks that we don't need.
  1143. */
  1144. for (i = 0, j = 0; i < OSSL_NELEM(rlayer_dispatch); i++) {
  1145. switch (rlayer_dispatch[i].function_id) {
  1146. case OSSL_FUNC_RLAYER_MSG_CALLBACK:
  1147. if (s->msg_callback == NULL)
  1148. continue;
  1149. break;
  1150. case OSSL_FUNC_RLAYER_PADDING:
  1151. if (s->rlayer.record_padding_cb == NULL)
  1152. continue;
  1153. break;
  1154. default:
  1155. break;
  1156. }
  1157. rlayer_dispatch_tmp[j++] = rlayer_dispatch[i];
  1158. }
  1159. rlret = meth->new_record_layer(sctx->libctx, sctx->propq, version,
  1160. s->server, direction, level, epoch,
  1161. key, keylen, iv, ivlen, mackey,
  1162. mackeylen, ciph, taglen, mactype, md,
  1163. compm, prev, thisbio, next, NULL, NULL,
  1164. settings, options, rlayer_dispatch_tmp,
  1165. s, &newrl);
  1166. BIO_free(prev);
  1167. switch (rlret) {
  1168. case OSSL_RECORD_RETURN_FATAL:
  1169. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_RECORD_LAYER_FAILURE);
  1170. return 0;
  1171. case OSSL_RECORD_RETURN_NON_FATAL_ERR:
  1172. if (*thismethod != meth && *thismethod != NULL) {
  1173. /*
  1174. * We tried a new record layer method, but it didn't work out,
  1175. * so we fallback to the original method and try again
  1176. */
  1177. meth = *thismethod;
  1178. continue;
  1179. }
  1180. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_RECORD_LAYER);
  1181. return 0;
  1182. case OSSL_RECORD_RETURN_SUCCESS:
  1183. break;
  1184. default:
  1185. /* Should not happen */
  1186. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1187. return 0;
  1188. }
  1189. break;
  1190. }
  1191. /*
  1192. * Free the old record layer if we have one except in the case of DTLS when
  1193. * writing and there are still buffered sent messages in our queue. In that
  1194. * case the record layer is still referenced by those buffered messages for
  1195. * potential retransmit. Only when those buffered messages get freed do we
  1196. * free the record layer object (see dtls1_hm_fragment_free)
  1197. */
  1198. if (!SSL_CONNECTION_IS_DTLS(s)
  1199. || direction == OSSL_RECORD_DIRECTION_READ
  1200. || pqueue_peek(s->d1->sent_messages) == NULL) {
  1201. if (*thismethod != NULL && !(*thismethod)->free(*thisrl)) {
  1202. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1203. return 0;
  1204. }
  1205. }
  1206. *thisrl = newrl;
  1207. *thismethod = meth;
  1208. return ssl_post_record_layer_select(s, direction);
  1209. }
  1210. int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers)
  1211. {
  1212. if (!ossl_assert(s->rlayer.rrlmethod != NULL)
  1213. || !ossl_assert(s->rlayer.wrlmethod != NULL))
  1214. return 0;
  1215. s->rlayer.rrlmethod->set_protocol_version(s->rlayer.rrl, s->version);
  1216. s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, s->version);
  1217. return 1;
  1218. }