crypto/rsa/rsa_x931g.c:61: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, +oo] by call to `BN_X931_derive_prime_ex`.
Showing all 24 steps of the trace


crypto/rsa/rsa_x931g.c:44:18: Call
42.           goto err;
43.       if (!rsa->e) {
44.           rsa->e = BN_dup(e);
                       ^
45.           if (!rsa->e)
46.               goto err;

crypto/bn/bn_lib.c:277:10: Call
  275.       if (t == NULL)
  276.           return NULL;
  277.       if (!BN_copy(t, a)) {
                  ^
  278.           BN_free(t);
  279.           return NULL;

crypto/bn/bn_lib.c:285:1: Parameter `*a->d`
    283.   }
    284.   
    285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
    286.   {
    287.       bn_check_top(b);

crypto/rsa/rsa_x931g.c:61:14: Call
59.               goto err;
60.   
61.           if (!BN_X931_derive_prime_ex(rsa->p, p1, p2,
                   ^
62.                                        Xp, Xp1, Xp2, e, ctx, cb))
63.               goto err;

crypto/bn/bn_x931p.c:94:10: Call
  92.           goto err;
  93.   
  94.       if (!BN_mul(p, p, p2, ctx))
                 ^
  95.           goto err;
  96.   

crypto/bn/bn_mul.c:497:1: Parameter `r->top`
    495.   #endif                          /* BN_RECURSION */
    496.   
    497. > int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
    498.   {
    499.       int ret = 0;

crypto/bn/bn_x931p.c:103:10: Call
  101.           goto err;
  102.   
  103.       if (!BN_sub(p, p, t))
                  ^
  104.           goto err;
  105.   

crypto/bn/bn_add.c:45:1: Parameter `a->top`
    43.   
    44.   /* signed sub of b from a. */
    45. > int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
    46.   {
    47.       int ret, r_neg, cmp_res;

crypto/bn/bn_x931p.c:111:10: Call
  109.       /* p now equals Rp */
  110.   
  111.       if (!BN_mod_sub(p, p, Xp, p1p2, ctx))
                  ^
  112.           goto err;
  113.   

crypto/bn/bn_mod.c:50:1: Parameter `*r->d`
    48.   }
    49.   
    50. > int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
    51.                  BN_CTX *ctx)
    52.   {

crypto/bn/bn_mod.c:53:10: Call
    51.                  BN_CTX *ctx)
    52.   {
    53.       if (!BN_sub(r, a, b))
                   ^
    54.           return 0;
    55.       return BN_nnmod(r, r, m, ctx);

crypto/bn/bn_add.c:45:1: Parameter `*r->d`
      43.   
      44.   /* signed sub of b from a. */
      45. > int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
      46.   {
      47.       int ret, r_neg, cmp_res;

crypto/bn/bn_add.c:54:15: Call
      52.       if (a->neg != b->neg) {
      53.           r_neg = a->neg;
      54.           ret = BN_uadd(r, a, b);
                          ^
      55.       } else {
      56.           cmp_res = BN_ucmp(a, b);

crypto/bn/bn_add.c:76:1: Parameter `*r->d`
        74.   
        75.   /* unsigned add of b to a, r can be equal to a or b. */
        76. > int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
        77.   {
        78.       int max, min, dif;

crypto/bn/bn_add.c:96:9: Call
        94.       dif = max - min;
        95.   
        96.       if (bn_wexpand(r, max + 1) == NULL)
                      ^
        97.           return 0;
        98.   

crypto/bn/bn_lib.c:910:1: Parameter `*a->d`
          908.   }
          909.   
          910. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
          911.   {
          912.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:912:37: Call
          910.   BIGNUM *bn_wexpand(BIGNUM *a, int words)
          911.   {
          912.       return (words <= a->dmax) ? a : bn_expand2(a, words);
                                                     ^
          913.   }
          914.   

crypto/bn/bn_lib.c:246:1: Parameter `*b->d`
            244.    */
            245.   
            246. > BIGNUM *bn_expand2(BIGNUM *b, int words)
            247.   {
            248.       bn_check_top(b);

crypto/bn/bn_lib.c:251:23: Call
            249.   
            250.       if (words > b->dmax) {
            251.           BN_ULONG *a = bn_expand_internal(b, words);
                                         ^
            252.           if (!a)
            253.               return NULL;

crypto/bn/bn_lib.c:208:1: <Offset trace>
206.   /* This is used by bn_expand2() */
207.   /* The caller MUST check that words > b->dmax before calling this */
208. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
209.   {
210.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:208:1: Parameter `b->top`
              206.   /* This is used by bn_expand2() */
              207.   /* The caller MUST check that words > b->dmax before calling this */
              208. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
              209.   {
              210.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:208:1: <Length trace>
206.   /* This is used by bn_expand2() */
207.   /* The caller MUST check that words > b->dmax before calling this */
208. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
209.   {
210.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:208:1: Parameter `*b->d`
              206.   /* This is used by bn_expand2() */
              207.   /* The caller MUST check that words > b->dmax before calling this */
              208. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
              209.   {
              210.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:233:9: Array access: Offset added: [8, +oo] Size: [0, +oo] by call to `BN_X931_derive_prime_ex` 
              231.       assert(b->top <= words);
              232.       if (b->top > 0)
              233.           memcpy(a, b->d, sizeof(*a) * b->top);
                             ^
              234.   
              235.       return a;

static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
    BN_ULONG *a = NULL;
    bn_check_top(b);
    if (words > (INT_MAX / (4 * BN_BITS2))) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
        return NULL;
    }
    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
        return NULL;
    }
    if (BN_get_flags(b, BN_FLG_SECURE))
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
    else
        a = OPENSSL_zalloc(words * sizeof(*a));
    if (a == NULL) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
        return NULL;
    }
    assert(b->top <= words);
    if (b->top > 0)
        memcpy(a, b->d, sizeof(*a) * b->top);
    return a;
}
['int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1,\n                       BIGNUM *q2, const BIGNUM *Xp1, const BIGNUM *Xp2,\n                       const BIGNUM *Xp, const BIGNUM *Xq1, const BIGNUM *Xq2,\n                       const BIGNUM *Xq, const BIGNUM *e, BN_GENCB *cb)\n{\n    BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL;\n    BN_CTX *ctx = NULL, *ctx2 = NULL;\n    int ret = 0;\n    if (!rsa)\n        goto err;\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    r0 = BN_CTX_get(ctx);\n    r1 = BN_CTX_get(ctx);\n    r2 = BN_CTX_get(ctx);\n    r3 = BN_CTX_get(ctx);\n    if (r3 == NULL)\n        goto err;\n    if (!rsa->e) {\n        rsa->e = BN_dup(e);\n        if (!rsa->e)\n            goto err;\n    } else {\n        e = rsa->e;\n    }\n    if (Xp && rsa->p == NULL) {\n        rsa->p = BN_new();\n        if (rsa->p == NULL)\n            goto err;\n        if (!BN_X931_derive_prime_ex(rsa->p, p1, p2,\n                                     Xp, Xp1, Xp2, e, ctx, cb))\n            goto err;\n    }\n    if (Xq && rsa->q == NULL) {\n        rsa->q = BN_new();\n        if (rsa->q == NULL)\n            goto err;\n        if (!BN_X931_derive_prime_ex(rsa->q, q1, q2,\n                                     Xq, Xq1, Xq2, e, ctx, cb))\n            goto err;\n    }\n    if (rsa->p == NULL || rsa->q == NULL) {\n        BN_CTX_end(ctx);\n        BN_CTX_free(ctx);\n        return 2;\n    }\n    rsa->n = BN_new();\n    if (rsa->n == NULL)\n        goto err;\n    if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx))\n        goto err;\n    if (!BN_sub(r1, rsa->p, BN_value_one()))\n        goto err;\n    if (!BN_sub(r2, rsa->q, BN_value_one()))\n        goto err;\n    if (!BN_mul(r0, r1, r2, ctx))\n        goto err;\n    if (!BN_gcd(r3, r1, r2, ctx))\n        goto err;\n    if (!BN_div(r0, NULL, r0, r3, ctx))\n        goto err;\n    ctx2 = BN_CTX_new();\n    if (ctx2 == NULL)\n        goto err;\n    rsa->d = BN_mod_inverse(NULL, rsa->e, r0, ctx2);\n    if (rsa->d == NULL)\n        goto err;\n    rsa->dmp1 = BN_new();\n    if (rsa->dmp1 == NULL)\n        goto err;\n    if (!BN_mod(rsa->dmp1, rsa->d, r1, ctx))\n        goto err;\n    rsa->dmq1 = BN_new();\n    if (rsa->dmq1 == NULL)\n        goto err;\n    if (!BN_mod(rsa->dmq1, rsa->d, r2, ctx))\n        goto err;\n    rsa->iqmp = BN_mod_inverse(NULL, rsa->q, rsa->p, ctx2);\n    ret = 1;\n err:\n    if (ctx)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    BN_CTX_free(ctx2);\n    return ret;\n}', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n    BIGNUM *t;\n    if (a == NULL)\n        return NULL;\n    bn_check_top(a);\n    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n    if (t == NULL)\n        return NULL;\n    if (!BN_copy(t, a)) {\n        BN_free(t);\n        return NULL;\n    }\n    bn_check_top(t);\n    return t;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\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 (b->top > 0)\n        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n    a->top = b->top;\n    a->neg = b->neg;\n    bn_check_top(a);\n    return a;\n}', 'int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,\n                            const BIGNUM *Xp, const BIGNUM *Xp1,\n                            const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,\n                            BN_GENCB *cb)\n{\n    int ret = 0;\n    BIGNUM *t, *p1p2, *pm1;\n    if (!BN_is_odd(e))\n        return 0;\n    BN_CTX_start(ctx);\n    if (p1 == NULL)\n        p1 = BN_CTX_get(ctx);\n    if (p2 == NULL)\n        p2 = BN_CTX_get(ctx);\n    t = BN_CTX_get(ctx);\n    p1p2 = BN_CTX_get(ctx);\n    pm1 = BN_CTX_get(ctx);\n    if (pm1 == NULL)\n        goto err;\n    if (!bn_x931_derive_pi(p1, Xp1, ctx, cb))\n        goto err;\n    if (!bn_x931_derive_pi(p2, Xp2, ctx, cb))\n        goto err;\n    if (!BN_mul(p1p2, p1, p2, ctx))\n        goto err;\n    if (!BN_mod_inverse(p, p2, p1, ctx))\n        goto err;\n    if (!BN_mul(p, p, p2, ctx))\n        goto err;\n    if (!BN_mod_inverse(t, p1, p2, ctx))\n        goto err;\n    if (!BN_mul(t, t, p1, ctx))\n        goto err;\n    if (!BN_sub(p, p, t))\n        goto err;\n    if (p->neg && !BN_add(p, p, p1p2))\n        goto err;\n    if (!BN_mod_sub(p, p, Xp, p1p2, ctx))\n        goto err;\n    if (!BN_add(p, p, Xp))\n        goto err;\n    for (;;) {\n        int i = 1;\n        BN_GENCB_call(cb, 0, i++);\n        if (!BN_copy(pm1, p))\n            goto err;\n        if (!BN_sub_word(pm1, 1))\n            goto err;\n        if (!BN_gcd(t, pm1, e, ctx))\n            goto err;\n        if (BN_is_one(t)) {\n            int r = BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb);\n            if (r < 0)\n                goto err;\n            if (r)\n                break;\n        }\n        if (!BN_add(p, p, p1p2))\n            goto err;\n    }\n    BN_GENCB_call(cb, 3, 0);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n    int ret = 0;\n    int top, al, bl;\n    BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n    int i;\n#endif\n#ifdef BN_RECURSION\n    BIGNUM *t = NULL;\n    int j = 0, k;\n#endif\n    bn_check_top(a);\n    bn_check_top(b);\n    bn_check_top(r);\n    al = a->top;\n    bl = b->top;\n    if ((al == 0) || (bl == 0)) {\n        BN_zero(r);\n        return 1;\n    }\n    top = al + bl;\n    BN_CTX_start(ctx);\n    if ((r == a) || (r == b)) {\n        if ((rr = BN_CTX_get(ctx)) == NULL)\n            goto err;\n    } else\n        rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n    i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n    if (i == 0) {\n# if 0\n        if (al == 4) {\n            if (bn_wexpand(rr, 8) == NULL)\n                goto err;\n            rr->top = 8;\n            bn_mul_comba4(rr->d, a->d, b->d);\n            goto end;\n        }\n# endif\n        if (al == 8) {\n            if (bn_wexpand(rr, 16) == NULL)\n                goto err;\n            rr->top = 16;\n            bn_mul_comba8(rr->d, a->d, b->d);\n            goto end;\n        }\n    }\n#endif\n#ifdef BN_RECURSION\n    if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n        if (i >= -1 && i <= 1) {\n            if (i >= 0) {\n                j = BN_num_bits_word((BN_ULONG)al);\n            }\n            if (i == -1) {\n                j = BN_num_bits_word((BN_ULONG)bl);\n            }\n            j = 1 << (j - 1);\n            assert(j <= al || j <= bl);\n            k = j + j;\n            t = BN_CTX_get(ctx);\n            if (t == NULL)\n                goto err;\n            if (al > j || bl > j) {\n                if (bn_wexpand(t, k * 4) == NULL)\n                    goto err;\n                if (bn_wexpand(rr, k * 4) == NULL)\n                    goto err;\n                bn_mul_part_recursive(rr->d, a->d, b->d,\n                                      j, al - j, bl - j, t->d);\n            } else {\n                if (bn_wexpand(t, k * 2) == NULL)\n                    goto err;\n                if (bn_wexpand(rr, k * 2) == NULL)\n                    goto err;\n                bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n            }\n            rr->top = top;\n            goto end;\n        }\n    }\n#endif\n    if (bn_wexpand(rr, top) == NULL)\n        goto err;\n    rr->top = top;\n    bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n    rr->neg = a->neg ^ b->neg;\n    bn_correct_top(rr);\n    if (r != rr && BN_copy(r, rr) == NULL)\n        goto err;\n    ret = 1;\n err:\n    bn_check_top(r);\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int ret, r_neg, cmp_res;\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a->neg != b->neg) {\n        r_neg = a->neg;\n        ret = BN_uadd(r, a, b);\n    } else {\n        cmp_res = BN_ucmp(a, b);\n        if (cmp_res > 0) {\n            r_neg = a->neg;\n            ret = BN_usub(r, a, b);\n        } else if (cmp_res < 0) {\n            r_neg = !b->neg;\n            ret = BN_usub(r, b, a);\n        } else {\n            r_neg = 0;\n            BN_zero(r);\n            ret = 1;\n        }\n    }\n    r->neg = r_neg;\n    bn_check_top(r);\n    return ret;\n}', 'int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n               BN_CTX *ctx)\n{\n    if (!BN_sub(r, a, b))\n        return 0;\n    return BN_nnmod(r, r, m, ctx);\n}', 'int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int max, min, dif;\n    const BN_ULONG *ap, *bp;\n    BN_ULONG *rp, carry, t1, t2;\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a->top < b->top) {\n        const BIGNUM *tmp;\n        tmp = a;\n        a = b;\n        b = tmp;\n    }\n    max = a->top;\n    min = b->top;\n    dif = max - min;\n    if (bn_wexpand(r, max + 1) == NULL)\n        return 0;\n    r->top = max;\n    ap = a->d;\n    bp = b->d;\n    rp = r->d;\n    carry = bn_add_words(rp, ap, bp, min);\n    rp += min;\n    ap += min;\n    while (dif) {\n        dif--;\n        t1 = *(ap++);\n        t2 = (t1 + carry) & BN_MASK2;\n        *(rp++) = t2;\n        carry &= (t2 == 0);\n    }\n    *rp = carry;\n    r->top += carry;\n    r->neg = 0;\n    bn_check_top(r);\n    return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n    bn_check_top(b);\n    if (words > b->dmax) {\n        BN_ULONG *a = bn_expand_internal(b, words);\n        if (!a)\n            return NULL;\n        if (b->d) {\n            OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n            bn_free_d(b);\n        }\n        b->d = a;\n        b->dmax = words;\n    }\n    bn_check_top(b);\n    return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n    BN_ULONG *a = NULL;\n    bn_check_top(b);\n    if (words > (INT_MAX / (4 * BN_BITS2))) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n        return NULL;\n    }\n    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n        return NULL;\n    }\n    if (BN_get_flags(b, BN_FLG_SECURE))\n        a = OPENSSL_secure_zalloc(words * sizeof(*a));\n    else\n        a = OPENSSL_zalloc(words * sizeof(*a));\n    if (a == NULL) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n        return NULL;\n    }\n    assert(b->top <= words);\n    if (b->top > 0)\n        memcpy(a, b->d, sizeof(*a) * b->top);\n    return a;\n}']