ssl3_record.c 70 KB

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