ssl/statem/statem_srvr.c:2663: error: INTEGER_OVERFLOW_L2
  ([0, +oo] - [`pkt->written`, `pkt->written` + 2]):unsigned64 by call to `WPACKET_sub_memcpy__`.
Showing all 14 steps of the trace


ssl/statem/statem_srvr.c:2465:1: Parameter `pkt->written`
2463.   }
2464.   
2465. > int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2466.   {
2467.   #ifndef OPENSSL_NO_DH

ssl/statem/statem_srvr.c:2663:21: Call
2661.            */
2662.           if (len > PSK_MAX_IDENTITY_LEN
2663.                   || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
                            ^
2664.                                              len)) {
2665.               SSLfatal(s, SSL_AD_INTERNAL_ERROR,

crypto/packet.c:402:10: Call
  400.                            size_t lenbytes)
  401.   {
  402.       if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
                  ^
  403.               || !WPACKET_memcpy(pkt, src, len)
  404.               || !WPACKET_close(pkt))

crypto/packet.c:292:1: Parameter `pkt->buf->length`
    290.   }
    291.   
    292. > int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
    293.   {
    294.       WPACKET_SUB *sub;

crypto/packet.c:403:17: Call
  401.   {
  402.       if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
  403.               || !WPACKET_memcpy(pkt, src, len)
                         ^
  404.               || !WPACKET_close(pkt))
  405.           return 0;

crypto/packet.c:383:1: Parameter `pkt->written`
    381.   }
    382.   
    383. > int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
    384.   {
    385.       unsigned char *dest;

crypto/packet.c:390:10: Call
    388.           return 1;
    389.   
    390.       if (!WPACKET_allocate_bytes(pkt, len, &dest))
                    ^
    391.           return 0;
    392.   

crypto/packet.c:16:1: Parameter `pkt->written`
      14.   #define DEFAULT_BUF_SIZE    256
      15.   
      16. > int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
      17.   {
      18.       if (!WPACKET_reserve_bytes(pkt, len, allocbytes))

crypto/packet.c:18:10: Call
      16.   int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
      17.   {
      18.       if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
                     ^
      19.           return 0;
      20.   

crypto/packet.c:43:1: <LHS trace>
41.                           : NULL))
42.   
43. > int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
44.   {
45.       /* Internal API, so should not fail */

crypto/packet.c:43:1: Parameter `pkt->buf->length`
        41.                           : NULL))
        42.   
        43. > int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
        44.   {
        45.       /* Internal API, so should not fail */

crypto/packet.c:43:1: <RHS trace>
41.                           : NULL))
42.   
43. > int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
44.   {
45.       /* Internal API, so should not fail */

crypto/packet.c:43:1: Parameter `len`
        41.                           : NULL))
        42.   
        43. > int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
        44.   {
        45.       /* Internal API, so should not fail */

crypto/packet.c:52:30: Binary operation: ([0, +oo] - [pkt->written, pkt->written + 2]):unsigned64 by call to `WPACKET_sub_memcpy__` 
        50.           return 0;
        51.   
        52.       if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
                                           ^
        53.           size_t newlen;
        54.           size_t reflen;

int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
    if (!ossl_assert(pkt->subs != NULL && len != 0))
        return 0;
    if (pkt->maxsize - pkt->written < len)
        return 0;
    if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
        size_t newlen;
        size_t reflen;
        reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
        if (reflen > SIZE_MAX / 2) {
            newlen = SIZE_MAX;
        } else {
            newlen = reflen * 2;
            if (newlen < DEFAULT_BUF_SIZE)
                newlen = DEFAULT_BUF_SIZE;
        }
        if (BUF_MEM_grow(pkt->buf, newlen) == 0)
            return 0;
    }
    if (allocbytes != NULL)
        *allocbytes = WPACKET_get_curr(pkt);
    return 1;
}
['int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)\n{\n#ifndef OPENSSL_NO_DH\n    EVP_PKEY *pkdh = NULL;\n#endif\n#ifndef OPENSSL_NO_EC\n    unsigned char *encodedPoint = NULL;\n    size_t encodedlen = 0;\n    int curve_id = 0;\n#endif\n    const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;\n    int i;\n    unsigned long type;\n    const BIGNUM *r[4];\n    EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();\n    EVP_PKEY_CTX *pctx = NULL;\n    size_t paramlen, paramoffset;\n    if (!WPACKET_get_total_written(pkt, &paramoffset)) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    if (md_ctx == NULL) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    type = s->s3.tmp.new_cipher->algorithm_mkey;\n    r[0] = r[1] = r[2] = r[3] = NULL;\n#ifndef OPENSSL_NO_PSK\n    if (type & (SSL_kPSK | SSL_kRSAPSK)) {\n    } else\n#endif\n#ifndef OPENSSL_NO_DH\n    if (type & (SSL_kDHE | SSL_kDHEPSK)) {\n        CERT *cert = s->cert;\n        EVP_PKEY *pkdhp = NULL;\n        DH *dh;\n        if (s->cert->dh_tmp_auto) {\n            DH *dhp = ssl_get_auto_dh(s);\n            pkdh = EVP_PKEY_new();\n            if (pkdh == NULL || dhp == NULL) {\n                DH_free(dhp);\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                         SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                         ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            EVP_PKEY_assign_DH(pkdh, dhp);\n            pkdhp = pkdh;\n        } else {\n            pkdhp = cert->dh_tmp;\n        }\n        if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {\n            DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);\n            pkdh = ssl_dh_to_pkey(dhp);\n            if (pkdh == NULL) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                         SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                         ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            pkdhp = pkdh;\n        }\n        if (pkdhp == NULL) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     SSL_R_MISSING_TMP_DH_KEY);\n            goto err;\n        }\n        if (!ssl_security(s, SSL_SECOP_TMP_DH,\n                          EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {\n            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     SSL_R_DH_KEY_TOO_SMALL);\n            goto err;\n        }\n        if (s->s3.tmp.pkey != NULL) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        s->s3.tmp.pkey = ssl_generate_pkey(pkdhp);\n        if (s->s3.tmp.pkey == NULL) {\n            goto err;\n        }\n        dh = EVP_PKEY_get0_DH(s->s3.tmp.pkey);\n        if (dh == NULL) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        EVP_PKEY_free(pkdh);\n        pkdh = NULL;\n        DH_get0_pqg(dh, &r[0], NULL, &r[1]);\n        DH_get0_key(dh, &r[2], NULL);\n    } else\n#endif\n#ifndef OPENSSL_NO_EC\n    if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {\n        if (s->s3.tmp.pkey != NULL) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        curve_id = tls1_shared_group(s, -2);\n        if (curve_id == 0) {\n            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);\n            goto err;\n        }\n        s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id);\n        if (s->s3.tmp.pkey == NULL) {\n            goto err;\n        }\n        encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3.tmp.pkey,\n                                                    &encodedPoint);\n        if (encodedlen == 0) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);\n            goto err;\n        }\n        r[0] = NULL;\n        r[1] = NULL;\n        r[2] = NULL;\n        r[3] = NULL;\n    } else\n#endif\n#ifndef OPENSSL_NO_SRP\n    if (type & SSL_kSRP) {\n        if ((s->srp_ctx.N == NULL) ||\n            (s->srp_ctx.g == NULL) ||\n            (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     SSL_R_MISSING_SRP_PARAM);\n            goto err;\n        }\n        r[0] = s->srp_ctx.N;\n        r[1] = s->srp_ctx.g;\n        r[2] = s->srp_ctx.s;\n        r[3] = s->srp_ctx.B;\n    } else\n#endif\n    {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);\n        goto err;\n    }\n    if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)\n        || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {\n        lu = NULL;\n    } else if (lu == NULL) {\n        SSLfatal(s, SSL_AD_DECODE_ERROR,\n                 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n#ifndef OPENSSL_NO_PSK\n    if (type & SSL_PSK) {\n        size_t len = (s->cert->psk_identity_hint == NULL)\n                        ? 0 : strlen(s->cert->psk_identity_hint);\n        if (len > PSK_MAX_IDENTITY_LEN\n                || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,\n                                           len)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n    }\n#endif\n    for (i = 0; i < 4 && r[i] != NULL; i++) {\n        unsigned char *binval;\n        int res;\n#ifndef OPENSSL_NO_SRP\n        if ((i == 2) && (type & SSL_kSRP)) {\n            res = WPACKET_start_sub_packet_u8(pkt);\n        } else\n#endif\n            res = WPACKET_start_sub_packet_u16(pkt);\n        if (!res) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n#ifndef OPENSSL_NO_DH\n        if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {\n            size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);\n            if (len > 0) {\n                if (!WPACKET_allocate_bytes(pkt, len, &binval)) {\n                    SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                             SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                             ERR_R_INTERNAL_ERROR);\n                    goto err;\n                }\n                memset(binval, 0, len);\n            }\n        }\n#endif\n        if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)\n                || !WPACKET_close(pkt)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        BN_bn2bin(r[i], binval);\n    }\n#ifndef OPENSSL_NO_EC\n    if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {\n        if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)\n                || !WPACKET_put_bytes_u8(pkt, 0)\n                || !WPACKET_put_bytes_u8(pkt, curve_id)\n                || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        OPENSSL_free(encodedPoint);\n        encodedPoint = NULL;\n    }\n#endif\n    if (lu != NULL) {\n        EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;\n        const EVP_MD *md;\n        unsigned char *sigbytes1, *sigbytes2, *tbs;\n        size_t siglen, tbslen;\n        int rv;\n        if (pkey == NULL || !tls1_lookup_md(lu, &md)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (!WPACKET_get_length(pkt, &paramlen)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        siglen = EVP_PKEY_size(pkey);\n        if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)\n            || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (lu->sig == EVP_PKEY_RSA_PSS) {\n            if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0\n                || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                         SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                        ERR_R_EVP_LIB);\n                goto err;\n            }\n        }\n        tbslen = construct_key_exchange_tbs(s, &tbs,\n                                            s->init_buf->data + paramoffset,\n                                            paramlen);\n        if (tbslen == 0) {\n            goto err;\n        }\n        rv = EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen);\n        OPENSSL_free(tbs);\n        if (rv <= 0 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)\n            || sigbytes1 != sigbytes2) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n    }\n    EVP_MD_CTX_free(md_ctx);\n    return 1;\n err:\n#ifndef OPENSSL_NO_DH\n    EVP_PKEY_free(pkdh);\n#endif\n#ifndef OPENSSL_NO_EC\n    OPENSSL_free(encodedPoint);\n#endif\n    EVP_MD_CTX_free(md_ctx);\n    return 0;\n}', 'int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n                         size_t lenbytes)\n{\n    if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n            || !WPACKET_memcpy(pkt, src, len)\n            || !WPACKET_close(pkt))\n        return 0;\n    return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n    WPACKET_SUB *sub;\n    unsigned char *lenchars;\n    if (!ossl_assert(pkt->subs != NULL))\n        return 0;\n    if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {\n        SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);\n        return 0;\n    }\n    sub->parent = pkt->subs;\n    pkt->subs = sub;\n    sub->pwritten = pkt->written + lenbytes;\n    sub->lenbytes = lenbytes;\n    if (lenbytes == 0) {\n        sub->packet_len = 0;\n        return 1;\n    }\n    sub->packet_len = pkt->written;\n    if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n        return 0;\n    return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n    unsigned char *dest;\n    if (len == 0)\n        return 1;\n    if (!WPACKET_allocate_bytes(pkt, len, &dest))\n        return 0;\n    if (dest != NULL)\n        memcpy(dest, src, len);\n    return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n    if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n        return 0;\n    pkt->written += len;\n    pkt->curr += len;\n    return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n    if (!ossl_assert(pkt->subs != NULL && len != 0))\n        return 0;\n    if (pkt->maxsize - pkt->written < len)\n        return 0;\n    if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {\n        size_t newlen;\n        size_t reflen;\n        reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n        if (reflen > SIZE_MAX / 2) {\n            newlen = SIZE_MAX;\n        } else {\n            newlen = reflen * 2;\n            if (newlen < DEFAULT_BUF_SIZE)\n                newlen = DEFAULT_BUF_SIZE;\n        }\n        if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n            return 0;\n    }\n    if (allocbytes != NULL)\n        *allocbytes = WPACKET_get_curr(pkt);\n    return 1;\n}']