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

1.8     ! bcook       1: /* $OpenBSD: x509.c,v 1.7 2015/09/21 13:13:06 bcook Exp $ */
1.1       jsing       2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
                      3:  * All rights reserved.
                      4:  *
                      5:  * This package is an SSL implementation written
                      6:  * by Eric Young (eay@cryptsoft.com).
                      7:  * The implementation was written so as to conform with Netscapes SSL.
                      8:  *
                      9:  * This library is free for commercial and non-commercial use as long as
                     10:  * the following conditions are aheared to.  The following conditions
                     11:  * apply to all code found in this distribution, be it the RC4, RSA,
                     12:  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
                     13:  * included with this distribution is covered by the same copyright terms
                     14:  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
                     15:  *
                     16:  * Copyright remains Eric Young's, and as such any Copyright notices in
                     17:  * the code are not to be removed.
                     18:  * If this package is used in a product, Eric Young should be given attribution
                     19:  * as the author of the parts of the library used.
                     20:  * This can be in the form of a textual message at program startup or
                     21:  * in documentation (online or textual) provided with the package.
                     22:  *
                     23:  * Redistribution and use in source and binary forms, with or without
                     24:  * modification, are permitted provided that the following conditions
                     25:  * are met:
                     26:  * 1. Redistributions of source code must retain the copyright
                     27:  *    notice, this list of conditions and the following disclaimer.
                     28:  * 2. Redistributions in binary form must reproduce the above copyright
                     29:  *    notice, this list of conditions and the following disclaimer in the
                     30:  *    documentation and/or other materials provided with the distribution.
                     31:  * 3. All advertising materials mentioning features or use of this software
                     32:  *    must display the following acknowledgement:
                     33:  *    "This product includes cryptographic software written by
                     34:  *     Eric Young (eay@cryptsoft.com)"
                     35:  *    The word 'cryptographic' can be left out if the rouines from the library
                     36:  *    being used are not cryptographic related :-).
                     37:  * 4. If you include any Windows specific code (or a derivative thereof) from
                     38:  *    the apps directory (application code) you must include an acknowledgement:
                     39:  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
                     40:  *
                     41:  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
                     42:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     43:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     44:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     45:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     46:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     47:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     48:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     49:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     50:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     51:  * SUCH DAMAGE.
                     52:  *
                     53:  * The licence and distribution terms for any publically available version or
                     54:  * derivative of this code cannot be changed.  i.e. this code cannot simply be
                     55:  * copied and put under another distribution licence
                     56:  * [including the GNU Public Licence.]
                     57:  */
                     58:
                     59: #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.7       bcook     138:        " -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:        " -certopt arg    - various certificate text options\n",
                    144:        NULL
                    145: };
                    146:
                    147: static int callb(int ok, X509_STORE_CTX *ctx);
                    148: static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
                    149:     const EVP_MD *digest, CONF *conf, char *section);
                    150: static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
                    151:     X509 *x, X509 *xca, EVP_PKEY *pkey, STACK_OF(OPENSSL_STRING) *sigopts,
                    152:     char *serial, int create, int days, int clrext, CONF *conf, char *section,
                    153:     ASN1_INTEGER *sno);
                    154: static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
                    155: static int reqfile = 0;
                    156:
                    157: int
                    158: x509_main(int argc, char **argv)
                    159: {
                    160:        int ret = 1;
                    161:        X509_REQ *req = NULL;
                    162:        X509 *x = NULL, *xca = NULL;
                    163:        ASN1_OBJECT *objtmp;
                    164:        STACK_OF(OPENSSL_STRING) *sigopts = NULL;
                    165:        EVP_PKEY *Upkey = NULL, *CApkey = NULL;
                    166:        ASN1_INTEGER *sno = NULL;
                    167:        int i, num, badops = 0;
                    168:        BIO *out = NULL;
                    169:        BIO *STDout = NULL;
                    170:        STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
                    171:        int informat, outformat, keyformat, CAformat, CAkeyformat;
                    172:        char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;
                    173:        char *CAkeyfile = NULL, *CAserial = NULL;
                    174:        char *alias = NULL;
                    175:        int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0,
                    176:            enddate = 0;
                    177:        int next_serial = 0;
                    178:        int subject_hash = 0, issuer_hash = 0, ocspid = 0;
                    179: #ifndef OPENSSL_NO_MD5
                    180:        int subject_hash_old = 0, issuer_hash_old = 0;
                    181: #endif
                    182:        int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0,
                    183:            email = 0;
                    184:        int ocsp_uri = 0;
                    185:        int trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0, clrext = 0;
                    186:        int C = 0;
                    187:        int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0;
                    188:        int pprint = 0;
                    189:        const char **pp;
                    190:        X509_STORE *ctx = NULL;
                    191:        X509_REQ *rq = NULL;
                    192:        int fingerprint = 0;
                    193:        char buf[256];
                    194:        const EVP_MD *md_alg, *digest = NULL;
                    195:        CONF *extconf = NULL;
                    196:        char *extsect = NULL, *extfile = NULL, *passin = NULL, *passargin = NULL;
                    197:        int checkend = 0, checkoffset = 0;
                    198:        unsigned long nmflag = 0, certflag = 0;
                    199:        const char *errstr = NULL;
                    200:
                    201:        reqfile = 0;
                    202:
                    203:        STDout = BIO_new_fp(stdout, BIO_NOCLOSE);
                    204:
                    205:        informat = FORMAT_PEM;
                    206:        outformat = FORMAT_PEM;
                    207:        keyformat = FORMAT_PEM;
                    208:        CAformat = FORMAT_PEM;
                    209:        CAkeyformat = FORMAT_PEM;
                    210:
                    211:        ctx = X509_STORE_new();
                    212:        if (ctx == NULL)
                    213:                goto end;
                    214:        X509_STORE_set_verify_cb(ctx, callb);
                    215:
                    216:        argc--;
                    217:        argv++;
                    218:        num = 0;
                    219:        while (argc >= 1) {
                    220:                if (strcmp(*argv, "-inform") == 0) {
                    221:                        if (--argc < 1)
                    222:                                goto bad;
                    223:                        informat = str2fmt(*(++argv));
                    224:                } else if (strcmp(*argv, "-outform") == 0) {
                    225:                        if (--argc < 1)
                    226:                                goto bad;
                    227:                        outformat = str2fmt(*(++argv));
                    228:                } else if (strcmp(*argv, "-keyform") == 0) {
                    229:                        if (--argc < 1)
                    230:                                goto bad;
                    231:                        keyformat = str2fmt(*(++argv));
                    232:                } else if (strcmp(*argv, "-req") == 0) {
                    233:                        reqfile = 1;
                    234:                } else if (strcmp(*argv, "-CAform") == 0) {
                    235:                        if (--argc < 1)
                    236:                                goto bad;
                    237:                        CAformat = str2fmt(*(++argv));
                    238:                } else if (strcmp(*argv, "-CAkeyform") == 0) {
                    239:                        if (--argc < 1)
                    240:                                goto bad;
                    241:                        CAkeyformat = str2fmt(*(++argv));
                    242:                } else if (strcmp(*argv, "-sigopt") == 0) {
                    243:                        if (--argc < 1)
                    244:                                goto bad;
                    245:                        if (!sigopts)
                    246:                                sigopts = sk_OPENSSL_STRING_new_null();
                    247:                        if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv)))
                    248:                                goto bad;
                    249:                } else if (strcmp(*argv, "-days") == 0) {
                    250:                        if (--argc < 1)
                    251:                                goto bad;
                    252:                        days = strtonum(*(++argv), 1, INT_MAX, &errstr);
                    253:                        if (errstr) {
                    254:                                BIO_printf(bio_err, "bad number of days: %s\n", errstr);
                    255:                                goto bad;
                    256:                        }
                    257:                } else if (strcmp(*argv, "-passin") == 0) {
                    258:                        if (--argc < 1)
                    259:                                goto bad;
                    260:                        passargin = *(++argv);
                    261:                } else if (strcmp(*argv, "-extfile") == 0) {
                    262:                        if (--argc < 1)
                    263:                                goto bad;
                    264:                        extfile = *(++argv);
                    265:                } else if (strcmp(*argv, "-extensions") == 0) {
                    266:                        if (--argc < 1)
                    267:                                goto bad;
                    268:                        extsect = *(++argv);
                    269:                } else if (strcmp(*argv, "-in") == 0) {
                    270:                        if (--argc < 1)
                    271:                                goto bad;
                    272:                        infile = *(++argv);
                    273:                } else if (strcmp(*argv, "-out") == 0) {
                    274:                        if (--argc < 1)
                    275:                                goto bad;
                    276:                        outfile = *(++argv);
                    277:                } else if (strcmp(*argv, "-signkey") == 0) {
                    278:                        if (--argc < 1)
                    279:                                goto bad;
                    280:                        keyfile = *(++argv);
                    281:                        sign_flag = ++num;
                    282:                } else if (strcmp(*argv, "-CA") == 0) {
                    283:                        if (--argc < 1)
                    284:                                goto bad;
                    285:                        CAfile = *(++argv);
                    286:                        CA_flag = ++num;
                    287:                } else if (strcmp(*argv, "-CAkey") == 0) {
                    288:                        if (--argc < 1)
                    289:                                goto bad;
                    290:                        CAkeyfile = *(++argv);
                    291:                } else if (strcmp(*argv, "-CAserial") == 0) {
                    292:                        if (--argc < 1)
                    293:                                goto bad;
                    294:                        CAserial = *(++argv);
                    295:                } else if (strcmp(*argv, "-set_serial") == 0) {
                    296:                        if (--argc < 1)
                    297:                                goto bad;
1.4       doug      298:                        M_ASN1_INTEGER_free(sno);
1.1       jsing     299:                        if (!(sno = s2i_ASN1_INTEGER(NULL, *(++argv))))
                    300:                                goto bad;
                    301:                } else if (strcmp(*argv, "-addtrust") == 0) {
                    302:                        if (--argc < 1)
                    303:                                goto bad;
                    304:                        if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) {
                    305:                                BIO_printf(bio_err,
                    306:                                    "Invalid trust object value %s\n", *argv);
                    307:                                goto bad;
                    308:                        }
                    309:                        if (!trust)
                    310:                                trust = sk_ASN1_OBJECT_new_null();
                    311:                        sk_ASN1_OBJECT_push(trust, objtmp);
                    312:                        trustout = 1;
                    313:                } else if (strcmp(*argv, "-addreject") == 0) {
                    314:                        if (--argc < 1)
                    315:                                goto bad;
                    316:                        if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) {
                    317:                                BIO_printf(bio_err,
                    318:                                    "Invalid reject object value %s\n", *argv);
                    319:                                goto bad;
                    320:                        }
                    321:                        if (!reject)
                    322:                                reject = sk_ASN1_OBJECT_new_null();
                    323:                        sk_ASN1_OBJECT_push(reject, objtmp);
                    324:                        trustout = 1;
                    325:                } else if (strcmp(*argv, "-setalias") == 0) {
                    326:                        if (--argc < 1)
                    327:                                goto bad;
                    328:                        alias = *(++argv);
                    329:                        trustout = 1;
                    330:                } else if (strcmp(*argv, "-certopt") == 0) {
                    331:                        if (--argc < 1)
                    332:                                goto bad;
                    333:                        if (!set_cert_ex(&certflag, *(++argv)))
                    334:                                goto bad;
                    335:                } else if (strcmp(*argv, "-nameopt") == 0) {
                    336:                        if (--argc < 1)
                    337:                                goto bad;
                    338:                        if (!set_name_ex(&nmflag, *(++argv)))
                    339:                                goto bad;
                    340:                }
                    341:                else if (strcmp(*argv, "-C") == 0)
                    342:                        C = ++num;
                    343:                else if (strcmp(*argv, "-email") == 0)
                    344:                        email = ++num;
                    345:                else if (strcmp(*argv, "-ocsp_uri") == 0)
                    346:                        ocsp_uri = ++num;
                    347:                else if (strcmp(*argv, "-serial") == 0)
                    348:                        serial = ++num;
                    349:                else if (strcmp(*argv, "-next_serial") == 0)
                    350:                        next_serial = ++num;
                    351:                else if (strcmp(*argv, "-modulus") == 0)
                    352:                        modulus = ++num;
                    353:                else if (strcmp(*argv, "-pubkey") == 0)
                    354:                        pubkey = ++num;
                    355:                else if (strcmp(*argv, "-x509toreq") == 0)
                    356:                        x509req = ++num;
                    357:                else if (strcmp(*argv, "-text") == 0)
                    358:                        text = ++num;
                    359:                else if (strcmp(*argv, "-hash") == 0 ||
                    360:                    strcmp(*argv, "-subject_hash") == 0)
                    361:                        subject_hash = ++num;
                    362: #ifndef OPENSSL_NO_MD5
                    363:                else if (strcmp(*argv, "-subject_hash_old") == 0)
                    364:                        subject_hash_old = ++num;
                    365: #endif
                    366:                else if (strcmp(*argv, "-issuer_hash") == 0)
                    367:                        issuer_hash = ++num;
                    368: #ifndef OPENSSL_NO_MD5
                    369:                else if (strcmp(*argv, "-issuer_hash_old") == 0)
                    370:                        issuer_hash_old = ++num;
                    371: #endif
                    372:                else if (strcmp(*argv, "-subject") == 0)
                    373:                        subject = ++num;
                    374:                else if (strcmp(*argv, "-issuer") == 0)
                    375:                        issuer = ++num;
                    376:                else if (strcmp(*argv, "-fingerprint") == 0)
                    377:                        fingerprint = ++num;
                    378:                else if (strcmp(*argv, "-dates") == 0) {
                    379:                        startdate = ++num;
                    380:                        enddate = ++num;
                    381:                } else if (strcmp(*argv, "-purpose") == 0)
                    382:                        pprint = ++num;
                    383:                else if (strcmp(*argv, "-startdate") == 0)
                    384:                        startdate = ++num;
                    385:                else if (strcmp(*argv, "-enddate") == 0)
                    386:                        enddate = ++num;
                    387:                else if (strcmp(*argv, "-checkend") == 0) {
                    388:                        if (--argc < 1)
                    389:                                goto bad;
                    390:                        checkoffset = strtonum(*(++argv), 0, INT_MAX, &errstr);
                    391:                        if (errstr) {
                    392:                                BIO_printf(bio_err, "checkend unusable: %s\n", errstr);
                    393:                                goto bad;
                    394:                        }
                    395:                        checkend = 1;
                    396:                } else if (strcmp(*argv, "-noout") == 0)
                    397:                        noout = ++num;
                    398:                else if (strcmp(*argv, "-trustout") == 0)
                    399:                        trustout = 1;
                    400:                else if (strcmp(*argv, "-clrtrust") == 0)
                    401:                        clrtrust = ++num;
                    402:                else if (strcmp(*argv, "-clrreject") == 0)
                    403:                        clrreject = ++num;
                    404:                else if (strcmp(*argv, "-alias") == 0)
                    405:                        aliasout = ++num;
                    406:                else if (strcmp(*argv, "-CAcreateserial") == 0)
                    407:                        CA_createserial = ++num;
                    408:                else if (strcmp(*argv, "-clrext") == 0)
                    409:                        clrext = 1;
                    410:                else if (strcmp(*argv, "-ocspid") == 0)
                    411:                        ocspid = ++num;
                    412:                else if ((md_alg = EVP_get_digestbyname(*argv + 1))) {
                    413:                        /* ok */
                    414:                        digest = md_alg;
                    415:                } else {
                    416:                        BIO_printf(bio_err, "unknown option %s\n", *argv);
                    417:                        badops = 1;
                    418:                        break;
                    419:                }
                    420:                argc--;
                    421:                argv++;
                    422:        }
                    423:
                    424:        if (badops) {
                    425: bad:
                    426:                for (pp = x509_usage; (*pp != NULL); pp++)
                    427:                        BIO_printf(bio_err, "%s", *pp);
                    428:                goto end;
                    429:        }
                    430:
                    431:        if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
                    432:                BIO_printf(bio_err, "Error getting password\n");
                    433:                goto end;
                    434:        }
                    435:        if (!X509_STORE_set_default_paths(ctx)) {
                    436:                ERR_print_errors(bio_err);
                    437:                goto end;
                    438:        }
                    439:        if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {
                    440:                CAkeyfile = CAfile;
                    441:        } else if ((CA_flag) && (CAkeyfile == NULL)) {
                    442:                BIO_printf(bio_err,
                    443:                    "need to specify a CAkey if using the CA command\n");
                    444:                goto end;
                    445:        }
                    446:        if (extfile) {
                    447:                long errorline = -1;
                    448:                X509V3_CTX ctx2;
                    449:                extconf = NCONF_new(NULL);
                    450:                if (!NCONF_load(extconf, extfile, &errorline)) {
                    451:                        if (errorline <= 0)
                    452:                                BIO_printf(bio_err,
                    453:                                    "error loading the config file '%s'\n",
                    454:                                    extfile);
                    455:                        else
                    456:                                BIO_printf(bio_err,
                    457:                                    "error on line %ld of config file '%s'\n",
                    458:                                    errorline, extfile);
                    459:                        goto end;
                    460:                }
                    461:                if (!extsect) {
                    462:                        extsect = NCONF_get_string(extconf, "default",
                    463:                            "extensions");
                    464:                        if (!extsect) {
                    465:                                ERR_clear_error();
                    466:                                extsect = "default";
                    467:                        }
                    468:                }
                    469:                X509V3_set_ctx_test(&ctx2);
                    470:                X509V3_set_nconf(&ctx2, extconf);
                    471:                if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
                    472:                        BIO_printf(bio_err,
                    473:                            "Error Loading extension section %s\n",
                    474:                            extsect);
                    475:                        ERR_print_errors(bio_err);
                    476:                        goto end;
                    477:                }
                    478:        }
                    479:        if (reqfile) {
                    480:                EVP_PKEY *pkey;
                    481:                BIO *in;
                    482:
                    483:                if (!sign_flag && !CA_flag) {
                    484:                        BIO_printf(bio_err, "We need a private key to sign with\n");
                    485:                        goto end;
                    486:                }
                    487:                in = BIO_new(BIO_s_file());
                    488:                if (in == NULL) {
                    489:                        ERR_print_errors(bio_err);
                    490:                        goto end;
                    491:                }
                    492:                if (infile == NULL)
                    493:                        BIO_set_fp(in, stdin, BIO_NOCLOSE | BIO_FP_TEXT);
                    494:                else {
                    495:                        if (BIO_read_filename(in, infile) <= 0) {
                    496:                                perror(infile);
                    497:                                BIO_free(in);
                    498:                                goto end;
                    499:                        }
                    500:                }
                    501:                req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
                    502:                BIO_free(in);
                    503:
                    504:                if (req == NULL) {
                    505:                        ERR_print_errors(bio_err);
                    506:                        goto end;
                    507:                }
                    508:                if ((req->req_info == NULL) ||
                    509:                    (req->req_info->pubkey == NULL) ||
                    510:                    (req->req_info->pubkey->public_key == NULL) ||
                    511:                    (req->req_info->pubkey->public_key->data == NULL)) {
                    512:                        BIO_printf(bio_err, "The certificate request appears to corrupted\n");
                    513:                        BIO_printf(bio_err, "It does not contain a public key\n");
                    514:                        goto end;
                    515:                }
                    516:                if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {
                    517:                        BIO_printf(bio_err, "error unpacking public key\n");
                    518:                        goto end;
                    519:                }
                    520:                i = X509_REQ_verify(req, pkey);
                    521:                EVP_PKEY_free(pkey);
                    522:                if (i < 0) {
                    523:                        BIO_printf(bio_err, "Signature verification error\n");
                    524:                        ERR_print_errors(bio_err);
                    525:                        goto end;
                    526:                }
                    527:                if (i == 0) {
                    528:                        BIO_printf(bio_err, "Signature did not match the certificate request\n");
                    529:                        goto end;
                    530:                } else
                    531:                        BIO_printf(bio_err, "Signature ok\n");
                    532:
                    533:                print_name(bio_err, "subject=", X509_REQ_get_subject_name(req), nmflag);
                    534:
                    535:                if ((x = X509_new()) == NULL)
                    536:                        goto end;
                    537:
                    538:                if (sno == NULL) {
                    539:                        sno = ASN1_INTEGER_new();
                    540:                        if (!sno || !rand_serial(NULL, sno))
                    541:                                goto end;
                    542:                        if (!X509_set_serialNumber(x, sno))
                    543:                                goto end;
                    544:                        ASN1_INTEGER_free(sno);
                    545:                        sno = NULL;
                    546:                } else if (!X509_set_serialNumber(x, sno))
                    547:                        goto end;
                    548:
                    549:                if (!X509_set_issuer_name(x, req->req_info->subject))
                    550:                        goto end;
                    551:                if (!X509_set_subject_name(x, req->req_info->subject))
                    552:                        goto end;
                    553:
                    554:                X509_gmtime_adj(X509_get_notBefore(x), 0);
                    555:                X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL);
                    556:
                    557:                pkey = X509_REQ_get_pubkey(req);
                    558:                X509_set_pubkey(x, pkey);
                    559:                EVP_PKEY_free(pkey);
                    560:        } else
