ssl3_record.c 64 KB

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