[BACK]Return to ca.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / openssl

Annotation of src/usr.bin/openssl/ca.c, Revision 1.17

1.17    ! doug        1: /* $OpenBSD: ca.c,v 1.16 2015/09/21 13:31:26 bcook Exp $ */
1.1       jsing       2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
                      3:  * All rights reserved.
                      4:  *
                      5:  * This package is an SSL implementation written
                      6:  * by Eric Young (eay@cryptsoft.com).
                      7:  * The implementation was written so as to conform with Netscapes SSL.
                      8:  *
                      9:  * This library is free for commercial and non-commercial use as long as
                     10:  * the following conditions are aheared to.  The following conditions
                     11:  * apply to all code found in this distribution, be it the RC4, RSA,
                     12:  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
                     13:  * included with this distribution is covered by the same copyright terms
                     14:  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
                     15:  *
                     16:  * Copyright remains Eric Young's, and as such any Copyright notices in
                     17:  * the code are not to be removed.
                     18:  * If this package is used in a product, Eric Young should be given attribution
                     19:  * as the author of the parts of the library used.
                     20:  * This can be in the form of a textual message at program startup or
                     21:  * in documentation (online or textual) provided with the package.
                     22:  *
                     23:  * Redistribution and use in source and binary forms, with or without
                     24:  * modification, are permitted provided that the following conditions
                     25:  * are met:
                     26:  * 1. Redistributions of source code must retain the copyright
                     27:  *    notice, this list of conditions and the following disclaimer.
                     28:  * 2. Redistributions in binary form must reproduce the above copyright
                     29:  *    notice, this list of conditions and the following disclaimer in the
                     30:  *    documentation and/or other materials provided with the distribution.
                     31:  * 3. All advertising materials mentioning features or use of this software
                     32:  *    must display the following acknowledgement:
                     33:  *    "This product includes cryptographic software written by
                     34:  *     Eric Young (eay@cryptsoft.com)"
                     35:  *    The word 'cryptographic' can be left out if the rouines from the library
                     36:  *    being used are not cryptographic related :-).
                     37:  * 4. If you include any Windows specific code (or a derivative thereof) from
                     38:  *    the apps directory (application code) you must include an acknowledgement:
                     39:  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
                     40:  *
                     41:  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
                     42:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     43:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     44:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     45:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     46:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     47:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     48:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     49:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     50:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     51:  * SUCH DAMAGE.
                     52:  *
                     53:  * The licence and distribution terms for any publically available version or
                     54:  * derivative of this code cannot be changed.  i.e. this code cannot simply be
                     55:  * copied and put under another distribution licence
                     56:  * [including the GNU Public Licence.]
                     57:  */
                     58:
                     59: /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */
                     60:
                     61: #include <sys/types.h>
                     62:
                     63: #include <ctype.h>
                     64: #include <stdio.h>
                     65: #include <stdlib.h>
                     66: #include <limits.h>
                     67: #include <string.h>
                     68: #include <unistd.h>
                     69:
                     70: #include "apps.h"
                     71:
                     72: #include <openssl/bio.h>
                     73: #include <openssl/bn.h>
                     74: #include <openssl/conf.h>
                     75: #include <openssl/err.h>
                     76: #include <openssl/evp.h>
                     77: #include <openssl/objects.h>
                     78: #include <openssl/ocsp.h>
                     79: #include <openssl/pem.h>
                     80: #include <openssl/txt_db.h>
                     81: #include <openssl/x509.h>
                     82: #include <openssl/x509v3.h>
                     83:
                     84: #define BASE_SECTION           "ca"
                     85:
                     86: #define ENV_DEFAULT_CA         "default_ca"
                     87:
                     88: #define STRING_MASK            "string_mask"
                     89: #define UTF8_IN                        "utf8"
                     90:
                     91: #define ENV_NEW_CERTS_DIR      "new_certs_dir"
                     92: #define ENV_CERTIFICATE        "certificate"
                     93: #define ENV_SERIAL             "serial"
                     94: #define ENV_CRLNUMBER          "crlnumber"
                     95: #define ENV_PRIVATE_KEY                "private_key"
                     96: #define ENV_DEFAULT_DAYS       "default_days"
                     97: #define ENV_DEFAULT_STARTDATE  "default_startdate"
                     98: #define ENV_DEFAULT_ENDDATE    "default_enddate"
                     99: #define ENV_DEFAULT_CRL_DAYS   "default_crl_days"
                    100: #define ENV_DEFAULT_CRL_HOURS  "default_crl_hours"
                    101: #define ENV_DEFAULT_MD         "default_md"
                    102: #define ENV_DEFAULT_EMAIL_DN   "email_in_dn"
                    103: #define ENV_PRESERVE           "preserve"
                    104: #define ENV_POLICY             "policy"
                    105: #define ENV_EXTENSIONS         "x509_extensions"
                    106: #define ENV_CRLEXT             "crl_extensions"
                    107: #define ENV_MSIE_HACK          "msie_hack"
                    108: #define ENV_NAMEOPT            "name_opt"
                    109: #define ENV_CERTOPT            "cert_opt"
                    110: #define ENV_EXTCOPY            "copy_extensions"
                    111: #define ENV_UNIQUE_SUBJECT     "unique_subject"
                    112:
                    113: #define ENV_DATABASE           "database"
                    114:
                    115: /* Additional revocation information types */
                    116:
                    117: #define REV_NONE               0       /* No addditional information */
                    118: #define REV_CRL_REASON         1       /* Value is CRL reason code */
                    119: #define REV_HOLD               2       /* Value is hold instruction */
                    120: #define REV_KEY_COMPROMISE     3       /* Value is cert key compromise time */
                    121: #define REV_CA_COMPROMISE      4       /* Value is CA key compromise time */
                    122:
                    123: static const char *ca_usage[] = {
                    124:        "usage: ca args\n",
                    125:        "\n",
                    126:        " -verbose        - Talk a lot while doing things\n",
                    127:        " -config file    - A config file\n",
                    128:        " -name arg       - The particular CA definition to use\n",
                    129:        " -gencrl         - Generate a new CRL\n",
                    130:        " -crldays days   - Days is when the next CRL is due\n",
                    131:        " -crlhours hours - Hours is when the next CRL is due\n",
                    132:        " -startdate YYMMDDHHMMSSZ  - certificate validity notBefore\n",
                    133:        " -enddate YYMMDDHHMMSSZ    - certificate validity notAfter (overrides -days)\n",
                    134:        " -days arg       - number of days to certify the certificate for\n",
1.15      bcook     135:        " -md arg         - md to use, one of md5 or sha1\n",
1.1       jsing     136:        " -policy arg     - The CA 'policy' to support\n",
                    137:        " -keyfile arg    - private key file\n",
1.12      bcook     138:        " -keyform arg    - private key file format (PEM)\n",
1.1       jsing     139:        " -key arg        - key to decode the private key if it is encrypted\n",
                    140:        " -cert file      - The CA certificate\n",
                    141:        " -selfsign       - sign a certificate with the key associated with it\n",
                    142:        " -in file        - The input PEM encoded certificate request(s)\n",
                    143:        " -out file       - Where to put the output file(s)\n",
                    144:        " -outdir dir     - Where to put output certificates\n",
                    145:        " -infiles ....   - The last argument, requests to process\n",
                    146:        " -spkac file     - File contains DN and signed public key and challenge\n",
                    147:        " -ss_cert file   - File contains a self signed cert to sign\n",
                    148:        " -preserveDN     - Don't re-order the DN\n",
                    149:        " -noemailDN      - Don't add the EMAIL field into certificate' subject\n",
                    150:        " -batch          - Don't ask questions\n",
                    151:        " -msie_hack      - msie modifications to handle all those universal strings\n",
                    152:        " -revoke file    - Revoke a certificate (given in file)\n",
                    153:        " -subj arg       - Use arg instead of request's subject\n",
                    154:        " -utf8           - input characters are UTF8 (default ASCII)\n",
                    155:        " -multivalue-rdn - enable support for multivalued RDNs\n",
                    156:        " -extensions ..  - Extension section (override value in config file)\n",
                    157:        " -extfile file   - Configuration file with X509v3 extentions to add\n",
                    158:        " -crlexts ..     - CRL extension section (override value in config file)\n",
                    159:        " -status serial  - Shows certificate status given the serial number\n",
                    160:        " -updatedb       - Updates db for expired certificates\n",
                    161:        NULL
                    162: };
                    163:
                    164: static void lookup_fail(const char *name, const char *tag);
                    165: static int certify(X509 ** xret, char *infile, EVP_PKEY * pkey, X509 * x509,
                    166:     const EVP_MD * dgst, STACK_OF(OPENSSL_STRING) * sigopts,
                    167:     STACK_OF(CONF_VALUE) * policy, CA_DB * db, BIGNUM * serial, char *subj,
                    168:     unsigned long chtype, int multirdn, int email_dn, char *startdate,
                    169:     char *enddate, long days, int batch, char *ext_sect, CONF * conf,
                    170:     int verbose, unsigned long certopt, unsigned long nameopt,
                    171:     int default_op, int ext_copy, int selfsign);
                    172: static int certify_cert(X509 ** xret, char *infile, EVP_PKEY * pkey,
                    173:     X509 * x509, const EVP_MD * dgst, STACK_OF(OPENSSL_STRING) * sigopts,
                    174:     STACK_OF(CONF_VALUE) * policy, CA_DB * db, BIGNUM * serial, char *subj,
                    175:     unsigned long chtype, int multirdn, int email_dn, char *startdate,
                    176:     char *enddate, long days, int batch, char *ext_sect, CONF * conf,
                    177:     int verbose, unsigned long certopt, unsigned long nameopt, int default_op,
1.12      bcook     178:     int ext_copy);
1.1       jsing     179: static int certify_spkac(X509 ** xret, char *infile, EVP_PKEY * pkey,
                    180:     X509 * x509, const EVP_MD * dgst, STACK_OF(OPENSSL_STRING) * sigopts,
                    181:     STACK_OF(CONF_VALUE) * policy, CA_DB * db, BIGNUM * serial, char *subj,
                    182:     unsigned long chtype, int multirdn, int email_dn, char *startdate,
                    183:     char *enddate, long days, char *ext_sect, CONF * conf, int verbose,
                    184:     unsigned long certopt, unsigned long nameopt, int default_op, int ext_copy);
                    185: static void write_new_certificate(BIO * bp, X509 * x, int output_der,
                    186:     int notext);
                    187: static int do_body(X509 ** xret, EVP_PKEY * pkey, X509 * x509,
                    188:     const EVP_MD * dgst, STACK_OF(OPENSSL_STRING) * sigopts,
                    189:     STACK_OF(CONF_VALUE) * policy, CA_DB * db, BIGNUM * serial, char *subj,
                    190:     unsigned long chtype, int multirdn, int email_dn, char *startdate,
                    191:     char *enddate, long days, int batch, int verbose, X509_REQ * req,
                    192:     char *ext_sect, CONF * conf, unsigned long certopt, unsigned long nameopt,
                    193:     int default_op, int ext_copy, int selfsign);
                    194: static int do_revoke(X509 * x509, CA_DB * db, int ext, char *extval);
                    195: static int get_certificate_status(const char *ser_status, CA_DB * db);
                    196: static int do_updatedb(CA_DB * db);
                    197: static int check_time_format(const char *str);
                    198: static char * bin2hex(unsigned char *, size_t);
                    199: char *make_revocation_str(int rev_type, char *rev_arg);
                    200: int make_revoked(X509_REVOKED * rev, const char *str);
                    201: int old_entry_print(BIO * bp, ASN1_OBJECT * obj, ASN1_STRING * str);
                    202: static CONF *conf = NULL;
                    203: static CONF *extconf = NULL;
                    204: static char *section = NULL;
                    205:
                    206: static int preserve = 0;
                    207: static int msie_hack = 0;
                    208:
                    209:
                    210: int
                    211: ca_main(int argc, char **argv)
                    212: {
                    213:        char *key = NULL, *passargin = NULL;
                    214:        int create_ser = 0;
                    215:        int free_key = 0;
                    216:        int total = 0;
                    217:        int total_done = 0;
                    218:        int badops = 0;
                    219:        int ret = 1;
                    220:        int email_dn = 1;
                    221:        int req = 0;
                    222:        int verbose = 0;
                    223:        int gencrl = 0;
                    224:        int dorevoke = 0;
                    225:        int doupdatedb = 0;
                    226:        long crldays = 0;
                    227:        long crlhours = 0;
                    228:        long crlsec = 0;
                    229:        long errorline = -1;
                    230:        char *configfile = NULL;
                    231:        char *md = NULL;
                    232:        char *policy = NULL;
                    233:        char *keyfile = NULL;
                    234:        char *certfile = NULL;
                    235:        int keyform = FORMAT_PEM;
                    236:        char *infile = NULL;
                    237:        char *spkac_file = NULL;
                    238:        char *ss_cert_file = NULL;
                    239:        char *ser_status = NULL;
                    240:        EVP_PKEY *pkey = NULL;
                    241:        int output_der = 0;
                    242:        char *outfile = NULL;
                    243:        char *outdir = NULL;
                    244:        char *serialfile = NULL;
                    245:        char *crlnumberfile = NULL;
                    246:        char *extensions = NULL;
                    247:        char *extfile = NULL;
                    248:        char *subj = NULL;
                    249:        unsigned long chtype = MBSTRING_ASC;
                    250:        int multirdn = 0;
                    251:        char *tmp_email_dn = NULL;
                    252:        char *crl_ext = NULL;
                    253:        int rev_type = REV_NONE;
                    254:        char *rev_arg = NULL;
                    255:        BIGNUM *serial = NULL;
                    256:        BIGNUM *crlnumber = NULL;
                    257:        char *startdate = NULL;
                    258:        char *enddate = NULL;
                    259:        long days = 0;
                    260:        int batch = 0;
                    261:        int notext = 0;
                    262:        unsigned long nameopt = 0, certopt = 0;
                    263:        int default_op = 1;
                    264:        int ext_copy = EXT_COPY_NONE;
                    265:        int selfsign = 0;
                    266:        X509 *x509 = NULL, *x509p = NULL;
                    267:        X509 *x = NULL;
                    268:        BIO *in = NULL, *out = NULL, *Sout = NULL, *Cout = NULL;
                    269:        char *dbfile = NULL;
                    270:        CA_DB *db = NULL;
                    271:        X509_CRL *crl = NULL;
                    272:        X509_REVOKED *r = NULL;
                    273:        ASN1_TIME *tmptm;
                    274:        ASN1_INTEGER *tmpser;
                    275:        char *f;
                    276:        const char *p;
                    277:        char *const * pp;
                    278:        int i, j;
                    279:        const EVP_MD *dgst = NULL;
                    280:        STACK_OF(CONF_VALUE) * attribs = NULL;
                    281:        STACK_OF(X509) * cert_sk = NULL;
                    282:        STACK_OF(OPENSSL_STRING) * sigopts = NULL;
1.4       bcook     283: #define BUFLEN 256
                    284:        char buf[3][BUFLEN];
1.1       jsing     285:        char *tofree = NULL;
                    286:        const char *errstr = NULL;
                    287:        DB_ATTR db_attr;
1.17    ! doug      288:
        !           289:        if (single_execution) {
        !           290:                if (pledge("stdio rpath wpath cpath", NULL) == -1)
        !           291:                        perror("pledge");
        !           292:        }
1.1       jsing     293:
                    294:        conf = NULL;
                    295:        key = NULL;
                    296:        section = NULL;
                    297:
                    298:        preserve = 0;
                    299:        msie_hack = 0;
                    300:
                    301:        argc--;
                    302:        argv++;
                    303:        while (argc >= 1) {
                    304:                if (strcmp(*argv, "-verbose") == 0)
                    305:                        verbose = 1;
                    306:                else if (strcmp(*argv, "-config") == 0) {
                    307:                        if (--argc < 1)
                    308:                                goto bad;
                    309:                        configfile = *(++argv);
                    310:                } else if (strcmp(*argv, "-name") == 0) {
                    311:                        if (--argc < 1)
                    312:                                goto bad;
                    313:                        section = *(++argv);
                    314:                } else if (strcmp(*argv, "-subj") == 0) {
                    315:                        if (--argc < 1)
                    316:                                goto bad;
                    317:                        subj = *(++argv);
                    318:                        /* preserve=1; */
                    319:                } else if (strcmp(*argv, "-utf8") == 0)
                    320:                        chtype = MBSTRING_UTF8;
                    321:                else if (strcmp(*argv, "-create_serial") == 0)
                    322:                        create_ser = 1;
                    323:                else if (strcmp(*argv, "-multivalue-rdn") == 0)
                    324:                        multirdn = 1;
                    325:                else if (strcmp(*argv, "-startdate") == 0) {
                    326:                        if (--argc < 1)
                    327:                                goto bad;
                    328:                        startdate = *(++argv);
                    329:                } else if (strcmp(*argv, "-enddate") == 0) {
                    330:                        if (--argc < 1)
                    331:                                goto bad;
                    332:                        enddate = *(++argv);
                    333:                } else if (strcmp(*argv, "-days") == 0) {
                    334:                        if (--argc < 1)
                    335:                                goto bad;
                    336:                        days = strtonum(*(++argv), 0, LONG_MAX, &errstr);
                    337:                        if (errstr)
                    338:                                goto bad;
                    339:                } else if (strcmp(*argv, "-md") == 0) {
                    340:                        if (--argc < 1)
                    341:                                goto bad;
                    342:                        md = *(++argv);
                    343:                } else if (strcmp(*argv, "-policy") == 0) {
                    344:                        if (--argc < 1)
                    345:                                goto bad;
                    346:                        policy = *(++argv);
                    347:                } else if (strcmp(*argv, "-keyfile") == 0) {
                    348:                        if (--argc < 1)
                    349:                                goto bad;
                    350:                        keyfile = *(++argv);
                    351:                } else if (strcmp(*argv, "-keyform") == 0) {
                    352:                        if (--argc < 1)
                    353:                                goto bad;
                    354:                        keyform = str2fmt(*(++argv));
                    355:                } else if (strcmp(*argv, "-passin") == 0) {
                    356:                        if (--argc < 1)
                    357:                                goto bad;
                    358:                        passargin = *(++argv);
                    359:                } else if (strcmp(*argv, "-key") == 0) {
                    360:                        if (--argc < 1)
                    361:                                goto bad;
                    362:                        key = *(++argv);
                    363:                } else if (strcmp(*argv, "-cert") == 0) {
                    364:                        if (--argc < 1)
                    365:                                goto bad;
                    366:                        certfile = *(++argv);
                    367:                } else if (strcmp(*argv, "-selfsign") == 0)
                    368:                        selfsign = 1;
                    369:                else if (strcmp(*argv, "-in") == 0) {
                    370:                        if (--argc < 1)
                    371:                                goto bad;
                    372:                        infile = *(++argv);
                    373:                        req = 1;
                    374:                } else if (strcmp(*argv, "-out") == 0) {
                    375:                        if (--argc < 1)
                    376:                                goto bad;
                    377:                        outfile = *(++argv);
                    378:                } else if (strcmp(*argv, "-outdir") == 0) {
                    379:                        if (--argc < 1)
                    380:                                goto bad;
                    381:                        outdir = *(++argv);
                    382:                } else if (strcmp(*argv, "-sigopt") == 0) {
                    383:                        if (--argc < 1)
                    384:                                goto bad;
                    385:                        if (!sigopts)
                    386:                                sigopts = sk_OPENSSL_STRING_new_null();
                    387:                        if (!sigopts ||
                    388:                            !sk_OPENSSL_STRING_push(sigopts, *(++argv)))
                    389:                                goto bad;
                    390:                } else if (strcmp(*argv, "-notext") == 0)
                    391:                        notext = 1;
                    392:                else if (strcmp(*argv, "-batch") == 0)
                    393:                        batch = 1;
                    394:                else if (strcmp(*argv, "-preserveDN") == 0)
                    395:                        preserve = 1;
                    396:                else if (strcmp(*argv, "-noemailDN") == 0)
                    397:                        email_dn = 0;
                    398:                else if (strcmp(*argv, "-gencrl") == 0)
                    399:                        gencrl = 1;
                    400:                else if (strcmp(*argv, "-msie_hack") == 0)
                    401:                        msie_hack = 1;
                    402:                else if (strcmp(*argv, "-crldays") == 0) {
                    403:                        if (--argc < 1)
                    404:                                goto bad;
                    405:                        crldays = strtonum(*(++argv), 0, LONG_MAX, &errstr);
                    406:                        if (errstr)
                    407:                                goto bad;
                    408:                } else if (strcmp(*argv, "-crlhours") == 0) {
                    409:                        if (--argc < 1)
                    410:                                goto bad;
                    411:                        crlhours = strtonum(*(++argv), 0, LONG_MAX, &errstr);
                    412:                        if (errstr)
                    413:                                goto bad;
                    414:                } else if (strcmp(*argv, "-crlsec") == 0) {
                    415:                        if (--argc < 1)
                    416:                                goto bad;
                    417:                        crlsec = strtonum(*(++argv), 0, LONG_MAX, &errstr);
                    418:                        if (errstr)
                    419:                                goto bad;
                    420:                } else if (strcmp(*argv, "-infiles") == 0) {
                    421:                        argc--;
                    422:                        argv++;
                    423:                        req = 1;
                    424:                        break;
                    425:                } else if (strcmp(*argv, "-ss_cert") == 0) {
                    426:                        if (--argc < 1)
                    427:                                goto bad;
                    428:                        ss_cert_file = *(++argv);
                    429:                        req = 1;
                    430:                } else if (strcmp(*argv, "-spkac") == 0) {
                    431:                        if (--argc < 1)
                    432:                                goto bad;
                    433:                        spkac_file = *(++argv);
                    434:                        req = 1;
                    435:                } else if (strcmp(*argv, "-revoke") == 0) {
                    436:                        if (--argc < 1)
                    437:                                goto bad;
                    438:                        infile = *(++argv);
                    439:                        dorevoke = 1;
                    440:                } else if (strcmp(*argv, "-extensions") == 0) {
                    441:                        if (--argc < 1)
                    442:                                goto bad;
                    443:                        extensions = *(++argv);
                    444:                } else if (strcmp(*argv, "-extfile") == 0) {
                    445:                        if (--argc < 1)
                    446:                                goto bad;
                    447:                        extfile = *(++argv);
                    448:                } else if (strcmp(*argv, "-status") == 0) {
                    449:                        if (--argc < 1)
                    450:                                goto bad;
                    451:                        ser_status = *(++argv);
                    452:                } else if (strcmp(*argv, "-updatedb") == 0) {
                    453:                        doupdatedb = 1;
                    454:                } else if (strcmp(*argv, "-crlexts") == 0) {
                    455:                        if (--argc < 1)
                    456:                                goto bad;
                    457:                        crl_ext = *(++argv);
                    458:                } else if (strcmp(*argv, "-crl_reason") == 0) {
                    459:                        if (--argc < 1)
                    460:                                goto bad;
                    461:                        rev_arg = *(++argv);
                    462:                        rev_type = REV_CRL_REASON;
                    463:                } else if (strcmp(*argv, "-crl_hold") == 0) {
                    464:                        if (--argc < 1)
                    465:                                goto bad;
                    466:                        rev_arg = *(++argv);
                    467:                        rev_type = REV_HOLD;
                    468:                } else if (strcmp(*argv, "-crl_compromise") == 0) {
                    469:                        if (--argc < 1)
                    470:                                goto bad;
                    471:                        rev_arg = *(++argv);
                    472:                        rev_type = REV_KEY_COMPROMISE;
                    473:                } else if (strcmp(*argv, "-crl_CA_compromise") == 0) {
                    474:                        if (--argc < 1)
                    475:                                goto bad;
                    476:                        rev_arg = *(++argv);
                    477:                        rev_type = REV_CA_COMPROMISE;
                    478:                }
                    479:                else {
                    480: bad:
                    481:                        if (errstr)
                    482:                                BIO_printf(bio_err, "invalid argument %s: %s\n",
                    483:                                    *argv, errstr);
                    484:                        else
                    485:                                BIO_printf(bio_err, "unknown option %s\n", *argv);
                    486:                        badops = 1;
                    487:                        break;
                    488:                }
                    489:                argc--;
                    490:                argv++;
                    491:        }
                    492:
                    493:        if (badops) {
                    494:                const char **pp2;
                    495:
                    496:                for (pp2 = ca_usage; (*pp2 != NULL); pp2++)
                    497:                        BIO_printf(bio_err, "%s", *pp2);
                    498:                goto err;
                    499:        }
                    500:
                    501:        /*****************************************************************/
                    502:        tofree = NULL;
                    503:        if (configfile == NULL)
                    504:                configfile = getenv("OPENSSL_CONF");
                    505:        if (configfile == NULL) {
                    506:                if ((tofree = make_config_name()) == NULL) {
                    507:                        BIO_printf(bio_err, "error making config file name\n");
                    508:                        goto err;
                    509:                }
                    510:                configfile = tofree;
                    511:        }
                    512:        BIO_printf(bio_err, "Using configuration from %s\n", configfile);
                    513:        conf = NCONF_new(NULL);
                    514:        if (NCONF_load(conf, configfile, &errorline) <= 0) {
                    515:                if (errorline <= 0)
                    516:                        BIO_printf(bio_err,
                    517:                            "error loading the config file '%s'\n",
                    518:                            configfile);
                    519:                else
                    520:                        BIO_printf(bio_err,
                    521:                            "error on line %ld of config file '%s'\n",
                    522:                            errorline, configfile);
                    523:                goto err;
                    524:        }
                    525:        free(tofree);
                    526:        tofree = NULL;
                    527:
                    528:        /* Lets get the config section we are using */
                    529:        if (section == NULL) {
                    530:                section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_CA);
                    531:                if (section == NULL) {
                    532:                        lookup_fail(BASE_SECTION, ENV_DEFAULT_CA);
                    533:                        goto err;
                    534:                }
                    535:        }
                    536:        if (conf != NULL) {
                    537:                p = NCONF_get_string(conf, NULL, "oid_file");
                    538:                if (p == NULL)
                    539:                        ERR_clear_error();
                    540:                if (p != NULL) {
                    541:                        BIO *oid_bio;
                    542:
                    543:                        oid_bio = BIO_new_file(p, "r");
                    544:                        if (oid_bio == NULL) {
                    545:                                /*
                    546:                                BIO_printf(bio_err,
                    547:                                    "problems opening %s for extra oid's\n", p);
                    548:                                ERR_print_errors(bio_err);
                    549:                                */
                    550:                                ERR_clear_error();
                    551:                        } else {
                    552:                                OBJ_create_objects(oid_bio);
                    553:                                BIO_free(oid_bio);
                    554:                        }
                    555:                }
                    556:                if (!add_oid_section(bio_err, conf)) {
                    557:                        ERR_print_errors(bio_err);
                    558:                        goto err;
                    559:                }
                    560:        }
                    561:        f = NCONF_get_string(conf, section, STRING_MASK);
                    562:        if (!f)
                    563:                ERR_clear_error();
                    564:
                    565:        if (f && !ASN1_STRING_set_default_mask_asc(f)) {
                    566:                BIO_printf(bio_err,
                    567:                    "Invalid global string mask setting %s\n", f);
                    568:                goto err;
                    569:        }
                    570:        if (chtype != MBSTRING_UTF8) {
                    571:                f = NCONF_get_string(conf, section, UTF8_IN);
                    572:                if (!f)
                    573:                        ERR_clear_error();
                    574:                else if (!strcmp(f, "yes"))
                    575:                        chtype = MBSTRING_UTF8;
                    576:        }
                    577:        db_attr.unique_subject = 1;
                    578:        p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT);
                    579:        if (p) {
                    580:                db_attr.unique_subject = parse_yesno(p, 1);
                    581:        } else
                    582:                ERR_clear_error();
                    583:
                    584:        in = BIO_new(BIO_s_file());
                    585:        out = BIO_new(BIO_s_file());
                    586:        Sout = BIO_new(BIO_s_file());
                    587:        Cout = BIO_new(BIO_s_file());
                    588:        if ((in == NULL) || (out == NULL) || (Sout == NULL) || (Cout == NULL)) {
                    589:                ERR_print_errors(bio_err);
                    590:                goto err;
                    591:        }
                    592:        /*****************************************************************/
                    593:        /* report status of cert with serial number given on command line */
                    594:        if (ser_status) {
                    595:                if ((dbfile = NCONF_get_string(conf, section,
                    596:                    ENV_DATABASE)) == NULL) {
                    597:                        lookup_fail(section, ENV_DATABASE);
                    598:                        goto err;
                    599:                }
                    600:                db = load_index(dbfile, &db_attr);
                    601:                if (db == NULL)
                    602:                        goto err;
                    603:
                    604:                if (!index_index(db))
                    605:                        goto err;
                    606:
                    607:                if (get_certificate_status(ser_status, db) != 1)
                    608:                        BIO_printf(bio_err, "Error verifying serial %s!\n",
                    609:                            ser_status);
                    610:                goto err;
                    611:        }
                    612:        /*****************************************************************/
                    613:        /* we definitely need a private key, so let's get it */
                    614:
                    615:        if ((keyfile == NULL) && ((keyfile = NCONF_get_string(conf,
                    616:            section, ENV_PRIVATE_KEY)) == NULL)) {
                    617:                lookup_fail(section, ENV_PRIVATE_KEY);
                    618:                goto err;
                    619:        }
                    620:        if (!key) {
                    621:                free_key = 1;
                    622:                if (!app_passwd(bio_err, passargin, NULL, &key, NULL)) {
                    623:                        BIO_printf(bio_err, "Error getting password\n");
                    624:                        goto err;
                    625:                }
                    626:        }
