ssl3_record.c 66 KB

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