ssl3_record.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. /*
  2. * Copyright 1995-2020 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 "../ssl_local.h"
  10. #include <openssl/trace.h>
  11. #include <openssl/rand.h>
  12. #include <openssl/core_names.h>
  13. #include "record_local.h"
  14. #include "internal/cryptlib.h"
  15. static const unsigned char ssl3_pad_1[48] = {
  16. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  17. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  18. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  19. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  20. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  21. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
  22. };
  23. static const unsigned char ssl3_pad_2[48] = {
  24. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  25. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  26. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  27. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  28. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  29. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
  30. };
  31. /*
  32. * Clear the contents of an SSL3_RECORD but retain any memory allocated
  33. */
  34. void SSL3_RECORD_clear(SSL3_RECORD *r, size_t num_recs)
  35. {
  36. unsigned char *comp;
  37. size_t i;
  38. for (i = 0; i < num_recs; i++) {
  39. comp = r[i].comp;
  40. memset(&r[i], 0, sizeof(*r));
  41. r[i].comp = comp;
  42. }
  43. }
  44. void SSL3_RECORD_release(SSL3_RECORD *r, size_t num_recs)
  45. {
  46. size_t i;
  47. for (i = 0; i < num_recs; i++) {
  48. OPENSSL_free(r[i].comp);
  49. r[i].comp = NULL;
  50. }
  51. }
  52. void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
  53. {
  54. memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
  55. }
  56. /*
  57. * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
  58. * for us in the buffer.
  59. */
  60. static int ssl3_record_app_data_waiting(SSL *s)
  61. {
  62. SSL3_BUFFER *rbuf;
  63. size_t left, len;
  64. unsigned char *p;
  65. rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
  66. p = SSL3_BUFFER_get_buf(rbuf);
  67. if (p == NULL)
  68. return 0;
  69. left = SSL3_BUFFER_get_left(rbuf);
  70. if (left < SSL3_RT_HEADER_LENGTH)
  71. return 0;
  72. p += SSL3_BUFFER_get_offset(rbuf);
  73. /*
  74. * We only check the type and record length, we will sanity check version
  75. * etc later
  76. */
  77. if (*p != SSL3_RT_APPLICATION_DATA)
  78. return 0;
  79. p += 3;
  80. n2s(p, len);
  81. if (left < SSL3_RT_HEADER_LENGTH + len)
  82. return 0;
  83. return 1;
  84. }
  85. int early_data_count_ok(SSL *s, size_t length, size_t overhead, int send)
  86. {
  87. uint32_t max_early_data;
  88. SSL_SESSION *sess = s->session;
  89. /*
  90. * If we are a client then we always use the max_early_data from the
  91. * session/psksession. Otherwise we go with the lowest out of the max early
  92. * data set in the session and the configured max_early_data.
  93. */
  94. if (!s->server && sess->ext.max_early_data == 0) {
  95. if (!ossl_assert(s->psksession != NULL
  96. && s->psksession->ext.max_early_data > 0)) {
  97. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_EARLY_DATA_COUNT_OK,
  98. ERR_R_INTERNAL_ERROR);
  99. return 0;
  100. }
  101. sess = s->psksession;
  102. }
  103. if (!s->server)
  104. max_early_data = sess->ext.max_early_data;
  105. else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
  106. max_early_data = s->recv_max_early_data;
  107. else
  108. max_early_data = s->recv_max_early_data < sess->ext.max_early_data
  109. ? s->recv_max_early_data : sess->ext.max_early_data;
  110. if (max_early_data == 0) {
  111. SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
  112. SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
  113. return 0;
  114. }
  115. /* If we are dealing with ciphertext we need to allow for the overhead */
  116. max_early_data += overhead;
  117. if (s->early_data_count + length > max_early_data) {
  118. SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
  119. SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
  120. return 0;
  121. }
  122. s->early_data_count += length;
  123. return 1;
  124. }
  125. /*
  126. * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
  127. * will be processed per call to ssl3_get_record. Without this limit an
  128. * attacker could send empty records at a faster rate than we can process and
  129. * cause ssl3_get_record to loop forever.
  130. */
  131. #define MAX_EMPTY_RECORDS 32
  132. #define SSL2_RT_HEADER_LENGTH 2
  133. /*-
  134. * Call this to get new input records.
  135. * It will return <= 0 if more data is needed, normally due to an error
  136. * or non-blocking IO.
  137. * When it finishes, |numrpipes| records have been decoded. For each record 'i':
  138. * rr[i].type - is the type of record
  139. * rr[i].data, - data
  140. * rr[i].length, - number of bytes
  141. * Multiple records will only be returned if the record types are all
  142. * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
  143. * |max_pipelines|
  144. */
  145. /* used only by ssl3_read_bytes */
  146. int ssl3_get_record(SSL *s)
  147. {
  148. int enc_err, rret;
  149. int i;
  150. size_t more, n;
  151. SSL3_RECORD *rr, *thisrr;
  152. SSL3_BUFFER *rbuf;
  153. SSL_SESSION *sess;
  154. unsigned char *p;
  155. unsigned char md[EVP_MAX_MD_SIZE];
  156. unsigned int version;
  157. size_t mac_size = 0;
  158. int imac_size;
  159. size_t num_recs = 0, max_recs, j;
  160. PACKET pkt, sslv2pkt;
  161. int is_ktls_left;
  162. SSL_MAC_BUF *macbufs = NULL;
  163. int ret = -1;
  164. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  165. rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
  166. is_ktls_left = (rbuf->left > 0);
  167. max_recs = s->max_pipelines;
  168. if (max_recs == 0)
  169. max_recs = 1;
  170. sess = s->session;
  171. do {
  172. thisrr = &rr[num_recs];
  173. /* check if we have the header */
  174. if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
  175. (RECORD_LAYER_get_packet_length(&s->rlayer)
  176. < SSL3_RT_HEADER_LENGTH)) {
  177. size_t sslv2len;
  178. unsigned int type;
  179. rret = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
  180. SSL3_BUFFER_get_len(rbuf), 0,
  181. num_recs == 0 ? 1 : 0, &n);
  182. if (rret <= 0) {
  183. #ifndef OPENSSL_NO_KTLS
  184. if (!BIO_get_ktls_recv(s->rbio) || rret == 0)
  185. return rret; /* error or non-blocking */
  186. switch (errno) {
  187. case EBADMSG:
  188. SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
  189. SSL_F_SSL3_GET_RECORD,
  190. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  191. break;
  192. case EMSGSIZE:
  193. SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
  194. SSL_F_SSL3_GET_RECORD,
  195. SSL_R_PACKET_LENGTH_TOO_LONG);
  196. break;
  197. case EINVAL:
  198. SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
  199. SSL_F_SSL3_GET_RECORD,
  200. SSL_R_WRONG_VERSION_NUMBER);
  201. break;
  202. default:
  203. break;
  204. }
  205. #endif
  206. return rret;
  207. }
  208. RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
  209. p = RECORD_LAYER_get_packet(&s->rlayer);
  210. if (!PACKET_buf_init(&pkt, RECORD_LAYER_get_packet(&s->rlayer),
  211. RECORD_LAYER_get_packet_length(&s->rlayer))) {
  212. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
  213. ERR_R_INTERNAL_ERROR);
  214. return -1;
  215. }
  216. sslv2pkt = pkt;
  217. if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
  218. || !PACKET_get_1(&sslv2pkt, &type)) {
  219. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
  220. ERR_R_INTERNAL_ERROR);
  221. return -1;
  222. }
  223. /*
  224. * The first record received by the server may be a V2ClientHello.
  225. */
  226. if (s->server && RECORD_LAYER_is_first_record(&s->rlayer)
  227. && (sslv2len & 0x8000) != 0
  228. && (type == SSL2_MT_CLIENT_HELLO)) {
  229. /*
  230. * SSLv2 style record
  231. *
  232. * |num_recs| here will actually always be 0 because
  233. * |num_recs > 0| only ever occurs when we are processing
  234. * multiple app data records - which we know isn't the case here
  235. * because it is an SSLv2ClientHello. We keep it using
  236. * |num_recs| for the sake of consistency
  237. */
  238. thisrr->type = SSL3_RT_HANDSHAKE;
  239. thisrr->rec_version = SSL2_VERSION;
  240. thisrr->length = sslv2len & 0x7fff;
  241. if (thisrr->length > SSL3_BUFFER_get_len(rbuf)
  242. - SSL2_RT_HEADER_LENGTH) {
  243. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
  244. SSL_R_PACKET_LENGTH_TOO_LONG);
  245. return -1;
  246. }
  247. if (thisrr->length < MIN_SSL2_RECORD_LEN) {
  248. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
  249. SSL_R_LENGTH_TOO_SHORT);
  250. return -1;
  251. }
  252. } else {
  253. /* SSLv3+ style record */
  254. /* Pull apart the header into the SSL3_RECORD */
  255. if (!PACKET_get_1(&pkt, &type)
  256. || !PACKET_get_net_2(&pkt, &version)
  257. || !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
  258. if (s->msg_callback)
  259. s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
  260. s->msg_callback_arg);
  261. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
  262. ERR_R_INTERNAL_ERROR);
  263. return -1;
  264. }
  265. thisrr->type = type;
  266. thisrr->rec_version = version;
  267. if (s->msg_callback)
  268. s->msg_callback(0, version, SSL3_RT_HEADER, p, 5, s,
  269. s->msg_callback_arg);
  270. /*
  271. * Lets check version. In TLSv1.3 we only check this field
  272. * when encryption is occurring (see later check). For the
  273. * ServerHello after an HRR we haven't actually selected TLSv1.3
  274. * yet, but we still treat it as TLSv1.3, so we must check for
  275. * that explicitly
  276. */
  277. if (!s->first_packet && !SSL_IS_TLS13(s)
  278. && s->hello_retry_request != SSL_HRR_PENDING
  279. && version != (unsigned int)s->version) {
  280. if ((s->version & 0xFF00) == (version & 0xFF00)
  281. && !s->enc_write_ctx && !s->write_hash) {
  282. if (thisrr->type == SSL3_RT_ALERT) {
  283. /*
  284. * The record is using an incorrect version number,
  285. * but what we've got appears to be an alert. We
  286. * haven't read the body yet to check whether its a
  287. * fatal or not - but chances are it is. We probably
  288. * shouldn't send a fatal alert back. We'll just
  289. * end.
  290. */
  291. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
  292. SSL_R_WRONG_VERSION_NUMBER);
  293. return -1;
  294. }
  295. /*
  296. * Send back error using their minor version number :-)
  297. */
  298. s->version = (unsigned short)version;
  299. }
  300. SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL3_GET_RECORD,
  301. SSL_R_WRONG_VERSION_NUMBER);
  302. return -1;
  303. }
  304. if ((version >> 8) != SSL3_VERSION_MAJOR) {
  305. if (RECORD_LAYER_is_first_record(&s->rlayer)) {
  306. /* Go back to start of packet, look at the five bytes
  307. * that we have. */
  308. p = RECORD_LAYER_get_packet(&s->rlayer);
  309. if (strncmp((char *)p, "GET ", 4) == 0 ||
  310. strncmp((char *)p, "POST ", 5) == 0 ||
  311. strncmp((char *)p, "HEAD ", 5) == 0 ||
  312. strncmp((char *)p, "PUT ", 4) == 0) {
  313. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
  314. SSL_R_HTTP_REQUEST);
  315. return -1;
  316. } else if (strncmp((char *)p, "CONNE", 5) == 0) {
  317. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
  318. SSL_R_HTTPS_PROXY_REQUEST);
  319. return -1;
  320. }
  321. /* Doesn't look like TLS - don't send an alert */
  322. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
  323. SSL_R_WRONG_VERSION_NUMBER);
  324. return -1;
  325. } else {
  326. SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
  327. SSL_F_SSL3_GET_RECORD,
  328. SSL_R_WRONG_VERSION_NUMBER);
  329. return -1;
  330. }
  331. }
  332. if (SSL_IS_TLS13(s) && s->enc_read_ctx != NULL) {
  333. if (thisrr->type != SSL3_RT_APPLICATION_DATA
  334. && (thisrr->type != SSL3_RT_CHANGE_CIPHER_SPEC
  335. || !SSL_IS_FIRST_HANDSHAKE(s))
  336. && (thisrr->type != SSL3_RT_ALERT
  337. || s->statem.enc_read_state
  338. != ENC_READ_STATE_ALLOW_PLAIN_ALERTS)) {
  339. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  340. SSL_F_SSL3_GET_RECORD, SSL_R_BAD_RECORD_TYPE);
  341. return -1;
  342. }
  343. if (thisrr->rec_version != TLS1_2_VERSION) {
  344. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
  345. SSL_R_WRONG_VERSION_NUMBER);
  346. return -1;
  347. }
  348. }
  349. if (thisrr->length >
  350. SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
  351. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
  352. SSL_R_PACKET_LENGTH_TOO_LONG);
  353. return -1;
  354. }
  355. }
  356. /* now s->rlayer.rstate == SSL_ST_READ_BODY */
  357. }
  358. if (SSL_IS_TLS13(s)) {
  359. if (thisrr->length > SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH) {
  360. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
  361. SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  362. return -1;
  363. }
  364. } else {
  365. size_t len = SSL3_RT_MAX_ENCRYPTED_LENGTH;
  366. #ifndef OPENSSL_NO_COMP
  367. /*
  368. * If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
  369. * does not include the compression overhead anyway.
  370. */
  371. if (s->expand == NULL)
  372. len -= SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  373. #endif
  374. if (thisrr->length > len && !BIO_get_ktls_recv(s->rbio)) {
  375. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
  376. SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  377. return -1;
  378. }
  379. }
  380. /*
  381. * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
  382. * Calculate how much more data we need to read for the rest of the
  383. * record
  384. */
  385. if (thisrr->rec_version == SSL2_VERSION) {
  386. more = thisrr->length + SSL2_RT_HEADER_LENGTH
  387. - SSL3_RT_HEADER_LENGTH;
  388. } else {
  389. more = thisrr->length;
  390. }
  391. if (more > 0) {
  392. /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
  393. rret = ssl3_read_n(s, more, more, 1, 0, &n);
  394. if (rret <= 0)
  395. return rret; /* error or non-blocking io */
  396. }
  397. /* set state for later operations */
  398. RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
  399. /*
  400. * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH
  401. * + thisrr->length, or s->packet_length == SSL2_RT_HEADER_LENGTH
  402. * + thisrr->length and we have that many bytes in s->packet
  403. */
  404. if (thisrr->rec_version == SSL2_VERSION) {
  405. thisrr->input =
  406. &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
  407. } else {
  408. thisrr->input =
  409. &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
  410. }
  411. /*
  412. * ok, we can now read from 's->packet' data into 'thisrr' thisrr->input
  413. * points at thisrr->length bytes, which need to be copied into
  414. * thisrr->data by either the decryption or by the decompression When
  415. * the data is 'copied' into the thisrr->data buffer, thisrr->input will
  416. * be pointed at the new buffer
  417. */
  418. /*
  419. * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
  420. * thisrr->length bytes of encrypted compressed stuff.
  421. */
  422. /* decrypt in place in 'thisrr->input' */
  423. thisrr->data = thisrr->input;
  424. thisrr->orig_len = thisrr->length;
  425. /* Mark this record as not read by upper layers yet */
  426. thisrr->read = 0;
  427. num_recs++;
  428. /* we have pulled in a full packet so zero things */
  429. RECORD_LAYER_reset_packet_length(&s->rlayer);
  430. RECORD_LAYER_clear_first_record(&s->rlayer);
  431. } while (num_recs < max_recs
  432. && thisrr->type == SSL3_RT_APPLICATION_DATA
  433. && SSL_USE_EXPLICIT_IV(s)
  434. && s->enc_read_ctx != NULL
  435. && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
  436. & EVP_CIPH_FLAG_PIPELINE)
  437. && ssl3_record_app_data_waiting(s));
  438. if (num_recs == 1
  439. && thisrr->type == SSL3_RT_CHANGE_CIPHER_SPEC
  440. && (SSL_IS_TLS13(s) || s->hello_retry_request != SSL_HRR_NONE)
  441. && SSL_IS_FIRST_HANDSHAKE(s)) {
  442. /*
  443. * CCS messages must be exactly 1 byte long, containing the value 0x01
  444. */
  445. if (thisrr->length != 1 || thisrr->data[0] != 0x01) {
  446. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL3_GET_RECORD,
  447. SSL_R_INVALID_CCS_MESSAGE);
  448. return -1;
  449. }
  450. /*
  451. * CCS messages are ignored in TLSv1.3. We treat it like an empty
  452. * handshake record
  453. */
  454. thisrr->type = SSL3_RT_HANDSHAKE;
  455. RECORD_LAYER_inc_empty_record_count(&s->rlayer);
  456. if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
  457. > MAX_EMPTY_RECORDS) {
  458. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
  459. SSL_R_UNEXPECTED_CCS_MESSAGE);
  460. return -1;
  461. }
  462. thisrr->read = 1;
  463. RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
  464. return 1;
  465. }
  466. /*
  467. * KTLS reads full records. If there is any data left,
  468. * then it is from before enabling ktls
  469. */
  470. if (BIO_get_ktls_recv(s->rbio) && !is_ktls_left)
  471. goto skip_decryption;
  472. /* TODO(size_t): convert this to do size_t properly */
  473. if (s->read_hash != NULL) {
  474. const EVP_MD *tmpmd = EVP_MD_CTX_md(s->read_hash);
  475. if (tmpmd != NULL) {
  476. imac_size = EVP_MD_size(tmpmd);
  477. if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
  478. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
  479. ERR_LIB_EVP);
  480. return -1;
  481. }
  482. mac_size = (size_t)imac_size;
  483. }
  484. }
  485. /*
  486. * If in encrypt-then-mac mode calculate mac from encrypted record. All
  487. * the details below are public so no timing details can leak.
  488. */
  489. if (SSL_READ_ETM(s) && s->read_hash) {
  490. unsigned char *mac;
  491. for (j = 0; j < num_recs; j++) {
  492. thisrr = &rr[j];
  493. if (thisrr->length < mac_size) {
  494. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
  495. SSL_R_LENGTH_TOO_SHORT);
  496. return -1;
  497. }
  498. thisrr->length -= mac_size;
  499. mac = thisrr->data + thisrr->length;
  500. i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
  501. if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
  502. SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
  503. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  504. return -1;
  505. }
  506. }
  507. /*
  508. * We've handled the mac now - there is no MAC inside the encrypted
  509. * record
  510. */
  511. mac_size = 0;
  512. }
  513. if (mac_size > 0) {
  514. macbufs = OPENSSL_zalloc(sizeof(*macbufs) * num_recs);
  515. if (macbufs == NULL) {
  516. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
  517. ERR_R_MALLOC_FAILURE);
  518. return -1;
  519. }
  520. }
  521. enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0, macbufs, mac_size);
  522. /*-
  523. * enc_err is:
  524. * 0: if the record is publicly invalid, or an internal error, or AEAD
  525. * decryption failed, or ETM decryption failed.
  526. * 1: Success or MTE decryption failed (MAC will be randomised)
  527. */
  528. if (enc_err == 0) {
  529. if (ossl_statem_in_error(s)) {
  530. /* SSLfatal() already got called */
  531. goto end;
  532. }
  533. if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
  534. /*
  535. * Valid early_data that we cannot decrypt will fail here. We treat
  536. * it like an empty record.
  537. */
  538. thisrr = &rr[0];
  539. if (!early_data_count_ok(s, thisrr->length,
  540. EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
  541. /* SSLfatal() already called */
  542. goto end;
  543. }
  544. thisrr->length = 0;
  545. thisrr->read = 1;
  546. RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
  547. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  548. ret = 1;
  549. goto end;
  550. }
  551. SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
  552. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  553. goto end;
  554. }
  555. OSSL_TRACE_BEGIN(TLS) {
  556. BIO_printf(trc_out, "dec %lu\n", (unsigned long)rr[0].length);
  557. BIO_dump_indent(trc_out, rr[0].data, rr[0].length, 4);
  558. } OSSL_TRACE_END(TLS);
  559. /* r->length is now the compressed data plus mac */
  560. if ((sess != NULL) &&
  561. (s->enc_read_ctx != NULL) &&
  562. (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)) {
  563. /* s->read_hash != NULL => mac_size != -1 */
  564. for (j = 0; j < num_recs; j++) {
  565. SSL_MAC_BUF *thismb = &macbufs[j];
  566. thisrr = &rr[j];
  567. i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
  568. if (i == 0 || thismb == NULL || thismb->mac == NULL
  569. || CRYPTO_memcmp(md, thismb->mac, (size_t)mac_size) != 0)
  570. enc_err = 0;
  571. if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
  572. enc_err = 0;
  573. }
  574. }
  575. if (enc_err == 0) {
  576. if (ossl_statem_in_error(s)) {
  577. /* We already called SSLfatal() */
  578. goto end;
  579. }
  580. /*
  581. * A separate 'decryption_failed' alert was introduced with TLS 1.0,
  582. * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
  583. * failure is directly visible from the ciphertext anyway, we should
  584. * not reveal which kind of error occurred -- this might become
  585. * visible to an attacker (e.g. via a logfile)
  586. */
  587. SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
  588. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  589. goto end;
  590. }
  591. skip_decryption:
  592. for (j = 0; j < num_recs; j++) {
  593. thisrr = &rr[j];
  594. /* thisrr->length is now just compressed */
  595. if (s->expand != NULL) {
  596. if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
  597. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
  598. SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  599. goto end;
  600. }
  601. if (!ssl3_do_uncompress(s, thisrr)) {
  602. SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE, SSL_F_SSL3_GET_RECORD,
  603. SSL_R_BAD_DECOMPRESSION);
  604. goto end;
  605. }
  606. }
  607. if (SSL_IS_TLS13(s)
  608. && s->enc_read_ctx != NULL
  609. && thisrr->type != SSL3_RT_ALERT) {
  610. size_t end;
  611. if (thisrr->length == 0
  612. || thisrr->type != SSL3_RT_APPLICATION_DATA) {
  613. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
  614. SSL_R_BAD_RECORD_TYPE);
  615. goto end;
  616. }
  617. /* Strip trailing padding */
  618. for (end = thisrr->length - 1; end > 0 && thisrr->data[end] == 0;
  619. end--)
  620. continue;
  621. thisrr->length = end;
  622. thisrr->type = thisrr->data[end];
  623. if (thisrr->type != SSL3_RT_APPLICATION_DATA
  624. && thisrr->type != SSL3_RT_ALERT
  625. && thisrr->type != SSL3_RT_HANDSHAKE) {
  626. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
  627. SSL_R_BAD_RECORD_TYPE);
  628. goto end;
  629. }
  630. if (s->msg_callback)
  631. s->msg_callback(0, s->version, SSL3_RT_INNER_CONTENT_TYPE,
  632. &thisrr->data[end], 1, s, s->msg_callback_arg);
  633. }
  634. /*
  635. * TLSv1.3 alert and handshake records are required to be non-zero in
  636. * length.
  637. */
  638. if (SSL_IS_TLS13(s)
  639. && (thisrr->type == SSL3_RT_HANDSHAKE
  640. || thisrr->type == SSL3_RT_ALERT)
  641. && thisrr->length == 0) {
  642. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
  643. SSL_R_BAD_LENGTH);
  644. goto end;
  645. }
  646. if (thisrr->length > SSL3_RT_MAX_PLAIN_LENGTH && !BIO_get_ktls_recv(s->rbio)) {
  647. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
  648. SSL_R_DATA_LENGTH_TOO_LONG);
  649. goto end;
  650. }
  651. /* If received packet overflows current Max Fragment Length setting */
  652. if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
  653. && thisrr->length > GET_MAX_FRAGMENT_LENGTH(s->session)
  654. && !BIO_get_ktls_recv(s->rbio)) {
  655. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
  656. SSL_R_DATA_LENGTH_TOO_LONG);
  657. goto end;
  658. }
  659. thisrr->off = 0;
  660. /*-
  661. * So at this point the following is true
  662. * thisrr->type is the type of record
  663. * thisrr->length == number of bytes in record
  664. * thisrr->off == offset to first valid byte
  665. * thisrr->data == where to take bytes from, increment after use :-).
  666. */
  667. /* just read a 0 length packet */
  668. if (thisrr->length == 0) {
  669. RECORD_LAYER_inc_empty_record_count(&s->rlayer);
  670. if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
  671. > MAX_EMPTY_RECORDS) {
  672. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
  673. SSL_R_RECORD_TOO_SMALL);
  674. goto end;
  675. }
  676. } else {
  677. RECORD_LAYER_reset_empty_record_count(&s->rlayer);
  678. }
  679. }
  680. if (s->early_data_state == SSL_EARLY_DATA_READING) {
  681. thisrr = &rr[0];
  682. if (thisrr->type == SSL3_RT_APPLICATION_DATA
  683. && !early_data_count_ok(s, thisrr->length, 0, 0)) {
  684. /* SSLfatal already called */
  685. goto end;
  686. }
  687. }
  688. RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
  689. ret = 1;
  690. end:
  691. if (macbufs != NULL) {
  692. for (j = 0; j < num_recs; j++) {
  693. if (macbufs[j].alloced)
  694. OPENSSL_free(macbufs[j].mac);
  695. }
  696. OPENSSL_free(macbufs);
  697. }
  698. return ret;
  699. }
  700. int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
  701. {
  702. #ifndef OPENSSL_NO_COMP
  703. int i;
  704. if (rr->comp == NULL) {
  705. rr->comp = (unsigned char *)
  706. OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
  707. }
  708. if (rr->comp == NULL)
  709. return 0;
  710. /* TODO(size_t): Convert this call */
  711. i = COMP_expand_block(ssl->expand, rr->comp,
  712. SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
  713. if (i < 0)
  714. return 0;
  715. else
  716. rr->length = i;
  717. rr->data = rr->comp;
  718. #endif
  719. return 1;
  720. }
  721. int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
  722. {
  723. #ifndef OPENSSL_NO_COMP
  724. int i;
  725. /* TODO(size_t): Convert this call */
  726. i = COMP_compress_block(ssl->compress, wr->data,
  727. (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
  728. wr->input, (int)wr->length);
  729. if (i < 0)
  730. return 0;
  731. else
  732. wr->length = i;
  733. wr->input = wr->data;
  734. #endif
  735. return 1;
  736. }
  737. /*-
  738. * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|. Calls SSLfatal on
  739. * internal error, but not otherwise. It is the responsibility of the caller to
  740. * report a bad_record_mac
  741. *
  742. * Returns:
  743. * 0: if the record is publicly invalid, or an internal error
  744. * 1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
  745. */
  746. int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int sending,
  747. SSL_MAC_BUF *mac, size_t macsize)
  748. {
  749. SSL3_RECORD *rec;
  750. EVP_CIPHER_CTX *ds;
  751. size_t l, i;
  752. size_t bs;
  753. const EVP_CIPHER *enc;
  754. rec = inrecs;
  755. /*
  756. * We shouldn't ever be called with more than one record in the SSLv3 case
  757. */
  758. if (n_recs != 1)
  759. return 0;
  760. if (sending) {
  761. ds = s->enc_write_ctx;
  762. if (s->enc_write_ctx == NULL)
  763. enc = NULL;
  764. else
  765. enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
  766. } else {
  767. ds = s->enc_read_ctx;
  768. if (s->enc_read_ctx == NULL)
  769. enc = NULL;
  770. else
  771. enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
  772. }
  773. if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
  774. memmove(rec->data, rec->input, rec->length);
  775. rec->input = rec->data;
  776. } else {
  777. int provided = (EVP_CIPHER_provider(enc) != NULL);
  778. l = rec->length;
  779. /* TODO(size_t): Convert this call */
  780. bs = EVP_CIPHER_CTX_block_size(ds);
  781. /* COMPRESS */
  782. if ((bs != 1) && sending && !provided) {
  783. /*
  784. * We only do this for legacy ciphers. Provided ciphers add the
  785. * padding on the provider side.
  786. */
  787. i = bs - (l % bs);
  788. /* we need to add 'i-1' padding bytes */
  789. l += i;
  790. /*
  791. * the last of these zero bytes will be overwritten with the
  792. * padding length.
  793. */
  794. memset(&rec->input[rec->length], 0, i);
  795. rec->length += i;
  796. rec->input[l - 1] = (unsigned char)(i - 1);
  797. }
  798. if (!sending) {
  799. if (l == 0 || l % bs != 0) {
  800. /* Publicly invalid */
  801. return 0;
  802. }
  803. /* otherwise, rec->length >= bs */
  804. }
  805. if (EVP_CIPHER_provider(enc) != NULL) {
  806. int outlen;
  807. if (!EVP_CipherUpdate(ds, rec->data, &outlen, rec->input,
  808. (unsigned int)l))
  809. return 0;
  810. rec->length = outlen;
  811. if (!sending && mac != NULL) {
  812. /* Now get a pointer to the MAC */
  813. OSSL_PARAM params[2], *p = params;
  814. /* Get the MAC */
  815. mac->alloced = 0;
  816. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
  817. (void **)&mac->mac,
  818. macsize);
  819. *p = OSSL_PARAM_construct_end();
  820. if (!EVP_CIPHER_CTX_get_params(ds, params)) {
  821. /* Shouldn't normally happen */
  822. SSLfatal(s, SSL_AD_INTERNAL_ERROR, 0,
  823. ERR_R_INTERNAL_ERROR);
  824. return 0;
  825. }
  826. }
  827. } else {
  828. /* TODO(size_t): Convert this call */
  829. if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1) {
  830. /* Shouldn't happen */
  831. SSLfatal(s, SSL_AD_BAD_RECORD_MAC, 0, ERR_R_INTERNAL_ERROR);
  832. return 0;
  833. }
  834. if (!sending)
  835. return ssl3_cbc_remove_padding_and_mac(&rec->length,
  836. rec->orig_len,
  837. rec->data,
  838. (mac != NULL) ? &mac->mac : NULL,
  839. (mac != NULL) ? &mac->alloced : NULL,
  840. bs,
  841. macsize,
  842. s->ctx->libctx);
  843. }
  844. }
  845. return 1;
  846. }
  847. #define MAX_PADDING 256
  848. /*-
  849. * tls1_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
  850. * error, but not otherwise. It is the responsibility of the caller to report
  851. * a bad_record_mac - if appropriate (DTLS just drops the record).
  852. *
  853. * Returns:
  854. * 0: if the record is publicly invalid, or an internal error, or AEAD
  855. * decryption failed, or Encrypt-then-mac decryption failed.
  856. * 1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
  857. */
  858. int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
  859. SSL_MAC_BUF *macs, size_t macsize)
  860. {
  861. EVP_CIPHER_CTX *ds;
  862. size_t reclen[SSL_MAX_PIPELINES];
  863. unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
  864. int i, pad = 0, tmpr;
  865. size_t bs, ctr, padnum, loop;
  866. unsigned char padval;
  867. const EVP_CIPHER *enc;
  868. int tlstree_enc = sending ? (s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
  869. : (s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
  870. if (n_recs == 0) {
  871. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  872. ERR_R_INTERNAL_ERROR);
  873. return 0;
  874. }
  875. if (sending) {
  876. if (EVP_MD_CTX_md(s->write_hash)) {
  877. int n = EVP_MD_CTX_size(s->write_hash);
  878. if (!ossl_assert(n >= 0)) {
  879. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  880. ERR_R_INTERNAL_ERROR);
  881. return 0;
  882. }
  883. }
  884. ds = s->enc_write_ctx;
  885. if (s->enc_write_ctx == NULL)
  886. enc = NULL;
  887. else {
  888. int ivlen;
  889. enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
  890. /* For TLSv1.1 and later explicit IV */
  891. if (SSL_USE_EXPLICIT_IV(s)
  892. && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
  893. ivlen = EVP_CIPHER_iv_length(enc);
  894. else
  895. ivlen = 0;
  896. if (ivlen > 1) {
  897. for (ctr = 0; ctr < n_recs; ctr++) {
  898. if (recs[ctr].data != recs[ctr].input) {
  899. /*
  900. * we can't write into the input stream: Can this ever
  901. * happen?? (steve)
  902. */
  903. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  904. ERR_R_INTERNAL_ERROR);
  905. return 0;
  906. } else if (RAND_bytes_ex(s->ctx->libctx, recs[ctr].input,
  907. ivlen) <= 0) {
  908. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  909. ERR_R_INTERNAL_ERROR);
  910. return 0;
  911. }
  912. }
  913. }
  914. }
  915. } else {
  916. if (EVP_MD_CTX_md(s->read_hash)) {
  917. int n = EVP_MD_CTX_size(s->read_hash);
  918. if (!ossl_assert(n >= 0)) {
  919. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  920. ERR_R_INTERNAL_ERROR);
  921. return 0;
  922. }
  923. }
  924. ds = s->enc_read_ctx;
  925. if (s->enc_read_ctx == NULL)
  926. enc = NULL;
  927. else
  928. enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
  929. }
  930. if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
  931. for (ctr = 0; ctr < n_recs; ctr++) {
  932. memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
  933. recs[ctr].input = recs[ctr].data;
  934. }
  935. } else {
  936. int provided = (EVP_CIPHER_provider(enc) != NULL);
  937. bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
  938. if (n_recs > 1) {
  939. if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
  940. & EVP_CIPH_FLAG_PIPELINE)) {
  941. /*
  942. * We shouldn't have been called with pipeline data if the
  943. * cipher doesn't support pipelining
  944. */
  945. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  946. SSL_R_PIPELINE_FAILURE);
  947. return 0;
  948. }
  949. }
  950. for (ctr = 0; ctr < n_recs; ctr++) {
  951. reclen[ctr] = recs[ctr].length;
  952. if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
  953. & EVP_CIPH_FLAG_AEAD_CIPHER) {
  954. unsigned char *seq;
  955. seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
  956. : RECORD_LAYER_get_read_sequence(&s->rlayer);
  957. if (SSL_IS_DTLS(s)) {
  958. /* DTLS does not support pipelining */
  959. unsigned char dtlsseq[9], *p = dtlsseq;
  960. s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
  961. DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
  962. memcpy(p, &seq[2], 6);
  963. memcpy(buf[ctr], dtlsseq, 8);
  964. } else {
  965. memcpy(buf[ctr], seq, 8);
  966. for (i = 7; i >= 0; i--) { /* increment */
  967. ++seq[i];
  968. if (seq[i] != 0)
  969. break;
  970. }
  971. }
  972. buf[ctr][8] = recs[ctr].type;
  973. buf[ctr][9] = (unsigned char)(s->version >> 8);
  974. buf[ctr][10] = (unsigned char)(s->version);
  975. buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
  976. buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
  977. pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
  978. EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
  979. if (pad <= 0) {
  980. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  981. ERR_R_INTERNAL_ERROR);
  982. return 0;
  983. }
  984. if (sending) {
  985. reclen[ctr] += pad;
  986. recs[ctr].length += pad;
  987. }
  988. } else if ((bs != 1) && sending && !provided) {
  989. /*
  990. * We only do this for legacy ciphers. Provided ciphers add the
  991. * padding on the provider side.
  992. */
  993. padnum = bs - (reclen[ctr] % bs);
  994. /* Add weird padding of up to 256 bytes */
  995. if (padnum > MAX_PADDING) {
  996. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  997. ERR_R_INTERNAL_ERROR);
  998. return 0;
  999. }
  1000. /* we need to add 'padnum' padding bytes of value padval */
  1001. padval = (unsigned char)(padnum - 1);
  1002. for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
  1003. recs[ctr].input[loop] = padval;
  1004. reclen[ctr] += padnum;
  1005. recs[ctr].length += padnum;
  1006. }
  1007. if (!sending) {
  1008. if (reclen[ctr] == 0 || reclen[ctr] % bs != 0) {
  1009. /* Publicly invalid */
  1010. return 0;
  1011. }
  1012. }
  1013. }
  1014. if (n_recs > 1) {
  1015. unsigned char *data[SSL_MAX_PIPELINES];
  1016. /* Set the output buffers */
  1017. for (ctr = 0; ctr < n_recs; ctr++) {
  1018. data[ctr] = recs[ctr].data;
  1019. }
  1020. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
  1021. (int)n_recs, data) <= 0) {
  1022. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  1023. SSL_R_PIPELINE_FAILURE);
  1024. return 0;
  1025. }
  1026. /* Set the input buffers */
  1027. for (ctr = 0; ctr < n_recs; ctr++) {
  1028. data[ctr] = recs[ctr].input;
  1029. }
  1030. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
  1031. (int)n_recs, data) <= 0
  1032. || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
  1033. (int)n_recs, reclen) <= 0) {
  1034. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  1035. SSL_R_PIPELINE_FAILURE);
  1036. return 0;
  1037. }
  1038. }
  1039. if (!SSL_IS_DTLS(s) && tlstree_enc) {
  1040. unsigned char *seq;
  1041. int decrement_seq = 0;
  1042. /*
  1043. * When sending, seq is incremented after MAC calculation.
  1044. * So if we are in ETM mode, we use seq 'as is' in the ctrl-function.
  1045. * Otherwise we have to decrease it in the implementation
  1046. */
  1047. if (sending && !SSL_WRITE_ETM(s))
  1048. decrement_seq = 1;
  1049. seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
  1050. : RECORD_LAYER_get_read_sequence(&s->rlayer);
  1051. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_TLSTREE, decrement_seq, seq) <= 0) {
  1052. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  1053. ERR_R_INTERNAL_ERROR);
  1054. return 0;
  1055. }
  1056. }
  1057. if (provided) {
  1058. int outlen;
  1059. /* Provided cipher - we do not support pipelining on this path */
  1060. if (n_recs > 1) {
  1061. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  1062. ERR_R_INTERNAL_ERROR);
  1063. return 0;
  1064. }
  1065. if (!EVP_CipherUpdate(ds, recs[0].data, &outlen, recs[0].input,
  1066. (unsigned int)reclen[0]))
  1067. return 0;
  1068. recs[0].length = outlen;
  1069. /*
  1070. * The length returned from EVP_CipherUpdate above is the actual
  1071. * payload length. We need to adjust the data/input ptr to skip over
  1072. * any explicit IV
  1073. */
  1074. if (!sending) {
  1075. if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
  1076. recs[0].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1077. recs[0].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1078. } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
  1079. recs[0].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1080. recs[0].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1081. } else if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
  1082. recs[0].data += bs;
  1083. recs[0].input += bs;
  1084. recs[0].orig_len -= bs;
  1085. }
  1086. /* Now get a pointer to the MAC (if applicable) */
  1087. if (macs != NULL) {
  1088. OSSL_PARAM params[2], *p = params;
  1089. /* Get the MAC */
  1090. macs[0].alloced = 0;
  1091. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
  1092. (void **)&macs[0].mac,
  1093. macsize);
  1094. *p = OSSL_PARAM_construct_end();
  1095. if (!EVP_CIPHER_CTX_get_params(ds, params)) {
  1096. /* Shouldn't normally happen */
  1097. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
  1098. ERR_R_INTERNAL_ERROR);
  1099. return 0;
  1100. }
  1101. }
  1102. }
  1103. } else {
  1104. /* Legacy cipher */
  1105. /* TODO(size_t): Convert this call */
  1106. tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
  1107. (unsigned int)reclen[0]);
  1108. if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
  1109. & EVP_CIPH_FLAG_CUSTOM_CIPHER)
  1110. ? (tmpr < 0)
  1111. : (tmpr == 0)) {
  1112. /* AEAD can fail to verify MAC */
  1113. return 0;
  1114. }
  1115. if (!sending) {
  1116. /* Adjust the record to remove the explicit IV/MAC/Tag */
  1117. if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
  1118. for (ctr = 0; ctr < n_recs; ctr++) {
  1119. recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1120. recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1121. recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1122. }
  1123. } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
  1124. for (ctr = 0; ctr < n_recs; ctr++) {
  1125. recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1126. recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1127. recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1128. }
  1129. }
  1130. for (ctr = 0; ctr < n_recs; ctr++) {
  1131. if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
  1132. if (recs[ctr].length < bs)
  1133. return 0;
  1134. recs[ctr].data += bs;
  1135. recs[ctr].input += bs;
  1136. recs[ctr].length -= bs;
  1137. recs[ctr].orig_len -= bs;
  1138. }
  1139. /*
  1140. * If using Mac-then-encrypt, then this will succeed but
  1141. * with a random MAC if padding is invalid
  1142. */
  1143. if (!tls1_cbc_remove_padding_and_mac(&recs[ctr].length,
  1144. recs[ctr].orig_len,
  1145. recs[ctr].data,
  1146. (macs != NULL) ? &macs[ctr].mac : NULL,
  1147. (macs != NULL) ? &macs[ctr].alloced
  1148. : NULL,
  1149. bs,
  1150. macsize,
  1151. (EVP_CIPHER_flags(enc)
  1152. & EVP_CIPH_FLAG_AEAD_CIPHER) != 0,
  1153. s->ctx->libctx))
  1154. return 0;
  1155. }
  1156. if (pad) {
  1157. for (ctr = 0; ctr < n_recs; ctr++) {
  1158. recs[ctr].length -= pad;
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. return 1;
  1165. }
  1166. /*
  1167. * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
  1168. * which ssl3_cbc_digest_record supports.
  1169. */
  1170. char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
  1171. {
  1172. switch (EVP_MD_CTX_type(ctx)) {
  1173. case NID_md5:
  1174. case NID_sha1:
  1175. case NID_sha224:
  1176. case NID_sha256:
  1177. case NID_sha384:
  1178. case NID_sha512:
  1179. return 1;
  1180. default:
  1181. return 0;
  1182. }
  1183. }
  1184. int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
  1185. {
  1186. unsigned char *mac_sec, *seq;
  1187. const EVP_MD_CTX *hash;
  1188. unsigned char *p, rec_char;
  1189. size_t md_size;
  1190. size_t npad;
  1191. int t;
  1192. if (sending) {
  1193. mac_sec = &(ssl->s3.write_mac_secret[0]);
  1194. seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
  1195. hash = ssl->write_hash;
  1196. } else {
  1197. mac_sec = &(ssl->s3.read_mac_secret[0]);
  1198. seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
  1199. hash = ssl->read_hash;
  1200. }
  1201. t = EVP_MD_CTX_size(hash);
  1202. if (t < 0)
  1203. return 0;
  1204. md_size = t;
  1205. npad = (48 / md_size) * md_size;
  1206. if (!sending &&
  1207. EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
  1208. ssl3_cbc_record_digest_supported(hash)) {
  1209. #ifdef OPENSSL_NO_DEPRECATED_3_0
  1210. return 0;
  1211. #else
  1212. /*
  1213. * This is a CBC-encrypted record. We must avoid leaking any
  1214. * timing-side channel information about how many blocks of data we
  1215. * are hashing because that gives an attacker a timing-oracle.
  1216. */
  1217. /*-
  1218. * npad is, at most, 48 bytes and that's with MD5:
  1219. * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
  1220. *
  1221. * With SHA-1 (the largest hash speced for SSLv3) the hash size
  1222. * goes up 4, but npad goes down by 8, resulting in a smaller
  1223. * total size.
  1224. */
  1225. unsigned char header[75];
  1226. size_t j = 0;
  1227. memcpy(header + j, mac_sec, md_size);
  1228. j += md_size;
  1229. memcpy(header + j, ssl3_pad_1, npad);
  1230. j += npad;
  1231. memcpy(header + j, seq, 8);
  1232. j += 8;
  1233. header[j++] = rec->type;
  1234. header[j++] = (unsigned char)(rec->length >> 8);
  1235. header[j++] = (unsigned char)(rec->length & 0xff);
  1236. /* Final param == is SSLv3 */
  1237. if (ssl3_cbc_digest_record(EVP_MD_CTX_md(hash),
  1238. md, &md_size,
  1239. header, rec->input,
  1240. rec->length, rec->orig_len,
  1241. mac_sec, md_size, 1) <= 0)
  1242. return 0;
  1243. #endif
  1244. } else {
  1245. unsigned int md_size_u;
  1246. /* Chop the digest off the end :-) */
  1247. EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
  1248. if (md_ctx == NULL)
  1249. return 0;
  1250. rec_char = rec->type;
  1251. p = md;
  1252. s2n(rec->length, p);
  1253. if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
  1254. || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
  1255. || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
  1256. || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
  1257. || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
  1258. || EVP_DigestUpdate(md_ctx, md, 2) <= 0
  1259. || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
  1260. || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
  1261. || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
  1262. || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
  1263. || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
  1264. || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
  1265. || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
  1266. EVP_MD_CTX_free(md_ctx);
  1267. return 0;
  1268. }
  1269. EVP_MD_CTX_free(md_ctx);
  1270. }
  1271. ssl3_record_sequence_update(seq);
  1272. return 1;
  1273. }
  1274. int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
  1275. {
  1276. unsigned char *seq;
  1277. EVP_MD_CTX *hash;
  1278. size_t md_size;
  1279. int i;
  1280. EVP_MD_CTX *hmac = NULL, *mac_ctx;
  1281. unsigned char header[13];
  1282. int stream_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
  1283. : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM);
  1284. int tlstree_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
  1285. : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
  1286. int t;
  1287. if (sending) {
  1288. seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
  1289. hash = ssl->write_hash;
  1290. } else {
  1291. seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
  1292. hash = ssl->read_hash;
  1293. }
  1294. t = EVP_MD_CTX_size(hash);
  1295. if (!ossl_assert(t >= 0))
  1296. return 0;
  1297. md_size = t;
  1298. /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
  1299. if (stream_mac) {
  1300. mac_ctx = hash;
  1301. } else {
  1302. hmac = EVP_MD_CTX_new();
  1303. if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash)) {
  1304. EVP_MD_CTX_free(hmac);
  1305. return 0;
  1306. }
  1307. mac_ctx = hmac;
  1308. }
  1309. if (!SSL_IS_DTLS(ssl) && tlstree_mac && EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_TLSTREE, 0, seq) <= 0) {
  1310. EVP_MD_CTX_free(hmac);
  1311. return 0;
  1312. }
  1313. if (SSL_IS_DTLS(ssl)) {
  1314. unsigned char dtlsseq[8], *p = dtlsseq;
  1315. s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
  1316. DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
  1317. memcpy(p, &seq[2], 6);
  1318. memcpy(header, dtlsseq, 8);
  1319. } else
  1320. memcpy(header, seq, 8);
  1321. header[8] = rec->type;
  1322. header[9] = (unsigned char)(ssl->version >> 8);
  1323. header[10] = (unsigned char)(ssl->version);
  1324. header[11] = (unsigned char)(rec->length >> 8);
  1325. header[12] = (unsigned char)(rec->length & 0xff);
  1326. if (!sending && !SSL_READ_ETM(ssl) &&
  1327. EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
  1328. ssl3_cbc_record_digest_supported(mac_ctx)) {
  1329. OSSL_PARAM tls_hmac_params[2], *p = tls_hmac_params;
  1330. *p++ = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE,
  1331. &rec->orig_len);
  1332. *p++ = OSSL_PARAM_construct_end();
  1333. if (!EVP_PKEY_CTX_set_params(EVP_MD_CTX_pkey_ctx(mac_ctx),
  1334. tls_hmac_params))
  1335. return 0;
  1336. }
  1337. /* TODO(size_t): Convert these calls */
  1338. if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
  1339. || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
  1340. || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
  1341. EVP_MD_CTX_free(hmac);
  1342. return 0;
  1343. }
  1344. EVP_MD_CTX_free(hmac);
  1345. OSSL_TRACE_BEGIN(TLS) {
  1346. BIO_printf(trc_out, "seq:\n");
  1347. BIO_dump_indent(trc_out, seq, 8, 4);
  1348. BIO_printf(trc_out, "rec:\n");
  1349. BIO_dump_indent(trc_out, rec->data, rec->length, 4);
  1350. } OSSL_TRACE_END(TLS);
  1351. if (!SSL_IS_DTLS(ssl)) {
  1352. for (i = 7; i >= 0; i--) {
  1353. ++seq[i];
  1354. if (seq[i] != 0)
  1355. break;
  1356. }
  1357. }
  1358. OSSL_TRACE_BEGIN(TLS) {
  1359. BIO_printf(trc_out, "md:\n");
  1360. BIO_dump_indent(trc_out, md, md_size, 4);
  1361. } OSSL_TRACE_END(TLS);
  1362. return 1;
  1363. }
  1364. int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
  1365. {
  1366. int i;
  1367. int enc_err;
  1368. SSL_SESSION *sess;
  1369. SSL3_RECORD *rr;
  1370. int imac_size;
  1371. size_t mac_size = 0;
  1372. unsigned char md[EVP_MAX_MD_SIZE];
  1373. size_t max_plain_length = SSL3_RT_MAX_PLAIN_LENGTH;
  1374. SSL_MAC_BUF macbuf = { NULL, 0 };
  1375. int ret = 0;
  1376. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  1377. sess = s->session;
  1378. /*
  1379. * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
  1380. * and we have that many bytes in s->packet
  1381. */
  1382. rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
  1383. /*
  1384. * ok, we can now read from 's->packet' data into 'rr' rr->input points
  1385. * at rr->length bytes, which need to be copied into rr->data by either
  1386. * the decryption or by the decompression When the data is 'copied' into
  1387. * the rr->data buffer, rr->input will be pointed at the new buffer
  1388. */
  1389. /*
  1390. * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
  1391. * bytes of encrypted compressed stuff.
  1392. */
  1393. /* check is not needed I believe */
  1394. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  1395. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
  1396. SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  1397. return 0;
  1398. }
  1399. /* decrypt in place in 'rr->input' */
  1400. rr->data = rr->input;
  1401. rr->orig_len = rr->length;
  1402. /* TODO(size_t): convert this to do size_t properly */
  1403. if (s->read_hash != NULL) {
  1404. const EVP_MD *tmpmd = EVP_MD_CTX_md(s->read_hash);
  1405. if (tmpmd != NULL) {
  1406. imac_size = EVP_MD_size(tmpmd);
  1407. if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
  1408. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
  1409. ERR_LIB_EVP);
  1410. return -1;
  1411. }
  1412. mac_size = (size_t)imac_size;
  1413. }
  1414. }
  1415. if (SSL_READ_ETM(s) && s->read_hash) {
  1416. unsigned char *mac;
  1417. if (rr->orig_len < mac_size) {
  1418. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
  1419. SSL_R_LENGTH_TOO_SHORT);
  1420. return 0;
  1421. }
  1422. rr->length -= mac_size;
  1423. mac = rr->data + rr->length;
  1424. i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
  1425. if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
  1426. SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_DTLS1_PROCESS_RECORD,
  1427. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  1428. return 0;
  1429. }
  1430. /*
  1431. * We've handled the mac now - there is no MAC inside the encrypted
  1432. * record
  1433. */
  1434. mac_size = 0;
  1435. }
  1436. enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0, &macbuf, mac_size);
  1437. /*-
  1438. * enc_err is:
  1439. * 0: if the record is publicly invalid, or an internal error, or AEAD
  1440. * decryption failed, or ETM decryption failed.
  1441. * 1: Success or MTE decryption failed (MAC will be randomised)
  1442. */
  1443. if (enc_err == 0) {
  1444. if (ossl_statem_in_error(s)) {
  1445. /* SSLfatal() got called */
  1446. goto end;
  1447. }
  1448. /* For DTLS we simply ignore bad packets. */
  1449. rr->length = 0;
  1450. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1451. goto end;
  1452. }
  1453. OSSL_TRACE_BEGIN(TLS) {
  1454. BIO_printf(trc_out, "dec %zd\n", rr->length);
  1455. BIO_dump_indent(trc_out, rr->data, rr->length, 4);
  1456. } OSSL_TRACE_END(TLS);
  1457. /* r->length is now the compressed data plus mac */
  1458. if ((sess != NULL) && !SSL_READ_ETM(s) &&
  1459. (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
  1460. /* s->read_hash != NULL => mac_size != -1 */
  1461. i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
  1462. if (i == 0 || macbuf.mac == NULL
  1463. || CRYPTO_memcmp(md, macbuf.mac, mac_size) != 0)
  1464. enc_err = 0;
  1465. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
  1466. enc_err = 0;
  1467. }
  1468. if (enc_err == 0) {
  1469. /* decryption failed, silently discard message */
  1470. rr->length = 0;
  1471. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1472. goto end;
  1473. }
  1474. /* r->length is now just compressed */
  1475. if (s->expand != NULL) {
  1476. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
  1477. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
  1478. SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  1479. goto end;
  1480. }
  1481. if (!ssl3_do_uncompress(s, rr)) {
  1482. SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE,
  1483. SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
  1484. goto end;
  1485. }
  1486. }
  1487. /* use current Max Fragment Length setting if applicable */
  1488. if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
  1489. max_plain_length = GET_MAX_FRAGMENT_LENGTH(s->session);
  1490. /* send overflow if the plaintext is too long now it has passed MAC */
  1491. if (rr->length > max_plain_length) {
  1492. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
  1493. SSL_R_DATA_LENGTH_TOO_LONG);
  1494. goto end;
  1495. }
  1496. rr->off = 0;
  1497. /*-
  1498. * So at this point the following is true
  1499. * ssl->s3.rrec.type is the type of record
  1500. * ssl->s3.rrec.length == number of bytes in record
  1501. * ssl->s3.rrec.off == offset to first valid byte
  1502. * ssl->s3.rrec.data == where to take bytes from, increment
  1503. * after use :-).
  1504. */
  1505. /* we have pulled in a full packet so zero things */
  1506. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1507. /* Mark receipt of record. */
  1508. dtls1_record_bitmap_update(s, bitmap);
  1509. ret = 1;
  1510. end:
  1511. if (macbuf.alloced)
  1512. OPENSSL_free(macbuf.mac);
  1513. return ret;
  1514. }
  1515. /*
  1516. * Retrieve a buffered record that belongs to the current epoch, i.e. processed
  1517. */
  1518. #define dtls1_get_processed_record(s) \
  1519. dtls1_retrieve_buffered_record((s), \
  1520. &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
  1521. /*-
  1522. * Call this to get a new input record.
  1523. * It will return <= 0 if more data is needed, normally due to an error
  1524. * or non-blocking IO.
  1525. * When it finishes, one packet has been decoded and can be found in
  1526. * ssl->s3.rrec.type - is the type of record
  1527. * ssl->s3.rrec.data - data
  1528. * ssl->s3.rrec.length - number of bytes
  1529. */
  1530. /* used only by dtls1_read_bytes */
  1531. int dtls1_get_record(SSL *s)
  1532. {
  1533. int ssl_major, ssl_minor;
  1534. int rret;
  1535. size_t more, n;
  1536. SSL3_RECORD *rr;
  1537. unsigned char *p = NULL;
  1538. unsigned short version;
  1539. DTLS1_BITMAP *bitmap;
  1540. unsigned int is_next_epoch;
  1541. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  1542. again:
  1543. /*
  1544. * The epoch may have changed. If so, process all the pending records.
  1545. * This is a non-blocking operation.
  1546. */
  1547. if (!dtls1_process_buffered_records(s)) {
  1548. /* SSLfatal() already called */
  1549. return -1;
  1550. }
  1551. /* if we're renegotiating, then there may be buffered records */
  1552. if (dtls1_get_processed_record(s))
  1553. return 1;
  1554. /* get something from the wire */
  1555. /* check if we have the header */
  1556. if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
  1557. (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
  1558. rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
  1559. SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
  1560. /* read timeout is handled by dtls1_read_bytes */
  1561. if (rret <= 0) {
  1562. /* SSLfatal() already called if appropriate */
  1563. return rret; /* error or non-blocking */
  1564. }
  1565. /* this packet contained a partial record, dump it */
  1566. if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
  1567. DTLS1_RT_HEADER_LENGTH) {
  1568. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1569. goto again;
  1570. }
  1571. RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
  1572. p = RECORD_LAYER_get_packet(&s->rlayer);
  1573. if (s->msg_callback)
  1574. s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
  1575. s, s->msg_callback_arg);
  1576. /* Pull apart the header into the DTLS1_RECORD */
  1577. rr->type = *(p++);
  1578. ssl_major = *(p++);
  1579. ssl_minor = *(p++);
  1580. version = (ssl_major << 8) | ssl_minor;
  1581. /* sequence number is 64 bits, with top 2 bytes = epoch */
  1582. n2s(p, rr->epoch);
  1583. memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
  1584. p += 6;
  1585. n2s(p, rr->length);
  1586. rr->read = 0;
  1587. /*
  1588. * Lets check the version. We tolerate alerts that don't have the exact
  1589. * version number (e.g. because of protocol version errors)
  1590. */
  1591. if (!s->first_packet && rr->type != SSL3_RT_ALERT) {
  1592. if (version != s->version) {
  1593. /* unexpected version, silently discard */
  1594. rr->length = 0;
  1595. rr->read = 1;
  1596. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1597. goto again;
  1598. }
  1599. }
  1600. if ((version & 0xff00) != (s->version & 0xff00)) {
  1601. /* wrong version, silently discard record */
  1602. rr->length = 0;
  1603. rr->read = 1;
  1604. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1605. goto again;
  1606. }
  1607. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  1608. /* record too long, silently discard it */
  1609. rr->length = 0;
  1610. rr->read = 1;
  1611. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1612. goto again;
  1613. }
  1614. /* If received packet overflows own-client Max Fragment Length setting */
  1615. if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
  1616. && rr->length > GET_MAX_FRAGMENT_LENGTH(s->session) + SSL3_RT_MAX_ENCRYPTED_OVERHEAD) {
  1617. /* record too long, silently discard it */
  1618. rr->length = 0;
  1619. rr->read = 1;
  1620. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1621. goto again;
  1622. }
  1623. /* now s->rlayer.rstate == SSL_ST_READ_BODY */
  1624. }
  1625. /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
  1626. if (rr->length >
  1627. RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
  1628. /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
  1629. more = rr->length;
  1630. rret = ssl3_read_n(s, more, more, 1, 1, &n);
  1631. /* this packet contained a partial record, dump it */
  1632. if (rret <= 0 || n != more) {
  1633. if (ossl_statem_in_error(s)) {
  1634. /* ssl3_read_n() called SSLfatal() */
  1635. return -1;
  1636. }
  1637. rr->length = 0;
  1638. rr->read = 1;
  1639. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1640. goto again;
  1641. }
  1642. /*
  1643. * now n == rr->length, and s->packet_length ==
  1644. * DTLS1_RT_HEADER_LENGTH + rr->length
  1645. */
  1646. }
  1647. /* set state for later operations */
  1648. RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
  1649. /* match epochs. NULL means the packet is dropped on the floor */
  1650. bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
  1651. if (bitmap == NULL) {
  1652. rr->length = 0;
  1653. RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
  1654. goto again; /* get another record */
  1655. }
  1656. #ifndef OPENSSL_NO_SCTP
  1657. /* Only do replay check if no SCTP bio */
  1658. if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
  1659. #endif
  1660. /* Check whether this is a repeat, or aged record. */
  1661. /*
  1662. * TODO: Does it make sense to have replay protection in epoch 0 where
  1663. * we have no integrity negotiated yet?
  1664. */
  1665. if (!dtls1_record_replay_check(s, bitmap)) {
  1666. rr->length = 0;
  1667. rr->read = 1;
  1668. RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
  1669. goto again; /* get another record */
  1670. }
  1671. #ifndef OPENSSL_NO_SCTP
  1672. }
  1673. #endif
  1674. /* just read a 0 length packet */
  1675. if (rr->length == 0) {
  1676. rr->read = 1;
  1677. goto again;
  1678. }
  1679. /*
  1680. * If this record is from the next epoch (either HM or ALERT), and a
  1681. * handshake is currently in progress, buffer it since it cannot be
  1682. * processed at this time.
  1683. */
  1684. if (is_next_epoch) {
  1685. if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
  1686. if (dtls1_buffer_record (s,
  1687. &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
  1688. rr->seq_num) < 0) {
  1689. /* SSLfatal() already called */
  1690. return -1;
  1691. }
  1692. }
  1693. rr->length = 0;
  1694. rr->read = 1;
  1695. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1696. goto again;
  1697. }
  1698. if (!dtls1_process_record(s, bitmap)) {
  1699. if (ossl_statem_in_error(s)) {
  1700. /* dtls1_process_record() called SSLfatal */
  1701. return -1;
  1702. }
  1703. rr->length = 0;
  1704. rr->read = 1;
  1705. RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
  1706. goto again; /* get another record */
  1707. }
  1708. return 1;
  1709. }
  1710. int dtls_buffer_listen_record(SSL *s, size_t len, unsigned char *seq, size_t off)
  1711. {
  1712. SSL3_RECORD *rr;
  1713. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  1714. memset(rr, 0, sizeof(SSL3_RECORD));
  1715. rr->length = len;
  1716. rr->type = SSL3_RT_HANDSHAKE;
  1717. memcpy(rr->seq_num, seq, sizeof(rr->seq_num));
  1718. rr->off = off;
  1719. s->rlayer.packet = RECORD_LAYER_get_rbuf(&s->rlayer)->buf;
  1720. s->rlayer.packet_length = DTLS1_RT_HEADER_LENGTH + len;
  1721. rr->data = s->rlayer.packet + DTLS1_RT_HEADER_LENGTH;
  1722. if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
  1723. SSL3_RECORD_get_seq_num(s->rlayer.rrec)) <= 0) {
  1724. /* SSLfatal() already called */
  1725. return 0;
  1726. }
  1727. return 1;
  1728. }