s2_pkt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /* ssl/s2_pkt.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * [email protected].
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  109. *
  110. */
  111. #include "ssl_locl.h"
  112. #ifndef OPENSSL_NO_SSL2
  113. #include <stdio.h>
  114. #include <errno.h>
  115. #define USE_SOCKETS
  116. static int read_n(SSL *s,unsigned int n,unsigned int max,unsigned int extend);
  117. static int n_do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len);
  118. static int write_pending(SSL *s, const unsigned char *buf, unsigned int len);
  119. static int ssl_mt_error(int n);
  120. /* SSL 2.0 imlementation for SSL_read/SSL_peek -
  121. * This routine will return 0 to len bytes, decrypted etc if required.
  122. */
  123. static int ssl2_read_internal(SSL *s, void *buf, int len, int peek)
  124. {
  125. int n;
  126. unsigned char mac[MAX_MAC_SIZE];
  127. unsigned char *p;
  128. int i;
  129. int mac_size;
  130. ssl2_read_again:
  131. if (SSL_in_init(s) && !s->in_handshake)
  132. {
  133. n=s->handshake_func(s);
  134. if (n < 0) return(n);
  135. if (n == 0)
  136. {
  137. SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_SSL_HANDSHAKE_FAILURE);
  138. return(-1);
  139. }
  140. }
  141. clear_sys_error();
  142. s->rwstate=SSL_NOTHING;
  143. if (len <= 0) return(len);
  144. if (s->s2->ract_data_length != 0) /* read from buffer */
  145. {
  146. if (len > s->s2->ract_data_length)
  147. n=s->s2->ract_data_length;
  148. else
  149. n=len;
  150. memcpy(buf,s->s2->ract_data,(unsigned int)n);
  151. if (!peek)
  152. {
  153. s->s2->ract_data_length-=n;
  154. s->s2->ract_data+=n;
  155. if (s->s2->ract_data_length == 0)
  156. s->rstate=SSL_ST_READ_HEADER;
  157. }
  158. return(n);
  159. }
  160. /* s->s2->ract_data_length == 0
  161. *
  162. * Fill the buffer, then goto ssl2_read_again.
  163. */
  164. if (s->rstate == SSL_ST_READ_HEADER)
  165. {
  166. if (s->first_packet)
  167. {
  168. n=read_n(s,5,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);
  169. if (n <= 0) return(n); /* error or non-blocking */
  170. s->first_packet=0;
  171. p=s->packet;
  172. if (!((p[0] & 0x80) && (
  173. (p[2] == SSL2_MT_CLIENT_HELLO) ||
  174. (p[2] == SSL2_MT_SERVER_HELLO))))
  175. {
  176. SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_NON_SSLV2_INITIAL_PACKET);
  177. return(-1);
  178. }
  179. }
  180. else
  181. {
  182. n=read_n(s,2,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);
  183. if (n <= 0) return(n); /* error or non-blocking */
  184. }
  185. /* part read stuff */
  186. s->rstate=SSL_ST_READ_BODY;
  187. p=s->packet;
  188. /* Do header */
  189. /*s->s2->padding=0;*/
  190. s->s2->escape=0;
  191. s->s2->rlength=(((unsigned int)p[0])<<8)|((unsigned int)p[1]);
  192. if ((p[0] & TWO_BYTE_BIT)) /* Two byte header? */
  193. {
  194. s->s2->three_byte_header=0;
  195. s->s2->rlength&=TWO_BYTE_MASK;
  196. }
  197. else
  198. {
  199. s->s2->three_byte_header=1;
  200. s->s2->rlength&=THREE_BYTE_MASK;
  201. /* security >s2->escape */
  202. s->s2->escape=((p[0] & SEC_ESC_BIT))?1:0;
  203. }
  204. }
  205. if (s->rstate == SSL_ST_READ_BODY)
  206. {
  207. n=s->s2->rlength+2+s->s2->three_byte_header;
  208. if (n > (int)s->packet_length)
  209. {
  210. n-=s->packet_length;
  211. i=read_n(s,(unsigned int)n,(unsigned int)n,1);
  212. if (i <= 0) return(i); /* ERROR */
  213. }
  214. p= &(s->packet[2]);
  215. s->rstate=SSL_ST_READ_HEADER;
  216. if (s->s2->three_byte_header)
  217. s->s2->padding= *(p++);
  218. else s->s2->padding=0;
  219. /* Data portion */
  220. if (s->s2->clear_text)
  221. {
  222. mac_size = 0;
  223. s->s2->mac_data=p;
  224. s->s2->ract_data=p;
  225. if (s->s2->padding)
  226. {
  227. SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING);
  228. return(-1);
  229. }
  230. }
  231. else
  232. {
  233. mac_size=EVP_MD_CTX_size(s->read_hash);
  234. if (mac_size < 0)
  235. return -1;
  236. OPENSSL_assert(mac_size <= MAX_MAC_SIZE);
  237. s->s2->mac_data=p;
  238. s->s2->ract_data= &p[mac_size];
  239. if (s->s2->padding + mac_size > s->s2->rlength)
  240. {
  241. SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING);
  242. return(-1);
  243. }
  244. }
  245. s->s2->ract_data_length=s->s2->rlength;
  246. /* added a check for length > max_size in case
  247. * encryption was not turned on yet due to an error */
  248. if ((!s->s2->clear_text) &&
  249. (s->s2->rlength >= (unsigned int)mac_size))
  250. {
  251. ssl2_enc(s,0);
  252. s->s2->ract_data_length-=mac_size;
  253. ssl2_mac(s,mac,0);
  254. s->s2->ract_data_length-=s->s2->padding;
  255. if ( (CRYPTO_memcmp(mac,s->s2->mac_data,mac_size) != 0) ||
  256. (s->s2->rlength%EVP_CIPHER_CTX_block_size(s->enc_read_ctx) != 0))
  257. {
  258. SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_MAC_DECODE);
  259. return(-1);
  260. }
  261. }
  262. INC32(s->s2->read_sequence); /* expect next number */
  263. /* s->s2->ract_data is now available for processing */
  264. /* Possibly the packet that we just read had 0 actual data bytes.
  265. * (SSLeay/OpenSSL itself never sends such packets; see ssl2_write.)
  266. * In this case, returning 0 would be interpreted by the caller
  267. * as indicating EOF, so it's not a good idea. Instead, we just
  268. * continue reading; thus ssl2_read_internal may have to process
  269. * multiple packets before it can return.
  270. *
  271. * [Note that using select() for blocking sockets *never* guarantees
  272. * that the next SSL_read will not block -- the available
  273. * data may contain incomplete packets, and except for SSL 2,
  274. * renegotiation can confuse things even more.] */
  275. goto ssl2_read_again; /* This should really be
  276. * "return ssl2_read(s,buf,len)",
  277. * but that would allow for
  278. * denial-of-service attacks if a
  279. * C compiler is used that does not
  280. * recognize end-recursion. */
  281. }
  282. else
  283. {
  284. SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_STATE);
  285. return(-1);
  286. }
  287. }
  288. int ssl2_read(SSL *s, void *buf, int len)
  289. {
  290. return ssl2_read_internal(s, buf, len, 0);
  291. }
  292. int ssl2_peek(SSL *s, void *buf, int len)
  293. {
  294. return ssl2_read_internal(s, buf, len, 1);
  295. }
  296. static int read_n(SSL *s, unsigned int n, unsigned int max,
  297. unsigned int extend)
  298. {
  299. int i,off,newb;
  300. /* if there is stuff still in the buffer from a previous read,
  301. * and there is more than we want, take some. */
  302. if (s->s2->rbuf_left >= (int)n)
  303. {
  304. if (extend)
  305. s->packet_length+=n;
  306. else
  307. {
  308. s->packet= &(s->s2->rbuf[s->s2->rbuf_offs]);
  309. s->packet_length=n;
  310. }
  311. s->s2->rbuf_left-=n;
  312. s->s2->rbuf_offs+=n;
  313. return(n);
  314. }
  315. if (!s->read_ahead) max=n;
  316. if (max > (unsigned int)(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2))
  317. max=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2;
  318. /* Else we want more than we have.
  319. * First, if there is some left or we want to extend */
  320. off=0;
  321. if ((s->s2->rbuf_left != 0) || ((s->packet_length != 0) && extend))
  322. {
  323. newb=s->s2->rbuf_left;
  324. if (extend)
  325. {
  326. off=s->packet_length;
  327. if (s->packet != s->s2->rbuf)
  328. memcpy(s->s2->rbuf,s->packet,
  329. (unsigned int)newb+off);
  330. }
  331. else if (s->s2->rbuf_offs != 0)
  332. {
  333. memcpy(s->s2->rbuf,&(s->s2->rbuf[s->s2->rbuf_offs]),
  334. (unsigned int)newb);
  335. s->s2->rbuf_offs=0;
  336. }
  337. s->s2->rbuf_left=0;
  338. }
  339. else
  340. newb=0;
  341. /* off is the offset to start writing too.
  342. * r->s2->rbuf_offs is the 'unread data', now 0.
  343. * newb is the number of new bytes so far
  344. */
  345. s->packet=s->s2->rbuf;
  346. while (newb < (int)n)
  347. {
  348. clear_sys_error();
  349. if (s->rbio != NULL)
  350. {
  351. s->rwstate=SSL_READING;
  352. i=BIO_read(s->rbio,(char *)&(s->s2->rbuf[off+newb]),
  353. max-newb);
  354. }
  355. else
  356. {
  357. SSLerr(SSL_F_READ_N,SSL_R_READ_BIO_NOT_SET);
  358. i= -1;
  359. }
  360. #ifdef PKT_DEBUG
  361. if (s->debug & 0x01) sleep(1);
  362. #endif
  363. if (i <= 0)
  364. {
  365. s->s2->rbuf_left+=newb;
  366. return(i);
  367. }
  368. newb+=i;
  369. }
  370. /* record unread data */
  371. if (newb > (int)n)
  372. {
  373. s->s2->rbuf_offs=n+off;
  374. s->s2->rbuf_left=newb-n;
  375. }
  376. else
  377. {
  378. s->s2->rbuf_offs=0;
  379. s->s2->rbuf_left=0;
  380. }
  381. if (extend)
  382. s->packet_length+=n;
  383. else
  384. s->packet_length=n;
  385. s->rwstate=SSL_NOTHING;
  386. return(n);
  387. }
  388. int ssl2_write(SSL *s, const void *_buf, int len)
  389. {
  390. const unsigned char *buf=_buf;
  391. unsigned int n,tot;
  392. int i;
  393. if (SSL_in_init(s) && !s->in_handshake)
  394. {
  395. i=s->handshake_func(s);
  396. if (i < 0) return(i);
  397. if (i == 0)
  398. {
  399. SSLerr(SSL_F_SSL2_WRITE,SSL_R_SSL_HANDSHAKE_FAILURE);
  400. return(-1);
  401. }
  402. }
  403. if (s->error)
  404. {
  405. ssl2_write_error(s);
  406. if (s->error)
  407. return(-1);
  408. }
  409. clear_sys_error();
  410. s->rwstate=SSL_NOTHING;
  411. if (len <= 0) return(len);
  412. tot=s->s2->wnum;
  413. s->s2->wnum=0;
  414. n=(len-tot);
  415. for (;;)
  416. {
  417. i=n_do_ssl_write(s,&(buf[tot]),n);
  418. if (i <= 0)
  419. {
  420. s->s2->wnum=tot;
  421. return(i);
  422. }
  423. if ((i == (int)n) ||
  424. (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))
  425. {
  426. return(tot+i);
  427. }
  428. n-=i;
  429. tot+=i;
  430. }
  431. }
  432. static int write_pending(SSL *s, const unsigned char *buf, unsigned int len)
  433. {
  434. int i;
  435. /* s->s2->wpend_len != 0 MUST be true. */
  436. /* check that they have given us the same buffer to
  437. * write */
  438. if ((s->s2->wpend_tot > (int)len) ||
  439. ((s->s2->wpend_buf != buf) &&
  440. !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)))
  441. {
  442. SSLerr(SSL_F_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY);
  443. return(-1);
  444. }
  445. for (;;)
  446. {
  447. clear_sys_error();
  448. if (s->wbio != NULL)
  449. {
  450. s->rwstate=SSL_WRITING;
  451. i=BIO_write(s->wbio,
  452. (char *)&(s->s2->write_ptr[s->s2->wpend_off]),
  453. (unsigned int)s->s2->wpend_len);
  454. }
  455. else
  456. {
  457. SSLerr(SSL_F_WRITE_PENDING,SSL_R_WRITE_BIO_NOT_SET);
  458. i= -1;
  459. }
  460. #ifdef PKT_DEBUG
  461. if (s->debug & 0x01) sleep(1);
  462. #endif
  463. if (i == s->s2->wpend_len)
  464. {
  465. s->s2->wpend_len=0;
  466. s->rwstate=SSL_NOTHING;
  467. return(s->s2->wpend_ret);
  468. }
  469. else if (i <= 0)
  470. return(i);
  471. s->s2->wpend_off+=i;
  472. s->s2->wpend_len-=i;
  473. }
  474. }
  475. static int n_do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len)
  476. {
  477. unsigned int j,k,olen,p,bs;
  478. int mac_size;
  479. register unsigned char *pp;
  480. olen=len;
  481. /* first check if there is data from an encryption waiting to
  482. * be sent - it must be sent because the other end is waiting.
  483. * This will happen with non-blocking IO. We print it and then
  484. * return.
  485. */
  486. if (s->s2->wpend_len != 0) return(write_pending(s,buf,len));
  487. /* set mac_size to mac size */
  488. if (s->s2->clear_text)
  489. mac_size=0;
  490. else
  491. {
  492. mac_size=EVP_MD_CTX_size(s->write_hash);
  493. if (mac_size < 0)
  494. return -1;
  495. }
  496. /* lets set the pad p */
  497. if (s->s2->clear_text)
  498. {
  499. if (len > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
  500. len=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
  501. p=0;
  502. s->s2->three_byte_header=0;
  503. /* len=len; */
  504. }
  505. else
  506. {
  507. bs=EVP_CIPHER_CTX_block_size(s->enc_read_ctx);
  508. j=len+mac_size;
  509. /* Two-byte headers allow for a larger record length than
  510. * three-byte headers, but we can't use them if we need
  511. * padding or if we have to set the escape bit. */
  512. if ((j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) &&
  513. (!s->s2->escape))
  514. {
  515. if (j > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
  516. j=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
  517. /* set k to the max number of bytes with 2
  518. * byte header */
  519. k=j-(j%bs);
  520. /* how many data bytes? */
  521. len=k-mac_size;
  522. s->s2->three_byte_header=0;
  523. p=0;
  524. }
  525. else if ((bs <= 1) && (!s->s2->escape))
  526. {
  527. /* j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, thus
  528. * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER */
  529. s->s2->three_byte_header=0;
  530. p=0;
  531. }
  532. else /* we may have to use a 3 byte header */
  533. {
  534. /* If s->s2->escape is not set, then
  535. * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, and thus
  536. * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER. */
  537. p=(j%bs);
  538. p=(p == 0)?0:(bs-p);
  539. if (s->s2->escape)
  540. {
  541. s->s2->three_byte_header=1;
  542. if (j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  543. j=SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER;
  544. }
  545. else
  546. s->s2->three_byte_header=(p == 0)?0:1;
  547. }
  548. }
  549. /* Now
  550. * j <= SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER
  551. * holds, and if s->s2->three_byte_header is set, then even
  552. * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER.
  553. */
  554. /* mac_size is the number of MAC bytes
  555. * len is the number of data bytes we are going to send
  556. * p is the number of padding bytes
  557. * (if it is a two-byte header, then p == 0) */
  558. s->s2->wlength=len;
  559. s->s2->padding=p;
  560. s->s2->mac_data= &(s->s2->wbuf[3]);
  561. s->s2->wact_data= &(s->s2->wbuf[3+mac_size]);
  562. /* we copy the data into s->s2->wbuf */
  563. memcpy(s->s2->wact_data,buf,len);
  564. if (p)
  565. memset(&(s->s2->wact_data[len]),0,p); /* arbitrary padding */
  566. if (!s->s2->clear_text)
  567. {
  568. s->s2->wact_data_length=len+p;
  569. ssl2_mac(s,s->s2->mac_data,1);
  570. s->s2->wlength+=p+mac_size;
  571. ssl2_enc(s,1);
  572. }
  573. /* package up the header */
  574. s->s2->wpend_len=s->s2->wlength;
  575. if (s->s2->three_byte_header) /* 3 byte header */
  576. {
  577. pp=s->s2->mac_data;
  578. pp-=3;
  579. pp[0]=(s->s2->wlength>>8)&(THREE_BYTE_MASK>>8);
  580. if (s->s2->escape) pp[0]|=SEC_ESC_BIT;
  581. pp[1]=s->s2->wlength&0xff;
  582. pp[2]=s->s2->padding;
  583. s->s2->wpend_len+=3;
  584. }
  585. else
  586. {
  587. pp=s->s2->mac_data;
  588. pp-=2;
  589. pp[0]=((s->s2->wlength>>8)&(TWO_BYTE_MASK>>8))|TWO_BYTE_BIT;
  590. pp[1]=s->s2->wlength&0xff;
  591. s->s2->wpend_len+=2;
  592. }
  593. s->s2->write_ptr=pp;
  594. INC32(s->s2->write_sequence); /* expect next number */
  595. /* lets try to actually write the data */
  596. s->s2->wpend_tot=olen;
  597. s->s2->wpend_buf=buf;
  598. s->s2->wpend_ret=len;
  599. s->s2->wpend_off=0;
  600. return(write_pending(s,buf,olen));
  601. }
  602. int ssl2_part_read(SSL *s, unsigned long f, int i)
  603. {
  604. unsigned char *p;
  605. int j;
  606. if (i < 0)
  607. {
  608. /* ssl2_return_error(s); */
  609. /* for non-blocking io,
  610. * this is not necessarily fatal */
  611. return(i);
  612. }
  613. else
  614. {
  615. s->init_num+=i;
  616. /* Check for error. While there are recoverable errors,
  617. * this function is not called when those must be expected;
  618. * any error detected here is fatal. */
  619. if (s->init_num >= 3)
  620. {
  621. p=(unsigned char *)s->init_buf->data;
  622. if (p[0] == SSL2_MT_ERROR)
  623. {
  624. j=(p[1]<<8)|p[2];
  625. SSLerr((int)f,ssl_mt_error(j));
  626. s->init_num -= 3;
  627. if (s->init_num > 0)
  628. memmove(p, p+3, s->init_num);
  629. }
  630. }
  631. /* If it's not an error message, we have some error anyway --
  632. * the message was shorter than expected. This too is treated
  633. * as fatal (at least if SSL_get_error is asked for its opinion). */
  634. return(0);
  635. }
  636. }
  637. int ssl2_do_write(SSL *s)
  638. {
  639. int ret;
  640. ret=ssl2_write(s,&s->init_buf->data[s->init_off],s->init_num);
  641. if (ret == s->init_num)
  642. {
  643. if (s->msg_callback)
  644. s->msg_callback(1, s->version, 0, s->init_buf->data, (size_t)(s->init_off + s->init_num), s, s->msg_callback_arg);
  645. return(1);
  646. }
  647. if (ret < 0)
  648. return(-1);
  649. s->init_off+=ret;
  650. s->init_num-=ret;
  651. return(0);
  652. }
  653. static int ssl_mt_error(int n)
  654. {
  655. int ret;
  656. switch (n)
  657. {
  658. case SSL2_PE_NO_CIPHER:
  659. ret=SSL_R_PEER_ERROR_NO_CIPHER;
  660. break;
  661. case SSL2_PE_NO_CERTIFICATE:
  662. ret=SSL_R_PEER_ERROR_NO_CERTIFICATE;
  663. break;
  664. case SSL2_PE_BAD_CERTIFICATE:
  665. ret=SSL_R_PEER_ERROR_CERTIFICATE;
  666. break;
  667. case SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE:
  668. ret=SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE;
  669. break;
  670. default:
  671. ret=SSL_R_UNKNOWN_REMOTE_ERROR_TYPE;
  672. break;
  673. }
  674. return(ret);
  675. }
  676. #else /* !OPENSSL_NO_SSL2 */
  677. # if PEDANTIC
  678. static void *dummy=&dummy;
  679. # endif
  680. #endif