1.12      bcook     627:        pkey = load_key(bio_err, keyfile, keyform, 0, key, "CA private key");
1.1       jsing     628:        if (key)
1.11      jsing     629:                explicit_bzero(key, strlen(key));
1.1       jsing     630:        if (pkey == NULL) {
                    631:                /* load_key() has already printed an appropriate message */
                    632:                goto err;
                    633:        }
                    634:        /*****************************************************************/
                    635:        /* we need a certificate */
                    636:        if (!selfsign || spkac_file || ss_cert_file || gencrl) {
                    637:                if ((certfile == NULL) &&
                    638:                    ((certfile = NCONF_get_string(conf,
                    639:                    section, ENV_CERTIFICATE)) == NULL)) {
                    640:                        lookup_fail(section, ENV_CERTIFICATE);
                    641:                        goto err;
                    642:                }
1.12      bcook     643:                x509 = load_cert(bio_err, certfile, FORMAT_PEM, NULL,
1.1       jsing     644:                    "CA certificate");
                    645:                if (x509 == NULL)
                    646:                        goto err;
                    647:
                    648:                if (!X509_check_private_key(x509, pkey)) {
                    649:                        BIO_printf(bio_err,
                    650:                            "CA certificate and CA private key do not match\n");
                    651:                        goto err;
                    652:                }
                    653:        }
                    654:        if (!selfsign)
                    655:                x509p = x509;
                    656:
                    657:        f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE);
                    658:        if (f == NULL)
                    659:                ERR_clear_error();
                    660:        if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
                    661:                preserve = 1;
                    662:        f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK);
                    663:        if (f == NULL)
                    664:                ERR_clear_error();
                    665:        if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
                    666:                msie_hack = 1;
                    667:
                    668:        f = NCONF_get_string(conf, section, ENV_NAMEOPT);
                    669:
                    670:        if (f) {
                    671:                if (!set_name_ex(&nameopt, f)) {
                    672:                        BIO_printf(bio_err,
                    673:                            "Invalid name options: \"%s\"\n", f);
                    674:                        goto err;
                    675:                }
                    676:                default_op = 0;
                    677:        } else
                    678:                ERR_clear_error();
                    679:
                    680:        f = NCONF_get_string(conf, section, ENV_CERTOPT);
                    681:
                    682:        if (f) {
                    683:                if (!set_cert_ex(&certopt, f)) {
                    684:                        BIO_printf(bio_err,
                    685:                            "Invalid certificate options: \"%s\"\n", f);
                    686:                        goto err;
                    687:                }
                    688:                default_op = 0;
                    689:        } else
                    690:                ERR_clear_error();
                    691:
                    692:        f = NCONF_get_string(conf, section, ENV_EXTCOPY);
                    693:
                    694:        if (f) {
                    695:                if (!set_ext_copy(&ext_copy, f)) {
                    696:                        BIO_printf(bio_err,
                    697:                            "Invalid extension copy option: \"%s\"\n", f);
                    698:                        goto err;
                    699:                }
                    700:        } else
                    701:                ERR_clear_error();
                    702:
                    703:        /*****************************************************************/
                    704:        /* lookup where to write new certificates */
                    705:        if ((outdir == NULL) && (req)) {
                    706:
                    707:                if ((outdir = NCONF_get_string(conf, section,
                    708:                    ENV_NEW_CERTS_DIR)) == NULL) {
                    709:                        BIO_printf(bio_err, "there needs to be defined a directory for new certificate to be placed in\n");
                    710:                        goto err;
                    711:                }
                    712:                /*
                    713:                 * outdir is a directory spec, but access() for VMS demands a
                    714:                 * filename.  In any case, stat(), below, will catch the
                    715:                 * problem if outdir is not a directory spec, and the fopen()
                    716:                 * or open() will catch an error if there is no write access.
                    717:                 *
                    718:                 * Presumably, this problem could also be solved by using the
                    719:                 * DEC C routines to convert the directory syntax to Unixly,
                    720:                 * and give that to access().  However, time's too short to
                    721:                 * do that just now.
                    722:                 */
                    723:                if (access(outdir, R_OK | W_OK | X_OK) != 0) {
                    724:                        BIO_printf(bio_err,
                    725:                            "I am unable to access the %s directory\n", outdir);
                    726:                        perror(outdir);
                    727:                        goto err;
                    728:                }
                    729:                if (app_isdir(outdir) <= 0) {
                    730:                        BIO_printf(bio_err,
                    731:                            "%s need to be a directory\n", outdir);
                    732:                        perror(outdir);
                    733:                        goto err;
                    734:                }
                    735:        }
                    736:        /*****************************************************************/
                    737:        /* we need to load the database file */
                    738:        if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) {
                    739:                lookup_fail(section, ENV_DATABASE);
                    740:                goto err;
                    741:        }
                    742:        db = load_index(dbfile, &db_attr);
                    743:        if (db == NULL)
                    744:                goto err;
                    745:
                    746:        /* Lets check some fields */
                    747:        for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
                    748:                pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
                    749:                if ((pp[DB_type][0] != DB_TYPE_REV) &&
                    750:                    (pp[DB_rev_date][0] != '\0')) {
                    751:                        BIO_printf(bio_err, "entry %d: not revoked yet, but has a revocation date\n", i + 1);
                    752:                        goto err;
                    753:                }
                    754:                if ((pp[DB_type][0] == DB_TYPE_REV) &&
                    755:                    !make_revoked(NULL, pp[DB_rev_date])) {
                    756:                        BIO_printf(bio_err, " in entry %d\n", i + 1);
                    757:                        goto err;
                    758:                }
                    759:                if (!check_time_format((char *) pp[DB_exp_date])) {
                    760:                        BIO_printf(bio_err, "entry %d: invalid expiry date\n",
                    761:                            i + 1);
                    762:                        goto err;
                    763:                }
                    764:                p = pp[DB_serial];
                    765:                j = strlen(p);
                    766:                if (*p == '-') {
                    767:                        p++;
                    768:                        j--;
                    769:                }
                    770:                if ((j & 1) || (j < 2)) {
                    771:                        BIO_printf(bio_err,
                    772:                            "entry %d: bad serial number length (%d)\n",
                    773:                            i + 1, j);
                    774:                        goto err;
                    775:                }
                    776:                while (*p) {
                    777:                        if (!(((*p >= '0') && (*p <= '9')) ||
                    778:                            ((*p >= 'A') && (*p <= 'F')) ||
                    779:                            ((*p >= 'a') && (*p <= 'f')))) {
                    780:                                BIO_printf(bio_err, "entry %d: bad serial number characters, char pos %ld, char is '%c'\n", i + 1, (long) (p - pp[DB_serial]), *p);
                    781:                                goto err;
                    782:                        }
                    783:                        p++;
                    784:                }
                    785:        }
                    786:        if (verbose) {
                    787:                BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);     /* cannot fail */
                    788:                TXT_DB_write(out, db->db);
                    789:                BIO_printf(bio_err, "%d entries loaded from the database\n",
                    790:                    sk_OPENSSL_PSTRING_num(db->db->data));
                    791:                BIO_printf(bio_err, "generating index\n");
                    792:        }
                    793:        if (!index_index(db))
                    794:                goto err;
                    795:
                    796:        /*****************************************************************/
                    797:        /* Update the db file for expired certificates */
                    798:        if (doupdatedb) {
                    799:                if (verbose)
                    800:                        BIO_printf(bio_err, "Updating %s ...\n", dbfile);
                    801:
                    802:                i = do_updatedb(db);
                    803:                if (i == -1) {
                    804:                        BIO_printf(bio_err, "Malloc failure\n");
                    805:                        goto err;
                    806:                } else if (i == 0) {
                    807:                        if (verbose)
                    808:                                BIO_printf(bio_err,
                    809:                                    "No entries found to mark expired\n");
                    810:                } else {
                    811:                        if (!save_index(dbfile, "new", db))
                    812:                                goto err;
                    813:
                    814:                        if (!rotate_index(dbfile, "new", "old"))
                    815:                                goto err;
                    816:
                    817:                        if (verbose)
                    818:                                BIO_printf(bio_err,
                    819:                                    "Done. %d entries marked as expired\n", i);
                    820:                }
                    821:        }
                    822:        /*****************************************************************/
                    823:        /* Read extentions config file                                   */
                    824:        if (extfile) {
                    825:                extconf = NCONF_new(NULL);
                    826:                if (NCONF_load(extconf, extfile, &errorline) <= 0) {
                    827:                        if (errorline <= 0)
                    828:                                BIO_printf(bio_err,
                    829:                                    "ERROR: loading the config file '%s'\n",
                    830:                                    extfile);
                    831:                        else
                    832:                                BIO_printf(bio_err,
                    833:                                    "ERROR: on line %ld of config file '%s'\n",
                    834:                                    errorline, extfile);
                    835:                        ret = 1;
                    836:                        goto err;
                    837:                }
                    838:                if (verbose)
                    839:                        BIO_printf(bio_err,
                    840:                            "Successfully loaded extensions file %s\n",
                    841:                            extfile);
                    842:
                    843:                /* We can have sections in the ext file */
                    844:                if (!extensions && !(extensions = NCONF_get_string(extconf,
                    845:                    "default", "extensions")))
                    846:                        extensions = "default";
                    847:        }
                    848:        /*****************************************************************/
                    849:        if (req || gencrl) {
                    850:                if (outfile != NULL) {
                    851:                        if (BIO_write_filename(Sout, outfile) <= 0) {
                    852:                                perror(outfile);
                    853:                                goto err;
                    854:                        }
                    855:                } else {
                    856:                        BIO_set_fp(Sout, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
                    857:                }
                    858:        }
                    859:        if ((md == NULL) && ((md = NCONF_get_string(conf, section,
                    860:            ENV_DEFAULT_MD)) == NULL)) {
                    861:                lookup_fail(section, ENV_DEFAULT_MD);
                    862:                goto err;
                    863:        }
                    864:        if (!strcmp(md, "default")) {
                    865:                int def_nid;
                    866:                if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) {
                    867:                        BIO_puts(bio_err, "no default digest\n");
                    868:                        goto err;
                    869:                }
                    870:                md = (char *) OBJ_nid2sn(def_nid);
                    871:        }
                    872:        if ((dgst = EVP_get_digestbyname(md)) == NULL) {
                    873:                BIO_printf(bio_err,
                    874:                    "%s is an unsupported message digest type\n", md);
                    875:                goto err;
                    876:        }
                    877:        if (req) {
                    878:                if ((email_dn == 1) && ((tmp_email_dn = NCONF_get_string(conf,
                    879:                    section, ENV_DEFAULT_EMAIL_DN)) != NULL)) {
                    880:                        if (strcmp(tmp_email_dn, "no") == 0)
                    881:                                email_dn = 0;
                    882:                }
                    883:                if (verbose)
                    884:                        BIO_printf(bio_err, "message digest is %s\n",
                    885:                            OBJ_nid2ln(dgst->type));
                    886:                if ((policy == NULL) && ((policy = NCONF_get_string(conf,
                    887:                    section, ENV_POLICY)) == NULL)) {
                    888:                        lookup_fail(section, ENV_POLICY);
                    889:                        goto err;
                    890:                }
                    891:                if (verbose)
                    892:                        BIO_printf(bio_err, "policy is %s\n", policy);
                    893:
                    894:                if ((serialfile = NCONF_get_string(conf, section,
                    895:                    ENV_SERIAL)) == NULL) {
                    896:                        lookup_fail(section, ENV_SERIAL);
                    897:                        goto err;
                    898:                }
                    899:                if (!extconf) {
                    900:                        /*
                    901:                         * no '-extfile' option, so we look for extensions in
                    902:                         * the main configuration file
                    903:                         */
                    904:                        if (!extensions) {
                    905:                                extensions = NCONF_get_string(conf, section,
                    906:                                    ENV_EXTENSIONS);
                    907:                                if (!extensions)
                    908:                                        ERR_clear_error();
                    909:                        }
                    910:                        if (extensions) {
                    911:                                /* Check syntax of file */
                    912:                                X509V3_CTX ctx;
                    913:                                X509V3_set_ctx_test(&ctx);
                    914:                                X509V3_set_nconf(&ctx, conf);
                    915:                                if (!X509V3_EXT_add_nconf(conf, &ctx,
                    916:                                    extensions, NULL)) {
                    917:                                        BIO_printf(bio_err,
                    918:                                            "Error Loading extension section %s\n",
                    919:                                            extensions);
                    920:                                        ret = 1;
                    921:                                        goto err;
                    922:                                }
                    923:                        }
                    924:                }
                    925:                if (startdate == NULL) {
                    926:                        startdate = NCONF_get_string(conf, section,
                    927:                            ENV_DEFAULT_STARTDATE);
                    928:                        if (startdate == NULL)
                    929:                                ERR_clear_error();
                    930:                }
                    931:                if (startdate && !ASN1_TIME_set_string(NULL, startdate)) {
                    932:                        BIO_printf(bio_err, "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
                    933:                        goto err;
                    934:                }
                    935:                if (startdate == NULL)
                    936:                        startdate = "today";
                    937:
                    938:                if (enddate == NULL) {
                    939:                        enddate = NCONF_get_string(conf, section,
                    940:                            ENV_DEFAULT_ENDDATE);
                    941:                        if (enddate == NULL)
                    942:                                ERR_clear_error();
                    943:                }
                    944:                if (enddate && !ASN1_TIME_set_string(NULL, enddate)) {
                    945:                        BIO_printf(bio_err, "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
                    946:                        goto err;
                    947:                }
                    948:                if (days == 0) {
                    949:                        if (!NCONF_get_number(conf, section,
                    950:                            ENV_DEFAULT_DAYS, &days))
                    951:                                days = 0;
                    952:                }
                    953:                if (!enddate && (days == 0)) {
                    954:                        BIO_printf(bio_err,
                    955:                            "cannot lookup how many days to certify for\n");
                    956:                        goto err;
                    957:                }
                    958:                if ((serial = load_serial(serialfile, create_ser, NULL)) ==
                    959:                    NULL) {
                    960:                        BIO_printf(bio_err,
                    961:                            "error while loading serial number\n");
                    962:                        goto err;
                    963:                }
                    964:                if (verbose) {
                    965:                        if (BN_is_zero(serial))
                    966:                                BIO_printf(bio_err,
                    967:                                    "next serial number is 00\n");
                    968:                        else {
                    969:                                if ((f = BN_bn2hex(serial)) == NULL)
                    970:                                        goto err;
                    971:                                BIO_printf(bio_err,
                    972:                                    "next serial number is %s\n", f);
                    973:                                free(f);
                    974:                        }
                    975:                }
                    976:                if ((attribs = NCONF_get_section(conf, policy)) == NULL) {
                    977:                        BIO_printf(bio_err,
                    978:                            "unable to find 'section' for %s\n", policy);
                    979:                        goto err;
                    980:                }
                    981:                if ((cert_sk = sk_X509_new_null()) == NULL) {
                    982:                        BIO_printf(bio_err, "Memory allocation failure\n");
                    983:                        goto err;
                    984:                }
                    985:                if (spkac_file != NULL) {
                    986:                        total++;
                    987:                        j = certify_spkac(&x, spkac_file, pkey, x509, dgst,
                    988:                            sigopts, attribs, db, serial, subj, chtype,
                    989:                            multirdn, email_dn, startdate, enddate, days,
                    990:                            extensions, conf, verbose, certopt, nameopt,
                    991:                            default_op, ext_copy);
                    992:                        if (j < 0)
                    993:                                goto err;
                    994:                        if (j > 0) {
                    995:                                total_done++;
                    996:                                BIO_printf(bio_err, "\n");
                    997:                                if (!BN_add_word(serial, 1))
                    998:                                        goto err;
                    999:                                if (!sk_X509_push(cert_sk, x)) {
                   1000:                                        BIO_printf(bio_err,
                   1001:                                            "Memory allocation failure\n");
                   1002:                                        goto err;
                   1003:                                }
                   1004:                                if (outfile) {
                   1005:                                        output_der = 1;
                   1006:                                        batch = 1;
                   1007:                                }
                   1008:                        }
                   1009:                }
                   1010:                if (ss_cert_file != NULL) {
                   1011:                        total++;
                   1012:                        j = certify_cert(&x, ss_cert_file, pkey, x509, dgst,
                   1013:                            sigopts, attribs, db, serial, subj, chtype,
                   1014:                            multirdn, email_dn, startdate, enddate, days, batch,
                   1015:                            extensions, conf, verbose, certopt, nameopt,
1.12      bcook    1016:                            default_op, ext_copy);
1.1       jsing    1017:                        if (j < 0)
                   1018:                                goto err;
                   1019:                        if (j > 0) {
                   1020:                                total_done++;
                   1021:                                BIO_printf(bio_err, "\n");
                   1022:                                if (!BN_add_word(serial, 1))
                   1023:                                        goto err;
                   1024:                                if (!sk_X509_push(cert_sk, x)) {
                   1025:                                        BIO_printf(bio_err,
                   1026:                                            "Memory allocation failure\n");
                   1027:                                        goto err;
                   1028:                                }
                   1029:                        }
                   1030:                }
                   1031:                if (infile != NULL) {
                   1032:                        total++;
                   1033:                        j = certify(&x, infile, pkey, x509p, dgst, sigopts,
                   1034:                            attribs, db, serial, subj, chtype, multirdn,
                   1035:                            email_dn, startdate, enddate, days, batch,
                   1036:                            extensions, conf, verbose, certopt, nameopt,
                   1037:                            default_op, ext_copy, selfsign);
                   1038:                        if (j < 0)
                   1039:                                goto err;
                   1040:                        if (j > 0) {
                   1041:                                total_done++;
                   1042:                                BIO_printf(bio_err, "\n");
                   1043:                                if (!BN_add_word(serial, 1))
                   1044:                                        goto err;
                   1045:                                if (!sk_X509_push(cert_sk, x)) {
                   1046:                                        BIO_printf(bio_err,
                   1047:                                            "Memory allocation failure\n");
                   1048:                                        goto err;
                   1049:                                }
                   1050:                        }
                   1051:                }
                   1052:                for (i = 0; i < argc; i++) {
                   1053:                        total++;
                   1054:                        j = certify(&x, argv[i], pkey, x509p, dgst, sigopts,
                   1055:                            attribs, db, serial, subj, chtype, multirdn,
                   1056:                            email_dn, startdate, enddate, days, batch,
                   1057:                            extensions, conf, verbose, certopt, nameopt,
                   1058:                            default_op, ext_copy, selfsign);
                   1059:                        if (j < 0)
                   1060:                                goto err;
                   1061:                        if (j > 0) {
                   1062:                                total_done++;
                   1063:                                BIO_printf(bio_err, "\n");
                   1064:                                if (!BN_add_word(serial, 1))
                   1065:                                        goto err;
                   1066:                                if (!sk_X509_push(cert_sk, x)) {
                   1067:                                        BIO_printf(bio_err,
                   1068:                                            "Memory allocation failure\n");
                   1069:                                        goto err;
                   1070:                                }
                   1071:                        }
                   1072:                }
                   1073:                /*
                   1074:                 * we have a stack of newly certified certificates and a data
                   1075:                 * base and serial number that need updating
                   1076:                 */
                   1077:
                   1078:                if (sk_X509_num(cert_sk) > 0) {
                   1079:                        if (!batch) {
                   1080:                                BIO_printf(bio_err, "\n%d out of %d certificate requests certified, commit? [y/n]", total_done, total);
                   1081:                                (void) BIO_flush(bio_err);
                   1082:                                buf[0][0] = '\0';
                   1083:                                if (!fgets(buf[0], 10, stdin)) {
                   1084:                                        BIO_printf(bio_err, "CERTIFICATION CANCELED: I/O error\n");
                   1085:                                        ret = 0;
                   1086:                                        goto err;
                   1087:                                }
                   1088:                                if ((buf[0][0] != 'y') && (buf[0][0] != 'Y')) {
                   1089:                                        BIO_printf(bio_err, "CERTIFICATION CANCELED\n");
                   1090:                                        ret = 0;
                   1091:                                        goto err;
                   1092:                                }
                   1093:                        }
                   1094:                        BIO_printf(bio_err, "Write out database with %d new entries\n", sk_X509_num(cert_sk));
                   1095:
                   1096:                        if (!save_serial(serialfile, "new", serial, NULL))
                   1097:                                goto err;
                   1098:
                   1099:                        if (!save_index(dbfile, "new", db))
                   1100:                                goto err;
                   1101:                }
                   1102:                if (verbose)
                   1103:                        BIO_printf(bio_err, "writing new certificates\n");
                   1104:                for (i = 0; i < sk_X509_num(cert_sk); i++) {
                   1105:                        int k;
1.3       doug     1106:                        char *serialstr;
1.1       jsing    1107:                        unsigned char *data;
                   1108:
                   1109:                        x = sk_X509_value(cert_sk, i);
                   1110:
                   1111:                        j = x->cert_info->serialNumber->length;
                   1112:                        data = (unsigned char *)x->cert_info->serialNumber->data;
                   1113:                        if (j > 0)
1.3       doug     1114:                                serialstr = bin2hex(data, j);
1.1       jsing    1115:                        else
1.3       doug     1116:                                serialstr = strdup("00");
                   1117:                        if (serialstr) {
1.1       jsing    1118:                                k = snprintf(buf[2], sizeof(buf[2]),
1.3       doug     1119:                                    "%s/%s.pem", outdir, serialstr);
                   1120:                                free(serialstr);
1.1       jsing    1121:                                if (k == -1 || k >= sizeof(buf[2])) {
                   1122:                                        BIO_printf(bio_err,
                   1123:                                            "certificate file name too long\n");
                   1124:                                        goto err;
                   1125:                                }
                   1126:                        } else {
                   1127:                                BIO_printf(bio_err,
                   1128:                                    "memory allocation failed\n");
                   1129:                                goto err;
                   1130:                        }
                   1131:                        if (verbose)
                   1132:                                BIO_printf(bio_err, "writing %s\n", buf[2]);
                   1133:
                   1134:                        if (BIO_write_filename(Cout, buf[2]) <= 0) {
                   1135:                                perror(buf[2]);
                   1136:                                goto err;
                   1137:                        }
                   1138:                        write_new_certificate(Cout, x, 0, notext);
                   1139:                        write_new_certificate(Sout, x, output_der, notext);
                   1140:                }
                   1141:
                   1142:                if (sk_X509_num(cert_sk)) {
                   1143:                        /* Rename the database and the serial file */
                   1144:                        if (!rotate_serial(serialfile, "new", "old"))
                   1145:                                goto err;
                   1146:
                   1147:                        if (!rotate_index(dbfile, "new", "old"))
                   1148:                                goto err;
                   1149:
                   1150:                        BIO_printf(bio_err, "Data Base Updated\n");
                   1151:                }
                   1152:        }
                   1153:        /*****************************************************************/
                   1154:        if (gencrl) {
                   1155:                int crl_v2 = 0;
                   1156:                if (!crl_ext) {
                   1157:                        crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT);
                   1158:                        if (!crl_ext)
                   1159:                                ERR_clear_error();
                   1160:                }
                   1161:                if (crl_ext) {
                   1162:                        /* Check syntax of file */
                   1163:                        X509V3_CTX ctx;
                   1164:                        X509V3_set_ctx_test(&ctx);
                   1165:                        X509V3_set_nconf(&ctx, conf);
                   1166:                        if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) {
                   1167:                                BIO_printf(bio_err,
                   1168:                                    "Error Loading CRL extension section %s\n",
                   1169:                                    crl_ext);
                   1170:                                ret = 1;
                   1171:                                goto err;
                   1172:                        }
                   1173:                }
                   1174:                if ((crlnumberfile = NCONF_get_string(conf, section,
                   1175:                    ENV_CRLNUMBER)) != NULL)
                   1176:                        if ((crlnumber = load_serial(crlnumberfile, 0,
                   1177:                            NULL)) == NULL) {
                   1178:                                BIO_printf(bio_err,
                   1179:                                    "error while loading CRL number\n");
                   1180:                                goto err;
                   1181:                        }
                   1182:                if (!crldays && !crlhours && !crlsec) {
                   1183:                        if (!NCONF_get_number(conf, section,
                   1184:                            ENV_DEFAULT_CRL_DAYS, &crldays))
                   1185:                                crldays = 0;
                   1186:                        if (!NCONF_get_number(conf, section,
                   1187:                            ENV_DEFAULT_CRL_HOURS, &crlhours))
                   1188:                                crlhours = 0;
                   1189:                        ERR_clear_error();
                   1190:                }
                   1191:                if ((crldays == 0) && (crlhours == 0) && (crlsec == 0)) {
                   1192:                        BIO_printf(bio_err, "cannot lookup how long until the next CRL is issued\n");
                   1193:                        goto err;
                   1194:                }
                   1195:                if (verbose)
                   1196:                        BIO_printf(bio_err, "making CRL\n");
                   1197:                if ((crl = X509_CRL_new()) == NULL)
                   1198:                        goto err;
                   1199:                if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))
                   1200:                        goto err;
                   1201:
                   1202:                tmptm = ASN1_TIME_new();
                   1203:                if (!tmptm)
                   1204:                        goto err;
                   1205:                X509_gmtime_adj(tmptm, 0);
                   1206:                X509_CRL_set_lastUpdate(crl, tmptm);
                   1207:                if (!X509_time_adj_ex(tmptm, crldays,
                   1208:                    crlhours * 60 * 60 + crlsec, NULL)) {
                   1209:                        BIO_puts(bio_err, "error setting CRL nextUpdate\n");
                   1210:                        goto err;
                   1211:                }
                   1212:                X509_CRL_set_nextUpdate(crl, tmptm);
                   1213:
                   1214:                ASN1_TIME_free(tmptm);
                   1215:
                   1216:                for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
                   1217:                        pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
                   1218:                        if (pp[DB_type][0] == DB_TYPE_REV) {
                   1219:                                if ((r = X509_REVOKED_new()) == NULL)
                   1220:                                        goto err;
                   1221:                                j = make_revoked(r, pp[DB_rev_date]);
                   1222:                                if (!j)
                   1223:                                        goto err;
                   1224:                                if (j == 2)
                   1225:                                        crl_v2 = 1;
                   1226:                                if (!BN_hex2bn(&serial, pp[DB_serial]))
                   1227:                                        goto err;
                   1228:                                tmpser = BN_to_ASN1_INTEGER(serial, NULL);
                   1229:                                BN_free(serial);
                   1230:                                serial = NULL;
                   1231:                                if (!tmpser)
                   1232:                                        goto err;
                   1233:                                X509_REVOKED_set_serialNumber(r, tmpser);
                   1234:                                ASN1_INTEGER_free(tmpser);
                   1235:                                X509_CRL_add0_revoked(crl, r);
                   1236:                        }
                   1237:                }
                   1238:
                   1239:                /*
                   1240:                 * sort the data so it will be written in serial number order
                   1241:                 */
                   1242:                X509_CRL_sort(crl);
                   1243:
                   1244:                /* we now have a CRL */
                   1245:                if (verbose)
                   1246:                        BIO_printf(bio_err, "signing CRL\n");
                   1247:
                   1248:                /* Add any extensions asked for */
                   1249:
                   1250:                if (crl_ext || crlnumberfile != NULL) {
                   1251:                        X509V3_CTX crlctx;
                   1252:                        X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
                   1253:                        X509V3_set_nconf(&crlctx, conf);
                   1254:
                   1255:                        if (crl_ext)
                   1256:                                if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx,
                   1257:                                    crl_ext, crl))
                   1258:                                        goto err;
                   1259:                        if (crlnumberfile != NULL) {
                   1260:                                tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
                   1261:                                if (!tmpser)
                   1262:                                        goto err;
                   1263:                                X509_CRL_add1_ext_i2d(crl, NID_crl_number,
                   1264:                                    tmpser, 0, 0);
                   1265:                                ASN1_INTEGER_free(tmpser);
                   1266:                                crl_v2 = 1;
                   1267:                                if (!BN_add_word(crlnumber, 1))
                   1268:                                        goto err;
                   1269:                        }
                   1270:                }
                   1271:                if (crl_ext || crl_v2) {
                   1272:                        if (!X509_CRL_set_version(crl, 1))
                   1273:                                goto err;       /* version 2 CRL */
                   1274:                }
                   1275:                if (crlnumberfile != NULL)      /* we have a CRL number that
                   1276:                                                 * need updating */
                   1277:                        if (!save_serial(crlnumberfile, "new", crlnumber, NULL))
                   1278:                                goto err;
                   1279:
                   1280:                if (crlnumber) {
                   1281:                        BN_free(crlnumber);
                   1282:                        crlnumber = NULL;
                   1283:                }
                   1284:                if (!do_X509_CRL_sign(bio_err, crl, pkey, dgst, sigopts))
                   1285:                        goto err;
                   1286:
                   1287:                PEM_write_bio_X509_CRL(Sout, crl);
                   1288:
                   1289:                if (crlnumberfile != NULL)      /* Rename the crlnumber file */
                   1290:                        if (!rotate_serial(crlnumberfile, "new", "old"))
                   1291:                                goto err;
                   1292:
                   1293:        }
                   1294:        /*****************************************************************/
                   1295:        if (dorevoke) {
                   1296:                if (infile == NULL) {
                   1297:                        BIO_printf(bio_err, "no input files\n");
                   1298:                        goto err;
                   1299:                } else {
                   1300:                        X509 *revcert;
                   1301:                        revcert = load_cert(bio_err, infile, FORMAT_PEM,
1.12      bcook    1302:                            NULL, infile);
1.1       jsing    1303:                        if (revcert == NULL)
                   1304:                                goto err;
                   1305:                        j = do_revoke(revcert, db, rev_type, rev_arg);
                   1306:                        if (j <= 0)
                   1307:                                goto err;
                   1308:                        X509_free(revcert);
                   1309:
                   1310:                        if (!save_index(dbfile, "new", db))
                   1311:                                goto err;
                   1312:
                   1313:                        if (!rotate_index(dbfile, "new", "old"))
                   1314:                                goto err;
                   1315:
                   1316:                        BIO_printf(bio_err, "Data Base Updated\n");
                   1317:                }
                   1318:        }
                   1319:        /*****************************************************************/
                   1320:        ret = 0;
                   1321:
                   1322: err:
                   1323:        free(tofree);
                   1324:
                   1325:        BIO_free_all(Cout);
                   1326:        BIO_free_all(Sout);
                   1327:        BIO_free_all(out);
                   1328:        BIO_free_all(in);
                   1329:
                   1330:        if (cert_sk)
                   1331:                sk_X509_pop_free(cert_sk, X509_free);
                   1332:
                   1333:        if (ret)
                   1334:                ERR_print_errors(bio_err);
                   1335:        if (free_key && key)
                   1336:                free(key);
                   1337:        BN_free(serial);
                   1338:        BN_free(crlnumber);
                   1339:        free_index(db);
                   1340:        if (sigopts)
                   1341:                sk_OPENSSL_STRING_free(sigopts);
                   1342:        EVP_PKEY_free(pkey);
                   1343:        if (x509)
                   1344:                X509_free(x509);
                   1345:        X509_CRL_free(crl);
                   1346:        NCONF_free(conf);
                   1347:        NCONF_free(extconf);
                   1348:        OBJ_cleanup();
                   1349:
                   1350:        return (ret);
                   1351: }
                   1352:
                   1353: static void
                   1354: lookup_fail(const char *name, const char *tag)
                   1355: {
                   1356:        BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag);
                   1357: }
                   1358:
                   1359: static int
                   1360: certify(X509 ** xret, char *infile, EVP_PKEY * pkey, X509 * x509,
                   1361:     const EVP_MD * dgst, STACK_OF(OPENSSL_STRING) * sigopts,
                   1362:     STACK_OF(CONF_VALUE) * policy, CA_DB * db, BIGNUM * serial, char *subj,
                   1363:     unsigned long chtype, int multirdn, int email_dn, char *startdate,
                   1364:     char *enddate, long days, int batch, char *ext_sect, CONF * lconf,
                   1365:     int verbose, unsigned long certopt, unsigned long nameopt, int default_op,
                   1366:     int ext_copy, int selfsign)
                   1367: {
                   1368:        X509_REQ *req = NULL;
                   1369:        BIO *in = NULL;
                   1370:        EVP_PKEY *pktmp = NULL;
                   1371:        int ok = -1, i;
                   1372:
                   1373:        in = BIO_new(BIO_s_file());
                   1374:
                   1375:        if (BIO_read_filename(in, infile) <= 0) {
                   1376:                perror(infile);
                   1377:                goto err;
                   1378:        }
                   1379:        if ((req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL)) == NULL) {
                   1380:                BIO_printf(bio_err, "Error reading certificate request in %s\n",
                   1381:                    infile);
                   1382:                goto err;
                   1383:        }
                   1384:        if (verbose)
                   1385:                X509_REQ_print(bio_err, req);
                   1386:
                   1387:        BIO_printf(bio_err, "Check that the request matches the signature\n");
                   1388:
                   1389:        if (selfsign && !X509_REQ_check_private_key(req, pkey)) {
                   1390:                BIO_printf(bio_err,
                   1391:                    "Certificate request and CA private key do not match\n");
                   1392:                ok = 0;
                   1393:                goto err;
                   1394:        }
                   1395:        if ((pktmp = X509_REQ_get_pubkey(req)) == NULL) {
                   1396:                BIO_printf(bio_err, "error unpacking public key\n");
                   1397:                goto err;
                   1398:        }
                   1399:        i = X509_REQ_verify(req, pktmp);
                   1400:        EVP_PKEY_free(pktmp);
                   1401:        if (i < 0) {
                   1402:                ok = 0;
                   1403:                BIO_printf(bio_err, "Signature verification problems....\n");
                   1404:                goto err;
                   1405:        }
                   1406:        if (i == 0) {
                   1407:                ok = 0;
                   1408:                BIO_printf(bio_err,
                   1409:                    "Signature did not match the certificate request\n");
                   1410:                goto err;
                   1411:        } else
                   1412:                BIO_printf(bio_err, "Signature ok\n");
                   1413:
                   1414:        ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial,
                   1415:            subj, chtype, multirdn, email_dn, startdate, enddate, days, batch,
                   1416:            verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
                   1417:            ext_copy, selfsign);
                   1418:
                   1419: err:
                   1420:        if (req != NULL)
                   1421:                X509_REQ_free(req);
                   1422:        if (in != NULL)
                   1423:                BIO_free(in);
                   1424:        return (ok);
                   1425: }
                   1426:
                   1427: static int
                   1428: certify_cert(X509 ** xret, char *infile, EVP_PKEY * pkey, X509 * x509,
                   1429:     const EVP_MD * dgst, STACK_OF(OPENSSL_STRING) * sigopts,
                   1430:     STACK_OF(CONF_VALUE) * policy, CA_DB * db, BIGNUM * serial, char *subj,
                   1431:     unsigned long chtype, int multirdn, int email_dn, char *startdate,
                   1432:     char *enddate, long days, int batch, char *ext_sect, CONF * lconf,
                   1433:     int verbose, unsigned long certopt, unsigned long nameopt, int default_op,
1.12      bcook    1434:     int ext_copy)
1.1       jsing    1435: {
                   1436:        X509 *req = NULL;
                   1437:        X509_REQ *rreq = NULL;
                   1438:        EVP_PKEY *pktmp = NULL;
                   1439:        int ok = -1, i;
                   1440:
1.12      bcook    1441:        if ((req = load_cert(bio_err, infile, FORMAT_PEM, NULL,
1.1       jsing    1442:            infile)) == NULL)
                   1443:                goto err;
                   1444:        if (verbose)
                   1445:                X509_print(bio_err, req);
                   1446:
                   1447:        BIO_printf(bio_err, "Check that the request matches the signature\n");
                   1448:
                   1449:        if ((pktmp = X509_get_pubkey(req)) == NULL) {
                   1450:                BIO_printf(bio_err, "error unpacking public key\n");
                   1451:                goto err;
                   1452:        }
                   1453:        i = X509_verify(req, pktmp);
                   1454:        EVP_PKEY_free(pktmp);
                   1455:        if (i < 0) {
                   1456:                ok = 0;
                   1457:                BIO_printf(bio_err, "Signature verification problems....\n");
                   1458:                goto err;
                   1459:        }
                   1460:        if (i == 0) {
                   1461:                ok = 0;
                   1462:                BIO_printf(bio_err,
                   1463:                    "Signature did not match the certificate\n");
                   1464:                goto err;
                   1465:        } else
                   1466:                BIO_printf(bio_err, "Signature ok\n");
                   1467:
                   1468:        if ((rreq = X509_to_X509_REQ(req, NULL, EVP_md5())) == NULL)
                   1469:                goto err;
                   1470:
                   1471:        ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial,
                   1472:            subj, chtype, multirdn, email_dn, startdate, enddate, days, batch,
                   1473:            verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op,
                   1474:            ext_copy, 0);
                   1475:
                   1476: err:
                   1477:        if (rreq != NULL)
                   1478:                X509_REQ_free(rreq);
                   1479:        if (req != NULL)
                   1480:                X509_free(req);
                   1481:        return (ok);
                   1482: }
                   1483:
                   1484: static int
                   1485: do_body(X509 ** xret, EVP_PKEY * pkey, X509 * x509, const EVP_MD * dgst,
                   1486:     STACK_OF(OPENSSL_STRING) * sigopts, STACK_OF(CONF_VALUE) * policy,
                   1487:     CA_DB * db, BIGNUM * serial, char *subj, unsigned long chtype, int multirdn,
                   1488:     int email_dn, char *startdate, char *enddate, long days, int batch,
                   1489:     int verbose, X509_REQ * req, char *ext_sect, CONF * lconf,
                   1490:     unsigned long certopt, unsigned long nameopt, int default_op,
                   1491:     int ext_copy, int selfsign)
                   1492: {
                   1493:        X509_NAME *name = NULL, *CAname = NULL, *subject = NULL, *dn_subject = NULL;
                   1494:        ASN1_UTCTIME *tm, *tmptm;
                   1495:        ASN1_STRING *str, *str2;
                   1496:        ASN1_OBJECT *obj;
                   1497:        X509 *ret = NULL;
                   1498:        X509_CINF *ci;
                   1499:        X509_NAME_ENTRY *ne;
                   1500:        X509_NAME_ENTRY *tne, *push;
                   1501:        EVP_PKEY *pktmp;
                   1502:        int ok = -1, i, j, last, nid;
                   1503:        const char *p;
                   1504:        CONF_VALUE *cv;
                   1505:        OPENSSL_STRING row[DB_NUMBER];
                   1506:        OPENSSL_STRING *irow = NULL;
                   1507:        OPENSSL_STRING *rrow = NULL;
                   1508:        char buf[25];
                   1509:
                   1510:        tmptm = ASN1_UTCTIME_new();
                   1511:        if (tmptm == NULL) {
                   1512:                BIO_printf(bio_err, "malloc error\n");
                   1513:                return (0);
                   1514:        }
                   1515:        for (i = 0; i < DB_NUMBER; i++)
                   1516:                row[i] = NULL;
                   1517:
                   1518:        if (subj) {
                   1519:                X509_NAME *n = parse_name(subj, chtype, multirdn);
                   1520:
                   1521:                if (!n) {
                   1522:                        ERR_print_errors(bio_err);
                   1523:                        goto err;
                   1524:                }
                   1525:                X509_REQ_set_subject_name(req, n);
                   1526:                req->req_info->enc.modified = 1;
                   1527:                X509_NAME_free(n);
                   1528:        }
                   1529:        if (default_op)
                   1530:                BIO_printf(bio_err,
                   1531:                    "The Subject's Distinguished Name is as follows\n");
                   1532:
                   1533:        name = X509_REQ_get_subject_name(req);
                   1534:        for (i = 0; i < X509_NAME_entry_count(name); i++) {
                   1535:                ne = X509_NAME_get_entry(name, i);
                   1536:                str = X509_NAME_ENTRY_get_data(ne);
                   1537:                obj = X509_NAME_ENTRY_get_object(ne);
                   1538:
                   1539:                if (msie_hack) {
                   1540:                        /* assume all type should be strings */
                   1541:                        nid = OBJ_obj2nid(ne->object);
                   1542:
                   1543:                        if (str->type == V_ASN1_UNIVERSALSTRING)
                   1544:                                ASN1_UNIVERSALSTRING_to_string(str);
                   1545:
                   1546:                        if ((str->type == V_ASN1_IA5STRING) &&
                   1547:                            (nid != NID_pkcs9_emailAddress))
                   1548:                                str->type = V_ASN1_T61STRING;
                   1549:
                   1550:                        if ((nid == NID_pkcs9_emailAddress) &&
                   1551:                            (str->type == V_ASN1_PRINTABLESTRING))
                   1552:                                str->type = V_ASN1_IA5STRING;
                   1553:                }
                   1554:                /* If no EMAIL is wanted in the subject */
                   1555:                if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn))
                   1556:                        continue;
                   1557:
                   1558:                /* check some things */
                   1559:                if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&
                   1560:                    (str->type != V_ASN1_IA5STRING)) {
                   1561:                        BIO_printf(bio_err, "\nemailAddress type needs to be of type IA5STRING\n");
                   1562:                        goto err;
                   1563:                }
                   1564:                if ((str->type != V_ASN1_BMPSTRING) &&
                   1565:                    (str->type != V_ASN1_UTF8STRING)) {
                   1566:                        j = ASN1_PRINTABLE_type(str->data, str->length);
                   1567:                        if (((j == V_ASN1_T61STRING) &&
                   1568:                            (str->type != V_ASN1_T61STRING)) ||
                   1569:                            ((j == V_ASN1_IA5STRING) &&
                   1570:                            (str->type == V_ASN1_PRINTABLESTRING))) {
                   1571:                                BIO_printf(bio_err, "\nThe string contains characters that are illegal for the ASN.1 type\n");
                   1572:                                goto err;
                   1573:                        }
                   1574:                }
                   1575:                if (default_op)
                   1576:                        old_entry_print(bio_err, obj, str);
                   1577:        }
                   1578:
                   1579:        /* Ok, now we check the 'policy' stuff. */
                   1580:        if ((subject = X509_NAME_new()) == NULL) {
                   1581:                BIO_printf(bio_err, "Memory allocation failure\n");
                   1582:                goto err;
                   1583:        }
                   1584:        /* take a copy of the issuer name before we mess with it. */
                   1585:        if (selfsign)
                   1586:                CAname = X509_NAME_dup(name);
                   1587:        else
                   1588:                CAname = X509_NAME_dup(x509->cert_info->subject);
                   1589:        if (CAname == NULL)
                   1590:                goto err;
                   1591:        str = str2 = NULL;
                   1592:
                   1593:        for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {
                   1594:                cv = sk_CONF_VALUE_value(policy, i);    /* get the object id */
                   1595:                if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {
                   1596:                        BIO_printf(bio_err, "%s:unknown object type in 'policy' configuration\n", cv->name);
                   1597:                        goto err;
                   1598:                }
                   1599:                obj = OBJ_nid2obj(j);
                   1600:
                   1601:                last = -1;
                   1602:                for (;;) {
                   1603:                        /* lookup the object in the supplied name list */
                   1604:                        j = X509_NAME_get_index_by_OBJ(name, obj, last);
                   1605:                        if (j < 0) {
                   1606:                                if (last != -1)
                   1607:                                        break;
                   1608:                                tne = NULL;
                   1609:                        } else {
                   1610:                                tne = X509_NAME_get_entry(name, j);
                   1611:                        }
                   1612:                        last = j;
                   1613:
                   1614:                        /* depending on the 'policy', decide what to do. */
                   1615:                        push = NULL;
                   1616:                        if (strcmp(cv->value, "optional") == 0) {
                   1617:                                if (tne != NULL)
                   1618:                                        push = tne;
                   1619:                        } else if (strcmp(cv->value, "supplied") == 0) {
                   1620:                                if (tne == NULL) {
                   1621:                                        BIO_printf(bio_err, "The %s field needed to be supplied and was missing\n", cv->name);
                   1622:                                        goto err;
                   1623:                                } else
                   1624:                                        push = tne;
                   1625:                        } else if (strcmp(cv->value, "match") == 0) {
                   1626:                                int last2;
                   1627:
                   1628:                                if (tne == NULL) {
                   1629:                                        BIO_printf(bio_err, "The mandatory %s field was missing\n", cv->name);
                   1630:                                        goto err;
                   1631:                                }
                   1632:                                last2 = -1;
                   1633:
                   1634: again2:
                   1635:                                j = X509_NAME_get_index_by_OBJ(CAname, obj, last2);
                   1636:                                if ((j < 0) && (last2 == -1)) {
                   1637:                                        BIO_printf(bio_err, "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n", cv->name);
                   1638:                                        goto err;
                   1639:                                }
                   1640:                                if (j >= 0) {
                   1641:                                        push = X509_NAME_get_entry(CAname, j);
                   1642:                                        str = X509_NAME_ENTRY_get_data(tne);
                   1643:                                        str2 = X509_NAME_ENTRY_get_data(push);
                   1644:                                        last2 = j;
                   1645:                                        if (ASN1_STRING_cmp(str, str2) != 0)
                   1646:                                                goto again2;
                   1647:                                }
                   1648:                                if (j < 0) {
                   1649:                                        BIO_printf(bio_err, "The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n", cv->name, ((str2 == NULL) ? "NULL" : (char *) str2->data), ((str == NULL) ? "NULL" : (char *) str->data));
                   1650:                                        goto err;
                   1651:                                }
                   1652:                        } else {
                   1653:                                BIO_printf(bio_err, "%s:invalid type in 'policy' configuration\n", cv->value);
                   1654:                                goto err;
                   1655:                        }
                   1656:
                   1657:                        if (push != NULL) {
                   1658:                                if (!X509_NAME_add_entry(subject, push,
                   1659:                                    -1, 0)) {
                   1660:                                        if (push != NULL)
                   1661:                                                X509_NAME_ENTRY_free(push);
                   1662:                                        BIO_printf(bio_err,
                   1663:                                            "Memory allocation failure\n");
                   1664:                                        goto err;
                   1665:                                }
                   1666:                        }
                   1667:                        if (j < 0)
                   1668:                                break;
                   1669:                }
                   1670:        }
                   1671:
                   1672:        if (preserve) {
                   1673:                X509_NAME_free(subject);
                   1674:                /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
                   1675:                subject = X509_NAME_dup(name);
                   1676:                if (subject == NULL)
                   1677:                        goto err;
                   1678:        }
                   1679:        if (verbose)
                   1680:                BIO_printf(bio_err, "The subject name appears to be ok, checking data base for clashes\n");
                   1681:
                   1682:        /* Build the correct Subject if no e-mail is wanted in the subject */
                   1683:        /*
                   1684:         * and add it later on because of the method extensions are added
                   1685:         * (altName)
                   1686:         */
                   1687:
                   1688:        if (email_dn)
                   1689:                dn_subject = subject;
                   1690:        else {
                   1691:                X509_NAME_ENTRY *tmpne;
                   1692:                /*
                   1693:                 * Its best to dup the subject DN and then delete any email
                   1694:                 * addresses because this retains its structure.
                   1695:                 */
                   1696:                if (!(dn_subject = X509_NAME_dup(subject))) {
                   1697:                        BIO_printf(bio_err, "Memory allocation failure\n");
                   1698:                        goto err;
                   1699:                }
                   1700:                while ((i = X509_NAME_get_index_by_NID(dn_subject,
                   1701:                    NID_pkcs9_emailAddress, -1)) >= 0) {
                   1702:                        tmpne = X509_NAME_get_entry(dn_subject, i);
                   1703:                        X509_NAME_delete_entry(dn_subject, i);
                   1704:                        X509_NAME_ENTRY_free(tmpne);
                   1705:                }
                   1706:        }
                   1707:
                   1708:        if (BN_is_zero(serial))
                   1709:                row[DB_serial] = strdup("00");
                   1710:        else
                   1711:                row[DB_serial] = BN_bn2hex(serial);
                   1712:        if (row[DB_serial] == NULL) {
                   1713:                BIO_printf(bio_err, "Memory allocation failure\n");
                   1714:                goto err;
                   1715:        }
                   1716:        if (db->attributes.unique_subject) {
                   1717:                OPENSSL_STRING *crow = row;
                   1718:
                   1719:                rrow = TXT_DB_get_by_index(db->db, DB_name, crow);
                   1720:                if (rrow != NULL) {
                   1721:                        BIO_printf(bio_err,
                   1722:                            "ERROR:There is already a certificate for %s\n",
                   1723:                            row[DB_name]);
                   1724:                }
                   1725:        }
                   1726:        if (rrow == NULL) {
                   1727:                rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
                   1728:                if (rrow != NULL) {
                   1729:                        BIO_printf(bio_err,
                   1730:                            "ERROR:Serial number %s has already been issued,\n",
                   1731:                            row[DB_serial]);
                   1732:                        BIO_printf(bio_err, "      check the database/serial_file for corruption\n");
                   1733:                }
                   1734:        }
                   1735:        if (rrow != NULL) {
                   1736:                BIO_printf(bio_err,
                   1737:                    "The matching entry has the following details\n");
                   1738:                if (rrow[DB_type][0] == 'E')
                   1739:                        p = "Expired";
                   1740:                else if (rrow[DB_type][0] == 'R')
                   1741:                        p = "Revoked";
                   1742:                else if (rrow[DB_type][0] == 'V')
                   1743:                        p = "Valid";
                   1744:                else
                   1745:                        p = "\ninvalid type, Data base error\n";
                   1746:                BIO_printf(bio_err, "Type         :%s\n", p);
                   1747:                if (rrow[DB_type][0] == 'R') {
                   1748:                        p = rrow[DB_exp_date];
                   1749:                        if (p == NULL)
                   1750:                                p = "undef";
                   1751:                        BIO_printf(bio_err, "Was revoked on:%s\n", p);
                   1752:                }
                   1753:                p = rrow[DB_exp_date];
                   1754:                if (p == NULL)
                   1755:                        p = "undef";
                   1756:                BIO_printf(bio_err, "Expires on    :%s\n", p);
                   1757:                p = rrow[DB_serial];
                   1758:                if (p == NULL)
                   1759:                        p = "undef";
                   1760:                BIO_printf(bio_err, "Serial Number :%s\n", p);
                   1761:                p = rrow[DB_file];
                   1762:                if (p == NULL)
                   1763:                        p = "undef";
                   1764:                BIO_printf(bio_err, "File name     :%s\n", p);
                   1765:                p = rrow[DB_name];
                   1766:                if (p == NULL)
                   1767:                        p = "undef";
                   1768:                BIO_printf(bio_err, "Subject Name  :%s\n", p);
                   1769:                ok = -1;        /* This is now a 'bad' error. */
                   1770:                goto err;
                   1771:        }
                   1772:        /* We are now totally happy, lets make and sign the certificate */
                   1773:        if (verbose)
                   1774:                BIO_printf(bio_err, "Everything appears to be ok, creating and signing the certificate\n");
                   1775:
                   1776:        if ((ret = X509_new()) == NULL)
                   1777:                goto err;
                   1778:        ci = ret->cert_info;
                   1779:
                   1780: #ifdef X509_V3
                   1781:        /* Make it an X509 v3 certificate. */
                   1782:        if (!X509_set_version(ret, 2))
                   1783:                goto err;
                   1784: #endif
