crypto/rsa/rsa_gen.c:151: error: BUFFER_OVERRUN_L3
  Offset: [3, +oo] (⇐ [0, +oo] + 3) Size: [0, 8388607] by call to `BN_generate_prime_ex`.
Showing all 16 steps of the trace


crypto/rsa/rsa_gen.c:87:1: Parameter `rsa->q->d`
85.   }
86.   
87. > static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value,
88.                                 BN_GENCB *cb)
89.   {

crypto/rsa/rsa_gen.c:151:18: Call
149.           unsigned int degenerate = 0;
150.           do {
151.               if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb))
                        ^
152.                   goto err;
153.           } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));

crypto/bn/bn_prime.c:207:1: Parameter `*ret->d`
  205.   }
  206.   
  207. > int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
  208.                            const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
  209.   {

crypto/bn/bn_prime.c:244:18: Call
  242.       } else {
  243.           if (safe) {
  244.               if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
                          ^
  245.                   goto err;
  246.           } else {

crypto/bn/bn_prime.c:628:1: Parameter `*p->d`
    626.   }
    627.   
    628. > static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
    629.                                     const BIGNUM *rem, BN_CTX *ctx)
    630.   {

crypto/bn/bn_prime.c:664:10: Call
    662.   
    663.       /* we now have a random number 'rand' to test. */
    664.       if (!BN_lshift1(p, q))
                    ^
    665.           goto err;
    666.       if (!BN_add_word(p, 1))

crypto/bn/bn_shift.c:61:1: Parameter `*r->d`
      59.   #include "bn_lcl.h"
      60.   
      61. > int BN_lshift1(BIGNUM *r, const BIGNUM *a)
      62.   {
      63.       register BN_ULONG *ap, *rp, t, c;

crypto/bn/bn_shift.c:75:13: Call
      73.           r->top = a->top;
      74.       } else {
      75.           if (bn_wexpand(r, a->top + 1) == NULL)
                        ^
      76.               return (0);
      77.       }

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

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

crypto/bn/bn_lib.c:389:1: Parameter `*b->d`
          387.    */
          388.   
          389. > BIGNUM *bn_expand2(BIGNUM *b, int words)
          390.   {
          391.       bn_check_top(b);

crypto/bn/bn_lib.c:394:23: Call
          392.   
          393.       if (words > b->dmax) {
          394.           BN_ULONG *a = bn_expand_internal(b, words);
                                       ^
          395.           if (!a)
          396.               return NULL;

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

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

crypto/bn/bn_lib.c:334:5: Assignment
            332.   
            333.   #if 1
            334.       B = b->d;
                       ^
            335.       /* Check if the previous number needs to be copied */
            336.       if (B != NULL) {

crypto/bn/bn_lib.c:351:18: Array access: Offset: [3, +oo] (⇐ [0, +oo] + 3) Size: [0, 8388607] by call to `BN_generate_prime_ex` 
            349.               a1 = B[1];
            350.               a2 = B[2];
            351.               a3 = B[3];
                                    ^
            352.               A[0] = a0;
            353.               A[1] = a1;

static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
    BN_ULONG *A, *a = NULL;
    const BN_ULONG *B;
    int i;
    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 = A = OPENSSL_secure_malloc(words * sizeof(*a));
    else
        a = A = OPENSSL_malloc(words * sizeof(*a));
    if (A == NULL) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
        return (NULL);
    }
#ifdef PURIFY
    memset(a, 0, sizeof(*a) * words);
#endif
#if 1
    B = b->d;
    if (B != NULL) {
        for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
            BN_ULONG a0, a1, a2, a3;
            a0 = B[0];
            a1 = B[1];
            a2 = B[2];
            a3 = B[3];
            A[0] = a0;
            A[1] = a1;
            A[2] = a2;
            A[3] = a3;
        }
        switch (b->top & 3) {
        case 3:
            A[2] = B[2];
        case 2:
            A[1] = B[1];
        case 1:
            A[0] = B[0];
        case 0:
            ;
        }
    }
#else
    memset(A, 0, sizeof(*A) * words);
    memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
    return (a);
}
['static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value,\n                              BN_GENCB *cb)\n{\n    BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;\n    int bitsp, bitsq, ok = -1, n = 0;\n    BN_CTX *ctx = NULL;\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    bitsp = (bits + 1) / 2;\n    bitsq = bits - bitsp;\n    if (!rsa->n && ((rsa->n = BN_new()) == NULL))\n        goto err;\n    if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->e && ((rsa->e = BN_new()) == NULL))\n        goto err;\n    if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->dmp1 && ((rsa->dmp1 = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->dmq1 && ((rsa->dmq1 = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->iqmp && ((rsa->iqmp = BN_secure_new()) == NULL))\n        goto err;\n    BN_copy(rsa->e, e_value);\n    for (;;) {\n        if (!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb))\n            goto err;\n        if (!BN_sub(r2, rsa->p, BN_value_one()))\n            goto err;\n        if (!BN_gcd(r1, r2, rsa->e, ctx))\n            goto err;\n        if (BN_is_one(r1))\n            break;\n        if (!BN_GENCB_call(cb, 2, n++))\n            goto err;\n    }\n    if (!BN_GENCB_call(cb, 3, 0))\n        goto err;\n    for (;;) {\n        unsigned int degenerate = 0;\n        do {\n            if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb))\n                goto err;\n        } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));\n        if (degenerate == 3) {\n            ok = 0;\n            RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);\n            goto err;\n        }\n        if (!BN_sub(r2, rsa->q, BN_value_one()))\n            goto err;\n        if (!BN_gcd(r1, r2, rsa->e, ctx))\n            goto err;\n        if (BN_is_one(r1))\n            break;\n        if (!BN_GENCB_call(cb, 2, n++))\n            goto err;\n    }\n    if (!BN_GENCB_call(cb, 3, 1))\n        goto err;\n    if (BN_cmp(rsa->p, rsa->q) < 0) {\n        tmp = rsa->p;\n        rsa->p = rsa->q;\n        rsa->q = tmp;\n    }\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    {\n        BIGNUM *local_r0 = NULL, *pr0;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            pr0 = local_r0 = BN_new();\n            if (local_r0 == NULL)\n                goto err;\n            BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);\n        } else {\n            pr0 = r0;\n        }\n        if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {\n            BN_free(local_r0);\n            goto err;\n        }\n        BN_free(local_r0);\n    }\n    {\n        BIGNUM *local_d = NULL, *d;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            d = local_d = BN_new();\n            if (local_d == NULL)\n                goto err;\n            BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n        } else {\n            d = rsa->d;\n        }\n        if (\n               !BN_mod(rsa->dmp1, d, r1, ctx)\n            || !BN_mod(rsa->dmq1, d, r2, ctx)) {\n            BN_free(local_d);\n            goto err;\n        }\n        BN_free(local_d);\n    }\n    {\n        BIGNUM *local_p = NULL, *p;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            p = local_p = BN_new();\n            if (local_p == NULL)\n                goto err;\n            BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);\n        } else {\n            p = rsa->p;\n        }\n        if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {\n            BN_free(local_p);\n            goto err;\n        }\n        BN_free(local_p);\n    }\n    ok = 1;\n err:\n    if (ok == -1) {\n        RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN);\n        ok = 0;\n    }\n    if (ctx != NULL)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    return ok;\n}', 'int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,\n                         const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)\n{\n    BIGNUM *t;\n    int found = 0;\n    int i, j, c1 = 0;\n    BN_CTX *ctx = NULL;\n    prime_t *mods = NULL;\n    int checks = BN_prime_checks_for_size(bits);\n    mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);\n    if (mods == NULL)\n        goto err;\n    if (bits < 2) {\n        BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);\n        return 0;\n    } else if (bits == 2 && safe) {\n        BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);\n        return 0;\n    }\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    t = BN_CTX_get(ctx);\n    if (!t)\n        goto err;\n loop:\n    if (add == NULL) {\n        if (!probable_prime(ret, bits, mods))\n            goto err;\n    } else {\n        if (safe) {\n            if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))\n                goto err;\n        } else {\n            if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))\n                goto err;\n        }\n    }\n    if (!BN_GENCB_call(cb, 0, c1++))\n        goto err;\n    if (!safe) {\n        i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);\n        if (i == -1)\n            goto err;\n        if (i == 0)\n            goto loop;\n    } else {\n        if (!BN_rshift1(t, ret))\n            goto err;\n        for (i = 0; i < checks; i++) {\n            j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);\n            if (j == -1)\n                goto err;\n            if (j == 0)\n                goto loop;\n            j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);\n            if (j == -1)\n                goto err;\n            if (j == 0)\n                goto loop;\n            if (!BN_GENCB_call(cb, 2, c1 - 1))\n                goto err;\n        }\n    }\n    found = 1;\n err:\n    OPENSSL_free(mods);\n    if (ctx != NULL)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    bn_check_top(ret);\n    return found;\n}', 'static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,\n                                  const BIGNUM *rem, BN_CTX *ctx)\n{\n    int i, ret = 0;\n    BIGNUM *t1, *qadd, *q;\n    bits--;\n    BN_CTX_start(ctx);\n    t1 = BN_CTX_get(ctx);\n    q = BN_CTX_get(ctx);\n    qadd = BN_CTX_get(ctx);\n    if (qadd == NULL)\n        goto err;\n    if (!BN_rshift1(qadd, padd))\n        goto err;\n    if (!BN_rand(q, bits, 0, 1))\n        goto err;\n    if (!BN_mod(t1, q, qadd, ctx))\n        goto err;\n    if (!BN_sub(q, q, t1))\n        goto err;\n    if (rem == NULL) {\n        if (!BN_add_word(q, 1))\n            goto err;\n    } else {\n        if (!BN_rshift1(t1, rem))\n            goto err;\n        if (!BN_add(q, q, t1))\n            goto err;\n    }\n    if (!BN_lshift1(p, q))\n        goto err;\n    if (!BN_add_word(p, 1))\n        goto err;\n loop:\n    for (i = 1; i < NUMPRIMES; i++) {\n        if ((BN_mod_word(p, (BN_ULONG)primes[i]) == 0) ||\n            (BN_mod_word(q, (BN_ULONG)primes[i]) == 0)) {\n            if (!BN_add(p, p, padd))\n                goto err;\n            if (!BN_add(q, q, qadd))\n                goto err;\n            goto loop;\n        }\n    }\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    bn_check_top(p);\n    return (ret);\n}', 'int BN_lshift1(BIGNUM *r, const BIGNUM *a)\n{\n    register BN_ULONG *ap, *rp, t, c;\n    int i;\n    bn_check_top(r);\n    bn_check_top(a);\n    if (r != a) {\n        r->neg = a->neg;\n        if (bn_wexpand(r, a->top + 1) == NULL)\n            return (0);\n        r->top = a->top;\n    } else {\n        if (bn_wexpand(r, a->top + 1) == NULL)\n            return (0);\n    }\n    ap = a->d;\n    rp = r->d;\n    c = 0;\n    for (i = 0; i < a->top; i++) {\n        t = *(ap++);\n        *(rp++) = ((t << 1) | c) & BN_MASK2;\n        c = (t & BN_TBIT) ? 1 : 0;\n    }\n    if (c) {\n        *rp = 1;\n        r->top++;\n    }\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, *a = NULL;\n    const BN_ULONG *B;\n    int i;\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 = A = OPENSSL_secure_malloc(words * sizeof(*a));\n    else\n        a = A = OPENSSL_malloc(words * sizeof(*a));\n    if (A == NULL) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n        return (NULL);\n    }\n#ifdef PURIFY\n    memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n    B = b->d;\n    if (B != NULL) {\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        }\n    }\n#else\n    memset(A, 0, sizeof(*A) * words);\n    memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n    return (a);\n}']