ssl3_record.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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;
  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->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->packet_length == SSL3_RT_HEADER_LENGTH
  370. * + thisrr->length, or s->packet_length == SSL2_RT_HEADER_LENGTH
  371. * + thisrr->length and we have that many bytes in s->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->packet' data into 'thisrr' thisrr->input
  382. * points at thisrr->length bytes, which need to be copied into
  383. * thisrr->data by either the decryption or by the decompression When
  384. * the data is 'copied' into the thisrr->data buffer, thisrr->input will
  385. * be pointed 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_DECRYPTION_FAILED, 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 publically invalid (i.e. too
  747. * short etc).
  748. * 1: if the record's padding is valid / the encryption was successful.
  749. * -1: if the record's padding 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 publically 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[9], *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 upto 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. #else
  1381. unsigned char rotated_mac[EVP_MAX_MD_SIZE];
  1382. #endif
  1383. /*
  1384. * mac_end is the index of |rec->data| just after the end of the MAC.
  1385. */
  1386. size_t mac_end = rec->length;
  1387. size_t mac_start = mac_end - md_size;
  1388. size_t in_mac;
  1389. /*
  1390. * scan_start contains the number of bytes that we can ignore because the
  1391. * MAC's position can only vary by 255 bytes.
  1392. */
  1393. size_t scan_start = 0;
  1394. size_t i, j;
  1395. size_t rotate_offset;
  1396. if (!ossl_assert(rec->orig_len >= md_size
  1397. && md_size <= EVP_MAX_MD_SIZE))
  1398. return 0;
  1399. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  1400. rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
  1401. #endif
  1402. /* This information is public so it's safe to branch based on it. */
  1403. if (rec->orig_len > md_size + 255 + 1)
  1404. scan_start = rec->orig_len - (md_size + 255 + 1);
  1405. in_mac = 0;
  1406. rotate_offset = 0;
  1407. memset(rotated_mac, 0, md_size);
  1408. for (i = scan_start, j = 0; i < rec->orig_len; i++) {
  1409. size_t mac_started = constant_time_eq_s(i, mac_start);
  1410. size_t mac_ended = constant_time_lt_s(i, mac_end);
  1411. unsigned char b = rec->data[i];
  1412. in_mac |= mac_started;
  1413. in_mac &= mac_ended;
  1414. rotate_offset |= j & mac_started;
  1415. rotated_mac[j++] |= b & in_mac;
  1416. j &= constant_time_lt_s(j, md_size);
  1417. }
  1418. /* Now rotate the MAC */
  1419. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  1420. j = 0;
  1421. for (i = 0; i < md_size; i++) {
  1422. /* in case cache-line is 32 bytes, touch second line */
  1423. ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
  1424. out[j++] = rotated_mac[rotate_offset++];
  1425. rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
  1426. }
  1427. #else
  1428. memset(out, 0, md_size);
  1429. rotate_offset = md_size - rotate_offset;
  1430. rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
  1431. for (i = 0; i < md_size; i++) {
  1432. for (j = 0; j < md_size; j++)
  1433. out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);
  1434. rotate_offset++;
  1435. rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
  1436. }
  1437. #endif
  1438. return 1;
  1439. }
  1440. int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
  1441. {
  1442. int i;
  1443. int enc_err;
  1444. SSL_SESSION *sess;
  1445. SSL3_RECORD *rr;
  1446. int imac_size;
  1447. size_t mac_size;
  1448. unsigned char md[EVP_MAX_MD_SIZE];
  1449. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  1450. sess = s->session;
  1451. /*
  1452. * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
  1453. * and we have that many bytes in s->packet
  1454. */
  1455. rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
  1456. /*
  1457. * ok, we can now read from 's->packet' data into 'rr' rr->input points
  1458. * at rr->length bytes, which need to be copied into rr->data by either
  1459. * the decryption or by the decompression When the data is 'copied' into
  1460. * the rr->data buffer, rr->input will be pointed at the new buffer
  1461. */
  1462. /*
  1463. * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
  1464. * bytes of encrypted compressed stuff.
  1465. */
  1466. /* check is not needed I believe */
  1467. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  1468. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
  1469. SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  1470. return 0;
  1471. }
  1472. /* decrypt in place in 'rr->input' */
  1473. rr->data = rr->input;
  1474. rr->orig_len = rr->length;
  1475. if (SSL_READ_ETM(s) && s->read_hash) {
  1476. unsigned char *mac;
  1477. mac_size = EVP_MD_CTX_size(s->read_hash);
  1478. if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
  1479. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
  1480. ERR_R_INTERNAL_ERROR);
  1481. return 0;
  1482. }
  1483. if (rr->orig_len < mac_size) {
  1484. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
  1485. SSL_R_LENGTH_TOO_SHORT);
  1486. return 0;
  1487. }
  1488. rr->length -= mac_size;
  1489. mac = rr->data + rr->length;
  1490. i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
  1491. if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
  1492. SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_DTLS1_PROCESS_RECORD,
  1493. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  1494. return 0;
  1495. }
  1496. }
  1497. enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
  1498. /*-
  1499. * enc_err is:
  1500. * 0: (in non-constant time) if the record is publically invalid.
  1501. * 1: if the padding is valid
  1502. * -1: if the padding is invalid
  1503. */
  1504. if (enc_err == 0) {
  1505. if (ossl_statem_in_error(s)) {
  1506. /* SSLfatal() got called */
  1507. return 0;
  1508. }
  1509. /* For DTLS we simply ignore bad packets. */
  1510. rr->length = 0;
  1511. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1512. return 0;
  1513. }
  1514. #ifdef SSL_DEBUG
  1515. printf("dec %ld\n", rr->length);
  1516. {
  1517. size_t z;
  1518. for (z = 0; z < rr->length; z++)
  1519. printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
  1520. }
  1521. printf("\n");
  1522. #endif
  1523. /* r->length is now the compressed data plus mac */
  1524. if ((sess != NULL) && !SSL_READ_ETM(s) &&
  1525. (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
  1526. /* s->read_hash != NULL => mac_size != -1 */
  1527. unsigned char *mac = NULL;
  1528. unsigned char mac_tmp[EVP_MAX_MD_SIZE];
  1529. /* TODO(size_t): Convert this to do size_t properly */
  1530. imac_size = EVP_MD_CTX_size(s->read_hash);
  1531. if (imac_size < 0) {
  1532. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
  1533. ERR_LIB_EVP);
  1534. return 0;
  1535. }
  1536. mac_size = (size_t)imac_size;
  1537. if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
  1538. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
  1539. ERR_R_INTERNAL_ERROR);
  1540. return 0;
  1541. }
  1542. /*
  1543. * orig_len is the length of the record before any padding was
  1544. * removed. This is public information, as is the MAC in use,
  1545. * therefore we can safely process the record in a different amount
  1546. * of time if it's too short to possibly contain a MAC.
  1547. */
  1548. if (rr->orig_len < mac_size ||
  1549. /* CBC records must have a padding length byte too. */
  1550. (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
  1551. rr->orig_len < mac_size + 1)) {
  1552. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
  1553. SSL_R_LENGTH_TOO_SHORT);
  1554. return 0;
  1555. }
  1556. if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
  1557. /*
  1558. * We update the length so that the TLS header bytes can be
  1559. * constructed correctly but we need to extract the MAC in
  1560. * constant time from within the record, without leaking the
  1561. * contents of the padding bytes.
  1562. */
  1563. mac = mac_tmp;
  1564. if (!ssl3_cbc_copy_mac(mac_tmp, rr, mac_size)) {
  1565. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
  1566. ERR_R_INTERNAL_ERROR);
  1567. return 0;
  1568. }
  1569. rr->length -= mac_size;
  1570. } else {
  1571. /*
  1572. * In this case there's no padding, so |rec->orig_len| equals
  1573. * |rec->length| and we checked that there's enough bytes for
  1574. * |mac_size| above.
  1575. */
  1576. rr->length -= mac_size;
  1577. mac = &rr->data[rr->length];
  1578. }
  1579. i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
  1580. if (i == 0 || mac == NULL
  1581. || CRYPTO_memcmp(md, mac, mac_size) != 0)
  1582. enc_err = -1;
  1583. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
  1584. enc_err = -1;
  1585. }
  1586. if (enc_err < 0) {
  1587. /* decryption failed, silently discard message */
  1588. rr->length = 0;
  1589. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1590. return 0;
  1591. }
  1592. /* r->length is now just compressed */
  1593. if (s->expand != NULL) {
  1594. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
  1595. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
  1596. SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  1597. return 0;
  1598. }
  1599. if (!ssl3_do_uncompress(s, rr)) {
  1600. SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE,
  1601. SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
  1602. return 0;
  1603. }
  1604. }
  1605. if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
  1606. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
  1607. SSL_R_DATA_LENGTH_TOO_LONG);
  1608. return 0;
  1609. }
  1610. rr->off = 0;
  1611. /*-
  1612. * So at this point the following is true
  1613. * ssl->s3->rrec.type is the type of record
  1614. * ssl->s3->rrec.length == number of bytes in record
  1615. * ssl->s3->rrec.off == offset to first valid byte
  1616. * ssl->s3->rrec.data == where to take bytes from, increment
  1617. * after use :-).
  1618. */
  1619. /* we have pulled in a full packet so zero things */
  1620. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1621. /* Mark receipt of record. */
  1622. dtls1_record_bitmap_update(s, bitmap);
  1623. return 1;
  1624. }
  1625. /*
  1626. * Retrieve a buffered record that belongs to the current epoch, i.e. processed
  1627. */
  1628. #define dtls1_get_processed_record(s) \
  1629. dtls1_retrieve_buffered_record((s), \
  1630. &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
  1631. /*-
  1632. * Call this to get a new input record.
  1633. * It will return <= 0 if more data is needed, normally due to an error
  1634. * or non-blocking IO.
  1635. * When it finishes, one packet has been decoded and can be found in
  1636. * ssl->s3->rrec.type - is the type of record
  1637. * ssl->s3->rrec.data, - data
  1638. * ssl->s3->rrec.length, - number of bytes
  1639. */
  1640. /* used only by dtls1_read_bytes */
  1641. int dtls1_get_record(SSL *s)
  1642. {
  1643. int ssl_major, ssl_minor;
  1644. int rret;
  1645. size_t more, n;
  1646. SSL3_RECORD *rr;
  1647. unsigned char *p = NULL;
  1648. unsigned short version;
  1649. DTLS1_BITMAP *bitmap;
  1650. unsigned int is_next_epoch;
  1651. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  1652. again:
  1653. /*
  1654. * The epoch may have changed. If so, process all the pending records.
  1655. * This is a non-blocking operation.
  1656. */
  1657. if (!dtls1_process_buffered_records(s)) {
  1658. /* SSLfatal() already called */
  1659. return -1;
  1660. }
  1661. /* if we're renegotiating, then there may be buffered records */
  1662. if (dtls1_get_processed_record(s))
  1663. return 1;
  1664. /* get something from the wire */
  1665. /* check if we have the header */
  1666. if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
  1667. (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
  1668. rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
  1669. SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
  1670. /* read timeout is handled by dtls1_read_bytes */
  1671. if (rret <= 0) {
  1672. /* SSLfatal() already called if appropriate */
  1673. return rret; /* error or non-blocking */
  1674. }
  1675. /* this packet contained a partial record, dump it */
  1676. if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
  1677. DTLS1_RT_HEADER_LENGTH) {
  1678. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1679. goto again;
  1680. }
  1681. RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
  1682. p = RECORD_LAYER_get_packet(&s->rlayer);
  1683. if (s->msg_callback)
  1684. s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
  1685. s, s->msg_callback_arg);
  1686. /* Pull apart the header into the DTLS1_RECORD */
  1687. rr->type = *(p++);
  1688. ssl_major = *(p++);
  1689. ssl_minor = *(p++);
  1690. version = (ssl_major << 8) | ssl_minor;
  1691. /* sequence number is 64 bits, with top 2 bytes = epoch */
  1692. n2s(p, rr->epoch);
  1693. memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
  1694. p += 6;
  1695. n2s(p, rr->length);
  1696. rr->read = 0;
  1697. /*
  1698. * Lets check the version. We tolerate alerts that don't have the exact
  1699. * version number (e.g. because of protocol version errors)
  1700. */
  1701. if (!s->first_packet && rr->type != SSL3_RT_ALERT) {
  1702. if (version != s->version) {
  1703. /* unexpected version, silently discard */
  1704. rr->length = 0;
  1705. rr->read = 1;
  1706. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1707. goto again;
  1708. }
  1709. }
  1710. if ((version & 0xff00) != (s->version & 0xff00)) {
  1711. /* wrong version, silently discard record */
  1712. rr->length = 0;
  1713. rr->read = 1;
  1714. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1715. goto again;
  1716. }
  1717. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  1718. /* record too long, silently discard it */
  1719. rr->length = 0;
  1720. rr->read = 1;
  1721. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1722. goto again;
  1723. }
  1724. /* If received packet overflows own-client Max Fragment Length setting */
  1725. if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
  1726. && rr->length > GET_MAX_FRAGMENT_LENGTH(s->session)) {
  1727. /* record too long, silently discard it */
  1728. rr->length = 0;
  1729. rr->read = 1;
  1730. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1731. goto again;
  1732. }
  1733. /* now s->rlayer.rstate == SSL_ST_READ_BODY */
  1734. }
  1735. /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
  1736. if (rr->length >
  1737. RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
  1738. /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
  1739. more = rr->length;
  1740. rret = ssl3_read_n(s, more, more, 1, 1, &n);
  1741. /* this packet contained a partial record, dump it */
  1742. if (rret <= 0 || n != more) {
  1743. if (ossl_statem_in_error(s)) {
  1744. /* ssl3_read_n() called SSLfatal() */
  1745. return -1;
  1746. }
  1747. rr->length = 0;
  1748. rr->read = 1;
  1749. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1750. goto again;
  1751. }
  1752. /*
  1753. * now n == rr->length, and s->packet_length ==
  1754. * DTLS1_RT_HEADER_LENGTH + rr->length
  1755. */
  1756. }
  1757. /* set state for later operations */
  1758. RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
  1759. /* match epochs. NULL means the packet is dropped on the floor */
  1760. bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
  1761. if (bitmap == NULL) {
  1762. rr->length = 0;
  1763. RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
  1764. goto again; /* get another record */
  1765. }
  1766. #ifndef OPENSSL_NO_SCTP
  1767. /* Only do replay check if no SCTP bio */
  1768. if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
  1769. #endif
  1770. /* Check whether this is a repeat, or aged record. */
  1771. /*
  1772. * TODO: Does it make sense to have replay protection in epoch 0 where
  1773. * we have no integrity negotiated yet?
  1774. */
  1775. if (!dtls1_record_replay_check(s, bitmap)) {
  1776. rr->length = 0;
  1777. rr->read = 1;
  1778. RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
  1779. goto again; /* get another record */
  1780. }
  1781. #ifndef OPENSSL_NO_SCTP
  1782. }
  1783. #endif
  1784. /* just read a 0 length packet */
  1785. if (rr->length == 0) {
  1786. rr->read = 1;
  1787. goto again;
  1788. }
  1789. /*
  1790. * If this record is from the next epoch (either HM or ALERT), and a
  1791. * handshake is currently in progress, buffer it since it cannot be
  1792. * processed at this time.
  1793. */
  1794. if (is_next_epoch) {
  1795. if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
  1796. if (dtls1_buffer_record (s,
  1797. &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
  1798. rr->seq_num) < 0) {
  1799. /* SSLfatal() already called */
  1800. return -1;
  1801. }
  1802. }
  1803. rr->length = 0;
  1804. rr->read = 1;
  1805. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1806. goto again;
  1807. }
  1808. if (!dtls1_process_record(s, bitmap)) {
  1809. if (ossl_statem_in_error(s)) {
  1810. /* dtls1_process_record() called SSLfatal */
  1811. return -1;
  1812. }
  1813. rr->length = 0;
  1814. rr->read = 1;
  1815. RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
  1816. goto again; /* get another record */
  1817. }
  1818. return 1;
  1819. }
  1820. int dtls_buffer_listen_record(SSL *s, size_t len, unsigned char *seq, size_t off)
  1821. {
  1822. SSL3_RECORD *rr;
  1823. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  1824. memset(rr, 0, sizeof(SSL3_RECORD));
  1825. rr->length = len;
  1826. rr->type = SSL3_RT_HANDSHAKE;
  1827. memcpy(rr->seq_num, seq, sizeof(rr->seq_num));
  1828. rr->off = off;
  1829. s->rlayer.packet = RECORD_LAYER_get_rbuf(&s->rlayer)->buf;
  1830. s->rlayer.packet_length = DTLS1_RT_HEADER_LENGTH + len;
  1831. rr->data = s->rlayer.packet + DTLS1_RT_HEADER_LENGTH;
  1832. if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
  1833. SSL3_RECORD_get_seq_num(s->rlayer.rrec)) <= 0) {
  1834. /* SSLfatal() already called */
  1835. return 0;
  1836. }
  1837. return 1;
  1838. }