1.13      beck     1785:        if (ci->serialNumber == NULL)
                   1786:                goto err;
1.8       jsing    1787:        if (BN_to_ASN1_INTEGER(serial, ci->serialNumber) == NULL)
1.1       jsing    1788:                goto err;
                   1789:        if (selfsign) {
                   1790:                if (!X509_set_issuer_name(ret, subject))
                   1791:                        goto err;
                   1792:        } else {
                   1793:                if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
                   1794:                        goto err;
                   1795:        }
                   1796:
                   1797:        if (strcmp(startdate, "today") == 0)
                   1798:                X509_gmtime_adj(X509_get_notBefore(ret), 0);
                   1799:        else
                   1800:                ASN1_TIME_set_string(X509_get_notBefore(ret), startdate);
                   1801:
                   1802:        if (enddate == NULL)
                   1803:                X509_time_adj_ex(X509_get_notAfter(ret), days, 0, NULL);
                   1804:        else
                   1805:                ASN1_TIME_set_string(X509_get_notAfter(ret), enddate);
                   1806:
                   1807:        if (!X509_set_subject_name(ret, subject))
                   1808:                goto err;
                   1809:
                   1810:        pktmp = X509_REQ_get_pubkey(req);
                   1811:        i = X509_set_pubkey(ret, pktmp);
                   1812:        EVP_PKEY_free(pktmp);
                   1813:        if (!i)
                   1814:                goto err;
                   1815:
                   1816:        /* Lets add the extensions, if there are any */
                   1817:        if (ext_sect) {
                   1818:                X509V3_CTX ctx;
                   1819:                if (ci->version == NULL)
                   1820:                        if ((ci->version = ASN1_INTEGER_new()) == NULL)
                   1821:                                goto err;
                   1822:                ASN1_INTEGER_set(ci->version, 2);       /* version 3 certificate */
                   1823:
                   1824:                /*
                   1825:                 * Free the current entries if any, there should not be any I
                   1826:                 * believe
                   1827:                 */
                   1828:                if (ci->extensions != NULL)
                   1829:                        sk_X509_EXTENSION_pop_free(ci->extensions,
                   1830:                            X509_EXTENSION_free);
                   1831:
                   1832:                ci->extensions = NULL;
                   1833:
                   1834:                /* Initialize the context structure */
                   1835:                if (selfsign)
                   1836:                        X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
                   1837:                else
                   1838:                        X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
                   1839:
                   1840:                if (extconf) {
                   1841:                        if (verbose)
                   1842:                                BIO_printf(bio_err,
                   1843:                                    "Extra configuration file found\n");
                   1844:
                   1845:                        /* Use the extconf configuration db LHASH */
                   1846:                        X509V3_set_nconf(&ctx, extconf);
                   1847:
                   1848:                        /* Test the structure (needed?) */
                   1849:                        /* X509V3_set_ctx_test(&ctx); */
                   1850:
                   1851:                        /* Adds exts contained in the configuration file */
                   1852:                        if (!X509V3_EXT_add_nconf(extconf, &ctx,
                   1853:                            ext_sect, ret)) {
                   1854:                                BIO_printf(bio_err,
                   1855:                                    "ERROR: adding extensions in section %s\n",
                   1856:                                    ext_sect);
                   1857:                                ERR_print_errors(bio_err);
                   1858:                                goto err;
                   1859:                        }
                   1860:                        if (verbose)
                   1861:                                BIO_printf(bio_err, "Successfully added extensions from file.\n");
                   1862:                } else if (ext_sect) {
                   1863:                        /* We found extensions to be set from config file */
                   1864:                        X509V3_set_nconf(&ctx, lconf);
                   1865:
                   1866:                        if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {
                   1867:                                BIO_printf(bio_err,
                   1868:                                    "ERROR: adding extensions in section %s\n",
                   1869:                                    ext_sect);
                   1870:                                ERR_print_errors(bio_err);
                   1871:                                goto err;
                   1872:                        }
                   1873:                        if (verbose)
                   1874:                                BIO_printf(bio_err, "Successfully added extensions from config\n");
                   1875:                }
                   1876:        }
                   1877:        /* Copy extensions from request (if any) */
                   1878:
                   1879:        if (!copy_extensions(ret, req, ext_copy)) {
                   1880:                BIO_printf(bio_err, "ERROR: adding extensions from request\n");
                   1881:                ERR_print_errors(bio_err);
                   1882:                goto err;
                   1883:        }
                   1884:        /* Set the right value for the noemailDN option */
                   1885:        if (email_dn == 0) {
                   1886:                if (!X509_set_subject_name(ret, dn_subject))
                   1887:                        goto err;
                   1888:        }
                   1889:        if (!default_op) {
                   1890:                BIO_printf(bio_err, "Certificate Details:\n");
                   1891:                /*
                   1892:                 * Never print signature details because signature not
                   1893:                 * present
                   1894:                 */
                   1895:                certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
                   1896:                X509_print_ex(bio_err, ret, nameopt, certopt);
                   1897:        }
                   1898:        BIO_printf(bio_err, "Certificate is to be certified until ");
                   1899:        ASN1_TIME_print(bio_err, X509_get_notAfter(ret));
                   1900:        if (days)
                   1901:                BIO_printf(bio_err, " (%ld days)", days);
                   1902:        BIO_printf(bio_err, "\n");
                   1903:
                   1904:        if (!batch) {
                   1905:
                   1906:                BIO_printf(bio_err, "Sign the certificate? [y/n]:");
                   1907:                (void) BIO_flush(bio_err);
                   1908:                buf[0] = '\0';
                   1909:                if (!fgets(buf, sizeof(buf) - 1, stdin)) {
                   1910:                        BIO_printf(bio_err,
                   1911:                            "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
                   1912:                        ok = 0;
                   1913:                        goto err;
                   1914:                }
                   1915:                if (!((buf[0] == 'y') || (buf[0] == 'Y'))) {
                   1916:                        BIO_printf(bio_err,
                   1917:                            "CERTIFICATE WILL NOT BE CERTIFIED\n");
                   1918:                        ok = 0;
                   1919:                        goto err;
                   1920:                }
                   1921:        }
                   1922:        pktmp = X509_get_pubkey(ret);
                   1923:        if (EVP_PKEY_missing_parameters(pktmp) &&
                   1924:            !EVP_PKEY_missing_parameters(pkey))
                   1925:                EVP_PKEY_copy_parameters(pktmp, pkey);
                   1926:        EVP_PKEY_free(pktmp);
                   1927:
                   1928:        if (!do_X509_sign(bio_err, ret, pkey, dgst, sigopts))
                   1929:                goto err;
                   1930:
                   1931:        /* We now just add it to the database */
                   1932:        row[DB_type] = malloc(2);
                   1933:
                   1934:        tm = X509_get_notAfter(ret);
                   1935:        row[DB_exp_date] = malloc(tm->length + 1);
1.16      bcook    1936:        if (row[DB_type] == NULL || row[DB_exp_date] == NULL) {
1.13      beck     1937:                BIO_printf(bio_err, "Memory allocation failure\n");
                   1938:                goto err;
                   1939:        }
                   1940:
1.1       jsing    1941:        memcpy(row[DB_exp_date], tm->data, tm->length);
                   1942:        row[DB_exp_date][tm->length] = '\0';
                   1943:
                   1944:        row[DB_rev_date] = NULL;
                   1945:
                   1946:        /* row[DB_serial] done already */
                   1947:        row[DB_file] = malloc(8);
                   1948:        row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
                   1949:
1.13      beck     1950:        if ((row[DB_type] == NULL) || (row[DB_file] == NULL) ||
                   1951:            (row[DB_name] == NULL)) {
1.1       jsing    1952:                BIO_printf(bio_err, "Memory allocation failure\n");
                   1953:                goto err;
                   1954:        }
                   1955:        (void) strlcpy(row[DB_file], "unknown", 8);
                   1956:        row[DB_type][0] = 'V';
                   1957:        row[DB_type][1] = '\0';
                   1958:
                   1959:        if ((irow = reallocarray(NULL, DB_NUMBER + 1, sizeof(char *))) ==
                   1960:            NULL) {
                   1961:                BIO_printf(bio_err, "Memory allocation failure\n");
                   1962:                goto err;
                   1963:        }
                   1964:        for (i = 0; i < DB_NUMBER; i++) {
                   1965:                irow[i] = row[i];
                   1966:                row[i] = NULL;
                   1967:        }
                   1968:        irow[DB_NUMBER] = NULL;
                   1969:
                   1970:        if (!TXT_DB_insert(db->db, irow)) {
                   1971:                BIO_printf(bio_err, "failed to update database\n");
                   1972:                BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
                   1973:                goto err;
                   1974:        }
                   1975:        ok = 1;
                   1976: err:
                   1977:        for (i = 0; i < DB_NUMBER; i++)
                   1978:                free(row[i]);
                   1979:
                   1980:        if (CAname != NULL)
                   1981:                X509_NAME_free(CAname);
                   1982:        if (subject != NULL)
                   1983:                X509_NAME_free(subject);
                   1984:        if ((dn_subject != NULL) && !email_dn)
                   1985:                X509_NAME_free(dn_subject);
                   1986:        if (tmptm != NULL)
                   1987:                ASN1_UTCTIME_free(tmptm);
                   1988:        if (ok <= 0) {
                   1989:                if (ret != NULL)
                   1990:                        X509_free(ret);
                   1991:                ret = NULL;
                   1992:        } else
                   1993:                *xret = ret;
                   1994:        return (ok);
                   1995: }
                   1996:
                   1997: static void
                   1998: write_new_certificate(BIO * bp, X509 * x, int output_der, int notext)
                   1999: {
                   2000:        if (output_der) {
                   2001:                (void) i2d_X509_bio(bp, x);
                   2002:                return;
                   2003:        }
                   2004:        if (!notext)
                   2005:                X509_print(bp, x);
                   2006:        PEM_write_bio_X509(bp, x);
                   2007: }
                   2008:
                   2009: static int
                   2010: certify_spkac(X509 ** xret, char *infile, EVP_PKEY * pkey, X509 * x509,
                   2011:     const EVP_MD * dgst, STACK_OF(OPENSSL_STRING) * sigopts,
                   2012:     STACK_OF(CONF_VALUE) * policy, CA_DB * db, BIGNUM * serial, char *subj,
                   2013:     unsigned long chtype, int multirdn, int email_dn, char *startdate,
                   2014:     char *enddate, long days, char *ext_sect, CONF * lconf, int verbose,
                   2015:     unsigned long certopt, unsigned long nameopt, int default_op, int ext_copy)
                   2016: {
                   2017:        STACK_OF(CONF_VALUE) * sk = NULL;
                   2018:        LHASH_OF(CONF_VALUE) * parms = NULL;
                   2019:        X509_REQ *req = NULL;
                   2020:        CONF_VALUE *cv = NULL;
                   2021:        NETSCAPE_SPKI *spki = NULL;
                   2022:        X509_REQ_INFO *ri;
                   2023:        char *type, *buf;
                   2024:        EVP_PKEY *pktmp = NULL;
                   2025:        X509_NAME *n = NULL;
                   2026:        int ok = -1, i, j;
                   2027:        long errline;
                   2028:        int nid;
                   2029:
                   2030:        /*
                   2031:         * Load input file into a hash table.  (This is just an easy
                   2032:         * way to read and parse the file, then put it into a convenient
                   2033:         * STACK format).
                   2034:         */
                   2035:        parms = CONF_load(NULL, infile, &errline);
                   2036:        if (parms == NULL) {
                   2037:                BIO_printf(bio_err, "error on line %ld of %s\n",
                   2038:                    errline, infile);
                   2039:                ERR_print_errors(bio_err);
                   2040:                goto err;
                   2041:        }
                   2042:        sk = CONF_get_section(parms, "default");
                   2043:        if (sk_CONF_VALUE_num(sk) == 0) {
                   2044:                BIO_printf(bio_err, "no name/value pairs found in %s\n",
                   2045:                    infile);
                   2046:                CONF_free(parms);
                   2047:                goto err;
                   2048:        }
                   2049:        /*
                   2050:         * Now create a dummy X509 request structure.  We don't actually
                   2051:         * have an X509 request, but we have many of the components
                   2052:         * (a public key, various DN components).  The idea is that we
                   2053:         * put these components into the right X509 request structure
                   2054:         * and we can use the same code as if you had a real X509 request.
                   2055:         */
                   2056:        req = X509_REQ_new();
                   2057:        if (req == NULL) {
                   2058:                ERR_print_errors(bio_err);
                   2059:                goto err;
                   2060:        }
                   2061:        /*
                   2062:         * Build up the subject name set.
                   2063:         */
                   2064:        ri = req->req_info;
                   2065:        n = ri->subject;
                   2066:
                   2067:        for (i = 0;; i++) {
                   2068:                if (sk_CONF_VALUE_num(sk) <= i)
                   2069:                        break;
                   2070:
                   2071:                cv = sk_CONF_VALUE_value(sk, i);
                   2072:                type = cv->name;
                   2073:                /*
                   2074:                 * Skip past any leading X. X: X, etc to allow for multiple
                   2075:                 * instances
                   2076:                 */
                   2077:                for (buf = cv->name; *buf; buf++) {
                   2078:                        if ((*buf == ':') || (*buf == ',') || (*buf == '.')) {
                   2079:                                buf++;
                   2080:                                if (*buf)
                   2081:                                        type = buf;
                   2082:                                break;
                   2083:                        }
                   2084:                }
                   2085:
                   2086:                buf = cv->value;
                   2087:                if ((nid = OBJ_txt2nid(type)) == NID_undef) {
                   2088:                        if (strcmp(type, "SPKAC") == 0) {
                   2089:                                spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
                   2090:                                if (spki == NULL) {
                   2091:                                        BIO_printf(bio_err, "unable to load Netscape SPKAC structure\n");
                   2092:                                        ERR_print_errors(bio_err);
                   2093:                                        goto err;
                   2094:                                }
                   2095:                        }
                   2096:                        continue;
                   2097:                }
                   2098:                if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
                   2099:                    (unsigned char *)buf, -1, -1, 0))
                   2100:                        goto err;
                   2101:        }
                   2102:        if (spki == NULL) {
                   2103:                BIO_printf(bio_err,
                   2104:                    "Netscape SPKAC structure not found in %s\n", infile);
                   2105:                goto err;
                   2106:        }
                   2107:        /*
                   2108:         * Now extract the key from the SPKI structure.
                   2109:         */
                   2110:
                   2111:        BIO_printf(bio_err,
                   2112:            "Check that the SPKAC request matches the signature\n");
                   2113:
                   2114:        if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) {
                   2115:                BIO_printf(bio_err, "error unpacking SPKAC public key\n");
                   2116:                goto err;
                   2117:        }
                   2118:        j = NETSCAPE_SPKI_verify(spki, pktmp);
                   2119:        if (j <= 0) {
                   2120:                BIO_printf(bio_err,
                   2121:                    "signature verification failed on SPKAC public key\n");
                   2122:                goto err;
                   2123:        }
                   2124:        BIO_printf(bio_err, "Signature ok\n");
                   2125:
                   2126:        X509_REQ_set_pubkey(req, pktmp);
                   2127:        EVP_PKEY_free(pktmp);
                   2128:        ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial,
                   2129:            subj, chtype, multirdn, email_dn, startdate, enddate, days, 1,
                   2130:            verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
                   2131:            ext_copy, 0);
                   2132:
                   2133: err:
                   2134:        if (req != NULL)
                   2135:                X509_REQ_free(req);
                   2136:        if (parms != NULL)
                   2137:                CONF_free(parms);
                   2138:        if (spki != NULL)
                   2139:                NETSCAPE_SPKI_free(spki);
                   2140:
                   2141:        return (ok);
                   2142: }
                   2143:
                   2144: static int
                   2145: check_time_format(const char *str)
                   2146: {
                   2147:        return ASN1_TIME_set_string(NULL, str);
                   2148: }
                   2149:
                   2150: static int
                   2151: do_revoke(X509 * x509, CA_DB * db, int type, char *value)
                   2152: {
                   2153:        ASN1_UTCTIME *tm = NULL;
                   2154:        char *row[DB_NUMBER], **rrow, **irow;
                   2155:        char *rev_str = NULL;
                   2156:        BIGNUM *bn = NULL;
                   2157:        int ok = -1, i;
                   2158:
                   2159:        for (i = 0; i < DB_NUMBER; i++)
                   2160:                row[i] = NULL;
                   2161:        row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0);
                   2162:        bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509), NULL);
                   2163:        if (!bn)
                   2164:                goto err;
                   2165:        if (BN_is_zero(bn))
                   2166:                row[DB_serial] = strdup("00");
                   2167:        else
                   2168:                row[DB_serial] = BN_bn2hex(bn);
                   2169:        BN_free(bn);
                   2170:        if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) {
                   2171:                BIO_printf(bio_err, "Memory allocation failure\n");
                   2172:                goto err;
                   2173:        }
                   2174:        /*
                   2175:         * We have to lookup by serial number because name lookup skips
                   2176:         * revoked certs
                   2177:         */
                   2178:        rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
                   2179:        if (rrow == NULL) {
                   2180:                BIO_printf(bio_err,
                   2181:                    "Adding Entry with serial number %s to DB for %s\n",
                   2182:                    row[DB_serial], row[DB_name]);
                   2183:
                   2184:                /* We now just add it to the database */
                   2185:                row[DB_type] = malloc(2);
                   2186:
                   2187:                tm = X509_get_notAfter(x509);
                   2188:                row[DB_exp_date] = malloc(tm->length + 1);
1.16      bcook    2189:                if (row[DB_type] == NULL || row[DB_exp_date] == NULL) {
1.13      beck     2190:                        BIO_printf(bio_err, "Memory allocation failure\n");
                   2191:                        goto err;
                   2192:                }
1.1       jsing    2193:                memcpy(row[DB_exp_date], tm->data, tm->length);
                   2194:                row[DB_exp_date][tm->length] = '\0';
                   2195:
                   2196:                row[DB_rev_date] = NULL;
                   2197:
                   2198:                /* row[DB_serial] done already */
                   2199:                row[DB_file] = malloc(8);
                   2200:
                   2201:                /* row[DB_name] done already */
                   2202:
1.13      beck     2203:                if ((row[DB_type] == NULL) || (row[DB_file] == NULL)) {
1.1       jsing    2204:                        BIO_printf(bio_err, "Memory allocation failure\n");
                   2205:                        goto err;
                   2206:                }
                   2207:                (void) strlcpy(row[DB_file], "unknown", 8);
                   2208:                row[DB_type][0] = 'V';
                   2209:                row[DB_type][1] = '\0';
                   2210:
                   2211:                if ((irow = reallocarray(NULL, sizeof(char *),
                   2212:                    (DB_NUMBER + 1))) == NULL) {
                   2213:                        BIO_printf(bio_err, "Memory allocation failure\n");
                   2214:                        goto err;
                   2215:                }
                   2216:                for (i = 0; i < DB_NUMBER; i++) {
                   2217:                        irow[i] = row[i];
                   2218:                        row[i] = NULL;
                   2219:                }
                   2220:                irow[DB_NUMBER] = NULL;
                   2221:
                   2222:                if (!TXT_DB_insert(db->db, irow)) {
                   2223:                        BIO_printf(bio_err, "failed to update database\n");
                   2224:                        BIO_printf(bio_err, "TXT_DB error number %ld\n",
                   2225:                            db->db->error);
                   2226:                        goto err;
                   2227:                }
                   2228:                /* Revoke Certificate */
                   2229:                ok = do_revoke(x509, db, type, value);
                   2230:
                   2231:                goto err;
                   2232:
                   2233:        } else if (index_name_cmp_noconst(row, rrow)) {
                   2234:                BIO_printf(bio_err, "ERROR:name does not match %s\n",
                   2235:                    row[DB_name]);
                   2236:                goto err;
                   2237:        } else if (rrow[DB_type][0] == 'R') {
                   2238:                BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n",
                   2239:                    row[DB_serial]);
                   2240:                goto err;
                   2241:        } else {
                   2242:                BIO_printf(bio_err, "Revoking Certificate %s.\n",
                   2243:                    rrow[DB_serial]);
                   2244:                rev_str = make_revocation_str(type, value);
                   2245:                if (!rev_str) {
                   2246:                        BIO_printf(bio_err, "Error in revocation arguments\n");
                   2247:                        goto err;
                   2248:                }
                   2249:                rrow[DB_type][0] = 'R';
                   2250:                rrow[DB_type][1] = '\0';
                   2251:                rrow[DB_rev_date] = rev_str;
                   2252:        }
                   2253:        ok = 1;
                   2254:
                   2255: err:
                   2256:        for (i = 0; i < DB_NUMBER; i++)
                   2257:                free(row[i]);
                   2258:
                   2259:        return (ok);
                   2260: }
                   2261:
                   2262: static int
                   2263: get_certificate_status(const char *serial, CA_DB * db)
                   2264: {
                   2265:        char *row[DB_NUMBER], **rrow;
                   2266:        int ok = -1, i;
                   2267:
                   2268:        /* Free Resources */
                   2269:        for (i = 0; i < DB_NUMBER; i++)
                   2270:                row[i] = NULL;
                   2271:
                   2272:        /* Malloc needed char spaces */
                   2273:        row[DB_serial] = malloc(strlen(serial) + 2);
                   2274:        if (row[DB_serial] == NULL) {
                   2275:                BIO_printf(bio_err, "Malloc failure\n");
                   2276:                goto err;
                   2277:        }
                   2278:        if (strlen(serial) % 2) {
                   2279:                /* Set the first char to 0 */ ;
                   2280:                row[DB_serial][0] = '0';
                   2281:
                   2282:                /* Copy String from serial to row[DB_serial] */
                   2283:                memcpy(row[DB_serial] + 1, serial, strlen(serial));
                   2284:                row[DB_serial][strlen(serial) + 1] = '\0';
                   2285:        } else {
                   2286:                /* Copy String from serial to row[DB_serial] */
                   2287:                memcpy(row[DB_serial], serial, strlen(serial));
                   2288:                row[DB_serial][strlen(serial)] = '\0';
                   2289:        }
                   2290:
                   2291:        /* Make it Upper Case */
                   2292:        for (i = 0; row[DB_serial][i] != '\0'; i++)
                   2293:                row[DB_serial][i] = toupper((unsigned char) row[DB_serial][i]);
                   2294:
                   2295:
                   2296:        ok = 1;
                   2297:
                   2298:        /* Search for the certificate */
                   2299:        rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
                   2300:        if (rrow == NULL) {
                   2301:                BIO_printf(bio_err, "Serial %s not present in db.\n",
                   2302:                    row[DB_serial]);
                   2303:                ok = -1;
                   2304:                goto err;
                   2305:        } else if (rrow[DB_type][0] == 'V') {
                   2306:                BIO_printf(bio_err, "%s=Valid (%c)\n",
                   2307:                    row[DB_serial], rrow[DB_type][0]);
                   2308:                goto err;
                   2309:        } else if (rrow[DB_type][0] == 'R') {
                   2310:                BIO_printf(bio_err, "%s=Revoked (%c)\n",
                   2311:                    row[DB_serial], rrow[DB_type][0]);
                   2312:                goto err;
                   2313:        } else if (rrow[DB_type][0] == 'E') {
                   2314:                BIO_printf(bio_err, "%s=Expired (%c)\n",
                   2315:                    row[DB_serial], rrow[DB_type][0]);
                   2316:                goto err;
                   2317:        } else if (rrow[DB_type][0] == 'S') {
                   2318:                BIO_printf(bio_err, "%s=Suspended (%c)\n",
                   2319:                    row[DB_serial], rrow[DB_type][0]);
                   2320:                goto err;
                   2321:        } else {
                   2322:                BIO_printf(bio_err, "%s=Unknown (%c).\n",
                   2323:                    row[DB_serial], rrow[DB_type][0]);
                   2324:                ok = -1;
                   2325:        }
                   2326:
                   2327: err:
                   2328:        for (i = 0; i < DB_NUMBER; i++)
                   2329:                free(row[i]);
                   2330:
                   2331:        return (ok);
                   2332: }
                   2333:
                   2334: static int
                   2335: do_updatedb(CA_DB * db)
                   2336: {
                   2337:        ASN1_UTCTIME *a_tm = NULL;
                   2338:        int i, cnt = 0;
                   2339:        int db_y2k, a_y2k;      /* flags = 1 if y >= 2000 */
                   2340:        char **rrow, *a_tm_s;
                   2341:
                   2342:        a_tm = ASN1_UTCTIME_new();
                   2343:
                   2344:        /* get actual time and make a string */
                   2345:        a_tm = X509_gmtime_adj(a_tm, 0);
                   2346:        a_tm_s = malloc(a_tm->length + 1);
                   2347:        if (a_tm_s == NULL) {
                   2348:                cnt = -1;
                   2349:                goto err;
                   2350:        }
                   2351:        memcpy(a_tm_s, a_tm->data, a_tm->length);
                   2352:        a_tm_s[a_tm->length] = '\0';
                   2353:
                   2354:        if (strncmp(a_tm_s, "49", 2) <= 0)
                   2355:                a_y2k = 1;
                   2356:        else
                   2357:                a_y2k = 0;
                   2358:
                   2359:        for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
                   2360:                rrow = sk_OPENSSL_PSTRING_value(db->db->data, i);
                   2361:
                   2362:                if (rrow[DB_type][0] == 'V') {
                   2363:                        /* ignore entries that are not valid */
                   2364:                        if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
                   2365:                                db_y2k = 1;
                   2366:                        else
                   2367:                                db_y2k = 0;
                   2368:
                   2369:                        if (db_y2k == a_y2k) {
                   2370:                                /* all on the same y2k side */
                   2371:                                if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0) {
                   2372:                                        rrow[DB_type][0] = 'E';
                   2373:                                        rrow[DB_type][1] = '\0';
                   2374:                                        cnt++;
                   2375:
                   2376:                                        BIO_printf(bio_err, "%s=Expired\n",
                   2377:                                            rrow[DB_serial]);
                   2378:                                }
                   2379:                        } else if (db_y2k < a_y2k) {
                   2380:                                rrow[DB_type][0] = 'E';
                   2381:                                rrow[DB_type][1] = '\0';
                   2382:                                cnt++;
                   2383:
                   2384:                                BIO_printf(bio_err, "%s=Expired\n",
                   2385:                                    rrow[DB_serial]);
                   2386:                        }
                   2387:                }
                   2388:        }
                   2389:
                   2390: err:
                   2391:        ASN1_UTCTIME_free(a_tm);
                   2392:        free(a_tm_s);
                   2393:
                   2394:        return (cnt);
                   2395: }
                   2396:
                   2397: static const char *crl_reasons[] = {
                   2398:        /* CRL reason strings */
                   2399:        "unspecified",
                   2400:        "keyCompromise",
                   2401:        "CACompromise",
                   2402:        "affiliationChanged",
                   2403:        "superseded",
                   2404:        "cessationOfOperation",
                   2405:        "certificateHold",
                   2406:        "removeFromCRL",
                   2407:        /* Additional pseudo reasons */
                   2408:        "holdInstruction",
                   2409:        "keyTime",
                   2410:        "CAkeyTime"
                   2411: };
                   2412:
                   2413: #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
                   2414:
                   2415: /* Given revocation information convert to a DB string.
                   2416:  * The format of the string is:
                   2417:  * revtime[,reason,extra]. Where 'revtime' is the
                   2418:  * revocation time (the current time). 'reason' is the
                   2419:  * optional CRL reason and 'extra' is any additional
                   2420:  * argument
                   2421:  */
                   2422:
                   2423: char *
                   2424: make_revocation_str(int rev_type, char *rev_arg)
                   2425: {
                   2426:        char *other = NULL, *str;
                   2427:        const char *reason = NULL;
                   2428:        ASN1_OBJECT *otmp;
                   2429:        ASN1_UTCTIME *revtm = NULL;
                   2430:        int i;
                   2431:        switch (rev_type) {
                   2432:        case REV_NONE:
                   2433:                break;
                   2434:
                   2435:        case REV_CRL_REASON:
                   2436:                for (i = 0; i < 8; i++) {
                   2437:                        if (!strcasecmp(rev_arg, crl_reasons[i])) {
                   2438:                                reason = crl_reasons[i];
                   2439:                                break;
                   2440:                        }
                   2441:                }
                   2442:                if (reason == NULL) {
                   2443:                        BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
                   2444:                        return NULL;
                   2445:                }
                   2446:                break;
                   2447:
                   2448:        case REV_HOLD:
                   2449:                /* Argument is an OID */
                   2450:
                   2451:                otmp = OBJ_txt2obj(rev_arg, 0);
                   2452:                ASN1_OBJECT_free(otmp);
                   2453:
                   2454:                if (otmp == NULL) {
                   2455:                        BIO_printf(bio_err,
                   2456:                            "Invalid object identifier %s\n", rev_arg);
                   2457:                        return NULL;
                   2458:                }
                   2459:                reason = "holdInstruction";
                   2460:                other = rev_arg;
                   2461:                break;
                   2462:
                   2463:        case REV_KEY_COMPROMISE:
                   2464:        case REV_CA_COMPROMISE:
                   2465:
                   2466:                /* Argument is the key compromise time  */
                   2467:                if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) {
                   2468:                        BIO_printf(bio_err,
                   2469:                            "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
                   2470:                            rev_arg);
                   2471:                        return NULL;
                   2472:                }
                   2473:                other = rev_arg;
                   2474:                if (rev_type == REV_KEY_COMPROMISE)
                   2475:                        reason = "keyTime";
                   2476:                else
                   2477:                        reason = "CAkeyTime";
                   2478:
                   2479:                break;
                   2480:
                   2481:        }
                   2482:
                   2483:        revtm = X509_gmtime_adj(NULL, 0);
                   2484:        if (asprintf(&str, "%s%s%s%s%s", revtm->data,
                   2485:            reason ? "," : "", reason ? reason : "",
                   2486:            other ? "," : "", other ? other : "") == -1)
                   2487:                str = NULL;
                   2488:        ASN1_UTCTIME_free(revtm);
                   2489:        return str;
                   2490: }
                   2491:
                   2492: /* Convert revocation field to X509_REVOKED entry
                   2493:  * return code:
                   2494:  * 0 error
                   2495:  * 1 OK
                   2496:  * 2 OK and some extensions added (i.e. V2 CRL)
                   2497:  */
                   2498:
                   2499: int
                   2500: make_revoked(X509_REVOKED * rev, const char *str)
                   2501: {
                   2502:        char *tmp = NULL;
                   2503:        int reason_code = -1;
                   2504:        int i, ret = 0;
                   2505:        ASN1_OBJECT *hold = NULL;
                   2506:        ASN1_GENERALIZEDTIME *comp_time = NULL;
                   2507:        ASN1_ENUMERATED *rtmp = NULL;
                   2508:
                   2509:        ASN1_TIME *revDate = NULL;
                   2510:
                   2511:        i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
                   2512:
                   2513:        if (i == 0)
                   2514:                goto err;
                   2515:
                   2516:        if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
                   2517:                goto err;
                   2518:
                   2519:        if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
                   2520:                rtmp = ASN1_ENUMERATED_new();
                   2521:                if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code))
                   2522:                        goto err;
                   2523:                if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
                   2524:                        goto err;
                   2525:        }
                   2526:        if (rev && comp_time) {
                   2527:                if (!X509_REVOKED_add1_ext_i2d(rev, NID_invalidity_date,
                   2528:                    comp_time, 0, 0))
                   2529:                        goto err;
                   2530:        }
                   2531:        if (rev && hold) {
                   2532:                if (!X509_REVOKED_add1_ext_i2d(rev, NID_hold_instruction_code,
                   2533:                    hold, 0, 0))
                   2534:                        goto err;
                   2535:        }
                   2536:        if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
                   2537:                ret = 2;
                   2538:        else
                   2539:                ret = 1;
                   2540:
                   2541: err:
                   2542:        free(tmp);
                   2543:
                   2544:        ASN1_OBJECT_free(hold);
                   2545:        ASN1_GENERALIZEDTIME_free(comp_time);
                   2546:        ASN1_ENUMERATED_free(rtmp);
                   2547:        ASN1_TIME_free(revDate);
                   2548:
                   2549:        return ret;
                   2550: }
                   2551:
                   2552: int
                   2553: old_entry_print(BIO * bp, ASN1_OBJECT * obj, ASN1_STRING * str)
                   2554: {
                   2555:        char buf[25], *pbuf, *p;
                   2556:        int j;
                   2557:
                   2558:        j = i2a_ASN1_OBJECT(bp, obj);
                   2559:        pbuf = buf;
                   2560:        for (j = 22 - j; j > 0; j--)
                   2561:                *(pbuf++) = ' ';
                   2562:        *(pbuf++) = ':';
                   2563:        *(pbuf++) = '\0';
                   2564:        BIO_puts(bp, buf);
                   2565:
                   2566:        if (str->type == V_ASN1_PRINTABLESTRING)
                   2567:                BIO_printf(bp, "PRINTABLE:'");
                   2568:        else if (str->type == V_ASN1_T61STRING)
                   2569:                BIO_printf(bp, "T61STRING:'");
                   2570:        else if (str->type == V_ASN1_IA5STRING)
                   2571:                BIO_printf(bp, "IA5STRING:'");
                   2572:        else if (str->type == V_ASN1_UNIVERSALSTRING)
                   2573:                BIO_printf(bp, "UNIVERSALSTRING:'");
                   2574:        else
                   2575:                BIO_printf(bp, "ASN.1 %2d:'", str->type);
                   2576:
                   2577:        p = (char *) str->data;
                   2578:        for (j = str->length; j > 0; j--) {
                   2579:                if ((*p >= ' ') && (*p <= '~'))
                   2580:                        BIO_printf(bp, "%c", *p);
                   2581:                else if (*p & 0x80)
                   2582:                        BIO_printf(bp, "\\0x%02X", *p);
                   2583:                else if ((unsigned char) *p == 0xf7)
                   2584:                        BIO_printf(bp, "^?");
                   2585:                else
                   2586:                        BIO_printf(bp, "^%c", *p + '@');
                   2587:                p++;
                   2588:        }
                   2589:        BIO_printf(bp, "'\n");
                   2590:        return 1;
                   2591: }
                   2592:
                   2593: int
                   2594: unpack_revinfo(ASN1_TIME ** prevtm, int *preason, ASN1_OBJECT ** phold,
                   2595:     ASN1_GENERALIZEDTIME ** pinvtm, const char *str)
                   2596: {
                   2597:        char *tmp = NULL;
                   2598:        char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
                   2599:        int reason_code = -1;
                   2600:        int ret = 0;
                   2601:        unsigned int i;
                   2602:        ASN1_OBJECT *hold = NULL;
                   2603:        ASN1_GENERALIZEDTIME *comp_time = NULL;
                   2604:
                   2605:        if ((tmp = strdup(str)) == NULL) {
                   2606:                BIO_printf(bio_err, "malloc failed\n");
                   2607:                goto err;
                   2608:        }
                   2609:        p = strchr(tmp, ',');
                   2610:        rtime_str = tmp;
                   2611:
                   2612:        if (p) {
                   2613:                *p = '\0';
                   2614:                p++;
                   2615:                reason_str = p;
                   2616:                p = strchr(p, ',');
                   2617:                if (p) {
                   2618:                        *p = '\0';
                   2619:                        arg_str = p + 1;
                   2620:                }
                   2621:        }
                   2622:        if (prevtm) {
                   2623:                *prevtm = ASN1_UTCTIME_new();
                   2624:                if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) {
                   2625:                        BIO_printf(bio_err, "invalid revocation date %s\n",
                   2626:                            rtime_str);
                   2627:                        goto err;
                   2628:                }
                   2629:        }
                   2630:        if (reason_str) {
                   2631:                for (i = 0; i < NUM_REASONS; i++) {
                   2632:                        if (!strcasecmp(reason_str, crl_reasons[i])) {
                   2633:                                reason_code = i;
                   2634:                                break;
                   2635:                        }
                   2636:                }
                   2637:                if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) {
                   2638:                        BIO_printf(bio_err, "invalid reason code %s\n",
                   2639:                            reason_str);
                   2640:                        goto err;
                   2641:                }
                   2642:                if (reason_code == 7)
                   2643:                        reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
                   2644:                else if (reason_code == 8) {    /* Hold instruction */
                   2645:                        if (!arg_str) {
                   2646:                                BIO_printf(bio_err,
                   2647:                                    "missing hold instruction\n");
                   2648:                                goto err;
                   2649:                        }
                   2650:                        reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
                   2651:                        hold = OBJ_txt2obj(arg_str, 0);
                   2652:
                   2653:                        if (!hold) {
                   2654:                                BIO_printf(bio_err,
                   2655:                                    "invalid object identifier %s\n", arg_str);
                   2656:                                goto err;
                   2657:                        }
                   2658:                        if (phold)
                   2659:                                *phold = hold;
                   2660:                } else if ((reason_code == 9) || (reason_code == 10)) {
                   2661:                        if (!arg_str) {
                   2662:                                BIO_printf(bio_err,
                   2663:                                    "missing compromised time\n");
                   2664:                                goto err;
                   2665:                        }
                   2666:                        comp_time = ASN1_GENERALIZEDTIME_new();
                   2667:                        if (!ASN1_GENERALIZEDTIME_set_string(comp_time,
                   2668:                            arg_str)) {
                   2669:                                BIO_printf(bio_err,
                   2670:                                    "invalid compromised time %s\n", arg_str);
                   2671:                                goto err;
                   2672:                        }
                   2673:                        if (reason_code == 9)
                   2674:                                reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
                   2675:                        else
                   2676:                                reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
                   2677:                }
                   2678:        }
                   2679:        if (preason)
                   2680:                *preason = reason_code;
                   2681:        if (pinvtm)
                   2682:                *pinvtm = comp_time;
                   2683:        else
                   2684:                ASN1_GENERALIZEDTIME_free(comp_time);
                   2685:
                   2686:        ret = 1;
                   2687:
                   2688: err:
                   2689:        free(tmp);
                   2690:
                   2691:        if (!phold)
                   2692:                ASN1_OBJECT_free(hold);
                   2693:        if (!pinvtm)
                   2694:                ASN1_GENERALIZEDTIME_free(comp_time);
                   2695:
                   2696:        return ret;
                   2697: }
                   2698:
                   2699: static char *
                   2700: bin2hex(unsigned char * data, size_t len)
                   2701: {
                   2702:        char *ret = NULL;
                   2703:        char hex[] = "0123456789ABCDEF";
                   2704:        int i;
                   2705:
                   2706:        if ((ret = malloc(len * 2 + 1))) {
                   2707:                for (i = 0; i < len; i++) {
                   2708:                        ret[i * 2 + 0] = hex[data[i] >> 4];
                   2709:                        ret[i * 2 + 1] = hex[data[i] & 0x0F];
                   2710:                }
                   2711:                ret[len * 2] = '\0';
                   2712:        }
                   2713:        return ret;
                   2714: }