ssl3_record.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  1. /*
  2. * Copyright 1995-2020 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_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
  426. & EVP_CIPH_FLAG_PIPELINE)
  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. /* TODO(size_t): convert this to do size_t properly */
  463. if (s->read_hash != NULL) {
  464. const EVP_MD *tmpmd = EVP_MD_CTX_md(s->read_hash);
  465. if (tmpmd != NULL) {
  466. imac_size = EVP_MD_size(tmpmd);
  467. if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
  468. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_LIB_EVP);
  469. return -1;
  470. }
  471. mac_size = (size_t)imac_size;
  472. }
  473. }
  474. /*
  475. * If in encrypt-then-mac mode calculate mac from encrypted record. All
  476. * the details below are public so no timing details can leak.
  477. */
  478. if (SSL_READ_ETM(s) && s->read_hash) {
  479. unsigned char *mac;
  480. for (j = 0; j < num_recs; j++) {
  481. thisrr = &rr[j];
  482. if (thisrr->length < mac_size) {
  483. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
  484. return -1;
  485. }
  486. thisrr->length -= mac_size;
  487. mac = thisrr->data + thisrr->length;
  488. i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
  489. if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
  490. SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
  491. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  492. return -1;
  493. }
  494. }
  495. /*
  496. * We've handled the mac now - there is no MAC inside the encrypted
  497. * record
  498. */
  499. mac_size = 0;
  500. }
  501. if (mac_size > 0) {
  502. macbufs = OPENSSL_zalloc(sizeof(*macbufs) * num_recs);
  503. if (macbufs == NULL) {
  504. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
  505. return -1;
  506. }
  507. }
  508. enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0, macbufs, mac_size);
  509. /*-
  510. * enc_err is:
  511. * 0: if the record is publicly invalid, or an internal error, or AEAD
  512. * decryption failed, or ETM decryption failed.
  513. * 1: Success or MTE decryption failed (MAC will be randomised)
  514. */
  515. if (enc_err == 0) {
  516. if (ossl_statem_in_error(s)) {
  517. /* SSLfatal() already got called */
  518. goto end;
  519. }
  520. if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
  521. /*
  522. * Valid early_data that we cannot decrypt will fail here. We treat
  523. * it like an empty record.
  524. */
  525. thisrr = &rr[0];
  526. if (!early_data_count_ok(s, thisrr->length,
  527. EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
  528. /* SSLfatal() already called */
  529. goto end;
  530. }
  531. thisrr->length = 0;
  532. thisrr->read = 1;
  533. RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
  534. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  535. ret = 1;
  536. goto end;
  537. }
  538. SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
  539. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  540. goto end;
  541. }
  542. OSSL_TRACE_BEGIN(TLS) {
  543. BIO_printf(trc_out, "dec %lu\n", (unsigned long)rr[0].length);
  544. BIO_dump_indent(trc_out, rr[0].data, rr[0].length, 4);
  545. } OSSL_TRACE_END(TLS);
  546. /* r->length is now the compressed data plus mac */
  547. if ((sess != NULL) &&
  548. (s->enc_read_ctx != NULL) &&
  549. (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)) {
  550. /* s->read_hash != NULL => mac_size != -1 */
  551. for (j = 0; j < num_recs; j++) {
  552. SSL_MAC_BUF *thismb = &macbufs[j];
  553. thisrr = &rr[j];
  554. i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
  555. if (i == 0 || thismb == NULL || thismb->mac == NULL
  556. || CRYPTO_memcmp(md, thismb->mac, (size_t)mac_size) != 0)
  557. enc_err = 0;
  558. if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
  559. enc_err = 0;
  560. }
  561. }
  562. if (enc_err == 0) {
  563. if (ossl_statem_in_error(s)) {
  564. /* We already called SSLfatal() */
  565. goto end;
  566. }
  567. /*
  568. * A separate 'decryption_failed' alert was introduced with TLS 1.0,
  569. * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
  570. * failure is directly visible from the ciphertext anyway, we should
  571. * not reveal which kind of error occurred -- this might become
  572. * visible to an attacker (e.g. via a logfile)
  573. */
  574. SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
  575. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  576. goto end;
  577. }
  578. skip_decryption:
  579. for (j = 0; j < num_recs; j++) {
  580. thisrr = &rr[j];
  581. /* thisrr->length is now just compressed */
  582. if (s->expand != NULL) {
  583. if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
  584. SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
  585. SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  586. goto end;
  587. }
  588. if (!ssl3_do_uncompress(s, thisrr)) {
  589. SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE,
  590. SSL_R_BAD_DECOMPRESSION);
  591. goto end;
  592. }
  593. }
  594. if (SSL_IS_TLS13(s)
  595. && s->enc_read_ctx != NULL
  596. && thisrr->type != SSL3_RT_ALERT) {
  597. size_t end;
  598. if (thisrr->length == 0
  599. || thisrr->type != SSL3_RT_APPLICATION_DATA) {
  600. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
  601. goto end;
  602. }
  603. /* Strip trailing padding */
  604. for (end = thisrr->length - 1; end > 0 && thisrr->data[end] == 0;
  605. end--)
  606. continue;
  607. thisrr->length = end;
  608. thisrr->type = thisrr->data[end];
  609. if (thisrr->type != SSL3_RT_APPLICATION_DATA
  610. && thisrr->type != SSL3_RT_ALERT
  611. && thisrr->type != SSL3_RT_HANDSHAKE) {
  612. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
  613. goto end;
  614. }
  615. if (s->msg_callback)
  616. s->msg_callback(0, s->version, SSL3_RT_INNER_CONTENT_TYPE,
  617. &thisrr->data[end], 1, s, s->msg_callback_arg);
  618. }
  619. /*
  620. * TLSv1.3 alert and handshake records are required to be non-zero in
  621. * length.
  622. */
  623. if (SSL_IS_TLS13(s)
  624. && (thisrr->type == SSL3_RT_HANDSHAKE
  625. || thisrr->type == SSL3_RT_ALERT)
  626. && thisrr->length == 0) {
  627. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_LENGTH);
  628. goto end;
  629. }
  630. if (thisrr->length > SSL3_RT_MAX_PLAIN_LENGTH
  631. && !BIO_get_ktls_recv(s->rbio)) {
  632. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
  633. goto end;
  634. }
  635. /* If received packet overflows current Max Fragment Length setting */
  636. if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
  637. && thisrr->length > GET_MAX_FRAGMENT_LENGTH(s->session)
  638. && !BIO_get_ktls_recv(s->rbio)) {
  639. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
  640. goto end;
  641. }
  642. thisrr->off = 0;
  643. /*-
  644. * So at this point the following is true
  645. * thisrr->type is the type of record
  646. * thisrr->length == number of bytes in record
  647. * thisrr->off == offset to first valid byte
  648. * thisrr->data == where to take bytes from, increment after use :-).
  649. */
  650. /* just read a 0 length packet */
  651. if (thisrr->length == 0) {
  652. RECORD_LAYER_inc_empty_record_count(&s->rlayer);
  653. if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
  654. > MAX_EMPTY_RECORDS) {
  655. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_RECORD_TOO_SMALL);
  656. goto end;
  657. }
  658. } else {
  659. RECORD_LAYER_reset_empty_record_count(&s->rlayer);
  660. }
  661. }
  662. if (s->early_data_state == SSL_EARLY_DATA_READING) {
  663. thisrr = &rr[0];
  664. if (thisrr->type == SSL3_RT_APPLICATION_DATA
  665. && !early_data_count_ok(s, thisrr->length, 0, 0)) {
  666. /* SSLfatal already called */
  667. goto end;
  668. }
  669. }
  670. RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
  671. ret = 1;
  672. end:
  673. if (macbufs != NULL) {
  674. for (j = 0; j < num_recs; j++) {
  675. if (macbufs[j].alloced)
  676. OPENSSL_free(macbufs[j].mac);
  677. }
  678. OPENSSL_free(macbufs);
  679. }
  680. return ret;
  681. }
  682. int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
  683. {
  684. #ifndef OPENSSL_NO_COMP
  685. int i;
  686. if (rr->comp == NULL) {
  687. rr->comp = (unsigned char *)
  688. OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
  689. }
  690. if (rr->comp == NULL)
  691. return 0;
  692. /* TODO(size_t): Convert this call */
  693. i = COMP_expand_block(ssl->expand, rr->comp,
  694. SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
  695. if (i < 0)
  696. return 0;
  697. else
  698. rr->length = i;
  699. rr->data = rr->comp;
  700. #endif
  701. return 1;
  702. }
  703. int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
  704. {
  705. #ifndef OPENSSL_NO_COMP
  706. int i;
  707. /* TODO(size_t): Convert this call */
  708. i = COMP_compress_block(ssl->compress, wr->data,
  709. (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
  710. wr->input, (int)wr->length);
  711. if (i < 0)
  712. return 0;
  713. else
  714. wr->length = i;
  715. wr->input = wr->data;
  716. #endif
  717. return 1;
  718. }
  719. /*-
  720. * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|. Calls SSLfatal on
  721. * internal error, but not otherwise. It is the responsibility of the caller to
  722. * report a bad_record_mac
  723. *
  724. * Returns:
  725. * 0: if the record is publicly invalid, or an internal error
  726. * 1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
  727. */
  728. int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int sending,
  729. SSL_MAC_BUF *mac, size_t macsize)
  730. {
  731. SSL3_RECORD *rec;
  732. EVP_CIPHER_CTX *ds;
  733. size_t l, i;
  734. size_t bs;
  735. const EVP_CIPHER *enc;
  736. rec = inrecs;
  737. /*
  738. * We shouldn't ever be called with more than one record in the SSLv3 case
  739. */
  740. if (n_recs != 1)
  741. return 0;
  742. if (sending) {
  743. ds = s->enc_write_ctx;
  744. if (s->enc_write_ctx == NULL)
  745. enc = NULL;
  746. else
  747. enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
  748. } else {
  749. ds = s->enc_read_ctx;
  750. if (s->enc_read_ctx == NULL)
  751. enc = NULL;
  752. else
  753. enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
  754. }
  755. if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
  756. memmove(rec->data, rec->input, rec->length);
  757. rec->input = rec->data;
  758. } else {
  759. int provided = (EVP_CIPHER_provider(enc) != NULL);
  760. l = rec->length;
  761. /* TODO(size_t): Convert this call */
  762. bs = EVP_CIPHER_CTX_block_size(ds);
  763. /* COMPRESS */
  764. if ((bs != 1) && sending && !provided) {
  765. /*
  766. * We only do this for legacy ciphers. Provided ciphers add the
  767. * padding on the provider side.
  768. */
  769. i = bs - (l % bs);
  770. /* we need to add 'i-1' padding bytes */
  771. l += i;
  772. /*
  773. * the last of these zero bytes will be overwritten with the
  774. * padding length.
  775. */
  776. memset(&rec->input[rec->length], 0, i);
  777. rec->length += i;
  778. rec->input[l - 1] = (unsigned char)(i - 1);
  779. }
  780. if (!sending) {
  781. if (l == 0 || l % bs != 0) {
  782. /* Publicly invalid */
  783. return 0;
  784. }
  785. /* otherwise, rec->length >= bs */
  786. }
  787. if (EVP_CIPHER_provider(enc) != NULL) {
  788. int outlen;
  789. if (!EVP_CipherUpdate(ds, rec->data, &outlen, rec->input,
  790. (unsigned int)l))
  791. return 0;
  792. rec->length = outlen;
  793. if (!sending && mac != NULL) {
  794. /* Now get a pointer to the MAC */
  795. OSSL_PARAM params[2], *p = params;
  796. /* Get the MAC */
  797. mac->alloced = 0;
  798. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
  799. (void **)&mac->mac,
  800. macsize);
  801. *p = OSSL_PARAM_construct_end();
  802. if (!EVP_CIPHER_CTX_get_params(ds, params)) {
  803. /* Shouldn't normally happen */
  804. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  805. return 0;
  806. }
  807. }
  808. } else {
  809. /* TODO(size_t): Convert this call */
  810. if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1) {
  811. /* Shouldn't happen */
  812. SSLfatal(s, SSL_AD_BAD_RECORD_MAC, ERR_R_INTERNAL_ERROR);
  813. return 0;
  814. }
  815. if (!sending)
  816. return ssl3_cbc_remove_padding_and_mac(&rec->length,
  817. rec->orig_len,
  818. rec->data,
  819. (mac != NULL) ? &mac->mac : NULL,
  820. (mac != NULL) ? &mac->alloced : NULL,
  821. bs,
  822. macsize,
  823. s->ctx->libctx);
  824. }
  825. }
  826. return 1;
  827. }
  828. #define MAX_PADDING 256
  829. /*-
  830. * tls1_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
  831. * error, but not otherwise. It is the responsibility of the caller to report
  832. * a bad_record_mac - if appropriate (DTLS just drops the record).
  833. *
  834. * Returns:
  835. * 0: if the record is publicly invalid, or an internal error, or AEAD
  836. * decryption failed, or Encrypt-then-mac decryption failed.
  837. * 1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
  838. */
  839. int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
  840. SSL_MAC_BUF *macs, size_t macsize)
  841. {
  842. EVP_CIPHER_CTX *ds;
  843. size_t reclen[SSL_MAX_PIPELINES];
  844. unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
  845. int i, pad = 0, tmpr;
  846. size_t bs, ctr, padnum, loop;
  847. unsigned char padval;
  848. const EVP_CIPHER *enc;
  849. int tlstree_enc = sending ? (s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
  850. : (s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
  851. if (n_recs == 0) {
  852. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  853. return 0;
  854. }
  855. if (sending) {
  856. if (EVP_MD_CTX_md(s->write_hash)) {
  857. int n = EVP_MD_CTX_size(s->write_hash);
  858. if (!ossl_assert(n >= 0)) {
  859. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  860. return 0;
  861. }
  862. }
  863. ds = s->enc_write_ctx;
  864. if (s->enc_write_ctx == NULL)
  865. enc = NULL;
  866. else {
  867. int ivlen;
  868. enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
  869. /* For TLSv1.1 and later explicit IV */
  870. if (SSL_USE_EXPLICIT_IV(s)
  871. && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
  872. ivlen = EVP_CIPHER_iv_length(enc);
  873. else
  874. ivlen = 0;
  875. if (ivlen > 1) {
  876. for (ctr = 0; ctr < n_recs; ctr++) {
  877. if (recs[ctr].data != recs[ctr].input) {
  878. /*
  879. * we can't write into the input stream: Can this ever
  880. * happen?? (steve)
  881. */
  882. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  883. return 0;
  884. } else if (RAND_bytes_ex(s->ctx->libctx, recs[ctr].input,
  885. ivlen) <= 0) {
  886. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  887. return 0;
  888. }
  889. }
  890. }
  891. }
  892. } else {
  893. if (EVP_MD_CTX_md(s->read_hash)) {
  894. int n = EVP_MD_CTX_size(s->read_hash);
  895. if (!ossl_assert(n >= 0)) {
  896. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  897. return 0;
  898. }
  899. }
  900. ds = s->enc_read_ctx;
  901. if (s->enc_read_ctx == NULL)
  902. enc = NULL;
  903. else
  904. enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
  905. }
  906. if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
  907. for (ctr = 0; ctr < n_recs; ctr++) {
  908. memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
  909. recs[ctr].input = recs[ctr].data;
  910. }
  911. } else {
  912. int provided = (EVP_CIPHER_provider(enc) != NULL);
  913. bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
  914. if (n_recs > 1) {
  915. if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
  916. & EVP_CIPH_FLAG_PIPELINE)) {
  917. /*
  918. * We shouldn't have been called with pipeline data if the
  919. * cipher doesn't support pipelining
  920. */
  921. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
  922. return 0;
  923. }
  924. }
  925. for (ctr = 0; ctr < n_recs; ctr++) {
  926. reclen[ctr] = recs[ctr].length;
  927. if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
  928. & EVP_CIPH_FLAG_AEAD_CIPHER) {
  929. unsigned char *seq;
  930. seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
  931. : RECORD_LAYER_get_read_sequence(&s->rlayer);
  932. if (SSL_IS_DTLS(s)) {
  933. /* DTLS does not support pipelining */
  934. unsigned char dtlsseq[9], *p = dtlsseq;
  935. s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
  936. DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
  937. memcpy(p, &seq[2], 6);
  938. memcpy(buf[ctr], dtlsseq, 8);
  939. } else {
  940. memcpy(buf[ctr], seq, 8);
  941. for (i = 7; i >= 0; i--) { /* increment */
  942. ++seq[i];
  943. if (seq[i] != 0)
  944. break;
  945. }
  946. }
  947. buf[ctr][8] = recs[ctr].type;
  948. buf[ctr][9] = (unsigned char)(s->version >> 8);
  949. buf[ctr][10] = (unsigned char)(s->version);
  950. buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
  951. buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
  952. pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
  953. EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
  954. if (pad <= 0) {
  955. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  956. return 0;
  957. }
  958. if (sending) {
  959. reclen[ctr] += pad;
  960. recs[ctr].length += pad;
  961. }
  962. } else if ((bs != 1) && sending && !provided) {
  963. /*
  964. * We only do this for legacy ciphers. Provided ciphers add the
  965. * padding on the provider side.
  966. */
  967. padnum = bs - (reclen[ctr] % bs);
  968. /* Add weird padding of up to 256 bytes */
  969. if (padnum > MAX_PADDING) {
  970. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  971. return 0;
  972. }
  973. /* we need to add 'padnum' padding bytes of value padval */
  974. padval = (unsigned char)(padnum - 1);
  975. for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
  976. recs[ctr].input[loop] = padval;
  977. reclen[ctr] += padnum;
  978. recs[ctr].length += padnum;
  979. }
  980. if (!sending) {
  981. if (reclen[ctr] == 0 || reclen[ctr] % bs != 0) {
  982. /* Publicly invalid */
  983. return 0;
  984. }
  985. }
  986. }
  987. if (n_recs > 1) {
  988. unsigned char *data[SSL_MAX_PIPELINES];
  989. /* Set the output buffers */
  990. for (ctr = 0; ctr < n_recs; ctr++) {
  991. data[ctr] = recs[ctr].data;
  992. }
  993. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
  994. (int)n_recs, data) <= 0) {
  995. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
  996. return 0;
  997. }
  998. /* Set the input buffers */
  999. for (ctr = 0; ctr < n_recs; ctr++) {
  1000. data[ctr] = recs[ctr].input;
  1001. }
  1002. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
  1003. (int)n_recs, data) <= 0
  1004. || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
  1005. (int)n_recs, reclen) <= 0) {
  1006. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
  1007. return 0;
  1008. }
  1009. }
  1010. if (!SSL_IS_DTLS(s) && tlstree_enc) {
  1011. unsigned char *seq;
  1012. int decrement_seq = 0;
  1013. /*
  1014. * When sending, seq is incremented after MAC calculation.
  1015. * So if we are in ETM mode, we use seq 'as is' in the ctrl-function.
  1016. * Otherwise we have to decrease it in the implementation
  1017. */
  1018. if (sending && !SSL_WRITE_ETM(s))
  1019. decrement_seq = 1;
  1020. seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
  1021. : RECORD_LAYER_get_read_sequence(&s->rlayer);
  1022. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_TLSTREE, decrement_seq, seq) <= 0) {
  1023. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1024. return 0;
  1025. }
  1026. }
  1027. if (provided) {
  1028. int outlen;
  1029. /* Provided cipher - we do not support pipelining on this path */
  1030. if (n_recs > 1) {
  1031. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  1032. return 0;
  1033. }
  1034. if (!EVP_CipherUpdate(ds, recs[0].data, &outlen, recs[0].input,
  1035. (unsigned int)reclen[0]))
  1036. return 0;
  1037. recs[0].length = outlen;
  1038. /*
  1039. * The length returned from EVP_CipherUpdate above is the actual
  1040. * payload length. We need to adjust the data/input ptr to skip over
  1041. * any explicit IV
  1042. */
  1043. if (!sending) {
  1044. if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
  1045. recs[0].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1046. recs[0].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1047. } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
  1048. recs[0].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1049. recs[0].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1050. } else if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
  1051. recs[0].data += bs;
  1052. recs[0].input += bs;
  1053. recs[0].orig_len -= bs;
  1054. }
  1055. /* Now get a pointer to the MAC (if applicable) */
  1056. if (macs != NULL) {
  1057. OSSL_PARAM params[2], *p = params;
  1058. /* Get the MAC */
  1059. macs[0].alloced = 0;
  1060. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
  1061. (void **)&macs[0].mac,
  1062. macsize);
  1063. *p = OSSL_PARAM_construct_end();
  1064. if (!EVP_CIPHER_CTX_get_params(ds, params)) {
  1065. /* Shouldn't normally happen */
  1066. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  1067. ERR_R_INTERNAL_ERROR);
  1068. return 0;
  1069. }
  1070. }
  1071. }
  1072. } else {
  1073. /* Legacy cipher */
  1074. /* TODO(size_t): Convert this call */
  1075. tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
  1076. (unsigned int)reclen[0]);
  1077. if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
  1078. & EVP_CIPH_FLAG_CUSTOM_CIPHER)
  1079. ? (tmpr < 0)
  1080. : (tmpr == 0)) {
  1081. /* AEAD can fail to verify MAC */
  1082. return 0;
  1083. }
  1084. if (!sending) {
  1085. /* Adjust the record to remove the explicit IV/MAC/Tag */
  1086. if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
  1087. for (ctr = 0; ctr < n_recs; ctr++) {
  1088. recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1089. recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1090. recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1091. }
  1092. } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
  1093. for (ctr = 0; ctr < n_recs; ctr++) {
  1094. recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1095. recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1096. recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1097. }
  1098. }
  1099. for (ctr = 0; ctr < n_recs; ctr++) {
  1100. if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
  1101. if (recs[ctr].length < bs)
  1102. return 0;
  1103. recs[ctr].data += bs;
  1104. recs[ctr].input += bs;
  1105. recs[ctr].length -= bs;
  1106. recs[ctr].orig_len -= bs;
  1107. }
  1108. /*
  1109. * If using Mac-then-encrypt, then this will succeed but
  1110. * with a random MAC if padding is invalid
  1111. */
  1112. if (!tls1_cbc_remove_padding_and_mac(&recs[ctr].length,
  1113. recs[ctr].orig_len,
  1114. recs[ctr].data,
  1115. (macs != NULL) ? &macs[ctr].mac : NULL,
  1116. (macs != NULL) ? &macs[ctr].alloced
  1117. : NULL,
  1118. bs,
  1119. macsize,
  1120. (EVP_CIPHER_flags(enc)
  1121. & EVP_CIPH_FLAG_AEAD_CIPHER) != 0,
  1122. s->ctx->libctx))
  1123. return 0;
  1124. }
  1125. if (pad) {
  1126. for (ctr = 0; ctr < n_recs; ctr++) {
  1127. recs[ctr].length -= pad;
  1128. }
  1129. }
  1130. }
  1131. }
  1132. }
  1133. return 1;
  1134. }
  1135. /*
  1136. * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
  1137. * which ssl3_cbc_digest_record supports.
  1138. */
  1139. char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
  1140. {
  1141. switch (EVP_MD_CTX_type(ctx)) {
  1142. case NID_md5:
  1143. case NID_sha1:
  1144. case NID_sha224:
  1145. case NID_sha256:
  1146. case NID_sha384:
  1147. case NID_sha512:
  1148. return 1;
  1149. default:
  1150. return 0;
  1151. }
  1152. }
  1153. int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
  1154. {
  1155. unsigned char *mac_sec, *seq;
  1156. const EVP_MD_CTX *hash;
  1157. unsigned char *p, rec_char;
  1158. size_t md_size;
  1159. size_t npad;
  1160. int t;
  1161. if (sending) {
  1162. mac_sec = &(ssl->s3.write_mac_secret[0]);
  1163. seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
  1164. hash = ssl->write_hash;
  1165. } else {
  1166. mac_sec = &(ssl->s3.read_mac_secret[0]);
  1167. seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
  1168. hash = ssl->read_hash;
  1169. }
  1170. t = EVP_MD_CTX_size(hash);
  1171. if (t < 0)
  1172. return 0;
  1173. md_size = t;
  1174. npad = (48 / md_size) * md_size;
  1175. if (!sending &&
  1176. EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
  1177. ssl3_cbc_record_digest_supported(hash)) {
  1178. #ifdef OPENSSL_NO_DEPRECATED_3_0
  1179. return 0;
  1180. #else
  1181. /*
  1182. * This is a CBC-encrypted record. We must avoid leaking any
  1183. * timing-side channel information about how many blocks of data we
  1184. * are hashing because that gives an attacker a timing-oracle.
  1185. */
  1186. /*-
  1187. * npad is, at most, 48 bytes and that's with MD5:
  1188. * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
  1189. *
  1190. * With SHA-1 (the largest hash speced for SSLv3) the hash size
  1191. * goes up 4, but npad goes down by 8, resulting in a smaller
  1192. * total size.
  1193. */
  1194. unsigned char header[75];
  1195. size_t j = 0;
  1196. memcpy(header + j, mac_sec, md_size);
  1197. j += md_size;
  1198. memcpy(header + j, ssl3_pad_1, npad);
  1199. j += npad;
  1200. memcpy(header + j, seq, 8);
  1201. j += 8;
  1202. header[j++] = rec->type;
  1203. header[j++] = (unsigned char)(rec->length >> 8);
  1204. header[j++] = (unsigned char)(rec->length & 0xff);
  1205. /* Final param == is SSLv3 */
  1206. if (ssl3_cbc_digest_record(EVP_MD_CTX_md(hash),
  1207. md, &md_size,
  1208. header, rec->input,
  1209. rec->length, rec->orig_len,
  1210. mac_sec, md_size, 1) <= 0)
  1211. return 0;
  1212. #endif
  1213. } else {
  1214. unsigned int md_size_u;
  1215. /* Chop the digest off the end :-) */
  1216. EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
  1217. if (md_ctx == NULL)
  1218. return 0;
  1219. rec_char = rec->type;
  1220. p = md;
  1221. s2n(rec->length, p);
  1222. if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
  1223. || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
  1224. || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
  1225. || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
  1226. || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
  1227. || EVP_DigestUpdate(md_ctx, md, 2) <= 0
  1228. || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
  1229. || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
  1230. || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
  1231. || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
  1232. || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
  1233. || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
  1234. || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
  1235. EVP_MD_CTX_free(md_ctx);
  1236. return 0;
  1237. }
  1238. EVP_MD_CTX_free(md_ctx);
  1239. }
  1240. ssl3_record_sequence_update(seq);
  1241. return 1;
  1242. }
  1243. int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
  1244. {
  1245. unsigned char *seq;
  1246. EVP_MD_CTX *hash;
  1247. size_t md_size;
  1248. int i;
  1249. EVP_MD_CTX *hmac = NULL, *mac_ctx;
  1250. unsigned char header[13];
  1251. int stream_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
  1252. : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM);
  1253. int tlstree_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
  1254. : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
  1255. int t;
  1256. if (sending) {
  1257. seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
  1258. hash = ssl->write_hash;
  1259. } else {
  1260. seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
  1261. hash = ssl->read_hash;
  1262. }
  1263. t = EVP_MD_CTX_size(hash);
  1264. if (!ossl_assert(t >= 0))
  1265. return 0;
  1266. md_size = t;
  1267. /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
  1268. if (stream_mac) {
  1269. mac_ctx = hash;
  1270. } else {
  1271. hmac = EVP_MD_CTX_new();
  1272. if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash)) {
  1273. EVP_MD_CTX_free(hmac);
  1274. return 0;
  1275. }
  1276. mac_ctx = hmac;
  1277. }
  1278. if (!SSL_IS_DTLS(ssl) && tlstree_mac && EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_TLSTREE, 0, seq) <= 0) {
  1279. EVP_MD_CTX_free(hmac);
  1280. return 0;
  1281. }
  1282. if (SSL_IS_DTLS(ssl)) {
  1283. unsigned char dtlsseq[8], *p = dtlsseq;
  1284. s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
  1285. DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
  1286. memcpy(p, &seq[2], 6);
  1287. memcpy(header, dtlsseq, 8);
  1288. } else
  1289. memcpy(header, seq, 8);
  1290. header[8] = rec->type;
  1291. header[9] = (unsigned char)(ssl->version >> 8);
  1292. header[10] = (unsigned char)(ssl->version);
  1293. header[11] = (unsigned char)(rec->length >> 8);
  1294. header[12] = (unsigned char)(rec->length & 0xff);
  1295. if (!sending && !SSL_READ_ETM(ssl) &&
  1296. EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
  1297. ssl3_cbc_record_digest_supported(mac_ctx)) {
  1298. OSSL_PARAM tls_hmac_params[2], *p = tls_hmac_params;
  1299. *p++ = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE,
  1300. &rec->orig_len);
  1301. *p++ = OSSL_PARAM_construct_end();
  1302. if (!EVP_PKEY_CTX_set_params(EVP_MD_CTX_pkey_ctx(mac_ctx),
  1303. tls_hmac_params))
  1304. return 0;
  1305. }
  1306. /* TODO(size_t): Convert these calls */
  1307. if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
  1308. || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
  1309. || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
  1310. EVP_MD_CTX_free(hmac);
  1311. return 0;
  1312. }
  1313. EVP_MD_CTX_free(hmac);
  1314. OSSL_TRACE_BEGIN(TLS) {
  1315. BIO_printf(trc_out, "seq:\n");
  1316. BIO_dump_indent(trc_out, seq, 8, 4);
  1317. BIO_printf(trc_out, "rec:\n");
  1318. BIO_dump_indent(trc_out, rec->data, rec->length, 4);
  1319. } OSSL_TRACE_END(TLS);
  1320. if (!SSL_IS_DTLS(ssl)) {
  1321. for (i = 7; i >= 0; i--) {
  1322. ++seq[i];
  1323. if (seq[i] != 0)
  1324. break;
  1325. }
  1326. }
  1327. OSSL_TRACE_BEGIN(TLS) {
  1328. BIO_printf(trc_out, "md:\n");
  1329. BIO_dump_indent(trc_out, md, md_size, 4);
  1330. } OSSL_TRACE_END(TLS);
  1331. return 1;
  1332. }
  1333. int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
  1334. {
  1335. int i;
  1336. int enc_err;
  1337. SSL_SESSION *sess;
  1338. SSL3_RECORD *rr;
  1339. int imac_size;
  1340. size_t mac_size = 0;
  1341. unsigned char md[EVP_MAX_MD_SIZE];
  1342. size_t max_plain_length = SSL3_RT_MAX_PLAIN_LENGTH;
  1343. SSL_MAC_BUF macbuf = { NULL, 0 };
  1344. int ret = 0;
  1345. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  1346. sess = s->session;
  1347. /*
  1348. * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
  1349. * and we have that many bytes in s->packet
  1350. */
  1351. rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
  1352. /*
  1353. * ok, we can now read from 's->packet' data into 'rr' rr->input points
  1354. * at rr->length bytes, which need to be copied into rr->data by either
  1355. * the decryption or by the decompression When the data is 'copied' into
  1356. * the rr->data buffer, rr->input will be pointed at the new buffer
  1357. */
  1358. /*
  1359. * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
  1360. * bytes of encrypted compressed stuff.
  1361. */
  1362. /* check is not needed I believe */
  1363. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  1364. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  1365. return 0;
  1366. }
  1367. /* decrypt in place in 'rr->input' */
  1368. rr->data = rr->input;
  1369. rr->orig_len = rr->length;
  1370. /* TODO(size_t): convert this to do size_t properly */
  1371. if (s->read_hash != NULL) {
  1372. const EVP_MD *tmpmd = EVP_MD_CTX_md(s->read_hash);
  1373. if (tmpmd != NULL) {
  1374. imac_size = EVP_MD_size(tmpmd);
  1375. if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
  1376. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_LIB_EVP);
  1377. return -1;
  1378. }
  1379. mac_size = (size_t)imac_size;
  1380. }
  1381. }
  1382. if (SSL_READ_ETM(s) && s->read_hash) {
  1383. unsigned char *mac;
  1384. if (rr->orig_len < mac_size) {
  1385. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
  1386. return 0;
  1387. }
  1388. rr->length -= mac_size;
  1389. mac = rr->data + rr->length;
  1390. i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
  1391. if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
  1392. SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
  1393. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  1394. return 0;
  1395. }
  1396. /*
  1397. * We've handled the mac now - there is no MAC inside the encrypted
  1398. * record
  1399. */
  1400. mac_size = 0;
  1401. }
  1402. /*
  1403. * Set a mark around the packet decryption attempt. This is DTLS, so
  1404. * bad packets are just ignored, and we don't want to leave stray
  1405. * errors in the queue from processing bogus junk that we ignored.
  1406. */
  1407. ERR_set_mark();
  1408. enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0, &macbuf, mac_size);
  1409. /*-
  1410. * enc_err is:
  1411. * 0: if the record is publicly invalid, or an internal error, or AEAD
  1412. * decryption failed, or ETM decryption failed.
  1413. * 1: Success or MTE decryption failed (MAC will be randomised)
  1414. */
  1415. if (enc_err == 0) {
  1416. ERR_pop_to_mark();
  1417. if (ossl_statem_in_error(s)) {
  1418. /* SSLfatal() got called */
  1419. goto end;
  1420. }
  1421. /* For DTLS we simply ignore bad packets. */
  1422. rr->length = 0;
  1423. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1424. goto end;
  1425. }
  1426. ERR_clear_last_mark();
  1427. OSSL_TRACE_BEGIN(TLS) {
  1428. BIO_printf(trc_out, "dec %zd\n", rr->length);
  1429. BIO_dump_indent(trc_out, rr->data, rr->length, 4);
  1430. } OSSL_TRACE_END(TLS);
  1431. /* r->length is now the compressed data plus mac */
  1432. if ((sess != NULL) && !SSL_READ_ETM(s) &&
  1433. (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
  1434. /* s->read_hash != NULL => mac_size != -1 */
  1435. i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
  1436. if (i == 0 || macbuf.mac == NULL
  1437. || CRYPTO_memcmp(md, macbuf.mac, mac_size) != 0)
  1438. enc_err = 0;
  1439. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
  1440. enc_err = 0;
  1441. }
  1442. if (enc_err == 0) {
  1443. /* decryption failed, silently discard message */
  1444. rr->length = 0;
  1445. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1446. goto end;
  1447. }
  1448. /* r->length is now just compressed */
  1449. if (s->expand != NULL) {
  1450. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
  1451. SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
  1452. SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  1453. goto end;
  1454. }
  1455. if (!ssl3_do_uncompress(s, rr)) {
  1456. SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE, SSL_R_BAD_DECOMPRESSION);
  1457. goto end;
  1458. }
  1459. }
  1460. /* use current Max Fragment Length setting if applicable */
  1461. if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
  1462. max_plain_length = GET_MAX_FRAGMENT_LENGTH(s->session);
  1463. /* send overflow if the plaintext is too long now it has passed MAC */
  1464. if (rr->length > max_plain_length) {
  1465. SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
  1466. goto end;
  1467. }
  1468. rr->off = 0;
  1469. /*-
  1470. * So at this point the following is true
  1471. * ssl->s3.rrec.type is the type of record
  1472. * ssl->s3.rrec.length == number of bytes in record
  1473. * ssl->s3.rrec.off == offset to first valid byte
  1474. * ssl->s3.rrec.data == where to take bytes from, increment
  1475. * after use :-).
  1476. */
  1477. /* we have pulled in a full packet so zero things */
  1478. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1479. /* Mark receipt of record. */
  1480. dtls1_record_bitmap_update(s, bitmap);
  1481. ret = 1;
  1482. end:
  1483. if (macbuf.alloced)
  1484. OPENSSL_free(macbuf.mac);
  1485. return ret;
  1486. }
  1487. /*
  1488. * Retrieve a buffered record that belongs to the current epoch, i.e. processed
  1489. */
  1490. #define dtls1_get_processed_record(s) \
  1491. dtls1_retrieve_buffered_record((s), \
  1492. &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
  1493. /*-
  1494. * Call this to get a new input record.
  1495. * It will return <= 0 if more data is needed, normally due to an error
  1496. * or non-blocking IO.
  1497. * When it finishes, one packet has been decoded and can be found in
  1498. * ssl->s3.rrec.type - is the type of record
  1499. * ssl->s3.rrec.data - data
  1500. * ssl->s3.rrec.length - number of bytes
  1501. */
  1502. /* used only by dtls1_read_bytes */
  1503. int dtls1_get_record(SSL *s)
  1504. {
  1505. int ssl_major, ssl_minor;
  1506. int rret;
  1507. size_t more, n;
  1508. SSL3_RECORD *rr;
  1509. unsigned char *p = NULL;
  1510. unsigned short version;
  1511. DTLS1_BITMAP *bitmap;
  1512. unsigned int is_next_epoch;
  1513. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  1514. again:
  1515. /*
  1516. * The epoch may have changed. If so, process all the pending records.
  1517. * This is a non-blocking operation.
  1518. */
  1519. if (!dtls1_process_buffered_records(s)) {
  1520. /* SSLfatal() already called */
  1521. return -1;
  1522. }
  1523. /* if we're renegotiating, then there may be buffered records */
  1524. if (dtls1_get_processed_record(s))
  1525. return 1;
  1526. /* get something from the wire */
  1527. /* check if we have the header */
  1528. if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
  1529. (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
  1530. rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
  1531. SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
  1532. /* read timeout is handled by dtls1_read_bytes */
  1533. if (rret <= 0) {
  1534. /* SSLfatal() already called if appropriate */
  1535. return rret; /* error or non-blocking */
  1536. }
  1537. /* this packet contained a partial record, dump it */
  1538. if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
  1539. DTLS1_RT_HEADER_LENGTH) {
  1540. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1541. goto again;
  1542. }
  1543. RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
  1544. p = RECORD_LAYER_get_packet(&s->rlayer);
  1545. if (s->msg_callback)
  1546. s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
  1547. s, s->msg_callback_arg);
  1548. /* Pull apart the header into the DTLS1_RECORD */
  1549. rr->type = *(p++);
  1550. ssl_major = *(p++);
  1551. ssl_minor = *(p++);
  1552. version = (ssl_major << 8) | ssl_minor;
  1553. /* sequence number is 64 bits, with top 2 bytes = epoch */
  1554. n2s(p, rr->epoch);
  1555. memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
  1556. p += 6;
  1557. n2s(p, rr->length);
  1558. rr->read = 0;
  1559. /*
  1560. * Lets check the version. We tolerate alerts that don't have the exact
  1561. * version number (e.g. because of protocol version errors)
  1562. */
  1563. if (!s->first_packet && rr->type != SSL3_RT_ALERT) {
  1564. if (version != s->version) {
  1565. /* unexpected version, silently discard */
  1566. rr->length = 0;
  1567. rr->read = 1;
  1568. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1569. goto again;
  1570. }
  1571. }
  1572. if ((version & 0xff00) != (s->version & 0xff00)) {
  1573. /* wrong version, silently discard record */
  1574. rr->length = 0;
  1575. rr->read = 1;
  1576. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1577. goto again;
  1578. }
  1579. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  1580. /* record too long, silently discard it */
  1581. rr->length = 0;
  1582. rr->read = 1;
  1583. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1584. goto again;
  1585. }
  1586. /* If received packet overflows own-client Max Fragment Length setting */
  1587. if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
  1588. && rr->length > GET_MAX_FRAGMENT_LENGTH(s->session) + SSL3_RT_MAX_ENCRYPTED_OVERHEAD) {
  1589. /* record too long, silently discard it */
  1590. rr->length = 0;
  1591. rr->read = 1;
  1592. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1593. goto again;
  1594. }
  1595. /* now s->rlayer.rstate == SSL_ST_READ_BODY */
  1596. }
  1597. /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
  1598. if (rr->length >
  1599. RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
  1600. /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
  1601. more = rr->length;
  1602. rret = ssl3_read_n(s, more, more, 1, 1, &n);
  1603. /* this packet contained a partial record, dump it */
  1604. if (rret <= 0 || n != more) {
  1605. if (ossl_statem_in_error(s)) {
  1606. /* ssl3_read_n() called SSLfatal() */
  1607. return -1;
  1608. }
  1609. rr->length = 0;
  1610. rr->read = 1;
  1611. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1612. goto again;
  1613. }
  1614. /*
  1615. * now n == rr->length, and s->packet_length ==
  1616. * DTLS1_RT_HEADER_LENGTH + rr->length
  1617. */
  1618. }
  1619. /* set state for later operations */
  1620. RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
  1621. /* match epochs. NULL means the packet is dropped on the floor */
  1622. bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
  1623. if (bitmap == NULL) {
  1624. rr->length = 0;
  1625. RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
  1626. goto again; /* get another record */
  1627. }
  1628. #ifndef OPENSSL_NO_SCTP
  1629. /* Only do replay check if no SCTP bio */
  1630. if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
  1631. #endif
  1632. /* Check whether this is a repeat, or aged record. */
  1633. /*
  1634. * TODO: Does it make sense to have replay protection in epoch 0 where
  1635. * we have no integrity negotiated yet?
  1636. */
  1637. if (!dtls1_record_replay_check(s, bitmap)) {
  1638. rr->length = 0;
  1639. rr->read = 1;
  1640. RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
  1641. goto again; /* get another record */
  1642. }
  1643. #ifndef OPENSSL_NO_SCTP
  1644. }
  1645. #endif
  1646. /* just read a 0 length packet */
  1647. if (rr->length == 0) {
  1648. rr->read = 1;
  1649. goto again;
  1650. }
  1651. /*
  1652. * If this record is from the next epoch (either HM or ALERT), and a
  1653. * handshake is currently in progress, buffer it since it cannot be
  1654. * processed at this time.
  1655. */
  1656. if (is_next_epoch) {
  1657. if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
  1658. if (dtls1_buffer_record (s,
  1659. &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
  1660. rr->seq_num) < 0) {
  1661. /* SSLfatal() already called */
  1662. return -1;
  1663. }
  1664. }
  1665. rr->length = 0;
  1666. rr->read = 1;
  1667. RECORD_LAYER_reset_packet_length(&s->rlayer);
  1668. goto again;
  1669. }
  1670. if (!dtls1_process_record(s, bitmap)) {
  1671. if (ossl_statem_in_error(s)) {
  1672. /* dtls1_process_record() called SSLfatal */
  1673. return -1;
  1674. }
  1675. rr->length = 0;
  1676. rr->read = 1;
  1677. RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
  1678. goto again; /* get another record */
  1679. }
  1680. return 1;
  1681. }
  1682. int dtls_buffer_listen_record(SSL *s, size_t len, unsigned char *seq, size_t off)
  1683. {
  1684. SSL3_RECORD *rr;
  1685. rr = RECORD_LAYER_get_rrec(&s->rlayer);
  1686. memset(rr, 0, sizeof(SSL3_RECORD));
  1687. rr->length = len;
  1688. rr->type = SSL3_RT_HANDSHAKE;
  1689. memcpy(rr->seq_num, seq, sizeof(rr->seq_num));
  1690. rr->off = off;
  1691. s->rlayer.packet = RECORD_LAYER_get_rbuf(&s->rlayer)->buf;
  1692. s->rlayer.packet_length = DTLS1_RT_HEADER_LENGTH + len;
  1693. rr->data = s->rlayer.packet + DTLS1_RT_HEADER_LENGTH;
  1694. if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
  1695. SSL3_RECORD_get_seq_num(s->rlayer.rrec)) <= 0) {
  1696. /* SSLfatal() already called */
  1697. return 0;
  1698. }
  1699. return 1;
  1700. }