1.6       bcook     561:                x = load_cert(bio_err, infile, informat, NULL, "Certificate");
1.1       jsing     562:
                    563:        if (x == NULL)
                    564:                goto end;
                    565:        if (CA_flag) {
1.6       bcook     566:                xca = load_cert(bio_err, CAfile, CAformat, NULL, "CA Certificate");
1.1       jsing     567:                if (xca == NULL)
                    568:                        goto end;
                    569:        }
                    570:        if (!noout || text || next_serial) {
                    571:                OBJ_create("2.99999.3",
                    572:                    "SET.ex3", "SET x509v3 extension 3");
                    573:
                    574:                out = BIO_new(BIO_s_file());
                    575:                if (out == NULL) {
                    576:                        ERR_print_errors(bio_err);
                    577:                        goto end;
                    578:                }
                    579:                if (outfile == NULL) {
                    580:                        BIO_set_fp(out, stdout, BIO_NOCLOSE);
                    581:                } else {
                    582:                        if (BIO_write_filename(out, outfile) <= 0) {
                    583:                                perror(outfile);
                    584:                                goto end;
                    585:                        }
                    586:                }
                    587:        }
                    588:        if (alias)
                    589:                X509_alias_set1(x, (unsigned char *) alias, -1);
                    590:
                    591:        if (clrtrust)
                    592:                X509_trust_clear(x);
                    593:        if (clrreject)
                    594:                X509_reject_clear(x);
                    595:
                    596:        if (trust) {
                    597:                for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
                    598:                        objtmp = sk_ASN1_OBJECT_value(trust, i);
                    599:                        X509_add1_trust_object(x, objtmp);
                    600:                }
                    601:        }
                    602:        if (reject) {
                    603:                for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
                    604:                        objtmp = sk_ASN1_OBJECT_value(reject, i);
                    605:                        X509_add1_reject_object(x, objtmp);
                    606:                }
                    607:        }
                    608:        if (num) {
                    609:                for (i = 1; i <= num; i++) {
                    610:                        if (issuer == i) {
                    611:                                print_name(STDout, "issuer= ",
                    612:                                    X509_get_issuer_name(x), nmflag);
                    613:                        } else if (subject == i) {
                    614:                                print_name(STDout, "subject= ",
                    615:                                    X509_get_subject_name(x), nmflag);
                    616:                        } else if (serial == i) {
                    617:                                BIO_printf(STDout, "serial=");
                    618:                                i2a_ASN1_INTEGER(STDout,
                    619:                                    X509_get_serialNumber(x));
                    620:                                BIO_printf(STDout, "\n");
                    621:                        } else if (next_serial == i) {
                    622:                                BIGNUM *bnser;
                    623:                                ASN1_INTEGER *ser;
                    624:                                ser = X509_get_serialNumber(x);
                    625:                                bnser = ASN1_INTEGER_to_BN(ser, NULL);
                    626:                                if (!bnser)
                    627:                                        goto end;
                    628:                                if (!BN_add_word(bnser, 1))
                    629:                                        goto end;
                    630:                                ser = BN_to_ASN1_INTEGER(bnser, NULL);
                    631:                                if (!ser)
                    632:                                        goto end;
                    633:                                BN_free(bnser);
                    634:                                i2a_ASN1_INTEGER(out, ser);
                    635:                                ASN1_INTEGER_free(ser);
                    636:                                BIO_puts(out, "\n");
                    637:                        } else if ((email == i) || (ocsp_uri == i)) {
                    638:                                int j;
                    639:                                STACK_OF(OPENSSL_STRING) *emlst;
                    640:                                if (email == i)
                    641:                                        emlst = X509_get1_email(x);
                    642:                                else
                    643:                                        emlst = X509_get1_ocsp(x);
                    644:                                for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
                    645:                                        BIO_printf(STDout, "%s\n",
                    646:                                            sk_OPENSSL_STRING_value(emlst, j));
                    647:                                X509_email_free(emlst);
                    648:                        } else if (aliasout == i) {
                    649:                                unsigned char *alstr;
                    650:                                alstr = X509_alias_get0(x, NULL);
                    651:                                if (alstr)
                    652:                                        BIO_printf(STDout, "%s\n", alstr);
                    653:                                else
                    654:                                        BIO_puts(STDout, "<No Alias>\n");
                    655:                        } else if (subject_hash == i) {
                    656:                                BIO_printf(STDout, "%08lx\n", X509_subject_name_hash(x));
                    657:                        }
                    658: #ifndef OPENSSL_NO_MD5
                    659:                        else if (subject_hash_old == i) {
                    660:                                BIO_printf(STDout, "%08lx\n", X509_subject_name_hash_old(x));
                    661:                        }
                    662: #endif
                    663:                        else if (issuer_hash == i) {
                    664:                                BIO_printf(STDout, "%08lx\n", X509_issuer_name_hash(x));
                    665:                        }
                    666: #ifndef OPENSSL_NO_MD5
                    667:                        else if (issuer_hash_old == i) {
                    668:                                BIO_printf(STDout, "%08lx\n", X509_issuer_name_hash_old(x));
                    669:                        }
                    670: #endif
                    671:                        else if (pprint == i) {
                    672:                                X509_PURPOSE *ptmp;
                    673:                                int j;
                    674:                                BIO_printf(STDout, "Certificate purposes:\n");
                    675:                                for (j = 0; j < X509_PURPOSE_get_count(); j++) {
                    676:                                        ptmp = X509_PURPOSE_get0(j);
                    677:                                        purpose_print(STDout, x, ptmp);
                    678:                                }
                    679:                        } else if (modulus == i) {
                    680:                                EVP_PKEY *pkey;
                    681:
                    682:                                pkey = X509_get_pubkey(x);
                    683:                                if (pkey == NULL) {
                    684:                                        BIO_printf(bio_err, "Modulus=unavailable\n");
                    685:                                        ERR_print_errors(bio_err);
                    686:                                        goto end;
                    687:                                }
                    688:                                BIO_printf(STDout, "Modulus=");
                    689:                                if (pkey->type == EVP_PKEY_RSA)
                    690:                                        BN_print(STDout, pkey->pkey.rsa->n);
                    691:                                else
                    692:                                                if (pkey->type == EVP_PKEY_DSA)
                    693:                                                        BN_print(STDout, pkey->pkey.dsa->pub_key);
                    694:                                else
                    695:                                                BIO_printf(STDout, "Wrong Algorithm type");
                    696:                                BIO_printf(STDout, "\n");
                    697:                                EVP_PKEY_free(pkey);
                    698:                        } else if (pubkey == i) {
                    699:                                EVP_PKEY *pkey;
                    700:
                    701:                                pkey = X509_get_pubkey(x);
                    702:                                if (pkey == NULL) {
                    703:                                        BIO_printf(bio_err, "Error getting public key\n");
                    704:                                        ERR_print_errors(bio_err);
                    705:                                        goto end;
                    706:                                }
                    707:                                PEM_write_bio_PUBKEY(STDout, pkey);
                    708:                                EVP_PKEY_free(pkey);
                    709:                        } else if (C == i) {
                    710:                                unsigned char *d;
                    711:                                char *m;
                    712:                                int y, z;
                    713:
                    714:                                X509_NAME_oneline(X509_get_subject_name(x),
                    715:                                    buf, sizeof buf);
                    716:                                BIO_printf(STDout, "/* subject:%s */\n", buf);
                    717:                                m = X509_NAME_oneline(
                    718:                                    X509_get_issuer_name(x), buf,
                    719:                                    sizeof buf);
                    720:                                BIO_printf(STDout, "/* issuer :%s */\n", buf);
                    721:
                    722:                                z = i2d_X509(x, NULL);
                    723:                                m = malloc(z);
1.8     ! bcook     724:                                if (m == NULL) {
        !           725:                                        BIO_printf(bio_err, "out of mem\n");
        !           726:                                        goto end;
        !           727:                                }
1.1       jsing     728:
                    729:                                d = (unsigned char *) m;
                    730:                                z = i2d_X509_NAME(X509_get_subject_name(x), &d);
                    731:                                BIO_printf(STDout, "unsigned char XXX_subject_name[%d]={\n", z);
                    732:                                d = (unsigned char *) m;
                    733:                                for (y = 0; y < z; y++) {
                    734:                                        BIO_printf(STDout, "0x%02X,", d[y]);
                    735:                                        if ((y & 0x0f) == 0x0f)
                    736:                                                BIO_printf(STDout, "\n");
                    737:                                }
                    738:                                if (y % 16 != 0)
                    739:                                        BIO_printf(STDout, "\n");
                    740:                                BIO_printf(STDout, "};\n");
                    741:
                    742:                                z = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);
                    743:                                BIO_printf(STDout, "unsigned char XXX_public_key[%d]={\n", z);
                    744:                                d = (unsigned char *) m;
                    745:                                for (y = 0; y < z; y++) {
                    746:                                        BIO_printf(STDout, "0x%02X,", d[y]);
                    747:                                        if ((y & 0x0f) == 0x0f)
                    748:                                                BIO_printf(STDout, "\n");
                    749:                                }
                    750:                                if (y % 16 != 0)
                    751:                                        BIO_printf(STDout, "\n");
                    752:                                BIO_printf(STDout, "};\n");
                    753:
                    754:                                z = i2d_X509(x, &d);
                    755:                                BIO_printf(STDout, "unsigned char XXX_certificate[%d]={\n", z);
                    756:                                d = (unsigned char *) m;
                    757:                                for (y = 0; y < z; y++) {
                    758:                                        BIO_printf(STDout, "0x%02X,", d[y]);
                    759:                                        if ((y & 0x0f) == 0x0f)
                    760:                                                BIO_printf(STDout, "\n");
                    761:                                }
                    762:                                if (y % 16 != 0)
                    763:                                        BIO_printf(STDout, "\n");
                    764:                                BIO_printf(STDout, "};\n");
                    765:
                    766:                                free(m);
                    767:                        } else if (text == i) {
                    768:                                X509_print_ex(STDout, x, nmflag, certflag);
                    769:                        } else if (startdate == i) {
                    770:                                BIO_puts(STDout, "notBefore=");
                    771:                                ASN1_TIME_print(STDout, X509_get_notBefore(x));
                    772:                                BIO_puts(STDout, "\n");
                    773:                        } else if (enddate == i) {
                    774:                                BIO_puts(STDout, "notAfter=");
                    775:                                ASN1_TIME_print(STDout, X509_get_notAfter(x));
                    776:                                BIO_puts(STDout, "\n");
                    777:                        } else if (fingerprint == i) {
                    778:                                int j;
                    779:                                unsigned int n;
                    780:                                unsigned char md[EVP_MAX_MD_SIZE];
                    781:                                const EVP_MD *fdig = digest;
                    782:
                    783:                                if (!fdig)
                    784:                                        fdig = EVP_sha1();
                    785:
                    786:                                if (!X509_digest(x, fdig, md, &n)) {
                    787:                                        BIO_printf(bio_err, "out of memory\n");
                    788:                                        goto end;
                    789:                                }
                    790:                                BIO_printf(STDout, "%s Fingerprint=",
                    791:                                    OBJ_nid2sn(EVP_MD_type(fdig)));
                    792:                                for (j = 0; j < (int) n; j++) {
                    793:                                        BIO_printf(STDout, "%02X%c", md[j],
                    794:                                            (j + 1 == (int)n) ? '\n' : ':');
                    795:                                }
                    796:                        }
                    797:                        /* should be in the library */
                    798:                        else if ((sign_flag == i) && (x509req == 0)) {
                    799:                                BIO_printf(bio_err, "Getting Private key\n");
                    800:                                if (Upkey == NULL) {
                    801:                                        Upkey = load_key(bio_err,
                    802:                                            keyfile, keyformat, 0,
1.6       bcook     803:                                            passin, "Private key");
1.1       jsing     804:                                        if (Upkey == NULL)
                    805:                                                goto end;
                    806:                                }
                    807:                                if (!sign(x, Upkey, days, clrext, digest,
                    808:                                    extconf, extsect))
                    809:                                        goto end;
                    810:                        } else if (CA_flag == i) {
                    811:                                BIO_printf(bio_err, "Getting CA Private Key\n");
                    812:                                if (CAkeyfile != NULL) {
                    813:                                        CApkey = load_key(bio_err,
                    814:                                            CAkeyfile, CAkeyformat,
1.6       bcook     815:                                            0, passin, "CA Private Key");
1.1       jsing     816:                                        if (CApkey == NULL)
                    817:                                                goto end;
                    818:                                }
                    819:                                if (!x509_certify(ctx, CAfile, digest, x, xca,
                    820:                                    CApkey, sigopts,
                    821:                                    CAserial, CA_createserial, days, clrext,
                    822:                                    extconf, extsect, sno))
                    823:                                        goto end;
                    824:                        } else if (x509req == i) {
                    825:                                EVP_PKEY *pk;
                    826:
                    827:                                BIO_printf(bio_err, "Getting request Private Key\n");
                    828:                                if (keyfile == NULL) {
                    829:                                        BIO_printf(bio_err, "no request key file specified\n");
                    830:                                        goto end;
                    831:                                } else {
                    832:                                        pk = load_key(bio_err,
                    833:                                            keyfile, keyformat, 0,
1.6       bcook     834:                                            passin, "request key");
1.1       jsing     835:                                        if (pk == NULL)
                    836:                                                goto end;
                    837:                                }
                    838:
                    839:                                BIO_printf(bio_err, "Generating certificate request\n");
                    840:
                    841:                                rq = X509_to_X509_REQ(x, pk, digest);
                    842:                                EVP_PKEY_free(pk);
                    843:                                if (rq == NULL) {
                    844:                                        ERR_print_errors(bio_err);
                    845:                                        goto end;
                    846:                                }
                    847:                                if (!noout) {
                    848:                                        X509_REQ_print(out, rq);
                    849:                                        PEM_write_bio_X509_REQ(out, rq);
                    850:                                }
                    851:                                noout = 1;
                    852:                        } else if (ocspid == i) {
                    853:                                X509_ocspid_print(out, x);
                    854:                        }
                    855:                }
                    856:        }
                    857:        if (checkend) {
                    858:                time_t tcheck = time(NULL) + checkoffset;
                    859:
                    860:                if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {
                    861:                        BIO_printf(out, "Certificate will expire\n");
                    862:                        ret = 1;
                    863:                } else {
                    864:                        BIO_printf(out, "Certificate will not expire\n");
                    865:                        ret = 0;
                    866:                }
                    867:                goto end;
                    868:        }
                    869:        if (noout) {
                    870:                ret = 0;
                    871:                goto end;
                    872:        }
                    873:        if (outformat == FORMAT_ASN1)
                    874:                i = i2d_X509_bio(out, x);
                    875:        else if (outformat == FORMAT_PEM) {
                    876:                if (trustout)
                    877:                        i = PEM_write_bio_X509_AUX(out, x);
                    878:                else
                    879:                        i = PEM_write_bio_X509(out, x);
                    880:        } else if (outformat == FORMAT_NETSCAPE) {
                    881:                NETSCAPE_X509 nx;
                    882:                ASN1_OCTET_STRING hdr;
                    883:
                    884:                hdr.data = (unsigned char *) NETSCAPE_CERT_HDR;
                    885:                hdr.length = strlen(NETSCAPE_CERT_HDR);
                    886:                nx.header = &hdr;
                    887:                nx.cert = x;
                    888:
                    889:                i = ASN1_item_i2d_bio(ASN1_ITEM_rptr(NETSCAPE_X509), out, &nx);
                    890:        } else {
                    891:                BIO_printf(bio_err, "bad output format specified for outfile\n");
                    892:                goto end;
                    893:        }
                    894:        if (!i) {
                    895:                BIO_printf(bio_err, "unable to write certificate\n");
                    896:                ERR_print_errors(bio_err);
                    897:                goto end;
                    898:        }
                    899:        ret = 0;
                    900:
                    901: end:
                    902:        OBJ_cleanup();
                    903:        NCONF_free(extconf);
                    904:        BIO_free_all(out);
                    905:        BIO_free_all(STDout);
                    906:        X509_STORE_free(ctx);
                    907:        X509_REQ_free(req);
                    908:        X509_free(x);
                    909:        X509_free(xca);
                    910:        EVP_PKEY_free(Upkey);
                    911:        EVP_PKEY_free(CApkey);
                    912:        if (sigopts)
                    913:                sk_OPENSSL_STRING_free(sigopts);
                    914:        X509_REQ_free(rq);
                    915:        ASN1_INTEGER_free(sno);
                    916:        sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
                    917:        sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
                    918:        free(passin);
                    919:
                    920:        return (ret);
                    921: }
                    922:
                    923: static ASN1_INTEGER *
                    924: x509_load_serial(char *CAfile, char *serialfile, int create)
                    925: {
                    926:        char *buf = NULL, *p;
                    927:        ASN1_INTEGER *bs = NULL;
                    928:        BIGNUM *serial = NULL;
                    929:        size_t len;
                    930:
                    931:        len = ((serialfile == NULL) ? (strlen(CAfile) + strlen(POSTFIX) + 1) :
                    932:            (strlen(serialfile))) + 1;
                    933:        buf = malloc(len);
                    934:        if (buf == NULL) {
                    935:                BIO_printf(bio_err, "out of mem\n");
                    936:                goto end;
                    937:        }
                    938:        if (serialfile == NULL) {
                    939:                strlcpy(buf, CAfile, len);
                    940:                for (p = buf; *p; p++)
                    941:                        if (*p == '.') {
                    942:                                *p = '\0';
                    943:                                break;
                    944:                        }
                    945:                strlcat(buf, POSTFIX, len);
                    946:        } else
                    947:                strlcpy(buf, serialfile, len);
                    948:
                    949:        serial = load_serial(buf, create, NULL);
                    950:        if (serial == NULL)
                    951:                goto end;
                    952:
                    953:        if (!BN_add_word(serial, 1)) {
                    954:                BIO_printf(bio_err, "add_word failure\n");
                    955:                goto end;
                    956:        }
                    957:        if (!save_serial(buf, NULL, serial, &bs))
                    958:                goto end;
                    959:
                    960: end:
                    961:        free(buf);
                    962:        BN_free(serial);
                    963:
                    964:        return bs;
                    965: }
                    966:
                    967: static int
                    968: x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest, X509 *x,
                    969:     X509 *xca, EVP_PKEY *pkey, STACK_OF(OPENSSL_STRING) *sigopts,
                    970:     char *serialfile, int create, int days, int clrext, CONF *conf,
                    971:     char *section, ASN1_INTEGER *sno)
                    972: {
                    973:        int ret = 0;
                    974:        ASN1_INTEGER *bs = NULL;
                    975:        X509_STORE_CTX xsc;
                    976:        EVP_PKEY *upkey;
                    977:
                    978:        upkey = X509_get_pubkey(xca);
                    979:        EVP_PKEY_copy_parameters(upkey, pkey);
                    980:        EVP_PKEY_free(upkey);
                    981:
                    982:        if (!X509_STORE_CTX_init(&xsc, ctx, x, NULL)) {
                    983:                BIO_printf(bio_err, "Error initialising X509 store\n");
                    984:                goto end;
                    985:        }
                    986:        if (sno)
                    987:                bs = sno;
                    988:        else if (!(bs = x509_load_serial(CAfile, serialfile, create)))
                    989:                goto end;
                    990:
                    991: /*     if (!X509_STORE_add_cert(ctx,x)) goto end;*/
                    992:
                    993:        /*
                    994:         * NOTE: this certificate can/should be self signed, unless it was a
                    995:         * certificate request in which case it is not.
                    996:         */
                    997:        X509_STORE_CTX_set_cert(&xsc, x);
                    998:        X509_STORE_CTX_set_flags(&xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
                    999:        if (!reqfile && X509_verify_cert(&xsc) <= 0)
                   1000:                goto end;
                   1001:
                   1002:        if (!X509_check_private_key(xca, pkey)) {
                   1003:                BIO_printf(bio_err, "CA certificate and CA private key do not match\n");
                   1004:                goto end;
                   1005:        }
                   1006:        if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
                   1007:                goto end;
                   1008:        if (!X509_set_serialNumber(x, bs))
                   1009:                goto end;
                   1010:
                   1011:        if (X509_gmtime_adj(X509_get_notBefore(x), 0L) == NULL)
                   1012:                goto end;
                   1013:
                   1014:        /* hardwired expired */
                   1015:        if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL)
                   1016:                goto end;
                   1017:
                   1018:        if (clrext) {
                   1019:                while (X509_get_ext_count(x) > 0)
                   1020:                        X509_delete_ext(x, 0);
                   1021:        }
                   1022:        if (conf) {
                   1023:                X509V3_CTX ctx2;
                   1024:                X509_set_version(x, 2); /* version 3 certificate */
                   1025:                X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
                   1026:                X509V3_set_nconf(&ctx2, conf);
                   1027:                if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
                   1028:                        goto end;
                   1029:        }
                   1030:        if (!do_X509_sign(bio_err, x, pkey, digest, sigopts))
                   1031:                goto end;
                   1032:        ret = 1;
                   1033: end:
                   1034:        X509_STORE_CTX_cleanup(&xsc);
                   1035:        if (!ret)
                   1036:                ERR_print_errors(bio_err);
                   1037:        if (!sno)
                   1038:                ASN1_INTEGER_free(bs);
                   1039:        return ret;
                   1040: }
                   1041:
                   1042: static int
                   1043: callb(int ok, X509_STORE_CTX *ctx)
                   1044: {
                   1045:        int err;
                   1046:        X509 *err_cert;
                   1047:
                   1048:        /*
                   1049:         * it is ok to use a self signed certificate This case will catch
                   1050:         * both the initial ok == 0 and the final ok == 1 calls to this
                   1051:         * function
                   1052:         */
                   1053:        err = X509_STORE_CTX_get_error(ctx);
                   1054:        if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
                   1055:                return 1;
                   1056:
                   1057:        /*
                   1058:         * BAD we should have gotten an error.  Normally if everything worked
                   1059:         * X509_STORE_CTX_get_error(ctx) will still be set to
                   1060:         * DEPTH_ZERO_SELF_....
                   1061:         */
                   1062:        if (ok) {
                   1063:                BIO_printf(bio_err, "error with certificate to be certified - should be self signed\n");
                   1064:                return 0;
                   1065:        } else {
                   1066:                err_cert = X509_STORE_CTX_get_current_cert(ctx);
                   1067:                print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
                   1068:                BIO_printf(bio_err, "error with certificate - error %d at depth %d\n%s\n",
                   1069:                    err, X509_STORE_CTX_get_error_depth(ctx),
                   1070:                    X509_verify_cert_error_string(err));
                   1071:                return 1;
                   1072:        }
                   1073: }
                   1074:
                   1075: /* self sign */
                   1076: static int
                   1077: sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest,
                   1078:     CONF *conf, char *section)
                   1079: {
                   1080:
                   1081:        EVP_PKEY *pktmp;
                   1082:
                   1083:        pktmp = X509_get_pubkey(x);
                   1084:        EVP_PKEY_copy_parameters(pktmp, pkey);
                   1085:        EVP_PKEY_save_parameters(pktmp, 1);
                   1086:        EVP_PKEY_free(pktmp);
                   1087:
                   1088:        if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
                   1089:                goto err;
                   1090:        if (X509_gmtime_adj(X509_get_notBefore(x), 0) == NULL)
                   1091:                goto err;
                   1092:
                   1093:        /* Lets just make it 12:00am GMT, Jan 1 1970 */
                   1094:        /* memcpy(x->cert_info->validity->notBefore,"700101120000Z",13); */
                   1095:        /* 28 days to be certified */
                   1096:
                   1097:        if (X509_gmtime_adj(X509_get_notAfter(x),
                   1098:            (long) 60 * 60 * 24 * days) == NULL)
                   1099:                goto err;
                   1100:
                   1101:        if (!X509_set_pubkey(x, pkey))
                   1102:                goto err;
                   1103:        if (clrext) {
                   1104:                while (X509_get_ext_count(x) > 0)
                   1105:                        X509_delete_ext(x, 0);
                   1106:        }
                   1107:        if (conf) {
                   1108:                X509V3_CTX ctx;
                   1109:                X509_set_version(x, 2); /* version 3 certificate */
                   1110:                X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
                   1111:                X509V3_set_nconf(&ctx, conf);
                   1112:                if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
                   1113:                        goto err;
                   1114:        }
                   1115:        if (!X509_sign(x, pkey, digest))
                   1116:                goto err;
                   1117:        return 1;
                   1118:
                   1119: err:
                   1120:        ERR_print_errors(bio_err);
                   1121:        return 0;
                   1122: }
                   1123:
                   1124: static int
                   1125: purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
                   1126: {
                   1127:        int id, i, idret;
                   1128:        char *pname;
                   1129:
                   1130:        id = X509_PURPOSE_get_id(pt);
                   1131:        pname = X509_PURPOSE_get0_name(pt);
                   1132:        for (i = 0; i < 2; i++) {
                   1133:                idret = X509_check_purpose(cert, id, i);
                   1134:                BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
                   1135:                if (idret == 1)
                   1136:                        BIO_printf(bio, "Yes\n");
                   1137:                else if (idret == 0)
                   1138:                        BIO_printf(bio, "No\n");
                   1139:                else
                   1140:                        BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
                   1141:        }
                   1142:        return 1;
                   1143: }