ssl3_record.c 68 KB

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