s2_pkt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /* ssl/s2_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-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. * 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 "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,
  117. unsigned int extend);
  118. static int n_do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len);
  119. static int write_pending(SSL *s, const unsigned char *buf, unsigned int len);
  120. static int ssl_mt_error(int n);
  121. /*
  122. * SSL 2.0 imlementation for SSL_read/SSL_peek - This routine will return 0
  123. * to len bytes, decrypted etc if required.
  124. */
  125. static int ssl2_read_internal(SSL *s, void *buf, int len, int peek)
  126. {
  127. int n;
  128. unsigned char mac[MAX_MAC_SIZE];
  129. unsigned char *p;
  130. int i;
  131. int mac_size;
  132. ssl2_read_again:
  133. if (SSL_in_init(s) && !s->in_handshake) {
  134. n = s->handshake_func(s);
  135. if (n < 0)
  136. return (n);
  137. if (n == 0) {
  138. SSLerr(SSL_F_SSL2_READ_INTERNAL, SSL_R_SSL_HANDSHAKE_FAILURE);
  139. return (-1);
  140. }
  141. }
  142. clear_sys_error();
  143. s->rwstate = SSL_NOTHING;
  144. if (len <= 0)
  145. return (len);
  146. if (s->s2->ract_data_length != 0) { /* read from buffer */
  147. if (len > s->s2->ract_data_length)
  148. n = s->s2->ract_data_length;
  149. else
  150. n = len;
  151. memcpy(buf, s->s2->ract_data, (unsigned int)n);
  152. if (!peek) {
  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. /*
  161. * s->s2->ract_data_length == 0 Fill the buffer, then goto
  162. * ssl2_read_again.
  163. */
  164. if (s->rstate == SSL_ST_READ_HEADER) {
  165. if (s->first_packet) {
  166. n = read_n(s, 5, SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2, 0);
  167. if (n <= 0)
  168. return (n); /* error or non-blocking */
  169. s->first_packet = 0;
  170. p = s->packet;
  171. if (!((p[0] & 0x80) && ((p[2] == SSL2_MT_CLIENT_HELLO) ||
  172. (p[2] == SSL2_MT_SERVER_HELLO)))) {
  173. SSLerr(SSL_F_SSL2_READ_INTERNAL,
  174. SSL_R_NON_SSLV2_INITIAL_PACKET);
  175. return (-1);
  176. }
  177. } else {
  178. n = read_n(s, 2, SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2, 0);
  179. if (n <= 0)
  180. return (n); /* error or non-blocking */
  181. }
  182. /* part read stuff */
  183. s->rstate = SSL_ST_READ_BODY;
  184. p = s->packet;
  185. /* Do header */
  186. /*
  187. * s->s2->padding=0;
  188. */
  189. s->s2->escape = 0;
  190. s->s2->rlength = (((unsigned int)p[0]) << 8) | ((unsigned int)p[1]);
  191. if ((p[0] & TWO_BYTE_BIT)) { /* Two byte header? */
  192. s->s2->three_byte_header = 0;
  193. s->s2->rlength &= TWO_BYTE_MASK;
  194. } else {
  195. s->s2->three_byte_header = 1;
  196. s->s2->rlength &= THREE_BYTE_MASK;
  197. /* security >s2->escape */
  198. s->s2->escape = ((p[0] & SEC_ESC_BIT)) ? 1 : 0;
  199. }
  200. }
  201. if (s->rstate == SSL_ST_READ_BODY) {
  202. n = s->s2->rlength + 2 + s->s2->three_byte_header;
  203. if (n > (int)s->packet_length) {
  204. n -= s->packet_length;
  205. i = read_n(s, (unsigned int)n, (unsigned int)n, 1);
  206. if (i <= 0)
  207. return (i); /* ERROR */
  208. }
  209. p = &(s->packet[2]);
  210. s->rstate = SSL_ST_READ_HEADER;
  211. if (s->s2->three_byte_header)
  212. s->s2->padding = *(p++);
  213. else
  214. s->s2->padding = 0;
  215. /* Data portion */
  216. if (s->s2->clear_text) {
  217. mac_size = 0;
  218. s->s2->mac_data = p;
  219. s->s2->ract_data = p;
  220. if (s->s2->padding) {
  221. SSLerr(SSL_F_SSL2_READ_INTERNAL, SSL_R_ILLEGAL_PADDING);
  222. return (-1);
  223. }
  224. } else {
  225. mac_size = EVP_MD_CTX_size(s->read_hash);
  226. if (mac_size < 0)
  227. return -1;
  228. OPENSSL_assert(mac_size <= MAX_MAC_SIZE);
  229. s->s2->mac_data = p;
  230. s->s2->ract_data = &p[mac_size];
  231. if (s->s2->padding + mac_size > s->s2->rlength) {
  232. SSLerr(SSL_F_SSL2_READ_INTERNAL, SSL_R_ILLEGAL_PADDING);
  233. return (-1);
  234. }
  235. }
  236. s->s2->ract_data_length = s->s2->rlength;
  237. /*
  238. * added a check for length > max_size in case encryption was not
  239. * turned on yet due to an error
  240. */
  241. if ((!s->s2->clear_text) &&
  242. (s->s2->rlength >= (unsigned int)mac_size)) {
  243. if (!ssl2_enc(s, 0)) {
  244. SSLerr(SSL_F_SSL2_READ_INTERNAL, SSL_R_DECRYPTION_FAILED);
  245. return (-1);
  246. }
  247. s->s2->ract_data_length -= mac_size;
  248. ssl2_mac(s, mac, 0);
  249. s->s2->ract_data_length -= s->s2->padding;
  250. if ((CRYPTO_memcmp(mac, s->s2->mac_data, mac_size) != 0) ||
  251. (s->s2->rlength %
  252. EVP_CIPHER_CTX_block_size(s->enc_read_ctx) != 0)) {
  253. SSLerr(SSL_F_SSL2_READ_INTERNAL, SSL_R_BAD_MAC_DECODE);
  254. return (-1);
  255. }
  256. }
  257. INC32(s->s2->read_sequence); /* expect next number */
  258. /* s->s2->ract_data is now available for processing */
  259. /*
  260. * Possibly the packet that we just read had 0 actual data bytes.
  261. * (SSLeay/OpenSSL itself never sends such packets; see ssl2_write.)
  262. * In this case, returning 0 would be interpreted by the caller as
  263. * indicating EOF, so it's not a good idea. Instead, we just
  264. * continue reading; thus ssl2_read_internal may have to process
  265. * multiple packets before it can return. [Note that using select()
  266. * for blocking sockets *never* guarantees that the next SSL_read
  267. * will not block -- the available data may contain incomplete
  268. * packets, and except for SSL 2, renegotiation can confuse things
  269. * even more.]
  270. */
  271. goto ssl2_read_again; /* This should really be "return
  272. * ssl2_read(s,buf,len)", but that would
  273. * allow for denial-of-service attacks if a C
  274. * compiler is used that does not recognize
  275. * end-recursion. */
  276. } else {
  277. SSLerr(SSL_F_SSL2_READ_INTERNAL, SSL_R_BAD_STATE);
  278. return (-1);
  279. }
  280. }
  281. int ssl2_read(SSL *s, void *buf, int len)
  282. {
  283. return ssl2_read_internal(s, buf, len, 0);
  284. }
  285. int ssl2_peek(SSL *s, void *buf, int len)
  286. {
  287. return ssl2_read_internal(s, buf, len, 1);
  288. }
  289. /*
  290. * Return values are as per SSL_read()
  291. */
  292. static int read_n(SSL *s, unsigned int n, unsigned int max,
  293. unsigned int extend)
  294. {
  295. int i, off, newb;
  296. /*
  297. * if there is stuff still in the buffer from a previous read, and there
  298. * is more than we want, take some.
  299. */
  300. if (s->s2->rbuf_left >= (int)n) {
  301. if (extend)
  302. s->packet_length += n;
  303. else {
  304. s->packet = &(s->s2->rbuf[s->s2->rbuf_offs]);
  305. s->packet_length = n;
  306. }
  307. s->s2->rbuf_left -= n;
  308. s->s2->rbuf_offs += n;
  309. return (n);
  310. }
  311. if (!s->read_ahead)
  312. max = n;
  313. if (max > (unsigned int)(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2))
  314. max = SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2;
  315. /*
  316. * Else we want more than we have. First, if there is some left or we
  317. * want to extend
  318. */
  319. off = 0;
  320. if ((s->s2->rbuf_left != 0) || ((s->packet_length != 0) && extend)) {
  321. newb = s->s2->rbuf_left;
  322. if (extend) {
  323. off = s->packet_length;
  324. if (s->packet != s->s2->rbuf)
  325. memcpy(s->s2->rbuf, s->packet, (unsigned int)newb + off);
  326. } else if (s->s2->rbuf_offs != 0) {
  327. memcpy(s->s2->rbuf, &(s->s2->rbuf[s->s2->rbuf_offs]),
  328. (unsigned int)newb);
  329. s->s2->rbuf_offs = 0;
  330. }
  331. s->s2->rbuf_left = 0;
  332. } else
  333. newb = 0;
  334. /*
  335. * off is the offset to start writing too. r->s2->rbuf_offs is the
  336. * 'unread data', now 0. newb is the number of new bytes so far
  337. */
  338. s->packet = s->s2->rbuf;
  339. while (newb < (int)n) {
  340. clear_sys_error();
  341. if (s->rbio != NULL) {
  342. s->rwstate = SSL_READING;
  343. i = BIO_read(s->rbio, (char *)&(s->s2->rbuf[off + newb]),
  344. max - newb);
  345. } else {
  346. SSLerr(SSL_F_READ_N, SSL_R_READ_BIO_NOT_SET);
  347. i = -1;
  348. }
  349. # ifdef PKT_DEBUG
  350. if (s->debug & 0x01)
  351. sleep(1);
  352. # endif
  353. if (i <= 0) {
  354. s->s2->rbuf_left += newb;
  355. return i;
  356. }
  357. newb += i;
  358. }
  359. /* record unread data */
  360. if (newb > (int)n) {
  361. s->s2->rbuf_offs = n + off;
  362. s->s2->rbuf_left = newb - n;
  363. } else {
  364. s->s2->rbuf_offs = 0;
  365. s->s2->rbuf_left = 0;
  366. }
  367. if (extend)
  368. s->packet_length += n;
  369. else
  370. s->packet_length = n;
  371. s->rwstate = SSL_NOTHING;
  372. return (n);
  373. }
  374. int ssl2_write(SSL *s, const void *_buf, int len)
  375. {
  376. const unsigned char *buf = _buf;
  377. unsigned int n, tot;
  378. int i;
  379. if (SSL_in_init(s) && !s->in_handshake) {
  380. i = s->handshake_func(s);
  381. if (i < 0)
  382. return (i);
  383. if (i == 0) {
  384. SSLerr(SSL_F_SSL2_WRITE, SSL_R_SSL_HANDSHAKE_FAILURE);
  385. return (-1);
  386. }
  387. }
  388. if (s->error) {
  389. ssl2_write_error(s);
  390. if (s->error)
  391. return (-1);
  392. }
  393. clear_sys_error();
  394. s->rwstate = SSL_NOTHING;
  395. if (len <= 0)
  396. return (len);
  397. tot = s->s2->wnum;
  398. s->s2->wnum = 0;
  399. n = (len - tot);
  400. for (;;) {
  401. i = n_do_ssl_write(s, &(buf[tot]), n);
  402. if (i <= 0) {
  403. s->s2->wnum = tot;
  404. return (i);
  405. }
  406. if ((i == (int)n) || (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) {
  407. return (tot + i);
  408. }
  409. n -= i;
  410. tot += i;
  411. }
  412. }
  413. /*
  414. * Return values are as per SSL_write()
  415. */
  416. static int write_pending(SSL *s, const unsigned char *buf, unsigned int len)
  417. {
  418. int i;
  419. /* s->s2->wpend_len != 0 MUST be true. */
  420. /*
  421. * check that they have given us the same buffer to write
  422. */
  423. if ((s->s2->wpend_tot > (int)len) ||
  424. ((s->s2->wpend_buf != buf) &&
  425. !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))) {
  426. SSLerr(SSL_F_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
  427. return (-1);
  428. }
  429. for (;;) {
  430. clear_sys_error();
  431. if (s->wbio != NULL) {
  432. s->rwstate = SSL_WRITING;
  433. i = BIO_write(s->wbio,
  434. (char *)&(s->s2->write_ptr[s->s2->wpend_off]),
  435. (unsigned int)s->s2->wpend_len);
  436. } else {
  437. SSLerr(SSL_F_WRITE_PENDING, SSL_R_WRITE_BIO_NOT_SET);
  438. i = -1;
  439. }
  440. # ifdef PKT_DEBUG
  441. if (s->debug & 0x01)
  442. sleep(1);
  443. # endif
  444. if (i == s->s2->wpend_len) {
  445. s->s2->wpend_len = 0;
  446. s->rwstate = SSL_NOTHING;
  447. return (s->s2->wpend_ret);
  448. } else if (i <= 0)
  449. return i;
  450. s->s2->wpend_off += i;
  451. s->s2->wpend_len -= i;
  452. }
  453. }
  454. static int n_do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len)
  455. {
  456. unsigned int j, k, olen, p, bs;
  457. int mac_size;
  458. register unsigned char *pp;
  459. olen = len;
  460. /*
  461. * first check if there is data from an encryption waiting to be sent -
  462. * it must be sent because the other end is waiting. This will happen
  463. * with non-blocking IO. We print it and then return.
  464. */
  465. if (s->s2->wpend_len != 0)
  466. return (write_pending(s, buf, len));
  467. /* set mac_size to mac size */
  468. if (s->s2->clear_text)
  469. mac_size = 0;
  470. else {
  471. mac_size = EVP_MD_CTX_size(s->write_hash);
  472. if (mac_size < 0)
  473. return -1;
  474. }
  475. /* lets set the pad p */
  476. if (s->s2->clear_text) {
  477. if (len > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
  478. len = SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
  479. p = 0;
  480. s->s2->three_byte_header = 0;
  481. /* len=len; */
  482. } else {
  483. bs = EVP_CIPHER_CTX_block_size(s->enc_read_ctx);
  484. j = len + mac_size;
  485. /*
  486. * Two-byte headers allow for a larger record length than three-byte
  487. * headers, but we can't use them if we need padding or if we have to
  488. * set the escape bit.
  489. */
  490. if ((j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) && (!s->s2->escape)) {
  491. if (j > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
  492. j = SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
  493. /*
  494. * set k to the max number of bytes with 2 byte header
  495. */
  496. k = j - (j % bs);
  497. /* how many data bytes? */
  498. len = k - mac_size;
  499. s->s2->three_byte_header = 0;
  500. p = 0;
  501. } else if ((bs <= 1) && (!s->s2->escape)) {
  502. /*-
  503. * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, thus
  504. * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER
  505. */
  506. s->s2->three_byte_header = 0;
  507. p = 0;
  508. } else { /* we may have to use a 3 byte header */
  509. /*-
  510. * If s->s2->escape is not set, then
  511. * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, and thus
  512. * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER.
  513. */
  514. p = (j % bs);
  515. p = (p == 0) ? 0 : (bs - p);
  516. if (s->s2->escape) {
  517. s->s2->three_byte_header = 1;
  518. if (j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  519. j = SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER;
  520. } else
  521. s->s2->three_byte_header = (p == 0) ? 0 : 1;
  522. }
  523. }
  524. /*-
  525. * Now
  526. * j <= SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER
  527. * holds, and if s->s2->three_byte_header is set, then even
  528. * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER.
  529. */
  530. /*
  531. * mac_size is the number of MAC bytes len is the number of data bytes we
  532. * are going to send p is the number of padding bytes (if it is a
  533. * two-byte header, then p == 0)
  534. */
  535. s->s2->wlength = len;
  536. s->s2->padding = p;
  537. s->s2->mac_data = &(s->s2->wbuf[3]);
  538. s->s2->wact_data = &(s->s2->wbuf[3 + mac_size]);
  539. /*
  540. * It would be clearer to write this as follows:
  541. * if (mac_size + len + p > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
  542. * However |len| is user input that could in theory be very large. We
  543. * know |mac_size| and |p| are small, so to avoid any possibility of
  544. * overflow we write it like this.
  545. *
  546. * In theory this should never fail because the logic above should have
  547. * modified |len| if it is too big. But we are being cautious.
  548. */
  549. if (len > (SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER - (mac_size + p))) {
  550. return -1;
  551. }
  552. /* we copy the data into s->s2->wbuf */
  553. memcpy(s->s2->wact_data, buf, len);
  554. if (p)
  555. memset(&(s->s2->wact_data[len]), 0, p); /* arbitrary padding */
  556. if (!s->s2->clear_text) {
  557. s->s2->wact_data_length = len + p;
  558. ssl2_mac(s, s->s2->mac_data, 1);
  559. s->s2->wlength += p + mac_size;
  560. if (ssl2_enc(s, 1) < 1)
  561. return -1;
  562. }
  563. /* package up the header */
  564. s->s2->wpend_len = s->s2->wlength;
  565. if (s->s2->three_byte_header) { /* 3 byte header */
  566. pp = s->s2->mac_data;
  567. pp -= 3;
  568. pp[0] = (s->s2->wlength >> 8) & (THREE_BYTE_MASK >> 8);
  569. if (s->s2->escape)
  570. pp[0] |= SEC_ESC_BIT;
  571. pp[1] = s->s2->wlength & 0xff;
  572. pp[2] = s->s2->padding;
  573. s->s2->wpend_len += 3;
  574. } else {
  575. pp = s->s2->mac_data;
  576. pp -= 2;
  577. pp[0] = ((s->s2->wlength >> 8) & (TWO_BYTE_MASK >> 8)) | TWO_BYTE_BIT;
  578. pp[1] = s->s2->wlength & 0xff;
  579. s->s2->wpend_len += 2;
  580. }
  581. s->s2->write_ptr = pp;
  582. INC32(s->s2->write_sequence); /* expect next number */
  583. /* lets try to actually write the data */
  584. s->s2->wpend_tot = olen;
  585. s->s2->wpend_buf = buf;
  586. s->s2->wpend_ret = len;
  587. s->s2->wpend_off = 0;
  588. return (write_pending(s, buf, olen));
  589. }
  590. int ssl2_part_read(SSL *s, unsigned long f, int i)
  591. {
  592. unsigned char *p;
  593. int j;
  594. if (i < 0) {
  595. /* ssl2_return_error(s); */
  596. /*
  597. * for non-blocking io, this is not necessarily fatal
  598. */
  599. return (i);
  600. } else {
  601. s->init_num += i;
  602. /*
  603. * Check for error. While there are recoverable errors, this
  604. * function is not called when those must be expected; any error
  605. * detected here is fatal.
  606. */
  607. if (s->init_num >= 3) {
  608. p = (unsigned char *)s->init_buf->data;
  609. if (p[0] == SSL2_MT_ERROR) {
  610. j = (p[1] << 8) | p[2];
  611. SSLerr((int)f, ssl_mt_error(j));
  612. s->init_num -= 3;
  613. if (s->init_num > 0)
  614. memmove(p, p + 3, s->init_num);
  615. }
  616. }
  617. /*
  618. * If it's not an error message, we have some error anyway -- the
  619. * message was shorter than expected. This too is treated as fatal
  620. * (at least if SSL_get_error is asked for its opinion).
  621. */
  622. return (0);
  623. }
  624. }
  625. int ssl2_do_write(SSL *s)
  626. {
  627. int ret;
  628. ret = ssl2_write(s, &s->init_buf->data[s->init_off], s->init_num);
  629. if (ret == s->init_num) {
  630. if (s->msg_callback)
  631. s->msg_callback(1, s->version, 0, s->init_buf->data,
  632. (size_t)(s->init_off + s->init_num), s,
  633. s->msg_callback_arg);
  634. return (1);
  635. }
  636. if (ret < 0)
  637. return (-1);
  638. s->init_off += ret;
  639. s->init_num -= ret;
  640. return (0);
  641. }
  642. static int ssl_mt_error(int n)
  643. {
  644. int ret;
  645. switch (n) {
  646. case SSL2_PE_NO_CIPHER:
  647. ret = SSL_R_PEER_ERROR_NO_CIPHER;
  648. break;
  649. case SSL2_PE_NO_CERTIFICATE:
  650. ret = SSL_R_PEER_ERROR_NO_CERTIFICATE;
  651. break;
  652. case SSL2_PE_BAD_CERTIFICATE:
  653. ret = SSL_R_PEER_ERROR_CERTIFICATE;
  654. break;
  655. case SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE:
  656. ret = SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE;
  657. break;
  658. default:
  659. ret = SSL_R_UNKNOWN_REMOTE_ERROR_TYPE;
  660. break;
  661. }
  662. return (ret);
  663. }
  664. #else /* !OPENSSL_NO_SSL2 */
  665. # if PEDANTIC
  666. static void *dummy = &dummy;
  667. # endif
  668. #endif