tls_common.c 68 KB

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