tls_common.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173
  1. /*
  2. * Copyright 2022-2025 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 maxalign = 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. maxalign = SSL3_ALIGN_PAYLOAD - 1;
  139. #endif
  140. defltlen = maxalign + 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 + maxalign + 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, maxalign = 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. maxalign = SSL3_ALIGN_PAYLOAD - 1;
  205. #endif
  206. if (b->buf == NULL) {
  207. len = rl->max_frag_len
  208. + SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + maxalign;
  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. unsigned char ctype = (unsigned char)rec->type;
  946. rl->msg_callback(0, rl->version, SSL3_RT_INNER_CONTENT_TYPE, &ctype,
  947. 1, rl->cbarg);
  948. }
  949. /*
  950. * TLSv1.3 alert and handshake records are required to be non-zero in
  951. * length.
  952. */
  953. if ((rec->type == SSL3_RT_HANDSHAKE || rec->type == SSL3_RT_ALERT)
  954. && rec->length == 0) {
  955. RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_LENGTH);
  956. return 0;
  957. }
  958. return 1;
  959. }
  960. int tls_read_record(OSSL_RECORD_LAYER *rl, void **rechandle, int *rversion,
  961. uint8_t *type, const unsigned char **data, size_t *datalen,
  962. uint16_t *epoch, unsigned char *seq_num)
  963. {
  964. TLS_RL_RECORD *rec;
  965. /*
  966. * tls_get_more_records() can return success without actually reading
  967. * anything useful (i.e. if empty records are read). We loop here until
  968. * we have something useful. tls_get_more_records() will eventually fail if
  969. * too many sequential empty records are read.
  970. */
  971. while (rl->curr_rec >= rl->num_recs) {
  972. int ret;
  973. if (rl->num_released != rl->num_recs) {
  974. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_RECORDS_NOT_RELEASED);
  975. return OSSL_RECORD_RETURN_FATAL;
  976. }
  977. ret = rl->funcs->get_more_records(rl);
  978. if (ret != OSSL_RECORD_RETURN_SUCCESS)
  979. return ret;
  980. }
  981. /*
  982. * We have now got rl->num_recs records buffered in rl->rrec. rl->curr_rec
  983. * points to the next one to read.
  984. */
  985. rec = &rl->rrec[rl->curr_rec++];
  986. *rechandle = rec;
  987. *rversion = rec->rec_version;
  988. *type = rec->type;
  989. *data = rec->data + rec->off;
  990. *datalen = rec->length;
  991. if (rl->isdtls) {
  992. *epoch = rec->epoch;
  993. memcpy(seq_num, rec->seq_num, sizeof(rec->seq_num));
  994. }
  995. return OSSL_RECORD_RETURN_SUCCESS;
  996. }
  997. int tls_release_record(OSSL_RECORD_LAYER *rl, void *rechandle, size_t length)
  998. {
  999. TLS_RL_RECORD *rec = &rl->rrec[rl->num_released];
  1000. if (!ossl_assert(rl->num_released < rl->curr_rec)
  1001. || !ossl_assert(rechandle == rec)) {
  1002. /* Should not happen */
  1003. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_INVALID_RECORD);
  1004. return OSSL_RECORD_RETURN_FATAL;
  1005. }
  1006. if (rec->length < length) {
  1007. /* Should not happen */
  1008. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1009. return OSSL_RECORD_RETURN_FATAL;
  1010. }
  1011. if ((rl->options & SSL_OP_CLEANSE_PLAINTEXT) != 0)
  1012. OPENSSL_cleanse(rec->data + rec->off, length);
  1013. rec->off += length;
  1014. rec->length -= length;
  1015. if (rec->length > 0)
  1016. return OSSL_RECORD_RETURN_SUCCESS;
  1017. rl->num_released++;
  1018. if (rl->curr_rec == rl->num_released
  1019. && (rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0
  1020. && TLS_BUFFER_get_left(&rl->rbuf) == 0)
  1021. tls_release_read_buffer(rl);
  1022. return OSSL_RECORD_RETURN_SUCCESS;
  1023. }
  1024. int tls_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options)
  1025. {
  1026. const OSSL_PARAM *p;
  1027. p = OSSL_PARAM_locate_const(options, OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS);
  1028. if (p != NULL && !OSSL_PARAM_get_uint64(p, &rl->options)) {
  1029. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1030. return 0;
  1031. }
  1032. p = OSSL_PARAM_locate_const(options, OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE);
  1033. if (p != NULL && !OSSL_PARAM_get_uint32(p, &rl->mode)) {
  1034. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1035. return 0;
  1036. }
  1037. if (rl->direction == OSSL_RECORD_DIRECTION_READ) {
  1038. p = OSSL_PARAM_locate_const(options,
  1039. OSSL_LIBSSL_RECORD_LAYER_READ_BUFFER_LEN);
  1040. if (p != NULL && !OSSL_PARAM_get_size_t(p, &rl->rbuf.default_len)) {
  1041. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1042. return 0;
  1043. }
  1044. } else {
  1045. p = OSSL_PARAM_locate_const(options,
  1046. OSSL_LIBSSL_RECORD_LAYER_PARAM_BLOCK_PADDING);
  1047. if (p != NULL && !OSSL_PARAM_get_size_t(p, &rl->block_padding)) {
  1048. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1049. return 0;
  1050. }
  1051. }
  1052. if (rl->level == OSSL_RECORD_PROTECTION_LEVEL_APPLICATION) {
  1053. /*
  1054. * We ignore any read_ahead setting prior to the application protection
  1055. * level. Otherwise we may read ahead data in a lower protection level
  1056. * that is destined for a higher protection level. To simplify the logic
  1057. * we don't support that at this stage.
  1058. */
  1059. p = OSSL_PARAM_locate_const(options,
  1060. OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD);
  1061. if (p != NULL && !OSSL_PARAM_get_int(p, &rl->read_ahead)) {
  1062. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1063. return 0;
  1064. }
  1065. }
  1066. return 1;
  1067. }
  1068. int
  1069. tls_int_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
  1070. int role, int direction, int level,
  1071. const EVP_CIPHER *ciph, size_t taglen,
  1072. const EVP_MD *md, COMP_METHOD *comp, BIO *prev,
  1073. BIO *transport, BIO *next, const OSSL_PARAM *settings,
  1074. const OSSL_PARAM *options,
  1075. const OSSL_DISPATCH *fns, void *cbarg,
  1076. OSSL_RECORD_LAYER **retrl)
  1077. {
  1078. OSSL_RECORD_LAYER *rl = OPENSSL_zalloc(sizeof(*rl));
  1079. const OSSL_PARAM *p;
  1080. *retrl = NULL;
  1081. if (rl == NULL)
  1082. return OSSL_RECORD_RETURN_FATAL;
  1083. /*
  1084. * Default the value for max_frag_len. This may be overridden by the
  1085. * settings
  1086. */
  1087. rl->max_frag_len = SSL3_RT_MAX_PLAIN_LENGTH;
  1088. /* Loop through all the settings since they must all be understood */
  1089. if (settings != NULL) {
  1090. for (p = settings; p->key != NULL; p++) {
  1091. if (strcmp(p->key, OSSL_LIBSSL_RECORD_LAYER_PARAM_USE_ETM) == 0) {
  1092. if (!OSSL_PARAM_get_int(p, &rl->use_etm)) {
  1093. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1094. goto err;
  1095. }
  1096. } else if (strcmp(p->key,
  1097. OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN) == 0) {
  1098. if (!OSSL_PARAM_get_uint(p, &rl->max_frag_len)) {
  1099. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1100. goto err;
  1101. }
  1102. } else if (strcmp(p->key,
  1103. OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA) == 0) {
  1104. if (!OSSL_PARAM_get_uint32(p, &rl->max_early_data)) {
  1105. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1106. goto err;
  1107. }
  1108. } else if (strcmp(p->key,
  1109. OSSL_LIBSSL_RECORD_LAYER_PARAM_STREAM_MAC) == 0) {
  1110. if (!OSSL_PARAM_get_int(p, &rl->stream_mac)) {
  1111. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1112. goto err;
  1113. }
  1114. } else if (strcmp(p->key,
  1115. OSSL_LIBSSL_RECORD_LAYER_PARAM_TLSTREE) == 0) {
  1116. if (!OSSL_PARAM_get_int(p, &rl->tlstree)) {
  1117. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1118. goto err;
  1119. }
  1120. } else {
  1121. ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_MANDATORY_PARAMETER);
  1122. goto err;
  1123. }
  1124. }
  1125. }
  1126. rl->libctx = libctx;
  1127. rl->propq = propq;
  1128. rl->version = vers;
  1129. rl->role = role;
  1130. rl->direction = direction;
  1131. rl->level = level;
  1132. rl->taglen = taglen;
  1133. rl->md = md;
  1134. rl->alert = SSL_AD_NO_ALERT;
  1135. rl->rstate = SSL_ST_READ_HEADER;
  1136. if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
  1137. rl->is_first_record = 1;
  1138. if (!tls_set1_bio(rl, transport))
  1139. goto err;
  1140. if (prev != NULL && !BIO_up_ref(prev))
  1141. goto err;
  1142. rl->prev = prev;
  1143. if (next != NULL && !BIO_up_ref(next))
  1144. goto err;
  1145. rl->next = next;
  1146. rl->cbarg = cbarg;
  1147. if (fns != NULL) {
  1148. for (; fns->function_id != 0; fns++) {
  1149. switch (fns->function_id) {
  1150. case OSSL_FUNC_RLAYER_SKIP_EARLY_DATA:
  1151. rl->skip_early_data = OSSL_FUNC_rlayer_skip_early_data(fns);
  1152. break;
  1153. case OSSL_FUNC_RLAYER_MSG_CALLBACK:
  1154. rl->msg_callback = OSSL_FUNC_rlayer_msg_callback(fns);
  1155. break;
  1156. case OSSL_FUNC_RLAYER_SECURITY:
  1157. rl->security = OSSL_FUNC_rlayer_security(fns);
  1158. break;
  1159. case OSSL_FUNC_RLAYER_PADDING:
  1160. rl->padding = OSSL_FUNC_rlayer_padding(fns);
  1161. default:
  1162. /* Just ignore anything we don't understand */
  1163. break;
  1164. }
  1165. }
  1166. }
  1167. if (!tls_set_options(rl, options)) {
  1168. ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_GET_PARAMETER);
  1169. goto err;
  1170. }
  1171. if ((rl->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) == 0
  1172. && rl->version <= TLS1_VERSION
  1173. && !EVP_CIPHER_is_a(ciph, "NULL")
  1174. && !EVP_CIPHER_is_a(ciph, "RC4")) {
  1175. /*
  1176. * Enable vulnerability countermeasure for CBC ciphers with known-IV
  1177. * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
  1178. */
  1179. rl->need_empty_fragments = 1;
  1180. }
  1181. *retrl = rl;
  1182. return OSSL_RECORD_RETURN_SUCCESS;
  1183. err:
  1184. tls_int_free(rl);
  1185. return OSSL_RECORD_RETURN_FATAL;
  1186. }
  1187. static int
  1188. tls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
  1189. int role, int direction, int level, uint16_t epoch,
  1190. unsigned char *secret, size_t secretlen,
  1191. unsigned char *key, size_t keylen, unsigned char *iv,
  1192. size_t ivlen, unsigned char *mackey, size_t mackeylen,
  1193. const EVP_CIPHER *ciph, size_t taglen,
  1194. int mactype,
  1195. const EVP_MD *md, COMP_METHOD *comp,
  1196. const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
  1197. BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
  1198. const OSSL_PARAM *settings, const OSSL_PARAM *options,
  1199. const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
  1200. OSSL_RECORD_LAYER **retrl)
  1201. {
  1202. int ret;
  1203. ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
  1204. ciph, taglen, md, comp, prev,
  1205. transport, next, settings,
  1206. options, fns, cbarg, retrl);
  1207. if (ret != OSSL_RECORD_RETURN_SUCCESS)
  1208. return ret;
  1209. switch (vers) {
  1210. case TLS_ANY_VERSION:
  1211. (*retrl)->funcs = &tls_any_funcs;
  1212. break;
  1213. case TLS1_3_VERSION:
  1214. (*retrl)->funcs = &tls_1_3_funcs;
  1215. break;
  1216. case TLS1_2_VERSION:
  1217. case TLS1_1_VERSION:
  1218. case TLS1_VERSION:
  1219. (*retrl)->funcs = &tls_1_funcs;
  1220. break;
  1221. case SSL3_VERSION:
  1222. (*retrl)->funcs = &ssl_3_0_funcs;
  1223. break;
  1224. default:
  1225. /* Should not happen */
  1226. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  1227. ret = OSSL_RECORD_RETURN_FATAL;
  1228. goto err;
  1229. }
  1230. ret = (*retrl)->funcs->set_crypto_state(*retrl, level, key, keylen, iv,
  1231. ivlen, mackey, mackeylen, ciph,
  1232. taglen, mactype, md, comp);
  1233. err:
  1234. if (ret != OSSL_RECORD_RETURN_SUCCESS) {
  1235. tls_int_free(*retrl);
  1236. *retrl = NULL;
  1237. }
  1238. return ret;
  1239. }
  1240. static void tls_int_free(OSSL_RECORD_LAYER *rl)
  1241. {
  1242. BIO_free(rl->prev);
  1243. BIO_free(rl->bio);
  1244. BIO_free(rl->next);
  1245. ossl_tls_buffer_release(&rl->rbuf);
  1246. tls_release_write_buffer(rl);
  1247. EVP_CIPHER_CTX_free(rl->enc_ctx);
  1248. EVP_MD_CTX_free(rl->md_ctx);
  1249. #ifndef OPENSSL_NO_COMP
  1250. COMP_CTX_free(rl->compctx);
  1251. #endif
  1252. if (rl->version == SSL3_VERSION)
  1253. OPENSSL_cleanse(rl->mac_secret, sizeof(rl->mac_secret));
  1254. TLS_RL_RECORD_release(rl->rrec, SSL_MAX_PIPELINES);
  1255. OPENSSL_free(rl);
  1256. }
  1257. int tls_free(OSSL_RECORD_LAYER *rl)
  1258. {
  1259. TLS_BUFFER *rbuf;
  1260. size_t left, written;
  1261. int ret = 1;
  1262. if (rl == NULL)
  1263. return 1;
  1264. rbuf = &rl->rbuf;
  1265. left = TLS_BUFFER_get_left(rbuf);
  1266. if (left > 0) {
  1267. /*
  1268. * This record layer is closing but we still have data left in our
  1269. * buffer. It must be destined for the next epoch - so push it there.
  1270. */
  1271. ret = BIO_write_ex(rl->next, rbuf->buf + rbuf->offset, left, &written);
  1272. }
  1273. tls_int_free(rl);
  1274. return ret;
  1275. }
  1276. int tls_unprocessed_read_pending(OSSL_RECORD_LAYER *rl)
  1277. {
  1278. return TLS_BUFFER_get_left(&rl->rbuf) != 0;
  1279. }
  1280. int tls_processed_read_pending(OSSL_RECORD_LAYER *rl)
  1281. {
  1282. return rl->curr_rec < rl->num_recs;
  1283. }
  1284. size_t tls_app_data_pending(OSSL_RECORD_LAYER *rl)
  1285. {
  1286. size_t i;
  1287. size_t num = 0;
  1288. for (i = rl->curr_rec; i < rl->num_recs; i++) {
  1289. if (rl->rrec[i].type != SSL3_RT_APPLICATION_DATA)
  1290. return num;
  1291. num += rl->rrec[i].length;
  1292. }
  1293. return num;
  1294. }
  1295. size_t tls_get_max_records_default(OSSL_RECORD_LAYER *rl, uint8_t type,
  1296. size_t len,
  1297. size_t maxfrag, size_t *preffrag)
  1298. {
  1299. /*
  1300. * If we have a pipeline capable cipher, and we have been configured to use
  1301. * it, then return the preferred number of pipelines.
  1302. */
  1303. if (rl->max_pipelines > 0
  1304. && rl->enc_ctx != NULL
  1305. && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(rl->enc_ctx))
  1306. & EVP_CIPH_FLAG_PIPELINE) != 0
  1307. && RLAYER_USE_EXPLICIT_IV(rl)) {
  1308. size_t pipes;
  1309. if (len == 0)
  1310. return 1;
  1311. pipes = ((len - 1) / *preffrag) + 1;
  1312. return (pipes < rl->max_pipelines) ? pipes : rl->max_pipelines;
  1313. }
  1314. return 1;
  1315. }
  1316. size_t tls_get_max_records(OSSL_RECORD_LAYER *rl, uint8_t type, size_t len,
  1317. size_t maxfrag, size_t *preffrag)
  1318. {
  1319. return rl->funcs->get_max_records(rl, type, len, maxfrag, preffrag);
  1320. }
  1321. int tls_allocate_write_buffers_default(OSSL_RECORD_LAYER *rl,
  1322. OSSL_RECORD_TEMPLATE *templates,
  1323. size_t numtempl,
  1324. size_t *prefix)
  1325. {
  1326. if (!tls_setup_write_buffer(rl, numtempl, 0, 0)) {
  1327. /* RLAYERfatal() already called */
  1328. return 0;
  1329. }
  1330. return 1;
  1331. }
  1332. int tls_initialise_write_packets_default(OSSL_RECORD_LAYER *rl,
  1333. OSSL_RECORD_TEMPLATE *templates,
  1334. size_t numtempl,
  1335. OSSL_RECORD_TEMPLATE *prefixtempl,
  1336. WPACKET *pkt,
  1337. TLS_BUFFER *bufs,
  1338. size_t *wpinited)
  1339. {
  1340. WPACKET *thispkt;
  1341. size_t j, align;
  1342. TLS_BUFFER *wb;
  1343. for (j = 0; j < numtempl; j++) {
  1344. thispkt = &pkt[j];
  1345. wb = &bufs[j];
  1346. wb->type = templates[j].type;
  1347. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  1348. align = (size_t)TLS_BUFFER_get_buf(wb);
  1349. align += rl->isdtls ? DTLS1_RT_HEADER_LENGTH : SSL3_RT_HEADER_LENGTH;
  1350. align = SSL3_ALIGN_PAYLOAD - 1
  1351. - ((align - 1) % SSL3_ALIGN_PAYLOAD);
  1352. #endif
  1353. TLS_BUFFER_set_offset(wb, align);
  1354. if (!WPACKET_init_static_len(thispkt, TLS_BUFFER_get_buf(wb),
  1355. TLS_BUFFER_get_len(wb), 0)) {
  1356. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1357. return 0;
  1358. }
  1359. (*wpinited)++;
  1360. if (!WPACKET_allocate_bytes(thispkt, align, NULL)) {
  1361. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1362. return 0;
  1363. }
  1364. }
  1365. return 1;
  1366. }
  1367. int tls_prepare_record_header_default(OSSL_RECORD_LAYER *rl,
  1368. WPACKET *thispkt,
  1369. OSSL_RECORD_TEMPLATE *templ,
  1370. uint8_t rectype,
  1371. unsigned char **recdata)
  1372. {
  1373. size_t maxcomplen;
  1374. *recdata = NULL;
  1375. maxcomplen = templ->buflen;
  1376. if (rl->compctx != NULL)
  1377. maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  1378. if (!WPACKET_put_bytes_u8(thispkt, rectype)
  1379. || !WPACKET_put_bytes_u16(thispkt, templ->version)
  1380. || !WPACKET_start_sub_packet_u16(thispkt)
  1381. || (rl->eivlen > 0
  1382. && !WPACKET_allocate_bytes(thispkt, rl->eivlen, NULL))
  1383. || (maxcomplen > 0
  1384. && !WPACKET_reserve_bytes(thispkt, maxcomplen,
  1385. recdata))) {
  1386. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1387. return 0;
  1388. }
  1389. return 1;
  1390. }
  1391. int tls_prepare_for_encryption_default(OSSL_RECORD_LAYER *rl,
  1392. size_t mac_size,
  1393. WPACKET *thispkt,
  1394. TLS_RL_RECORD *thiswr)
  1395. {
  1396. size_t len;
  1397. unsigned char *recordstart;
  1398. /*
  1399. * we should still have the output to thiswr->data and the input from
  1400. * wr->input. Length should be thiswr->length. thiswr->data still points
  1401. * in the wb->buf
  1402. */
  1403. if (!rl->use_etm && mac_size != 0) {
  1404. unsigned char *mac;
  1405. if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
  1406. || !rl->funcs->mac(rl, thiswr, mac, 1)) {
  1407. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1408. return 0;
  1409. }
  1410. }
  1411. /*
  1412. * Reserve some bytes for any growth that may occur during encryption. If
  1413. * we are adding the MAC independently of the cipher algorithm, then the
  1414. * max encrypted overhead does not need to include an allocation for that
  1415. * MAC
  1416. */
  1417. if (!WPACKET_reserve_bytes(thispkt, SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
  1418. - mac_size, NULL)
  1419. /*
  1420. * We also need next the amount of bytes written to this
  1421. * sub-packet
  1422. */
  1423. || !WPACKET_get_length(thispkt, &len)) {
  1424. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1425. return 0;
  1426. }
  1427. /* Get a pointer to the start of this record excluding header */
  1428. recordstart = WPACKET_get_curr(thispkt) - len;
  1429. TLS_RL_RECORD_set_data(thiswr, recordstart);
  1430. TLS_RL_RECORD_reset_input(thiswr);
  1431. TLS_RL_RECORD_set_length(thiswr, len);
  1432. return 1;
  1433. }
  1434. int tls_post_encryption_processing_default(OSSL_RECORD_LAYER *rl,
  1435. size_t mac_size,
  1436. OSSL_RECORD_TEMPLATE *thistempl,
  1437. WPACKET *thispkt,
  1438. TLS_RL_RECORD *thiswr)
  1439. {
  1440. size_t origlen, len;
  1441. size_t headerlen = rl->isdtls ? DTLS1_RT_HEADER_LENGTH
  1442. : SSL3_RT_HEADER_LENGTH;
  1443. /* Allocate bytes for the encryption overhead */
  1444. if (!WPACKET_get_length(thispkt, &origlen)
  1445. /* Check we allowed enough room for the encryption growth */
  1446. || !ossl_assert(origlen + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
  1447. - mac_size >= thiswr->length)
  1448. /* Encryption should never shrink the data! */
  1449. || origlen > thiswr->length
  1450. || (thiswr->length > origlen
  1451. && !WPACKET_allocate_bytes(thispkt,
  1452. thiswr->length - origlen,
  1453. NULL))) {
  1454. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1455. return 0;
  1456. }
  1457. if (rl->use_etm && mac_size != 0) {
  1458. unsigned char *mac;
  1459. if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
  1460. || !rl->funcs->mac(rl, thiswr, mac, 1)) {
  1461. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1462. return 0;
  1463. }
  1464. TLS_RL_RECORD_add_length(thiswr, mac_size);
  1465. }
  1466. if (!WPACKET_get_length(thispkt, &len)
  1467. || !WPACKET_close(thispkt)) {
  1468. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1469. return 0;
  1470. }
  1471. if (rl->msg_callback != NULL) {
  1472. unsigned char *recordstart;
  1473. recordstart = WPACKET_get_curr(thispkt) - len - headerlen;
  1474. rl->msg_callback(1, thiswr->rec_version, SSL3_RT_HEADER, recordstart,
  1475. headerlen, rl->cbarg);
  1476. if (rl->version == TLS1_3_VERSION && rl->enc_ctx != NULL) {
  1477. unsigned char ctype = thistempl->type;
  1478. rl->msg_callback(1, thiswr->rec_version, SSL3_RT_INNER_CONTENT_TYPE,
  1479. &ctype, 1, rl->cbarg);
  1480. }
  1481. }
  1482. if (!WPACKET_finish(thispkt)) {
  1483. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1484. return 0;
  1485. }
  1486. TLS_RL_RECORD_add_length(thiswr, headerlen);
  1487. return 1;
  1488. }
  1489. int tls_write_records_default(OSSL_RECORD_LAYER *rl,
  1490. OSSL_RECORD_TEMPLATE *templates,
  1491. size_t numtempl)
  1492. {
  1493. WPACKET pkt[SSL_MAX_PIPELINES + 1];
  1494. TLS_RL_RECORD wr[SSL_MAX_PIPELINES + 1];
  1495. WPACKET *thispkt;
  1496. TLS_RL_RECORD *thiswr;
  1497. int mac_size = 0, ret = 0;
  1498. size_t wpinited = 0;
  1499. size_t j, prefix = 0;
  1500. OSSL_RECORD_TEMPLATE prefixtempl;
  1501. OSSL_RECORD_TEMPLATE *thistempl;
  1502. if (rl->md_ctx != NULL && EVP_MD_CTX_get0_md(rl->md_ctx) != NULL) {
  1503. mac_size = EVP_MD_CTX_get_size(rl->md_ctx);
  1504. if (mac_size < 0) {
  1505. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1506. goto err;
  1507. }
  1508. }
  1509. if (!rl->funcs->allocate_write_buffers(rl, templates, numtempl, &prefix)) {
  1510. /* RLAYERfatal() already called */
  1511. goto err;
  1512. }
  1513. if (!rl->funcs->initialise_write_packets(rl, templates, numtempl,
  1514. &prefixtempl, pkt, rl->wbuf,
  1515. &wpinited)) {
  1516. /* RLAYERfatal() already called */
  1517. goto err;
  1518. }
  1519. /* Clear our TLS_RL_RECORD structures */
  1520. memset(wr, 0, sizeof(wr));
  1521. for (j = 0; j < numtempl + prefix; j++) {
  1522. unsigned char *compressdata = NULL;
  1523. uint8_t rectype;
  1524. thispkt = &pkt[j];
  1525. thiswr = &wr[j];
  1526. thistempl = (j < prefix) ? &prefixtempl : &templates[j - prefix];
  1527. /*
  1528. * Default to the record type as specified in the template unless the
  1529. * protocol implementation says differently.
  1530. */
  1531. if (rl->funcs->get_record_type != NULL)
  1532. rectype = rl->funcs->get_record_type(rl, thistempl);
  1533. else
  1534. rectype = thistempl->type;
  1535. TLS_RL_RECORD_set_type(thiswr, rectype);
  1536. TLS_RL_RECORD_set_rec_version(thiswr, thistempl->version);
  1537. if (!rl->funcs->prepare_record_header(rl, thispkt, thistempl, rectype,
  1538. &compressdata)) {
  1539. /* RLAYERfatal() already called */
  1540. goto err;
  1541. }
  1542. /* lets setup the record stuff. */
  1543. TLS_RL_RECORD_set_data(thiswr, compressdata);
  1544. TLS_RL_RECORD_set_length(thiswr, thistempl->buflen);
  1545. TLS_RL_RECORD_set_input(thiswr, (unsigned char *)thistempl->buf);
  1546. /*
  1547. * we now 'read' from thiswr->input, thiswr->length bytes into
  1548. * thiswr->data
  1549. */
  1550. /* first we compress */
  1551. if (rl->compctx != NULL) {
  1552. if (!tls_do_compress(rl, thiswr)
  1553. || !WPACKET_allocate_bytes(thispkt, thiswr->length, NULL)) {
  1554. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE);
  1555. goto err;
  1556. }
  1557. } else if (compressdata != NULL) {
  1558. if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {
  1559. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1560. goto err;
  1561. }
  1562. TLS_RL_RECORD_reset_input(&wr[j]);
  1563. }
  1564. if (rl->funcs->add_record_padding != NULL
  1565. && !rl->funcs->add_record_padding(rl, thistempl, thispkt,
  1566. thiswr)) {
  1567. /* RLAYERfatal() already called */
  1568. goto err;
  1569. }
  1570. if (!rl->funcs->prepare_for_encryption(rl, mac_size, thispkt, thiswr)) {
  1571. /* RLAYERfatal() already called */
  1572. goto err;
  1573. }
  1574. }
  1575. if (prefix) {
  1576. if (rl->funcs->cipher(rl, wr, 1, 1, NULL, mac_size) < 1) {
  1577. if (rl->alert == SSL_AD_NO_ALERT) {
  1578. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1579. }
  1580. goto err;
  1581. }
  1582. }
  1583. if (rl->funcs->cipher(rl, wr + prefix, numtempl, 1, NULL, mac_size) < 1) {
  1584. if (rl->alert == SSL_AD_NO_ALERT) {
  1585. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1586. }
  1587. goto err;
  1588. }
  1589. for (j = 0; j < numtempl + prefix; j++) {
  1590. thispkt = &pkt[j];
  1591. thiswr = &wr[j];
  1592. thistempl = (j < prefix) ? &prefixtempl : &templates[j - prefix];
  1593. if (!rl->funcs->post_encryption_processing(rl, mac_size, thistempl,
  1594. thispkt, thiswr)) {
  1595. /* RLAYERfatal() already called */
  1596. goto err;
  1597. }
  1598. /* now let's set up wb */
  1599. TLS_BUFFER_set_left(&rl->wbuf[j], TLS_RL_RECORD_get_length(thiswr));
  1600. }
  1601. ret = 1;
  1602. err:
  1603. for (j = 0; j < wpinited; j++)
  1604. WPACKET_cleanup(&pkt[j]);
  1605. return ret;
  1606. }
  1607. int tls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
  1608. size_t numtempl)
  1609. {
  1610. /* Check we don't have pending data waiting to write */
  1611. if (!ossl_assert(rl->nextwbuf >= rl->numwpipes
  1612. || TLS_BUFFER_get_left(&rl->wbuf[rl->nextwbuf]) == 0)) {
  1613. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1614. return OSSL_RECORD_RETURN_FATAL;
  1615. }
  1616. if (!rl->funcs->write_records(rl, templates, numtempl)) {
  1617. /* RLAYERfatal already called */
  1618. return OSSL_RECORD_RETURN_FATAL;
  1619. }
  1620. rl->nextwbuf = 0;
  1621. /* we now just need to write the buffers */
  1622. return tls_retry_write_records(rl);
  1623. }
  1624. int tls_retry_write_records(OSSL_RECORD_LAYER *rl)
  1625. {
  1626. int i, ret;
  1627. TLS_BUFFER *thiswb;
  1628. size_t tmpwrit = 0;
  1629. if (rl->nextwbuf >= rl->numwpipes)
  1630. return OSSL_RECORD_RETURN_SUCCESS;
  1631. for (;;) {
  1632. thiswb = &rl->wbuf[rl->nextwbuf];
  1633. clear_sys_error();
  1634. if (rl->bio != NULL) {
  1635. if (rl->funcs->prepare_write_bio != NULL) {
  1636. ret = rl->funcs->prepare_write_bio(rl, thiswb->type);
  1637. if (ret != OSSL_RECORD_RETURN_SUCCESS)
  1638. return ret;
  1639. }
  1640. i = BIO_write(rl->bio, (char *)
  1641. &(TLS_BUFFER_get_buf(thiswb)
  1642. [TLS_BUFFER_get_offset(thiswb)]),
  1643. (unsigned int)TLS_BUFFER_get_left(thiswb));
  1644. if (i >= 0) {
  1645. tmpwrit = i;
  1646. if (i == 0 && BIO_should_retry(rl->bio))
  1647. ret = OSSL_RECORD_RETURN_RETRY;
  1648. else
  1649. ret = OSSL_RECORD_RETURN_SUCCESS;
  1650. } else {
  1651. if (BIO_should_retry(rl->bio))
  1652. ret = OSSL_RECORD_RETURN_RETRY;
  1653. else
  1654. ret = OSSL_RECORD_RETURN_FATAL;
  1655. }
  1656. } else {
  1657. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_BIO_NOT_SET);
  1658. ret = OSSL_RECORD_RETURN_FATAL;
  1659. i = -1;
  1660. }
  1661. /*
  1662. * When an empty fragment is sent on a connection using KTLS,
  1663. * it is sent as a write of zero bytes. If this zero byte
  1664. * write succeeds, i will be 0 rather than a non-zero value.
  1665. * Treat i == 0 as success rather than an error for zero byte
  1666. * writes to permit this case.
  1667. */
  1668. if (i >= 0 && tmpwrit == TLS_BUFFER_get_left(thiswb)) {
  1669. TLS_BUFFER_set_left(thiswb, 0);
  1670. TLS_BUFFER_add_offset(thiswb, tmpwrit);
  1671. if (++(rl->nextwbuf) < rl->numwpipes)
  1672. continue;
  1673. if (rl->nextwbuf == rl->numwpipes
  1674. && (rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0)
  1675. tls_release_write_buffer(rl);
  1676. return OSSL_RECORD_RETURN_SUCCESS;
  1677. } else if (i <= 0) {
  1678. if (rl->isdtls) {
  1679. /*
  1680. * For DTLS, just drop it. That's kind of the whole point in
  1681. * using a datagram service
  1682. */
  1683. TLS_BUFFER_set_left(thiswb, 0);
  1684. if (++(rl->nextwbuf) == rl->numwpipes
  1685. && (rl->mode & SSL_MODE_RELEASE_BUFFERS) != 0)
  1686. tls_release_write_buffer(rl);
  1687. }
  1688. return ret;
  1689. }
  1690. TLS_BUFFER_add_offset(thiswb, tmpwrit);
  1691. TLS_BUFFER_sub_left(thiswb, tmpwrit);
  1692. }
  1693. }
  1694. int tls_get_alert_code(OSSL_RECORD_LAYER *rl)
  1695. {
  1696. return rl->alert;
  1697. }
  1698. int tls_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio)
  1699. {
  1700. if (bio != NULL && !BIO_up_ref(bio))
  1701. return 0;
  1702. BIO_free(rl->bio);
  1703. rl->bio = bio;
  1704. return 1;
  1705. }
  1706. /* Shared by most methods except tlsany_meth */
  1707. int tls_default_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
  1708. {
  1709. if (rl->version != version)
  1710. return 0;
  1711. return 1;
  1712. }
  1713. int tls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
  1714. {
  1715. return rl->funcs->set_protocol_version(rl, version);
  1716. }
  1717. void tls_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow)
  1718. {
  1719. rl->allow_plain_alerts = allow;
  1720. }
  1721. void tls_set_first_handshake(OSSL_RECORD_LAYER *rl, int first)
  1722. {
  1723. rl->is_first_handshake = first;
  1724. }
  1725. void tls_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines)
  1726. {
  1727. rl->max_pipelines = max_pipelines;
  1728. if (max_pipelines > 1)
  1729. rl->read_ahead = 1;
  1730. }
  1731. void tls_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
  1732. const char **longstr)
  1733. {
  1734. const char *shrt, *lng;
  1735. switch (rl->rstate) {
  1736. case SSL_ST_READ_HEADER:
  1737. shrt = "RH";
  1738. lng = "read header";
  1739. break;
  1740. case SSL_ST_READ_BODY:
  1741. shrt = "RB";
  1742. lng = "read body";
  1743. break;
  1744. default:
  1745. shrt = lng = "unknown";
  1746. break;
  1747. }
  1748. if (shortstr != NULL)
  1749. *shortstr = shrt;
  1750. if (longstr != NULL)
  1751. *longstr = lng;
  1752. }
  1753. const COMP_METHOD *tls_get_compression(OSSL_RECORD_LAYER *rl)
  1754. {
  1755. #ifndef OPENSSL_NO_COMP
  1756. return (rl->compctx == NULL) ? NULL : COMP_CTX_get_method(rl->compctx);
  1757. #else
  1758. return NULL;
  1759. #endif
  1760. }
  1761. void tls_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len)
  1762. {
  1763. rl->max_frag_len = max_frag_len;
  1764. /*
  1765. * We don't need to adjust buffer sizes. Write buffer sizes are
  1766. * automatically checked anyway. We should only be changing the read buffer
  1767. * size during the handshake, so we will create a new buffer when we create
  1768. * the new record layer. We can't change the existing buffer because it may
  1769. * already have data in it.
  1770. */
  1771. }
  1772. int tls_increment_sequence_ctr(OSSL_RECORD_LAYER *rl)
  1773. {
  1774. int i;
  1775. /* Increment the sequence counter */
  1776. for (i = SEQ_NUM_SIZE; i > 0; i--) {
  1777. ++(rl->sequence[i - 1]);
  1778. if (rl->sequence[i - 1] != 0)
  1779. break;
  1780. }
  1781. if (i == 0) {
  1782. /* Sequence has wrapped */
  1783. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_SEQUENCE_CTR_WRAPPED);
  1784. return 0;
  1785. }
  1786. return 1;
  1787. }
  1788. int tls_alloc_buffers(OSSL_RECORD_LAYER *rl)
  1789. {
  1790. if (rl->direction == OSSL_RECORD_DIRECTION_WRITE) {
  1791. /* If we have a pending write then buffers are already allocated */
  1792. if (rl->nextwbuf < rl->numwpipes)
  1793. return 1;
  1794. /*
  1795. * We assume 1 pipe with default sized buffer. If what we need ends up
  1796. * being a different size to that then it will be reallocated on demand.
  1797. * If we need more than 1 pipe then that will also be allocated on
  1798. * demand
  1799. */
  1800. if (!tls_setup_write_buffer(rl, 1, 0, 0))
  1801. return 0;
  1802. /*
  1803. * Normally when we allocate write buffers we immediately write
  1804. * something into it. In this case we're not doing that so mark the
  1805. * buffer as empty.
  1806. */
  1807. TLS_BUFFER_set_left(&rl->wbuf[0], 0);
  1808. return 1;
  1809. }
  1810. /* Read direction */
  1811. /* If we have pending data to be read then buffers are already allocated */
  1812. if (rl->curr_rec < rl->num_recs || TLS_BUFFER_get_left(&rl->rbuf) != 0)
  1813. return 1;
  1814. return tls_setup_read_buffer(rl);
  1815. }
  1816. int tls_free_buffers(OSSL_RECORD_LAYER *rl)
  1817. {
  1818. if (rl->direction == OSSL_RECORD_DIRECTION_WRITE) {
  1819. if (rl->nextwbuf < rl->numwpipes) {
  1820. /*
  1821. * We may have pending data. If we've just got one empty buffer
  1822. * allocated then it has probably just been alloc'd via
  1823. * tls_alloc_buffers, and it is fine to free it. Otherwise this
  1824. * looks like real pending data and it is an error.
  1825. */
  1826. if (rl->nextwbuf != 0
  1827. || rl->numwpipes != 1
  1828. || TLS_BUFFER_get_left(&rl->wbuf[0]) != 0)
  1829. return 0;
  1830. }
  1831. tls_release_write_buffer(rl);
  1832. return 1;
  1833. }
  1834. /* Read direction */
  1835. /* If we have pending data to be read then fail */
  1836. if (rl->curr_rec < rl->num_recs
  1837. || rl->curr_rec != rl->num_released
  1838. || TLS_BUFFER_get_left(&rl->rbuf) != 0
  1839. || rl->rstate == SSL_ST_READ_BODY)
  1840. return 0;
  1841. return tls_release_read_buffer(rl);
  1842. }
  1843. const OSSL_RECORD_METHOD ossl_tls_record_method = {
  1844. tls_new_record_layer,
  1845. tls_free,
  1846. tls_unprocessed_read_pending,
  1847. tls_processed_read_pending,
  1848. tls_app_data_pending,
  1849. tls_get_max_records,
  1850. tls_write_records,
  1851. tls_retry_write_records,
  1852. tls_read_record,
  1853. tls_release_record,
  1854. tls_get_alert_code,
  1855. tls_set1_bio,
  1856. tls_set_protocol_version,
  1857. tls_set_plain_alerts,
  1858. tls_set_first_handshake,
  1859. tls_set_max_pipelines,
  1860. NULL,
  1861. tls_get_state,
  1862. tls_set_options,
  1863. tls_get_compression,
  1864. tls_set_max_frag_len,
  1865. NULL,
  1866. tls_increment_sequence_ctr,
  1867. tls_alloc_buffers,
  1868. tls_free_buffers
  1869. };