s3_pkt.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /* ssl/s3_pkt.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  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 (tjh@cryptsoft.com).
  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 (eay@cryptsoft.com)"
  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 (tjh@cryptsoft.com)"
  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-2002 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. * openssl-core@openssl.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <errno.h>
  113. #define USE_SOCKETS
  114. #include "ssl_locl.h"
  115. #include <openssl/evp.h>
  116. #include <openssl/buffer.h>
  117. static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
  118. unsigned int len, int create_empty_fragment);
  119. static int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
  120. unsigned int len);
  121. static int ssl3_get_record(SSL *s);
  122. static int do_compress(SSL *ssl);
  123. static int do_uncompress(SSL *ssl);
  124. static int do_change_cipher_spec(SSL *ssl);
  125. /* used only by ssl3_get_record */
  126. static int ssl3_read_n(SSL *s, int n, int max, int extend)
  127. {
  128. /* If extend == 0, obtain new n-byte packet; if extend == 1, increase
  129. * packet by another n bytes.
  130. * The packet will be in the sub-array of s->s3->rbuf.buf specified
  131. * by s->packet and s->packet_length.
  132. * (If s->read_ahead is set, 'max' bytes may be stored in rbuf
  133. * [plus s->packet_length bytes if extend == 1].)
  134. */
  135. int i,off,newb;
  136. if (!extend)
  137. {
  138. /* start with empty packet ... */
  139. if (s->s3->rbuf.left == 0)
  140. s->s3->rbuf.offset = 0;
  141. s->packet = s->s3->rbuf.buf + s->s3->rbuf.offset;
  142. s->packet_length = 0;
  143. /* ... now we can act as if 'extend' was set */
  144. }
  145. /* if there is enough in the buffer from a previous read, take some */
  146. if (s->s3->rbuf.left >= (int)n)
  147. {
  148. s->packet_length+=n;
  149. s->s3->rbuf.left-=n;
  150. s->s3->rbuf.offset+=n;
  151. return(n);
  152. }
  153. /* else we need to read more data */
  154. if (!s->read_ahead)
  155. max=n;
  156. {
  157. /* avoid buffer overflow */
  158. int max_max = s->s3->rbuf.len - s->packet_length;
  159. if (max > max_max)
  160. max = max_max;
  161. }
  162. if (n > max) /* does not happen */
  163. {
  164. SSLerr(SSL_F_SSL3_READ_N,ERR_R_INTERNAL_ERROR);
  165. return -1;
  166. }
  167. off = s->packet_length;
  168. newb = s->s3->rbuf.left;
  169. /* Move any available bytes to front of buffer:
  170. * 'off' bytes already pointed to by 'packet',
  171. * 'newb' extra ones at the end */
  172. if (s->packet != s->s3->rbuf.buf)
  173. {
  174. /* off > 0 */
  175. memmove(s->s3->rbuf.buf, s->packet, off+newb);
  176. s->packet = s->s3->rbuf.buf;
  177. }
  178. while (newb < n)
  179. {
  180. /* Now we have off+newb bytes at the front of s->s3->rbuf.buf and need
  181. * to read in more until we have off+n (up to off+max if possible) */
  182. clear_sys_error();
  183. if (s->rbio != NULL)
  184. {
  185. s->rwstate=SSL_READING;
  186. i=BIO_read(s->rbio, &(s->s3->rbuf.buf[off+newb]), max-newb);
  187. }
  188. else
  189. {
  190. SSLerr(SSL_F_SSL3_READ_N,SSL_R_READ_BIO_NOT_SET);
  191. i = -1;
  192. }
  193. if (i <= 0)
  194. {
  195. s->s3->rbuf.left = newb;
  196. return(i);
  197. }
  198. newb+=i;
  199. }
  200. /* done reading, now the book-keeping */
  201. s->s3->rbuf.offset = off + n;
  202. s->s3->rbuf.left = newb - n;
  203. s->packet_length += n;
  204. s->rwstate=SSL_NOTHING;
  205. return(n);
  206. }
  207. /* Call this to get a new input record.
  208. * It will return <= 0 if more data is needed, normally due to an error
  209. * or non-blocking IO.
  210. * When it finishes, one packet has been decoded and can be found in
  211. * ssl->s3->rrec.type - is the type of record
  212. * ssl->s3->rrec.data, - data
  213. * ssl->s3->rrec.length, - number of bytes
  214. */
  215. /* used only by ssl3_read_bytes */
  216. static int ssl3_get_record(SSL *s)
  217. {
  218. int ssl_major,ssl_minor,al;
  219. int enc_err,n,i,ret= -1;
  220. SSL3_RECORD *rr;
  221. SSL_SESSION *sess;
  222. unsigned char *p;
  223. unsigned char md[EVP_MAX_MD_SIZE];
  224. short version;
  225. unsigned int mac_size;
  226. int clear=0;
  227. size_t extra;
  228. int decryption_failed_or_bad_record_mac = 0;
  229. unsigned char *mac = NULL;
  230. rr= &(s->s3->rrec);
  231. sess=s->session;
  232. if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
  233. extra=SSL3_RT_MAX_EXTRA;
  234. else
  235. extra=0;
  236. if (extra != s->s3->rbuf.len - SSL3_RT_MAX_PACKET_SIZE)
  237. {
  238. /* actually likely an application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER
  239. * set after ssl3_setup_buffers() was done */
  240. SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
  241. return -1;
  242. }
  243. again:
  244. /* check if we have the header */
  245. if ( (s->rstate != SSL_ST_READ_BODY) ||
  246. (s->packet_length < SSL3_RT_HEADER_LENGTH))
  247. {
  248. n=ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
  249. if (n <= 0) return(n); /* error or non-blocking */
  250. s->rstate=SSL_ST_READ_BODY;
  251. p=s->packet;
  252. /* Pull apart the header into the SSL3_RECORD */
  253. rr->type= *(p++);
  254. ssl_major= *(p++);
  255. ssl_minor= *(p++);
  256. version=(ssl_major<<8)|ssl_minor;
  257. n2s(p,rr->length);
  258. /* Lets check version */
  259. if (s->first_packet)
  260. {
  261. s->first_packet=0;
  262. }
  263. else
  264. {
  265. if (version != s->version)
  266. {
  267. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
  268. /* Send back error using their
  269. * version number :-) */
  270. s->version=version;
  271. al=SSL_AD_PROTOCOL_VERSION;
  272. goto f_err;
  273. }
  274. }
  275. if ((version>>8) != SSL3_VERSION_MAJOR)
  276. {
  277. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
  278. goto err;
  279. }
  280. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)
  281. {
  282. al=SSL_AD_RECORD_OVERFLOW;
  283. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG);
  284. goto f_err;
  285. }
  286. /* now s->rstate == SSL_ST_READ_BODY */
  287. }
  288. /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
  289. if (rr->length > s->packet_length-SSL3_RT_HEADER_LENGTH)
  290. {
  291. /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
  292. i=rr->length;
  293. n=ssl3_read_n(s,i,i,1);
  294. if (n <= 0) return(n); /* error or non-blocking io */
  295. /* now n == rr->length,
  296. * and s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length */
  297. }
  298. s->rstate=SSL_ST_READ_HEADER; /* set state for later operations */
  299. /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
  300. * and we have that many bytes in s->packet
  301. */
  302. rr->input= &(s->packet[SSL3_RT_HEADER_LENGTH]);
  303. /* ok, we can now read from 's->packet' data into 'rr'
  304. * rr->input points at rr->length bytes, which
  305. * need to be copied into rr->data by either
  306. * the decryption or by the decompression
  307. * When the data is 'copied' into the rr->data buffer,
  308. * rr->input will be pointed at the new buffer */
  309. /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
  310. * rr->length bytes of encrypted compressed stuff. */
  311. /* check is not needed I believe */
  312. if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)
  313. {
  314. al=SSL_AD_RECORD_OVERFLOW;
  315. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  316. goto f_err;
  317. }
  318. /* decrypt in place in 'rr->input' */
  319. rr->data=rr->input;
  320. enc_err = s->method->ssl3_enc->enc(s,0);
  321. if (enc_err <= 0)
  322. {
  323. if (enc_err == 0)
  324. /* SSLerr() and ssl3_send_alert() have been called */
  325. goto err;
  326. /* Otherwise enc_err == -1, which indicates bad padding
  327. * (rec->length has not been changed in this case).
  328. * To minimize information leaked via timing, we will perform
  329. * the MAC computation anyway. */
  330. decryption_failed_or_bad_record_mac = 1;
  331. }
  332. #ifdef TLS_DEBUG
  333. printf("dec %d\n",rr->length);
  334. { unsigned int z; for (z=0; z<rr->length; z++) printf("%02X%c",rr->data[z],((z+1)%16)?' ':'\n'); }
  335. printf("\n");
  336. #endif
  337. /* r->length is now the compressed data plus mac */
  338. if ( (sess == NULL) ||
  339. (s->enc_read_ctx == NULL) ||
  340. (s->read_hash == NULL))
  341. clear=1;
  342. if (!clear)
  343. {
  344. mac_size=EVP_MD_size(s->read_hash);
  345. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size)
  346. {
  347. #if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */
  348. al=SSL_AD_RECORD_OVERFLOW;
  349. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);
  350. goto f_err;
  351. #else
  352. decryption_failed_or_bad_record_mac = 1;
  353. #endif
  354. }
  355. /* check the MAC for rr->input (it's in mac_size bytes at the tail) */
  356. if (rr->length >= mac_size)
  357. {
  358. rr->length -= mac_size;
  359. mac = &rr->data[rr->length];
  360. }
  361. else
  362. {
  363. /* record (minus padding) is too short to contain a MAC */
  364. #if 0 /* OK only for stream ciphers */
  365. al=SSL_AD_DECODE_ERROR;
  366. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT);
  367. goto f_err;
  368. #else
  369. decryption_failed_or_bad_record_mac = 1;
  370. rr->length = 0;
  371. #endif
  372. }
  373. i=s->method->ssl3_enc->mac(s,md,0);
  374. if (mac == NULL || memcmp(md, mac, mac_size) != 0)
  375. {
  376. decryption_failed_or_bad_record_mac = 1;
  377. }
  378. }
  379. if (decryption_failed_or_bad_record_mac)
  380. {
  381. /* A separate 'decryption_failed' alert was introduced with TLS 1.0,
  382. * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
  383. * failure is directly visible from the ciphertext anyway,
  384. * we should not reveal which kind of error occured -- this
  385. * might become visible to an attacker (e.g. via a logfile) */
  386. al=SSL_AD_BAD_RECORD_MAC;
  387. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  388. goto f_err;
  389. }
  390. /* r->length is now just compressed */
  391. if (s->expand != NULL)
  392. {
  393. if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra)
  394. {
  395. al=SSL_AD_RECORD_OVERFLOW;
  396. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG);
  397. goto f_err;
  398. }
  399. if (!do_uncompress(s))
  400. {
  401. al=SSL_AD_DECOMPRESSION_FAILURE;
  402. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_DECOMPRESSION);
  403. goto f_err;
  404. }
  405. }
  406. if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH+extra)
  407. {
  408. al=SSL_AD_RECORD_OVERFLOW;
  409. SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DATA_LENGTH_TOO_LONG);
  410. goto f_err;
  411. }
  412. rr->off=0;
  413. /* So at this point the following is true
  414. * ssl->s3->rrec.type is the type of record
  415. * ssl->s3->rrec.length == number of bytes in record
  416. * ssl->s3->rrec.off == offset to first valid byte
  417. * ssl->s3->rrec.data == where to take bytes from, increment
  418. * after use :-).
  419. */
  420. /* we have pulled in a full packet so zero things */
  421. s->packet_length=0;
  422. /* just read a 0 length packet */
  423. if (rr->length == 0) goto again;
  424. return(1);
  425. f_err:
  426. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  427. err:
  428. return(ret);
  429. }
  430. static int do_uncompress(SSL *ssl)
  431. {
  432. int i;
  433. SSL3_RECORD *rr;
  434. rr= &(ssl->s3->rrec);
  435. i=COMP_expand_block(ssl->expand,rr->comp,
  436. SSL3_RT_MAX_PLAIN_LENGTH,rr->data,(int)rr->length);
  437. if (i < 0)
  438. return(0);
  439. else
  440. rr->length=i;
  441. rr->data=rr->comp;
  442. return(1);
  443. }
  444. static int do_compress(SSL *ssl)
  445. {
  446. int i;
  447. SSL3_RECORD *wr;
  448. wr= &(ssl->s3->wrec);
  449. i=COMP_compress_block(ssl->compress,wr->data,
  450. SSL3_RT_MAX_COMPRESSED_LENGTH,
  451. wr->input,(int)wr->length);
  452. if (i < 0)
  453. return(0);
  454. else
  455. wr->length=i;
  456. wr->input=wr->data;
  457. return(1);
  458. }
  459. /* Call this to write data in records of type 'type'
  460. * It will return <= 0 if not all data has been sent or non-blocking IO.
  461. */
  462. int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
  463. {
  464. const unsigned char *buf=buf_;
  465. unsigned int tot,n,nw;
  466. int i;
  467. s->rwstate=SSL_NOTHING;
  468. tot=s->s3->wnum;
  469. s->s3->wnum=0;
  470. if (SSL_in_init(s) && !s->in_handshake)
  471. {
  472. i=s->handshake_func(s);
  473. if (i < 0) return(i);
  474. if (i == 0)
  475. {
  476. SSLerr(SSL_F_SSL3_WRITE_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
  477. return -1;
  478. }
  479. }
  480. n=(len-tot);
  481. for (;;)
  482. {
  483. if (n > SSL3_RT_MAX_PLAIN_LENGTH)
  484. nw=SSL3_RT_MAX_PLAIN_LENGTH;
  485. else
  486. nw=n;
  487. i=do_ssl3_write(s, type, &(buf[tot]), nw, 0);
  488. if (i <= 0)
  489. {
  490. s->s3->wnum=tot;
  491. return i;
  492. }
  493. if ((i == (int)n) ||
  494. (type == SSL3_RT_APPLICATION_DATA &&
  495. (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)))
  496. {
  497. /* next chunk of data should get another prepended empty fragment
  498. * in ciphersuites with known-IV weakness: */
  499. s->s3->empty_fragment_done = 0;
  500. return tot+i;
  501. }
  502. n-=i;
  503. tot+=i;
  504. }
  505. }
  506. static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
  507. unsigned int len, int create_empty_fragment)
  508. {
  509. unsigned char *p,*plen;
  510. int i,mac_size,clear=0;
  511. int prefix_len = 0;
  512. SSL3_RECORD *wr;
  513. SSL3_BUFFER *wb;
  514. SSL_SESSION *sess;
  515. /* first check if there is a SSL3_BUFFER still being written
  516. * out. This will happen with non blocking IO */
  517. if (s->s3->wbuf.left != 0)
  518. return(ssl3_write_pending(s,type,buf,len));
  519. /* If we have an alert to send, lets send it */
  520. if (s->s3->alert_dispatch)
  521. {
  522. i=ssl3_dispatch_alert(s);
  523. if (i <= 0)
  524. return(i);
  525. /* if it went, fall through and send more stuff */
  526. }
  527. if (len == 0 && !create_empty_fragment)
  528. return 0;
  529. wr= &(s->s3->wrec);
  530. wb= &(s->s3->wbuf);
  531. sess=s->session;
  532. if ( (sess == NULL) ||
  533. (s->enc_write_ctx == NULL) ||
  534. (s->write_hash == NULL))
  535. clear=1;
  536. if (clear)
  537. mac_size=0;
  538. else
  539. mac_size=EVP_MD_size(s->write_hash);
  540. /* 'create_empty_fragment' is true only when this function calls itself */
  541. if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done)
  542. {
  543. /* countermeasure against known-IV weakness in CBC ciphersuites
  544. * (see http://www.openssl.org/~bodo/tls-cbc.txt) */
  545. if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA)
  546. {
  547. /* recursive function call with 'create_empty_fragment' set;
  548. * this prepares and buffers the data for an empty fragment
  549. * (these 'prefix_len' bytes are sent out later
  550. * together with the actual payload) */
  551. prefix_len = do_ssl3_write(s, type, buf, 0, 1);
  552. if (prefix_len <= 0)
  553. goto err;
  554. if (s->s3->wbuf.len < (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE)
  555. {
  556. /* insufficient space */
  557. SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
  558. goto err;
  559. }
  560. }
  561. s->s3->empty_fragment_done = 1;
  562. }
  563. p = wb->buf + prefix_len;
  564. /* write the header */
  565. *(p++)=type&0xff;
  566. wr->type=type;
  567. *(p++)=(s->version>>8);
  568. *(p++)=s->version&0xff;
  569. /* field where we are to write out packet length */
  570. plen=p;
  571. p+=2;
  572. /* lets setup the record stuff. */
  573. wr->data=p;
  574. wr->length=(int)len;
  575. wr->input=(unsigned char *)buf;
  576. /* we now 'read' from wr->input, wr->length bytes into
  577. * wr->data */
  578. /* first we compress */
  579. if (s->compress != NULL)
  580. {
  581. if (!do_compress(s))
  582. {
  583. SSLerr(SSL_F_DO_SSL3_WRITE,SSL_R_COMPRESSION_FAILURE);
  584. goto err;
  585. }
  586. }
  587. else
  588. {
  589. memcpy(wr->data,wr->input,wr->length);
  590. wr->input=wr->data;
  591. }
  592. /* we should still have the output to wr->data and the input
  593. * from wr->input. Length should be wr->length.
  594. * wr->data still points in the wb->buf */
  595. if (mac_size != 0)
  596. {
  597. s->method->ssl3_enc->mac(s,&(p[wr->length]),1);
  598. wr->length+=mac_size;
  599. wr->input=p;
  600. wr->data=p;
  601. }
  602. /* ssl3_enc can only have an error on read */
  603. s->method->ssl3_enc->enc(s,1);
  604. /* record length after mac and block padding */
  605. s2n(wr->length,plen);
  606. /* we should now have
  607. * wr->data pointing to the encrypted data, which is
  608. * wr->length long */
  609. wr->type=type; /* not needed but helps for debugging */
  610. wr->length+=SSL3_RT_HEADER_LENGTH;
  611. if (create_empty_fragment)
  612. {
  613. /* we are in a recursive call;
  614. * just return the length, don't write out anything here
  615. */
  616. return wr->length;
  617. }
  618. /* now let's set up wb */
  619. wb->left = prefix_len + wr->length;
  620. wb->offset = 0;
  621. /* memorize arguments so that ssl3_write_pending can detect bad write retries later */
  622. s->s3->wpend_tot=len;
  623. s->s3->wpend_buf=buf;
  624. s->s3->wpend_type=type;
  625. s->s3->wpend_ret=len;
  626. /* we now just need to write the buffer */
  627. return ssl3_write_pending(s,type,buf,len);
  628. err:
  629. return -1;
  630. }
  631. /* if s->s3->wbuf.left != 0, we need to call this */
  632. static int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
  633. unsigned int len)
  634. {
  635. int i;
  636. /* XXXX */
  637. if ((s->s3->wpend_tot > (int)len)
  638. || ((s->s3->wpend_buf != buf) &&
  639. !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
  640. || (s->s3->wpend_type != type))
  641. {
  642. SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY);
  643. return(-1);
  644. }
  645. for (;;)
  646. {
  647. clear_sys_error();
  648. if (s->wbio != NULL)
  649. {
  650. s->rwstate=SSL_WRITING;
  651. i=BIO_write(s->wbio,
  652. (char *)&(s->s3->wbuf.buf[s->s3->wbuf.offset]),
  653. (unsigned int)s->s3->wbuf.left);
  654. }
  655. else
  656. {
  657. SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BIO_NOT_SET);
  658. i= -1;
  659. }
  660. if (i == s->s3->wbuf.left)
  661. {
  662. s->s3->wbuf.left=0;
  663. s->rwstate=SSL_NOTHING;
  664. return(s->s3->wpend_ret);
  665. }
  666. else if (i <= 0)
  667. return(i);
  668. s->s3->wbuf.offset+=i;
  669. s->s3->wbuf.left-=i;
  670. }
  671. }
  672. /* Return up to 'len' payload bytes received in 'type' records.
  673. * 'type' is one of the following:
  674. *
  675. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  676. * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
  677. * - 0 (during a shutdown, no data has to be returned)
  678. *
  679. * If we don't have stored data to work from, read a SSL/TLS record first
  680. * (possibly multiple records if we still don't have anything to return).
  681. *
  682. * This function must handle any surprises the peer may have for us, such as
  683. * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
  684. * a surprise, but handled as if it were), or renegotiation requests.
  685. * Also if record payloads contain fragments too small to process, we store
  686. * them until there is enough for the respective protocol (the record protocol
  687. * may use arbitrary fragmentation and even interleaving):
  688. * Change cipher spec protocol
  689. * just 1 byte needed, no need for keeping anything stored
  690. * Alert protocol
  691. * 2 bytes needed (AlertLevel, AlertDescription)
  692. * Handshake protocol
  693. * 4 bytes needed (HandshakeType, uint24 length) -- we just have
  694. * to detect unexpected Client Hello and Hello Request messages
  695. * here, anything else is handled by higher layers
  696. * Application data protocol
  697. * none of our business
  698. */
  699. int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
  700. {
  701. int al,i,j,ret;
  702. unsigned int n;
  703. SSL3_RECORD *rr;
  704. void (*cb)(const SSL *ssl,int type2,int val)=NULL;
  705. if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
  706. if (!ssl3_setup_buffers(s))
  707. return(-1);
  708. if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type) ||
  709. (peek && (type != SSL3_RT_APPLICATION_DATA)))
  710. {
  711. SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
  712. return -1;
  713. }
  714. if ((type == SSL3_RT_HANDSHAKE) && (s->s3->handshake_fragment_len > 0))
  715. /* (partially) satisfy request from storage */
  716. {
  717. unsigned char *src = s->s3->handshake_fragment;
  718. unsigned char *dst = buf;
  719. unsigned int k;
  720. /* peek == 0 */
  721. n = 0;
  722. while ((len > 0) && (s->s3->handshake_fragment_len > 0))
  723. {
  724. *dst++ = *src++;
  725. len--; s->s3->handshake_fragment_len--;
  726. n++;
  727. }
  728. /* move any remaining fragment bytes: */
  729. for (k = 0; k < s->s3->handshake_fragment_len; k++)
  730. s->s3->handshake_fragment[k] = *src++;
  731. return n;
  732. }
  733. /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
  734. if (!s->in_handshake && SSL_in_init(s))
  735. {
  736. /* type == SSL3_RT_APPLICATION_DATA */
  737. i=s->handshake_func(s);
  738. if (i < 0) return(i);
  739. if (i == 0)
  740. {
  741. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
  742. return(-1);
  743. }
  744. }
  745. start:
  746. s->rwstate=SSL_NOTHING;
  747. /* s->s3->rrec.type - is the type of record
  748. * s->s3->rrec.data, - data
  749. * s->s3->rrec.off, - offset into 'data' for next read
  750. * s->s3->rrec.length, - number of bytes. */
  751. rr = &(s->s3->rrec);
  752. /* get new packet if necessary */
  753. if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY))
  754. {
  755. ret=ssl3_get_record(s);
  756. if (ret <= 0) return(ret);
  757. }
  758. /* we now have a packet which can be read and processed */
  759. if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
  760. * reset by ssl3_get_finished */
  761. && (rr->type != SSL3_RT_HANDSHAKE))
  762. {
  763. al=SSL_AD_UNEXPECTED_MESSAGE;
  764. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
  765. goto err;
  766. }
  767. /* If the other end has shut down, throw anything we read away
  768. * (even in 'peek' mode) */
  769. if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
  770. {
  771. rr->length=0;
  772. s->rwstate=SSL_NOTHING;
  773. return(0);
  774. }
  775. if (type == rr->type) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
  776. {
  777. /* make sure that we are not getting application data when we
  778. * are doing a handshake for the first time */
  779. if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
  780. (s->enc_read_ctx == NULL))
  781. {
  782. al=SSL_AD_UNEXPECTED_MESSAGE;
  783. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE);
  784. goto f_err;
  785. }
  786. if (len <= 0) return(len);
  787. if ((unsigned int)len > rr->length)
  788. n = rr->length;
  789. else
  790. n = (unsigned int)len;
  791. memcpy(buf,&(rr->data[rr->off]),n);
  792. if (!peek)
  793. {
  794. rr->length-=n;
  795. rr->off+=n;
  796. if (rr->length == 0)
  797. {
  798. s->rstate=SSL_ST_READ_HEADER;
  799. rr->off=0;
  800. }
  801. }
  802. return(n);
  803. }
  804. /* If we get here, then type != rr->type; if we have a handshake
  805. * message, then it was unexpected (Hello Request or Client Hello). */
  806. /* In case of record types for which we have 'fragment' storage,
  807. * fill that so that we can process the data at a fixed place.
  808. */
  809. {
  810. unsigned int dest_maxlen = 0;
  811. unsigned char *dest = NULL;
  812. unsigned int *dest_len = NULL;
  813. if (rr->type == SSL3_RT_HANDSHAKE)
  814. {
  815. dest_maxlen = sizeof s->s3->handshake_fragment;
  816. dest = s->s3->handshake_fragment;
  817. dest_len = &s->s3->handshake_fragment_len;
  818. }
  819. else if (rr->type == SSL3_RT_ALERT)
  820. {
  821. dest_maxlen = sizeof s->s3->alert_fragment;
  822. dest = s->s3->alert_fragment;
  823. dest_len = &s->s3->alert_fragment_len;
  824. }
  825. if (dest_maxlen > 0)
  826. {
  827. n = dest_maxlen - *dest_len; /* available space in 'dest' */
  828. if (rr->length < n)
  829. n = rr->length; /* available bytes */
  830. /* now move 'n' bytes: */
  831. while (n-- > 0)
  832. {
  833. dest[(*dest_len)++] = rr->data[rr->off++];
  834. rr->length--;
  835. }
  836. if (*dest_len < dest_maxlen)
  837. goto start; /* fragment was too small */
  838. }
  839. }
  840. /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
  841. * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
  842. * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
  843. /* If we are a client, check for an incoming 'Hello Request': */
  844. if ((!s->server) &&
  845. (s->s3->handshake_fragment_len >= 4) &&
  846. (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
  847. (s->session != NULL) && (s->session->cipher != NULL))
  848. {
  849. s->s3->handshake_fragment_len = 0;
  850. if ((s->s3->handshake_fragment[1] != 0) ||
  851. (s->s3->handshake_fragment[2] != 0) ||
  852. (s->s3->handshake_fragment[3] != 0))
  853. {
  854. al=SSL_AD_DECODE_ERROR;
  855. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_HELLO_REQUEST);
  856. goto err;
  857. }
  858. if (s->msg_callback)
  859. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
  860. if (SSL_is_init_finished(s) &&
  861. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
  862. !s->s3->renegotiate)
  863. {
  864. ssl3_renegotiate(s);
  865. if (ssl3_renegotiate_check(s))
  866. {
  867. i=s->handshake_func(s);
  868. if (i < 0) return(i);
  869. if (i == 0)
  870. {
  871. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
  872. return(-1);
  873. }
  874. if (!(s->mode & SSL_MODE_AUTO_RETRY))
  875. {
  876. if (s->s3->rbuf.left == 0) /* no read-ahead left? */
  877. {
  878. BIO *bio;
  879. /* In the case where we try to read application data,
  880. * but we trigger an SSL handshake, we return -1 with
  881. * the retry option set. Otherwise renegotiation may
  882. * cause nasty problems in the blocking world */
  883. s->rwstate=SSL_READING;
  884. bio=SSL_get_rbio(s);
  885. BIO_clear_retry_flags(bio);
  886. BIO_set_retry_read(bio);
  887. return(-1);
  888. }
  889. }
  890. }
  891. }
  892. /* we either finished a handshake or ignored the request,
  893. * now try again to obtain the (application) data we were asked for */
  894. goto start;
  895. }
  896. if (s->s3->alert_fragment_len >= 2)
  897. {
  898. int alert_level = s->s3->alert_fragment[0];
  899. int alert_descr = s->s3->alert_fragment[1];
  900. s->s3->alert_fragment_len = 0;
  901. if (s->msg_callback)
  902. s->msg_callback(0, s->version, SSL3_RT_ALERT, s->s3->alert_fragment, 2, s, s->msg_callback_arg);
  903. if (s->info_callback != NULL)
  904. cb=s->info_callback;
  905. else if (s->ctx->info_callback != NULL)
  906. cb=s->ctx->info_callback;
  907. if (cb != NULL)
  908. {
  909. j = (alert_level << 8) | alert_descr;
  910. cb(s, SSL_CB_READ_ALERT, j);
  911. }
  912. if (alert_level == 1) /* warning */
  913. {
  914. s->s3->warn_alert = alert_descr;
  915. if (alert_descr == SSL_AD_CLOSE_NOTIFY)
  916. {
  917. s->shutdown |= SSL_RECEIVED_SHUTDOWN;
  918. return(0);
  919. }
  920. }
  921. else if (alert_level == 2) /* fatal */
  922. {
  923. char tmp[16];
  924. s->rwstate=SSL_NOTHING;
  925. s->s3->fatal_alert = alert_descr;
  926. SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
  927. BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr);
  928. ERR_add_error_data(2,"SSL alert number ",tmp);
  929. s->shutdown|=SSL_RECEIVED_SHUTDOWN;
  930. SSL_CTX_remove_session(s->ctx,s->session);
  931. return(0);
  932. }
  933. else
  934. {
  935. al=SSL_AD_ILLEGAL_PARAMETER;
  936. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE);
  937. goto f_err;
  938. }
  939. goto start;
  940. }
  941. if (s->shutdown & SSL_SENT_SHUTDOWN) /* but we have not received a shutdown */
  942. {
  943. s->rwstate=SSL_NOTHING;
  944. rr->length=0;
  945. return(0);
  946. }
  947. if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC)
  948. {
  949. /* 'Change Cipher Spec' is just a single byte, so we know
  950. * exactly what the record payload has to look like */
  951. if ( (rr->length != 1) || (rr->off != 0) ||
  952. (rr->data[0] != SSL3_MT_CCS))
  953. {
  954. i=SSL_AD_ILLEGAL_PARAMETER;
  955. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC);
  956. goto err;
  957. }
  958. /* Check we have a cipher to change to */
  959. if (s->s3->tmp.new_cipher == NULL)
  960. {
  961. i=SSL_AD_UNEXPECTED_MESSAGE;
  962. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
  963. goto err;
  964. }
  965. rr->length=0;
  966. if (s->msg_callback)
  967. s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, s->msg_callback_arg);
  968. s->s3->change_cipher_spec=1;
  969. if (!do_change_cipher_spec(s))
  970. goto err;
  971. else
  972. goto start;
  973. }
  974. /* Unexpected handshake message (Client Hello, or protocol violation) */
  975. if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake)
  976. {
  977. if (((s->state&SSL_ST_MASK) == SSL_ST_OK) &&
  978. !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS))
  979. {
  980. #if 0 /* worked only because C operator preferences are not as expected (and
  981. * because this is not really needed for clients except for detecting
  982. * protocol violations): */
  983. s->state=SSL_ST_BEFORE|(s->server)
  984. ?SSL_ST_ACCEPT
  985. :SSL_ST_CONNECT;
  986. #else
  987. s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
  988. #endif
  989. s->new_session=1;
  990. }
  991. i=s->handshake_func(s);
  992. if (i < 0) return(i);
  993. if (i == 0)
  994. {
  995. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
  996. return(-1);
  997. }
  998. if (!(s->mode & SSL_MODE_AUTO_RETRY))
  999. {
  1000. if (s->s3->rbuf.left == 0) /* no read-ahead left? */
  1001. {
  1002. BIO *bio;
  1003. /* In the case where we try to read application data,
  1004. * but we trigger an SSL handshake, we return -1 with
  1005. * the retry option set. Otherwise renegotiation may
  1006. * cause nasty problems in the blocking world */
  1007. s->rwstate=SSL_READING;
  1008. bio=SSL_get_rbio(s);
  1009. BIO_clear_retry_flags(bio);
  1010. BIO_set_retry_read(bio);
  1011. return(-1);
  1012. }
  1013. }
  1014. goto start;
  1015. }
  1016. switch (rr->type)
  1017. {
  1018. default:
  1019. #ifndef OPENSSL_NO_TLS
  1020. /* TLS just ignores unknown message types */
  1021. if (s->version == TLS1_VERSION)
  1022. {
  1023. rr->length = 0;
  1024. goto start;
  1025. }
  1026. #endif
  1027. al=SSL_AD_UNEXPECTED_MESSAGE;
  1028. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
  1029. goto f_err;
  1030. case SSL3_RT_CHANGE_CIPHER_SPEC:
  1031. case SSL3_RT_ALERT:
  1032. case SSL3_RT_HANDSHAKE:
  1033. /* we already handled all of these, with the possible exception
  1034. * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that
  1035. * should not happen when type != rr->type */
  1036. al=SSL_AD_UNEXPECTED_MESSAGE;
  1037. SSLerr(SSL_F_SSL3_READ_BYTES,ERR_R_INTERNAL_ERROR);
  1038. goto f_err;
  1039. case SSL3_RT_APPLICATION_DATA:
  1040. /* At this point, we were expecting handshake data,
  1041. * but have application data. If the library was
  1042. * running inside ssl3_read() (i.e. in_read_app_data
  1043. * is set) and it makes sense to read application data
  1044. * at this point (session renegotiation not yet started),
  1045. * we will indulge it.
  1046. */
  1047. if (s->s3->in_read_app_data &&
  1048. (s->s3->total_renegotiations != 0) &&
  1049. ((
  1050. (s->state & SSL_ST_CONNECT) &&
  1051. (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
  1052. (s->state <= SSL3_ST_CR_SRVR_HELLO_A)
  1053. ) || (
  1054. (s->state & SSL_ST_ACCEPT) &&
  1055. (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
  1056. (s->state >= SSL3_ST_SR_CLNT_HELLO_A)
  1057. )
  1058. ))
  1059. {
  1060. s->s3->in_read_app_data=2;
  1061. return(-1);
  1062. }
  1063. else
  1064. {
  1065. al=SSL_AD_UNEXPECTED_MESSAGE;
  1066. SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
  1067. goto f_err;
  1068. }
  1069. }
  1070. /* not reached */
  1071. f_err:
  1072. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1073. err:
  1074. return(-1);
  1075. }
  1076. static int do_change_cipher_spec(SSL *s)
  1077. {
  1078. int i;
  1079. const char *sender;
  1080. int slen;
  1081. if (s->state & SSL_ST_ACCEPT)
  1082. i=SSL3_CHANGE_CIPHER_SERVER_READ;
  1083. else
  1084. i=SSL3_CHANGE_CIPHER_CLIENT_READ;
  1085. if (s->s3->tmp.key_block == NULL)
  1086. {
  1087. s->session->cipher=s->s3->tmp.new_cipher;
  1088. if (!s->method->ssl3_enc->setup_key_block(s)) return(0);
  1089. }
  1090. if (!s->method->ssl3_enc->change_cipher_state(s,i))
  1091. return(0);
  1092. /* we have to record the message digest at
  1093. * this point so we can get it before we read
  1094. * the finished message */
  1095. if (s->state & SSL_ST_CONNECT)
  1096. {
  1097. sender=s->method->ssl3_enc->server_finished_label;
  1098. slen=s->method->ssl3_enc->server_finished_label_len;
  1099. }
  1100. else
  1101. {
  1102. sender=s->method->ssl3_enc->client_finished_label;
  1103. slen=s->method->ssl3_enc->client_finished_label_len;
  1104. }
  1105. s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
  1106. &(s->s3->finish_dgst1),
  1107. &(s->s3->finish_dgst2),
  1108. sender,slen,s->s3->tmp.peer_finish_md);
  1109. return(1);
  1110. }
  1111. void ssl3_send_alert(SSL *s, int level, int desc)
  1112. {
  1113. /* Map tls/ssl alert value to correct one */
  1114. desc=s->method->ssl3_enc->alert_value(desc);
  1115. if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
  1116. desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have protocol_version alerts */
  1117. if (desc < 0) return;
  1118. /* If a fatal one, remove from cache */
  1119. if ((level == 2) && (s->session != NULL))
  1120. SSL_CTX_remove_session(s->ctx,s->session);
  1121. s->s3->alert_dispatch=1;
  1122. s->s3->send_alert[0]=level;
  1123. s->s3->send_alert[1]=desc;
  1124. if (s->s3->wbuf.left == 0) /* data still being written out? */
  1125. ssl3_dispatch_alert(s);
  1126. /* else data is still being written out, we will get written
  1127. * some time in the future */
  1128. }
  1129. int ssl3_dispatch_alert(SSL *s)
  1130. {
  1131. int i,j;
  1132. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  1133. s->s3->alert_dispatch=0;
  1134. i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
  1135. if (i <= 0)
  1136. {
  1137. s->s3->alert_dispatch=1;
  1138. }
  1139. else
  1140. {
  1141. /* Alert sent to BIO. If it is important, flush it now.
  1142. * If the message does not get sent due to non-blocking IO,
  1143. * we will not worry too much. */
  1144. if (s->s3->send_alert[0] == SSL3_AL_FATAL)
  1145. (void)BIO_flush(s->wbio);
  1146. if (s->msg_callback)
  1147. s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, s->msg_callback_arg);
  1148. if (s->info_callback != NULL)
  1149. cb=s->info_callback;
  1150. else if (s->ctx->info_callback != NULL)
  1151. cb=s->ctx->info_callback;
  1152. if (cb != NULL)
  1153. {
  1154. j=(s->s3->send_alert[0]<<8)|s->s3->send_alert[1];
  1155. cb(s,SSL_CB_WRITE_ALERT,j);
  1156. }
  1157. }
  1158. return(i);
  1159. }