s3_pkt.c 37 KB

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