s3_pkt.c 36 KB

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