ssl3_record.c 72 KB

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