ssl/s3_srvr.c:1325: error: INTEGER_OVERFLOW_L2
  ([0, +oo] - 2):unsigned64 by call to `ssl_parse_clienthello_tlsext`.
Showing all 24 steps of the trace


ssl/s3_srvr.c:874:10: Call
872.           return ((int)n);
873.       s->first_packet = 0;
874.       if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
                ^
875.           SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
876.           al = SSL_AD_INTERNAL_ERROR;

ssl/packet_locl.h:109:1: Parameter `pkt->remaining`
  107.    * is being used.
  108.    */
  109. > static inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf, size_t len)
  110.   {
  111.       /* Sanity check for negative values. */

ssl/s3_srvr.c:932:13: Call
930.            * differ: see RFC 2246, Appendix E, second paragraph)
931.            */
932.           if(!PACKET_get_net_2(&pkt, (unsigned int *)&s->client_version)) {
                   ^
933.               al = SSL_AD_DECODE_ERROR;
934.               SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);

ssl/packet_locl.h:169:8: Parameter `pkt->remaining`
  167.   /* Equivalent of n2s */
  168.   /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
  169.   __owur static inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
                ^
  170.   {
  171.       if (!PACKET_peek_net_2(pkt, data))

ssl/packet_locl.h:171:10: Call
  169.   __owur static inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
  170.   {
  171.       if (!PACKET_peek_net_2(pkt, data))
                  ^
  172.           return 0;
  173.   

ssl/packet_locl.h:155:8: Parameter `pkt->remaining`
    153.    * |*data|
    154.    */
    155.   __owur static inline int PACKET_peek_net_2(const PACKET *pkt,
                  ^
    156.                                              unsigned int *data)
    157.   {

ssl/s3_srvr.c:1072:14: Call
1070.           unsigned int cookie_len;
1071.           /* load the client random and get the session-id */
1072.           if (!PACKET_copy_bytes(&pkt, s->s3->client_random, SSL3_RANDOM_SIZE)
                     ^
1073.   		|| !PACKET_get_length_prefixed_1(&pkt, &session_id)) {
1074.               al = SSL_AD_DECODE_ERROR;

ssl/packet_locl.h:340:8: Parameter `pkt->remaining`
  338.    * The caller is responsible for ensuring that |data| can hold |len| bytes.
  339.    */
  340.   __owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data,
                ^
  341.                                              size_t len)
  342.   {

ssl/packet_locl.h:343:10: Call
  341.                                              size_t len)
  342.   {
  343.       if (!PACKET_peek_copy_bytes(pkt, data, len))
                  ^
  344.           return 0;
  345.   

ssl/packet_locl.h:325:8: Parameter `len`
    323.   
    324.   /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
    325.   __owur static inline int PACKET_peek_copy_bytes(const PACKET *pkt,
                  ^
    326.                                                   unsigned char *data, size_t len)
    327.   {

ssl/s3_srvr.c:1325:14: Call
1323.       /* TLS extensions */
1324.       if (s->version >= SSL3_VERSION) {
1325.           if (!ssl_parse_clienthello_tlsext(s, &pkt)) {
                     ^
1326.               SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1327.               goto err;

ssl/t1_lib.c:2325:1: Parameter `pkt->remaining`
  2323.   }
  2324.   
  2325. > int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
  2326.   {
  2327.       int al = -1;

ssl/t1_lib.c:2329:9: Call
  2327.       int al = -1;
  2328.       custom_ext_init(&s->cert->srv_ext);
  2329.       if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
                  ^
  2330.           ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2331.           return 0;

ssl/t1_lib.c:1884:1: Parameter `pkt->remaining`
    1882.   #endif                         /* !OPENSSL_NO_EC */
    1883.   
    1884. > static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
    1885.   {
    1886.       unsigned int type;

ssl/t1_lib.c:1907:9: Call
    1905.   #ifndef OPENSSL_NO_EC
    1906.       if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
    1907.           ssl_check_for_safari(s, pkt);
                    ^
    1908.   # endif /* !OPENSSL_NO_EC */
    1909.   

ssl/t1_lib.c:1818:1: Parameter `pkt->remaining`
      1816.    * 10.8..10.8.3 (which don't work).
      1817.    */
      1818. > static void ssl_check_for_safari(SSL *s, PACKET *pkt)
      1819.   {
      1820.       unsigned int type, size;

ssl/t1_lib.c:1849:10: Call
      1847.       };
      1848.   
      1849.       if (!PACKET_forward(pkt, 2)
                       ^
      1850.               || !PACKET_get_net_2(pkt, &type)
      1851.               || !PACKET_get_net_2(pkt, &size)

ssl/packet_locl.h:401:8: Parameter `len`
        399.   
        400.   /* Move the current reading position forward |len| bytes */
        401.   __owur static inline int PACKET_forward(PACKET *pkt, size_t len)
                      ^
        402.   {
        403.       if (PACKET_remaining(pkt) < len)

ssl/packet_locl.h:406:5: Call
        404.           return 0;
        405.   
        406.       packet_forward(pkt, len);
                   ^
        407.   
        408.       return 1;

ssl/packet_locl.h:79:1: <LHS trace>
77.   
78.   /* Internal unchecked shorthand; don't use outside this file. */
79. > static inline void packet_forward(PACKET *pkt, size_t len)
80.   {
81.       pkt->curr += len;

ssl/packet_locl.h:79:1: Parameter `pkt->remaining`
          77.   
          78.   /* Internal unchecked shorthand; don't use outside this file. */
          79. > static inline void packet_forward(PACKET *pkt, size_t len)
          80.   {
          81.       pkt->curr += len;

ssl/packet_locl.h:79:1: <RHS trace>
77.   
78.   /* Internal unchecked shorthand; don't use outside this file. */
79. > static inline void packet_forward(PACKET *pkt, size_t len)
80.   {
81.       pkt->curr += len;

ssl/packet_locl.h:79:1: Parameter `len`
          77.   
          78.   /* Internal unchecked shorthand; don't use outside this file. */
          79. > static inline void packet_forward(PACKET *pkt, size_t len)
          80.   {
          81.       pkt->curr += len;

ssl/packet_locl.h:82:5: Binary operation: ([0, +oo] - 2):unsigned64 by call to `ssl_parse_clienthello_tlsext` 
          80.   {
          81.       pkt->curr += len;
          82.       pkt->remaining -= len;
                    ^
          83.   }
          84.   

static inline void packet_forward(PACKET *pkt, size_t len)
{
    pkt->curr += len;
    pkt->remaining -= len;
}
['int ssl3_get_client_hello(SSL *s)\n{\n    int i, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;\n    unsigned int j, complen = 0;\n    long n;\n    unsigned long id;\n    SSL_CIPHER *c;\n#ifndef OPENSSL_NO_COMP\n    SSL_COMP *comp = NULL;\n#endif\n    STACK_OF(SSL_CIPHER) *ciphers = NULL;\n    int protverr = 1;\n    PACKET pkt, cipher_suite, compression;\n    if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet)\n        goto retry_cert;\n    if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {\n        s->state = SSL3_ST_SR_CLNT_HELLO_B;\n    }\n    s->first_packet = 1;\n    n = s->method->ssl_get_message(s,\n                                   SSL3_ST_SR_CLNT_HELLO_B,\n                                   SSL3_ST_SR_CLNT_HELLO_C,\n                                   SSL3_MT_CLIENT_HELLO,\n                                   SSL3_RT_MAX_PLAIN_LENGTH, &ok);\n    if (!ok)\n        return ((int)n);\n    s->first_packet = 0;\n    if (!PACKET_buf_init(&pkt, s->init_msg, n)) {\n        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n        al = SSL_AD_INTERNAL_ERROR;\n        goto f_err;\n    }\n    if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {\n        unsigned int version;\n        unsigned int mt;\n        if (!PACKET_get_1(&pkt, &mt)\n                || mt != SSL2_MT_CLIENT_HELLO) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (!PACKET_get_net_2(&pkt, &version)) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);\n            goto err;\n        }\n        if (version == 0x0002) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);\n            goto err;\n        } else if ((version & 0xff00) == (SSL3_VERSION_MAJOR << 8)) {\n            s->client_version = version;\n        } else {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);\n            goto err;\n        }\n    } else {\n        if(!PACKET_get_net_2(&pkt, (unsigned int *)&s->client_version)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n    }\n    if (!SSL_IS_DTLS(s)) {\n        if (s->version != TLS_ANY_VERSION) {\n            if (s->client_version >= s->version) {\n                protverr = 0;\n            }\n        } else if (s->client_version >= SSL3_VERSION) {\n            switch(s->client_version) {\n            default:\n            case TLS1_2_VERSION:\n                if(!(s->options & SSL_OP_NO_TLSv1_2)) {\n                    s->version = TLS1_2_VERSION;\n                    s->method = TLSv1_2_server_method();\n                    protverr = 0;\n                    break;\n                }\n            case TLS1_1_VERSION:\n                if(!(s->options & SSL_OP_NO_TLSv1_1)) {\n                    s->version = TLS1_1_VERSION;\n                    s->method = TLSv1_1_server_method();\n                    protverr = 0;\n                    break;\n                }\n            case TLS1_VERSION:\n                if(!(s->options & SSL_OP_NO_TLSv1)) {\n                    s->version = TLS1_VERSION;\n                    s->method = TLSv1_server_method();\n                    protverr = 0;\n                    break;\n                }\n            case SSL3_VERSION:\n#ifndef OPENSSL_NO_SSL3\n                if(!(s->options & SSL_OP_NO_SSLv3)) {\n                    s->version = SSL3_VERSION;\n                    s->method = SSLv3_server_method();\n                    protverr = 0;\n                    break;\n                }\n#else\n                break;\n#endif\n            }\n        }\n    } else if (s->client_version <= s->version\n                || s->method->version == DTLS_ANY_VERSION) {\n        protverr = 0;\n    }\n    if (protverr) {\n        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);\n        if ((!s->enc_write_ctx && !s->write_hash)) {\n            s->version = s->client_version;\n        }\n        al = SSL_AD_PROTOCOL_VERSION;\n        goto f_err;\n    }\n    if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {\n        unsigned int cipher_len, session_id_len, challenge_len;\n        if (!PACKET_get_net_2(&pkt, &cipher_len)\n                || !PACKET_get_net_2(&pkt, &session_id_len)\n                || !PACKET_get_net_2(&pkt, &challenge_len)) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);\n            al = SSL_AD_DECODE_ERROR;\n            goto f_err;\n        }\n        if (cipher_len == 0) {\n            al = SSL_AD_ILLEGAL_PARAMETER;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);\n            goto f_err;\n        }\n        if (!PACKET_get_sub_packet(&pkt, &cipher_suite, cipher_len)) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);\n            al = SSL_AD_DECODE_ERROR;\n            goto f_err;\n        }\n        if (ssl_bytes_to_cipher_list(s, PACKET_data(&cipher_suite),\n                                     cipher_len, &(ciphers), 1) == NULL) {\n            goto err;\n        }\n        if (!PACKET_forward(&pkt, session_id_len)) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);\n            al = SSL_AD_DECODE_ERROR;\n            goto f_err;\n        }\n        s->hit = 0;\n        if (!ssl_get_new_session(s, 1))\n            goto err;\n        i = challenge_len > SSL3_RANDOM_SIZE ? SSL3_RANDOM_SIZE : challenge_len;\n        memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE);\n        if (!PACKET_peek_copy_bytes(&pkt,\n                                    s->s3->client_random + SSL3_RANDOM_SIZE - i,\n                                    i)\n                || !PACKET_forward(&pkt, challenge_len)\n                || PACKET_remaining(&pkt) != 0) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);\n            al = SSL_AD_DECODE_ERROR;\n            goto f_err;\n        }\n    } else {\n        PACKET session_id;\n        unsigned int cookie_len;\n        if (!PACKET_copy_bytes(&pkt, s->s3->client_random, SSL3_RANDOM_SIZE)\n\t\t|| !PACKET_get_length_prefixed_1(&pkt, &session_id)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {\n            if (!PACKET_peek_1(&pkt, &cookie_len)) {\n                al = SSL_AD_DECODE_ERROR;\n                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);\n                goto f_err;\n            }\n            if (cookie_len == 0)\n                return 1;\n        }\n        s->hit = 0;\n        if ((s->new_session\n             && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {\n            if (!ssl_get_new_session(s, 1))\n                goto err;\n        } else {\n\t    i = ssl_get_prev_session(s, &pkt, PACKET_data(&session_id),\n\t\t                     PACKET_remaining(&session_id));\n            if (i == 1 && s->version == s->session->ssl_version) {\n                s->hit = 1;\n            } else if (i == -1)\n                goto err;\n            else {\n                if (!ssl_get_new_session(s, 1))\n                    goto err;\n            }\n        }\n        if (SSL_IS_DTLS(s)) {\n            PACKET cookie;\n            if (!PACKET_get_length_prefixed_1(&pkt, &cookie)) {\n                al = SSL_AD_DECODE_ERROR;\n                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);\n                goto f_err;\n            }\n\t    cookie_len = PACKET_remaining(&cookie);\n            if (cookie_len > sizeof(s->d1->rcvd_cookie)) {\n                al = SSL_AD_DECODE_ERROR;\n                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);\n                goto f_err;\n            }\n            if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)\n                    && cookie_len > 0) {\n                if (!PACKET_copy_bytes(&cookie, s->d1->rcvd_cookie, cookie_len)) {\n                    al = SSL_AD_INTERNAL_ERROR;\n                    SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n                    goto f_err;\n                }\n                if (s->ctx->app_verify_cookie_cb != NULL) {\n                    if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,\n                                                     cookie_len) == 0) {\n                        al = SSL_AD_HANDSHAKE_FAILURE;\n                        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,\n                               SSL_R_COOKIE_MISMATCH);\n                        goto f_err;\n                    }\n                }\n                else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,\n                                s->d1->cookie_len) != 0) {\n                    al = SSL_AD_HANDSHAKE_FAILURE;\n                    SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);\n                    goto f_err;\n                }\n                ret = -2;\n            }\n            if (s->method->version == DTLS_ANY_VERSION) {\n                if (s->client_version <= DTLS1_2_VERSION &&\n                    !(s->options & SSL_OP_NO_DTLSv1_2)) {\n                    s->version = DTLS1_2_VERSION;\n                    s->method = DTLSv1_2_server_method();\n                } else if (tls1_suiteb(s)) {\n                    SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,\n                           SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);\n                    s->version = s->client_version;\n                    al = SSL_AD_PROTOCOL_VERSION;\n                    goto f_err;\n                } else if (s->client_version <= DTLS1_VERSION &&\n                           !(s->options & SSL_OP_NO_DTLSv1)) {\n                    s->version = DTLS1_VERSION;\n                    s->method = DTLSv1_server_method();\n                } else {\n                    SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,\n                           SSL_R_WRONG_VERSION_NUMBER);\n                    s->version = s->client_version;\n                    al = SSL_AD_PROTOCOL_VERSION;\n                    goto f_err;\n                }\n                s->session->ssl_version = s->version;\n            }\n        }\n        if (!PACKET_get_length_prefixed_2(&pkt, &cipher_suite)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        if (PACKET_remaining(&cipher_suite) == 0) {\n            al = SSL_AD_ILLEGAL_PARAMETER;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);\n            goto f_err;\n        }\n        if (ssl_bytes_to_cipher_list(s, PACKET_data(&cipher_suite),\n                                     PACKET_remaining(&cipher_suite),\n                                     &(ciphers), 0) == NULL) {\n            goto err;\n        }\n        if (s->hit) {\n            j = 0;\n            id = s->session->cipher->id;\n#ifdef CIPHER_DEBUG\n            fprintf(stderr, "client sent %d ciphers\\n",\n                    sk_SSL_CIPHER_num(ciphers));\n#endif\n            for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {\n                c = sk_SSL_CIPHER_value(ciphers, i);\n#ifdef CIPHER_DEBUG\n                fprintf(stderr, "client [%2d of %2d]:%s\\n",\n                        i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));\n#endif\n                if (c->id == id) {\n                    j = 1;\n                    break;\n                }\n            }\n#if 0\n            if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)\n                && (sk_SSL_CIPHER_num(ciphers) == 1)) {\n                c = sk_SSL_CIPHER_value(ciphers, 0);\n                if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {\n                    s->session->cipher = c;\n                    j = 1;\n                }\n            }\n#endif\n            if (j == 0) {\n                al = SSL_AD_ILLEGAL_PARAMETER;\n                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,\n                       SSL_R_REQUIRED_CIPHER_MISSING);\n                goto f_err;\n            }\n        }\n        if (!PACKET_get_length_prefixed_1(&pkt, &compression)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        complen = PACKET_remaining(&compression);\n        for (j = 0; j < complen; j++) {\n            if (PACKET_data(&compression)[j] == 0)\n                break;\n        }\n        if (j >= complen) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);\n            goto f_err;\n        }\n    }\n    if (s->version >= SSL3_VERSION) {\n        if (!ssl_parse_clienthello_tlsext(s, &pkt)) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);\n            goto err;\n        }\n    }\n    {\n        unsigned char *pos;\n        pos = s->s3->server_random;\n        if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {\n            goto f_err;\n        }\n    }\n    if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {\n        SSL_CIPHER *pref_cipher = NULL;\n        s->session->master_key_length = sizeof(s->session->master_key);\n        if (s->tls_session_secret_cb(s, s->session->master_key,\n                                     &s->session->master_key_length, ciphers,\n                                     &pref_cipher,\n                                     s->tls_session_secret_cb_arg)) {\n            s->hit = 1;\n            s->session->ciphers = ciphers;\n            s->session->verify_result = X509_V_OK;\n            ciphers = NULL;\n            pref_cipher =\n                pref_cipher ? pref_cipher : ssl3_choose_cipher(s,\n                                                               s->\n                                                               session->ciphers,\n                                                               SSL_get_ciphers\n                                                               (s));\n            if (pref_cipher == NULL) {\n                al = SSL_AD_HANDSHAKE_FAILURE;\n                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);\n                goto f_err;\n            }\n            s->session->cipher = pref_cipher;\n            sk_SSL_CIPHER_free(s->cipher_list);\n            s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);\n            sk_SSL_CIPHER_free(s->cipher_list_by_id);\n            s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);\n        }\n    }\n    s->s3->tmp.new_compression = NULL;\n#ifndef OPENSSL_NO_COMP\n    if (s->session->compress_meth != 0) {\n        int m, comp_id = s->session->compress_meth;\n        unsigned int k;\n        if (!ssl_allow_compression(s)) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,\n                   SSL_R_INCONSISTENT_COMPRESSION);\n            goto f_err;\n        }\n        for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {\n            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);\n            if (comp_id == comp->id) {\n                s->s3->tmp.new_compression = comp;\n                break;\n            }\n        }\n        if (s->s3->tmp.new_compression == NULL) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,\n                   SSL_R_INVALID_COMPRESSION_ALGORITHM);\n            goto f_err;\n        }\n        for (k = 0; k < complen; k++) {\n            if (PACKET_data(&compression)[k] == comp_id)\n                break;\n        }\n        if (k >= complen) {\n            al = SSL_AD_ILLEGAL_PARAMETER;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,\n                   SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);\n            goto f_err;\n        }\n    } else if (s->hit)\n        comp = NULL;\n    else if (ssl_allow_compression(s) && s->ctx->comp_methods) {\n        int m, nn, v, done = 0;\n        unsigned int o;\n        nn = sk_SSL_COMP_num(s->ctx->comp_methods);\n        for (m = 0; m < nn; m++) {\n            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);\n            v = comp->id;\n            for (o = 0; o < complen; o++) {\n                if (v == PACKET_data(&compression)[o]) {\n                    done = 1;\n                    break;\n                }\n            }\n            if (done)\n                break;\n        }\n        if (done)\n            s->s3->tmp.new_compression = comp;\n        else\n            comp = NULL;\n    }\n#else\n    if (s->session->compress_meth != 0) {\n        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);\n        goto f_err;\n    }\n#endif\n    if (!s->hit) {\n#ifdef OPENSSL_NO_COMP\n        s->session->compress_meth = 0;\n#else\n        s->session->compress_meth = (comp == NULL) ? 0 : comp->id;\n#endif\n        sk_SSL_CIPHER_free(s->session->ciphers);\n        s->session->ciphers = ciphers;\n        if (ciphers == NULL) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        ciphers = NULL;\n        if (!tls1_set_server_sigalgs(s)) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);\n            goto err;\n        }\n retry_cert:\n        if (s->cert->cert_cb) {\n            int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);\n            if (rv == 0) {\n                al = SSL_AD_INTERNAL_ERROR;\n                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);\n                goto f_err;\n            }\n            if (rv < 0) {\n                s->rwstate = SSL_X509_LOOKUP;\n                return -1;\n            }\n            s->rwstate = SSL_NOTHING;\n        }\n        c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));\n        if (c == NULL) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);\n            goto f_err;\n        }\n        s->s3->tmp.new_cipher = c;\n        if (s->not_resumable_session_cb != NULL)\n            s->session->not_resumable = s->not_resumable_session_cb(s,\n                                                                    ((c->algorithm_mkey & (SSL_kDHE | SSL_kECDHE))\n                                                                     != 0));\n        if (s->session->not_resumable)\n            s->tlsext_ticket_expected = 0;\n    } else {\n        s->s3->tmp.new_cipher = s->session->cipher;\n    }\n    if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) {\n        if (!ssl3_digest_cached_records(s, 0))\n            goto f_err;\n    }\n    if (s->version >= SSL3_VERSION) {\n        if (ssl_check_clienthello_tlsext_late(s) <= 0) {\n            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);\n            goto err;\n        }\n    }\n    if (ret < 0)\n        ret = -ret;\n    if (0) {\n f_err:\n        ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n        s->state = SSL_ST_ERR;\n    }\n    sk_SSL_CIPHER_free(ciphers);\n    return ret < 0 ? -1 : ret;\n}', 'int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)\n{\n    int al = -1;\n    custom_ext_init(&s->cert->srv_ext);\n    if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {\n        ssl3_send_alert(s, SSL3_AL_FATAL, al);\n        return 0;\n    }\n    if (ssl_check_clienthello_tlsext_early(s) <= 0) {\n        SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);\n        return 0;\n    }\n    return 1;\n}', "static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)\n{\n    unsigned int type;\n    unsigned int size;\n    unsigned int len;\n    unsigned char *data;\n    int renegotiate_seen = 0;\n    s->servername_done = 0;\n    s->tlsext_status_type = -1;\n#ifndef OPENSSL_NO_NEXTPROTONEG\n    s->s3->next_proto_neg_seen = 0;\n#endif\n    OPENSSL_free(s->s3->alpn_selected);\n    s->s3->alpn_selected = NULL;\n#ifndef OPENSSL_NO_HEARTBEATS\n    s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |\n                             SSL_TLSEXT_HB_DONT_SEND_REQUESTS);\n#endif\n#ifndef OPENSSL_NO_EC\n    if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)\n        ssl_check_for_safari(s, pkt);\n# endif\n    OPENSSL_free(s->s3->tmp.peer_sigalgs);\n    s->s3->tmp.peer_sigalgs = NULL;\n#ifdef TLSEXT_TYPE_encrypt_then_mac\n    s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;\n#endif\n#ifndef OPENSSL_NO_SRP\n    OPENSSL_free(s->srp_ctx.login);\n    s->srp_ctx.login = NULL;\n#endif\n    s->srtp_profile = NULL;\n    if (PACKET_remaining(pkt) == 0)\n        goto ri_check;\n    if (!PACKET_get_net_2(pkt, &len))\n        goto err;\n    while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {\n        PACKET subpkt;\n        if (!PACKET_peek_bytes(pkt, &data, size))\n            goto err;\n        if (s->tlsext_debug_cb)\n            s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);\n        if (!PACKET_get_sub_packet(pkt, &subpkt, size))\n            goto err;\n        if (type == TLSEXT_TYPE_renegotiate) {\n            if (!ssl_parse_clienthello_renegotiate_ext(s, &subpkt, al))\n                return 0;\n            renegotiate_seen = 1;\n        } else if (s->version == SSL3_VERSION) {\n        }\n        else if (type == TLSEXT_TYPE_server_name) {\n            unsigned char *sdata;\n            unsigned int servname_type;\n            unsigned int dsize;\n            PACKET ssubpkt;\n            if (!PACKET_get_net_2(&subpkt, &dsize)\n                    || !PACKET_get_sub_packet(&subpkt, &ssubpkt, dsize))\n                goto err;\n            while (PACKET_remaining(&ssubpkt) > 3) {\n                if (!PACKET_get_1(&ssubpkt, &servname_type)\n                        || !PACKET_get_net_2(&ssubpkt, &len)\n                        || PACKET_remaining(&ssubpkt) < len)\n                    goto err;\n                if (s->servername_done == 0)\n                    switch (servname_type) {\n                    case TLSEXT_NAMETYPE_host_name:\n                        if (!s->hit) {\n                            if (s->session->tlsext_hostname)\n                                goto err;\n                            if (len > TLSEXT_MAXLEN_host_name) {\n                                *al = TLS1_AD_UNRECOGNIZED_NAME;\n                                return 0;\n                            }\n                            if ((s->session->tlsext_hostname =\n                                 OPENSSL_malloc(len + 1)) == NULL) {\n                                *al = TLS1_AD_INTERNAL_ERROR;\n                                return 0;\n                            }\n                            if (!PACKET_copy_bytes(&ssubpkt,\n                                    (unsigned char *)s->session\n                                        ->tlsext_hostname,\n                                    len)) {\n                                *al = SSL_AD_DECODE_ERROR;\n                                return 0;\n                            }\n                            s->session->tlsext_hostname[len] = '\\0';\n                            if (strlen(s->session->tlsext_hostname) != len) {\n                                OPENSSL_free(s->session->tlsext_hostname);\n                                s->session->tlsext_hostname = NULL;\n                                *al = TLS1_AD_UNRECOGNIZED_NAME;\n                                return 0;\n                            }\n                            s->servername_done = 1;\n                        } else {\n                            if (!PACKET_get_bytes(&ssubpkt, &sdata, len)) {\n                                *al = SSL_AD_DECODE_ERROR;\n                                return 0;\n                            }\n                            s->servername_done = s->session->tlsext_hostname\n                                && strlen(s->session->tlsext_hostname) == len\n                                && strncmp(s->session->tlsext_hostname,\n                                           (char *)sdata, len) == 0;\n                        }\n                        break;\n                    default:\n                        break;\n                    }\n            }\n            if (PACKET_remaining(&ssubpkt) != 0)\n                goto err;\n        }\n#ifndef OPENSSL_NO_SRP\n        else if (type == TLSEXT_TYPE_srp) {\n            if (!PACKET_get_1(&subpkt, &len)\n                    || s->srp_ctx.login != NULL)\n                goto err;\n            if ((s->srp_ctx.login = OPENSSL_malloc(len + 1)) == NULL)\n                return -1;\n            if (!PACKET_copy_bytes(&subpkt, (unsigned char *)s->srp_ctx.login,\n                                   len))\n                goto err;\n            s->srp_ctx.login[len] = '\\0';\n            if (strlen(s->srp_ctx.login) != len\n                    || PACKET_remaining(&subpkt))\n                goto err;\n        }\n#endif\n#ifndef OPENSSL_NO_EC\n        else if (type == TLSEXT_TYPE_ec_point_formats) {\n            unsigned int ecpointformatlist_length;\n            if (!PACKET_get_1(&subpkt, &ecpointformatlist_length)\n                    || ecpointformatlist_length == 0)\n                goto err;\n            if (!s->hit) {\n                OPENSSL_free(s->session->tlsext_ecpointformatlist);\n                s->session->tlsext_ecpointformatlist = NULL;\n                s->session->tlsext_ecpointformatlist_length = 0;\n                if ((s->session->tlsext_ecpointformatlist =\n                     OPENSSL_malloc(ecpointformatlist_length)) == NULL) {\n                    *al = TLS1_AD_INTERNAL_ERROR;\n                    return 0;\n                }\n                s->session->tlsext_ecpointformatlist_length =\n                    ecpointformatlist_length;\n                if (!PACKET_copy_bytes(&subpkt,\n                        s->session->tlsext_ecpointformatlist,\n                        ecpointformatlist_length))\n                    goto err;\n            } else if (!PACKET_forward(&subpkt, ecpointformatlist_length)) {\n                goto err;\n            }\n            if (PACKET_remaining(&subpkt)) {\n                *al = TLS1_AD_DECODE_ERROR;\n                return 0;\n            }\n        } else if (type == TLSEXT_TYPE_elliptic_curves) {\n            unsigned int ellipticcurvelist_length;\n            if (!PACKET_get_net_2(&subpkt, &ellipticcurvelist_length)\n                    || ellipticcurvelist_length == 0\n                    || (ellipticcurvelist_length & 1) != 0)\n                goto err;\n            if (!s->hit) {\n                if (s->session->tlsext_ellipticcurvelist)\n                    goto err;\n                s->session->tlsext_ellipticcurvelist_length = 0;\n                if ((s->session->tlsext_ellipticcurvelist =\n                     OPENSSL_malloc(ellipticcurvelist_length)) == NULL) {\n                    *al = TLS1_AD_INTERNAL_ERROR;\n                    return 0;\n                }\n                s->session->tlsext_ellipticcurvelist_length =\n                    ellipticcurvelist_length;\n                if (!PACKET_copy_bytes(&subpkt,\n                        s->session->tlsext_ellipticcurvelist,\n                        ellipticcurvelist_length))\n                    goto err;\n            } else if (!PACKET_forward(&subpkt, ellipticcurvelist_length)) {\n                goto err;\n            }\n            if (PACKET_remaining(&subpkt)) {\n                goto err;\n            }\n        }\n#endif\n        else if (type == TLSEXT_TYPE_session_ticket) {\n            if (!PACKET_forward(&subpkt, size)\n                || (s->tls_session_ticket_ext_cb &&\n                    !s->tls_session_ticket_ext_cb(s, data, size,\n                                        s->tls_session_ticket_ext_cb_arg))) {\n                *al = TLS1_AD_INTERNAL_ERROR;\n                return 0;\n            }\n        } else if (type == TLSEXT_TYPE_signature_algorithms) {\n            unsigned int dsize;\n            if (s->s3->tmp.peer_sigalgs\n                    || !PACKET_get_net_2(&subpkt, &dsize)\n                    || (dsize & 1) != 0\n                    || (dsize == 0)\n                    || !PACKET_get_bytes(&subpkt, &data, dsize)\n                    || PACKET_remaining(&subpkt) != 0\n                    || !tls1_save_sigalgs(s, data, dsize)) {\n                goto err;\n            }\n        } else if (type == TLSEXT_TYPE_status_request) {\n            PACKET ssubpkt;\n            if (!PACKET_get_1(&subpkt,\n                              (unsigned int *)&s->tlsext_status_type))\n                goto err;\n            if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {\n                const unsigned char *sdata;\n                unsigned int dsize;\n                if (!PACKET_get_net_2(&subpkt, &dsize)\n                        || !PACKET_get_sub_packet(&subpkt, &ssubpkt, dsize))\n                    goto err;\n                while (PACKET_remaining(&ssubpkt)) {\n                    OCSP_RESPID *id;\n                    unsigned int idsize;\n                    if (PACKET_remaining(&ssubpkt) < 4\n                            || !PACKET_get_net_2(&ssubpkt, &idsize)\n                            || !PACKET_get_bytes(&ssubpkt, &data, idsize)) {\n                        goto err;\n                    }\n                    sdata = data;\n                    data += idsize;\n                    id = d2i_OCSP_RESPID(NULL, &sdata, idsize);\n                    if (!id)\n                        goto err;\n                    if (data != sdata) {\n                        OCSP_RESPID_free(id);\n                        goto err;\n                    }\n                    if (!s->tlsext_ocsp_ids\n                        && !(s->tlsext_ocsp_ids =\n                             sk_OCSP_RESPID_new_null())) {\n                        OCSP_RESPID_free(id);\n                        *al = SSL_AD_INTERNAL_ERROR;\n                        return 0;\n                    }\n                    if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {\n                        OCSP_RESPID_free(id);\n                        *al = SSL_AD_INTERNAL_ERROR;\n                        return 0;\n                    }\n                }\n                if (!PACKET_get_net_2(&subpkt, &dsize)\n                        || !PACKET_get_bytes(&subpkt, &data, dsize)\n                        || PACKET_remaining(&subpkt)) {\n                    goto err;\n                }\n                sdata = data;\n                if (dsize > 0) {\n                    sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,\n                                               X509_EXTENSION_free);\n                    s->tlsext_ocsp_exts =\n                        d2i_X509_EXTENSIONS(NULL, &sdata, dsize);\n                    if (!s->tlsext_ocsp_exts || (data + dsize != sdata))\n                        goto err;\n                }\n            }\n            else\n                s->tlsext_status_type = -1;\n        }\n#ifndef OPENSSL_NO_HEARTBEATS\n        else if (type == TLSEXT_TYPE_heartbeat) {\n            unsigned int hbtype;\n            if (!PACKET_get_1(&subpkt, &hbtype)\n                    || PACKET_remaining(&subpkt)) {\n                *al = SSL_AD_DECODE_ERROR;\n                return 0;\n            }\n            switch (hbtype) {\n            case 0x01:\n                s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;\n                break;\n            case 0x02:\n                s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;\n                s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;\n                break;\n            default:\n                *al = SSL_AD_ILLEGAL_PARAMETER;\n                return 0;\n            }\n        }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n        else if (type == TLSEXT_TYPE_next_proto_neg &&\n                 s->s3->tmp.finish_md_len == 0 &&\n                 s->s3->alpn_selected == NULL) {\n            s->s3->next_proto_neg_seen = 1;\n        }\n#endif\n        else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&\n                 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {\n            if (tls1_alpn_handle_client_hello(s, &subpkt, al) != 0)\n                return 0;\n#ifndef OPENSSL_NO_NEXTPROTONEG\n            s->s3->next_proto_neg_seen = 0;\n#endif\n        }\n#ifndef OPENSSL_NO_SRTP\n        else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)\n                 && type == TLSEXT_TYPE_use_srtp) {\n            if (ssl_parse_clienthello_use_srtp_ext(s, &subpkt, al))\n                return 0;\n        }\n#endif\n#ifdef TLSEXT_TYPE_encrypt_then_mac\n        else if (type == TLSEXT_TYPE_encrypt_then_mac)\n            s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;\n#endif\n        else if (type == TLSEXT_TYPE_extended_master_secret) {\n            if (!s->hit)\n                s->session->flags |= SSL_SESS_FLAG_EXTMS;\n        }\n        else if (!s->hit) {\n            if (custom_ext_parse(s, 1, type, data, size, al) <= 0)\n                return 0;\n        }\n    }\n    if (PACKET_remaining(pkt) != 0)\n        goto err;\n ri_check:\n    if (!renegotiate_seen && s->renegotiate &&\n        !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {\n        *al = SSL_AD_HANDSHAKE_FAILURE;\n        SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,\n               SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);\n        return 0;\n    }\n    return 1;\nerr:\n    *al = SSL_AD_DECODE_ERROR;\n    return 0;\n}", 'static void ssl_check_for_safari(SSL *s, PACKET *pkt)\n{\n    unsigned int type, size;\n    unsigned char *eblock1, *eblock2;\n    static const unsigned char kSafariExtensionsBlock[] = {\n        0x00, 0x0a,\n        0x00, 0x08,\n        0x00, 0x06,\n        0x00, 0x17,\n        0x00, 0x18,\n        0x00, 0x19,\n        0x00, 0x0b,\n        0x00, 0x02,\n        0x01,\n        0x00,\n    };\n    static const unsigned char kSafariTLS12ExtensionsBlock[] = {\n        0x00, 0x0d,\n        0x00, 0x0c,\n        0x00, 0x0a,\n        0x05, 0x01,\n        0x04, 0x01,\n        0x02, 0x01,\n        0x04, 0x03,\n        0x02, 0x03,\n    };\n    if (!PACKET_forward(pkt, 2)\n            || !PACKET_get_net_2(pkt, &type)\n            || !PACKET_get_net_2(pkt, &size)\n            || !PACKET_forward(pkt, size))\n        return;\n    if (type != TLSEXT_TYPE_server_name)\n        return;\n    if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {\n        const size_t len1 = sizeof(kSafariExtensionsBlock);\n        const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);\n        if (!PACKET_get_bytes(pkt, &eblock1, len1)\n                || !PACKET_get_bytes(pkt, &eblock2, len2)\n                || PACKET_remaining(pkt))\n            return;\n        if (memcmp(eblock1, kSafariExtensionsBlock, len1) != 0)\n            return;\n        if (memcmp(eblock2, kSafariTLS12ExtensionsBlock, len2) != 0)\n            return;\n    } else {\n        const size_t len = sizeof(kSafariExtensionsBlock);\n        if (!PACKET_get_bytes(pkt, &eblock1, len)\n                || PACKET_remaining(pkt))\n            return;\n        if (memcmp(eblock1, kSafariExtensionsBlock, len) != 0)\n            return;\n    }\n    s->s3->is_probably_safari = 1;\n}', 'static inline void packet_forward(PACKET *pkt, size_t len)\n{\n    pkt->curr += len;\n    pkt->remaining -= len;\n}']