[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.1

1.1     ! jsing       1: /* $OpenBSD: x509.c,v 1.49 2014/07/14 00:35:10 deraadt Exp $ */
        !             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",
        !           138:        " -md2/-md5/-sha1/-mdc2 - digest to use\n",
        !           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;
        !           307:                        if (!(sno = s2i_ASN1_INTEGER(NULL, *(++argv))))
        !           308:                                goto bad;
        !           309:                } else if (strcmp(*argv, "-addtrust") == 0) {
        !           310:                        if (--argc < 1)
        !           311:                                goto bad;
        !           312:                        if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) {
        !           313:                                BIO_printf(bio_err,
        !           314:                                    "Invalid trust object value %s\n", *argv);
        !           315:                                goto bad;
        !           316:                        }
        !           317:                        if (!trust)
        !           318:                                trust = sk_ASN1_OBJECT_new_null();
        !           319:                        sk_ASN1_OBJECT_push(trust, objtmp);
        !           320:                        trustout = 1;
        !           321:                } else if (strcmp(*argv, "-addreject") == 0) {
        !           322:                        if (--argc < 1)
        !           323:                                goto bad;
        !           324:                        if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) {
        !           325:                                BIO_printf(bio_err,
        !           326:                                    "Invalid reject object value %s\n", *argv);
        !           327:                                goto bad;
        !           328:                        }
        !           329:                        if (!reject)
        !           330:                                reject = sk_ASN1_OBJECT_new_null();
        !           331:                        sk_ASN1_OBJECT_push(reject, objtmp);
        !           332:                        trustout = 1;
        !           333:                } else if (strcmp(*argv, "-setalias") == 0) {
        !           334:                        if (--argc < 1)
        !           335:                                goto bad;
        !           336:                        alias = *(++argv);
        !           337:                        trustout = 1;
        !           338:                } else if (strcmp(*argv, "-certopt") == 0) {
        !           339:                        if (--argc < 1)
        !           340:                                goto bad;
        !           341:                        if (!set_cert_ex(&certflag, *(++argv)))
        !           342:                                goto bad;
        !           343:                } else if (strcmp(*argv, "-nameopt") == 0) {
        !           344:                        if (--argc < 1)
        !           345:                                goto bad;
        !           346:                        if (!set_name_ex(&nmflag, *(++argv)))
        !           347:                                goto bad;
        !           348:                }
        !           349: #ifndef OPENSSL_NO_ENGINE
        !           350:                else if (strcmp(*argv, "-engine") == 0) {
        !           351:                        if (--argc < 1)
        !           352:                                goto bad;
        !           353:                        engine = *(++argv);
        !           354:                }
        !           355: #endif
        !           356:                else if (strcmp(*argv, "-C") == 0)
        !           357:                        C = ++num;
        !           358:                else if (strcmp(*argv, "-email") == 0)
        !           359:                        email = ++num;
        !           360:                else if (strcmp(*argv, "-ocsp_uri") == 0)
        !           361:                        ocsp_uri = ++num;
        !           362:                else if (strcmp(*argv, "-serial") == 0)
        !           363:                        serial = ++num;
        !           364:                else if (strcmp(*argv, "-next_serial") == 0)
        !           365:                        next_serial = ++num;
        !           366:                else if (strcmp(*argv, "-modulus") == 0)
        !           367:                        modulus = ++num;
        !           368:                else if (strcmp(*argv, "-pubkey") == 0)
        !           369:                        pubkey = ++num;
        !           370:                else if (strcmp(*argv, "-x509toreq") == 0)
        !           371:                        x509req = ++num;
        !           372:                else if (strcmp(*argv, "-text") == 0)
        !           373:                        text = ++num;
        !           374:                else if (strcmp(*argv, "-hash") == 0 ||
        !           375:                    strcmp(*argv, "-subject_hash") == 0)
        !           376:                        subject_hash = ++num;
        !           377: #ifndef OPENSSL_NO_MD5
        !           378:                else if (strcmp(*argv, "-subject_hash_old") == 0)
        !           379:                        subject_hash_old = ++num;
        !           380: #endif
        !           381:                else if (strcmp(*argv, "-issuer_hash") == 0)
        !           382:                        issuer_hash = ++num;
        !           383: #ifndef OPENSSL_NO_MD5
        !           384:                else if (strcmp(*argv, "-issuer_hash_old") == 0)
        !           385:                        issuer_hash_old = ++num;
        !           386: #endif
        !           387:                else if (strcmp(*argv, "-subject") == 0)
        !           388:                        subject = ++num;
        !           389:                else if (strcmp(*argv, "-issuer") == 0)
        !           390:                        issuer = ++num;
        !           391:                else if (strcmp(*argv, "-fingerprint") == 0)
        !           392:                        fingerprint = ++num;
        !           393:                else if (strcmp(*argv, "-dates") == 0) {
        !           394:                        startdate = ++num;
        !           395:                        enddate = ++num;
        !           396:                } else if (strcmp(*argv, "-purpose") == 0)
        !           397:                        pprint = ++num;
        !           398:                else if (strcmp(*argv, "-startdate") == 0)
        !           399:                        startdate = ++num;
        !           400:                else if (strcmp(*argv, "-enddate") == 0)
        !           401:                        enddate = ++num;
        !           402:                else if (strcmp(*argv, "-checkend") == 0) {
        !           403:                        if (--argc < 1)
        !           404:                                goto bad;
        !           405:                        checkoffset = strtonum(*(++argv), 0, INT_MAX, &errstr);
        !           406:                        if (errstr) {
        !           407:                                BIO_printf(bio_err, "checkend unusable: %s\n", errstr);
        !           408:                                goto bad;
        !           409:                        }
        !           410:                        checkend = 1;
        !           411:                } else if (strcmp(*argv, "-noout") == 0)
        !           412:                        noout = ++num;
        !           413:                else if (strcmp(*argv, "-trustout") == 0)
        !           414:                        trustout = 1;
        !           415:                else if (strcmp(*argv, "-clrtrust") == 0)
        !           416:                        clrtrust = ++num;
        !           417:                else if (strcmp(*argv, "-clrreject") == 0)
        !           418:                        clrreject = ++num;
        !           419:                else if (strcmp(*argv, "-alias") == 0)
        !           420:                        aliasout = ++num;
        !           421:                else if (strcmp(*argv, "-CAcreateserial") == 0)
        !           422:                        CA_createserial = ++num;
        !           423:                else if (strcmp(*argv, "-clrext") == 0)
        !           424:                        clrext = 1;
        !           425:                else if (strcmp(*argv, "-ocspid") == 0)
        !           426:                        ocspid = ++num;
        !           427:                else if ((md_alg = EVP_get_digestbyname(*argv + 1))) {
        !           428:                        /* ok */
        !           429:                        digest = md_alg;
        !           430:                } else {
        !           431:                        BIO_printf(bio_err, "unknown option %s\n", *argv);
        !           432:                        badops = 1;
        !           433:                        break;
        !           434:                }
        !           435:                argc--;
        !           436:                argv++;
        !           437:        }
        !           438:
        !           439:        if (badops) {
        !           440: bad:
        !           441:                for (pp = x509_usage; (*pp != NULL); pp++)
        !           442:                        BIO_printf(bio_err, "%s", *pp);
        !           443:                goto end;
        !           444:        }
        !           445: #ifndef OPENSSL_NO_ENGINE
        !           446:        e = setup_engine(bio_err, engine, 0);
        !           447: #endif
        !           448:
        !           449:        ERR_load_crypto_strings();
        !           450:
        !           451:        if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
        !           452:                BIO_printf(bio_err, "Error getting password\n");
        !           453:                goto end;
        !           454:        }
        !           455:        if (!X509_STORE_set_default_paths(ctx)) {
        !           456:                ERR_print_errors(bio_err);
        !           457:                goto end;
        !           458:        }
        !           459:        if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {
        !           460:                CAkeyfile = CAfile;
        !           461:        } else if ((CA_flag) && (CAkeyfile == NULL)) {
        !           462:                BIO_printf(bio_err,
        !           463:                    "need to specify a CAkey if using the CA command\n");
        !           464:                goto end;
        !           465:        }
        !           466:        if (extfile) {
        !           467:                long errorline = -1;
        !           468:                X509V3_CTX ctx2;
        !           469:                extconf = NCONF_new(NULL);
        !           470:                if (!NCONF_load(extconf, extfile, &errorline)) {
        !           471:                        if (errorline <= 0)
        !           472:                                BIO_printf(bio_err,
        !           473:                                    "error loading the config file '%s'\n",
        !           474:                                    extfile);
        !           475:                        else
        !           476:                                BIO_printf(bio_err,
        !           477:                                    "error on line %ld of config file '%s'\n",
        !           478:                                    errorline, extfile);
        !           479:                        goto end;
        !           480:                }
        !           481:                if (!extsect) {
        !           482:                        extsect = NCONF_get_string(extconf, "default",
        !           483:                            "extensions");
        !           484:                        if (!extsect) {
        !           485:                                ERR_clear_error();
        !           486:                                extsect = "default";
        !           487:                        }
        !           488:                }
        !           489:                X509V3_set_ctx_test(&ctx2);
        !           490:                X509V3_set_nconf(&ctx2, extconf);
        !           491:                if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
        !           492:                        BIO_printf(bio_err,
        !           493:                            "Error Loading extension section %s\n",
        !           494:                            extsect);
        !           495:                        ERR_print_errors(bio_err);
        !           496:                        goto end;
        !           497:                }
        !           498:        }
        !           499:        if (reqfile) {
        !           500:                EVP_PKEY *pkey;
        !           501:                BIO *in;
        !           502:
        !           503:                if (!sign_flag && !CA_flag) {
        !           504:                        BIO_printf(bio_err, "We need a private key to sign with\n");
        !           505:                        goto end;
        !           506:                }
        !           507:                in = BIO_new(BIO_s_file());
        !           508:                if (in == NULL) {
        !           509:                        ERR_print_errors(bio_err);
        !           510:                        goto end;
        !           511:                }
        !           512:                if (infile == NULL)
        !           513:                        BIO_set_fp(in, stdin, BIO_NOCLOSE | BIO_FP_TEXT);
        !           514:                else {
        !           515:                        if (BIO_read_filename(in, infile) <= 0) {
        !           516:                                perror(infile);
        !           517:                                BIO_free(in);
        !           518:                                goto end;
        !           519:                        }
        !           520:                }
        !           521:                req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
        !           522:                BIO_free(in);
        !           523:
        !           524:                if (req == NULL) {
        !           525:                        ERR_print_errors(bio_err);
        !           526:                        goto end;
        !           527:                }
        !           528:                if ((req->req_info == NULL) ||
        !           529:                    (req->req_info->pubkey == NULL) ||
        !           530:                    (req->req_info->pubkey->public_key == NULL) ||
        !           531:                    (req->req_info->pubkey->public_key->data == NULL)) {
        !           532:                        BIO_printf(bio_err, "The certificate request appears to corrupted\n");
        !           533:                        BIO_printf(bio_err, "It does not contain a public key\n");
        !           534:                        goto end;
        !           535:                }
        !           536:                if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {
        !           537:                        BIO_printf(bio_err, "error unpacking public key\n");
        !           538:                        goto end;
        !           539:                }
        !           540:                i = X509_REQ_verify(req, pkey);
        !           541:                EVP_PKEY_free(pkey);
        !           542:                if (i < 0) {
        !           543:                        BIO_printf(bio_err, "Signature verification error\n");
        !           544:                        ERR_print_errors(bio_err);
        !           545:                        goto end;
        !           546:                }
        !           547:                if (i == 0) {
        !           548:                        BIO_printf(bio_err, "Signature did not match the certificate request\n");
        !           549:                        goto end;
        !           550:                } else
        !           551:                        BIO_printf(bio_err, "Signature ok\n");
        !           552:
        !           553:                print_name(bio_err, "subject=", X509_REQ_get_subject_name(req), nmflag);
        !           554:
        !           555:                if ((x = X509_new()) == NULL)
        !           556:                        goto end;
        !           557:
        !           558:                if (sno == NULL) {
        !           559:                        sno = ASN1_INTEGER_new();
        !           560:                        if (!sno || !rand_serial(NULL, sno))
        !           561:                                goto end;
        !           562:                        if (!X509_set_serialNumber(x, sno))
        !           563:                                goto end;
        !           564:                        ASN1_INTEGER_free(sno);
        !           565:                        sno = NULL;
        !           566:                } else if (!X509_set_serialNumber(x, sno))
        !           567:                        goto end;
        !           568:
        !           569:                if (!X509_set_issuer_name(x, req->req_info->subject))
        !           570:                        goto end;
        !           571:                if (!X509_set_subject_name(x, req->req_info->subject))
        !           572:                        goto end;
        !           573:
        !           574:                X509_gmtime_adj(X509_get_notBefore(x), 0);
        !           575:                X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL);
        !           576:
        !           577:                pkey = X509_REQ_get_pubkey(req);
        !           578:                X509_set_pubkey(x, pkey);
        !           579:                EVP_PKEY_free(pkey);
        !           580:        } else
        !           581:                x = load_cert(bio_err, infile, informat, NULL, e, "Certificate");
        !           582:
        !           583:        if (x == NULL)
        !           584:                goto end;
        !           585:        if (CA_flag) {
        !           586:                xca = load_cert(bio_err, CAfile, CAformat, NULL, e, "CA Certificate");
        !           587:                if (xca == NULL)
        !           588:                        goto end;
        !           589:        }
        !           590:        if (!noout || text || next_serial) {
        !           591:                OBJ_create("2.99999.3",
        !           592:                    "SET.ex3", "SET x509v3 extension 3");
        !           593:
        !           594:                out = BIO_new(BIO_s_file());
        !           595:                if (out == NULL) {
        !           596:                        ERR_print_errors(bio_err);
        !           597:                        goto end;
        !           598:                }
        !           599:                if (outfile == NULL) {
        !           600:                        BIO_set_fp(out, stdout, BIO_NOCLOSE);
        !           601:                } else {
        !           602:                        if (BIO_write_filename(out, outfile) <= 0) {
        !           603:                                perror(outfile);
        !           604:                                goto end;
        !           605:                        }
        !           606:                }
        !           607:        }
        !           608:        if (alias)
        !           609:                X509_alias_set1(x, (unsigned char *) alias, -1);
        !           610:
        !           611:        if (clrtrust)
        !           612:                X509_trust_clear(x);
        !           613:        if (clrreject)
        !           614:                X509_reject_clear(x);
        !           615:
        !           616:        if (trust) {
        !           617:                for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
        !           618:                        objtmp = sk_ASN1_OBJECT_value(trust, i);
        !           619:                        X509_add1_trust_object(x, objtmp);
        !           620:                }
        !           621:        }
        !           622:        if (reject) {
        !           623:                for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
        !           624:                        objtmp = sk_ASN1_OBJECT_value(reject, i);
        !           625:                        X509_add1_reject_object(x, objtmp);
        !           626:                }
        !           627:        }
        !           628:        if (num) {
        !           629:                for (i = 1; i <= num; i++) {
        !           630:                        if (issuer == i) {
        !           631:                                print_name(STDout, "issuer= ",
        !           632:                                    X509_get_issuer_name(x), nmflag);
        !           633:                        } else if (subject == i) {
        !           634:                                print_name(STDout, "subject= ",
        !           635:                                    X509_get_subject_name(x), nmflag);
        !           636:                        } else if (serial == i) {
        !           637:                                BIO_printf(STDout, "serial=");
        !           638:                                i2a_ASN1_INTEGER(STDout,
        !           639:                                    X509_get_serialNumber(x));
        !           640:                                BIO_printf(STDout, "\n");
        !           641:                        } else if (next_serial == i) {
        !           642:                                BIGNUM *bnser;
        !           643:                                ASN1_INTEGER *ser;
        !           644:                                ser = X509_get_serialNumber(x);
        !           645:                                bnser = ASN1_INTEGER_to_BN(ser, NULL);
        !           646:                                if (!bnser)
        !           647:                                        goto end;
        !           648:                                if (!BN_add_word(bnser, 1))
        !           649:                                        goto end;
        !           650:                                ser = BN_to_ASN1_INTEGER(bnser, NULL);
        !           651:                                if (!ser)
        !           652:                                        goto end;
        !           653:                                BN_free(bnser);
        !           654:                                i2a_ASN1_INTEGER(out, ser);
        !           655:                                ASN1_INTEGER_free(ser);
        !           656:                                BIO_puts(out, "\n");
        !           657:                        } else if ((email == i) || (ocsp_uri == i)) {
        !           658:                                int j;
        !           659:                                STACK_OF(OPENSSL_STRING) *emlst;
        !           660:                                if (email == i)
        !           661:                                        emlst = X509_get1_email(x);
        !           662:                                else
        !           663:                                        emlst = X509_get1_ocsp(x);
        !           664:                                for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
        !           665:                                        BIO_printf(STDout, "%s\n",
        !           666:                                            sk_OPENSSL_STRING_value(emlst, j));
        !           667:                                X509_email_free(emlst);
        !           668:                        } else if (aliasout == i) {
        !           669:                                unsigned char *alstr;
        !           670:                                alstr = X509_alias_get0(x, NULL);
        !           671:                                if (alstr)
        !           672:                                        BIO_printf(STDout, "%s\n", alstr);
        !           673:                                else
        !           674:                                        BIO_puts(STDout, "<No Alias>\n");
        !           675:                        } else if (subject_hash == i) {
        !           676:                                BIO_printf(STDout, "%08lx\n", X509_subject_name_hash(x));
        !           677:                        }
        !           678: #ifndef OPENSSL_NO_MD5
        !           679:                        else if (subject_hash_old == i) {
        !           680:                                BIO_printf(STDout, "%08lx\n", X509_subject_name_hash_old(x));
        !           681:                        }
        !           682: #endif
        !           683:                        else if (issuer_hash == i) {
        !           684:                                BIO_printf(STDout, "%08lx\n", X509_issuer_name_hash(x));
        !           685:                        }
        !           686: #ifndef OPENSSL_NO_MD5
        !           687:                        else if (issuer_hash_old == i) {
        !           688:                                BIO_printf(STDout, "%08lx\n", X509_issuer_name_hash_old(x));
        !           689:                        }
        !           690: #endif
        !           691:                        else if (pprint == i) {
        !           692:                                X509_PURPOSE *ptmp;
        !           693:                                int j;
        !           694:                                BIO_printf(STDout, "Certificate purposes:\n");
        !           695:                                for (j = 0; j < X509_PURPOSE_get_count(); j++) {
        !           696:                                        ptmp = X509_PURPOSE_get0(j);
        !           697:                                        purpose_print(STDout, x, ptmp);
        !           698:                                }
        !           699:                        } else if (modulus == i) {
        !           700:                                EVP_PKEY *pkey;
        !           701:
        !           702:                                pkey = X509_get_pubkey(x);
        !           703:                                if (pkey == NULL) {
        !           704:                                        BIO_printf(bio_err, "Modulus=unavailable\n");
        !           705:                                        ERR_print_errors(bio_err);
        !           706:                                        goto end;
        !           707:                                }
        !           708:                                BIO_printf(STDout, "Modulus=");
        !           709:                                if (pkey->type == EVP_PKEY_RSA)
        !           710:                                        BN_print(STDout, pkey->pkey.rsa->n);
        !           711:                                else
        !           712:                                                if (pkey->type == EVP_PKEY_DSA)
        !           713:                                                        BN_print(STDout, pkey->pkey.dsa->pub_key);
        !           714:                                else
        !           715:                                                BIO_printf(STDout, "Wrong Algorithm type");
        !           716:                                BIO_printf(STDout, "\n");
        !           717:                                EVP_PKEY_free(pkey);
        !           718:                        } else if (pubkey == i) {
        !           719:                                EVP_PKEY *pkey;
        !           720:
        !           721:                                pkey = X509_get_pubkey(x);
        !           722:                                if (pkey == NULL) {
        !           723:                                        BIO_printf(bio_err, "Error getting public key\n");
        !           724:                                        ERR_print_errors(bio_err);
        !           725:                                        goto end;
        !           726:                                }
        !           727:                                PEM_write_bio_PUBKEY(STDout, pkey);
        !           728:                                EVP_PKEY_free(pkey);
        !           729:                        } else if (C == i) {
        !           730:                                unsigned char *d;
        !           731:                                char *m;
        !           732:                                int y, z;
        !           733:
        !           734:                                X509_NAME_oneline(X509_get_subject_name(x),
        !           735:                                    buf, sizeof buf);
        !           736:                                BIO_printf(STDout, "/* subject:%s */\n", buf);
        !           737:                                m = X509_NAME_oneline(
        !           738:                                    X509_get_issuer_name(x), buf,
        !           739:                                    sizeof buf);
        !           740:                                BIO_printf(STDout, "/* issuer :%s */\n", buf);
        !           741:
        !           742:                                z = i2d_X509(x, NULL);
        !           743:                                m = malloc(z);
        !           744:
        !           745:                                d = (unsigned char *) m;
        !           746:                                z = i2d_X509_NAME(X509_get_subject_name(x), &d);
        !           747:                                BIO_printf(STDout, "unsigned char XXX_subject_name[%d]={\n", z);
        !           748:                                d = (unsigned char *) m;
        !           749:                                for (y = 0; y < z; y++) {
        !           750:                                        BIO_printf(STDout, "0x%02X,", d[y]);
        !           751:                                        if ((y & 0x0f) == 0x0f)
        !           752:                                                BIO_printf(STDout, "\n");
        !           753:                                }
        !           754:                                if (y % 16 != 0)
        !           755:                                        BIO_printf(STDout, "\n");
        !           756:                                BIO_printf(STDout, "};\n");
        !           757:
        !           758:                                z = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);
        !           759:                                BIO_printf(STDout, "unsigned char XXX_public_key[%d]={\n", z);
        !           760:                                d = (unsigned char *) m;
        !           761:                                for (y = 0; y < z; y++) {
        !           762:                                        BIO_printf(STDout, "0x%02X,", d[y]);
        !           763:                                        if ((y & 0x0f) == 0x0f)
        !           764:                                                BIO_printf(STDout, "\n");
        !           765:                                }
        !           766:                                if (y % 16 != 0)
        !           767:                                        BIO_printf(STDout, "\n");
        !           768:                                BIO_printf(STDout, "};\n");
        !           769:
        !           770:                                z = i2d_X509(x, &d);
        !           771:                                BIO_printf(STDout, "unsigned char XXX_certificate[%d]={\n", z);
        !           772:                                d = (unsigned char *) m;
        !           773:                                for (y = 0; y < z; y++) {
        !           774:                                        BIO_printf(STDout, "0x%02X,", d[y]);
        !           775:                                        if ((y & 0x0f) == 0x0f)
        !           776:                                                BIO_printf(STDout, "\n");
        !           777:                                }
        !           778:                                if (y % 16 != 0)
        !           779:                                        BIO_printf(STDout, "\n");
        !           780:                                BIO_printf(STDout, "};\n");
        !           781:
        !           782:                                free(m);
        !           783:                        } else if (text == i) {
        !           784:                                X509_print_ex(STDout, x, nmflag, certflag);
        !           785:                        } else if (startdate == i) {
        !           786:                                BIO_puts(STDout, "notBefore=");
        !           787:                                ASN1_TIME_print(STDout, X509_get_notBefore(x));
        !           788:                                BIO_puts(STDout, "\n");
        !           789:                        } else if (enddate == i) {
        !           790:                                BIO_puts(STDout, "notAfter=");
        !           791:                                ASN1_TIME_print(STDout, X509_get_notAfter(x));
        !           792:                                BIO_puts(STDout, "\n");
        !           793:                        } else if (fingerprint == i) {
        !           794:                                int j;
        !           795:                                unsigned int n;
        !           796:                                unsigned char md[EVP_MAX_MD_SIZE];
        !           797:                                const EVP_MD *fdig = digest;
        !           798:
        !           799:                                if (!fdig)
        !           800:                                        fdig = EVP_sha1();
        !           801:
        !           802:                                if (!X509_digest(x, fdig, md, &n)) {
        !           803:                                        BIO_printf(bio_err, "out of memory\n");
        !           804:                                        goto end;
        !           805:                                }
        !           806:                                BIO_printf(STDout, "%s Fingerprint=",
        !           807:                                    OBJ_nid2sn(EVP_MD_type(fdig)));
        !           808:                                for (j = 0; j < (int) n; j++) {
        !           809:                                        BIO_printf(STDout, "%02X%c", md[j],
        !           810:                                            (j + 1 == (int)n) ? '\n' : ':');
        !           811:                                }
        !           812:                        }
        !           813:                        /* should be in the library */
        !           814:                        else if ((sign_flag == i) && (x509req == 0)) {
        !           815:                                BIO_printf(bio_err, "Getting Private key\n");
        !           816:                                if (Upkey == NULL) {
        !           817:                                        Upkey = load_key(bio_err,
        !           818:                                            keyfile, keyformat, 0,
        !           819:                                            passin, e, "Private key");
        !           820:                                        if (Upkey == NULL)
        !           821:                                                goto end;
        !           822:                                }
        !           823:                                if (!sign(x, Upkey, days, clrext, digest,
        !           824:                                    extconf, extsect))
        !           825:                                        goto end;
        !           826:                        } else if (CA_flag == i) {
        !           827:                                BIO_printf(bio_err, "Getting CA Private Key\n");
        !           828:                                if (CAkeyfile != NULL) {
        !           829:                                        CApkey = load_key(bio_err,
        !           830:                                            CAkeyfile, CAkeyformat,
        !           831:                                            0, passin, e,
        !           832:                                            "CA Private Key");
        !           833:                                        if (CApkey == NULL)
        !           834:                                                goto end;
        !           835:                                }
        !           836:                                if (!x509_certify(ctx, CAfile, digest, x, xca,
        !           837:                                    CApkey, sigopts,
        !           838:                                    CAserial, CA_createserial, days, clrext,
        !           839:                                    extconf, extsect, sno))
        !           840:                                        goto end;
        !           841:                        } else if (x509req == i) {
        !           842:                                EVP_PKEY *pk;
        !           843:
        !           844:                                BIO_printf(bio_err, "Getting request Private Key\n");
        !           845:                                if (keyfile == NULL) {
        !           846:                                        BIO_printf(bio_err, "no request key file specified\n");
        !           847:                                        goto end;
        !           848:                                } else {
        !           849:                                        pk = load_key(bio_err,
        !           850:                                            keyfile, keyformat, 0,
        !           851:                                            passin, e, "request key");
        !           852:                                        if (pk == NULL)
        !           853:                                                goto end;
        !           854:                                }
        !           855:
        !           856:                                BIO_printf(bio_err, "Generating certificate request\n");
        !           857:
        !           858:                                rq = X509_to_X509_REQ(x, pk, digest);
        !           859:                                EVP_PKEY_free(pk);
        !           860:                                if (rq == NULL) {
        !           861:                                        ERR_print_errors(bio_err);
        !           862:                                        goto end;
        !           863:                                }
        !           864:                                if (!noout) {
        !           865:                                        X509_REQ_print(out, rq);
        !           866:                                        PEM_write_bio_X509_REQ(out, rq);
        !           867:                                }
        !           868:                                noout = 1;
        !           869:                        } else if (ocspid == i) {
        !           870:                                X509_ocspid_print(out, x);
        !           871:                        }
        !           872:                }
        !           873:        }
        !           874:        if (checkend) {
        !           875:                time_t tcheck = time(NULL) + checkoffset;
        !           876:
        !           877:                if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {
        !           878:                        BIO_printf(out, "Certificate will expire\n");
        !           879:                        ret = 1;
        !           880:                } else {
        !           881:                        BIO_printf(out, "Certificate will not expire\n");
        !           882:                        ret = 0;
        !           883:                }
        !           884:                goto end;
        !           885:        }
        !           886:        if (noout) {
        !           887:                ret = 0;
        !           888:                goto end;
        !           889:        }
        !           890:        if (outformat == FORMAT_ASN1)
        !           891:                i = i2d_X509_bio(out, x);
        !           892:        else if (outformat == FORMAT_PEM) {
        !           893:                if (trustout)
        !           894:                        i = PEM_write_bio_X509_AUX(out, x);
        !           895:                else
        !           896:                        i = PEM_write_bio_X509(out, x);
        !           897:        } else if (outformat == FORMAT_NETSCAPE) {
        !           898:                NETSCAPE_X509 nx;
        !           899:                ASN1_OCTET_STRING hdr;
        !           900:
        !           901:                hdr.data = (unsigned char *) NETSCAPE_CERT_HDR;
        !           902:                hdr.length = strlen(NETSCAPE_CERT_HDR);
        !           903:                nx.header = &hdr;
        !           904:                nx.cert = x;
        !           905:
        !           906:                i = ASN1_item_i2d_bio(ASN1_ITEM_rptr(NETSCAPE_X509), out, &nx);
        !           907:        } else {
        !           908:                BIO_printf(bio_err, "bad output format specified for outfile\n");
        !           909:                goto end;
        !           910:        }
        !           911:        if (!i) {
        !           912:                BIO_printf(bio_err, "unable to write certificate\n");
        !           913:                ERR_print_errors(bio_err);
        !           914:                goto end;
        !           915:        }
        !           916:        ret = 0;
        !           917:
        !           918: end:
        !           919:        OBJ_cleanup();
        !           920:        NCONF_free(extconf);
        !           921:        BIO_free_all(out);
        !           922:        BIO_free_all(STDout);
        !           923:        X509_STORE_free(ctx);
        !           924:        X509_REQ_free(req);
        !           925:        X509_free(x);
        !           926:        X509_free(xca);
        !           927:        EVP_PKEY_free(Upkey);
        !           928:        EVP_PKEY_free(CApkey);
        !           929:        if (sigopts)
        !           930:                sk_OPENSSL_STRING_free(sigopts);
        !           931:        X509_REQ_free(rq);
        !           932:        ASN1_INTEGER_free(sno);
        !           933:        sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
        !           934:        sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
        !           935:        free(passin);
        !           936:
        !           937:        return (ret);
        !           938: }
        !           939:
        !           940: static ASN1_INTEGER *
        !           941: x509_load_serial(char *CAfile, char *serialfile, int create)
        !           942: {
        !           943:        char *buf = NULL, *p;
        !           944:        ASN1_INTEGER *bs = NULL;
        !           945:        BIGNUM *serial = NULL;
        !           946:        size_t len;
        !           947:
        !           948:        len = ((serialfile == NULL) ? (strlen(CAfile) + strlen(POSTFIX) + 1) :
        !           949:            (strlen(serialfile))) + 1;
        !           950:        buf = malloc(len);
        !           951:        if (buf == NULL) {
        !           952:                BIO_printf(bio_err, "out of mem\n");
        !           953:                goto end;
        !           954:        }
        !           955:        if (serialfile == NULL) {
        !           956:                strlcpy(buf, CAfile, len);
        !           957:                for (p = buf; *p; p++)
        !           958:                        if (*p == '.') {
        !           959:                                *p = '\0';
        !           960:                                break;
        !           961:                        }
        !           962:                strlcat(buf, POSTFIX, len);
        !           963:        } else
        !           964:                strlcpy(buf, serialfile, len);
        !           965:
        !           966:        serial = load_serial(buf, create, NULL);
        !           967:        if (serial == NULL)
        !           968:                goto end;
        !           969:
        !           970:        if (!BN_add_word(serial, 1)) {
        !           971:                BIO_printf(bio_err, "add_word failure\n");
        !           972:                goto end;
        !           973:        }
        !           974:        if (!save_serial(buf, NULL, serial, &bs))
        !           975:                goto end;
        !           976:
        !           977: end:
        !           978:        free(buf);
        !           979:        BN_free(serial);
        !           980:
        !           981:        return bs;
        !           982: }
        !           983:
        !           984: static int
        !           985: x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest, X509 *x,
        !           986:     X509 *xca, EVP_PKEY *pkey, STACK_OF(OPENSSL_STRING) *sigopts,
        !           987:     char *serialfile, int create, int days, int clrext, CONF *conf,
        !           988:     char *section, ASN1_INTEGER *sno)
        !           989: {
        !           990:        int ret = 0;
        !           991:        ASN1_INTEGER *bs = NULL;
        !           992:        X509_STORE_CTX xsc;
        !           993:        EVP_PKEY *upkey;
        !           994:
        !           995:        upkey = X509_get_pubkey(xca);
        !           996:        EVP_PKEY_copy_parameters(upkey, pkey);
        !           997:        EVP_PKEY_free(upkey);
        !           998:
        !           999:        if (!X509_STORE_CTX_init(&xsc, ctx, x, NULL)) {
        !          1000:                BIO_printf(bio_err, "Error initialising X509 store\n");
        !          1001:                goto end;
        !          1002:        }
        !          1003:        if (sno)
        !          1004:                bs = sno;
        !          1005:        else if (!(bs = x509_load_serial(CAfile, serialfile, create)))
        !          1006:                goto end;
        !          1007:
        !          1008: /*     if (!X509_STORE_add_cert(ctx,x)) goto end;*/
        !          1009:
        !          1010:        /*
        !          1011:         * NOTE: this certificate can/should be self signed, unless it was a
        !          1012:         * certificate request in which case it is not.
        !          1013:         */
        !          1014:        X509_STORE_CTX_set_cert(&xsc, x);
        !          1015:        X509_STORE_CTX_set_flags(&xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
        !          1016:        if (!reqfile && X509_verify_cert(&xsc) <= 0)
        !          1017:                goto end;
        !          1018:
        !          1019:        if (!X509_check_private_key(xca, pkey)) {
        !          1020:                BIO_printf(bio_err, "CA certificate and CA private key do not match\n");
        !          1021:                goto end;
        !          1022:        }
        !          1023:        if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
        !          1024:                goto end;
        !          1025:        if (!X509_set_serialNumber(x, bs))
        !          1026:                goto end;
        !          1027:
        !          1028:        if (X509_gmtime_adj(X509_get_notBefore(x), 0L) == NULL)
        !          1029:                goto end;
        !          1030:
        !          1031:        /* hardwired expired */
        !          1032:        if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL)
        !          1033:                goto end;
        !          1034:
        !          1035:        if (clrext) {
        !          1036:                while (X509_get_ext_count(x) > 0)
        !          1037:                        X509_delete_ext(x, 0);
        !          1038:        }
        !          1039:        if (conf) {
        !          1040:                X509V3_CTX ctx2;
        !          1041:                X509_set_version(x, 2); /* version 3 certificate */
        !          1042:                X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
        !          1043:                X509V3_set_nconf(&ctx2, conf);
        !          1044:                if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
        !          1045:                        goto end;
        !          1046:        }
        !          1047:        if (!do_X509_sign(bio_err, x, pkey, digest, sigopts))
        !          1048:                goto end;
        !          1049:        ret = 1;
        !          1050: end:
        !          1051:        X509_STORE_CTX_cleanup(&xsc);
        !          1052:        if (!ret)
        !          1053:                ERR_print_errors(bio_err);
        !          1054:        if (!sno)
        !          1055:                ASN1_INTEGER_free(bs);
        !          1056:        return ret;
        !          1057: }
        !          1058:
        !          1059: static int
        !          1060: callb(int ok, X509_STORE_CTX *ctx)
        !          1061: {
        !          1062:        int err;
        !          1063:        X509 *err_cert;
        !          1064:
        !          1065:        /*
        !          1066:         * it is ok to use a self signed certificate This case will catch
        !          1067:         * both the initial ok == 0 and the final ok == 1 calls to this
        !          1068:         * function
        !          1069:         */
        !          1070:        err = X509_STORE_CTX_get_error(ctx);
        !          1071:        if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
        !          1072:                return 1;
        !          1073:
        !          1074:        /*
        !          1075:         * BAD we should have gotten an error.  Normally if everything worked
        !          1076:         * X509_STORE_CTX_get_error(ctx) will still be set to
        !          1077:         * DEPTH_ZERO_SELF_....
        !          1078:         */
        !          1079:        if (ok) {
        !          1080:                BIO_printf(bio_err, "error with certificate to be certified - should be self signed\n");
        !          1081:                return 0;
        !          1082:        } else {
        !          1083:                err_cert = X509_STORE_CTX_get_current_cert(ctx);
        !          1084:                print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
        !          1085:                BIO_printf(bio_err, "error with certificate - error %d at depth %d\n%s\n",
        !          1086:                    err, X509_STORE_CTX_get_error_depth(ctx),
        !          1087:                    X509_verify_cert_error_string(err));
        !          1088:                return 1;
        !          1089:        }
        !          1090: }
        !          1091:
        !          1092: /* self sign */
        !          1093: static int
        !          1094: sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest,
        !          1095:     CONF *conf, char *section)
        !          1096: {
        !          1097:
        !          1098:        EVP_PKEY *pktmp;
        !          1099:
        !          1100:        pktmp = X509_get_pubkey(x);
        !          1101:        EVP_PKEY_copy_parameters(pktmp, pkey);
        !          1102:        EVP_PKEY_save_parameters(pktmp, 1);
        !          1103:        EVP_PKEY_free(pktmp);
        !          1104:
        !          1105:        if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
        !          1106:                goto err;
        !          1107:        if (X509_gmtime_adj(X509_get_notBefore(x), 0) == NULL)
        !          1108:                goto err;
        !          1109:
        !          1110:        /* Lets just make it 12:00am GMT, Jan 1 1970 */
        !          1111:        /* memcpy(x->cert_info->validity->notBefore,"700101120000Z",13); */
        !          1112:        /* 28 days to be certified */
        !          1113:
        !          1114:        if (X509_gmtime_adj(X509_get_notAfter(x),
        !          1115:            (long) 60 * 60 * 24 * days) == NULL)
        !          1116:                goto err;
        !          1117:
        !          1118:        if (!X509_set_pubkey(x, pkey))
        !          1119:                goto err;
        !          1120:        if (clrext) {
        !          1121:                while (X509_get_ext_count(x) > 0)
        !          1122:                        X509_delete_ext(x, 0);
        !          1123:        }
        !          1124:        if (conf) {
        !          1125:                X509V3_CTX ctx;
        !          1126:                X509_set_version(x, 2); /* version 3 certificate */
        !          1127:                X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
        !          1128:                X509V3_set_nconf(&ctx, conf);
        !          1129:                if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
        !          1130:                        goto err;
        !          1131:        }
        !          1132:        if (!X509_sign(x, pkey, digest))
        !          1133:                goto err;
        !          1134:        return 1;
        !          1135:
        !          1136: err:
        !          1137:        ERR_print_errors(bio_err);
        !          1138:        return 0;
        !          1139: }
        !          1140:
        !          1141: static int
        !          1142: purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
        !          1143: {
        !          1144:        int id, i, idret;
        !          1145:        char *pname;
        !          1146:
        !          1147:        id = X509_PURPOSE_get_id(pt);
        !          1148:        pname = X509_PURPOSE_get0_name(pt);
        !          1149:        for (i = 0; i < 2; i++) {
        !          1150:                idret = X509_check_purpose(cert, id, i);
        !          1151:                BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
        !          1152:                if (idret == 1)
        !          1153:                        BIO_printf(bio, "Yes\n");
        !          1154:                else if (idret == 0)
        !          1155:                        BIO_printf(bio, "No\n");
        !          1156:                else
        !          1157:                        BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
        !          1158:        }
        !          1159:        return 1;
        !          1160: }