int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
{
    ASN1_TIME t;
    struct tm tm;
    int rv = 0;
    t.length = strlen(str);
    t.data = (unsigned char *)str;
    t.flags = ASN1_STRING_FLAG_X509_TIME;
    t.type = V_ASN1_UTCTIME;
    if (!ASN1_TIME_check(&t)) {
        t.type = V_ASN1_GENERALIZEDTIME;
        if (!ASN1_TIME_check(&t))
            goto out;
    }
    if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) {
        if (!asn1_generalizedtime_to_tm(&tm, &t))
            goto out;
        if (tm.tm_year >= 50 && tm.tm_year < 150) {
            t.length -= 2;
            t.data = OPENSSL_zalloc(t.length + 1);
            if (t.data == NULL)
                goto out;
            memcpy(t.data, str + 2, t.length);
            t.type = V_ASN1_UTCTIME;
        }
    }
    if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
        rv = 1;
    if (t.data != (unsigned char *)str)
        OPENSSL_free(t.data);
out:
    return rv;
}
['int ca_main(int argc, char **argv)\n{\n    CONF *conf = NULL;\n    ENGINE *e = NULL;\n    BIGNUM *crlnumber = NULL, *serial = NULL;\n    EVP_PKEY *pkey = NULL;\n    BIO *in = NULL, *out = NULL, *Sout = NULL;\n    ASN1_INTEGER *tmpser;\n    ASN1_TIME *tmptm;\n    CA_DB *db = NULL;\n    DB_ATTR db_attr;\n    STACK_OF(CONF_VALUE) *attribs = NULL;\n    STACK_OF(OPENSSL_STRING) *sigopts = NULL;\n    STACK_OF(X509) *cert_sk = NULL;\n    X509_CRL *crl = NULL;\n    const EVP_MD *dgst = NULL;\n    char *configfile = default_config_file, *section = NULL;\n    char *md = NULL, *policy = NULL, *keyfile = NULL;\n    char *certfile = NULL, *crl_ext = NULL, *crlnumberfile = NULL, *key = NULL;\n    const char *infile = NULL, *spkac_file = NULL, *ss_cert_file = NULL;\n    const char *extensions = NULL, *extfile = NULL, *passinarg = NULL;\n    char *outdir = NULL, *outfile = NULL, *rev_arg = NULL, *ser_status = NULL;\n    const char *serialfile = NULL, *subj = NULL;\n    char *prog, *startdate = NULL, *enddate = NULL;\n    char *dbfile = NULL, *f, *randfile = NULL;\n    char new_cert[CERT_MAX + 1];\n    char tmp[10 + 1] = "\\0";\n    char *const *pp;\n    const char *p;\n    int create_ser = 0, free_key = 0, total = 0, total_done = 0;\n    int batch = 0, default_op = 1, doupdatedb = 0, ext_copy = EXT_COPY_NONE;\n    int keyformat = FORMAT_PEM, multirdn = 0, notext = 0, output_der = 0;\n    int ret = 1, email_dn = 1, req = 0, verbose = 0, gencrl = 0, dorevoke = 0;\n    int i, j, selfsign = 0;\n    long crldays = 0, crlhours = 0, crlsec = 0, days = 0;\n    unsigned long chtype = MBSTRING_ASC, certopt = 0;\n    X509 *x509 = NULL, *x509p = NULL, *x = NULL;\n    REVINFO_TYPE rev_type = REV_NONE;\n    X509_REVOKED *r = NULL;\n    OPTION_CHOICE o;\n    new_cert[CERT_MAX] = \'\\0\';\n    prog = opt_init(argc, argv, ca_options);\n    while ((o = opt_next()) != OPT_EOF) {\n        switch (o) {\n        case OPT_EOF:\n        case OPT_ERR:\nopthelp:\n            BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n            goto end;\n        case OPT_HELP:\n            opt_help(ca_options);\n            ret = 0;\n            goto end;\n        case OPT_IN:\n            req = 1;\n            infile = opt_arg();\n            break;\n        case OPT_OUT:\n            outfile = opt_arg();\n            break;\n        case OPT_VERBOSE:\n            verbose = 1;\n            break;\n        case OPT_CONFIG:\n            configfile = opt_arg();\n            break;\n        case OPT_NAME:\n            section = opt_arg();\n            break;\n        case OPT_SUBJ:\n            subj = opt_arg();\n            break;\n        case OPT_UTF8:\n            chtype = MBSTRING_UTF8;\n            break;\n        case OPT_CREATE_SERIAL:\n            create_ser = 1;\n            break;\n        case OPT_MULTIVALUE_RDN:\n            multirdn = 1;\n            break;\n        case OPT_STARTDATE:\n            startdate = opt_arg();\n            break;\n        case OPT_ENDDATE:\n            enddate = opt_arg();\n            break;\n        case OPT_DAYS:\n            days = atoi(opt_arg());\n            break;\n        case OPT_MD:\n            md = opt_arg();\n            break;\n        case OPT_POLICY:\n            policy = opt_arg();\n            break;\n        case OPT_KEYFILE:\n            keyfile = opt_arg();\n            break;\n        case OPT_KEYFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))\n                goto opthelp;\n            break;\n        case OPT_PASSIN:\n            passinarg = opt_arg();\n            break;\n        case OPT_KEY:\n            key = opt_arg();\n            break;\n        case OPT_CERT:\n            certfile = opt_arg();\n            break;\n        case OPT_SELFSIGN:\n            selfsign = 1;\n            break;\n        case OPT_OUTDIR:\n            outdir = opt_arg();\n            break;\n        case OPT_SIGOPT:\n            if (sigopts == NULL)\n                sigopts = sk_OPENSSL_STRING_new_null();\n            if (sigopts == NULL\n                || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))\n                goto end;\n            break;\n        case OPT_NOTEXT:\n            notext = 1;\n            break;\n        case OPT_BATCH:\n            batch = 1;\n            break;\n        case OPT_PRESERVEDN:\n            preserve = 1;\n            break;\n        case OPT_NOEMAILDN:\n            email_dn = 0;\n            break;\n        case OPT_GENCRL:\n            gencrl = 1;\n            break;\n        case OPT_MSIE_HACK:\n            msie_hack = 1;\n            break;\n        case OPT_CRLDAYS:\n            crldays = atol(opt_arg());\n            break;\n        case OPT_CRLHOURS:\n            crlhours = atol(opt_arg());\n            break;\n        case OPT_CRLSEC:\n            crlsec = atol(opt_arg());\n            break;\n        case OPT_INFILES:\n            req = 1;\n            goto end_of_options;\n        case OPT_SS_CERT:\n            ss_cert_file = opt_arg();\n            req = 1;\n            break;\n        case OPT_SPKAC:\n            spkac_file = opt_arg();\n            req = 1;\n            break;\n        case OPT_REVOKE:\n            infile = opt_arg();\n            dorevoke = 1;\n            break;\n        case OPT_VALID:\n            infile = opt_arg();\n            dorevoke = 2;\n            break;\n        case OPT_EXTENSIONS:\n            extensions = opt_arg();\n            break;\n        case OPT_EXTFILE:\n            extfile = opt_arg();\n            break;\n        case OPT_STATUS:\n            ser_status = opt_arg();\n            break;\n        case OPT_UPDATEDB:\n            doupdatedb = 1;\n            break;\n        case OPT_CRLEXTS:\n            crl_ext = opt_arg();\n            break;\n        case OPT_CRL_REASON:\n        case OPT_CRL_HOLD:\n        case OPT_CRL_COMPROMISE:\n        case OPT_CRL_CA_COMPROMISE:\n            rev_arg = opt_arg();\n            rev_type = (o - OPT_CRL_REASON) + REV_CRL_REASON;\n            break;\n        case OPT_ENGINE:\n            e = setup_engine(opt_arg(), 0);\n            break;\n        }\n    }\nend_of_options:\n    argc = opt_num_rest();\n    argv = opt_rest();\n    BIO_printf(bio_err, "Using configuration from %s\\n", configfile);\n    if ((conf = app_load_config(configfile)) == NULL)\n        goto end;\n    if (configfile != default_config_file && !app_load_modules(conf))\n        goto end;\n    if (section == NULL\n        && (section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_CA)) == NULL)\n        goto end;\n    if (conf != NULL) {\n        p = NCONF_get_string(conf, NULL, "oid_file");\n        if (p == NULL)\n            ERR_clear_error();\n        if (p != NULL) {\n            BIO *oid_bio;\n            oid_bio = BIO_new_file(p, "r");\n            if (oid_bio == NULL) {\n                ERR_clear_error();\n            } else {\n                OBJ_create_objects(oid_bio);\n                BIO_free(oid_bio);\n            }\n        }\n        if (!add_oid_section(conf)) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");\n    if (randfile == NULL)\n        ERR_clear_error();\n    app_RAND_load_file(randfile, 0);\n    f = NCONF_get_string(conf, section, STRING_MASK);\n    if (f == NULL)\n        ERR_clear_error();\n    if (f != NULL && !ASN1_STRING_set_default_mask_asc(f)) {\n        BIO_printf(bio_err, "Invalid global string mask setting %s\\n", f);\n        goto end;\n    }\n    if (chtype != MBSTRING_UTF8) {\n        f = NCONF_get_string(conf, section, UTF8_IN);\n        if (f == NULL)\n            ERR_clear_error();\n        else if (strcmp(f, "yes") == 0)\n            chtype = MBSTRING_UTF8;\n    }\n    db_attr.unique_subject = 1;\n    p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT);\n    if (p != NULL)\n        db_attr.unique_subject = parse_yesno(p, 1);\n    else\n        ERR_clear_error();\n    if (ser_status) {\n        dbfile = lookup_conf(conf, section, ENV_DATABASE);\n        if (dbfile == NULL)\n            goto end;\n        db = load_index(dbfile, &db_attr);\n        if (db == NULL)\n            goto end;\n        if (!index_index(db))\n            goto end;\n        if (get_certificate_status(ser_status, db) != 1)\n            BIO_printf(bio_err, "Error verifying serial %s!\\n", ser_status);\n        goto end;\n    }\n    if (keyfile == NULL\n        && (keyfile = lookup_conf(conf, section, ENV_PRIVATE_KEY)) == NULL)\n        goto end;\n    if (key == NULL) {\n        free_key = 1;\n        if (!app_passwd(passinarg, NULL, &key, NULL)) {\n            BIO_printf(bio_err, "Error getting password\\n");\n            goto end;\n        }\n    }\n    pkey = load_key(keyfile, keyformat, 0, key, e, "CA private key");\n    if (key != NULL)\n        OPENSSL_cleanse(key, strlen(key));\n    if (pkey == NULL)\n        goto end;\n    if (!selfsign || spkac_file || ss_cert_file || gencrl) {\n        if (certfile == NULL\n            && (certfile = lookup_conf(conf, section, ENV_CERTIFICATE)) == NULL)\n            goto end;\n        x509 = load_cert(certfile, FORMAT_PEM, "CA certificate");\n        if (x509 == NULL)\n            goto end;\n        if (!X509_check_private_key(x509, pkey)) {\n            BIO_printf(bio_err,\n                       "CA certificate and CA private key do not match\\n");\n            goto end;\n        }\n    }\n    if (!selfsign)\n        x509p = x509;\n    f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE);\n    if (f == NULL)\n        ERR_clear_error();\n    if ((f != NULL) && ((*f == \'y\') || (*f == \'Y\')))\n        preserve = 1;\n    f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK);\n    if (f == NULL)\n        ERR_clear_error();\n    if ((f != NULL) && ((*f == \'y\') || (*f == \'Y\')))\n        msie_hack = 1;\n    f = NCONF_get_string(conf, section, ENV_NAMEOPT);\n    if (f != NULL) {\n        if (!set_nameopt(f)) {\n            BIO_printf(bio_err, "Invalid name options: \\"%s\\"\\n", f);\n            goto end;\n        }\n        default_op = 0;\n    }\n    f = NCONF_get_string(conf, section, ENV_CERTOPT);\n    if (f != NULL) {\n        if (!set_cert_ex(&certopt, f)) {\n            BIO_printf(bio_err, "Invalid certificate options: \\"%s\\"\\n", f);\n            goto end;\n        }\n        default_op = 0;\n    } else {\n        ERR_clear_error();\n    }\n    f = NCONF_get_string(conf, section, ENV_EXTCOPY);\n    if (f != NULL) {\n        if (!set_ext_copy(&ext_copy, f)) {\n            BIO_printf(bio_err, "Invalid extension copy option: \\"%s\\"\\n", f);\n            goto end;\n        }\n    } else {\n        ERR_clear_error();\n    }\n    if ((outdir == NULL) && (req)) {\n        outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR);\n        if (outdir == NULL) {\n            BIO_printf(bio_err,\n                       "there needs to be defined a directory for new certificate to be placed in\\n");\n            goto end;\n        }\n#ifndef OPENSSL_SYS_VMS\n        if (app_isdir(outdir) <= 0) {\n            BIO_printf(bio_err, "%s: %s is not a directory\\n", prog, outdir);\n            perror(outdir);\n            goto end;\n        }\n#endif\n    }\n    dbfile = lookup_conf(conf, section, ENV_DATABASE);\n    if (dbfile == NULL)\n        goto end;\n    db = load_index(dbfile, &db_attr);\n    if (db == NULL)\n        goto end;\n    for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {\n        pp = sk_OPENSSL_PSTRING_value(db->db->data, i);\n        if ((pp[DB_type][0] != DB_TYPE_REV) && (pp[DB_rev_date][0] != \'\\0\')) {\n            BIO_printf(bio_err,\n                       "entry %d: not revoked yet, but has a revocation date\\n",\n                       i + 1);\n            goto end;\n        }\n        if ((pp[DB_type][0] == DB_TYPE_REV) &&\n            !make_revoked(NULL, pp[DB_rev_date])) {\n            BIO_printf(bio_err, " in entry %d\\n", i + 1);\n            goto end;\n        }\n        if (!check_time_format((char *)pp[DB_exp_date])) {\n            BIO_printf(bio_err, "entry %d: invalid expiry date\\n", i + 1);\n            goto end;\n        }\n        p = pp[DB_serial];\n        j = strlen(p);\n        if (*p == \'-\') {\n            p++;\n            j--;\n        }\n        if ((j & 1) || (j < 2)) {\n            BIO_printf(bio_err, "entry %d: bad serial number length (%d)\\n",\n                       i + 1, j);\n            goto end;\n        }\n        for ( ; *p; p++) {\n            if (!isxdigit(_UC(*p))) {\n                BIO_printf(bio_err,\n                           "entry %d: bad char 0%o \'%c\' in serial number\\n",\n                           i + 1, *p, *p);\n                goto end;\n            }\n        }\n    }\n    if (verbose) {\n        TXT_DB_write(bio_out, db->db);\n        BIO_printf(bio_err, "%d entries loaded from the database\\n",\n                   sk_OPENSSL_PSTRING_num(db->db->data));\n        BIO_printf(bio_err, "generating index\\n");\n    }\n    if (!index_index(db))\n        goto end;\n    if (doupdatedb) {\n        if (verbose)\n            BIO_printf(bio_err, "Updating %s ...\\n", dbfile);\n        i = do_updatedb(db);\n        if (i == -1) {\n            BIO_printf(bio_err, "Malloc failure\\n");\n            goto end;\n        } else if (i == 0) {\n            if (verbose)\n                BIO_printf(bio_err, "No entries found to mark expired\\n");\n        } else {\n            if (!save_index(dbfile, "new", db))\n                goto end;\n            if (!rotate_index(dbfile, "new", "old"))\n                goto end;\n            if (verbose)\n                BIO_printf(bio_err,\n                           "Done. %d entries marked as expired\\n", i);\n        }\n    }\n    if (extfile) {\n        if ((extconf = app_load_config(extfile)) == NULL) {\n            ret = 1;\n            goto end;\n        }\n        if (verbose)\n            BIO_printf(bio_err, "Successfully loaded extensions file %s\\n",\n                       extfile);\n        if (extensions == NULL) {\n            extensions = NCONF_get_string(extconf, "default", "extensions");\n            if (extensions == NULL)\n                extensions = "default";\n        }\n    }\n    if (req || gencrl) {\n        Sout = bio_open_default(outfile, \'w\', FORMAT_TEXT);\n        if (Sout == NULL)\n            goto end;\n    }\n    if (md == NULL\n        && (md = lookup_conf(conf, section, ENV_DEFAULT_MD)) == NULL)\n        goto end;\n    if (strcmp(md, "default") == 0) {\n        int def_nid;\n        if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) {\n            BIO_puts(bio_err, "no default digest\\n");\n            goto end;\n        }\n        md = (char *)OBJ_nid2sn(def_nid);\n    }\n    if (!opt_md(md, &dgst)) {\n        goto end;\n    }\n    if (req) {\n        if (email_dn == 1) {\n            char *tmp_email_dn = NULL;\n            tmp_email_dn = NCONF_get_string(conf, section, ENV_DEFAULT_EMAIL_DN);\n            if (tmp_email_dn != NULL && strcmp(tmp_email_dn, "no") == 0)\n                email_dn = 0;\n        }\n        if (verbose)\n            BIO_printf(bio_err, "message digest is %s\\n",\n                       OBJ_nid2ln(EVP_MD_type(dgst)));\n        if (policy == NULL\n            && (policy = lookup_conf(conf, section, ENV_POLICY)) == NULL)\n            goto end;\n        if (verbose)\n            BIO_printf(bio_err, "policy is %s\\n", policy);\n        serialfile = lookup_conf(conf, section, ENV_SERIAL);\n        if (serialfile == NULL)\n            goto end;\n        if (extconf == NULL) {\n            if (extensions == NULL) {\n                extensions = NCONF_get_string(conf, section, ENV_EXTENSIONS);\n                if (extensions == NULL)\n                    ERR_clear_error();\n            }\n            if (extensions != NULL) {\n                X509V3_CTX ctx;\n                X509V3_set_ctx_test(&ctx);\n                X509V3_set_nconf(&ctx, conf);\n                if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) {\n                    BIO_printf(bio_err,\n                               "Error Loading extension section %s\\n",\n                               extensions);\n                    ret = 1;\n                    goto end;\n                }\n            }\n        }\n        if (startdate == NULL) {\n            startdate = NCONF_get_string(conf, section,\n                                         ENV_DEFAULT_STARTDATE);\n            if (startdate == NULL)\n                ERR_clear_error();\n        }\n        if (startdate != NULL && !ASN1_TIME_set_string_X509(NULL, startdate)) {\n            BIO_printf(bio_err,\n                       "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\\n");\n            goto end;\n        }\n        if (startdate == NULL)\n            startdate = "today";\n        if (enddate == NULL) {\n            enddate = NCONF_get_string(conf, section, ENV_DEFAULT_ENDDATE);\n            if (enddate == NULL)\n                ERR_clear_error();\n        }\n        if (enddate != NULL && !ASN1_TIME_set_string_X509(NULL, enddate)) {\n            BIO_printf(bio_err,\n                       "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\\n");\n            goto end;\n        }\n        if (days == 0) {\n            if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days))\n                days = 0;\n        }\n        if (enddate == NULL && (days == 0)) {\n            BIO_printf(bio_err,\n                       "cannot lookup how many days to certify for\\n");\n            goto end;\n        }\n        if ((serial = load_serial(serialfile, create_ser, NULL)) == NULL) {\n            BIO_printf(bio_err, "error while loading serial number\\n");\n            goto end;\n        }\n        if (verbose) {\n            if (BN_is_zero(serial)) {\n                BIO_printf(bio_err, "next serial number is 00\\n");\n            } else {\n                if ((f = BN_bn2hex(serial)) == NULL)\n                    goto end;\n                BIO_printf(bio_err, "next serial number is %s\\n", f);\n                OPENSSL_free(f);\n            }\n        }\n        if ((attribs = NCONF_get_section(conf, policy)) == NULL) {\n            BIO_printf(bio_err, "unable to find \'section\' for %s\\n", policy);\n            goto end;\n        }\n        if ((cert_sk = sk_X509_new_null()) == NULL) {\n            BIO_printf(bio_err, "Memory allocation failure\\n");\n            goto end;\n        }\n        if (spkac_file != NULL) {\n            total++;\n            j = certify_spkac(&x, spkac_file, pkey, x509, dgst, sigopts,\n                              attribs, db, serial, subj, chtype, multirdn,\n                              email_dn, startdate, enddate, days, extensions,\n                              conf, verbose, certopt, get_nameopt(), default_op,\n                              ext_copy);\n            if (j < 0)\n                goto end;\n            if (j > 0) {\n                total_done++;\n                BIO_printf(bio_err, "\\n");\n                if (!BN_add_word(serial, 1))\n                    goto end;\n                if (!sk_X509_push(cert_sk, x)) {\n                    BIO_printf(bio_err, "Memory allocation failure\\n");\n                    goto end;\n                }\n                if (outfile) {\n                    output_der = 1;\n                    batch = 1;\n                }\n            }\n        }\n        if (ss_cert_file != NULL) {\n            total++;\n            j = certify_cert(&x, ss_cert_file, pkey, x509, dgst, sigopts,\n                             attribs,\n                             db, serial, subj, chtype, multirdn, email_dn,\n                             startdate, enddate, days, batch, extensions,\n                             conf, verbose, certopt, get_nameopt(), default_op,\n                             ext_copy);\n            if (j < 0)\n                goto end;\n            if (j > 0) {\n                total_done++;\n                BIO_printf(bio_err, "\\n");\n                if (!BN_add_word(serial, 1))\n                    goto end;\n                if (!sk_X509_push(cert_sk, x)) {\n                    BIO_printf(bio_err, "Memory allocation failure\\n");\n                    goto end;\n                }\n            }\n        }\n        if (infile != NULL) {\n            total++;\n            j = certify(&x, infile, pkey, x509p, dgst, sigopts, attribs, db,\n                        serial, subj, chtype, multirdn, email_dn, startdate,\n                        enddate, days, batch, extensions, conf, verbose,\n                        certopt, get_nameopt(), default_op, ext_copy, selfsign);\n            if (j < 0)\n                goto end;\n            if (j > 0) {\n                total_done++;\n                BIO_printf(bio_err, "\\n");\n                if (!BN_add_word(serial, 1))\n                    goto end;\n                if (!sk_X509_push(cert_sk, x)) {\n                    BIO_printf(bio_err, "Memory allocation failure\\n");\n                    goto end;\n                }\n            }\n        }\n        for (i = 0; i < argc; i++) {\n            total++;\n            j = certify(&x, argv[i], pkey, x509p, dgst, sigopts, attribs, db,\n                        serial, subj, chtype, multirdn, email_dn, startdate,\n                        enddate, days, batch, extensions, conf, verbose,\n                        certopt, get_nameopt(), default_op, ext_copy, selfsign);\n            if (j < 0)\n                goto end;\n            if (j > 0) {\n                total_done++;\n                BIO_printf(bio_err, "\\n");\n                if (!BN_add_word(serial, 1))\n                    goto end;\n                if (!sk_X509_push(cert_sk, x)) {\n                    BIO_printf(bio_err, "Memory allocation failure\\n");\n                    goto end;\n                }\n            }\n        }\n        if (sk_X509_num(cert_sk) > 0) {\n            if (!batch) {\n                BIO_printf(bio_err,\n                           "\\n%d out of %d certificate requests certified, commit? [y/n]",\n                           total_done, total);\n                (void)BIO_flush(bio_err);\n                tmp[0] = \'\\0\';\n                if (fgets(tmp, sizeof(tmp), stdin) == NULL) {\n                    BIO_printf(bio_err,\n                               "CERTIFICATION CANCELED: I/O error\\n");\n                    ret = 0;\n                    goto end;\n                }\n                if (tmp[0] != \'y\' && tmp[0] != \'Y\') {\n                    BIO_printf(bio_err, "CERTIFICATION CANCELED\\n");\n                    ret = 0;\n                    goto end;\n                }\n            }\n            BIO_printf(bio_err, "Write out database with %d new entries\\n",\n                       sk_X509_num(cert_sk));\n            if (!save_serial(serialfile, "new", serial, NULL))\n                goto end;\n            if (!save_index(dbfile, "new", db))\n                goto end;\n        }\n        if (verbose)\n            BIO_printf(bio_err, "writing new certificates\\n");\n        for (i = 0; i < sk_X509_num(cert_sk); i++) {\n            BIO *Cout = NULL;\n            X509 *xi = sk_X509_value(cert_sk, i);\n            ASN1_INTEGER *serialNumber = X509_get_serialNumber(xi);\n            int k;\n            char *n;\n            j = ASN1_STRING_length(serialNumber);\n            p = (const char *)ASN1_STRING_get0_data(serialNumber);\n            if (strlen(outdir) >= (size_t)(j ? CERT_MAX - j * 2 - 6 : CERT_MAX - 8)) {\n                BIO_printf(bio_err, "certificate file name too long\\n");\n                goto end;\n            }\n            strcpy(new_cert, outdir);\n#ifndef OPENSSL_SYS_VMS\n            OPENSSL_strlcat(new_cert, "/", sizeof(new_cert));\n#endif\n            n = (char *)&(new_cert[strlen(new_cert)]);\n            if (j > 0) {\n                for (k = 0; k < j; k++) {\n                    if (n >= &(new_cert[sizeof(new_cert)]))\n                        break;\n                    BIO_snprintf(n,\n                                 &new_cert[0] + sizeof(new_cert) - n,\n                                 "%02X", (unsigned char)*(p++));\n                    n += 2;\n                }\n            } else {\n                *(n++) = \'0\';\n                *(n++) = \'0\';\n            }\n            *(n++) = \'.\';\n            *(n++) = \'p\';\n            *(n++) = \'e\';\n            *(n++) = \'m\';\n            *n = \'\\0\';\n            if (verbose)\n                BIO_printf(bio_err, "writing %s\\n", new_cert);\n            Cout = BIO_new_file(new_cert, "w");\n            if (Cout == NULL) {\n                perror(new_cert);\n                goto end;\n            }\n            write_new_certificate(Cout, xi, 0, notext);\n            write_new_certificate(Sout, xi, output_der, notext);\n            BIO_free_all(Cout);\n        }\n        if (sk_X509_num(cert_sk)) {\n            if (!rotate_serial(serialfile, "new", "old"))\n                goto end;\n            if (!rotate_index(dbfile, "new", "old"))\n                goto end;\n            BIO_printf(bio_err, "Data Base Updated\\n");\n        }\n    }\n    if (gencrl) {\n        int crl_v2 = 0;\n        if (crl_ext == NULL) {\n            crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT);\n            if (crl_ext == NULL)\n                ERR_clear_error();\n        }\n        if (crl_ext != NULL) {\n            X509V3_CTX ctx;\n            X509V3_set_ctx_test(&ctx);\n            X509V3_set_nconf(&ctx, conf);\n            if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) {\n                BIO_printf(bio_err,\n                           "Error Loading CRL extension section %s\\n",\n                           crl_ext);\n                ret = 1;\n                goto end;\n            }\n        }\n        if ((crlnumberfile = NCONF_get_string(conf, section, ENV_CRLNUMBER))\n            != NULL)\n            if ((crlnumber = load_serial(crlnumberfile, 0, NULL)) == NULL) {\n                BIO_printf(bio_err, "error while loading CRL number\\n");\n                goto end;\n            }\n        if (!crldays && !crlhours && !crlsec) {\n            if (!NCONF_get_number(conf, section,\n                                  ENV_DEFAULT_CRL_DAYS, &crldays))\n                crldays = 0;\n            if (!NCONF_get_number(conf, section,\n                                  ENV_DEFAULT_CRL_HOURS, &crlhours))\n                crlhours = 0;\n            ERR_clear_error();\n        }\n        if ((crldays == 0) && (crlhours == 0) && (crlsec == 0)) {\n            BIO_printf(bio_err,\n                       "cannot lookup how long until the next CRL is issued\\n");\n            goto end;\n        }\n        if (verbose)\n            BIO_printf(bio_err, "making CRL\\n");\n        if ((crl = X509_CRL_new()) == NULL)\n            goto end;\n        if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))\n            goto end;\n        tmptm = ASN1_TIME_new();\n        if (tmptm == NULL)\n            goto end;\n        X509_gmtime_adj(tmptm, 0);\n        X509_CRL_set1_lastUpdate(crl, tmptm);\n        if (!X509_time_adj_ex(tmptm, crldays, crlhours * 60 * 60 + crlsec,\n                              NULL)) {\n            BIO_puts(bio_err, "error setting CRL nextUpdate\\n");\n            goto end;\n        }\n        X509_CRL_set1_nextUpdate(crl, tmptm);\n        ASN1_TIME_free(tmptm);\n        for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {\n            pp = sk_OPENSSL_PSTRING_value(db->db->data, i);\n            if (pp[DB_type][0] == DB_TYPE_REV) {\n                if ((r = X509_REVOKED_new()) == NULL)\n                    goto end;\n                j = make_revoked(r, pp[DB_rev_date]);\n                if (!j)\n                    goto end;\n                if (j == 2)\n                    crl_v2 = 1;\n                if (!BN_hex2bn(&serial, pp[DB_serial]))\n                    goto end;\n                tmpser = BN_to_ASN1_INTEGER(serial, NULL);\n                BN_free(serial);\n                serial = NULL;\n                if (!tmpser)\n                    goto end;\n                X509_REVOKED_set_serialNumber(r, tmpser);\n                ASN1_INTEGER_free(tmpser);\n                X509_CRL_add0_revoked(crl, r);\n            }\n        }\n        X509_CRL_sort(crl);\n        if (verbose)\n            BIO_printf(bio_err, "signing CRL\\n");\n        if (crl_ext != NULL || crlnumberfile != NULL) {\n            X509V3_CTX crlctx;\n            X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);\n            X509V3_set_nconf(&crlctx, conf);\n            if (crl_ext != NULL)\n                if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl))\n                    goto end;\n            if (crlnumberfile != NULL) {\n                tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);\n                if (!tmpser)\n                    goto end;\n                X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0);\n                ASN1_INTEGER_free(tmpser);\n                crl_v2 = 1;\n                if (!BN_add_word(crlnumber, 1))\n                    goto end;\n            }\n        }\n        if (crl_ext != NULL || crl_v2) {\n            if (!X509_CRL_set_version(crl, 1))\n                goto end;\n        }\n        if (crlnumberfile != NULL)\n            if (!save_serial(crlnumberfile, "new", crlnumber, NULL))\n                goto end;\n        BN_free(crlnumber);\n        crlnumber = NULL;\n        if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts))\n            goto end;\n        PEM_write_bio_X509_CRL(Sout, crl);\n        if (crlnumberfile != NULL)\n            if (!rotate_serial(crlnumberfile, "new", "old"))\n                goto end;\n    }\n    if (dorevoke) {\n        if (infile == NULL) {\n            BIO_printf(bio_err, "no input files\\n");\n            goto end;\n        } else {\n            X509 *revcert;\n            revcert = load_cert(infile, FORMAT_PEM, infile);\n            if (revcert == NULL)\n                goto end;\n            if (dorevoke == 2)\n                rev_type = REV_VALID;\n            j = do_revoke(revcert, db, rev_type, rev_arg);\n            if (j <= 0)\n                goto end;\n            X509_free(revcert);\n            if (!save_index(dbfile, "new", db))\n                goto end;\n            if (!rotate_index(dbfile, "new", "old"))\n                goto end;\n            BIO_printf(bio_err, "Data Base Updated\\n");\n        }\n    }\n    ret = 0;\n end:\n    BIO_free_all(Sout);\n    BIO_free_all(out);\n    BIO_free_all(in);\n    sk_X509_pop_free(cert_sk, X509_free);\n    if (ret)\n        ERR_print_errors(bio_err);\n    app_RAND_write_file(randfile);\n    if (free_key)\n        OPENSSL_free(key);\n    BN_free(serial);\n    BN_free(crlnumber);\n    free_index(db);\n    sk_OPENSSL_STRING_free(sigopts);\n    EVP_PKEY_free(pkey);\n    X509_free(x509);\n    X509_CRL_free(crl);\n    NCONF_free(conf);\n    NCONF_free(extconf);\n    release_engine(e);\n    return (ret);\n}', 'static char *lookup_conf(const CONF *conf, const char *section, const char *tag)\n{\n    char *entry = NCONF_get_string(conf, section, tag);\n    if (entry == NULL)\n        BIO_printf(bio_err, "variable lookup failed for %s::%s\\n", section, tag);\n    return entry;\n}', 'int BIO_printf(BIO *bio, const char *format, ...)\n{\n    va_list args;\n    int ret;\n    va_start(args, format);\n    ret = BIO_vprintf(bio, format, args);\n    va_end(args);\n    return (ret);\n}', 'int BIO_vprintf(BIO *bio, const char *format, va_list args)\n{\n    int ret;\n    size_t retlen;\n    char hugebuf[1024 * 2];\n    char *hugebufp = hugebuf;\n    size_t hugebufsize = sizeof(hugebuf);\n    char *dynbuf = NULL;\n    int ignored;\n    dynbuf = NULL;\n    if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,\n                args)) {\n        OPENSSL_free(dynbuf);\n        return -1;\n    }\n    if (dynbuf) {\n        ret = BIO_write(bio, dynbuf, (int)retlen);\n        OPENSSL_free(dynbuf);\n    } else {\n        ret = BIO_write(bio, hugebuf, (int)retlen);\n    }\n    return (ret);\n}', "static int\n_dopr(char **sbuffer,\n      char **buffer,\n      size_t *maxlen,\n      size_t *retlen, int *truncated, const char *format, va_list args)\n{\n    char ch;\n    int64_t value;\n    LDOUBLE fvalue;\n    char *strvalue;\n    int min;\n    int max;\n    int state;\n    int flags;\n    int cflags;\n    size_t currlen;\n    state = DP_S_DEFAULT;\n    flags = currlen = cflags = min = 0;\n    max = -1;\n    ch = *format++;\n    while (state != DP_S_DONE) {\n        if (ch == '\\0' || (buffer == NULL && currlen >= *maxlen))\n            state = DP_S_DONE;\n        switch (state) {\n        case DP_S_DEFAULT:\n            if (ch == '%')\n                state = DP_S_FLAGS;\n            else\n                if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n                    return 0;\n            ch = *format++;\n            break;\n        case DP_S_FLAGS:\n            switch (ch) {\n            case '-':\n                flags |= DP_F_MINUS;\n                ch = *format++;\n                break;\n            case '+':\n                flags |= DP_F_PLUS;\n                ch = *format++;\n                break;\n            case ' ':\n                flags |= DP_F_SPACE;\n                ch = *format++;\n                break;\n            case '#':\n                flags |= DP_F_NUM;\n                ch = *format++;\n                break;\n            case '0':\n                flags |= DP_F_ZERO;\n                ch = *format++;\n                break;\n            default:\n                state = DP_S_MIN;\n                break;\n            }\n            break;\n        case DP_S_MIN:\n            if (isdigit((unsigned char)ch)) {\n                min = 10 * min + char_to_int(ch);\n                ch = *format++;\n            } else if (ch == '*') {\n                min = va_arg(args, int);\n                ch = *format++;\n                state = DP_S_DOT;\n            } else\n                state = DP_S_DOT;\n            break;\n        case DP_S_DOT:\n            if (ch == '.') {\n                state = DP_S_MAX;\n                ch = *format++;\n            } else\n                state = DP_S_MOD;\n            break;\n        case DP_S_MAX:\n            if (isdigit((unsigned char)ch)) {\n                if (max < 0)\n                    max = 0;\n                max = 10 * max + char_to_int(ch);\n                ch = *format++;\n            } else if (ch == '*') {\n                max = va_arg(args, int);\n                ch = *format++;\n                state = DP_S_MOD;\n            } else\n                state = DP_S_MOD;\n            break;\n        case DP_S_MOD:\n            switch (ch) {\n            case 'h':\n                cflags = DP_C_SHORT;\n                ch = *format++;\n                break;\n            case 'l':\n                if (*format == 'l') {\n                    cflags = DP_C_LLONG;\n                    format++;\n                } else\n                    cflags = DP_C_LONG;\n                ch = *format++;\n                break;\n            case 'q':\n            case 'j':\n                cflags = DP_C_LLONG;\n                ch = *format++;\n                break;\n            case 'L':\n                cflags = DP_C_LDOUBLE;\n                ch = *format++;\n                break;\n            case 'z':\n                cflags = DP_C_SIZE;\n                ch = *format++;\n                break;\n            default:\n                break;\n            }\n            state = DP_S_CONV;\n            break;\n        case DP_S_CONV:\n            switch (ch) {\n            case 'd':\n            case 'i':\n                switch (cflags) {\n                case DP_C_SHORT:\n                    value = (short int)va_arg(args, int);\n                    break;\n                case DP_C_LONG:\n                    value = va_arg(args, long int);\n                    break;\n                case DP_C_LLONG:\n                    value = va_arg(args, int64_t);\n                    break;\n                case DP_C_SIZE:\n                    value = va_arg(args, ossl_ssize_t);\n                    break;\n                default:\n                    value = va_arg(args, int);\n                    break;\n                }\n                if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,\n                            max, flags))\n                    return 0;\n                break;\n            case 'X':\n                flags |= DP_F_UP;\n            case 'x':\n            case 'o':\n            case 'u':\n                flags |= DP_F_UNSIGNED;\n                switch (cflags) {\n                case DP_C_SHORT:\n                    value = (unsigned short int)va_arg(args, unsigned int);\n                    break;\n                case DP_C_LONG:\n                    value = va_arg(args, unsigned long int);\n                    break;\n                case DP_C_LLONG:\n                    value = va_arg(args, uint64_t);\n                    break;\n                case DP_C_SIZE:\n                    value = va_arg(args, size_t);\n                    break;\n                default:\n                    value = va_arg(args, unsigned int);\n                    break;\n                }\n                if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,\n                            ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),\n                            min, max, flags))\n                    return 0;\n                break;\n            case 'f':\n                if (cflags == DP_C_LDOUBLE)\n                    fvalue = va_arg(args, LDOUBLE);\n                else\n                    fvalue = va_arg(args, double);\n                if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n                           flags, F_FORMAT))\n                    return 0;\n                break;\n            case 'E':\n                flags |= DP_F_UP;\n            case 'e':\n                if (cflags == DP_C_LDOUBLE)\n                    fvalue = va_arg(args, LDOUBLE);\n                else\n                    fvalue = va_arg(args, double);\n                if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n                           flags, E_FORMAT))\n                    return 0;\n                break;\n            case 'G':\n                flags |= DP_F_UP;\n            case 'g':\n                if (cflags == DP_C_LDOUBLE)\n                    fvalue = va_arg(args, LDOUBLE);\n                else\n                    fvalue = va_arg(args, double);\n                if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n                           flags, G_FORMAT))\n                    return 0;\n                break;\n            case 'c':\n                if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,\n                            va_arg(args, int)))\n                    return 0;\n                break;\n            case 's':\n                strvalue = va_arg(args, char *);\n                if (max < 0) {\n                    if (buffer)\n                        max = INT_MAX;\n                    else\n                        max = *maxlen;\n                }\n                if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,\n                            flags, min, max))\n                    return 0;\n                break;\n            case 'p':\n                value = (size_t)va_arg(args, void *);\n                if (!fmtint(sbuffer, buffer, &currlen, maxlen,\n                            value, 16, min, max, flags | DP_F_NUM))\n                    return 0;\n                break;\n            case 'n':\n                {\n                    int *num;\n                    num = va_arg(args, int *);\n                    *num = currlen;\n                }\n                break;\n            case '%':\n                if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n                    return 0;\n                break;\n            case 'w':\n                ch = *format++;\n                break;\n            default:\n                break;\n            }\n            ch = *format++;\n            state = DP_S_DEFAULT;\n            flags = cflags = min = 0;\n            max = -1;\n            break;\n        case DP_S_DONE:\n            break;\n        default:\n            break;\n        }\n    }\n    if (buffer == NULL) {\n        *truncated = (currlen > *maxlen - 1);\n        if (*truncated)\n            currlen = *maxlen - 1;\n    }\n    if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\\0'))\n        return 0;\n    *retlen = currlen - 1;\n    return 1;\n}", 'static int\ndoapr_outch(char **sbuffer,\n            char **buffer, size_t *currlen, size_t *maxlen, int c)\n{\n    OPENSSL_assert(*sbuffer != NULL || buffer != NULL);\n    OPENSSL_assert(*currlen <= *maxlen);\n    if (buffer && *currlen == *maxlen) {\n        if (*maxlen > INT_MAX - BUFFER_INC)\n            return 0;\n        *maxlen += BUFFER_INC;\n        if (*buffer == NULL) {\n            *buffer = OPENSSL_malloc(*maxlen);\n            if (*buffer == NULL)\n                return 0;\n            if (*currlen > 0) {\n                OPENSSL_assert(*sbuffer != NULL);\n                memcpy(*buffer, *sbuffer, *currlen);\n            }\n            *sbuffer = NULL;\n        } else {\n            char *tmpbuf;\n            tmpbuf = OPENSSL_realloc(*buffer, *maxlen);\n            if (tmpbuf == NULL)\n                return 0;\n            *buffer = tmpbuf;\n        }\n    }\n    if (*currlen < *maxlen) {\n        if (*sbuffer)\n            (*sbuffer)[(*currlen)++] = (char)c;\n        else\n            (*buffer)[(*currlen)++] = (char)c;\n    }\n    return 1;\n}', 'int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)\n{\n    ASN1_TIME t;\n    struct tm tm;\n    int rv = 0;\n    t.length = strlen(str);\n    t.data = (unsigned char *)str;\n    t.flags = ASN1_STRING_FLAG_X509_TIME;\n    t.type = V_ASN1_UTCTIME;\n    if (!ASN1_TIME_check(&t)) {\n        t.type = V_ASN1_GENERALIZEDTIME;\n        if (!ASN1_TIME_check(&t))\n            goto out;\n    }\n    if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) {\n        if (!asn1_generalizedtime_to_tm(&tm, &t))\n            goto out;\n        if (tm.tm_year >= 50 && tm.tm_year < 150) {\n            t.length -= 2;\n            t.data = OPENSSL_zalloc(t.length + 1);\n            if (t.data == NULL)\n                goto out;\n            memcpy(t.data, str + 2, t.length);\n            t.type = V_ASN1_UTCTIME;\n        }\n    }\n    if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))\n        rv = 1;\n    if (t.data != (unsigned char *)str)\n        OPENSSL_free(t.data);\nout:\n    return rv;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n    void *ret = CRYPTO_malloc(num, file, line);\n    FAILTEST();\n    if (ret != NULL)\n        memset(ret, 0, num);\n    return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n        return malloc_impl(num, file, line);\n    if (num == 0)\n        return NULL;\n    FAILTEST();\n    allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    osslargused(file); osslargused(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}']