crypto/ecdh/ech_ossl.c:139: error: NULL_DEREFERENCE
  pointer `x` last assigned on line 127 could be null and is dereferenced by call to `EC_GROUP_get_cofactor()` at line 139, column 14.
Showing all 45 steps of the trace


crypto/ecdh/ech_ossl.c:104:1: start of procedure ecdh_compute_key()
102.    * Finally an optional KDF is applied.
103.    */
104. > static int ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
105.                               EC_KEY *ecdh,
106.                               void *(*KDF) (const void *in, size_t inlen,

crypto/ecdh/ech_ossl.c:110:5: 
108.   {
109.       BN_CTX *ctx;
110. >     EC_POINT *tmp = NULL;
111.       BIGNUM *x = NULL, *y = NULL;
112.       const BIGNUM *priv_key;

crypto/ecdh/ech_ossl.c:111:5: 
109.       BN_CTX *ctx;
110.       EC_POINT *tmp = NULL;
111. >     BIGNUM *x = NULL, *y = NULL;
112.       const BIGNUM *priv_key;
113.       const EC_GROUP *group;

crypto/ecdh/ech_ossl.c:114:5: 
112.       const BIGNUM *priv_key;
113.       const EC_GROUP *group;
114. >     int ret = -1;
115.       size_t buflen, len;
116.       unsigned char *buf = NULL;

crypto/ecdh/ech_ossl.c:116:5: 
114.       int ret = -1;
115.       size_t buflen, len;
116. >     unsigned char *buf = NULL;
117.   
118.       if (outlen > INT_MAX) {

crypto/ecdh/ech_ossl.c:118:9: Taking false branch
116.       unsigned char *buf = NULL;
117.   
118.       if (outlen > INT_MAX) {
               ^
119.           ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE); /* sort of,
120.                                                                    * anyway */

crypto/ecdh/ech_ossl.c:124:9: Taking false branch
122.       }
123.   
124.       if ((ctx = BN_CTX_new()) == NULL)
               ^
125.           goto err;
126.       BN_CTX_start(ctx);

crypto/ecdh/ech_ossl.c:126:5: 
124.       if ((ctx = BN_CTX_new()) == NULL)
125.           goto err;
126. >     BN_CTX_start(ctx);
127.       x = BN_CTX_get(ctx);
128.       y = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:240:1: start of procedure BN_CTX_start()
  238.   }
  239.   
  240. > void BN_CTX_start(BN_CTX *ctx)
  241.   {
  242.       CTXDBG_ENTRY("BN_CTX_start", ctx);

crypto/bn/bn_ctx.c:244:9: Taking true branch
  242.       CTXDBG_ENTRY("BN_CTX_start", ctx);
  243.       /* If we're already overflowing ... */
  244.       if (ctx->err_stack || ctx->too_many)
                 ^
  245.           ctx->err_stack++;
  246.       /* (Try to) get a new frame pointer */

crypto/bn/bn_ctx.c:245:9: 
  243.       /* If we're already overflowing ... */
  244.       if (ctx->err_stack || ctx->too_many)
  245. >         ctx->err_stack++;
  246.       /* (Try to) get a new frame pointer */
  247.       else if (!BN_STACK_push(&ctx->stack, ctx->used)) {

crypto/bn/bn_ctx.c:244:5: 
  242.       CTXDBG_ENTRY("BN_CTX_start", ctx);
  243.       /* If we're already overflowing ... */
  244. >     if (ctx->err_stack || ctx->too_many)
  245.           ctx->err_stack++;
  246.       /* (Try to) get a new frame pointer */

crypto/bn/bn_ctx.c:252:1: return from a call to BN_CTX_start
  250.       }
  251.       CTXDBG_EXIT(ctx);
  252. > }
  253.   
  254.   void BN_CTX_end(BN_CTX *ctx)

crypto/ecdh/ech_ossl.c:127:5: 
125.           goto err;
126.       BN_CTX_start(ctx);
127. >     x = BN_CTX_get(ctx);
128.       y = BN_CTX_get(ctx);
129.   

crypto/bn/bn_ctx.c:271:1: start of procedure BN_CTX_get()
  269.   }
  270.   
  271. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
  272.   {
  273.       BIGNUM *ret;

crypto/bn/bn_ctx.c:276:9: Taking true branch
  274.   
  275.       CTXDBG_ENTRY("BN_CTX_get", ctx);
  276.       if (ctx->err_stack || ctx->too_many)
                 ^
  277.           return NULL;
  278.       if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {

crypto/bn/bn_ctx.c:277:9: 
  275.       CTXDBG_ENTRY("BN_CTX_get", ctx);
  276.       if (ctx->err_stack || ctx->too_many)
  277. >         return NULL;
  278.       if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {
  279.           /*

crypto/bn/bn_ctx.c:292:1: return from a call to BN_CTX_get
  290.       CTXDBG_RET(ctx, ret);
  291.       return ret;
  292. > }
  293.   
  294.   /************/

crypto/ecdh/ech_ossl.c:128:5: 
126.       BN_CTX_start(ctx);
127.       x = BN_CTX_get(ctx);
128. >     y = BN_CTX_get(ctx);
129.   
130.       priv_key = EC_KEY_get0_private_key(ecdh);

crypto/bn/bn_ctx.c:271:1: start of procedure BN_CTX_get()
  269.   }
  270.   
  271. > BIGNUM *BN_CTX_get(BN_CTX *ctx)
  272.   {
  273.       BIGNUM *ret;

crypto/bn/bn_ctx.c:276:9: Taking true branch
  274.   
  275.       CTXDBG_ENTRY("BN_CTX_get", ctx);
  276.       if (ctx->err_stack || ctx->too_many)
                 ^
  277.           return NULL;
  278.       if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {

crypto/bn/bn_ctx.c:277:9: 
  275.       CTXDBG_ENTRY("BN_CTX_get", ctx);
  276.       if (ctx->err_stack || ctx->too_many)
  277. >         return NULL;
  278.       if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {
  279.           /*

crypto/bn/bn_ctx.c:292:1: return from a call to BN_CTX_get
  290.       CTXDBG_RET(ctx, ret);
  291.       return ret;
  292. > }
  293.   
  294.   /************/

crypto/ecdh/ech_ossl.c:130:5: 
128.       y = BN_CTX_get(ctx);
129.   
130. >     priv_key = EC_KEY_get0_private_key(ecdh);
131.       if (priv_key == NULL) {
132.           ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_NO_PRIVATE_VALUE);

crypto/ec/ec_key.c:433:1: start of procedure EC_KEY_get0_private_key()
  431.   }
  432.   
  433. > const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
  434.   {
  435.       return key->priv_key;

crypto/ec/ec_key.c:435:5: 
  433.   const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
  434.   {
  435. >     return key->priv_key;
  436.   }
  437.   

crypto/ec/ec_key.c:436:1: return from a call to EC_KEY_get0_private_key
  434.   {
  435.       return key->priv_key;
  436. > }
  437.   
  438.   int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)

crypto/ecdh/ech_ossl.c:131:9: Taking false branch
129.   
130.       priv_key = EC_KEY_get0_private_key(ecdh);
131.       if (priv_key == NULL) {
               ^
132.           ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_NO_PRIVATE_VALUE);
133.           goto err;

crypto/ecdh/ech_ossl.c:136:5: 
134.       }
135.   
136. >     group = EC_KEY_get0_group(ecdh);
137.   
138.       if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {

crypto/ec/ec_key.c:421:1: start of procedure EC_KEY_get0_group()
  419.   }
  420.   
  421. > const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
  422.   {
  423.       return key->group;

crypto/ec/ec_key.c:423:5: 
  421.   const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
  422.   {
  423. >     return key->group;
  424.   }
  425.   

crypto/ec/ec_key.c:424:1: return from a call to EC_KEY_get0_group
  422.   {
  423.       return key->group;
  424. > }
  425.   
  426.   int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)

crypto/ecdh/ech_ossl.c:138:9: 
136.       group = EC_KEY_get0_group(ecdh);
137.   
138. >     if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
139.           if (!EC_GROUP_get_cofactor(group, x, ctx) ||
140.               !BN_mul(x, x, priv_key, ctx)) {

crypto/ec/ec_key.c:527:1: start of procedure EC_KEY_get_flags()
  525.   }
  526.   
  527. > int EC_KEY_get_flags(const EC_KEY *key)
  528.   {
  529.       return key->flags;

crypto/ec/ec_key.c:529:5: 
  527.   int EC_KEY_get_flags(const EC_KEY *key)
  528.   {
  529. >     return key->flags;
  530.   }
  531.   

crypto/ec/ec_key.c:530:1: return from a call to EC_KEY_get_flags
  528.   {
  529.       return key->flags;
  530. > }
  531.   
  532.   void EC_KEY_set_flags(EC_KEY *key, int flags)

crypto/ecdh/ech_ossl.c:138:9: Taking true branch
136.       group = EC_KEY_get0_group(ecdh);
137.   
138.       if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
               ^
139.           if (!EC_GROUP_get_cofactor(group, x, ctx) ||
140.               !BN_mul(x, x, priv_key, ctx)) {

crypto/ecdh/ech_ossl.c:139:14: 
137.   
138.       if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
139. >         if (!EC_GROUP_get_cofactor(group, x, ctx) ||
140.               !BN_mul(x, x, priv_key, ctx)) {
141.               ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);

crypto/ec/ec_lib.c:333:1: start of procedure EC_GROUP_get_cofactor()
  331.   }
  332.   
  333. > int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  334.                             BN_CTX *ctx)
  335.   {

crypto/ec/ec_lib.c:336:10: 
  334.                             BN_CTX *ctx)
  335.   {
  336. >     if (!BN_copy(cofactor, group->cofactor))
  337.           return 0;
  338.   

crypto/bn/bn_lib.c:430:1: start of procedure BN_copy()
    428.   }
    429.   
    430. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
    431.   {
    432.       int i;

crypto/bn/bn_lib.c:438:9: Taking false branch
    436.       bn_check_top(b);
    437.   
    438.       if (a == b)
                   ^
    439.           return (a);
    440.       if (bn_wexpand(a, b->top) == NULL)

crypto/bn/bn_lib.c:440:9: 
    438.       if (a == b)
    439.           return (a);
    440. >     if (bn_wexpand(a, b->top) == NULL)
    441.           return (NULL);
    442.   

crypto/bn/bn_lib.c:993:1: start of procedure bn_wexpand()
      991.   }
      992.   
      993. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
      994.   {
      995.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:995:13: 
      993.   BIGNUM *bn_wexpand(BIGNUM *a, int words)
      994.   {
      995. >     return (words <= a->dmax) ? a : bn_expand2(a, words);
      996.   }
      997.   

static int ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
                            EC_KEY *ecdh,
                            void *(*KDF) (const void *in, size_t inlen,
                                          void *out, size_t *outlen))
{
    BN_CTX *ctx;
    EC_POINT *tmp = NULL;
    BIGNUM *x = NULL, *y = NULL;
    const BIGNUM *priv_key;
    const EC_GROUP *group;
    int ret = -1;
    size_t buflen, len;
    unsigned char *buf = NULL;
    if (outlen > INT_MAX) {
        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
        return -1;
    }
    if ((ctx = BN_CTX_new()) == NULL)
        goto err;
    BN_CTX_start(ctx);
    x = BN_CTX_get(ctx);
    y = BN_CTX_get(ctx);
    priv_key = EC_KEY_get0_private_key(ecdh);
    if (priv_key == NULL) {
        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_NO_PRIVATE_VALUE);
        goto err;
    }
    group = EC_KEY_get0_group(ecdh);
    if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
        if (!EC_GROUP_get_cofactor(group, x, ctx) ||
            !BN_mul(x, x, priv_key, ctx)) {
            ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
            goto err;
        }
        priv_key = x;
    }
    if ((tmp = EC_POINT_new(group)) == NULL) {
        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
        goto err;
    }
    if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {
        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_POINT_ARITHMETIC_FAILURE);
        goto err;
    }
    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
        NID_X9_62_prime_field) {
        if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, y, ctx)) {
            ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_POINT_ARITHMETIC_FAILURE);
            goto err;
        }
    }
