tls_common.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152
  1. /*
  2. * Copyright 2022-2023 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 <assert.h>
  10. #include <openssl/bio.h>
  11. #include <openssl/ssl.h>
  12. #include <openssl/err.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/comp.h>
  15. #include <openssl/ssl.h>
  16. #include "internal/e_os.h"
  17. #include "internal/packet.h"
  18. #include "internal/ssl3_cbc.h"
  19. #include "../../ssl_local.h"
  20. #include "../record_local.h"
  21. #include "recmethod_local.h"
  22. static void tls_int_free(OSSL_RECORD_LAYER *rl);
  23. void ossl_tls_buffer_release(TLS_BUFFER *b)
  24. {
  25. OPENSSL_free(b->buf);
  26. b->buf = NULL;
  27. }
  28. static void TLS_RL_RECORD_release(TLS_RL_RECORD *r, size_t num_recs)
  29. {
  30. size_t i;
  31. for (i = 0; i < num_recs; i++) {
  32. OPENSSL_free(r[i].comp);
  33. r[i].comp = NULL;
  34. }
  35. }
  36. void ossl_tls_rl_record_set_seq_num(TLS_RL_RECORD *r,
  37. const unsigned char *seq_num)
  38. {
  39. memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
  40. }
  41. void ossl_rlayer_fatal(OSSL_RECORD_LAYER *rl, int al, int reason,
  42. const char *fmt, ...)
  43. {
  44. va_list args;
  45. va_start(args, fmt);
  46. ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
  47. va_end(args);
  48. rl->alert = al;
  49. }
  50. int ossl_set_tls_provider_parameters(OSSL_RECORD_LAYER *rl,
  51. EVP_CIPHER_CTX *ctx,
  52. const EVP_CIPHER *ciph,
  53. const EVP_MD *md)
  54. {
  55. /*
  56. * Provided cipher, the TLS padding/MAC removal is performed provider
  57. * side so we need to tell the ctx about our TLS version and mac size
  58. */
  59. OSSL_PARAM params[3], *pprm = params;
  60. size_t macsize = 0;
  61. int imacsize = -1;
  62. if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0
  63. && !rl->use_etm)
  64. imacsize = EVP_MD_get_size(md);
  65. if (imacsize >= 0)
  66. macsize = (size_t)imacsize;
  67. *pprm++ = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
  68. &rl->version);
  69. *pprm++ = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE,
  70. &macsize);
  71. *pprm = OSSL_PARAM_construct_end();
  72. if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
  73. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  74. return 0;
  75. }
  76. return 1;
  77. }
  78. /*
  79. * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
  80. * which ssl3_cbc_digest_record supports.
  81. */
  82. char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
  83. {
  84. switch (EVP_MD_CTX_get_type(ctx)) {
  85. case NID_md5:
  86. case NID_sha1:
  87. case NID_sha224:
  88. case NID_sha256:
  89. case NID_sha384:
  90. case NID_sha512:
  91. return 1;
  92. default:
  93. return 0;
  94. }
  95. }
  96. #ifndef OPENSSL_NO_COMP
  97. static int tls_allow_compression(OSSL_RECORD_LAYER *rl)
  98. {
  99. if (rl->options & SSL_OP_NO_COMPRESSION)
  100. return 0;
  101. return rl->security == NULL
  102. || rl->security(rl->cbarg, SSL_SECOP_COMPRESSION, 0, 0, NULL);
  103. }
  104. #endif
  105. static void tls_release_write_buffer_int(OSSL_RECORD_LAYER *rl, size_t start)
  106. {
  107. TLS_BUFFER *wb;
  108. size_t pipes;
  109. pipes = rl->numwpipes;
  110. while (pipes > start) {
  111. wb = &rl->wbuf[pipes - 1];
  112. if (TLS_BUFFER_is_app_buffer(wb))
  113. TLS_BUFFER_set_app_buffer(wb, 0);
  114. else
  115. OPENSSL_free(wb->buf);
  116. wb->buf = NULL;
  117. pipes--;
  118. }
  119. }
  120. int tls_setup_write_buffer(OSSL_RECORD_LAYER *rl, size_t numwpipes,
  121. size_t firstlen, size_t nextlen)
  122. {
  123. unsigned char *p;
  124. size_t align = 0, headerlen;
  125. TLS_BUFFER *wb;
  126. size_t currpipe;
  127. size_t defltlen = 0;
  128. if (firstlen == 0 || (numwpipes > 1 && nextlen == 0)) {
  129. if (rl->isdtls)
  130. headerlen = DTLS1_RT_HEADER_LENGTH + 1;
  131. else
  132. headerlen = SSL3_RT_HEADER_LENGTH;
  133. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  134. align = SSL3_ALIGN_PAYLOAD - 1;
  135. #endif
  136. defltlen = rl->max_frag_len + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
  137. + headerlen + align + rl->eivlen;
  138. #ifndef OPENSSL_NO_COMP
  139. if (tls_allow_compression(rl))
  140. defltlen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  141. #endif
  142. /*
  143. * We don't need to add eivlen here since empty fragments only occur
  144. * when we don't have an explicit IV
  145. */
  146. if (!(rl->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  147. defltlen += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
  148. }
  149. wb = rl->wbuf;
  150. for (currpipe = 0; currpipe < numwpipes; currpipe++) {
  151. TLS_BUFFER *thiswb = &wb[currpipe];
  152. size_t len = (currpipe == 0) ? firstlen : nextlen;
  153. if (len == 0)
  154. len = defltlen;
  155. if (thiswb->len != len) {
  156. OPENSSL_free(thiswb->buf);
  157. thiswb->buf = NULL; /* force reallocation */
  158. }
  159. p = thiswb->buf;
  160. if (p == NULL) {
  161. p = OPENSSL_malloc(len);
  162. if (p == NULL) {
  163. if (rl->numwpipes < currpipe)
  164. rl->numwpipes = currpipe;
  165. /*
  166. * We've got a malloc failure, and we're still initialising
  167. * buffers. We assume we're so doomed that we won't even be able
  168. * to send an alert.
  169. */
  170. RLAYERfatal(rl, SSL_AD_NO_ALERT, ERR_R_CRYPTO_LIB);
  171. return 0;
  172. }
  173. }
  174. memset(thiswb, 0, sizeof(TLS_BUFFER));
  175. thiswb->buf = p;
  176. thiswb->len = len;
  177. }
  178. /* Free any previously allocated buffers that we are no longer using */
  179. tls_release_write_buffer_int(rl, currpipe);
  180. rl->numwpipes = numwpipes;
  181. return 1;
  182. }
  183. static void tls_release_write_buffer(OSSL_RECORD_LAYER *rl)
  184. {
  185. tls_release_write_buffer_int(rl, 0);
  186. rl->numwpipes = 0;
  187. }
  188. int tls_setup_read_buffer(OSSL_RECORD_LAYER *rl)
  189. {
  190. unsigned char *p;
  191. size_t len, align = 0, headerlen;
  192. TLS_BUFFER *b;
  193. b = &rl->rbuf;
  194. if (rl->isdtls)
  195. headerlen = DTLS1_RT_HEADER_LENGTH;
  196. else
  197. headerlen = SSL3_RT_HEADER_LENGTH;
  198. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  199. align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
  200. #endif
  201. if (b->buf == NULL) {
  202. len = rl->max_frag_len
  203. + SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
  204. #ifndef OPENSSL_NO_COMP
  205. if (tls_allow_compression(rl))
  206. len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  207. #endif
  208. /* Ensure our buffer is large enough to support all our pipelines */
  209. if (rl->max_pipelines > 1)
  210. len *= rl->max_pipelines;
  211. if (b->default_len > len)
  212. len = b->default_len;
  213. if ((p = OPENSSL_malloc(len)) == NULL) {
  214. /*
  215. * We've got a malloc failure, and we're still initialising buffers.
  216. * We assume we're so doomed that we won't even be able to send an
  217. * alert.
  218. */
  219. RLAYERfatal(rl, SSL_AD_NO_ALERT, ERR_R_CRYPTO_LIB);
  220. return 0;
  221. }
  222. b->buf = p;
  223. b->len = len;
  224. }
  225. return 1;
  226. }
  227. static int tls_release_read_buffer(OSSL_RECORD_LAYER *rl)
  228. {
  229. TLS_BUFFER *b;
  230. b = &rl->rbuf;
  231. if ((rl->options & SSL_OP_CLEANSE_PLAINTEXT) != 0)
  232. OPENSSL_cleanse(b->buf, b->len);
  233. OPENSSL_free(b->buf);
  234. b->buf = NULL;
  235. return 1;
  236. }
  237. /*
  238. * Return values are as per SSL_read()
  239. */
  240. int tls_default_read_n(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
  241. int clearold, size_t *readbytes)
  242. {
  243. /*
  244. * If extend == 0, obtain new n-byte packet; if extend == 1, increase
  245. * packet by another n bytes. The packet will be in the sub-array of
  246. * rl->rbuf.buf specified by rl->packet and rl->packet_length. (If
  247. * rl->read_ahead is set, 'max' bytes may be stored in rbuf [plus
  248. * rl->packet_length bytes if extend == 1].) if clearold == 1, move the
  249. * packet to the start of the buffer; if clearold == 0 then leave any old
  250. * packets where they were
  251. */
  252. size_t len, left, align = 0;
  253. unsigned char *pkt;
  254. TLS_BUFFER *rb;
  255. if (n == 0)
  256. return OSSL_RECORD_RETURN_NON_FATAL_ERR;
  257. rb = &rl->rbuf;
  258. left = rb->left;
  259. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  260. align = (size_t)rb->buf + SSL3_RT_HEADER_LENGTH;
  261. align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
  262. #endif
  263. if (!extend) {
  264. /* start with empty packet ... */
  265. if (left == 0)
  266. rb->offset = align;
  267. rl->packet = rb->buf + rb->offset;
  268. rl->packet_length = 0;
  269. /* ... now we can act as if 'extend' was set */
  270. }
  271. len = rl->packet_length;
  272. pkt = rb->buf + align;
  273. /*
  274. * Move any available bytes to front of buffer: 'len' bytes already
  275. * pointed to by 'packet', 'left' extra ones at the end
  276. */
  277. if (rl->packet != pkt && clearold == 1) {
  278. memmove(pkt, rl->packet, len + left);
  279. rl->packet = pkt;
  280. rb->offset = len + align;
  281. }
  282. /*
  283. * For DTLS/UDP reads should not span multiple packets because the read
  284. * operation returns the whole packet at once (as long as it fits into
  285. * the buffer).
  286. */
  287. if (rl->isdtls) {
  288. if (left == 0 && extend) {
  289. /*
  290. * We received a record with a header but no body data. This will
  291. * get dumped.
  292. */
  293. return OSSL_RECORD_RETURN_NON_FATAL_ERR;
  294. }
  295. if (left > 0 && n > left)
  296. n = left;
  297. }
  298. /* if there is enough in the buffer from a previous read, take some */
  299. if (left >= n) {
  300. rl->packet_length += n;
  301. rb->left = left - n;
  302. rb->offset += n;
  303. *readbytes = n;
  304. return OSSL_RECORD_RETURN_SUCCESS;
  305. }
  306. /* else we need to read more data */
  307. if (n > rb->len - rb->offset) {
  308. /* does not happen */
  309. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  310. return OSSL_RECORD_RETURN_FATAL;
  311. }
  312. /* We always act like read_ahead is set for DTLS */
  313. if (!rl->read_ahead && !rl->isdtls) {
  314. /* ignore max parameter */
  315. max = n;
  316. } else {
  317. if (max < n)
  318. max = n;
  319. if (max > rb->len - rb->offset)
  320. max = rb->len - rb->offset;
  321. }
  322. while (left < n) {
  323. size_t bioread = 0;
  324. int ret;
  325. BIO *bio = rl->prev != NULL ? rl->prev : rl->bio;
  326. /*
  327. * Now we have len+left bytes at the front of rl->rbuf.buf and
  328. * need to read in more until we have len + n (up to len + max if
  329. * possible)
  330. */
  331. clear_sys_error();
  332. if (bio != NULL) {
  333. ret = BIO_read(bio, pkt + len + left, max - left);
  334. if (ret > 0) {
  335. bioread = ret;
  336. ret = OSSL_RECORD_RETURN_SUCCESS;
  337. } else if (BIO_should_retry(bio)) {
  338. if (rl->prev != NULL) {
  339. /*
  340. * We were reading from the previous epoch. Now there is no
  341. * more data, so swap to the actual transport BIO
  342. */
  343. BIO_free(rl->prev);
  344. rl->prev = NULL;
  345. continue;
  346. }
  347. ret = OSSL_RECORD_RETURN_RETRY;
  348. } else if (BIO_eof(bio)) {
  349. ret = OSSL_RECORD_RETURN_EOF;
  350. } else {
  351. ret = OSSL_RECORD_RETURN_FATAL;
  352. }
  353. } else {
  354. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_READ_BIO_NOT_SET);
  355. ret = OSSL_RECORD_RETURN_FATAL;
  356. }
  357. if (ret <= OSSL_RECORD_RETURN_RETRY) {
  358. rb->left = left;
  359. if ((rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0 && !rl->isdtls)
  360. if (len + left == 0)
  361. tls_release_read_buffer(rl);
  362. return ret;
  363. }
  364. left += bioread;
  365. /*
  366. * reads should *never* span multiple packets for DTLS because the
  367. * underlying transport protocol is message oriented as opposed to
  368. * byte oriented as in the TLS case.
  369. */
  370. if (rl->isdtls) {
  371. if (n > left)
  372. n = left; /* makes the while condition false */
  373. }
  374. }
  375. /* done reading, now the book-keeping */
  376. rb->offset += n;
  377. rb->left = left - n;
  378. rl->packet_length += n;
  379. *readbytes = n;
  380. return OSSL_RECORD_RETURN_SUCCESS;
  381. }
  382. /*
  383. * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
  384. * for us in the buffer.
  385. */
  386. static int tls_record_app_data_waiting(OSSL_RECORD_LAYER *rl)
  387. {
  388. TLS_BUFFER *rbuf;
  389. size_t left, len;
  390. unsigned char *p;
  391. rbuf = &rl->rbuf;
  392. p = TLS_BUFFER_get_buf(rbuf);
  393. if (p == NULL)
  394. return 0;
  395. left = TLS_BUFFER_get_left(rbuf);
  396. if (left < SSL3_RT_HEADER_LENGTH)
  397. return 0;
  398. p += TLS_BUFFER_get_offset(rbuf);
  399. /*
  400. * We only check the type and record length, we will sanity check version
  401. * etc later
  402. */
  403. if (*p != SSL3_RT_APPLICATION_DATA)
  404. return 0;
  405. p += 3;
  406. n2s(p, len);
  407. if (left < SSL3_RT_HEADER_LENGTH + len)
  408. return 0;
  409. return 1;
  410. }
  411. static int rlayer_early_data_count_ok(OSSL_RECORD_LAYER *rl, size_t length,
  412. size_t overhead, int send)
  413. {
  414. uint32_t max_early_data = rl->max_early_data;
  415. if (max_early_data == 0) {
  416. RLAYERfatal(rl, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
  417. SSL_R_TOO_MUCH_EARLY_DATA);
  418. return 0;
  419. }
  420. /* If we are dealing with ciphertext we need to allow for the overhead */
  421. max_early_data += overhead;
  422. if (rl->early_data_count + length > max_early_data) {
  423. RLAYERfatal(rl, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
  424. SSL_R_TOO_MUCH_EARLY_DATA);
  425. return 0;
  426. }
  427. rl->early_data_count += length;
  428. return 1;
  429. }
  430. /*
  431. * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
  432. * will be processed per call to tls_get_more_records. Without this limit an
  433. * attacker could send empty records at a faster rate than we can process and
  434. * cause tls_get_more_records to loop forever.
  435. */
  436. #define MAX_EMPTY_RECORDS 32
  437. #define SSL2_RT_HEADER_LENGTH 2
  438. /*-
  439. * Call this to buffer new input records in rl->rrec.
  440. * It will return a OSSL_RECORD_RETURN_* value.
  441. * When it finishes successfully (OSSL_RECORD_RETURN_SUCCESS), |rl->num_recs|
  442. * records have been decoded. For each record 'i':
  443. * rrec[i].type - is the type of record
  444. * rrec[i].data, - data
  445. * rrec[i].length, - number of bytes
  446. * Multiple records will only be returned if the record types are all
  447. * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
  448. * |max_pipelines|
  449. */
  450. int tls_get_more_records(OSSL_RECORD_LAYER *rl)
  451. {
  452. int enc_err, rret;
  453. int i;
  454. size_t more, n;
  455. TLS_RL_RECORD *rr, *thisrr;
  456. TLS_BUFFER *rbuf;
  457. unsigned char *p;
  458. unsigned char md[EVP_MAX_MD_SIZE];
  459. unsigned int version;
  460. size_t mac_size = 0;
  461. int imac_size;
  462. size_t num_recs = 0, max_recs, j;
  463. PACKET pkt, sslv2pkt;
  464. SSL_MAC_BUF *macbufs = NULL;
  465. int ret = OSSL_RECORD_RETURN_FATAL;
  466. rr = rl->rrec;
  467. rbuf = &rl->rbuf;
  468. if (rbuf->buf == NULL) {
  469. if (!tls_setup_read_buffer(rl)) {
  470. /* RLAYERfatal() already called */
  471. return OSSL_RECORD_RETURN_FATAL;
  472. }
  473. }
  474. max_recs = rl->max_pipelines;
  475. if (max_recs == 0)
  476. max_recs = 1;
  477. do {
  478. thisrr = &rr[num_recs];
  479. /* check if we have the header */
  480. if ((rl->rstate != SSL_ST_READ_BODY) ||
  481. (rl->packet_length < SSL3_RT_HEADER_LENGTH)) {
  482. size_t sslv2len;
  483. unsigned int type;
  484. rret = rl->funcs->read_n(rl, SSL3_RT_HEADER_LENGTH,
  485. TLS_BUFFER_get_len(rbuf), 0,
  486. num_recs == 0 ? 1 : 0, &n);
  487. if (rret < OSSL_RECORD_RETURN_SUCCESS)
  488. return rret; /* error or non-blocking */
  489. rl->rstate = SSL_ST_READ_BODY;
  490. p = rl->packet;
  491. if (!PACKET_buf_init(&pkt, p, rl->packet_length)) {
  492. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  493. return OSSL_RECORD_RETURN_FATAL;
  494. }
  495. sslv2pkt = pkt;
  496. if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
  497. || !PACKET_get_1(&sslv2pkt, &type)) {
  498. RLAYERfatal(rl, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
  499. return OSSL_RECORD_RETURN_FATAL;
  500. }
  501. /*
  502. * The first record received by the server may be a V2ClientHello.
  503. */
  504. if (rl->role == OSSL_RECORD_ROLE_SERVER
  505. && rl->is_first_record
  506. && (sslv2len & 0x8000) != 0
  507. && (type == SSL2_MT_CLIENT_HELLO)) {
  508. /*
  509. * SSLv2 style record
  510. *
  511. * |num_recs| here will actually always be 0 because
  512. * |num_recs > 0| only ever occurs when we are processing
  513. * multiple app data records - which we know isn't the case here
  514. * because it is an SSLv2ClientHello. We keep it using
  515. * |num_recs| for the sake of consistency
  516. */
  517. thisrr->type = SSL3_RT_HANDSHAKE;
  518. thisrr->rec_version = SSL2_VERSION;
  519. thisrr->length = sslv2len & 0x7fff;
  520. if (thisrr->length > TLS_BUFFER_get_len(rbuf)
  521. - SSL2_RT_HEADER_LENGTH) {
  522. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
  523. SSL_R_PACKET_LENGTH_TOO_LONG);
  524. return OSSL_RECORD_RETURN_FATAL;
  525. }
  526. } else {
  527. /* SSLv3+ style record */
  528. /* Pull apart the header into the TLS_RL_RECORD */
  529. if (!PACKET_get_1(&pkt, &type)
  530. || !PACKET_get_net_2(&pkt, &version)
  531. || !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
  532. if (rl->msg_callback != NULL)
  533. rl->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, rl->cbarg);
  534. RLAYERfatal(rl, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
  535. return OSSL_RECORD_RETURN_FATAL;
  536. }
  537. thisrr->type = type;
  538. thisrr->rec_version = version;
  539. /*
  540. * When we call validate_record_header() only records actually
  541. * received in SSLv2 format should have the record version set
  542. * to SSL2_VERSION. This way validate_record_header() can know
  543. * what format the record was in based on the version.
  544. */
  545. if (thisrr->rec_version == SSL2_VERSION) {
  546. RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
  547. SSL_R_WRONG_VERSION_NUMBER);
  548. return OSSL_RECORD_RETURN_FATAL;
  549. }
  550. if (rl->msg_callback != NULL)
  551. rl->msg_callback(0, version, SSL3_RT_HEADER, p, 5, rl->cbarg);
  552. if (thisrr->length >
  553. TLS_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
  554. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
  555. SSL_R_PACKET_LENGTH_TOO_LONG);
  556. return OSSL_RECORD_RETURN_FATAL;
  557. }
  558. }
  559. if (!rl->funcs->validate_record_header(rl, thisrr)) {
  560. /* RLAYERfatal already called */
  561. return OSSL_RECORD_RETURN_FATAL;
  562. }
  563. /* now rl->rstate == SSL_ST_READ_BODY */
  564. }
  565. /*
  566. * rl->rstate == SSL_ST_READ_BODY, get and decode the data. Calculate
  567. * how much more data we need to read for the rest of the record
  568. */
  569. if (thisrr->rec_version == SSL2_VERSION) {
  570. more = thisrr->length + SSL2_RT_HEADER_LENGTH
  571. - SSL3_RT_HEADER_LENGTH;
  572. } else {
  573. more = thisrr->length;
  574. }
  575. if (more > 0) {
  576. /* now rl->packet_length == SSL3_RT_HEADER_LENGTH */
  577. rret = rl->funcs->read_n(rl, more, more, 1, 0, &n);
  578. if (rret < OSSL_RECORD_RETURN_SUCCESS)
  579. return rret; /* error or non-blocking io */
  580. }
  581. /* set state for later operations */
  582. rl->rstate = SSL_ST_READ_HEADER;
  583. /*
  584. * At this point, rl->packet_length == SSL3_RT_HEADER_LENGTH
  585. * + thisrr->length, or rl->packet_length == SSL2_RT_HEADER_LENGTH
  586. * + thisrr->length and we have that many bytes in rl->packet
  587. */
  588. if (thisrr->rec_version == SSL2_VERSION)
  589. thisrr->input = &(rl->packet[SSL2_RT_HEADER_LENGTH]);
  590. else
  591. thisrr->input = &(rl->packet[SSL3_RT_HEADER_LENGTH]);
  592. /*
  593. * ok, we can now read from 'rl->packet' data into 'thisrr'.
  594. * thisrr->input points at thisrr->length bytes, which need to be copied
  595. * into thisrr->data by either the decryption or by the decompression.
  596. * When the data is 'copied' into the thisrr->data buffer,
  597. * thisrr->input will be updated to point at the new buffer
  598. */
  599. /*
  600. * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
  601. * thisrr->length bytes of encrypted compressed stuff.
  602. */
  603. /* decrypt in place in 'thisrr->input' */
  604. thisrr->data = thisrr->input;
  605. thisrr->orig_len = thisrr->length;
  606. num_recs++;
  607. /* we have pulled in a full packet so zero things */
  608. rl->packet_length = 0;
  609. rl->is_first_record = 0;
  610. } while (num_recs < max_recs
  611. && thisrr->type == SSL3_RT_APPLICATION_DATA
  612. && RLAYER_USE_EXPLICIT_IV(rl)
  613. && rl->enc_ctx != NULL
  614. && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(rl->enc_ctx))
  615. & EVP_CIPH_FLAG_PIPELINE) != 0
  616. && tls_record_app_data_waiting(rl));
  617. if (num_recs == 1
  618. && thisrr->type == SSL3_RT_CHANGE_CIPHER_SPEC
  619. /* The following can happen in tlsany_meth after HRR */
  620. && rl->version == TLS1_3_VERSION
  621. && rl->is_first_handshake) {
  622. /*
  623. * CCS messages must be exactly 1 byte long, containing the value 0x01
  624. */
  625. if (thisrr->length != 1 || thisrr->data[0] != 0x01) {
  626. RLAYERfatal(rl, SSL_AD_ILLEGAL_PARAMETER,
  627. SSL_R_INVALID_CCS_MESSAGE);
  628. return OSSL_RECORD_RETURN_FATAL;
  629. }
  630. /*
  631. * CCS messages are ignored in TLSv1.3. We treat it like an empty
  632. * handshake record
  633. */
  634. thisrr->type = SSL3_RT_HANDSHAKE;
  635. if (++(rl->empty_record_count) > MAX_EMPTY_RECORDS) {
  636. RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE,
  637. SSL_R_UNEXPECTED_CCS_MESSAGE);
  638. return OSSL_RECORD_RETURN_FATAL;
  639. }
  640. rl->num_recs = 0;
  641. rl->curr_rec = 0;
  642. rl->num_released = 0;
  643. return OSSL_RECORD_RETURN_SUCCESS;
  644. }
  645. if (rl->md_ctx != NULL) {
  646. const EVP_MD *tmpmd = EVP_MD_CTX_get0_md(rl->md_ctx);
  647. if (tmpmd != NULL) {
  648. imac_size = EVP_MD_get_size(tmpmd);
  649. if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
  650. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  651. return OSSL_RECORD_RETURN_FATAL;
  652. }
  653. mac_size = (size_t)imac_size;
  654. }
  655. }
  656. /*
  657. * If in encrypt-then-mac mode calculate mac from encrypted record. All
  658. * the details below are public so no timing details can leak.
  659. */
  660. if (rl->use_etm && rl->md_ctx != NULL) {
  661. unsigned char *mac;
  662. for (j = 0; j < num_recs; j++) {
  663. thisrr = &rr[j];
  664. if (thisrr->length < mac_size) {
  665. RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
  666. return OSSL_RECORD_RETURN_FATAL;
  667. }
  668. thisrr->length -= mac_size;
  669. mac = thisrr->data + thisrr->length;
  670. i = rl->funcs->mac(rl, thisrr, md, 0 /* not send */);
  671. if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
  672. RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
  673. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  674. return OSSL_RECORD_RETURN_FATAL;
  675. }
  676. }
  677. /*
  678. * We've handled the mac now - there is no MAC inside the encrypted
  679. * record
  680. */
  681. mac_size = 0;
  682. }
  683. if (mac_size > 0) {
  684. macbufs = OPENSSL_zalloc(sizeof(*macbufs) * num_recs);
  685. if (macbufs == NULL) {
  686. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  687. return OSSL_RECORD_RETURN_FATAL;
  688. }
  689. }
  690. ERR_set_mark();
  691. enc_err = rl->funcs->cipher(rl, rr, num_recs, 0, macbufs, mac_size);
  692. /*-
  693. * enc_err is:
  694. * 0: if the record is publicly invalid, or an internal error, or AEAD
  695. * decryption failed, or ETM decryption failed.
  696. * 1: Success or MTE decryption failed (MAC will be randomised)
  697. */
  698. if (enc_err == 0) {
  699. if (rl->alert != SSL_AD_NO_ALERT) {
  700. /* RLAYERfatal() already got called */
  701. ERR_clear_last_mark();
  702. goto end;
  703. }
  704. if (num_recs == 1
  705. && rl->skip_early_data != NULL
  706. && rl->skip_early_data(rl->cbarg)) {
  707. /*
  708. * Valid early_data that we cannot decrypt will fail here. We treat
  709. * it like an empty record.
  710. */
  711. /*
  712. * Remove any errors from the stack. Decryption failures are normal
  713. * behaviour.
  714. */
  715. ERR_pop_to_mark();
  716. thisrr = &rr[0];
  717. if (!rlayer_early_data_count_ok(rl, thisrr->length,
  718. EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
  719. /* RLAYERfatal() already called */
  720. goto end;
  721. }
  722. thisrr->length = 0;
  723. rl->num_recs = 0;
  724. rl->curr_rec = 0;
  725. rl->num_released = 0;
  726. /* Reset the read sequence */
  727. memset(rl->sequence, 0, sizeof(rl->sequence));
  728. ret = 1;
  729. goto end;
  730. }
  731. ERR_clear_last_mark();
  732. RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
  733. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  734. goto end;
  735. } else {
  736. ERR_clear_last_mark();
  737. }
  738. OSSL_TRACE_BEGIN(TLS) {
  739. BIO_printf(trc_out, "dec %lu\n", (unsigned long)rr[0].length);
  740. BIO_dump_indent(trc_out, rr[0].data, rr[0].length, 4);
  741. } OSSL_TRACE_END(TLS);
  742. /* r->length is now the compressed data plus mac */
  743. if (rl->enc_ctx != NULL
  744. && !rl->use_etm
  745. && EVP_MD_CTX_get0_md(rl->md_ctx) != NULL) {
  746. for (j = 0; j < num_recs; j++) {
  747. SSL_MAC_BUF *thismb = &macbufs[j];
  748. thisrr = &rr[j];
  749. i = rl->funcs->mac(rl, thisrr, md, 0 /* not send */);
  750. if (i == 0 || thismb == NULL || thismb->mac == NULL
  751. || CRYPTO_memcmp(md, thismb->mac, (size_t)mac_size) != 0)
  752. enc_err = 0;
  753. if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
  754. enc_err = 0;
  755. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  756. if (enc_err == 0 && mac_size > 0 && thismb != NULL &&
  757. thismb->mac != NULL && (md[0] ^ thismb->mac[0]) != 0xFF) {
  758. enc_err = 1;
  759. }
  760. #endif
  761. }
  762. }
  763. if (enc_err == 0) {
  764. if (rl->alert != SSL_AD_NO_ALERT) {
  765. /* We already called RLAYERfatal() */
  766. goto end;
  767. }
  768. /*
  769. * A separate 'decryption_failed' alert was introduced with TLS 1.0,
  770. * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
  771. * failure is directly visible from the ciphertext anyway, we should
  772. * not reveal which kind of error occurred -- this might become
  773. * visible to an attacker (e.g. via a logfile)
  774. */
  775. RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
  776. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  777. goto end;
  778. }
  779. for (j = 0; j < num_recs; j++) {
  780. thisrr = &rr[j];
  781. if (!rl->funcs->post_process_record(rl, thisrr)) {
  782. /* RLAYERfatal already called */
  783. goto end;
  784. }
  785. /*
  786. * Check if the received packet overflows the current
  787. * Max Fragment Length setting.
  788. * Note: rl->max_frag_len > 0 and KTLS are mutually exclusive.
  789. */
  790. if (thisrr->length > rl->max_frag_len) {
  791. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
  792. goto end;
  793. }
  794. thisrr->off = 0;
  795. /*-
  796. * So at this point the following is true
  797. * thisrr->type is the type of record
  798. * thisrr->length == number of bytes in record
  799. * thisrr->off == offset to first valid byte
  800. * thisrr->data == where to take bytes from, increment after use :-).
  801. */
  802. /* just read a 0 length packet */
  803. if (thisrr->length == 0) {
  804. if (++(rl->empty_record_count) > MAX_EMPTY_RECORDS) {
  805. RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE,
  806. SSL_R_RECORD_TOO_SMALL);
  807. goto end;
  808. }
  809. } else {
  810. rl->empty_record_count = 0;
  811. }
  812. }
  813. if (rl->level == OSSL_RECORD_PROTECTION_LEVEL_EARLY) {
  814. thisrr = &rr[0];
  815. if (thisrr->type == SSL3_RT_APPLICATION_DATA
  816. && !rlayer_early_data_count_ok(rl, thisrr->length, 0, 0)) {
  817. /* RLAYERfatal already called */
  818. goto end;
  819. }
  820. }
  821. rl->num_recs = num_recs;
  822. rl->curr_rec = 0;
  823. rl->num_released = 0;
  824. ret = OSSL_RECORD_RETURN_SUCCESS;
  825. end:
  826. if (macbufs != NULL) {
  827. for (j = 0; j < num_recs; j++) {
  828. if (macbufs[j].alloced)
  829. OPENSSL_free(macbufs[j].mac);
  830. }
  831. OPENSSL_free(macbufs);
  832. }
  833. return ret;
  834. }
  835. /* Shared by ssl3_meth and tls1_meth */
  836. int tls_default_validate_record_header(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
  837. {
  838. size_t len = SSL3_RT_MAX_ENCRYPTED_LENGTH;
  839. if (rec->rec_version != rl->version) {
  840. RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION, SSL_R_WRONG_VERSION_NUMBER);
  841. return 0;
  842. }
  843. #ifndef OPENSSL_NO_COMP
  844. /*
  845. * If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
  846. * does not include the compression overhead anyway.
  847. */
  848. if (rl->compctx == NULL)
  849. len -= SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  850. #endif
  851. if (rec->length > len) {
  852. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
  853. SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  854. return 0;
  855. }
  856. return 1;
  857. }
  858. int tls_do_compress(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *wr)
  859. {
  860. #ifndef OPENSSL_NO_COMP
  861. int i;
  862. i = COMP_compress_block(rl->compctx, wr->data,
  863. (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
  864. wr->input, (int)wr->length);
  865. if (i < 0)
  866. return 0;
  867. wr->length = i;
  868. wr->input = wr->data;
  869. return 1;
  870. #else
  871. return 0;
  872. #endif
  873. }
  874. int tls_do_uncompress(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
  875. {
  876. #ifndef OPENSSL_NO_COMP
  877. int i;
  878. if (rec->comp == NULL) {
  879. rec->comp = (unsigned char *)
  880. OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
  881. }
  882. if (rec->comp == NULL)
  883. return 0;
  884. i = COMP_expand_block(rl->compctx, rec->comp, SSL3_RT_MAX_PLAIN_LENGTH,
  885. rec->data, (int)rec->length);
  886. if (i < 0)
  887. return 0;
  888. else
  889. rec->length = i;
  890. rec->data = rec->comp;
  891. return 1;
  892. #else
  893. return 0;
  894. #endif
  895. }
  896. /* Shared by tlsany_meth, ssl3_meth and tls1_meth */
  897. int tls_default_post_process_record(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
  898. {
  899. if (rl->compctx != NULL) {
  900. if (rec->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
  901. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
  902. SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  903. return 0;
  904. }
  905. if (!tls_do_uncompress(rl, rec)) {
  906. RLAYERfatal(rl, SSL_AD_DECOMPRESSION_FAILURE,
  907. SSL_R_BAD_DECOMPRESSION);
  908. return 0;
  909. }
  910. }
  911. if (rec->length > SSL3_RT_MAX_PLAIN_LENGTH) {
  912. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
  913. return 0;
  914. }
  915. return 1;
  916. }
  917. /* Shared by tls13_meth and ktls_meth */
  918. int tls13_common_post_process_record(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
  919. {
  920. if (rec->type != SSL3_RT_APPLICATION_DATA
  921. && rec->type != SSL3_RT_ALERT
  922. && rec->type != SSL3_RT_HANDSHAKE) {
  923. RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
  924. return 0;
  925. }
  926. if (rl->msg_callback != NULL)
  927. rl->msg_callback(0, rl->version, SSL3_RT_INNER_CONTENT_TYPE, &rec->type,
  928. 1, rl->cbarg);
  929. /*
  930. * TLSv1.3 alert and handshake records are required to be non-zero in
  931. * length.
  932. */
  933. if ((rec->type == SSL3_RT_HANDSHAKE || rec->type == SSL3_RT_ALERT)
  934. && rec->length == 0) {
  935. RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_LENGTH);
  936. return 0;
  937. }
  938. return 1;
  939. }
  940. int tls_read_record(OSSL_RECORD_LAYER *rl, void **rechandle, int *rversion,
  941. uint8_t *type, const unsigned char **data, size_t *datalen,
  942. uint16_t *epoch, unsigned char *seq_num)
  943. {
  944. TLS_RL_RECORD *rec;
  945. /*
  946. * tls_get_more_records() can return success without actually reading
  947. * anything useful (i.e. if empty records are read). We loop here until
  948. * we have something useful. tls_get_more_records() will eventually fail if
  949. * too many sequential empty records are read.
  950. */
  951. while (rl->curr_rec >= rl->num_recs) {
  952. int ret;
  953. if (rl->num_released != rl->num_recs) {
  954. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_RECORDS_NOT_RELEASED);
  955. return OSSL_RECORD_RETURN_FATAL;
  956. }
  957. ret = rl->funcs->get_more_records(rl);
  958. if (ret != OSSL_RECORD_RETURN_SUCCESS)
  959. return ret;
  960. }
  961. /*
  962. * We have now got rl->num_recs records buffered in rl->rrec. rl->curr_rec
  963. * points to the next one to read.
  964. */
  965. rec = &rl->rrec[rl->curr_rec++];
  966. *rechandle = rec;
  967. *rversion = rec->rec_version;
  968. *type = rec->type;
  969. *data = rec->data + rec->off;
  970. *datalen = rec->length;
  971. if (rl->isdtls) {
  972. *epoch = rec->epoch;
  973. memcpy(seq_num, rec->seq_num, sizeof(rec->seq_num));
  974. }
  975. return OSSL_RECORD_RETURN_SUCCESS;
  976. }
  977. int tls_release_record(OSSL_RECORD_LAYER *rl, void *rechandle, size_t length)
  978. {
  979. TLS_RL_RECORD *rec = &rl->rrec[rl->num_released];
  980. if (!ossl_assert(rl->num_released < rl->curr_rec)
  981. || !ossl_assert(rechandle == rec)) {
  982. /* Should not happen */
  983. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_INVALID_RECORD);
  984. return OSSL_RECORD_RETURN_FATAL;
  985. }
  986. if (rec->length < length) {
  987. /* Should not happen */
  988. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  989. return OSSL_RECORD_RETURN_FATAL;
  990. }
  991. if ((rl->options & SSL_OP_CLEANSE_PLAINTEXT) != 0)
  992. OPENSSL_cleanse(rec->data + rec->off, length);
  993. rec->off += length;
  994. rec->length -= length;
  995. if (rec->length > 0)
  996. return OSSL_RECORD_RETURN_SUCCESS;
  997. rl->num_released++;
  998. if (rl->curr_rec == rl->num_released
  999. && (rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0
  1000. && TLS_BUFFER_get_left(&rl->rbuf) == 0)
  1001. tls_release_read_buffer(rl);
  1002. return OSSL_RECORD_RETURN_SUCCESS;
  1003. }
  1004. int tls_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options)
  1005. {
  1006. const OSSL_PARAM *p;
  1007. p = OSSL_PARAM_locate_const(options, OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS);
  1008. if (p != NULL && !OSSL_PARAM_get_uint64(p, &rl->options)) {
  1009. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1010. return 0;
  1011. }
  1012. p = OSSL_PARAM_locate_const(options, OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE);
  1013. if (p != NULL && !OSSL_PARAM_get_uint32(p, &rl->mode)) {
  1014. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1015. return 0;
  1016. }
  1017. if (rl->direction == OSSL_RECORD_DIRECTION_READ) {
  1018. p = OSSL_PARAM_locate_const(options,
  1019. OSSL_LIBSSL_RECORD_LAYER_READ_BUFFER_LEN);
  1020. if (p != NULL && !OSSL_PARAM_get_size_t(p, &rl->rbuf.default_len)) {
  1021. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1022. return 0;
  1023. }
  1024. } else {
  1025. p = OSSL_PARAM_locate_const(options,
  1026. OSSL_LIBSSL_RECORD_LAYER_PARAM_BLOCK_PADDING);
  1027. if (p != NULL && !OSSL_PARAM_get_size_t(p, &rl->block_padding)) {
  1028. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1029. return 0;
  1030. }
  1031. }
  1032. if (rl->level == OSSL_RECORD_PROTECTION_LEVEL_APPLICATION) {
  1033. /*
  1034. * We ignore any read_ahead setting prior to the application protection
  1035. * level. Otherwise we may read ahead data in a lower protection level
  1036. * that is destined for a higher protection level. To simplify the logic
  1037. * we don't support that at this stage.
  1038. */
  1039. p = OSSL_PARAM_locate_const(options,
  1040. OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD);
  1041. if (p != NULL && !OSSL_PARAM_get_int(p, &rl->read_ahead)) {
  1042. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1043. return 0;
  1044. }
  1045. }
  1046. return 1;
  1047. }
  1048. int
  1049. tls_int_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
  1050. int role, int direction, int level, unsigned char *key,
  1051. size_t keylen, unsigned char *iv, size_t ivlen,
  1052. unsigned char *mackey, size_t mackeylen,
  1053. const EVP_CIPHER *ciph, size_t taglen,
  1054. int mactype,
  1055. const EVP_MD *md, COMP_METHOD *comp, BIO *prev,
  1056. BIO *transport, BIO *next, BIO_ADDR *local,
  1057. BIO_ADDR *peer, const OSSL_PARAM *settings,
  1058. const OSSL_PARAM *options,
  1059. const OSSL_DISPATCH *fns, void *cbarg,
  1060. OSSL_RECORD_LAYER **retrl)
  1061. {
  1062. OSSL_RECORD_LAYER *rl = OPENSSL_zalloc(sizeof(*rl));
  1063. const OSSL_PARAM *p;
  1064. *retrl = NULL;
  1065. if (rl == NULL)
  1066. return OSSL_RECORD_RETURN_FATAL;
  1067. /*
  1068. * Default the value for max_frag_len. This may be overridden by the
  1069. * settings
  1070. */
  1071. rl->max_frag_len = SSL3_RT_MAX_PLAIN_LENGTH;
  1072. /* Loop through all the settings since they must all be understood */
  1073. if (settings != NULL) {
  1074. for (p = settings; p->key != NULL; p++) {
  1075. if (strcmp(p->key, OSSL_LIBSSL_RECORD_LAYER_PARAM_USE_ETM) == 0) {
  1076. if (!OSSL_PARAM_get_int(p, &rl->use_etm)) {
  1077. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1078. goto err;
  1079. }
  1080. } else if (strcmp(p->key,
  1081. OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN) == 0) {
  1082. if (!OSSL_PARAM_get_uint(p, &rl->max_frag_len)) {
  1083. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1084. goto err;
  1085. }
  1086. } else if (strcmp(p->key,
  1087. OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA) == 0) {
  1088. if (!OSSL_PARAM_get_uint32(p, &rl->max_early_data)) {
  1089. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1090. goto err;
  1091. }
  1092. } else if (strcmp(p->key,
  1093. OSSL_LIBSSL_RECORD_LAYER_PARAM_STREAM_MAC) == 0) {
  1094. if (!OSSL_PARAM_get_int(p, &rl->stream_mac)) {
  1095. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1096. goto err;
  1097. }
  1098. } else if (strcmp(p->key,
  1099. OSSL_LIBSSL_RECORD_LAYER_PARAM_TLSTREE) == 0) {
  1100. if (!OSSL_PARAM_get_int(p, &rl->tlstree)) {
  1101. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1102. goto err;
  1103. }
  1104. } else {
  1105. ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_MANDATORY_PARAMETER);
  1106. goto err;
  1107. }
  1108. }
  1109. }
  1110. rl->libctx = libctx;
  1111. rl->propq = propq;
  1112. rl->version = vers;
  1113. rl->role = role;
  1114. rl->direction = direction;
  1115. rl->level = level;
  1116. rl->taglen = taglen;
  1117. rl->md = md;
  1118. rl->alert = SSL_AD_NO_ALERT;
  1119. rl->rstate = SSL_ST_READ_HEADER;
  1120. if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
  1121. rl->is_first_record = 1;
  1122. if (!tls_set1_bio(rl, transport))
  1123. goto err;
  1124. if (prev != NULL && !BIO_up_ref(prev))
  1125. goto err;
  1126. rl->prev = prev;
  1127. if (next != NULL && !BIO_up_ref(next))
  1128. goto err;
  1129. rl->next = next;
  1130. rl->cbarg = cbarg;
  1131. if (fns != NULL) {
  1132. for (; fns->function_id != 0; fns++) {
  1133. switch (fns->function_id) {
  1134. case OSSL_FUNC_RLAYER_SKIP_EARLY_DATA:
  1135. rl->skip_early_data = OSSL_FUNC_rlayer_skip_early_data(fns);
  1136. break;
  1137. case OSSL_FUNC_RLAYER_MSG_CALLBACK:
  1138. rl->msg_callback = OSSL_FUNC_rlayer_msg_callback(fns);
  1139. break;
  1140. case OSSL_FUNC_RLAYER_SECURITY:
  1141. rl->security = OSSL_FUNC_rlayer_security(fns);
  1142. break;
  1143. case OSSL_FUNC_RLAYER_PADDING:
  1144. rl->padding = OSSL_FUNC_rlayer_padding(fns);
  1145. default:
  1146. /* Just ignore anything we don't understand */
  1147. break;
  1148. }
  1149. }
  1150. }
  1151. if (!tls_set_options(rl, options)) {
  1152. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1153. goto err;
  1154. }
  1155. if ((rl->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) == 0
  1156. && rl->version <= TLS1_VERSION
  1157. && !EVP_CIPHER_is_a(ciph, "NULL")
  1158. && !EVP_CIPHER_is_a(ciph, "RC4")) {
  1159. /*
  1160. * Enable vulnerability countermeasure for CBC ciphers with known-IV
  1161. * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
  1162. */
  1163. rl->need_empty_fragments = 1;
  1164. }
  1165. *retrl = rl;
  1166. return OSSL_RECORD_RETURN_SUCCESS;
  1167. err:
  1168. tls_int_free(rl);
  1169. return OSSL_RECORD_RETURN_FATAL;
  1170. }
  1171. static int
  1172. tls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
  1173. int role, int direction, int level, uint16_t epoch,
  1174. unsigned char *secret, size_t secretlen,
  1175. unsigned char *key, size_t keylen, unsigned char *iv,
  1176. size_t ivlen, unsigned char *mackey, size_t mackeylen,
  1177. const EVP_CIPHER *ciph, size_t taglen,
  1178. int mactype,
  1179. const EVP_MD *md, COMP_METHOD *comp,
  1180. const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
  1181. BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
  1182. const OSSL_PARAM *settings, const OSSL_PARAM *options,
  1183. const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
  1184. OSSL_RECORD_LAYER **retrl)
  1185. {
  1186. int ret;
  1187. ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
  1188. key, keylen, iv, ivlen, mackey, mackeylen,
  1189. ciph, taglen, mactype, md, comp, prev,
  1190. transport, next, local, peer, settings,
  1191. options, fns, cbarg, retrl);
  1192. if (ret != OSSL_RECORD_RETURN_SUCCESS)
  1193. return ret;
  1194. switch (vers) {
  1195. case TLS_ANY_VERSION:
  1196. (*retrl)->funcs = &tls_any_funcs;
  1197. break;
  1198. case TLS1_3_VERSION:
  1199. (*retrl)->funcs = &tls_1_3_funcs;
  1200. break;
  1201. case TLS1_2_VERSION:
  1202. case TLS1_1_VERSION:
  1203. case TLS1_VERSION:
  1204. (*retrl)->funcs = &tls_1_funcs;
  1205. break;
  1206. case SSL3_VERSION:
  1207. (*retrl)->funcs = &ssl_3_0_funcs;
  1208. break;
  1209. default:
  1210. /* Should not happen */
  1211. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  1212. ret = OSSL_RECORD_RETURN_FATAL;
  1213. goto err;
  1214. }
  1215. ret = (*retrl)->funcs->set_crypto_state(*retrl, level, key, keylen, iv,
  1216. ivlen, mackey, mackeylen, ciph,
  1217. taglen, mactype, md, comp);
  1218. err:
  1219. if (ret != OSSL_RECORD_RETURN_SUCCESS) {
  1220. tls_int_free(*retrl);
  1221. *retrl = NULL;
  1222. }
  1223. return ret;
  1224. }
  1225. static void tls_int_free(OSSL_RECORD_LAYER *rl)
  1226. {
  1227. BIO_free(rl->prev);
  1228. BIO_free(rl->bio);
  1229. BIO_free(rl->next);
  1230. ossl_tls_buffer_release(&rl->rbuf);
  1231. tls_release_write_buffer(rl);
  1232. EVP_CIPHER_CTX_free(rl->enc_ctx);
  1233. EVP_MD_CTX_free(rl->md_ctx);
  1234. #ifndef OPENSSL_NO_COMP
  1235. COMP_CTX_free(rl->compctx);
  1236. #endif
  1237. if (rl->version == SSL3_VERSION)
  1238. OPENSSL_cleanse(rl->mac_secret, sizeof(rl->mac_secret));
  1239. TLS_RL_RECORD_release(rl->rrec, SSL_MAX_PIPELINES);
  1240. OPENSSL_free(rl);
  1241. }
  1242. int tls_free(OSSL_RECORD_LAYER *rl)
  1243. {
  1244. TLS_BUFFER *rbuf;
  1245. size_t left, written;
  1246. int ret = 1;
  1247. if (rl == NULL)
  1248. return 1;
  1249. rbuf = &rl->rbuf;
  1250. left = TLS_BUFFER_get_left(rbuf);
  1251. if (left > 0) {
  1252. /*
  1253. * This record layer is closing but we still have data left in our
  1254. * buffer. It must be destined for the next epoch - so push it there.
  1255. */
  1256. ret = BIO_write_ex(rl->next, rbuf->buf + rbuf->offset, left, &written);
  1257. }
  1258. tls_int_free(rl);
  1259. return ret;
  1260. }
  1261. int tls_unprocessed_read_pending(OSSL_RECORD_LAYER *rl)
  1262. {
  1263. return TLS_BUFFER_get_left(&rl->rbuf) != 0;
  1264. }
  1265. int tls_processed_read_pending(OSSL_RECORD_LAYER *rl)
  1266. {
  1267. return rl->curr_rec < rl->num_recs;
  1268. }
  1269. size_t tls_app_data_pending(OSSL_RECORD_LAYER *rl)
  1270. {
  1271. size_t i;
  1272. size_t num = 0;
  1273. for (i = rl->curr_rec; i < rl->num_recs; i++) {
  1274. if (rl->rrec[i].type != SSL3_RT_APPLICATION_DATA)
  1275. return num;
  1276. num += rl->rrec[i].length;
  1277. }
  1278. return num;
  1279. }
  1280. size_t tls_get_max_records_default(OSSL_RECORD_LAYER *rl, uint8_t type,
  1281. size_t len,
  1282. size_t maxfrag, size_t *preffrag)
  1283. {
  1284. /*
  1285. * If we have a pipeline capable cipher, and we have been configured to use
  1286. * it, then return the preferred number of pipelines.
  1287. */
  1288. if (rl->max_pipelines > 0
  1289. && rl->enc_ctx != NULL
  1290. && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(rl->enc_ctx))
  1291. & EVP_CIPH_FLAG_PIPELINE) != 0
  1292. && RLAYER_USE_EXPLICIT_IV(rl)) {
  1293. size_t pipes;
  1294. if (len == 0)
  1295. return 1;
  1296. pipes = ((len - 1) / *preffrag) + 1;
  1297. return (pipes < rl->max_pipelines) ? pipes : rl->max_pipelines;
  1298. }
  1299. return 1;
  1300. }
  1301. size_t tls_get_max_records(OSSL_RECORD_LAYER *rl, uint8_t type, size_t len,
  1302. size_t maxfrag, size_t *preffrag)
  1303. {
  1304. return rl->funcs->get_max_records(rl, type, len, maxfrag, preffrag);
  1305. }
  1306. int tls_allocate_write_buffers_default(OSSL_RECORD_LAYER *rl,
  1307. OSSL_RECORD_TEMPLATE *templates,
  1308. size_t numtempl,
  1309. size_t *prefix)
  1310. {
  1311. if (!tls_setup_write_buffer(rl, numtempl, 0, 0)) {
  1312. /* RLAYERfatal() already called */
  1313. return 0;
  1314. }
  1315. return 1;
  1316. }
  1317. int tls_initialise_write_packets_default(OSSL_RECORD_LAYER *rl,
  1318. OSSL_RECORD_TEMPLATE *templates,
  1319. size_t numtempl,
  1320. OSSL_RECORD_TEMPLATE *prefixtempl,
  1321. WPACKET *pkt,
  1322. TLS_BUFFER *bufs,
  1323. size_t *wpinited)
  1324. {
  1325. WPACKET *thispkt;
  1326. size_t j, align;
  1327. TLS_BUFFER *wb;
  1328. for (j = 0; j < numtempl; j++) {
  1329. thispkt = &pkt[j];
  1330. wb = &bufs[j];
  1331. wb->type = templates[j].type;
  1332. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  1333. align = (size_t)TLS_BUFFER_get_buf(wb);
  1334. align += rl->isdtls ? DTLS1_RT_HEADER_LENGTH : SSL3_RT_HEADER_LENGTH;
  1335. align = SSL3_ALIGN_PAYLOAD - 1
  1336. - ((align - 1) % SSL3_ALIGN_PAYLOAD);
  1337. #endif
  1338. TLS_BUFFER_set_offset(wb, align);
  1339. if (!WPACKET_init_static_len(thispkt, TLS_BUFFER_get_buf(wb),
  1340. TLS_BUFFER_get_len(wb), 0)) {
  1341. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1342. return 0;
  1343. }
  1344. (*wpinited)++;
  1345. if (!WPACKET_allocate_bytes(thispkt, align, NULL)) {
  1346. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1347. return 0;
  1348. }
  1349. }
  1350. return 1;
  1351. }
  1352. int tls_prepare_record_header_default(OSSL_RECORD_LAYER *rl,
  1353. WPACKET *thispkt,
  1354. OSSL_RECORD_TEMPLATE *templ,
  1355. uint8_t rectype,
  1356. unsigned char **recdata)
  1357. {
  1358. size_t maxcomplen;
  1359. *recdata = NULL;
  1360. maxcomplen = templ->buflen;
  1361. if (rl->compctx != NULL)
  1362. maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  1363. if (!WPACKET_put_bytes_u8(thispkt, rectype)
  1364. || !WPACKET_put_bytes_u16(thispkt, templ->version)
  1365. || !WPACKET_start_sub_packet_u16(thispkt)
  1366. || (rl->eivlen > 0
  1367. && !WPACKET_allocate_bytes(thispkt, rl->eivlen, NULL))
  1368. || (maxcomplen > 0
  1369. && !WPACKET_reserve_bytes(thispkt, maxcomplen,
  1370. recdata))) {
  1371. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1372. return 0;
  1373. }
  1374. return 1;
  1375. }
  1376. int tls_prepare_for_encryption_default(OSSL_RECORD_LAYER *rl,
  1377. size_t mac_size,
  1378. WPACKET *thispkt,
  1379. TLS_RL_RECORD *thiswr)
  1380. {
  1381. size_t len;
  1382. unsigned char *recordstart;
  1383. /*
  1384. * we should still have the output to thiswr->data and the input from
  1385. * wr->input. Length should be thiswr->length. thiswr->data still points
  1386. * in the wb->buf
  1387. */
  1388. if (!rl->use_etm && mac_size != 0) {
  1389. unsigned char *mac;
  1390. if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
  1391. || !rl->funcs->mac(rl, thiswr, mac, 1)) {
  1392. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1393. return 0;
  1394. }
  1395. }
  1396. /*
  1397. * Reserve some bytes for any growth that may occur during encryption. If
  1398. * we are adding the MAC independently of the cipher algorithm, then the
  1399. * max encrypted overhead does not need to include an allocation for that
  1400. * MAC
  1401. */
  1402. if (!WPACKET_reserve_bytes(thispkt, SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
  1403. - mac_size, NULL)
  1404. /*
  1405. * We also need next the amount of bytes written to this
  1406. * sub-packet
  1407. */
  1408. || !WPACKET_get_length(thispkt, &len)) {
  1409. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1410. return 0;
  1411. }
  1412. /* Get a pointer to the start of this record excluding header */
  1413. recordstart = WPACKET_get_curr(thispkt) - len;
  1414. TLS_RL_RECORD_set_data(thiswr, recordstart);
  1415. TLS_RL_RECORD_reset_input(thiswr);
  1416. TLS_RL_RECORD_set_length(thiswr, len);
  1417. return 1;
  1418. }
  1419. int tls_post_encryption_processing_default(OSSL_RECORD_LAYER *rl,
  1420. size_t mac_size,
  1421. OSSL_RECORD_TEMPLATE *thistempl,
  1422. WPACKET *thispkt,
  1423. TLS_RL_RECORD *thiswr)
  1424. {
  1425. size_t origlen, len;
  1426. size_t headerlen = rl->isdtls ? DTLS1_RT_HEADER_LENGTH
  1427. : SSL3_RT_HEADER_LENGTH;
  1428. /* Allocate bytes for the encryption overhead */
  1429. if (!WPACKET_get_length(thispkt, &origlen)
  1430. /* Check we allowed enough room for the encryption growth */
  1431. || !ossl_assert(origlen + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
  1432. - mac_size >= thiswr->length)
  1433. /* Encryption should never shrink the data! */
  1434. || origlen > thiswr->length
  1435. || (thiswr->length > origlen
  1436. && !WPACKET_allocate_bytes(thispkt,
  1437. thiswr->length - origlen,
  1438. NULL))) {
  1439. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1440. return 0;
  1441. }
  1442. if (rl->use_etm && mac_size != 0) {
  1443. unsigned char *mac;
  1444. if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
  1445. || !rl->funcs->mac(rl, thiswr, mac, 1)) {
  1446. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1447. return 0;
  1448. }
  1449. TLS_RL_RECORD_add_length(thiswr, mac_size);
  1450. }
  1451. if (!WPACKET_get_length(thispkt, &len)
  1452. || !WPACKET_close(thispkt)) {
  1453. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1454. return 0;
  1455. }
  1456. if (rl->msg_callback != NULL) {
  1457. unsigned char *recordstart;
  1458. recordstart = WPACKET_get_curr(thispkt) - len - headerlen;
  1459. rl->msg_callback(1, thiswr->rec_version, SSL3_RT_HEADER, recordstart,
  1460. headerlen, rl->cbarg);
  1461. if (rl->version == TLS1_3_VERSION && rl->enc_ctx != NULL) {
  1462. unsigned char ctype = thistempl->type;
  1463. rl->msg_callback(1, thiswr->rec_version, SSL3_RT_INNER_CONTENT_TYPE,
  1464. &ctype, 1, rl->cbarg);
  1465. }
  1466. }
  1467. if (!WPACKET_finish(thispkt)) {
  1468. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1469. return 0;
  1470. }
  1471. TLS_RL_RECORD_add_length(thiswr, headerlen);
  1472. return 1;
  1473. }
  1474. int tls_write_records_default(OSSL_RECORD_LAYER *rl,
  1475. OSSL_RECORD_TEMPLATE *templates,
  1476. size_t numtempl)
  1477. {
  1478. WPACKET pkt[SSL_MAX_PIPELINES + 1];
  1479. TLS_RL_RECORD wr[SSL_MAX_PIPELINES + 1];
  1480. WPACKET *thispkt;
  1481. TLS_RL_RECORD *thiswr;
  1482. int mac_size = 0, ret = 0;
  1483. size_t wpinited = 0;
  1484. size_t j, prefix = 0;
  1485. OSSL_RECORD_TEMPLATE prefixtempl;
  1486. OSSL_RECORD_TEMPLATE *thistempl;
  1487. if (rl->md_ctx != NULL && EVP_MD_CTX_get0_md(rl->md_ctx) != NULL) {
  1488. mac_size = EVP_MD_CTX_get_size(rl->md_ctx);
  1489. if (mac_size < 0) {
  1490. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1491. goto err;
  1492. }
  1493. }
  1494. if (!rl->funcs->allocate_write_buffers(rl, templates, numtempl, &prefix)) {
  1495. /* RLAYERfatal() already called */
  1496. goto err;
  1497. }
  1498. if (!rl->funcs->initialise_write_packets(rl, templates, numtempl,
  1499. &prefixtempl, pkt, rl->wbuf,
  1500. &wpinited)) {
  1501. /* RLAYERfatal() already called */
  1502. goto err;
  1503. }
  1504. /* Clear our TLS_RL_RECORD structures */
  1505. memset(wr, 0, sizeof(wr));
  1506. for (j = 0; j < numtempl + prefix; j++) {
  1507. unsigned char *compressdata = NULL;
  1508. uint8_t rectype;
  1509. thispkt = &pkt[j];
  1510. thiswr = &wr[j];
  1511. thistempl = (j < prefix) ? &prefixtempl : &templates[j - prefix];
  1512. /*
  1513. * Default to the record type as specified in the template unless the
  1514. * protocol implementation says differently.
  1515. */
  1516. if (rl->funcs->get_record_type != NULL)
  1517. rectype = rl->funcs->get_record_type(rl, thistempl);
  1518. else
  1519. rectype = thistempl->type;
  1520. TLS_RL_RECORD_set_type(thiswr, rectype);
  1521. TLS_RL_RECORD_set_rec_version(thiswr, thistempl->version);
  1522. if (!rl->funcs->prepare_record_header(rl, thispkt, thistempl, rectype,
  1523. &compressdata)) {
  1524. /* RLAYERfatal() already called */
  1525. goto err;
  1526. }
  1527. /* lets setup the record stuff. */
  1528. TLS_RL_RECORD_set_data(thiswr, compressdata);
  1529. TLS_RL_RECORD_set_length(thiswr, thistempl->buflen);
  1530. TLS_RL_RECORD_set_input(thiswr, (unsigned char *)thistempl->buf);
  1531. /*
  1532. * we now 'read' from thiswr->input, thiswr->length bytes into
  1533. * thiswr->data
  1534. */
  1535. /* first we compress */
  1536. if (rl->compctx != NULL) {
  1537. if (!tls_do_compress(rl, thiswr)
  1538. || !WPACKET_allocate_bytes(thispkt, thiswr->length, NULL)) {
  1539. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE);
  1540. goto err;
  1541. }
  1542. } else if (compressdata != NULL) {
  1543. if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {
  1544. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1545. goto err;
  1546. }
  1547. TLS_RL_RECORD_reset_input(&wr[j]);
  1548. }
  1549. if (rl->funcs->add_record_padding != NULL
  1550. && !rl->funcs->add_record_padding(rl, thistempl, thispkt,
  1551. thiswr)) {
  1552. /* RLAYERfatal() already called */
  1553. goto err;
  1554. }
  1555. if (!rl->funcs->prepare_for_encryption(rl, mac_size, thispkt, thiswr)) {
  1556. /* RLAYERfatal() already called */
  1557. goto err;
  1558. }
  1559. }
  1560. if (prefix) {
  1561. if (rl->funcs->cipher(rl, wr, 1, 1, NULL, mac_size) < 1) {
  1562. if (rl->alert == SSL_AD_NO_ALERT) {
  1563. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1564. }
  1565. goto err;
  1566. }
  1567. }
  1568. if (rl->funcs->cipher(rl, wr + prefix, numtempl, 1, NULL, mac_size) < 1) {
  1569. if (rl->alert == SSL_AD_NO_ALERT) {
  1570. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1571. }
  1572. goto err;
  1573. }
  1574. for (j = 0; j < numtempl + prefix; j++) {
  1575. thispkt = &pkt[j];
  1576. thiswr = &wr[j];
  1577. thistempl = (j < prefix) ? &prefixtempl : &templates[j - prefix];
  1578. if (!rl->funcs->post_encryption_processing(rl, mac_size, thistempl,
  1579. thispkt, thiswr)) {
  1580. /* RLAYERfatal() already called */
  1581. goto err;
  1582. }
  1583. /* now let's set up wb */
  1584. TLS_BUFFER_set_left(&rl->wbuf[j], TLS_RL_RECORD_get_length(thiswr));
  1585. }
  1586. ret = 1;
  1587. err:
  1588. for (j = 0; j < wpinited; j++)
  1589. WPACKET_cleanup(&pkt[j]);
  1590. return ret;
  1591. }
  1592. int tls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
  1593. size_t numtempl)
  1594. {
  1595. /* Check we don't have pending data waiting to write */
  1596. if (!ossl_assert(rl->nextwbuf >= rl->numwpipes
  1597. || TLS_BUFFER_get_left(&rl->wbuf[rl->nextwbuf]) == 0)) {
  1598. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1599. return OSSL_RECORD_RETURN_FATAL;
  1600. }
  1601. if (!rl->funcs->write_records(rl, templates, numtempl)) {
  1602. /* RLAYERfatal already called */
  1603. return OSSL_RECORD_RETURN_FATAL;
  1604. }
  1605. rl->nextwbuf = 0;
  1606. /* we now just need to write the buffers */
  1607. return tls_retry_write_records(rl);
  1608. }
  1609. int tls_retry_write_records(OSSL_RECORD_LAYER *rl)
  1610. {
  1611. int i, ret;
  1612. TLS_BUFFER *thiswb;
  1613. size_t tmpwrit = 0;
  1614. if (rl->nextwbuf >= rl->numwpipes)
  1615. return OSSL_RECORD_RETURN_SUCCESS;
  1616. for (;;) {
  1617. thiswb = &rl->wbuf[rl->nextwbuf];
  1618. clear_sys_error();
  1619. if (rl->bio != NULL) {
  1620. if (rl->funcs->prepare_write_bio != NULL) {
  1621. ret = rl->funcs->prepare_write_bio(rl, thiswb->type);
  1622. if (ret != OSSL_RECORD_RETURN_SUCCESS)
  1623. return ret;
  1624. }
  1625. i = BIO_write(rl->bio, (char *)
  1626. &(TLS_BUFFER_get_buf(thiswb)
  1627. [TLS_BUFFER_get_offset(thiswb)]),
  1628. (unsigned int)TLS_BUFFER_get_left(thiswb));
  1629. if (i >= 0) {
  1630. tmpwrit = i;
  1631. if (i == 0 && BIO_should_retry(rl->bio))
  1632. ret = OSSL_RECORD_RETURN_RETRY;
  1633. else
  1634. ret = OSSL_RECORD_RETURN_SUCCESS;
  1635. } else {
  1636. if (BIO_should_retry(rl->bio))
  1637. ret = OSSL_RECORD_RETURN_RETRY;
  1638. else
  1639. ret = OSSL_RECORD_RETURN_FATAL;
  1640. }
  1641. } else {
  1642. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_BIO_NOT_SET);
  1643. ret = OSSL_RECORD_RETURN_FATAL;
  1644. i = -1;
  1645. }
  1646. /*
  1647. * When an empty fragment is sent on a connection using KTLS,
  1648. * it is sent as a write of zero bytes. If this zero byte
  1649. * write succeeds, i will be 0 rather than a non-zero value.
  1650. * Treat i == 0 as success rather than an error for zero byte
  1651. * writes to permit this case.
  1652. */
  1653. if (i >= 0 && tmpwrit == TLS_BUFFER_get_left(thiswb)) {
  1654. TLS_BUFFER_set_left(thiswb, 0);
  1655. TLS_BUFFER_add_offset(thiswb, tmpwrit);
  1656. if (++(rl->nextwbuf) < rl->numwpipes)
  1657. continue;
  1658. if (rl->nextwbuf == rl->numwpipes
  1659. && (rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0)
  1660. tls_release_write_buffer(rl);
  1661. return OSSL_RECORD_RETURN_SUCCESS;
  1662. } else if (i <= 0) {
  1663. if (rl->isdtls) {
  1664. /*
  1665. * For DTLS, just drop it. That's kind of the whole point in
  1666. * using a datagram service
  1667. */
  1668. TLS_BUFFER_set_left(thiswb, 0);
  1669. if (++(rl->nextwbuf) == rl->numwpipes
  1670. && (rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0)
  1671. tls_release_write_buffer(rl);
  1672. }
  1673. return ret;
  1674. }
  1675. TLS_BUFFER_add_offset(thiswb, tmpwrit);
  1676. TLS_BUFFER_sub_left(thiswb, tmpwrit);
  1677. }
  1678. }
  1679. int tls_get_alert_code(OSSL_RECORD_LAYER *rl)
  1680. {
  1681. return rl->alert;
  1682. }
  1683. int tls_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio)
  1684. {
  1685. if (bio != NULL && !BIO_up_ref(bio))
  1686. return 0;
  1687. BIO_free(rl->bio);
  1688. rl->bio = bio;
  1689. return 1;
  1690. }
  1691. /* Shared by most methods except tlsany_meth */
  1692. int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
  1693. {
  1694. if (rl->version != version)
  1695. return 0;
  1696. return 1;
  1697. }
  1698. int tls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
  1699. {
  1700. return rl->funcs->set_protocol_version(rl, version);
  1701. }
  1702. void tls_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow)
  1703. {
  1704. rl->allow_plain_alerts = allow;
  1705. }
  1706. void tls_set_first_handshake(OSSL_RECORD_LAYER *rl, int first)
  1707. {
  1708. rl->is_first_handshake = first;
  1709. }
  1710. void tls_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines)
  1711. {
  1712. rl->max_pipelines = max_pipelines;
  1713. if (max_pipelines > 1)
  1714. rl->read_ahead = 1;
  1715. }
  1716. void tls_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
  1717. const char **longstr)
  1718. {
  1719. const char *shrt, *lng;
  1720. switch (rl->rstate) {
  1721. case SSL_ST_READ_HEADER:
  1722. shrt = "RH";
  1723. lng = "read header";
  1724. break;
  1725. case SSL_ST_READ_BODY:
  1726. shrt = "RB";
  1727. lng = "read body";
  1728. break;
  1729. default:
  1730. shrt = lng = "unknown";
  1731. break;
  1732. }
  1733. if (shortstr != NULL)
  1734. *shortstr = shrt;
  1735. if (longstr != NULL)
  1736. *longstr = lng;
  1737. }
  1738. const COMP_METHOD *tls_get_compression(OSSL_RECORD_LAYER *rl)
  1739. {
  1740. #ifndef OPENSSL_NO_COMP
  1741. return (rl->compctx == NULL) ? NULL : COMP_CTX_get_method(rl->compctx);
  1742. #else
  1743. return NULL;
  1744. #endif
  1745. }
  1746. void tls_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len)
  1747. {
  1748. rl->max_frag_len = max_frag_len;
  1749. /*
  1750. * We don't need to adjust buffer sizes. Write buffer sizes are
  1751. * automatically checked anyway. We should only be changing the read buffer
  1752. * size during the handshake, so we will create a new buffer when we create
  1753. * the new record layer. We can't change the existing buffer because it may
  1754. * already have data in it.
  1755. */
  1756. }
  1757. int tls_increment_sequence_ctr(OSSL_RECORD_LAYER *rl)
  1758. {
  1759. int i;
  1760. /* Increment the sequence counter */
  1761. for (i = SEQ_NUM_SIZE; i > 0; i--) {
  1762. ++(rl->sequence[i - 1]);
  1763. if (rl->sequence[i - 1] != 0)
  1764. break;
  1765. }
  1766. if (i == 0) {
  1767. /* Sequence has wrapped */
  1768. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_SEQUENCE_CTR_WRAPPED);
  1769. return 0;
  1770. }
  1771. return 1;
  1772. }
  1773. int tls_alloc_buffers(OSSL_RECORD_LAYER *rl)
  1774. {
  1775. if (rl->direction == OSSL_RECORD_DIRECTION_WRITE) {
  1776. /* If we have a pending write then buffers are already allocated */
  1777. if (rl->nextwbuf < rl->numwpipes)
  1778. return 1;
  1779. /*
  1780. * We assume 1 pipe with default sized buffer. If what we need ends up
  1781. * being a different size to that then it will be reallocated on demand.
  1782. * If we need more than 1 pipe then that will also be allocated on
  1783. * demand
  1784. */
  1785. if (!tls_setup_write_buffer(rl, 1, 0, 0))
  1786. return 0;
  1787. /*
  1788. * Normally when we allocate write buffers we immediately write
  1789. * something into it. In this case we're not doing that so mark the
  1790. * buffer as empty.
  1791. */
  1792. TLS_BUFFER_set_left(&rl->wbuf[0], 0);
  1793. return 1;
  1794. }
  1795. /* Read direction */
  1796. /* If we have pending data to be read then buffers are already allocated */
  1797. if (rl->curr_rec < rl->num_recs || TLS_BUFFER_get_left(&rl->rbuf) != 0)
  1798. return 1;
  1799. return tls_setup_read_buffer(rl);
  1800. }
  1801. int tls_free_buffers(OSSL_RECORD_LAYER *rl)
  1802. {
  1803. if (rl->direction == OSSL_RECORD_DIRECTION_WRITE) {
  1804. if (rl->nextwbuf < rl->numwpipes) {
  1805. /*
  1806. * We may have pending data. If we've just got one empty buffer
  1807. * allocated then it has probably just been alloc'd via
  1808. * tls_alloc_buffers, and it is fine to free it. Otherwise this
  1809. * looks like real pending data and it is an error.
  1810. */
  1811. if (rl->nextwbuf != 0
  1812. || rl->numwpipes != 1
  1813. || TLS_BUFFER_get_left(&rl->wbuf[0]) != 0)
  1814. return 0;
  1815. }
  1816. tls_release_write_buffer(rl);
  1817. return 1;
  1818. }
  1819. /* Read direction */
  1820. /* If we have pending data to be read then fail */
  1821. if (rl->curr_rec < rl->num_recs || TLS_BUFFER_get_left(&rl->rbuf) != 0)
  1822. return 0;
  1823. return tls_release_read_buffer(rl);
  1824. }
  1825. const OSSL_RECORD_METHOD ossl_tls_record_method = {
  1826. tls_new_record_layer,
  1827. tls_free,
  1828. tls_unprocessed_read_pending,
  1829. tls_processed_read_pending,
  1830. tls_app_data_pending,
  1831. tls_get_max_records,
  1832. tls_write_records,
  1833. tls_retry_write_records,
  1834. tls_read_record,
  1835. tls_release_record,
  1836. tls_get_alert_code,
  1837. tls_set1_bio,
  1838. tls_set_protocol_version,
  1839. tls_set_plain_alerts,
  1840. tls_set_first_handshake,
  1841. tls_set_max_pipelines,
  1842. NULL,
  1843. tls_get_state,
  1844. tls_set_options,
  1845. tls_get_compression,
  1846. tls_set_max_frag_len,
  1847. NULL,
  1848. tls_increment_sequence_ctr,
  1849. tls_alloc_buffers,
  1850. tls_free_buffers
  1851. };