s3_pkt.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /* ssl/s3_pkt.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2019 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * [email protected].
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <limits.h>
  113. #include <errno.h>
  114. #define USE_SOCKETS
  115. #include "ssl_locl.h"
  116. #include <openssl/evp.h>
  117. #include <openssl/buffer.h>
  118. #include <openssl/rand.h>
  119. #ifndef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  120. # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
  121. #endif
  122. #if defined(OPENSSL_SMALL_FOOTPRINT) || \
  123. !( defined(AES_ASM) && ( \
  124. defined(__x86_64) || defined(__x86_64__) || \
  125. defined(_M_AMD64) || defined(_M_X64) || \
  126. defined(__INTEL__) ) \
  127. )
  128. # undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  129. # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
  130. #endif
  131. static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
  132. unsigned int len, int create_empty_fragment);
  133. static int ssl3_get_record(SSL *s);
  134. /*
  135. * Return values are as per SSL_read()
  136. */
  137. int ssl3_read_n(SSL *s, int n, int max, int extend)
  138. {
  139. /*
  140. * If extend == 0, obtain new n-byte packet; if extend == 1, increase
  141. * packet by another n bytes. The packet will be in the sub-array of
  142. * s->s3->rbuf.buf specified by s->packet and s->packet_length. (If
  143. * s->read_ahead is set, 'max' bytes may be stored in rbuf [plus
  144. * s->packet_length bytes if extend == 1].)
  145. */
  146. int i, len, left;
  147. long align = 0;
  148. unsigned char *pkt;
  149. SSL3_BUFFER *rb;
  150. if (n <= 0)
  151. return n;
  152. rb = &(s->s3->rbuf);
  153. if (rb->buf == NULL)
  154. if (!ssl3_setup_read_buffer(s))
  155. return -1;
  156. left = rb->left;
  157. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  158. align = (long)rb->buf + SSL3_RT_HEADER_LENGTH;
  159. align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
  160. #endif
  161. if (!extend) {
  162. /* start with empty packet ... */
  163. if (left == 0)
  164. rb->offset = align;
  165. else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
  166. /*
  167. * check if next packet length is large enough to justify payload
  168. * alignment...
  169. */
  170. pkt = rb->buf + rb->offset;
  171. if (pkt[0] == SSL3_RT_APPLICATION_DATA
  172. && (pkt[3] << 8 | pkt[4]) >= 128) {
  173. /*
  174. * Note that even if packet is corrupted and its length field
  175. * is insane, we can only be led to wrong decision about
  176. * whether memmove will occur or not. Header values has no
  177. * effect on memmove arguments and therefore no buffer
  178. * overrun can be triggered.
  179. */
  180. memmove(rb->buf + align, pkt, left);
  181. rb->offset = align;
  182. }
  183. }
  184. s->packet = rb->buf + rb->offset;
  185. s->packet_length = 0;
  186. /* ... now we can act as if 'extend' was set */
  187. }
  188. /*
  189. * For DTLS/UDP reads should not span multiple packets because the read
  190. * operation returns the whole packet at once (as long as it fits into
  191. * the buffer).
  192. */
  193. if (SSL_IS_DTLS(s)) {
  194. if (left == 0 && extend)
  195. return 0;
  196. if (left > 0 && n > left)
  197. n = left;
  198. }
  199. /* if there is enough in the buffer from a previous read, take some */
  200. if (left >= n) {
  201. s->packet_length += n;
  202. rb->left = left - n;
  203. rb->offset += n;
  204. return (n);
  205. }
  206. /* else we need to read more data */
  207. len = s->packet_length;
  208. pkt = rb->buf + align;
  209. /*
  210. * Move any available bytes to front of buffer: 'len' bytes already
  211. * pointed to by 'packet', 'left' extra ones at the end
  212. */
  213. if (s->packet != pkt) { /* len > 0 */
  214. memmove(pkt, s->packet, len + left);
  215. s->packet = pkt;
  216. rb->offset = len + align;
  217. }
  218. if (n > (int)(rb->len - rb->offset)) { /* does not happen */
  219. SSLerr(SSL_F_SSL3_READ_N, ERR_R_INTERNAL_ERROR);
  220. return -1;
  221. }
  222. /* We always act like read_ahead is set for DTLS */
  223. if (!s->read_ahead && !SSL_IS_DTLS(s))
  224. /* ignore max parameter */
  225. max = n;
  226. else {
  227. if (max < n)
  228. max = n;
  229. if (max > (int)(rb->len - rb->offset))
  230. max = rb->len - rb->offset;
  231. }
  232. while (left < n) {
  233. /*
  234. * Now we have len+left bytes at the front of s->s3->rbuf.buf and
  235. * need to read in more until we have len+n (up to len+max if
  236. * possible)
  237. */
  238. clear_sys_error();
  239. if (s->rbio != NULL) {
  240. s->rwstate = SSL_READING;
  241. i = BIO_read(s->rbio, pkt + len + left, max - left);
  242. } else {
  243. SSLerr(SSL_F_SSL3_READ_N, SSL_R_READ_BIO_NOT_SET);
  244. i = -1;
  245. }
  246. if (i <= 0) {
  247. rb->left = left;
  248. if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
  249. if (len + left == 0)
  250. ssl3_release_read_buffer(s);
  251. return (i);
  252. }
  253. left += i;
  254. /*
  255. * reads should *never* span multiple packets for DTLS because the
  256. * underlying transport protocol is message oriented as opposed to
  257. * byte oriented as in the TLS case.
  258. */
  259. if (SSL_IS_DTLS(s)) {
  260. if (n > left)
  261. n = left; /* makes the while condition false */
  262. }
  263. }
  264. /* done reading, now the book-keeping */
  265. rb->offset += n;
  266. rb->left = left - n;
  267. s->packet_length += n;
  268. s->rwstate = SSL_NOTHING;
  269. return (n);
  270. }
  271. /*
  272. * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
  273. * will be processed per call to ssl3_get_record. Without this limit an
  274. * attacker could send empty records at a faster rate than we can process and
  275. * cause ssl3_get_record to loop forever.
  276. */
  277. #define MAX_EMPTY_RECORDS 32
  278. /*-
  279. * Call this to get a new input record.
  280. * It will return <= 0 if more data is needed, normally due to an error
  281. * or non-blocking IO.
  282. * When it finishes, one packet has been decoded and can be found in
  283. * ssl->s3->rrec.type - is the type of record
  284. * ssl->s3->rrec.data, - data
  285. * ssl->s3->rrec.length, - number of bytes
  286. */
  287. /* used only by ssl3_read_bytes */
  288. static int ssl3_get_record(SSL *s)
  289. {
  290. int ssl_major, ssl_minor, al;
  291. int enc_err, n, i, ret = -1;
  292. SSL3_RECORD *rr;
  293. SSL_SESSION *sess;
  294. unsigned char *p;
  295. unsigned char md[EVP_MAX_MD_SIZE];
  296. short version;
  297. unsigned mac_size, orig_len;
  298. size_t extra;
  299. unsigned empty_record_count = 0;
  300. rr = &(s->s3->rrec);
  301. sess = s->session;
  302. if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
  303. extra = SSL3_RT_MAX_EXTRA;
  304. else
  305. extra = 0;
  306. if (extra && !s->s3->init_extra) {
  307. /*
  308. * An application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER set after
  309. * ssl3_setup_buffers() was done
  310. */
  311. SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
  312. return -1;
  313. }
  314. again:
  315. /* check if we have the header */
  316. if ((s->rstate != SSL_ST_READ_BODY) ||
  317. (s->packet_length < SSL3_RT_HEADER_LENGTH)) {
  318. n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
  319. if (n <= 0)
  320. return (n); /* error or non-blocking */
  321. s->rstate = SSL_ST_READ_BODY;
  322. p = s->packet;
  323. if (s->msg_callback)
  324. s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
  325. s->msg_callback_arg);
  326. /* Pull apart the header into the SSL3_RECORD */
  327. rr->type = *(p++);
  328. ssl_major = *(p++);
  329. ssl_minor = *(p++);
  330. version = (ssl_major << 8) | ssl_minor;
  331. n2s(p, rr->length);
  332. #if 0
  333. fprintf(stderr, "Record type=%d, Length=%d\n", rr->type, rr->length);
  334. #endif
  335. /* Lets check version */
  336. if (!s->first_packet) {
  337. if (version != s->version) {
  338. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
  339. if ((s->version & 0xFF00) == (version & 0xFF00)
  340. && !s->enc_write_ctx && !s->write_hash) {
  341. if (rr->type == SSL3_RT_ALERT) {
  342. /*
  343. * The record is using an incorrect version number, but
  344. * what we've got appears to be an alert. We haven't
  345. * read the body yet to check whether its a fatal or
  346. * not - but chances are it is. We probably shouldn't
  347. * send a fatal alert back. We'll just end.
  348. */
  349. goto err;
  350. }
  351. /*
  352. * Send back error using their minor version number :-)
  353. */
  354. s->version = (unsigned short)version;
  355. }
  356. al = SSL_AD_PROTOCOL_VERSION;
  357. goto f_err;
  358. }
  359. }
  360. if ((version >> 8) != SSL3_VERSION_MAJOR) {
  361. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
  362. goto err;
  363. }
  364. if (rr->length > s->s3->rbuf.len - SSL3_RT_HEADER_LENGTH) {
  365. al = SSL_AD_RECORD_OVERFLOW;
  366. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
  367. goto f_err;
  368. }
  369. /* now s->rstate == SSL_ST_READ_BODY */
  370. }
  371. /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
  372. if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
  373. /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
  374. i = rr->length;
  375. n = ssl3_read_n(s, i, i, 1);
  376. if (n <= 0)
  377. return (n); /* error or non-blocking io */
  378. /*
  379. * now n == rr->length, and s->packet_length == SSL3_RT_HEADER_LENGTH
  380. * + rr->length
  381. */
  382. }
  383. s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
  384. /*
  385. * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
  386. * and we have that many bytes in s->packet
  387. */
  388. rr->input = &(s->packet[SSL3_RT_HEADER_LENGTH]);
  389. /*
  390. * ok, we can now read from 's->packet' data into 'rr' rr->input points
  391. * at rr->length bytes, which need to be copied into rr->data by either
  392. * the decryption or by the decompression When the data is 'copied' into
  393. * the rr->data buffer, rr->input will be pointed at the new buffer
  394. */
  395. /*
  396. * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
  397. * bytes of encrypted compressed stuff.
  398. */
  399. /* check is not needed I believe */
  400. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
  401. al = SSL_AD_RECORD_OVERFLOW;
  402. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  403. goto f_err;
  404. }
  405. /* decrypt in place in 'rr->input' */
  406. rr->data = rr->input;
  407. enc_err = s->method->ssl3_enc->enc(s, 0);
  408. /*-
  409. * enc_err is:
  410. * 0: (in non-constant time) if the record is publically invalid.
  411. * 1: if the padding is valid
  412. * -1: if the padding is invalid
  413. */
  414. if (enc_err == 0) {
  415. al = SSL_AD_DECRYPTION_FAILED;
  416. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
  417. goto f_err;
  418. }
  419. #ifdef TLS_DEBUG
  420. printf("dec %d\n", rr->length);
  421. {
  422. unsigned int z;
  423. for (z = 0; z < rr->length; z++)
  424. printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
  425. }
  426. printf("\n");
  427. #endif
  428. /* r->length is now the compressed data plus mac */
  429. if ((sess != NULL) &&
  430. (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
  431. /* s->read_hash != NULL => mac_size != -1 */
  432. unsigned char *mac = NULL;
  433. unsigned char mac_tmp[EVP_MAX_MD_SIZE];
  434. mac_size = EVP_MD_CTX_size(s->read_hash);
  435. OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
  436. /*
  437. * kludge: *_cbc_remove_padding passes padding length in rr->type
  438. */
  439. orig_len = rr->length + ((unsigned int)rr->type >> 8);
  440. /*
  441. * orig_len is the length of the record before any padding was
  442. * removed. This is public information, as is the MAC in use,
  443. * therefore we can safely process the record in a different amount
  444. * of time if it's too short to possibly contain a MAC.
  445. */
  446. if (orig_len < mac_size ||
  447. /* CBC records must have a padding length byte too. */
  448. (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
  449. orig_len < mac_size + 1)) {
  450. al = SSL_AD_DECODE_ERROR;
  451. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
  452. goto f_err;
  453. }
  454. if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
  455. /*
  456. * We update the length so that the TLS header bytes can be
  457. * constructed correctly but we need to extract the MAC in
  458. * constant time from within the record, without leaking the
  459. * contents of the padding bytes.
  460. */
  461. mac = mac_tmp;
  462. ssl3_cbc_copy_mac(mac_tmp, rr, mac_size, orig_len);
  463. rr->length -= mac_size;
  464. } else {
  465. /*
  466. * In this case there's no padding, so |orig_len| equals
  467. * |rec->length| and we checked that there's enough bytes for
  468. * |mac_size| above.
  469. */
  470. rr->length -= mac_size;
  471. mac = &rr->data[rr->length];
  472. }
  473. i = s->method->ssl3_enc->mac(s, md, 0 /* not send */ );
  474. if (i < 0 || mac == NULL
  475. || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
  476. enc_err = -1;
  477. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + extra + mac_size)
  478. enc_err = -1;
  479. }
  480. if (enc_err < 0) {
  481. /*
  482. * A separate 'decryption_failed' alert was introduced with TLS 1.0,
  483. * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
  484. * failure is directly visible from the ciphertext anyway, we should
  485. * not reveal which kind of error occured -- this might become
  486. * visible to an attacker (e.g. via a logfile)
  487. */
  488. al = SSL_AD_BAD_RECORD_MAC;
  489. SSLerr(SSL_F_SSL3_GET_RECORD,
  490. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  491. goto f_err;
  492. }
  493. /* r->length is now just compressed */
  494. if (s->expand != NULL) {
  495. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + extra) {
  496. al = SSL_AD_RECORD_OVERFLOW;
  497. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  498. goto f_err;
  499. }
  500. if (!ssl3_do_uncompress(s)) {
  501. al = SSL_AD_DECOMPRESSION_FAILURE;
  502. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
  503. goto f_err;
  504. }
  505. }
  506. if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
  507. al = SSL_AD_RECORD_OVERFLOW;
  508. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
  509. goto f_err;
  510. }
  511. rr->off = 0;
  512. /*-
  513. * So at this point the following is true
  514. * ssl->s3->rrec.type is the type of record
  515. * ssl->s3->rrec.length == number of bytes in record
  516. * ssl->s3->rrec.off == offset to first valid byte
  517. * ssl->s3->rrec.data == where to take bytes from, increment
  518. * after use :-).
  519. */
  520. /* we have pulled in a full packet so zero things */
  521. s->packet_length = 0;
  522. /* just read a 0 length packet */
  523. if (rr->length == 0) {
  524. empty_record_count++;
  525. if (empty_record_count > MAX_EMPTY_RECORDS) {
  526. al = SSL_AD_UNEXPECTED_MESSAGE;
  527. SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
  528. goto f_err;
  529. }
  530. goto again;
  531. }
  532. #if 0
  533. fprintf(stderr, "Ultimate Record type=%d, Length=%d\n", rr->type,
  534. rr->length);
  535. #endif
  536. return (1);
  537. f_err:
  538. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  539. err:
  540. return (ret);
  541. }
  542. int ssl3_do_uncompress(SSL *ssl)
  543. {
  544. #ifndef OPENSSL_NO_COMP
  545. int i;
  546. SSL3_RECORD *rr;
  547. rr = &(ssl->s3->rrec);
  548. i = COMP_expand_block(ssl->expand, rr->comp,
  549. SSL3_RT_MAX_PLAIN_LENGTH, rr->data,
  550. (int)rr->length);
  551. if (i < 0)
  552. return (0);
  553. else
  554. rr->length = i;
  555. rr->data = rr->comp;
  556. #endif
  557. return (1);
  558. }
  559. int ssl3_do_compress(SSL *ssl)
  560. {
  561. #ifndef OPENSSL_NO_COMP
  562. int i;
  563. SSL3_RECORD *wr;
  564. wr = &(ssl->s3->wrec);
  565. i = COMP_compress_block(ssl->compress, wr->data,
  566. SSL3_RT_MAX_COMPRESSED_LENGTH,
  567. wr->input, (int)wr->length);
  568. if (i < 0)
  569. return (0);
  570. else
  571. wr->length = i;
  572. wr->input = wr->data;
  573. #endif
  574. return (1);
  575. }
  576. /*
  577. * Call this to write data in records of type 'type' It will return <= 0 if
  578. * not all data has been sent or non-blocking IO.
  579. */
  580. int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
  581. {
  582. const unsigned char *buf = buf_;
  583. int tot;
  584. unsigned int n, nw;
  585. #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  586. unsigned int max_send_fragment;
  587. #endif
  588. SSL3_BUFFER *wb = &(s->s3->wbuf);
  589. int i;
  590. s->rwstate = SSL_NOTHING;
  591. OPENSSL_assert(s->s3->wnum <= INT_MAX);
  592. tot = s->s3->wnum;
  593. s->s3->wnum = 0;
  594. if (SSL_in_init(s) && !s->in_handshake) {
  595. i = s->handshake_func(s);
  596. if (i < 0)
  597. return (i);
  598. if (i == 0) {
  599. SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  600. return -1;
  601. }
  602. }
  603. /*
  604. * ensure that if we end up with a smaller value of data to write out
  605. * than the the original len from a write which didn't complete for
  606. * non-blocking I/O and also somehow ended up avoiding the check for
  607. * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
  608. * possible to end up with (len-tot) as a large number that will then
  609. * promptly send beyond the end of the users buffer ... so we trap and
  610. * report the error in a way the user will notice
  611. */
  612. if ((len < tot) || ((wb->left != 0) && (len < (tot + s->s3->wpend_tot)))) {
  613. SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_BAD_LENGTH);
  614. return (-1);
  615. }
  616. /*
  617. * first check if there is a SSL3_BUFFER still being written out. This
  618. * will happen with non blocking IO
  619. */
  620. if (wb->left != 0) {
  621. i = ssl3_write_pending(s, type, &buf[tot], s->s3->wpend_tot);
  622. if (i <= 0) {
  623. /* XXX should we ssl3_release_write_buffer if i<0? */
  624. s->s3->wnum = tot;
  625. return i;
  626. }
  627. tot += i; /* this might be last fragment */
  628. }
  629. #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  630. /*
  631. * Depending on platform multi-block can deliver several *times*
  632. * better performance. Downside is that it has to allocate
  633. * jumbo buffer to accomodate up to 8 records, but the
  634. * compromise is considered worthy.
  635. */
  636. if (type == SSL3_RT_APPLICATION_DATA &&
  637. len >= 4 * (int)(max_send_fragment = s->max_send_fragment) &&
  638. s->compress == NULL && s->msg_callback == NULL &&
  639. SSL_USE_EXPLICIT_IV(s) &&
  640. s->enc_write_ctx != NULL &&
  641. EVP_CIPHER_flags(s->enc_write_ctx->cipher) &
  642. EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) {
  643. unsigned char aad[13];
  644. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
  645. int packlen;
  646. /* minimize address aliasing conflicts */
  647. if ((max_send_fragment & 0xfff) == 0)
  648. max_send_fragment -= 512;
  649. if (tot == 0 || wb->buf == NULL) { /* allocate jumbo buffer */
  650. ssl3_release_write_buffer(s);
  651. packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  652. EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,
  653. max_send_fragment, NULL);
  654. if (len >= 8 * (int)max_send_fragment)
  655. packlen *= 8;
  656. else
  657. packlen *= 4;
  658. wb->buf = OPENSSL_malloc(packlen);
  659. if (!wb->buf) {
  660. SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_MALLOC_FAILURE);
  661. return -1;
  662. }
  663. wb->len = packlen;
  664. } else if (tot == len) { /* done? */
  665. OPENSSL_free(wb->buf); /* free jumbo buffer */
  666. wb->buf = NULL;
  667. return tot;
  668. }
  669. n = (len - tot);
  670. for (;;) {
  671. if (n < 4 * max_send_fragment) {
  672. OPENSSL_free(wb->buf); /* free jumbo buffer */
  673. wb->buf = NULL;
  674. break;
  675. }
  676. if (s->s3->alert_dispatch) {
  677. i = s->method->ssl_dispatch_alert(s);
  678. if (i <= 0) {
  679. s->s3->wnum = tot;
  680. return i;
  681. }
  682. }
  683. if (n >= 8 * max_send_fragment)
  684. nw = max_send_fragment * (mb_param.interleave = 8);
  685. else
  686. nw = max_send_fragment * (mb_param.interleave = 4);
  687. memcpy(aad, s->s3->write_sequence, 8);
  688. aad[8] = type;
  689. aad[9] = (unsigned char)(s->version >> 8);
  690. aad[10] = (unsigned char)(s->version);
  691. aad[11] = 0;
  692. aad[12] = 0;
  693. mb_param.out = NULL;
  694. mb_param.inp = aad;
  695. mb_param.len = nw;
  696. packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  697. EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
  698. sizeof(mb_param), &mb_param);
  699. if (packlen <= 0 || packlen > (int)wb->len) { /* never happens */
  700. OPENSSL_free(wb->buf); /* free jumbo buffer */
  701. wb->buf = NULL;
  702. break;
  703. }
  704. mb_param.out = wb->buf;
  705. mb_param.inp = &buf[tot];
  706. mb_param.len = nw;
  707. if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
  708. EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
  709. sizeof(mb_param), &mb_param) <= 0)
  710. return -1;
  711. s->s3->write_sequence[7] += mb_param.interleave;
  712. if (s->s3->write_sequence[7] < mb_param.interleave) {
  713. int j = 6;
  714. while (j >= 0 && (++s->s3->write_sequence[j--]) == 0) ;
  715. }
  716. wb->offset = 0;
  717. wb->left = packlen;
  718. s->s3->wpend_tot = nw;
  719. s->s3->wpend_buf = &buf[tot];
  720. s->s3->wpend_type = type;
  721. s->s3->wpend_ret = nw;
  722. i = ssl3_write_pending(s, type, &buf[tot], nw);
  723. if (i <= 0) {
  724. if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {
  725. OPENSSL_free(wb->buf);
  726. wb->buf = NULL;
  727. }
  728. s->s3->wnum = tot;
  729. return i;
  730. }
  731. if (i == (int)n) {
  732. OPENSSL_free(wb->buf); /* free jumbo buffer */
  733. wb->buf = NULL;
  734. return tot + i;
  735. }
  736. n -= i;
  737. tot += i;
  738. }
  739. } else
  740. #endif
  741. if (tot == len) { /* done? */
  742. if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
  743. ssl3_release_write_buffer(s);
  744. return tot;
  745. }
  746. n = (len - tot);
  747. for (;;) {
  748. if (n > s->max_send_fragment)
  749. nw = s->max_send_fragment;
  750. else
  751. nw = n;
  752. i = do_ssl3_write(s, type, &(buf[tot]), nw, 0);
  753. if (i <= 0) {
  754. /* XXX should we ssl3_release_write_buffer if i<0? */
  755. s->s3->wnum = tot;
  756. return i;
  757. }
  758. if ((i == (int)n) ||
  759. (type == SSL3_RT_APPLICATION_DATA &&
  760. (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
  761. /*
  762. * next chunk of data should get another prepended empty fragment
  763. * in ciphersuites with known-IV weakness:
  764. */
  765. s->s3->empty_fragment_done = 0;
  766. if ((i == (int)n) && s->mode & SSL_MODE_RELEASE_BUFFERS &&
  767. !SSL_IS_DTLS(s))
  768. ssl3_release_write_buffer(s);
  769. return tot + i;
  770. }
  771. n -= i;
  772. tot += i;
  773. }
  774. }
  775. static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
  776. unsigned int len, int create_empty_fragment)
  777. {
  778. unsigned char *p, *plen;
  779. int i, mac_size, clear = 0;
  780. int prefix_len = 0;
  781. int eivlen;
  782. long align = 0;
  783. SSL3_RECORD *wr;
  784. SSL3_BUFFER *wb = &(s->s3->wbuf);
  785. SSL_SESSION *sess;
  786. /*
  787. * first check if there is a SSL3_BUFFER still being written out. This
  788. * will happen with non blocking IO
  789. */
  790. if (wb->left != 0)
  791. return (ssl3_write_pending(s, type, buf, len));
  792. /* If we have an alert to send, lets send it */
  793. if (s->s3->alert_dispatch) {
  794. i = s->method->ssl_dispatch_alert(s);
  795. if (i <= 0)
  796. return (i);
  797. /* if it went, fall through and send more stuff */
  798. }
  799. if (wb->buf == NULL)
  800. if (!ssl3_setup_write_buffer(s))
  801. return -1;
  802. if (len == 0 && !create_empty_fragment)
  803. return 0;
  804. wr = &(s->s3->wrec);
  805. sess = s->session;
  806. if ((sess == NULL) ||
  807. (s->enc_write_ctx == NULL) ||
  808. (EVP_MD_CTX_md(s->write_hash) == NULL)) {
  809. #if 1
  810. clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */
  811. #else
  812. clear = 1;
  813. #endif
  814. mac_size = 0;
  815. } else {
  816. mac_size = EVP_MD_CTX_size(s->write_hash);
  817. if (mac_size < 0)
  818. goto err;
  819. }
  820. /*
  821. * 'create_empty_fragment' is true only when this function calls itself
  822. */
  823. if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {
  824. /*
  825. * countermeasure against known-IV weakness in CBC ciphersuites (see
  826. * http://www.openssl.org/~bodo/tls-cbc.txt)
  827. */
  828. if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
  829. /*
  830. * recursive function call with 'create_empty_fragment' set; this
  831. * prepares and buffers the data for an empty fragment (these
  832. * 'prefix_len' bytes are sent out later together with the actual
  833. * payload)
  834. */
  835. prefix_len = do_ssl3_write(s, type, buf, 0, 1);
  836. if (prefix_len <= 0)
  837. goto err;
  838. if (prefix_len >
  839. (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD))
  840. {
  841. /* insufficient space */
  842. SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
  843. goto err;
  844. }
  845. }
  846. s->s3->empty_fragment_done = 1;
  847. }
  848. if (create_empty_fragment) {
  849. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  850. /*
  851. * extra fragment would be couple of cipher blocks, which would be
  852. * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real
  853. * payload, then we can just pretent we simply have two headers.
  854. */
  855. align = (long)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
  856. align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
  857. #endif
  858. p = wb->buf + align;
  859. wb->offset = align;
  860. } else if (prefix_len) {
  861. p = wb->buf + wb->offset + prefix_len;
  862. } else {
  863. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  864. align = (long)wb->buf + SSL3_RT_HEADER_LENGTH;
  865. align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
  866. #endif
  867. p = wb->buf + align;
  868. wb->offset = align;
  869. }
  870. /* write the header */
  871. *(p++) = type & 0xff;
  872. wr->type = type;
  873. *(p++) = (s->version >> 8);
  874. /*
  875. * Some servers hang if iniatial client hello is larger than 256 bytes
  876. * and record version number > TLS 1.0
  877. */
  878. if (s->state == SSL3_ST_CW_CLNT_HELLO_B
  879. && !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION)
  880. *(p++) = 0x1;
  881. else
  882. *(p++) = s->version & 0xff;
  883. /* field where we are to write out packet length */
  884. plen = p;
  885. p += 2;
  886. /* Explicit IV length, block ciphers appropriate version flag */
  887. if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) {
  888. int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
  889. if (mode == EVP_CIPH_CBC_MODE) {
  890. eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
  891. if (eivlen <= 1)
  892. eivlen = 0;
  893. }
  894. /* Need explicit part of IV for GCM mode */
  895. else if (mode == EVP_CIPH_GCM_MODE)
  896. eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
  897. else
  898. eivlen = 0;
  899. } else
  900. eivlen = 0;
  901. /* lets setup the record stuff. */
  902. wr->data = p + eivlen;
  903. wr->length = (int)len;
  904. wr->input = (unsigned char *)buf;
  905. /*
  906. * we now 'read' from wr->input, wr->length bytes into wr->data
  907. */
  908. /* first we compress */
  909. if (s->compress != NULL) {
  910. if (!ssl3_do_compress(s)) {
  911. SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE);
  912. goto err;
  913. }
  914. } else {
  915. memcpy(wr->data, wr->input, wr->length);
  916. wr->input = wr->data;
  917. }
  918. /*
  919. * we should still have the output to wr->data and the input from
  920. * wr->input. Length should be wr->length. wr->data still points in the
  921. * wb->buf
  922. */
  923. if (mac_size != 0) {
  924. if (s->method->ssl3_enc->mac(s, &(p[wr->length + eivlen]), 1) < 0)
  925. goto err;
  926. wr->length += mac_size;
  927. }
  928. wr->input = p;
  929. wr->data = p;
  930. if (eivlen) {
  931. /*
  932. * if (RAND_pseudo_bytes(p, eivlen) <= 0) goto err;
  933. */
  934. wr->length += eivlen;
  935. }
  936. if (s->method->ssl3_enc->enc(s, 1) < 1)
  937. goto err;
  938. /* record length after mac and block padding */
  939. s2n(wr->length, plen);
  940. if (s->msg_callback)
  941. s->msg_callback(1, 0, SSL3_RT_HEADER, plen - 5, 5, s,
  942. s->msg_callback_arg);
  943. /*
  944. * we should now have wr->data pointing to the encrypted data, which is
  945. * wr->length long
  946. */
  947. wr->type = type; /* not needed but helps for debugging */
  948. wr->length += SSL3_RT_HEADER_LENGTH;
  949. if (create_empty_fragment) {
  950. /*
  951. * we are in a recursive call; just return the length, don't write
  952. * out anything here
  953. */
  954. return wr->length;
  955. }
  956. /* now let's set up wb */
  957. wb->left = prefix_len + wr->length;
  958. /*
  959. * memorize arguments so that ssl3_write_pending can detect bad write
  960. * retries later
  961. */
  962. s->s3->wpend_tot = len;
  963. s->s3->wpend_buf = buf;
  964. s->s3->wpend_type = type;
  965. s->s3->wpend_ret = len;
  966. /* we now just need to write the buffer */
  967. return ssl3_write_pending(s, type, buf, len);
  968. err:
  969. return -1;
  970. }
  971. /* if s->s3->wbuf.left != 0, we need to call this
  972. *
  973. * Return values are as per SSL_write(), i.e.
  974. */
  975. int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
  976. unsigned int len)
  977. {
  978. int i;
  979. SSL3_BUFFER *wb = &(s->s3->wbuf);
  980. if ((s->s3->wpend_tot > (int)len)
  981. || (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)
  982. && (s->s3->wpend_buf != buf))
  983. || (s->s3->wpend_type != type)) {
  984. SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
  985. return (-1);
  986. }
  987. for (;;) {
  988. clear_sys_error();
  989. if (s->wbio != NULL) {
  990. s->rwstate = SSL_WRITING;
  991. i = BIO_write(s->wbio,
  992. (char *)&(wb->buf[wb->offset]),
  993. (unsigned int)wb->left);
  994. } else {
  995. SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
  996. i = -1;
  997. }
  998. if (i == wb->left) {
  999. wb->left = 0;
  1000. wb->offset += i;
  1001. s->rwstate = SSL_NOTHING;
  1002. return (s->s3->wpend_ret);
  1003. } else if (i <= 0) {
  1004. if (SSL_IS_DTLS(s)) {
  1005. /*
  1006. * For DTLS, just drop it. That's kind of the whole point in
  1007. * using a datagram service
  1008. */
  1009. wb->left = 0;
  1010. }
  1011. return i;
  1012. }
  1013. wb->offset += i;
  1014. wb->left -= i;
  1015. }
  1016. }
  1017. /*-
  1018. * Return up to 'len' payload bytes received in 'type' records.
  1019. * 'type' is one of the following:
  1020. *
  1021. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  1022. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  1023. * - 0 (during a shutdown, no data has to be returned)
  1024. *
  1025. * If we don't have stored data to work from, read a SSL/TLS record first
  1026. * (possibly multiple records if we still don't have anything to return).
  1027. *
  1028. * This function must handle any surprises the peer may have for us, such as
  1029. * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
  1030. * a surprise, but handled as if it were), or renegotiation requests.
  1031. * Also if record payloads contain fragments too small to process, we store
  1032. * them until there is enough for the respective protocol (the record protocol
  1033. * may use arbitrary fragmentation and even interleaving):
  1034. * Change cipher spec protocol
  1035. * just 1 byte needed, no need for keeping anything stored
  1036. * Alert protocol
  1037. * 2 bytes needed (AlertLevel, AlertDescription)
  1038. * Handshake protocol
  1039. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  1040. * to detect unexpected Client Hello and Hello Request messages
  1041. * here, anything else is handled by higher layers
  1042. * Application data protocol
  1043. * none of our business
  1044. */
  1045. int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
  1046. {
  1047. int al, i, j, ret;
  1048. unsigned int n;
  1049. SSL3_RECORD *rr;
  1050. void (*cb) (const SSL *ssl, int type2, int val) = NULL;
  1051. if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
  1052. if (!ssl3_setup_read_buffer(s))
  1053. return (-1);
  1054. if ((type && (type != SSL3_RT_APPLICATION_DATA)
  1055. && (type != SSL3_RT_HANDSHAKE)) || (peek
  1056. && (type !=
  1057. SSL3_RT_APPLICATION_DATA))) {
  1058. SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
  1059. return -1;
  1060. }
  1061. if ((type == SSL3_RT_HANDSHAKE) && (s->s3->handshake_fragment_len > 0))
  1062. /* (partially) satisfy request from storage */
  1063. {
  1064. unsigned char *src = s->s3->handshake_fragment;
  1065. unsigned char *dst = buf;
  1066. unsigned int k;
  1067. /* peek == 0 */
  1068. n = 0;
  1069. while ((len > 0) && (s->s3->handshake_fragment_len > 0)) {
  1070. *dst++ = *src++;
  1071. len--;
  1072. s->s3->handshake_fragment_len--;
  1073. n++;
  1074. }
  1075. /* move any remaining fragment bytes: */
  1076. for (k = 0; k < s->s3->handshake_fragment_len; k++)
  1077. s->s3->handshake_fragment[k] = *src++;
  1078. return n;
  1079. }
  1080. /*
  1081. * Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
  1082. */
  1083. if (!s->in_handshake && SSL_in_init(s)) {
  1084. /* type == SSL3_RT_APPLICATION_DATA */
  1085. i = s->handshake_func(s);
  1086. if (i < 0)
  1087. return (i);
  1088. if (i == 0) {
  1089. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  1090. return (-1);
  1091. }
  1092. }
  1093. start:
  1094. s->rwstate = SSL_NOTHING;
  1095. /*-
  1096. * s->s3->rrec.type - is the type of record
  1097. * s->s3->rrec.data, - data
  1098. * s->s3->rrec.off, - offset into 'data' for next read
  1099. * s->s3->rrec.length, - number of bytes.
  1100. */
  1101. rr = &(s->s3->rrec);
  1102. /* get new packet if necessary */
  1103. if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) {
  1104. ret = ssl3_get_record(s);
  1105. if (ret <= 0)
  1106. return (ret);
  1107. }
  1108. /*
  1109. * Reset the count of consecutive warning alerts if we've got a non-empty
  1110. * record that isn't an alert.
  1111. */
  1112. if (rr->type != SSL3_RT_ALERT && rr->length != 0)
  1113. s->cert->alert_count = 0;
  1114. /* we now have a packet which can be read and processed */
  1115. if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
  1116. * reset by ssl3_get_finished */
  1117. && (rr->type != SSL3_RT_HANDSHAKE)) {
  1118. al = SSL_AD_UNEXPECTED_MESSAGE;
  1119. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
  1120. goto f_err;
  1121. }
  1122. /*
  1123. * If the other end has shut down, throw anything we read away (even in
  1124. * 'peek' mode)
  1125. */
  1126. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1127. rr->length = 0;
  1128. s->rwstate = SSL_NOTHING;
  1129. return (0);
  1130. }
  1131. if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or
  1132. * SSL3_RT_HANDSHAKE */
  1133. /*
  1134. * make sure that we are not getting application data when we are
  1135. * doing a handshake for the first time
  1136. */
  1137. if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
  1138. (s->enc_read_ctx == NULL)) {
  1139. al = SSL_AD_UNEXPECTED_MESSAGE;
  1140. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
  1141. goto f_err;
  1142. }
  1143. if (len <= 0)
  1144. return (len);
  1145. if ((unsigned int)len > rr->length)
  1146. n = rr->length;
  1147. else
  1148. n = (unsigned int)len;
  1149. memcpy(buf, &(rr->data[rr->off]), n);
  1150. if (!peek) {
  1151. rr->length -= n;
  1152. rr->off += n;
  1153. if (rr->length == 0) {
  1154. s->rstate = SSL_ST_READ_HEADER;
  1155. rr->off = 0;
  1156. if (s->mode & SSL_MODE_RELEASE_BUFFERS
  1157. && s->s3->rbuf.left == 0)
  1158. ssl3_release_read_buffer(s);
  1159. }
  1160. }
  1161. return (n);
  1162. }
  1163. /*
  1164. * If we get here, then type != rr->type; if we have a handshake message,
  1165. * then it was unexpected (Hello Request or Client Hello).
  1166. */
  1167. /*
  1168. * In case of record types for which we have 'fragment' storage, fill
  1169. * that so that we can process the data at a fixed place.
  1170. */
  1171. {
  1172. unsigned int dest_maxlen = 0;
  1173. unsigned char *dest = NULL;
  1174. unsigned int *dest_len = NULL;
  1175. if (rr->type == SSL3_RT_HANDSHAKE) {
  1176. dest_maxlen = sizeof(s->s3->handshake_fragment);
  1177. dest = s->s3->handshake_fragment;
  1178. dest_len = &s->s3->handshake_fragment_len;
  1179. } else if (rr->type == SSL3_RT_ALERT) {
  1180. dest_maxlen = sizeof(s->s3->alert_fragment);
  1181. dest = s->s3->alert_fragment;
  1182. dest_len = &s->s3->alert_fragment_len;
  1183. }
  1184. #ifndef OPENSSL_NO_HEARTBEATS
  1185. else if (rr->type == TLS1_RT_HEARTBEAT) {
  1186. i = tls1_process_heartbeat(s);
  1187. if (i < 0)
  1188. return i;
  1189. rr->length = 0;
  1190. if (s->mode & SSL_MODE_AUTO_RETRY)
  1191. goto start;
  1192. /* Exit and notify application to read again */
  1193. s->rwstate = SSL_READING;
  1194. BIO_clear_retry_flags(SSL_get_rbio(s));
  1195. BIO_set_retry_read(SSL_get_rbio(s));
  1196. return (-1);
  1197. }
  1198. #endif
  1199. if (dest_maxlen > 0) {
  1200. n = dest_maxlen - *dest_len; /* available space in 'dest' */
  1201. if (rr->length < n)
  1202. n = rr->length; /* available bytes */
  1203. /* now move 'n' bytes: */
  1204. while (n-- > 0) {
  1205. dest[(*dest_len)++] = rr->data[rr->off++];
  1206. rr->length--;
  1207. }
  1208. if (*dest_len < dest_maxlen)
  1209. goto start; /* fragment was too small */
  1210. }
  1211. }
  1212. /*-
  1213. * s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
  1214. * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
  1215. * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
  1216. */
  1217. /* If we are a client, check for an incoming 'Hello Request': */
  1218. if ((!s->server) &&
  1219. (s->s3->handshake_fragment_len >= 4) &&
  1220. (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
  1221. (s->session != NULL) && (s->session->cipher != NULL)) {
  1222. s->s3->handshake_fragment_len = 0;
  1223. if ((s->s3->handshake_fragment[1] != 0) ||
  1224. (s->s3->handshake_fragment[2] != 0) ||
  1225. (s->s3->handshake_fragment[3] != 0)) {
  1226. al = SSL_AD_DECODE_ERROR;
  1227. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
  1228. goto f_err;
  1229. }
  1230. if (s->msg_callback)
  1231. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  1232. s->s3->handshake_fragment, 4, s,
  1233. s->msg_callback_arg);
  1234. if (SSL_is_init_finished(s) &&
  1235. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
  1236. !s->s3->renegotiate) {
  1237. ssl3_renegotiate(s);
  1238. if (ssl3_renegotiate_check(s)) {
  1239. i = s->handshake_func(s);
  1240. if (i < 0)
  1241. return (i);
  1242. if (i == 0) {
  1243. SSLerr(SSL_F_SSL3_READ_BYTES,
  1244. SSL_R_SSL_HANDSHAKE_FAILURE);
  1245. return (-1);
  1246. }
  1247. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  1248. if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
  1249. BIO *bio;
  1250. /*
  1251. * In the case where we try to read application data,
  1252. * but we trigger an SSL handshake, we return -1 with
  1253. * the retry option set. Otherwise renegotiation may
  1254. * cause nasty problems in the blocking world
  1255. */
  1256. s->rwstate = SSL_READING;
  1257. bio = SSL_get_rbio(s);
  1258. BIO_clear_retry_flags(bio);
  1259. BIO_set_retry_read(bio);
  1260. return (-1);
  1261. }
  1262. }
  1263. }
  1264. }
  1265. /*
  1266. * we either finished a handshake or ignored the request, now try
  1267. * again to obtain the (application) data we were asked for
  1268. */
  1269. goto start;
  1270. }
  1271. /*
  1272. * If we are a server and get a client hello when renegotiation isn't
  1273. * allowed send back a no renegotiation alert and carry on.
  1274. */
  1275. if (s->server
  1276. && SSL_is_init_finished(s)
  1277. && !s->s3->send_connection_binding
  1278. && s->version > SSL3_VERSION
  1279. && s->s3->handshake_fragment_len >= SSL3_HM_HEADER_LENGTH
  1280. && s->s3->handshake_fragment[0] == SSL3_MT_CLIENT_HELLO
  1281. && s->s3->previous_client_finished_len != 0
  1282. && (s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0) {
  1283. s->s3->handshake_fragment_len = 0;
  1284. rr->length = 0;
  1285. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
  1286. goto start;
  1287. }
  1288. if (s->s3->alert_fragment_len >= 2) {
  1289. int alert_level = s->s3->alert_fragment[0];
  1290. int alert_descr = s->s3->alert_fragment[1];
  1291. s->s3->alert_fragment_len = 0;
  1292. if (s->msg_callback)
  1293. s->msg_callback(0, s->version, SSL3_RT_ALERT,
  1294. s->s3->alert_fragment, 2, s, s->msg_callback_arg);
  1295. if (s->info_callback != NULL)
  1296. cb = s->info_callback;
  1297. else if (s->ctx->info_callback != NULL)
  1298. cb = s->ctx->info_callback;
  1299. if (cb != NULL) {
  1300. j = (alert_level << 8) | alert_descr;
  1301. cb(s, SSL_CB_READ_ALERT, j);
  1302. }
  1303. if (alert_level == SSL3_AL_WARNING) {
  1304. s->s3->warn_alert = alert_descr;
  1305. s->cert->alert_count++;
  1306. if (s->cert->alert_count == MAX_WARN_ALERT_COUNT) {
  1307. al = SSL_AD_UNEXPECTED_MESSAGE;
  1308. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_TOO_MANY_WARN_ALERTS);
  1309. goto f_err;
  1310. }
  1311. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  1312. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  1313. return (0);
  1314. }
  1315. /*
  1316. * This is a warning but we receive it if we requested
  1317. * renegotiation and the peer denied it. Terminate with a fatal
  1318. * alert because if application tried to renegotiatie it
  1319. * presumably had a good reason and expects it to succeed. In
  1320. * future we might have a renegotiation where we don't care if
  1321. * the peer refused it where we carry on.
  1322. */
  1323. else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  1324. al = SSL_AD_HANDSHAKE_FAILURE;
  1325. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_NO_RENEGOTIATION);
  1326. goto f_err;
  1327. }
  1328. #ifdef SSL_AD_MISSING_SRP_USERNAME
  1329. else if (alert_descr == SSL_AD_MISSING_SRP_USERNAME)
  1330. return (0);
  1331. #endif
  1332. } else if (alert_level == SSL3_AL_FATAL) {
  1333. char tmp[16];
  1334. s->rwstate = SSL_NOTHING;
  1335. s->s3->fatal_alert = alert_descr;
  1336. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
  1337. BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
  1338. ERR_add_error_data(2, "SSL alert number ", tmp);
  1339. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  1340. SSL_CTX_remove_session(s->session_ctx, s->session);
  1341. s->state = SSL_ST_ERR;
  1342. return (0);
  1343. } else {
  1344. al = SSL_AD_ILLEGAL_PARAMETER;
  1345. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
  1346. goto f_err;
  1347. }
  1348. goto start;
  1349. }
  1350. if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
  1351. * shutdown */
  1352. s->rwstate = SSL_NOTHING;
  1353. rr->length = 0;
  1354. return (0);
  1355. }
  1356. if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  1357. /*
  1358. * 'Change Cipher Spec' is just a single byte, so we know exactly
  1359. * what the record payload has to look like
  1360. */
  1361. if ((rr->length != 1) || (rr->off != 0) ||
  1362. (rr->data[0] != SSL3_MT_CCS)) {
  1363. al = SSL_AD_ILLEGAL_PARAMETER;
  1364. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  1365. goto f_err;
  1366. }
  1367. /* Check we have a cipher to change to */
  1368. if (s->s3->tmp.new_cipher == NULL) {
  1369. al = SSL_AD_UNEXPECTED_MESSAGE;
  1370. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
  1371. goto f_err;
  1372. }
  1373. if (!(s->s3->flags & SSL3_FLAGS_CCS_OK)) {
  1374. al = SSL_AD_UNEXPECTED_MESSAGE;
  1375. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
  1376. goto f_err;
  1377. }
  1378. s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
  1379. rr->length = 0;
  1380. if (s->msg_callback)
  1381. s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
  1382. rr->data, 1, s, s->msg_callback_arg);
  1383. s->s3->change_cipher_spec = 1;
  1384. if (!ssl3_do_change_cipher_spec(s))
  1385. goto err;
  1386. else
  1387. goto start;
  1388. }
  1389. /*
  1390. * Unexpected handshake message (Client Hello, or protocol violation)
  1391. */
  1392. if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake) {
  1393. if (((s->state & SSL_ST_MASK) == SSL_ST_OK) &&
  1394. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
  1395. #if 0 /* worked only because C operator preferences
  1396. * are not as expected (and because this is
  1397. * not really needed for clients except for
  1398. * detecting protocol violations): */
  1399. s->state = SSL_ST_BEFORE | (s->server)
  1400. ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
  1401. #else
  1402. s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
  1403. #endif
  1404. s->renegotiate = 1;
  1405. s->new_session = 1;
  1406. }
  1407. i = s->handshake_func(s);
  1408. if (i < 0)
  1409. return (i);
  1410. if (i == 0) {
  1411. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
  1412. return (-1);
  1413. }
  1414. if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
  1415. if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
  1416. BIO *bio;
  1417. /*
  1418. * In the case where we try to read application data, but we
  1419. * trigger an SSL handshake, we return -1 with the retry
  1420. * option set. Otherwise renegotiation may cause nasty
  1421. * problems in the blocking world
  1422. */
  1423. s->rwstate = SSL_READING;
  1424. bio = SSL_get_rbio(s);
  1425. BIO_clear_retry_flags(bio);
  1426. BIO_set_retry_read(bio);
  1427. return (-1);
  1428. }
  1429. }
  1430. goto start;
  1431. }
  1432. switch (rr->type) {
  1433. default:
  1434. /*
  1435. * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but
  1436. * TLS 1.2 says you MUST send an unexpected message alert. We use the
  1437. * TLS 1.2 behaviour for all protocol versions to prevent issues where
  1438. * no progress is being made and the peer continually sends unrecognised
  1439. * record types, using up resources processing them.
  1440. */
  1441. al = SSL_AD_UNEXPECTED_MESSAGE;
  1442. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
  1443. goto f_err;
  1444. case SSL3_RT_CHANGE_CIPHER_SPEC:
  1445. case SSL3_RT_ALERT:
  1446. case SSL3_RT_HANDSHAKE:
  1447. /*
  1448. * we already handled all of these, with the possible exception of
  1449. * SSL3_RT_HANDSHAKE when s->in_handshake is set, but that should not
  1450. * happen when type != rr->type
  1451. */
  1452. al = SSL_AD_UNEXPECTED_MESSAGE;
  1453. SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
  1454. goto f_err;
  1455. case SSL3_RT_APPLICATION_DATA:
  1456. /*
  1457. * At this point, we were expecting handshake data, but have
  1458. * application data. If the library was running inside ssl3_read()
  1459. * (i.e. in_read_app_data is set) and it makes sense to read
  1460. * application data at this point (session renegotiation not yet
  1461. * started), we will indulge it.
  1462. */
  1463. if (s->s3->in_read_app_data &&
  1464. (s->s3->total_renegotiations != 0) &&
  1465. (((s->state & SSL_ST_CONNECT) &&
  1466. (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
  1467. (s->state <= SSL3_ST_CR_SRVR_HELLO_A)
  1468. ) || ((s->state & SSL_ST_ACCEPT) &&
  1469. (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
  1470. (s->state >= SSL3_ST_SR_CLNT_HELLO_A)
  1471. )
  1472. )) {
  1473. s->s3->in_read_app_data = 2;
  1474. return (-1);
  1475. } else {
  1476. al = SSL_AD_UNEXPECTED_MESSAGE;
  1477. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
  1478. goto f_err;
  1479. }
  1480. }
  1481. /* not reached */
  1482. f_err:
  1483. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1484. err:
  1485. return (-1);
  1486. }
  1487. int ssl3_do_change_cipher_spec(SSL *s)
  1488. {
  1489. int i;
  1490. const char *sender;
  1491. int slen;
  1492. if (s->state & SSL_ST_ACCEPT)
  1493. i = SSL3_CHANGE_CIPHER_SERVER_READ;
  1494. else
  1495. i = SSL3_CHANGE_CIPHER_CLIENT_READ;
  1496. if (s->s3->tmp.key_block == NULL) {
  1497. if (s->session == NULL || s->session->master_key_length == 0) {
  1498. /* might happen if dtls1_read_bytes() calls this */
  1499. SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC,
  1500. SSL_R_CCS_RECEIVED_EARLY);
  1501. return (0);
  1502. }
  1503. s->session->cipher = s->s3->tmp.new_cipher;
  1504. if (!s->method->ssl3_enc->setup_key_block(s))
  1505. return (0);
  1506. }
  1507. if (!s->method->ssl3_enc->change_cipher_state(s, i))
  1508. return (0);
  1509. /*
  1510. * we have to record the message digest at this point so we can get it
  1511. * before we read the finished message
  1512. */
  1513. if (s->state & SSL_ST_CONNECT) {
  1514. sender = s->method->ssl3_enc->server_finished_label;
  1515. slen = s->method->ssl3_enc->server_finished_label_len;
  1516. } else {
  1517. sender = s->method->ssl3_enc->client_finished_label;
  1518. slen = s->method->ssl3_enc->client_finished_label_len;
  1519. }
  1520. i = s->method->ssl3_enc->final_finish_mac(s,
  1521. sender, slen,
  1522. s->s3->tmp.peer_finish_md);
  1523. if (i == 0) {
  1524. SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
  1525. return 0;
  1526. }
  1527. s->s3->tmp.peer_finish_md_len = i;
  1528. return (1);
  1529. }
  1530. int ssl3_send_alert(SSL *s, int level, int desc)
  1531. {
  1532. /* Map tls/ssl alert value to correct one */
  1533. desc = s->method->ssl3_enc->alert_value(desc);
  1534. if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
  1535. desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
  1536. * protocol_version alerts */
  1537. if (desc < 0)
  1538. return -1;
  1539. /* If a fatal one, remove from cache and go into the error state */
  1540. if (level == SSL3_AL_FATAL) {
  1541. if (s->session != NULL)
  1542. SSL_CTX_remove_session(s->session_ctx, s->session);
  1543. s->state = SSL_ST_ERR;
  1544. }
  1545. s->s3->alert_dispatch = 1;
  1546. s->s3->send_alert[0] = level;
  1547. s->s3->send_alert[1] = desc;
  1548. if (s->s3->wbuf.left == 0) /* data still being written out? */
  1549. return s->method->ssl_dispatch_alert(s);
  1550. /*
  1551. * else data is still being written out, we will get written some time in
  1552. * the future
  1553. */
  1554. return -1;
  1555. }
  1556. int ssl3_dispatch_alert(SSL *s)
  1557. {
  1558. int i, j;
  1559. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  1560. s->s3->alert_dispatch = 0;
  1561. i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
  1562. if (i <= 0) {
  1563. s->s3->alert_dispatch = 1;
  1564. } else {
  1565. /*
  1566. * Alert sent to BIO. If it is important, flush it now. If the
  1567. * message does not get sent due to non-blocking IO, we will not
  1568. * worry too much.
  1569. */
  1570. if (s->s3->send_alert[0] == SSL3_AL_FATAL)
  1571. (void)BIO_flush(s->wbio);
  1572. if (s->msg_callback)
  1573. s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
  1574. 2, s, s->msg_callback_arg);
  1575. if (s->info_callback != NULL)
  1576. cb = s->info_callback;
  1577. else if (s->ctx->info_callback != NULL)
  1578. cb = s->ctx->info_callback;
  1579. if (cb != NULL) {
  1580. j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
  1581. cb(s, SSL_CB_WRITE_ALERT, j);
  1582. }
  1583. }
  1584. return (i);
  1585. }