LCOV - code coverage report
Current view: top level - third_party/openssl/ssl - s2_pkt.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 0 259 0.0 %
Date: 2015-10-10 Functions: 0 9 0.0 %

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

Generated by: LCOV version 1.10