tls_common.c 67 KB

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