#ifndef OPENSSL_NO_EC2M
    else {
        if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, y, ctx)) {
            ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_POINT_ARITHMETIC_FAILURE);
            goto err;
        }
    }
#endif
    buflen = (EC_GROUP_get_degree(group) + 7) / 8;
    len = BN_num_bytes(x);
    if (len > buflen) {
        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
        goto err;
    }
    if ((buf = OPENSSL_malloc(buflen)) == NULL) {
        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
        goto err;
    }
    memset(buf, 0, buflen - len);
    if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {
        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_BN_LIB);
        goto err;
    }
    if (KDF != 0) {
        if (KDF(buf, buflen, out, &outlen) == NULL) {
            ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_KDF_FAILED);
            goto err;
        }
        ret = outlen;
    } else {
        if (outlen > buflen)
            outlen = buflen;
        memcpy(out, buf, outlen);
        ret = outlen;
    }
 err:
    EC_POINT_free(tmp);
    if (ctx)
        BN_CTX_end(ctx);
    BN_CTX_free(ctx);
    OPENSSL_free(buf);
    return (ret);
}
['static int ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,\n                            EC_KEY *ecdh,\n                            void *(*KDF) (const void *in, size_t inlen,\n                                          void *out, size_t *outlen))\n{\n    BN_CTX *ctx;\n    EC_POINT *tmp = NULL;\n    BIGNUM *x = NULL, *y = NULL;\n    const BIGNUM *priv_key;\n    const EC_GROUP *group;\n    int ret = -1;\n    size_t buflen, len;\n    unsigned char *buf = NULL;\n    if (outlen > INT_MAX) {\n        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n        return -1;\n    }\n    if ((ctx = BN_CTX_new()) == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    x = BN_CTX_get(ctx);\n    y = BN_CTX_get(ctx);\n    priv_key = EC_KEY_get0_private_key(ecdh);\n    if (priv_key == NULL) {\n        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_NO_PRIVATE_VALUE);\n        goto err;\n    }\n    group = EC_KEY_get0_group(ecdh);\n    if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {\n        if (!EC_GROUP_get_cofactor(group, x, ctx) ||\n            !BN_mul(x, x, priv_key, ctx)) {\n            ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        priv_key = x;\n    }\n    if ((tmp = EC_POINT_new(group)) == NULL) {\n        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {\n        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_POINT_ARITHMETIC_FAILURE);\n        goto err;\n    }\n    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n        NID_X9_62_prime_field) {\n        if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, y, ctx)) {\n            ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_POINT_ARITHMETIC_FAILURE);\n            goto err;\n        }\n    }\n#ifndef OPENSSL_NO_EC2M\n    else {\n        if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, y, ctx)) {\n            ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_POINT_ARITHMETIC_FAILURE);\n            goto err;\n        }\n    }\n#endif\n    buflen = (EC_GROUP_get_degree(group) + 7) / 8;\n    len = BN_num_bytes(x);\n    if (len > buflen) {\n        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    if ((buf = OPENSSL_malloc(buflen)) == NULL) {\n        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    memset(buf, 0, buflen - len);\n    if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {\n        ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_BN_LIB);\n        goto err;\n    }\n    if (KDF != 0) {\n        if (KDF(buf, buflen, out, &outlen) == NULL) {\n            ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ECDH_R_KDF_FAILED);\n            goto err;\n        }\n        ret = outlen;\n    } else {\n        if (outlen > buflen)\n            outlen = buflen;\n        memcpy(out, buf, outlen);\n        ret = outlen;\n    }\n err:\n    EC_POINT_free(tmp);\n    if (ctx)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    OPENSSL_free(buf);\n    return (ret);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n    CTXDBG_ENTRY("BN_CTX_start", ctx);\n    if (ctx->err_stack || ctx->too_many)\n        ctx->err_stack++;\n    else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n        BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n        ctx->err_stack++;\n    }\n    CTXDBG_EXIT(ctx);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n    BIGNUM *ret;\n    CTXDBG_ENTRY("BN_CTX_get", ctx);\n    if (ctx->err_stack || ctx->too_many)\n        return NULL;\n    if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n        ctx->too_many = 1;\n        BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n        return NULL;\n    }\n    BN_zero(ret);\n    ctx->used++;\n    CTXDBG_RET(ctx, ret);\n    return ret;\n}', 'const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)\n{\n    return key->priv_key;\n}', 'const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)\n{\n    return key->group;\n}', 'int EC_KEY_get_flags(const EC_KEY *key)\n{\n    return key->flags;\n}', 'int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,\n                          BN_CTX *ctx)\n{\n    if (!BN_copy(cofactor, group->cofactor))\n        return 0;\n    return !BN_is_zero(group->cofactor);\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n    int i;\n    BN_ULONG *A;\n    const BN_ULONG *B;\n    bn_check_top(b);\n    if (a == b)\n        return (a);\n    if (bn_wexpand(a, b->top) == NULL)\n        return (NULL);\n#if 1\n    A = a->d;\n    B = b->d;\n    for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n        BN_ULONG a0, a1, a2, a3;\n        a0 = B[0];\n        a1 = B[1];\n        a2 = B[2];\n        a3 = B[3];\n        A[0] = a0;\n        A[1] = a1;\n        A[2] = a2;\n        A[3] = a3;\n    }\n    switch (b->top & 3) {\n    case 3:\n        A[2] = B[2];\n    case 2:\n        A[1] = B[1];\n    case 1:\n        A[0] = B[0];\n    case 0:;\n    }\n#else\n    memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n    a->top = b->top;\n    a->neg = b->neg;\n    bn_check_top(a);\n    return (a);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']