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

Annotation of src/usr.bin/openssl/x509.c, Revision 1.4

1.4     ! doug        1: /* $OpenBSD: x509.c,v 1.3 2015/06/20 01:07:25 doug 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: #include <assert.h>
                     60: #include <stdio.h>
                     61: #include <stdlib.h>
                     62: #include <limits.h>
                     63: #include <string.h>
                     64:
                     65: #include "apps.h"
                     66:
                     67: #include <openssl/asn1.h>
                     68: #include <openssl/bio.h>
                     69: #include <openssl/bn.h>
                     70: #include <openssl/err.h>
                     71: #include <openssl/evp.h>
                     72: #include <openssl/objects.h>
                     73: #include <openssl/pem.h>
                     74: #include <openssl/x509.h>
                     75: #include <openssl/x509v3.h>
                     76:
                     77: #include <openssl/dsa.h>
                     78:
                     79: #include <openssl/rsa.h>
                     80:
                     81: #define        POSTFIX ".srl"
                     82: #define DEF_DAYS       30
                     83:
                     84: static const char *x509_usage[] = {
                     85:        "usage: x509 args\n",
                     86:        " -inform arg     - input format - default PEM (one of DER, NET or PEM)\n",
                     87:        " -outform arg    - output format - default PEM (one of DER, NET or PEM)\n",
                     88:        " -keyform arg    - private key format - default PEM\n",
                     89:        " -CAform arg     - CA format - default PEM\n",
                     90:        " -CAkeyform arg  - CA key format - default PEM\n",
                     91:        " -in arg         - input file - default stdin\n",
                     92:        " -out arg        - output file - default stdout\n",
                     93:        " -passin arg     - private key password source\n",
                     94:        " -serial         - print serial number value\n",
                     95:        " -subject_hash   - print subject hash value\n",
                     96: #ifndef OPENSSL_NO_MD5
                     97:        " -subject_hash_old   - print old-style (MD5) subject hash value\n",
                     98: #endif
                     99:        " -issuer_hash    - print issuer hash value\n",
                    100: #ifndef OPENSSL_NO_MD5
                    101:        " -issuer_hash_old    - print old-style (MD5) issuer hash value\n",
                    102: #endif
                    103:        " -hash           - synonym for -subject_hash\n",
                    104:        " -subject        - print subject DN\n",
                    105:        " -issuer         - print issuer DN\n",
                    106:        " -email          - print email address(es)\n",
                    107:        " -startdate      - notBefore field\n",
                    108:        " -enddate        - notAfter field\n",
                    109:        " -purpose        - print out certificate purposes\n",
                    110:        " -dates          - both Before and After dates\n",
                    111:        " -modulus        - print the RSA key modulus\n",
                    112:        " -pubkey         - output the public key\n",
                    113:        " -fingerprint    - print the certificate fingerprint\n",
                    114:        " -alias          - output certificate alias\n",
                    115:        " -noout          - no certificate output\n",
                    116:        " -ocspid         - print OCSP hash values for the subject name and public key\n",
                    117:        " -ocsp_uri       - print OCSP Responder URL(s)\n",
                    118:        " -trustout       - output a \"trusted\" certificate\n",
                    119:        " -clrtrust       - clear all trusted purposes\n",
                    120:        " -clrreject      - clear all rejected purposes\n",
                    121:        " -addtrust arg   - trust certificate for a given purpose\n",
                    122:        " -addreject arg  - reject certificate for a given purpose\n",
                    123:        " -setalias arg   - set certificate alias\n",
                    124:        " -days arg       - How long till expiry of a signed certificate - def 30 days\n",
                    125:        " -checkend arg   - check whether the cert expires in the next arg seconds\n",
                    126:        "                   exit 1 if so, 0 if not\n",
                    127:        " -signkey arg    - self sign cert with arg\n",
                    128:        " -x509toreq      - output a certification request object\n",
                    129:        " -req            - input is a certificate request, sign and output.\n",
                    130:        " -CA arg         - set the CA certificate, must be PEM format.\n",
                    131:        " -CAkey arg      - set the CA key, must be PEM format\n",
                    132:        "                   missing, it is assumed to be in the CA file.\n",
                    133:        " -CAcreateserial - create serial number file if it does not exist\n",
                    134:        " -CAserial arg   - serial file\n",
                    135:        " -set_serial     - serial number to use\n",
                    136:        " -text           - print the certificate in text form\n",
                    137:        " -C              - print out C code forms\n",
1.3       doug      138:        " -md2/-md5/-sha1 - digest to use\n",
1.1       jsing     139:        " -extfile        - configuration file with X509V3 extensions to add\n",
                    140:        " -extensions     - section from config file with X509V3 extensions to add\n",
                    141:        " -clrext         - delete extensions before signing and input certificate\n",
                    142:        " -nameopt arg    - various certificate name options\n",
                    143: #ifndef OPENSSL_NO_ENGINE
                    144:        " -engine e       - use engine e, possibly a hardware device.\n",
                    145: #endif
                    146:        " -certopt arg    - various certificate text options\n",
                    147:        NULL
                    148: };
                    149:
                    150: static int callb(int ok, X509_STORE_CTX *ctx);
                    151: static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
                    152:     const EVP_MD *digest, CONF *conf, char *section);
                    153: static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
                    154:     X509 *x, X509 *xca, EVP_PKEY *pkey, STACK_OF(OPENSSL_STRING) *sigopts,
                    155:     char *serial, int create, int days, int clrext, CONF *conf, char *section,
                    156:     ASN1_INTEGER *sno);
                    157: static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
                    158: static int reqfile = 0;
                    159:
                    160: int x509_main(int, char **);
                    161:
                    162: int
                    163: x509_main(int argc, char **argv)
                    164: {
                    165:        ENGINE *e = NULL;
                    166:        int ret = 1;
                    167:        X509_REQ *req = NULL;
                    168:        X509 *x = NULL, *xca = NULL;
                    169:        ASN1_OBJECT *objtmp;
                    170:        STACK_OF(OPENSSL_STRING) *sigopts = NULL;
                    171:        EVP_PKEY *Upkey = NULL, *CApkey = NULL;
                    172:        ASN1_INTEGER *sno = NULL;
                    173:        int i, num, badops = 0;
                    174:        BIO *out = NULL;
                    175:        BIO *STDout = NULL;
                    176:        STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
                    177:        int informat, outformat, keyformat, CAformat, CAkeyformat;
                    178:        char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;
                    179:        char *CAkeyfile = NULL, *CAserial = NULL;
                    180:        char *alias = NULL;
                    181:        int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0,
                    182:            enddate = 0;
                    183:        int next_serial = 0;
                    184:        int subject_hash = 0, issuer_hash = 0, ocspid = 0;
                    185: #ifndef OPENSSL_NO_MD5
                    186:        int subject_hash_old = 0, issuer_hash_old = 0;
                    187: #endif
                    188:        int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0,
                    189:            email = 0;
                    190:        int ocsp_uri = 0;
                    191:        int trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0, clrext = 0;
                    192:        int C = 0;
                    193:        int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0;
                    194:        int pprint = 0;
                    195:        const char **pp;
                    196:        X509_STORE *ctx = NULL;
                    197:        X509_REQ *rq = NULL;
                    198:        int fingerprint = 0;
                    199:        char buf[256];
                    200:        const EVP_MD *md_alg, *digest = NULL;
                    201:        CONF *extconf = NULL;
                    202:        char *extsect = NULL, *extfile = NULL, *passin = NULL, *passargin = NULL;
                    203:        int checkend = 0, checkoffset = 0;
                    204:        unsigned long nmflag = 0, certflag = 0;
                    205: #ifndef OPENSSL_NO_ENGINE
                    206:        char *engine = NULL;
                    207: #endif
                    208:        const char *errstr = NULL;
                    209:
                    210:        reqfile = 0;
                    211:
                    212:        STDout = BIO_new_fp(stdout, BIO_NOCLOSE);
                    213:
                    214:        informat = FORMAT_PEM;
                    215:        outformat = FORMAT_PEM;
                    216:        keyformat = FORMAT_PEM;
                    217:        CAformat = FORMAT_PEM;
                    218:        CAkeyformat = FORMAT_PEM;
                    219:
                    220:        ctx = X509_STORE_new();
                    221:        if (ctx == NULL)
                    222:                goto end;
                    223:        X509_STORE_set_verify_cb(ctx, callb);
                    224:
                    225:        argc--;
                    226:        argv++;
                    227:        num = 0;
                    228:        while (argc >= 1) {
                    229:                if (strcmp(*argv, "-inform") == 0) {
                    230:                        if (--argc < 1)
                    231:                                goto bad;
                    232:                        informat = str2fmt(*(++argv));
                    233:                } else if (strcmp(*argv, "-outform") == 0) {
                    234:                        if (--argc < 1)
                    235:                                goto bad;
                    236:                        outformat = str2fmt(*(++argv));
                    237:                } else if (strcmp(*argv, "-keyform") == 0) {
                    238:                        if (--argc < 1)
                    239:                                goto bad;
                    240:                        keyformat = str2fmt(*(++argv));
                    241:                } else if (strcmp(*argv, "-req") == 0) {
                    242:                        reqfile = 1;
                    243:                } else if (strcmp(*argv, "-CAform") == 0) {
                    244:                        if (--argc < 1)
                    245:                                goto bad;
                    246:                        CAformat = str2fmt(*(++argv));
                    247:                } else if (strcmp(*argv, "-CAkeyform") == 0) {
                    248:                        if (--argc < 1)
                    249:                                goto bad;
                    250:                        CAkeyformat = str2fmt(*(++argv));
                    251:                } else if (strcmp(*argv, "-sigopt") == 0) {
                    252:                        if (--argc < 1)
                    253:                                goto bad;
                    254:                        if (!sigopts)
                    255:                                sigopts = sk_OPENSSL_STRING_new_null();
                    256:                        if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv)))
                    257:                                goto bad;
                    258:                } else if (strcmp(*argv, "-days") == 0) {
                    259:                        if (--argc < 1)
                    260:                                goto bad;
                    261:                        days = strtonum(*(++argv), 1, INT_MAX, &errstr);
                    262:                        if (errstr) {
                    263:                                BIO_printf(bio_err, "bad number of days: %s\n", errstr);
                    264:                                goto bad;
                    265:                        }
                    266:                } else if (strcmp(*argv, "-passin") == 0) {
                    267:                        if (--argc < 1)
                    268:                                goto bad;
                    269:                        passargin = *(++argv);
                    270:                } else if (strcmp(*argv, "-extfile") == 0) {
                    271:                        if (--argc < 1)
                    272:                                goto bad;
                    273:                        extfile = *(++argv);
                    274:                } else if (strcmp(*argv, "-extensions") == 0) {
                    275:                        if (--argc < 1)
                    276:                                goto bad;
                    277:                        extsect = *(++argv);
                    278:                } else if (strcmp(*argv, "-in") == 0) {
                    279:                        if (--argc < 1)
                    280:                                goto bad;
                    281:                        infile = *(++argv);
                    282:                } else if (strcmp(*argv, "-out") == 0) {
                    283:                        if (--argc < 1)
                    284:                                goto bad;
                    285:                        outfile = *(++argv);
                    286:                } else if (strcmp(*argv, "-signkey") == 0) {
                    287:                        if (--argc < 1)
                    288:                                goto bad;
                    289:                        keyfile = *(++argv);
                    290:                        sign_flag = ++num;
                    291:                } else if (strcmp(*argv, "-CA") == 0) {
                    292:                        if (--argc < 1)
                    293:                                goto bad;
                    294:                        CAfile = *(++argv);
                    295:                        CA_flag = ++num;
                    296:                } else if (strcmp(*argv, "-CAkey") == 0) {
                    297:                        if (--argc < 1)
                    298:                                goto bad;
                    299:                        CAkeyfile = *(++argv);
                    300:                } else if (strcmp(*argv, "-CAserial") == 0) {
                    301:                        if (--argc < 1)
                    302:                                goto bad;
                    303:                        CAserial = *(++argv);
                    304:                } else if (strcmp(*argv, "-set_serial") == 0) {
                    305:                        if (--argc < 1)
                    306:                                goto bad;
1.4     ! doug      307:                        M_ASN1_INTEGER_free(sno);
1.1       jsing     308:                        if (!(sno = s2i_ASN1_INTEGER(NULL, *(++argv))))
                    309:                                goto bad;
                    310:                } else if (strcmp(*argv, "-addtrust") == 0) {
                    311:                        if (--argc < 1)
                    312:                                goto bad;
                    313:                        if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) {
                    314:                                BIO_printf(bio_err,
                    315:                                    "Invalid trust object value %s\n", *argv);
                    316:                                goto bad;
                    317:                        }
                    318:                        if (!trust)
                    319:                                trust = sk_ASN1_OBJECT_new_null();
                    320:                        sk_ASN1_OBJECT_push(trust, objtmp);
                    321:                        trustout = 1;
                    322:                } else if (strcmp(*argv, "-addreject") == 0) {
                    323:                        if (--argc < 1)
                    324:                                goto bad;
                    325:                        if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) {
                    326:                                BIO_printf(bio_err,
                    327:                                    "Invalid reject object value %s\n", *argv);
                    328:                                goto bad;
                    329:                        }
                    330:                        if (!reject)
                    331:                                reject = sk_ASN1_OBJECT_new_null();
                    332:                        sk_ASN1_OBJECT_push(reject, objtmp);
                    333:                        trustout = 1;
                    334:                } else if (strcmp(*argv, "-setalias") == 0) {
                    335:                        if (--argc < 1)
                    336:                                goto bad;
                    337:                        alias = *(++argv);
                    338:                        trustout = 1;
                    339:                } else if (strcmp(*argv, "-certopt") == 0) {
                    340:                        if (--argc < 1)
                    341:                                goto bad;
                    342:                        if (!set_cert_ex(&certflag, *(++argv)))
                    343:                                goto bad;
                    344:                } else if (strcmp(*argv, "-nameopt") == 0) {
                    345:                        if (--argc < 1)
                    346:                                goto bad;
                    347:                        if (!set_name_ex(&nmflag, *(++argv)))
                    348:                                goto bad;
                    349:                }
                    350: #ifndef OPENSSL_NO_ENGINE
                    351:                else if (strcmp(*argv, "-engine") == 0) {
                    352:                        if (--argc < 1)
                    353:                                goto bad;
                    354:                        engine = *(++argv);
                    355:                }
                    356: #endif
                    357:                else if (strcmp(*argv, "-C") == 0)
                    358:                        C = ++num;
                    359:                else if (strcmp(*argv, "-email") == 0)
                    360:                        email = ++num;
                    361:                else if (strcmp(*argv, "-ocsp_uri") == 0)
                    362:                        ocsp_uri = ++num;
                    363:                else if (strcmp(*argv, "-serial") == 0)
                    364:                        serial = ++num;
                    365:                else if (strcmp(*argv, "-next_serial") == 0)
                    366:                        next_serial = ++num;
                    367:                else if (strcmp(*argv, "-modulus") == 0)
                    368:                        modulus = ++num;
                    369:                else if (strcmp(*argv, "-pubkey") == 0)
                    370:                        pubkey = ++num;
                    371:                else if (strcmp(*argv, "-x509toreq") == 0)
                    372:                        x509req = ++num;
                    373:                else if (strcmp(*argv, "-text") == 0)
                    374:                        text = ++num;
                    375:                else if (strcmp(*argv, "-hash") == 0 ||
                    376:                    strcmp(*argv, "-subject_hash") == 0)
                    377:                        subject_hash = ++num;
                    378: #ifndef OPENSSL_NO_MD5
                    379:                else if (strcmp(*argv, "-subject_hash_old") == 0)
                    380:                        subject_hash_old = ++num;
                    381: #endif
                    382:                else if (strcmp(*argv, "-issuer_hash") == 0)
                    383:                        issuer_hash = ++num;
                    384: #ifndef OPENSSL_NO_MD5
                    385:                else if (strcmp(*argv, "-issuer_hash_old") == 0)
                    386:                        issuer_hash_old = ++num;
                    387: #endif
                    388:                else if (strcmp(*argv, "-subject") == 0)
                    389:                        subject = ++num;
                    390:                else if (strcmp(*argv, "-issuer") == 0)
                    391:                        issuer = ++num;
                    392:                else if (strcmp(*argv, "-fingerprint") == 0)
                    393:                        fingerprint = ++num;
                    394:                else if (strcmp(*argv, "-dates") == 0) {
                    395:                        startdate = ++num;
                    396:                        enddate = ++num;
                    397:                } else if (strcmp(*argv, "-purpose") == 0)
                    398:                        pprint = ++num;
                    399:                else if (strcmp(*argv, "-startdate") == 0)
                    400:                        startdate = ++num;
                    401:                else if (strcmp(*argv, "-enddate") == 0)
                    402:                        enddate = ++num;
                    403:                else if (strcmp(*argv, "-checkend") == 0) {
                    404:                        if (--argc < 1)
                    405:                                goto bad;
                    406:                        checkoffset = strtonum(*(++argv), 0, INT_MAX, &errstr);
                    407:                        if (errstr) {
                    408:                                BIO_printf(bio_err, "checkend unusable: %s\n", errstr);
                    409:                                goto bad;
                    410:                        }
                    411:                        checkend = 1;
                    412:                } else if (strcmp(*argv, "-noout") == 0)
                    413:                        noout = ++num;
                    414:                else if (strcmp(*argv, "-trustout") == 0)
                    415:                        trustout = 1;
                    416:                else if (strcmp(*argv, "-clrtrust") == 0)
                    417:                        clrtrust = ++num;
                    418:                else if (strcmp(*argv, "-clrreject") == 0)
                    419:                        clrreject = ++num;
                    420:                else if (strcmp(*argv, "-alias") == 0)
                    421:                        aliasout = ++num;
                    422:                else if (strcmp(*argv, "-CAcreateserial") == 0)
                    423:                        CA_createserial = ++num;
                    424:                else if (strcmp(*argv, "-clrext") == 0)
                    425:                        clrext = 1;
                    426:                else if (strcmp(*argv, "-ocspid") == 0)
                    427:                        ocspid = ++num;
                    428:                else if ((md_alg = EVP_get_digestbyname(*argv + 1))) {
                    429:                        /* ok */
                    430:                        digest = md_alg;
                    431:                } else {
                    432:                        BIO_printf(bio_err, "unknown option %s\n", *argv);
                    433:                        badops = 1;
                    434:                        break;
                    435:                }
                    436:                argc--;
                    437:                argv++;
                    438:        }
                    439:
                    440:        if (badops) {
                    441: bad:
                    442:                for (pp = x509_usage; (*pp != NULL); pp++)
                    443:                        BIO_printf(bio_err, "%s", *pp);
                    444:                goto end;
                    445:        }
                    446: #ifndef OPENSSL_NO_ENGINE
                    447:        e = setup_engine(bio_err, engine, 0);
                    448: #endif
                    449:
                    450:        if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
                    451:                BIO_printf(bio_err, "Error getting password\n");
                    452:                goto end;
                    453:        }
                    454:        if (!X509_STORE_set_default_paths(ctx)) {
                    455:                ERR_print_errors(bio_err);
                    456:                goto end;
                    457:        }
                    458:        if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {
                    459:                CAkeyfile = CAfile;
                    460:        } else if ((CA_flag) && (CAkeyfile == NULL)) {
                    461:                BIO_printf(bio_err,
                    462:                    "need to specify a CAkey if using the CA command\n");
                    463:                goto end;
                    464:        }
                    465:        if (extfile) {
                    466:                long errorline = -1;
                    467:                X509V3_CTX ctx2;
                    468:                extconf = NCONF_new(NULL);
                    469:                if (!NCONF_load(extconf, extfile, &errorline)) {
                    470:                        if (errorline <= 0)
                    471:                                BIO_printf(bio_err,
                    472:                                    "error loading the config file '%s'\n",
                    473:                                    extfile);
                    474:                        else
                    475:                                BIO_printf(bio_err,
                    476:                                    "error on line %ld of config file '%s'\n",
                    477:                                    errorline, extfile);
                    478:                        goto end;
                    479:                }
                    480:                if (!extsect) {
                    481:                        extsect = NCONF_get_string(extconf, "default",
                    482:                            "extensions");
                    483:                        if (!extsect) {
                    484:                                ERR_clear_error();
                    485:                                extsect = "default";
                    486:                        }
                    487:                }
                    488:                X509V3_set_ctx_test(&ctx2);
                    489:                X509V3_set_nconf(&ctx2, extconf);
                    490:                if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
                    491:                        BIO_printf(bio_err,
                    492:                            "Error Loading extension section %s\n",
                    493:                            extsect);
                    494:                        ERR_print_errors(bio_err);
                    495:                        goto end;
                    496:                }
                    497:        }
                    498:        if (reqfile) {
                    499:                EVP_PKEY *pkey;
                    500:                BIO *in;
                    501:
                    502:                if (!sign_flag && !CA_flag) {
                    503:                        BIO_printf(bio_err, "We need a private key to sign with\n");
                    504:                        goto end;
                    505:                }
                    506:                in = BIO_new(BIO_s_file());
                    507:                if (in == NULL) {
                    508:                        ERR_print_errors(bio_err);
                    509:                        goto end;
                    510:                }
                    511:                if (infile == NULL)
                    512:                        BIO_set_fp(in, stdin, BIO_NOCLOSE | BIO_FP_TEXT);
                    513:                else {
                    514:                        if (BIO_read_filename(in, infile) <= 0) {
                    515:                                perror(infile);
                    516:                                BIO_free(in);
                    517:                                goto end;
                    518:                        }
                    519:                }
                    520:                req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
                    521:                BIO_free(in);
                    522:
                    523:                if (req == NULL) {
                    524:                        ERR_print_errors(bio_err);
                    525:                        goto end;
                    526:                }
                    527:                if ((req->req_info == NULL) ||
                    528:                    (req->req_info->pubkey == NULL) ||
                    529:                    (req->req_info->pubkey->public_key == NULL) ||
                    530:                    (req->req_info->pubkey->public_key->data == NULL)) {
                    531:                        BIO_printf(bio_err, "The certificate request appears to corrupted\n");
                    532:                        BIO_printf(bio_err, "It does not contain a public key\n");
                    533:                        goto end;
                    534:                }
                    535:                if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {
                    536:                        BIO_printf(bio_err, "error unpacking public key\n");
                    537:                        goto end;
                    538:                }
                    539:                i = X509_REQ_verify(req, pkey);
                    540:                EVP_PKEY_free(pkey);
                    541:                if (i < 0) {
                    542:                        BIO_printf(bio_err, "Signature verification error\n");
                    543:                        ERR_print_errors(bio_err);
                    544:                        goto end;
                    545:                }
                    546:                if (i == 0) {
                    547:                        BIO_printf(bio_err, "Signature did not match the certificate request\n");
                    548:                        goto end;
                    549:                } else
                    550:                        BIO_printf(bio_err, "Signature ok\n");
                    551:
                    552:                print_name(bio_err, "subject=", X509_REQ_get_subject_name(req), nmflag);
                    553:
                    554:                if ((x = X509_new()) == NULL)
                    555:                        goto end;
                    556:
                    557:                if (sno == NULL) {
                    558:                        sno = ASN1_INTEGER_new();
                    559:                        if (!sno || !rand_serial(NULL, sno))
                    560:                                goto end;
                    561:                        if (!X509_set_serialNumber(x, sno))
                    562:                                goto end;
                    563:                        ASN1_INTEGER_free(sno);
                    564:                        sno = NULL;
                    565:                } else if (!X509_set_serialNumber(x, sno))
                    566:                        goto end;
                    567:
                    568:                if (!X509_set_issuer_name(x, req->req_info->subject))
                    569:                        goto end;
                    570:                if (!X509_set_subject_name(x, req->req_info->subject))
                    571:                        goto end;
                    572:
                    573:                X509_gmtime_adj(X509_get_notBefore(x), 0);
                    574:                X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL);
                    575:
                    576:                pkey = X509_REQ_get_pubkey(req);
                    577:                X509_set_pubkey(x, pkey);
                    578:                EVP_PKEY_free(pkey);
                    579:        } else
                    580:                x = load_cert(bio_err, infile, informat, NULL, e, "Certificate");
                    581:
                    582:        if (x == NULL)
                    583:                goto end;
                    584:        if (CA_flag) {
                    585:                xca = load_cert(bio_err, CAfile, CAformat, NULL, e, "CA Certificate");
                    586:                if (xca == NULL)
                    587:                        goto end;
                    588:        }
                    589:        if (!noout || text || next_serial) {
                    590:                OBJ_create("2.99999.3",
                    591:                    "SET.ex3", "SET x509v3 extension 3");
                    592:
                    593:                out = BIO_new(BIO_s_file());
                    594:                if (out == NULL) {
                    595:                        ERR_print_errors(bio_err);
                    596:                        goto end;
                    597:                }
                    598:                if (outfile == NULL) {
                    599:                        BIO_set_fp(out, stdout, BIO_NOCLOSE);
                    600:                } else {
                    601:                        if (BIO_write_filename(out, outfile) <= 0) {
                    602:                                perror(outfile);
                    603:                                goto end;
                    604:                        }
                    605:                }
                    606:        }
                    607:        if (alias)
                    608:                X509_alias_set1(x, (unsigned char *) alias, -1);
                    609:
                    610:        if (clrtrust)
                    611:                X509_trust_clear(x);
                    612:        if (clrreject)
                    613:                X509_reject_clear(x);
                    614:
                    615:        if (trust) {
                    616:                for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
                    617:                        objtmp = sk_ASN1_OBJECT_value(trust, i);
                    618:                        X509_add1_trust_object(x, objtmp);
                    619:                }
                    620:        }
                    621:        if (reject) {
                    622:                for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
                    623:                        objtmp = sk_ASN1_OBJECT_value(reject, i);
                    624:                        X509_add1_reject_object(x, objtmp);
                    625:                }
                    626:        }
                    627:        if (num) {
                    628:                for (i = 1; i <= num; i++) {
                    629:                        if (issuer == i) {
                    630:                                print_name(STDout, "issuer= ",
                    631:                                    X509_get_issuer_name(x), nmflag);
                    632:                        } else if (subject == i) {
                    633:                                print_name(STDout, "subject= ",
                    634:                                    X509_get_subject_name(x), nmflag);
                    635:                        } else if (serial == i) {
                    636:                                BIO_printf(STDout, "serial=");
                    637:                                i2a_ASN1_INTEGER(STDout,
                    638:                                    X509_get_serialNumber(x));
                    639:                                BIO_printf(STDout, "\n");
                    640:                        } else if (next_serial == i) {
                    641:                                BIGNUM *bnser;
                    642:                                ASN1_INTEGER *ser;
                    643:                                ser = X509_get_serialNumber(x);
                    644:                                bnser = ASN1_INTEGER_to_BN(ser, NULL);
                    645:                                if (!bnser)
                    646:                                        goto end;
                    647:                                if (!BN_add_word(bnser, 1))
                    648:                                        goto end;
                    649:                                ser = BN_to_ASN1_INTEGER(bnser, NULL);
                    650:                                if (!ser)
                    651:                                        goto end;
                    652:                                BN_free(bnser);
                    653:                                i2a_ASN1_INTEGER(out, ser);
                    654:                                ASN1_INTEGER_free(ser);
                    655:                                BIO_puts(out, "\n");
                    656:                        } else if ((email == i) || (ocsp_uri == i)) {
                    657:                                int j;
                    658:                                STACK_OF(OPENSSL_STRING) *emlst;
                    659:                                if (email == i)
                    660:                                        emlst = X509_get1_email(x);
                    661:                                else
                    662:                                        emlst = X509_get1_ocsp(x);
                    663:                                for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
                    664:                                        BIO_printf(STDout, "%s\n",
                    665:                                            sk_OPENSSL_STRING_value(emlst, j));
                    666:                                X509_email_free(emlst);
                    667:                        } else if (aliasout == i) {
                    668:                                unsigned char *alstr;
                    669:                                alstr = X509_alias_get0(x, NULL);
                    670:                                if (alstr)
                    671:                                        BIO_printf(STDout, "%s\n", alstr);
                    672:                                else
                    673:                                        BIO_puts(STDout, "<No Alias>\n");
                    674:                        } else if (subject_hash == i) {
                    675:                                BIO_printf(STDout, "%08lx\n", X509_subject_name_hash(x));
                    676:                        }
                    677: #ifndef OPENSSL_NO_MD5
                    678:                        else if (subject_hash_old == i) {
                    679:                                BIO_printf(STDout, "%08lx\n", X509_subject_name_hash_old(x));
                    680:                        }
                    681: #endif
                    682:                        else if (issuer_hash == i) {
                    683:                                BIO_printf(STDout, "%08lx\n", X509_issuer_name_hash(x));
                    684:                        }
                    685: #ifndef OPENSSL_NO_MD5
                    686:                        else if (issuer_hash_old == i) {
                    687:                                BIO_printf(STDout, "%08lx\n", X509_issuer_name_hash_old(x));
                    688:                        }
                    689: #endif
                    690:                        else if (pprint == i) {
                    691:                                X509_PURPOSE *ptmp;
                    692:                                int j;
                    693:                                BIO_printf(STDout, "Certificate purposes:\n");
                    694:                                for (j = 0; j < X509_PURPOSE_get_count(); j++) {
                    695:                                        ptmp = X509_PURPOSE_get0(j);
                    696:                                        purpose_print(STDout, x, ptmp);
                    697:                                }
                    698:                        } else if (modulus == i) {
                    699:                                EVP_PKEY *pkey;
                    700:
                    701:                                pkey = X509_get_pubkey(x);
                    702:                                if (pkey == NULL) {
                    703:                                        BIO_printf(bio_err, "Modulus=unavailable\n");
                    704:                                        ERR_print_errors(bio_err);
                    705:                                        goto end;
                    706:                                }
                    707:                                BIO_printf(STDout, "Modulus=");
                    708:                                if (pkey->type == EVP_PKEY_RSA)
                    709:                                        BN_print(STDout, pkey->pkey.rsa->n);
                    710:                                else
                    711:                                                if (pkey->type == EVP_PKEY_DSA)
                    712:                                                        BN_print(STDout, pkey->pkey.dsa->pub_key);
                    713:                                else
                    714:                                                BIO_printf(STDout, "Wrong Algorithm type");
                    715:                                BIO_printf(STDout, "\n");
                    716:                                EVP_PKEY_free(pkey);
                    717:                        } else if (pubkey == i) {
                    718:                                EVP_PKEY *pkey;
                    719:
                    720:                                pkey = X509_get_pubkey(x);
                    721:                                if (pkey == NULL) {
                    722:                                        BIO_printf(bio_err, "Error getting public key\n");
                    723:                                        ERR_print_errors(bio_err);
                    724:                                        goto end;
                    725:                                }
                    726:                                PEM_write_bio_PUBKEY(STDout, pkey);
                    727:                                EVP_PKEY_free(pkey);
                    728:                        } else if (C == i) {
                    729:                                unsigned char *d;
                    730:                                char *m;
                    731:                                int y, z;
                    732:
                    733:                                X509_NAME_oneline(X509_get_subject_name(x),
                    734:                                    buf, sizeof buf);
                    735:                                BIO_printf(STDout, "/* subject:%s */\n", buf);
                    736:                                m = X509_NAME_oneline(
                    737:                                    X509_get_issuer_name(x), buf,
                    738:                                    sizeof buf);
                    739:                                BIO_printf(STDout, "/* issuer :%s */\n", buf);
                    740:
                    741:                                z = i2d_X509(x, NULL);
                    742:                                m = malloc(z);
                    743:
                    744:                                d = (unsigned char *) m;
                    745:                                z = i2d_X509_NAME(X509_get_subject_name(x), &d);
                    746:                                BIO_printf(STDout, "unsigned char XXX_subject_name[%d]={\n", z);
                    747:                                d = (unsigned char *) m;
                    748:                                for (y = 0; y < z; y++) {
                    749:                                        BIO_printf(STDout, "0x%02X,", d[y]);
                    750:                                        if ((y & 0x0f) == 0x0f)
                    751:                                                BIO_printf(STDout, "\n");
                    752:                                }
                    753:                                if (y % 16 != 0)
                    754:                                        BIO_printf(STDout, "\n");
                    755:                                BIO_printf(STDout, "};\n");
                    756:
                    757:                                z = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);
                    758:                                BIO_printf(STDout, "unsigned char XXX_public_key[%d]={\n", z);
                    759:                                d = (unsigned char *) m;
                    760:                                for (y = 0; y < z; y++) {
                    761:                                        BIO_printf(STDout, "0x%02X,", d[y]);
                    762:                                        if ((y & 0x0f) == 0x0f)
                    763:                                                BIO_printf(STDout, "\n");
                    764:                                }
                    765:                                if (y % 16 != 0)
                    766:                                        BIO_printf(STDout, "\n");
                    767:                                BIO_printf(STDout, "};\n");
                    768:
                    769:                                z = i2d_X509(x, &d);
                    770:                                BIO_printf(STDout, "unsigned char XXX_certificate[%d]={\n", z);
                    771:                                d = (unsigned char *) m;
                    772:                                for (y = 0; y < z; y++) {
                    773:                                        BIO_printf(STDout, "0x%02X,", d[y]);
                    774:                                        if ((y & 0x0f) == 0x0f)
                    775:                                                BIO_printf(STDout, "\n");
                    776:                                }
                    777:                                if (y % 16 != 0)
                    778:                                        BIO_printf(STDout, "\n");
                    779:                                BIO_printf(STDout, "};\n");
                    780:
                    781:                                free(m);
                    782:                        } else if (text == i) {
                    783:                                X509_print_ex(STDout, x, nmflag, certflag);
                    784:                        } else if (startdate == i) {
                    785:                                BIO_puts(STDout, "notBefore=");
                    786:                                ASN1_TIME_print(STDout, X509_get_notBefore(x));
                    787:                                BIO_puts(STDout, "\n");
                    788:                        } else if (enddate == i) {
                    789:                                BIO_puts(STDout, "notAfter=");
                    790:                                ASN1_TIME_print(STDout, X509_get_notAfter(x));
                    791:                                BIO_puts(STDout, "\n");
                    792:                        } else if (fingerprint == i) {
                    793:                                int j;
                    794:                                unsigned int n;
                    795:                                unsigned char md[EVP_MAX_MD_SIZE];
                    796:                                const EVP_MD *fdig = digest;
                    797:
                    798:                                if (!fdig)
                    799:                                        fdig = EVP_sha1();
                    800:
                    801:                                if (!X509_digest(x, fdig, md, &n)) {
                    802:                                        BIO_printf(bio_err, "out of memory\n");
                    803:                                        goto end;
                    804:                                }
                    805:                                BIO_printf(STDout, "%s Fingerprint=",
                    806:                                    OBJ_nid2sn(EVP_MD_type(fdig)));
                    807:                                for (j = 0; j < (int) n; j++) {
                    808:                                        BIO_printf(STDout, "%02X%c", md[j],
                    809:                                            (j + 1 == (int)n) ? '\n' : ':');
                    810:                                }
                    811:                        }
                    812:                        /* should be in the library */
                    813:                        else if ((sign_flag == i) && (x509req == 0)) {
                    814:                                BIO_printf(bio_err, "Getting Private key\n");
                    815:                                if (Upkey == NULL) {
                    816:                                        Upkey = load_key(bio_err,
                    817:                                            keyfile, keyformat, 0,
                    818:                                            passin, e, "Private key");
                    819:                                        if (Upkey == NULL)
                    820:                                                goto end;
                    821:                                }
                    822:                                if (!sign(x, Upkey, days, clrext, digest,
                    823:                                    extconf, extsect))
                    824:                                        goto end;
                    825:                        } else if (CA_flag == i) {
                    826:                                BIO_printf(bio_err, "Getting CA Private Key\n");
                    827:                                if (CAkeyfile != NULL) {
                    828:                                        CApkey = load_key(bio_err,
                    829:                                            CAkeyfile, CAkeyformat,
                    830:                                            0, passin, e,
                    831:                                            "CA Private Key");
                    832:                                        if (CApkey == NULL)
                    833:                                                goto end;
                    834:                                }
                    835:                                if (!x509_certify(ctx, CAfile, digest, x, xca,
                    836:                                    CApkey, sigopts,
                    837:                                    CAserial, CA_createserial, days, clrext,
                    838:                                    extconf, extsect, sno))
                    839:                                        goto end;
                    840:                        } else if (x509req == i) {
                    841:                                EVP_PKEY *pk;
                    842:
                    843:                                BIO_printf(bio_err, "Getting request Private Key\n");
                    844:                                if (keyfile == NULL) {
                    845:                                        BIO_printf(bio_err, "no request key file specified\n");
                    846:                                        goto end;
                    847:                                } else {
                    848:                                        pk = load_key(bio_err,
                    849:                                            keyfile, keyformat, 0,
                    850:                                            passin, e, "request key");
                    851:                                        if (pk == NULL)
                    852:                                                goto end;
                    853:                                }
                    854:
                    855:                                BIO_printf(bio_err, "Generating certificate request\n");
                    856:
                    857:                                rq = X509_to_X509_REQ(x, pk, digest);
                    858:                                EVP_PKEY_free(pk);
                    859:                                if (rq == NULL) {
                    860:                                        ERR_print_errors(bio_err);
                    861:                                        goto end;
                    862:                                }
                    863:                                if (!noout) {
                    864:                                        X509_REQ_print(out, rq);
                    865:                                        PEM_write_bio_X509_REQ(out, rq);
                    866:                                }
                    867:                                noout = 1;
                    868:                        } else if (ocspid == i) {
                    869:                                X509_ocspid_print(out, x);
                    870:                        }
                    871:                }
                    872:        }
                    873:        if (checkend) {
                    874:                time_t tcheck = time(NULL) + checkoffset;
                    875:
                    876:                if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {
                    877:                        BIO_printf(out, "Certificate will expire\n");
                    878:                        ret = 1;
                    879:                } else {
                    880:                        BIO_printf(out, "Certificate will not expire\n");
                    881:                        ret = 0;
                    882:                }
                    883:                goto end;
                    884:        }
                    885:        if (noout) {
                    886:                ret = 0;
                    887:                goto end;
                    888:        }
                    889:        if (outformat == FORMAT_ASN1)
                    890:                i = i2d_X509_bio(out, x);
                    891:        else if (outformat == FORMAT_PEM) {
                    892:                if (trustout)
                    893:                        i = PEM_write_bio_X509_AUX(out, x);
                    894:                else
                    895:                        i = PEM_write_bio_X509(out, x);
                    896:        } else if (outformat == FORMAT_NETSCAPE) {
                    897:                NETSCAPE_X509 nx;
                    898:                ASN1_OCTET_STRING hdr;
                    899:
                    900:                hdr.data = (unsigned char *) NETSCAPE_CERT_HDR;
                    901:                hdr.length = strlen(NETSCAPE_CERT_HDR);
                    902:                nx.header = &hdr;
                    903:                nx.cert = x;
                    904:
                    905:                i = ASN1_item_i2d_bio(ASN1_ITEM_rptr(NETSCAPE_X509), out, &nx);
                    906:        } else {
                    907:                BIO_printf(bio_err, "bad output format specified for outfile\n");
                    908:                goto end;
                    909:        }
                    910:        if (!i) {
                    911:                BIO_printf(bio_err, "unable to write certificate\n");
                    912:                ERR_print_errors(bio_err);
                    913:                goto end;
                    914:        }
                    915:        ret = 0;
                    916:
                    917: end:
                    918:        OBJ_cleanup();
                    919:        NCONF_free(extconf);
                    920:        BIO_free_all(out);
                    921:        BIO_free_all(STDout);
                    922:        X509_STORE_free(ctx);
                    923:        X509_REQ_free(req);
                    924:        X509_free(x);
                    925:        X509_free(xca);
                    926:        EVP_PKEY_free(Upkey);
                    927:        EVP_PKEY_free(CApkey);
                    928:        if (sigopts)
                    929:                sk_OPENSSL_STRING_free(sigopts);
                    930:        X509_REQ_free(rq);
                    931:        ASN1_INTEGER_free(sno);
                    932:        sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
                    933:        sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
                    934:        free(passin);
                    935:
                    936:        return (ret);
                    937: }
                    938:
                    939: static ASN1_INTEGER *
                    940: x509_load_serial(char *CAfile, char *serialfile, int create)
                    941: {
                    942:        char *buf = NULL, *p;
                    943:        ASN1_INTEGER *bs = NULL;
                    944:        BIGNUM *serial = NULL;
                    945:        size_t len;
                    946:
                    947:        len = ((serialfile == NULL) ? (strlen(CAfile) + strlen(POSTFIX) + 1) :
                    948:            (strlen(serialfile))) + 1;
                    949:        buf = malloc(len);
                    950:        if (buf == NULL) {
                    951:                BIO_printf(bio_err, "out of mem\n");
                    952:                goto end;
                    953:        }
                    954:        if (serialfile == NULL) {
                    955:                strlcpy(buf, CAfile, len);
                    956:                for (p = buf; *p; p++)
                    957:                        if (*p == '.') {
                    958:                                *p = '\0';
                    959:                                break;
                    960:                        }
                    961:                strlcat(buf, POSTFIX, len);
                    962:        } else
                    963:                strlcpy(buf, serialfile, len);
                    964:
                    965:        serial = load_serial(buf, create, NULL);
                    966:        if (serial == NULL)
                    967:                goto end;
                    968:
                    969:        if (!BN_add_word(serial, 1)) {
                    970:                BIO_printf(bio_err, "add_word failure\n");
                    971:                goto end;
                    972:        }
                    973:        if (!save_serial(buf, NULL, serial, &bs))
                    974:                goto end;
                    975:
                    976: end:
                    977:        free(buf);
                    978:        BN_free(serial);
                    979:
                    980:        return bs;
                    981: }
                    982:
                    983: static int
                    984: x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest, X509 *x,
                    985:     X509 *xca, EVP_PKEY *pkey, STACK_OF(OPENSSL_STRING) *sigopts,
                    986:     char *serialfile, int create, int days, int clrext, CONF *conf,
                    987:     char *section, ASN1_INTEGER *sno)
                    988: {
                    989:        int ret = 0;
                    990:        ASN1_INTEGER *bs = NULL;
                    991:        X509_STORE_CTX xsc;
                    992:        EVP_PKEY *upkey;
                    993:
                    994:        upkey = X509_get_pubkey(xca);
                    995:        EVP_PKEY_copy_parameters(upkey, pkey);
                    996:        EVP_PKEY_free(upkey);
                    997:
                    998:        if (!X509_STORE_CTX_init(&xsc, ctx, x, NULL)) {
                    999:                BIO_printf(bio_err, "Error initialising X509 store\n");
                   1000:                goto end;
                   1001:        }
                   1002:        if (sno)
                   1003:                bs = sno;
                   1004:        else if (!(bs = x509_load_serial(CAfile, serialfile, create)))
                   1005:                goto end;
                   1006:
                   1007: /*     if (!X509_STORE_add_cert(ctx,x)) goto end;*/
                   1008:
                   1009:        /*
                   1010:         * NOTE: this certificate can/should be self signed, unless it was a
                   1011:         * certificate request in which case it is not.
                   1012:         */
                   1013:        X509_STORE_CTX_set_cert(&xsc, x);
                   1014:        X509_STORE_CTX_set_flags(&xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
                   1015:        if (!reqfile && X509_verify_cert(&xsc) <= 0)
                   1016:                goto end;
                   1017:
                   1018:        if (!X509_check_private_key(xca, pkey)) {
                   1019:                BIO_printf(bio_err, "CA certificate and CA private key do not match\n");
                   1020:                goto end;
                   1021:        }
                   1022:        if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
                   1023:                goto end;
                   1024:        if (!X509_set_serialNumber(x, bs))
                   1025:                goto end;
                   1026:
                   1027:        if (X509_gmtime_adj(X509_get_notBefore(x), 0L) == NULL)
                   1028:                goto end;
                   1029:
                   1030:        /* hardwired expired */
                   1031:        if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL)
                   1032:                goto end;
                   1033:
                   1034:        if (clrext) {
                   1035:                while (X509_get_ext_count(x) > 0)
                   1036:                        X509_delete_ext(x, 0);
                   1037:        }
                   1038:        if (conf) {
                   1039:                X509V3_CTX ctx2;
                   1040:                X509_set_version(x, 2); /* version 3 certificate */
                   1041:                X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
                   1042:                X509V3_set_nconf(&ctx2, conf);
                   1043:                if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
                   1044:                        goto end;
                   1045:        }
                   1046:        if (!do_X509_sign(bio_err, x, pkey, digest, sigopts))
                   1047:                goto end;
                   1048:        ret = 1;
                   1049: end:
                   1050:        X509_STORE_CTX_cleanup(&xsc);
                   1051:        if (!ret)
                   1052:                ERR_print_errors(bio_err);
                   1053:        if (!sno)
                   1054:                ASN1_INTEGER_free(bs);
                   1055:        return ret;
                   1056: }
                   1057:
                   1058: static int
                   1059: callb(int ok, X509_STORE_CTX *ctx)
                   1060: {
                   1061:        int err;
                   1062:        X509 *err_cert;
                   1063:
                   1064:        /*
                   1065:         * it is ok to use a self signed certificate This case will catch
                   1066:         * both the initial ok == 0 and the final ok == 1 calls to this
                   1067:         * function
                   1068:         */
                   1069:        err = X509_STORE_CTX_get_error(ctx);
                   1070:        if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
                   1071:                return 1;
                   1072:
                   1073:        /*
                   1074:         * BAD we should have gotten an error.  Normally if everything worked
                   1075:         * X509_STORE_CTX_get_error(ctx) will still be set to
                   1076:         * DEPTH_ZERO_SELF_....
                   1077:         */
                   1078:        if (ok) {
                   1079:                BIO_printf(bio_err, "error with certificate to be certified - should be self signed\n");
                   1080:                return 0;
                   1081:        } else {
                   1082:                err_cert = X509_STORE_CTX_get_current_cert(ctx);
                   1083:                print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
                   1084:                BIO_printf(bio_err, "error with certificate - error %d at depth %d\n%s\n",
                   1085:                    err, X509_STORE_CTX_get_error_depth(ctx),
                   1086:                    X509_verify_cert_error_string(err));
                   1087:                return 1;
                   1088:        }
                   1089: }
                   1090:
                   1091: /* self sign */
                   1092: static int
                   1093: sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest,
                   1094:     CONF *conf, char *section)
                   1095: {
                   1096:
                   1097:        EVP_PKEY *pktmp;
                   1098:
                   1099:        pktmp = X509_get_pubkey(x);
                   1100:        EVP_PKEY_copy_parameters(pktmp, pkey);
                   1101:        EVP_PKEY_save_parameters(pktmp, 1);
                   1102:        EVP_PKEY_free(pktmp);
                   1103:
                   1104:        if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
                   1105:                goto err;
                   1106:        if (X509_gmtime_adj(X509_get_notBefore(x), 0) == NULL)
                   1107:                goto err;
                   1108:
                   1109:        /* Lets just make it 12:00am GMT, Jan 1 1970 */
                   1110:        /* memcpy(x->cert_info->validity->notBefore,"700101120000Z",13); */
                   1111:        /* 28 days to be certified */
                   1112:
                   1113:        if (X509_gmtime_adj(X509_get_notAfter(x),
                   1114:            (long) 60 * 60 * 24 * days) == NULL)
                   1115:                goto err;
                   1116:
                   1117:        if (!X509_set_pubkey(x, pkey))
                   1118:                goto err;
                   1119:        if (clrext) {
                   1120:                while (X509_get_ext_count(x) > 0)
                   1121:                        X509_delete_ext(x, 0);
                   1122:        }
                   1123:        if (conf) {
                   1124:                X509V3_CTX ctx;
                   1125:                X509_set_version(x, 2); /* version 3 certificate */
                   1126:                X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
                   1127:                X509V3_set_nconf(&ctx, conf);
                   1128:                if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
                   1129:                        goto err;
                   1130:        }
                   1131:        if (!X509_sign(x, pkey, digest))
                   1132:                goto err;
                   1133:        return 1;
                   1134:
                   1135: err:
                   1136:        ERR_print_errors(bio_err);
                   1137:        return 0;
                   1138: }
                   1139:
                   1140: static int
                   1141: purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
                   1142: {
                   1143:        int id, i, idret;
                   1144:        char *pname;
                   1145:
                   1146:        id = X509_PURPOSE_get_id(pt);
                   1147:        pname = X509_PURPOSE_get0_name(pt);
                   1148:        for (i = 0; i < 2; i++) {
                   1149:                idret = X509_check_purpose(cert, id, i);
                   1150:                BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
                   1151:                if (idret == 1)
                   1152:                        BIO_printf(bio, "Yes\n");
                   1153:                else if (idret == 0)
                   1154:                        BIO_printf(bio, "No\n");
                   1155:                else
                   1156:                        BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
                   1157:        }
                   1158:        return 1;
                   1159: }