apps/x509.c:672: error: BUFFER_OVERRUN_L3
  Offset: [3, +oo] (⇐ [0, +oo] + 3) Size: [0, 8388607] by call to `BN_add_word`.
Showing all 14 steps of the trace


apps/x509.c:324:24: Call
322.               break;
323.           case OPT_SET_SERIAL:
324.               if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
                              ^
325.                   goto opthelp;
326.               break;

crypto/x509v3/v3_utl.c:201:15: Call
  199.   
  200.       if (ishex)
  201.           ret = BN_hex2bn(&bn, value);
                       ^
  202.       else
  203.           ret = BN_dec2bn(&bn, value);

crypto/bn/bn_print.c:172:1: Parameter `*(*bn)->d`
    170.   }
    171.   
    172. > int BN_hex2bn(BIGNUM **bn, const char *a)
    173.   {
    174.       BIGNUM *ret = NULL;

apps/x509.c:672:22: Call
670.                   if (!bnser)
671.                       goto end;
672.                   if (!BN_add_word(bnser, 1))
                            ^
673.                       goto end;
674.                   ser = BN_to_ASN1_INTEGER(bnser, NULL);

crypto/bn/bn_word.c:123:1: Parameter `*a->d`
  121.   }
  122.   
  123. > int BN_add_word(BIGNUM *a, BN_ULONG w)
  124.   {
  125.       BN_ULONG l;

crypto/bn/bn_word.c:136:16: Call
  134.       /* degenerate case: a is zero */
  135.       if (BN_is_zero(a))
  136.           return BN_set_word(a, w);
                        ^
  137.       /* handle 'a' when negative */
  138.       if (a->neg) {

crypto/bn/bn_lib.c:530:1: Parameter `*a->d`
    528.   }
    529.   
    530. > int BN_set_word(BIGNUM *a, BN_ULONG w)
    531.   {
    532.       bn_check_top(a);

crypto/bn/bn_lib.c:533:9: Call
    531.   {
    532.       bn_check_top(a);
    533.       if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
                   ^
    534.           return (0);
    535.       a->neg = 0;

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

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

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

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

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

crypto/bn/bn_lib.c:352:18: Array access: Offset: [3, +oo] (⇐ [0, +oo] + 3) Size: [0, 8388607] by call to `BN_add_word` 
        350.               a1 = B[1];
        351.               a2 = B[2];
        352.               a3 = B[3];
                                ^
        353.               A[0] = a0;
        354.               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);
}
['int x509_main(int argc, char **argv)\n{\n    ASN1_INTEGER *sno = NULL;\n    ASN1_OBJECT *objtmp;\n    BIO *out = NULL;\n    CONF *extconf = NULL;\n    EVP_PKEY *Upkey = NULL, *CApkey = NULL, *fkey = NULL;\n    STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;\n    STACK_OF(OPENSSL_STRING) *sigopts = NULL;\n    X509 *x = NULL, *xca = NULL;\n    X509_REQ *req = NULL, *rq = NULL;\n    X509_STORE *ctx = NULL;\n    const EVP_MD *digest = NULL;\n    char *CAkeyfile = NULL, *CAserial = NULL, *fkeyfile = NULL, *alias = NULL;\n    char *checkhost = NULL, *checkemail = NULL, *checkip = NULL;\n    char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;\n    char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;\n    char buf[256], *prog;\n    int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0, pprint = 0;\n    int C = 0, CAformat = FORMAT_PEM, CAkeyformat = FORMAT_PEM;\n    int fingerprint = 0, reqfile = 0, need_rand = 0, checkend = 0;\n    int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;\n    int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;\n    int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0;\n    int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;\n    int ret = 1, i, num = 0, badsig = 0, clrext = 0, nocert = 0;\n    int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0;\n    int checkoffset = 0, enddate = 0;\n    unsigned long nmflag = 0, certflag = 0;\n    char nmflag_set = 0;\n    OPTION_CHOICE o;\n    ENGINE *e = NULL;\n#ifndef OPENSSL_NO_MD5\n    int subject_hash_old = 0, issuer_hash_old = 0;\n#endif\n    ctx = X509_STORE_new();\n    if (ctx == NULL)\n        goto end;\n    X509_STORE_set_verify_cb(ctx, callb);\n    prog = opt_init(argc, argv, x509_options);\n    while ((o = opt_next()) != OPT_EOF) {\n        switch (o) {\n        case OPT_EOF:\n        case OPT_ERR:\n opthelp:\n            BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n            goto end;\n        case OPT_HELP:\n            opt_help(x509_options);\n            ret = 0;\n            goto end;\n        case OPT_INFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))\n                goto opthelp;\n            break;\n        case OPT_IN:\n            infile = opt_arg();\n            break;\n        case OPT_OUTFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))\n                goto opthelp;\n            break;\n        case OPT_KEYFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))\n                goto opthelp;\n            break;\n        case OPT_CAFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat))\n                goto opthelp;\n            break;\n        case OPT_CAKEYFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAkeyformat))\n                goto opthelp;\n            break;\n        case OPT_OUT:\n            outfile = opt_arg();\n            break;\n        case OPT_REQ:\n            reqfile = need_rand = 1;\n            break;\n        case OPT_SIGOPT:\n            if (!sigopts)\n                sigopts = sk_OPENSSL_STRING_new_null();\n            if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))\n                goto opthelp;\n            break;\n        case OPT_FORCE_VERSION:\n#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL\n            force_version = atoi(opt_arg()) - 1;\n#endif\n            break;\n        case OPT_DAYS:\n            days = atoi(opt_arg());\n            break;\n        case OPT_PASSIN:\n            passinarg = opt_arg();\n            break;\n        case OPT_EXTFILE:\n            extfile = opt_arg();\n            break;\n        case OPT_EXTENSIONS:\n            extsect = opt_arg();\n            break;\n        case OPT_SIGNKEY:\n            keyfile = opt_arg();\n            sign_flag = ++num;\n            need_rand = 1;\n            break;\n        case OPT_CA:\n            CAfile = opt_arg();\n            CA_flag = ++num;\n            need_rand = 1;\n            break;\n        case OPT_CAKEY:\n            CAkeyfile = opt_arg();\n            break;\n        case OPT_CASERIAL:\n            CAserial = opt_arg();\n            break;\n        case OPT_SET_SERIAL:\n            if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)\n                goto opthelp;\n            break;\n        case OPT_FORCE_PUBKEY:\n            fkeyfile = opt_arg();\n            break;\n        case OPT_ADDTRUST:\n            if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {\n                BIO_printf(bio_err,\n                           "%s: Invalid trust object value %s\\n",\n                           prog, opt_arg());\n                goto opthelp;\n            }\n            if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)\n                goto end;\n            sk_ASN1_OBJECT_push(trust, objtmp);\n            trustout = 1;\n            break;\n        case OPT_ADDREJECT:\n            if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {\n                BIO_printf(bio_err,\n                           "%s: Invalid reject object value %s\\n",\n                           prog, opt_arg());\n                goto opthelp;\n            }\n            if (reject == NULL\n                && (reject = sk_ASN1_OBJECT_new_null()) == NULL)\n                goto end;\n            sk_ASN1_OBJECT_push(reject, objtmp);\n            trustout = 1;\n            break;\n        case OPT_SETALIAS:\n            alias = opt_arg();\n            trustout = 1;\n            break;\n        case OPT_CERTOPT:\n            if (!set_cert_ex(&certflag, opt_arg()))\n                goto opthelp;\n            break;\n        case OPT_NAMEOPT:\n            nmflag_set = 1;\n            if (!set_name_ex(&nmflag, opt_arg()))\n                goto opthelp;\n            break;\n        case OPT_ENGINE:\n            e = setup_engine(opt_arg(), 0);\n            break;\n        case OPT_C:\n            C = ++num;\n            break;\n        case OPT_EMAIL:\n            email = ++num;\n            break;\n        case OPT_OCSP_URI:\n            ocsp_uri = ++num;\n            break;\n        case OPT_SERIAL:\n            serial = ++num;\n            break;\n        case OPT_NEXT_SERIAL:\n            next_serial = ++num;\n            break;\n        case OPT_MODULUS:\n            modulus = ++num;\n            break;\n        case OPT_PUBKEY:\n            pubkey = ++num;\n            break;\n        case OPT_X509TOREQ:\n            x509req = ++num;\n            break;\n        case OPT_TEXT:\n            text = ++num;\n            break;\n        case OPT_SUBJECT:\n            subject = ++num;\n            break;\n        case OPT_ISSUER:\n            issuer = ++num;\n            break;\n        case OPT_FINGERPRINT:\n            fingerprint = ++num;\n            break;\n        case OPT_HASH:\n            subject_hash = ++num;\n            break;\n        case OPT_ISSUER_HASH:\n            issuer_hash = ++num;\n            break;\n        case OPT_PURPOSE:\n            pprint = ++num;\n            break;\n        case OPT_STARTDATE:\n            startdate = ++num;\n            break;\n        case OPT_ENDDATE:\n            enddate = ++num;\n            break;\n        case OPT_NOOUT:\n            noout = ++num;\n            break;\n        case OPT_NOCERT:\n            nocert = 1;\n            break;\n        case OPT_TRUSTOUT:\n            trustout = 1;\n            break;\n        case OPT_CLRTRUST:\n            clrtrust = ++num;\n            break;\n        case OPT_CLRREJECT:\n            clrreject = ++num;\n            break;\n        case OPT_ALIAS:\n            aliasout = ++num;\n            break;\n        case OPT_CACREATESERIAL:\n            CA_createserial = ++num;\n            break;\n        case OPT_CLREXT:\n            clrext = 1;\n            break;\n        case OPT_OCSPID:\n            ocspid = ++num;\n            break;\n        case OPT_BADSIG:\n            badsig = 1;\n            break;\n#ifndef OPENSSL_NO_MD5\n        case OPT_SUBJECT_HASH_OLD:\n            subject_hash_old = ++num;\n            break;\n        case OPT_ISSUER_HASH_OLD:\n            issuer_hash_old = ++num;\n            break;\n#else\n        case OPT_SUBJECT_HASH_OLD:\n        case OPT_ISSUER_HASH_OLD:\n            break;\n#endif\n        case OPT_DATES:\n            startdate = ++num;\n            enddate = ++num;\n            break;\n        case OPT_CHECKEND:\n            checkoffset = atoi(opt_arg());\n            checkend = 1;\n            break;\n        case OPT_CHECKHOST:\n            checkhost = opt_arg();\n            break;\n        case OPT_CHECKEMAIL:\n            checkemail = opt_arg();\n            break;\n        case OPT_CHECKIP:\n            checkip = opt_arg();\n            break;\n        case OPT_MD:\n            if (!opt_md(opt_unknown(), &digest))\n                goto opthelp;\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    if (argc != 0) {\n        BIO_printf(bio_err, "%s: Unknown parameter %s\\n", prog, argv[0]);\n        goto opthelp;\n    }\n    if (!nmflag_set)\n        nmflag = XN_FLAG_ONELINE;\n    out = bio_open_default(outfile, \'w\', outformat);\n    if (out == NULL)\n        goto end;\n    if (need_rand)\n        app_RAND_load_file(NULL, 0);\n    if (!app_passwd(passinarg, NULL, &passin, NULL)) {\n        BIO_printf(bio_err, "Error getting password\\n");\n        goto end;\n    }\n    if (!X509_STORE_set_default_paths(ctx)) {\n        ERR_print_errors(bio_err);\n        goto end;\n    }\n    if (fkeyfile) {\n        fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key");\n        if (fkey == NULL)\n            goto end;\n    }\n    if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {\n        CAkeyfile = CAfile;\n    } else if ((CA_flag) && (CAkeyfile == NULL)) {\n        BIO_printf(bio_err,\n                   "need to specify a CAkey if using the CA command\\n");\n        goto end;\n    }\n    if (extfile) {\n        X509V3_CTX ctx2;\n        if ((extconf = app_load_config(extfile)) == NULL)\n            goto end;\n        if (!extsect) {\n            extsect = NCONF_get_string(extconf, "default", "extensions");\n            if (!extsect) {\n                ERR_clear_error();\n                extsect = "default";\n            }\n        }\n        X509V3_set_ctx_test(&ctx2);\n        X509V3_set_nconf(&ctx2, extconf);\n        if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {\n            BIO_printf(bio_err,\n                       "Error Loading extension section %s\\n", extsect);\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (reqfile) {\n        EVP_PKEY *pkey;\n        BIO *in;\n        if (!sign_flag && !CA_flag) {\n            BIO_printf(bio_err, "We need a private key to sign with\\n");\n            goto end;\n        }\n        in = bio_open_default(infile, \'r\', informat);\n        if (in == NULL)\n            goto end;\n        req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);\n        BIO_free(in);\n        if (req == NULL) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n        if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {\n            BIO_printf(bio_err, "error unpacking public key\\n");\n            goto end;\n        }\n        i = X509_REQ_verify(req, pkey);\n        EVP_PKEY_free(pkey);\n        if (i < 0) {\n            BIO_printf(bio_err, "Signature verification error\\n");\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n        if (i == 0) {\n            BIO_printf(bio_err,\n                       "Signature did not match the certificate request\\n");\n            goto end;\n        } else\n            BIO_printf(bio_err, "Signature ok\\n");\n        print_name(bio_err, "subject=", X509_REQ_get_subject_name(req),\n                   nmflag);\n        if ((x = X509_new()) == NULL)\n            goto end;\n        if (sno == NULL) {\n            sno = ASN1_INTEGER_new();\n            if (sno == NULL || !rand_serial(NULL, sno))\n                goto end;\n            if (!X509_set_serialNumber(x, sno))\n                goto end;\n            ASN1_INTEGER_free(sno);\n            sno = NULL;\n        } else if (!X509_set_serialNumber(x, sno))\n            goto end;\n        if (!X509_set_issuer_name(x, X509_REQ_get_subject_name(req)))\n            goto end;\n        if (!X509_set_subject_name(x, X509_REQ_get_subject_name(req)))\n            goto end;\n        X509_gmtime_adj(X509_get_notBefore(x), 0);\n        X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL);\n        if (fkey)\n            X509_set_pubkey(x, fkey);\n        else {\n            pkey = X509_REQ_get_pubkey(req);\n            X509_set_pubkey(x, pkey);\n            EVP_PKEY_free(pkey);\n        }\n    } else\n        x = load_cert(infile, informat, NULL, e, "Certificate");\n    if (x == NULL)\n        goto end;\n    if (CA_flag) {\n        xca = load_cert(CAfile, CAformat, NULL, e, "CA Certificate");\n        if (xca == NULL)\n            goto end;\n    }\n    if (!noout || text || next_serial) {\n        OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");\n    }\n    if (alias)\n        X509_alias_set1(x, (unsigned char *)alias, -1);\n    if (clrtrust)\n        X509_trust_clear(x);\n    if (clrreject)\n        X509_reject_clear(x);\n    if (trust) {\n        for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {\n            objtmp = sk_ASN1_OBJECT_value(trust, i);\n            X509_add1_trust_object(x, objtmp);\n        }\n    }\n    if (reject) {\n        for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {\n            objtmp = sk_ASN1_OBJECT_value(reject, i);\n            X509_add1_reject_object(x, objtmp);\n        }\n    }\n    if (num) {\n        for (i = 1; i <= num; i++) {\n            if (issuer == i) {\n                print_name(out, "issuer= ", X509_get_issuer_name(x), nmflag);\n            } else if (subject == i) {\n                print_name(out, "subject= ",\n                           X509_get_subject_name(x), nmflag);\n            } else if (serial == i) {\n                BIO_printf(out, "serial=");\n                i2a_ASN1_INTEGER(out, X509_get_serialNumber(x));\n                BIO_printf(out, "\\n");\n            } else if (next_serial == i) {\n                BIGNUM *bnser;\n                ASN1_INTEGER *ser;\n                ser = X509_get_serialNumber(x);\n                bnser = ASN1_INTEGER_to_BN(ser, NULL);\n                if (!bnser)\n                    goto end;\n                if (!BN_add_word(bnser, 1))\n                    goto end;\n                ser = BN_to_ASN1_INTEGER(bnser, NULL);\n                if (!ser)\n                    goto end;\n                BN_free(bnser);\n                i2a_ASN1_INTEGER(out, ser);\n                ASN1_INTEGER_free(ser);\n                BIO_puts(out, "\\n");\n            } else if ((email == i) || (ocsp_uri == i)) {\n                int j;\n                STACK_OF(OPENSSL_STRING) *emlst;\n                if (email == i)\n                    emlst = X509_get1_email(x);\n                else\n                    emlst = X509_get1_ocsp(x);\n                for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)\n                    BIO_printf(out, "%s\\n",\n                               sk_OPENSSL_STRING_value(emlst, j));\n                X509_email_free(emlst);\n            } else if (aliasout == i) {\n                unsigned char *alstr;\n                alstr = X509_alias_get0(x, NULL);\n                if (alstr)\n                    BIO_printf(out, "%s\\n", alstr);\n                else\n                    BIO_puts(out, "<No Alias>\\n");\n            } else if (subject_hash == i) {\n                BIO_printf(out, "%08lx\\n", X509_subject_name_hash(x));\n            }\n#ifndef OPENSSL_NO_MD5\n            else if (subject_hash_old == i) {\n                BIO_printf(out, "%08lx\\n", X509_subject_name_hash_old(x));\n            }\n#endif\n            else if (issuer_hash == i) {\n                BIO_printf(out, "%08lx\\n", X509_issuer_name_hash(x));\n            }\n#ifndef OPENSSL_NO_MD5\n            else if (issuer_hash_old == i) {\n                BIO_printf(out, "%08lx\\n", X509_issuer_name_hash_old(x));\n            }\n#endif\n            else if (pprint == i) {\n                X509_PURPOSE *ptmp;\n                int j;\n                BIO_printf(out, "Certificate purposes:\\n");\n                for (j = 0; j < X509_PURPOSE_get_count(); j++) {\n                    ptmp = X509_PURPOSE_get0(j);\n                    purpose_print(out, x, ptmp);\n                }\n            } else if (modulus == i) {\n                EVP_PKEY *pkey;\n                pkey = X509_get0_pubkey(x);\n                if (pkey == NULL) {\n                    BIO_printf(bio_err, "Modulus=unavailable\\n");\n                    ERR_print_errors(bio_err);\n                    goto end;\n                }\n                BIO_printf(out, "Modulus=");\n#ifndef OPENSSL_NO_RSA\n                if (pkey->type == EVP_PKEY_RSA)\n                    BN_print(out, pkey->pkey.rsa->n);\n                else\n#endif\n#ifndef OPENSSL_NO_DSA\n                if (pkey->type == EVP_PKEY_DSA)\n                    BN_print(out, pkey->pkey.dsa->pub_key);\n                else\n#endif\n                    BIO_printf(out, "Wrong Algorithm type");\n                BIO_printf(out, "\\n");\n            } else if (pubkey == i) {\n                EVP_PKEY *pkey;\n                pkey = X509_get0_pubkey(x);\n                if (pkey == NULL) {\n                    BIO_printf(bio_err, "Error getting public key\\n");\n                    ERR_print_errors(bio_err);\n                    goto end;\n                }\n                PEM_write_bio_PUBKEY(out, pkey);\n            } else if (C == i) {\n                unsigned char *d;\n                char *m;\n                int len;\n                X509_NAME_oneline(X509_get_subject_name(x), buf, sizeof buf);\n                BIO_printf(out, "/*\\n"\n                                " * Subject: %s\\n", buf);\n                m = X509_NAME_oneline(X509_get_issuer_name(x), buf, sizeof buf);\n                BIO_printf(out, " * Issuer:  %s\\n"\n                                " */\\n", buf);\n                len = i2d_X509(x, NULL);\n                m = app_malloc(len, "x509 name buffer");\n                d = (unsigned char *)m;\n                len = i2d_X509_NAME(X509_get_subject_name(x), &d);\n                print_array(out, "the_subject_name", len, (unsigned char *)m);\n                d = (unsigned char *)m;\n                len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);\n                print_array(out, "the_public_key", len, (unsigned char *)m);\n                d = (unsigned char *)m;\n                len = i2d_X509(x, &d);\n                print_array(out, "the_certificate", len, (unsigned char *)m);\n                OPENSSL_free(m);\n            } else if (text == i) {\n                X509_print_ex(out, x, nmflag, certflag);\n            } else if (startdate == i) {\n                BIO_puts(out, "notBefore=");\n                ASN1_TIME_print(out, X509_get_notBefore(x));\n                BIO_puts(out, "\\n");\n            } else if (enddate == i) {\n                BIO_puts(out, "notAfter=");\n                ASN1_TIME_print(out, X509_get_notAfter(x));\n                BIO_puts(out, "\\n");\n            } else if (fingerprint == i) {\n                int j;\n                unsigned int n;\n                unsigned char md[EVP_MAX_MD_SIZE];\n                const EVP_MD *fdig = digest;\n                if (!fdig)\n                    fdig = EVP_sha1();\n                if (!X509_digest(x, fdig, md, &n)) {\n                    BIO_printf(bio_err, "out of memory\\n");\n                    goto end;\n                }\n                BIO_printf(out, "%s Fingerprint=",\n                           OBJ_nid2sn(EVP_MD_type(fdig)));\n                for (j = 0; j < (int)n; j++) {\n                    BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n)\n                               ? \'\\n\' : \':\');\n                }\n            }\n            else if ((sign_flag == i) && (x509req == 0)) {\n                BIO_printf(bio_err, "Getting Private key\\n");\n                if (Upkey == NULL) {\n                    Upkey = load_key(keyfile, keyformat, 0,\n                                     passin, e, "Private key");\n                    if (Upkey == NULL)\n                        goto end;\n                }\n                assert(need_rand);\n                if (!sign(x, Upkey, days, clrext, digest, extconf, extsect))\n                    goto end;\n            } else if (CA_flag == i) {\n                BIO_printf(bio_err, "Getting CA Private Key\\n");\n                if (CAkeyfile != NULL) {\n                    CApkey = load_key(CAkeyfile, CAkeyformat,\n                                      0, passin, e, "CA Private Key");\n                    if (CApkey == NULL)\n                        goto end;\n                }\n                assert(need_rand);\n                if (!x509_certify(ctx, CAfile, digest, x, xca,\n                                  CApkey, sigopts,\n                                  CAserial, CA_createserial, days, clrext,\n                                  extconf, extsect, sno, reqfile))\n                    goto end;\n            } else if (x509req == i) {\n                EVP_PKEY *pk;\n                BIO_printf(bio_err, "Getting request Private Key\\n");\n                if (keyfile == NULL) {\n                    BIO_printf(bio_err, "no request key file specified\\n");\n                    goto end;\n                } else {\n                    pk = load_key(keyfile, keyformat, 0,\n                                  passin, e, "request key");\n                    if (pk == NULL)\n                        goto end;\n                }\n                BIO_printf(bio_err, "Generating certificate request\\n");\n                rq = X509_to_X509_REQ(x, pk, digest);\n                EVP_PKEY_free(pk);\n                if (rq == NULL) {\n                    ERR_print_errors(bio_err);\n                    goto end;\n                }\n                if (!noout) {\n                    X509_REQ_print(out, rq);\n                    PEM_write_bio_X509_REQ(out, rq);\n                }\n                noout = 1;\n            } else if (ocspid == i) {\n                X509_ocspid_print(out, x);\n            }\n        }\n    }\n    if (checkend) {\n        time_t tcheck = time(NULL) + checkoffset;\n        if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {\n            BIO_printf(out, "Certificate will expire\\n");\n            ret = 1;\n        } else {\n            BIO_printf(out, "Certificate will not expire\\n");\n            ret = 0;\n        }\n        goto end;\n    }\n    print_cert_checks(out, x, checkhost, checkemail, checkip);\n    if (noout || nocert) {\n        ret = 0;\n        goto end;\n    }\n    if (badsig) {\n        ASN1_BIT_STRING *signature;\n        unsigned char *s;\n        X509_get0_signature(&signature, NULL, x);\n        s = ASN1_STRING_data(signature);\n        s[ASN1_STRING_length(signature) - 1] ^= 0x1;\n    }\n    if (outformat == FORMAT_ASN1)\n        i = i2d_X509_bio(out, x);\n    else if (outformat == FORMAT_PEM) {\n        if (trustout)\n            i = PEM_write_bio_X509_AUX(out, x);\n        else\n            i = PEM_write_bio_X509(out, x);\n    } else {\n        BIO_printf(bio_err, "bad output format specified for outfile\\n");\n        goto end;\n    }\n    if (!i) {\n        BIO_printf(bio_err, "unable to write certificate\\n");\n        ERR_print_errors(bio_err);\n        goto end;\n    }\n    ret = 0;\n end:\n    if (need_rand)\n        app_RAND_write_file(NULL);\n    OBJ_cleanup();\n    NCONF_free(extconf);\n    BIO_free_all(out);\n    X509_STORE_free(ctx);\n    X509_REQ_free(req);\n    X509_free(x);\n    X509_free(xca);\n    EVP_PKEY_free(Upkey);\n    EVP_PKEY_free(CApkey);\n    EVP_PKEY_free(fkey);\n    sk_OPENSSL_STRING_free(sigopts);\n    X509_REQ_free(rq);\n    ASN1_INTEGER_free(sno);\n    sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);\n    sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);\n    OPENSSL_free(passin);\n    return (ret);\n}', "ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)\n{\n    BIGNUM *bn = NULL;\n    ASN1_INTEGER *aint;\n    int isneg, ishex;\n    int ret;\n    if (value == NULL) {\n        X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);\n        return NULL;\n    }\n    bn = BN_new();\n    if (bn == NULL) {\n        X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);\n        return NULL;\n    }\n    if (value[0] == '-') {\n        value++;\n        isneg = 1;\n    } else\n        isneg = 0;\n    if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {\n        value += 2;\n        ishex = 1;\n    } else\n        ishex = 0;\n    if (ishex)\n        ret = BN_hex2bn(&bn, value);\n    else\n        ret = BN_dec2bn(&bn, value);\n    if (!ret || value[ret]) {\n        BN_free(bn);\n        X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);\n        return NULL;\n    }\n    if (isneg && BN_is_zero(bn))\n        isneg = 0;\n    aint = BN_to_ASN1_INTEGER(bn, NULL);\n    BN_free(bn);\n    if (!aint) {\n        X509V3err(X509V3_F_S2I_ASN1_INTEGER,\n                  X509V3_R_BN_TO_ASN1_INTEGER_ERROR);\n        return NULL;\n    }\n    if (isneg)\n        aint->type |= V_ASN1_NEG;\n    return aint;\n}", "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n    BIGNUM *ret = NULL;\n    BN_ULONG l = 0;\n    int neg = 0, h, m, i, j, k, c;\n    int num;\n    if ((a == NULL) || (*a == '\\0'))\n        return (0);\n    if (*a == '-') {\n        neg = 1;\n        a++;\n    }\n    for (i = 0; isxdigit((unsigned char)a[i]); i++) ;\n    num = i + neg;\n    if (bn == NULL)\n        return (num);\n    if (*bn == NULL) {\n        if ((ret = BN_new()) == NULL)\n            return (0);\n    } else {\n        ret = *bn;\n        BN_zero(ret);\n    }\n    if (bn_expand(ret, i * 4) == NULL)\n        goto err;\n    j = i;\n    m = 0;\n    h = 0;\n    while (j > 0) {\n        m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;\n        l = 0;\n        for (;;) {\n            c = a[j - m];\n            if ((c >= '0') && (c <= '9'))\n                k = c - '0';\n            else if ((c >= 'a') && (c <= 'f'))\n                k = c - 'a' + 10;\n            else if ((c >= 'A') && (c <= 'F'))\n                k = c - 'A' + 10;\n            else\n                k = 0;\n            l = (l << 4) | k;\n            if (--m <= 0) {\n                ret->d[h++] = l;\n                break;\n            }\n        }\n        j -= (BN_BYTES * 2);\n    }\n    ret->top = h;\n    bn_correct_top(ret);\n    ret->neg = neg;\n    *bn = ret;\n    bn_check_top(ret);\n    return (num);\n err:\n    if (*bn == NULL)\n        BN_free(ret);\n    return (0);\n}", 'int BN_add_word(BIGNUM *a, BN_ULONG w)\n{\n    BN_ULONG l;\n    int i;\n    bn_check_top(a);\n    w &= BN_MASK2;\n    if (!w)\n        return 1;\n    if (BN_is_zero(a))\n        return BN_set_word(a, w);\n    if (a->neg) {\n        a->neg = 0;\n        i = BN_sub_word(a, w);\n        if (!BN_is_zero(a))\n            a->neg = !(a->neg);\n        return (i);\n    }\n    for (i = 0; w != 0 && i < a->top; i++) {\n        a->d[i] = l = (a->d[i] + w) & BN_MASK2;\n        w = (w > l) ? 1 : 0;\n    }\n    if (w && i == a->top) {\n        if (bn_wexpand(a, a->top + 1) == NULL)\n            return 0;\n        a->top++;\n        a->d[i] = w;\n    }\n    bn_check_top(a);\n    return (1);\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n    bn_check_top(a);\n    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n        return (0);\n    a->neg = 0;\n    a->d[0] = w;\n    a->top = (w ? 1 : 0);\n    bn_check_top(a);\n    return (1);\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}']