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

Annotation of src/usr.bin/openssl/req.c, Revision 1.11

1.11    ! semarie     1: /* $OpenBSD: req.c,v 1.10 2015/10/10 22:28:51 doug Exp $ */
1.1       jsing       2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
                      3:  * All rights reserved.
                      4:  *
                      5:  * This package is an SSL implementation written
                      6:  * by Eric Young (eay@cryptsoft.com).
                      7:  * The implementation was written so as to conform with Netscapes SSL.
                      8:  *
                      9:  * This library is free for commercial and non-commercial use as long as
                     10:  * the following conditions are aheared to.  The following conditions
                     11:  * apply to all code found in this distribution, be it the RC4, RSA,
                     12:  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
                     13:  * included with this distribution is covered by the same copyright terms
                     14:  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
                     15:  *
                     16:  * Copyright remains Eric Young's, and as such any Copyright notices in
                     17:  * the code are not to be removed.
                     18:  * If this package is used in a product, Eric Young should be given attribution
                     19:  * as the author of the parts of the library used.
                     20:  * This can be in the form of a textual message at program startup or
                     21:  * in documentation (online or textual) provided with the package.
                     22:  *
                     23:  * Redistribution and use in source and binary forms, with or without
                     24:  * modification, are permitted provided that the following conditions
                     25:  * are met:
                     26:  * 1. Redistributions of source code must retain the copyright
                     27:  *    notice, this list of conditions and the following disclaimer.
                     28:  * 2. Redistributions in binary form must reproduce the above copyright
                     29:  *    notice, this list of conditions and the following disclaimer in the
                     30:  *    documentation and/or other materials provided with the distribution.
                     31:  * 3. All advertising materials mentioning features or use of this software
                     32:  *    must display the following acknowledgement:
                     33:  *    "This product includes cryptographic software written by
                     34:  *     Eric Young (eay@cryptsoft.com)"
                     35:  *    The word 'cryptographic' can be left out if the rouines from the library
                     36:  *    being used are not cryptographic related :-).
                     37:  * 4. If you include any Windows specific code (or a derivative thereof) from
                     38:  *    the apps directory (application code) you must include an acknowledgement:
                     39:  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
                     40:  *
                     41:  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
                     42:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     43:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     44:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     45:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     46:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     47:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     48:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     49:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     50:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     51:  * SUCH DAMAGE.
                     52:  *
                     53:  * The licence and distribution terms for any publically available version or
                     54:  * derivative of this code cannot be changed.  i.e. this code cannot simply be
                     55:  * copied and put under another distribution licence
                     56:  * [including the GNU Public Licence.]
                     57:  */
                     58:
                     59: /* Until the key-gen callbacks are modified to use newer prototypes, we allow
                     60:  * deprecated functions for openssl-internal code */
                     61: #ifdef OPENSSL_NO_DEPRECATED
                     62: #undef OPENSSL_NO_DEPRECATED
                     63: #endif
                     64:
                     65: #include <stdio.h>
                     66: #include <stdlib.h>
                     67: #include <limits.h>
                     68: #include <string.h>
                     69: #include <time.h>
                     70:
                     71: #include "apps.h"
                     72:
                     73: #include <openssl/asn1.h>
                     74: #include <openssl/bio.h>
                     75: #include <openssl/bn.h>
                     76: #include <openssl/conf.h>
                     77: #include <openssl/err.h>
                     78: #include <openssl/evp.h>
                     79: #include <openssl/objects.h>
                     80: #include <openssl/pem.h>
                     81: #include <openssl/x509.h>
                     82: #include <openssl/x509v3.h>
                     83:
                     84: #include <openssl/dsa.h>
                     85:
                     86: #include <openssl/rsa.h>
                     87:
                     88: #define SECTION                "req"
                     89:
                     90: #define BITS           "default_bits"
                     91: #define KEYFILE                "default_keyfile"
                     92: #define PROMPT         "prompt"
                     93: #define DISTINGUISHED_NAME     "distinguished_name"
                     94: #define ATTRIBUTES     "attributes"
                     95: #define V3_EXTENSIONS  "x509_extensions"
                     96: #define REQ_EXTENSIONS "req_extensions"
                     97: #define STRING_MASK    "string_mask"
                     98: #define UTF8_IN                "utf8"
                     99:
1.3       sthen     100: #define DEFAULT_KEY_LENGTH     2048
1.1       jsing     101: #define MIN_KEY_LENGTH         384
                    102:
                    103:
                    104: /* -inform arg - input format - default PEM (DER or PEM)
                    105:  * -outform arg - output format - default PEM
                    106:  * -in arg     - input file - default stdin
                    107:  * -out arg    - output file - default stdout
                    108:  * -verify     - check request signature
                    109:  * -noout      - don't print stuff out.
                    110:  * -text       - print out human readable text.
                    111:  * -nodes      - no des encryption
                    112:  * -config file        - Load configuration file.
                    113:  * -key file   - make a request using key in file (or use it for verification).
                    114:  * -keyform arg        - key file format.
                    115:  * -newkey     - make a key and a request.
                    116:  * -modulus    - print RSA modulus.
                    117:  * -pubkey     - output Public Key.
                    118:  * -x509       - output a self signed X509 structure instead.
                    119:  * -asn1-kludge        - output new certificate request in a format that some CA's
                    120:  *               require.  This format is wrong
                    121:  */
                    122:
                    123: static int make_REQ(X509_REQ * req, EVP_PKEY * pkey, char *dn, int mutlirdn,
                    124:     int attribs, unsigned long chtype);
                    125: static int build_subject(X509_REQ * req, char *subj, unsigned long chtype,
                    126:     int multirdn);
                    127: static int prompt_info(X509_REQ * req,
                    128:     STACK_OF(CONF_VALUE) * dn_sk, char *dn_sect,
                    129:     STACK_OF(CONF_VALUE) * attr_sk, char *attr_sect, int attribs,
                    130:     unsigned long chtype);
                    131: static int auto_info(X509_REQ * req, STACK_OF(CONF_VALUE) * sk,
                    132:     STACK_OF(CONF_VALUE) * attr, int attribs,
                    133:     unsigned long chtype);
                    134: static int add_attribute_object(X509_REQ * req, char *text, const char *def,
                    135:     char *value, int nid, int n_min,
                    136:     int n_max, unsigned long chtype);
                    137: static int add_DN_object(X509_NAME * n, char *text, const char *def, char *value,
                    138:     int nid, int n_min, int n_max, unsigned long chtype, int mval);
                    139: static int genpkey_cb(EVP_PKEY_CTX * ctx);
                    140: static int req_check_len(int len, int n_min, int n_max);
                    141: static int check_end(const char *str, const char *end);
                    142: static EVP_PKEY_CTX *set_keygen_ctx(BIO * err, const char *gstr, int *pkey_type,
1.7       bcook     143:     long *pkeylen, char **palgnam);
1.1       jsing     144: static CONF *req_conf = NULL;
                    145: static int batch = 0;
                    146:
                    147: int
                    148: req_main(int argc, char **argv)
                    149: {
                    150:        unsigned long nmflag = 0, reqflag = 0;
                    151:        int ex = 1, x509 = 0, days = 30;
                    152:        X509 *x509ss = NULL;
                    153:        X509_REQ *req = NULL;
                    154:        EVP_PKEY_CTX *genctx = NULL;
                    155:        const char *keyalg = NULL;
                    156:        char *keyalgstr = NULL;
                    157:        STACK_OF(OPENSSL_STRING) * pkeyopts = NULL, *sigopts = NULL;
                    158:        EVP_PKEY *pkey = NULL;
                    159:        int i = 0, badops = 0, newreq = 0, verbose = 0, pkey_type = -1;
                    160:        long newkey = -1;
                    161:        BIO *in = NULL, *out = NULL;
                    162:        int informat, outformat, verify = 0, noout = 0, text = 0, keyform = FORMAT_PEM;
                    163:        int nodes = 0, kludge = 0, newhdr = 0, subject = 0, pubkey = 0;
                    164:        char *infile, *outfile, *prog, *keyfile = NULL, *template = NULL,
                    165:        *keyout = NULL;
                    166:        char *extensions = NULL;
                    167:        char *req_exts = NULL;
                    168:        const EVP_CIPHER *cipher = NULL;
                    169:        ASN1_INTEGER *serial = NULL;
                    170:        int modulus = 0;
                    171:        char *passargin = NULL, *passargout = NULL;
                    172:        char *passin = NULL, *passout = NULL;
                    173:        char *p;
                    174:        char *subj = NULL;
                    175:        int multirdn = 0;
                    176:        const EVP_MD *md_alg = NULL, *digest = NULL;
                    177:        unsigned long chtype = MBSTRING_ASC;
1.10      doug      178:
                    179:        if (single_execution) {
1.11    ! semarie   180:                if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
1.10      doug      181:                        perror("pledge");
                    182:        }
1.1       jsing     183:
                    184:        req_conf = NULL;
1.3       sthen     185:        cipher = EVP_aes_256_cbc();
                    186:        digest = EVP_sha256();
1.1       jsing     187:
                    188:        infile = NULL;
                    189:        outfile = NULL;
                    190:        informat = FORMAT_PEM;
                    191:        outformat = FORMAT_PEM;
                    192:
                    193:        prog = argv[0];
                    194:        argc--;
                    195:        argv++;
                    196:        while (argc >= 1) {
                    197:                if (strcmp(*argv, "-inform") == 0) {
                    198:                        if (--argc < 1)
                    199:                                goto bad;
                    200:                        informat = str2fmt(*(++argv));
                    201:                } else if (strcmp(*argv, "-outform") == 0) {
                    202:                        if (--argc < 1)
                    203:                                goto bad;
                    204:                        outformat = str2fmt(*(++argv));
                    205:                }
                    206:                else if (strcmp(*argv, "-key") == 0) {
                    207:                        if (--argc < 1)
                    208:                                goto bad;
                    209:                        keyfile = *(++argv);
                    210:                } else if (strcmp(*argv, "-pubkey") == 0) {
                    211:                        pubkey = 1;
                    212:                } else if (strcmp(*argv, "-new") == 0) {
                    213:                        newreq = 1;
                    214:                } else if (strcmp(*argv, "-config") == 0) {
                    215:                        if (--argc < 1)
                    216:                                goto bad;
                    217:                        template = *(++argv);
                    218:                } else if (strcmp(*argv, "-keyform") == 0) {
                    219:                        if (--argc < 1)
                    220:                                goto bad;
                    221:                        keyform = str2fmt(*(++argv));
                    222:                } else if (strcmp(*argv, "-in") == 0) {
                    223:                        if (--argc < 1)
                    224:                                goto bad;
                    225:                        infile = *(++argv);
                    226:                } else if (strcmp(*argv, "-out") == 0) {
                    227:                        if (--argc < 1)
                    228:                                goto bad;
                    229:                        outfile = *(++argv);
                    230:                } else if (strcmp(*argv, "-keyout") == 0) {
                    231:                        if (--argc < 1)
                    232:                                goto bad;
                    233:                        keyout = *(++argv);
                    234:                } else if (strcmp(*argv, "-passin") == 0) {
                    235:                        if (--argc < 1)
                    236:                                goto bad;
                    237:                        passargin = *(++argv);
                    238:                } else if (strcmp(*argv, "-passout") == 0) {
                    239:                        if (--argc < 1)
                    240:                                goto bad;
                    241:                        passargout = *(++argv);
                    242:                } else if (strcmp(*argv, "-newkey") == 0) {
                    243:                        if (--argc < 1)
                    244:                                goto bad;
                    245:                        keyalg = *(++argv);
                    246:                        newreq = 1;
                    247:                } else if (strcmp(*argv, "-pkeyopt") == 0) {
                    248:                        if (--argc < 1)
                    249:                                goto bad;
                    250:                        if (!pkeyopts)
                    251:                                pkeyopts = sk_OPENSSL_STRING_new_null();
                    252:                        if (!pkeyopts || !sk_OPENSSL_STRING_push(pkeyopts, *(++argv)))
                    253:                                goto bad;
                    254:                } else if (strcmp(*argv, "-sigopt") == 0) {
                    255:                        if (--argc < 1)
                    256:                                goto bad;
                    257:                        if (!sigopts)
                    258:                                sigopts = sk_OPENSSL_STRING_new_null();
                    259:                        if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv)))
                    260:                                goto bad;
                    261:                } else if (strcmp(*argv, "-batch") == 0)
                    262:                        batch = 1;
                    263:                else if (strcmp(*argv, "-newhdr") == 0)
                    264:                        newhdr = 1;
                    265:                else if (strcmp(*argv, "-modulus") == 0)
                    266:                        modulus = 1;
                    267:                else if (strcmp(*argv, "-verify") == 0)
                    268:                        verify = 1;
                    269:                else if (strcmp(*argv, "-nodes") == 0)
                    270:                        nodes = 1;
                    271:                else if (strcmp(*argv, "-noout") == 0)
                    272:                        noout = 1;
                    273:                else if (strcmp(*argv, "-verbose") == 0)
                    274:                        verbose = 1;
                    275:                else if (strcmp(*argv, "-utf8") == 0)
                    276:                        chtype = MBSTRING_UTF8;
                    277:                else if (strcmp(*argv, "-nameopt") == 0) {
                    278:                        if (--argc < 1)
                    279:                                goto bad;
                    280:                        if (!set_name_ex(&nmflag, *(++argv)))
                    281:                                goto bad;
                    282:                } else if (strcmp(*argv, "-reqopt") == 0) {
                    283:                        if (--argc < 1)
                    284:                                goto bad;
                    285:                        if (!set_cert_ex(&reqflag, *(++argv)))
                    286:                                goto bad;
                    287:                } else if (strcmp(*argv, "-subject") == 0)
                    288:                        subject = 1;
                    289:                else if (strcmp(*argv, "-text") == 0)
                    290:                        text = 1;
                    291:                else if (strcmp(*argv, "-x509") == 0)
                    292:                        x509 = 1;
                    293:                else if (strcmp(*argv, "-asn1-kludge") == 0)
                    294:                        kludge = 1;
                    295:                else if (strcmp(*argv, "-no-asn1-kludge") == 0)
                    296:                        kludge = 0;
                    297:                else if (strcmp(*argv, "-subj") == 0) {
                    298:                        if (--argc < 1)
                    299:                                goto bad;
                    300:                        subj = *(++argv);
                    301:                } else if (strcmp(*argv, "-multivalue-rdn") == 0)
                    302:                        multirdn = 1;
                    303:                else if (strcmp(*argv, "-days") == 0) {
                    304:                        const char *errstr;
                    305:
                    306:                        if (--argc < 1)
                    307:                                goto bad;
                    308:                        days = strtonum(*(++argv), 1, INT_MAX, &errstr);
                    309:                        if (errstr) {
                    310:                                BIO_printf(bio_err, "bad -days %s, using 0: %s\n",
                    311:                                    *argv, errstr);
                    312:                                days = 30;
                    313:                        }
                    314:                } else if (strcmp(*argv, "-set_serial") == 0) {
                    315:                        if (--argc < 1)
                    316:                                goto bad;
                    317:                        serial = s2i_ASN1_INTEGER(NULL, *(++argv));
                    318:                        if (!serial)
                    319:                                goto bad;
                    320:                } else if (strcmp(*argv, "-extensions") == 0) {
                    321:                        if (--argc < 1)
                    322:                                goto bad;
                    323:                        extensions = *(++argv);
                    324:                } else if (strcmp(*argv, "-reqexts") == 0) {
                    325:                        if (--argc < 1)
                    326:                                goto bad;
                    327:                        req_exts = *(++argv);
                    328:                } else if ((md_alg = EVP_get_digestbyname(&((*argv)[1]))) != NULL) {
                    329:                        /* ok */
                    330:                        digest = md_alg;
                    331:                } else {
                    332:                        BIO_printf(bio_err, "unknown option %s\n", *argv);
                    333:                        badops = 1;
                    334:                        break;
                    335:                }
                    336:                argc--;
                    337:                argv++;
                    338:        }
                    339:
                    340:        if (badops) {
                    341: bad:
                    342:                BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
                    343:                BIO_printf(bio_err, "where options  are\n");
                    344:                BIO_printf(bio_err, " -inform arg    input format - DER or PEM\n");
                    345:                BIO_printf(bio_err, " -outform arg   output format - DER or PEM\n");
                    346:                BIO_printf(bio_err, " -in arg        input file\n");
                    347:                BIO_printf(bio_err, " -out arg       output file\n");
                    348:                BIO_printf(bio_err, " -text          text form of request\n");
                    349:                BIO_printf(bio_err, " -pubkey        output public key\n");
                    350:                BIO_printf(bio_err, " -noout         do not output REQ\n");
                    351:                BIO_printf(bio_err, " -verify        verify signature on REQ\n");
                    352:                BIO_printf(bio_err, " -modulus       RSA modulus\n");
                    353:                BIO_printf(bio_err, " -nodes         don't encrypt the output key\n");
                    354:                BIO_printf(bio_err, " -subject       output the request's subject\n");
                    355:                BIO_printf(bio_err, " -passin        private key password source\n");
                    356:                BIO_printf(bio_err, " -key file      use the private key contained in file\n");
                    357:                BIO_printf(bio_err, " -keyform arg   key file format\n");
                    358:                BIO_printf(bio_err, " -keyout arg    file to send the key to\n");
                    359:                BIO_printf(bio_err, " -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
                    360:                BIO_printf(bio_err, " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
                    361:                BIO_printf(bio_err, " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
1.9       doug      362:                BIO_printf(bio_err, " -[digest]      Digest to sign with (md5, sha1, md4)\n");
1.1       jsing     363:                BIO_printf(bio_err, " -config file   request template file.\n");
                    364:                BIO_printf(bio_err, " -subj arg      set or modify request subject\n");
                    365:                BIO_printf(bio_err, " -multivalue-rdn enable support for multivalued RDNs\n");
                    366:                BIO_printf(bio_err, " -new           new request.\n");
                    367:                BIO_printf(bio_err, " -batch         do not ask anything during request generation\n");
                    368:                BIO_printf(bio_err, " -x509          output a x509 structure instead of a cert. req.\n");
                    369:                BIO_printf(bio_err, " -days          number of days a certificate generated by -x509 is valid for.\n");
                    370:                BIO_printf(bio_err, " -set_serial    serial number to use for a certificate generated by -x509.\n");
                    371:                BIO_printf(bio_err, " -newhdr        output \"NEW\" in the header lines\n");
                    372:                BIO_printf(bio_err, " -asn1-kludge   Output the 'request' in a format that is wrong but some CA's\n");
                    373:                BIO_printf(bio_err, "                have been reported as requiring\n");
                    374:                BIO_printf(bio_err, " -extensions .. specify certificate extension section (override value in config file)\n");
                    375:                BIO_printf(bio_err, " -reqexts ..    specify request extension section (override value in config file)\n");
                    376:                BIO_printf(bio_err, " -utf8          input characters are UTF8 (default ASCII)\n");
                    377:                BIO_printf(bio_err, " -nameopt arg    - various certificate name options\n");
                    378:                BIO_printf(bio_err, " -reqopt arg    - various request text options\n\n");
                    379:                goto end;
                    380:        }
1.2       jsing     381:
1.1       jsing     382:        if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
                    383:                BIO_printf(bio_err, "Error getting passwords\n");
                    384:                goto end;
                    385:        }
                    386:        if (template != NULL) {
                    387:                long errline = -1;
                    388:
                    389:                if (verbose)
                    390:                        BIO_printf(bio_err, "Using configuration from %s\n", template);
                    391:                req_conf = NCONF_new(NULL);
                    392:                i = NCONF_load(req_conf, template, &errline);
                    393:                if (i == 0) {
                    394:                        BIO_printf(bio_err, "error on line %ld of %s\n", errline, template);
                    395:                        goto end;
                    396:                }
                    397:        } else {
                    398:                req_conf = config;
                    399:
                    400:                if (req_conf == NULL) {
                    401:                        BIO_printf(bio_err, "Unable to load config info from %s\n", default_config_file);
                    402:                        if (newreq)
                    403:                                goto end;
                    404:                } else if (verbose)
                    405:                        BIO_printf(bio_err, "Using configuration from %s\n",
                    406:                            default_config_file);
                    407:        }
                    408:
                    409:        if (req_conf != NULL) {
                    410:                if (!load_config(bio_err, req_conf))
                    411:                        goto end;
                    412:                p = NCONF_get_string(req_conf, NULL, "oid_file");
                    413:                if (p == NULL)
                    414:                        ERR_clear_error();
                    415:                if (p != NULL) {
                    416:                        BIO *oid_bio;
                    417:
                    418:                        oid_bio = BIO_new_file(p, "r");
                    419:                        if (oid_bio == NULL) {
                    420:                                /*
                    421:                                BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
                    422:                                ERR_print_errors(bio_err);
                    423:                                */
                    424:                        } else {
                    425:                                OBJ_create_objects(oid_bio);
                    426:                                BIO_free(oid_bio);
                    427:                        }
                    428:                }
                    429:        }
                    430:        if (!add_oid_section(bio_err, req_conf))
                    431:                goto end;
                    432:
                    433:        if (md_alg == NULL) {
                    434:                p = NCONF_get_string(req_conf, SECTION, "default_md");
                    435:                if (p == NULL)
                    436:                        ERR_clear_error();
                    437:                if (p != NULL) {
                    438:                        if ((md_alg = EVP_get_digestbyname(p)) != NULL)
                    439:                                digest = md_alg;
                    440:                }
                    441:        }
                    442:        if (!extensions) {
                    443:                extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
                    444:                if (!extensions)
                    445:                        ERR_clear_error();
                    446:        }
                    447:        if (extensions) {
                    448:                /* Check syntax of file */
                    449:                X509V3_CTX ctx;
                    450:                X509V3_set_ctx_test(&ctx);
                    451:                X509V3_set_nconf(&ctx, req_conf);
                    452:                if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
                    453:                        BIO_printf(bio_err,
                    454:                            "Error Loading extension section %s\n", extensions);
                    455:                        goto end;
                    456:                }
                    457:        }
                    458:        if (!passin) {
                    459:                passin = NCONF_get_string(req_conf, SECTION, "input_password");
                    460:                if (!passin)
                    461:                        ERR_clear_error();
                    462:        }
                    463:        if (!passout) {
                    464:                passout = NCONF_get_string(req_conf, SECTION, "output_password");
                    465:                if (!passout)
                    466:                        ERR_clear_error();
                    467:        }
                    468:        p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
                    469:        if (!p)
                    470:                ERR_clear_error();
                    471:
                    472:        if (p && !ASN1_STRING_set_default_mask_asc(p)) {
                    473:                BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
                    474:                goto end;
                    475:        }
                    476:        if (chtype != MBSTRING_UTF8) {
                    477:                p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
                    478:                if (!p)
                    479:                        ERR_clear_error();
                    480:                else if (!strcmp(p, "yes"))
                    481:                        chtype = MBSTRING_UTF8;
                    482:        }
                    483:        if (!req_exts) {
                    484:                req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
                    485:                if (!req_exts)
                    486:                        ERR_clear_error();
                    487:        }
                    488:        if (req_exts) {
                    489:                /* Check syntax of file */
                    490:                X509V3_CTX ctx;
                    491:                X509V3_set_ctx_test(&ctx);
                    492:                X509V3_set_nconf(&ctx, req_conf);
                    493:                if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
                    494:                        BIO_printf(bio_err,
                    495:                            "Error Loading request extension section %s\n",
                    496:                            req_exts);
                    497:                        goto end;
                    498:                }
                    499:        }
                    500:        in = BIO_new(BIO_s_file());
                    501:        out = BIO_new(BIO_s_file());
                    502:        if ((in == NULL) || (out == NULL))
                    503:                goto end;
                    504:
                    505:        if (keyfile != NULL) {
1.7       bcook     506:                pkey = load_key(bio_err, keyfile, keyform, 0, passin,
1.1       jsing     507:                    "Private Key");
                    508:                if (!pkey) {
                    509:                        /*
                    510:                         * load_key() has already printed an appropriate
                    511:                         * message
                    512:                         */
                    513:                        goto end;
                    514:                }
                    515:        }
                    516:        if (newreq && (pkey == NULL)) {
                    517:                if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) {
                    518:                        newkey = DEFAULT_KEY_LENGTH;
                    519:                }
                    520:                if (keyalg) {
                    521:                        genctx = set_keygen_ctx(bio_err, keyalg, &pkey_type, &newkey,
1.7       bcook     522:                            &keyalgstr);
1.1       jsing     523:                        if (!genctx)
                    524:                                goto end;
                    525:                }
                    526:                if (newkey < MIN_KEY_LENGTH && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) {
                    527:                        BIO_printf(bio_err, "private key length is too short,\n");
                    528:                        BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n", MIN_KEY_LENGTH, newkey);
                    529:                        goto end;
                    530:                }
                    531:                if (!genctx) {
                    532:                        genctx = set_keygen_ctx(bio_err, NULL, &pkey_type, &newkey,
1.7       bcook     533:                            &keyalgstr);
1.1       jsing     534:                        if (!genctx)
                    535:                                goto end;
                    536:                }
                    537:                if (pkeyopts) {
                    538:                        char *genopt;
                    539:                        for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
                    540:                                genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
                    541:                                if (pkey_ctrl_string(genctx, genopt) <= 0) {
                    542:                                        BIO_printf(bio_err,
                    543:                                            "parameter error \"%s\"\n",
                    544:                                            genopt);
                    545:                                        ERR_print_errors(bio_err);
                    546:                                        goto end;
                    547:                                }
                    548:                        }
                    549:                }
                    550:                BIO_printf(bio_err, "Generating a %ld bit %s private key\n",
                    551:                    newkey, keyalgstr);
                    552:
                    553:                EVP_PKEY_CTX_set_cb(genctx, genpkey_cb);
                    554:                EVP_PKEY_CTX_set_app_data(genctx, bio_err);
                    555:
                    556:                if (EVP_PKEY_keygen(genctx, &pkey) <= 0) {
                    557:                        BIO_puts(bio_err, "Error Generating Key\n");
                    558:                        goto end;
                    559:                }
                    560:                EVP_PKEY_CTX_free(genctx);
                    561:                genctx = NULL;
                    562:
                    563:                if (keyout == NULL) {
                    564:                        keyout = NCONF_get_string(req_conf, SECTION, KEYFILE);
                    565:                        if (keyout == NULL)
                    566:                                ERR_clear_error();
                    567:                }
                    568:                if (keyout == NULL) {
                    569:                        BIO_printf(bio_err, "writing new private key to stdout\n");
                    570:                        BIO_set_fp(out, stdout, BIO_NOCLOSE);
                    571:                } else {
                    572:                        BIO_printf(bio_err, "writing new private key to '%s'\n", keyout);
                    573:                        if (BIO_write_filename(out, keyout) <= 0) {
                    574:                                perror(keyout);
                    575:                                goto end;
                    576:                        }
                    577:                }
                    578:
                    579:                p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key");
                    580:                if (p == NULL) {
                    581:                        ERR_clear_error();
                    582:                        p = NCONF_get_string(req_conf, SECTION, "encrypt_key");
                    583:                        if (p == NULL)
                    584:                                ERR_clear_error();
                    585:                }
                    586:                if ((p != NULL) && (strcmp(p, "no") == 0))
                    587:                        cipher = NULL;
                    588:                if (nodes)
                    589:                        cipher = NULL;
                    590:
                    591:                i = 0;
                    592: loop:
                    593:                if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
                    594:                        NULL, 0, NULL, passout)) {
                    595:                        if ((ERR_GET_REASON(ERR_peek_error()) ==
                    596:                                PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) {
                    597:                                ERR_clear_error();
                    598:                                i++;
                    599:                                goto loop;
                    600:                        }
                    601:                        goto end;
                    602:                }
                    603:                BIO_printf(bio_err, "-----\n");
                    604:        }
                    605:        if (!newreq) {
                    606:                /*
                    607:                 * Since we are using a pre-existing certificate request, the
                    608:                 * kludge 'format' info should not be changed.
                    609:                 */
                    610:                kludge = -1;
                    611:                if (infile == NULL)
                    612:                        BIO_set_fp(in, stdin, BIO_NOCLOSE);
                    613:                else {
                    614:                        if (BIO_read_filename(in, infile) <= 0) {
                    615:                                perror(infile);
                    616:                                goto end;
                    617:                        }
                    618:                }
                    619:
                    620:                if (informat == FORMAT_ASN1)
                    621:                        req = d2i_X509_REQ_bio(in, NULL);
                    622:                else if (informat == FORMAT_PEM)
                    623:                        req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
                    624:                else {
                    625:                        BIO_printf(bio_err, "bad input format specified for X509 request\n");
                    626:                        goto end;
                    627:                }
                    628:                if (req == NULL) {
                    629:                        BIO_printf(bio_err, "unable to load X509 request\n");
                    630:                        goto end;
                    631:                }
                    632:        }
                    633:        if (newreq || x509) {
                    634:                if (pkey == NULL) {
                    635:                        BIO_printf(bio_err, "you need to specify a private key\n");
                    636:                        goto end;
                    637:                }
                    638:                if (req == NULL) {
                    639:                        req = X509_REQ_new();
                    640:                        if (req == NULL) {
                    641:                                goto end;
                    642:                        }
                    643:                        i = make_REQ(req, pkey, subj, multirdn, !x509, chtype);
                    644:                        subj = NULL;    /* done processing '-subj' option */
                    645:                        if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) {
                    646:                                sk_X509_ATTRIBUTE_free(req->req_info->attributes);
                    647:                                req->req_info->attributes = NULL;
                    648:                        }
                    649:                        if (!i) {
                    650:                                BIO_printf(bio_err, "problems making Certificate Request\n");
                    651:                                goto end;
                    652:                        }
                    653:                }
                    654:                if (x509) {
                    655:                        EVP_PKEY *tmppkey;
                    656:                        X509V3_CTX ext_ctx;
                    657:                        if ((x509ss = X509_new()) == NULL)
                    658:                                goto end;
                    659:
                    660:                        /* Set version to V3 */
                    661:                        if (extensions && !X509_set_version(x509ss, 2))
                    662:                                goto end;
                    663:                        if (serial) {
                    664:                                if (!X509_set_serialNumber(x509ss, serial))
                    665:                                        goto end;
                    666:                        } else {
                    667:                                if (!rand_serial(NULL,
                    668:                                        X509_get_serialNumber(x509ss)))
                    669:                                        goto end;
                    670:                        }
                    671:
                    672:                        if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)))
                    673:                                goto end;
                    674:                        if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0))
                    675:                                goto end;
                    676:                        if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL))
                    677:                                goto end;
                    678:                        if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req)))
                    679:                                goto end;
                    680:                        tmppkey = X509_REQ_get_pubkey(req);
                    681:                        if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey))
                    682:                                goto end;
                    683:                        EVP_PKEY_free(tmppkey);
                    684:
                    685:                        /* Set up V3 context struct */
                    686:
                    687:                        X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
                    688:                        X509V3_set_nconf(&ext_ctx, req_conf);
                    689:
                    690:                        /* Add extensions */
                    691:                        if (extensions && !X509V3_EXT_add_nconf(req_conf,
                    692:                                &ext_ctx, extensions, x509ss)) {
                    693:                                BIO_printf(bio_err,
                    694:                                    "Error Loading extension section %s\n",
                    695:                                    extensions);
                    696:                                goto end;
                    697:                        }
                    698:                        i = do_X509_sign(bio_err, x509ss, pkey, digest, sigopts);
                    699:                        if (!i) {
                    700:                                ERR_print_errors(bio_err);
                    701:                                goto end;
                    702:                        }
                    703:                } else {
                    704:                        X509V3_CTX ext_ctx;
                    705:
                    706:                        /* Set up V3 context struct */
                    707:
                    708:                        X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
                    709:                        X509V3_set_nconf(&ext_ctx, req_conf);
                    710:
                    711:                        /* Add extensions */
                    712:                        if (req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
                    713:                                &ext_ctx, req_exts, req)) {
                    714:                                BIO_printf(bio_err,
                    715:                                    "Error Loading extension section %s\n",
                    716:                                    req_exts);
                    717:                                goto end;
                    718:                        }
                    719:                        i = do_X509_REQ_sign(bio_err, req, pkey, digest, sigopts);
                    720:                        if (!i) {
                    721:                                ERR_print_errors(bio_err);
                    722:                                goto end;
                    723:                        }
                    724:                }
                    725:        }
                    726:        if (subj && x509) {
                    727:                BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
                    728:                goto end;
                    729:        }
                    730:        if (subj && !x509) {
                    731:                if (verbose) {
                    732:                        BIO_printf(bio_err, "Modifying Request's Subject\n");
                    733:                        print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag);
                    734:                }
                    735:                if (build_subject(req, subj, chtype, multirdn) == 0) {
                    736:                        BIO_printf(bio_err, "ERROR: cannot modify subject\n");
                    737:                        ex = 1;
                    738:                        goto end;
                    739:                }
                    740:                req->req_info->enc.modified = 1;
                    741:
                    742:                if (verbose) {
                    743:                        print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag);
                    744:                }
                    745:        }
                    746:        if (verify && !x509) {
                    747:                int tmp = 0;
                    748:
                    749:                if (pkey == NULL) {
                    750:                        pkey = X509_REQ_get_pubkey(req);
                    751:                        tmp = 1;
                    752:                        if (pkey == NULL)
                    753:                                goto end;
                    754:                }
                    755:                i = X509_REQ_verify(req, pkey);
                    756:                if (tmp) {
                    757:                        EVP_PKEY_free(pkey);
                    758:                        pkey = NULL;
                    759:                }
                    760:                if (i < 0) {
                    761:                        goto end;
                    762:                } else if (i == 0) {
                    763:                        BIO_printf(bio_err, "verify failure\n");
                    764:                        ERR_print_errors(bio_err);
                    765:                } else          /* if (i > 0) */
                    766:                        BIO_printf(bio_err, "verify OK\n");
                    767:        }
                    768:        if (noout && !text && !modulus && !subject && !pubkey) {
                    769:                ex = 0;
                    770:                goto end;
                    771:        }
                    772:        if (outfile == NULL) {
                    773:                BIO_set_fp(out, stdout, BIO_NOCLOSE);
                    774:        } else {
                    775:                if ((keyout != NULL) && (strcmp(outfile, keyout) == 0))
                    776:                        i = (int) BIO_append_filename(out, outfile);
                    777:                else
                    778:                        i = (int) BIO_write_filename(out, outfile);
                    779:                if (!i) {
                    780:                        perror(outfile);
                    781:                        goto end;
                    782:                }
                    783:        }
                    784:
                    785:        if (pubkey) {
                    786:                EVP_PKEY *tpubkey;
                    787:                tpubkey = X509_REQ_get_pubkey(req);
                    788:                if (tpubkey == NULL) {
                    789:                        BIO_printf(bio_err, "Error getting public key\n");
                    790:                        ERR_print_errors(bio_err);
                    791:                        goto end;
                    792:                }
                    793:                PEM_write_bio_PUBKEY(out, tpubkey);
                    794:                EVP_PKEY_free(tpubkey);
                    795:        }
                    796:        if (text) {
                    797:                if (x509)
                    798:                        X509_print_ex(out, x509ss, nmflag, reqflag);
                    799:                else
                    800:                        X509_REQ_print_ex(out, req, nmflag, reqflag);
                    801:        }
                    802:        if (subject) {
                    803:                if (x509)
                    804:                        print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag);
                    805:                else
                    806:                        print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag);
                    807:        }
                    808:        if (modulus) {
                    809:                EVP_PKEY *tpubkey;
                    810:
                    811:                if (x509)
                    812:                        tpubkey = X509_get_pubkey(x509ss);
                    813:                else
                    814:                        tpubkey = X509_REQ_get_pubkey(req);
                    815:                if (tpubkey == NULL) {
                    816:                        fprintf(stdout, "Modulus=unavailable\n");
                    817:                        goto end;
                    818:                }
                    819:                fprintf(stdout, "Modulus=");
                    820:                if (EVP_PKEY_base_id(tpubkey) == EVP_PKEY_RSA)
                    821:                        BN_print(out, tpubkey->pkey.rsa->n);
                    822:                else
                    823:                        fprintf(stdout, "Wrong Algorithm type");
                    824:                EVP_PKEY_free(tpubkey);
                    825:                fprintf(stdout, "\n");
                    826:        }
                    827:        if (!noout && !x509) {
                    828:                if (outformat == FORMAT_ASN1)
                    829:                        i = i2d_X509_REQ_bio(out, req);
                    830:                else if (outformat == FORMAT_PEM) {
                    831:                        if (newhdr)
                    832:                                i = PEM_write_bio_X509_REQ_NEW(out, req);
                    833:                        else
                    834:                                i = PEM_write_bio_X509_REQ(out, req);
                    835:                } else {
                    836:                        BIO_printf(bio_err, "bad output format specified for outfile\n");
                    837:                        goto end;
                    838:                }
                    839:                if (!i) {
                    840:                        BIO_printf(bio_err, "unable to write X509 request\n");
                    841:                        goto end;
                    842:                }
                    843:        }
                    844:        if (!noout && x509 && (x509ss != NULL)) {
                    845:                if (outformat == FORMAT_ASN1)
                    846:                        i = i2d_X509_bio(out, x509ss);
                    847:                else if (outformat == FORMAT_PEM)
                    848:                        i = PEM_write_bio_X509(out, x509ss);
                    849:                else {
                    850:                        BIO_printf(bio_err, "bad output format specified for outfile\n");
                    851:                        goto end;
                    852:                }
                    853:                if (!i) {
                    854:                        BIO_printf(bio_err, "unable to write X509 certificate\n");
                    855:                        goto end;
                    856:                }
                    857:        }
                    858:        ex = 0;
                    859: end:
                    860:        if (ex) {
                    861:                ERR_print_errors(bio_err);
                    862:        }
                    863:        if ((req_conf != NULL) && (req_conf != config))
                    864:                NCONF_free(req_conf);
                    865:        BIO_free(in);
                    866:        BIO_free_all(out);
                    867:        EVP_PKEY_free(pkey);
                    868:        if (genctx)
                    869:                EVP_PKEY_CTX_free(genctx);
                    870:        if (pkeyopts)
                    871:                sk_OPENSSL_STRING_free(pkeyopts);
                    872:        if (sigopts)
                    873:                sk_OPENSSL_STRING_free(sigopts);
                    874:        free(keyalgstr);
                    875:        X509_REQ_free(req);
                    876:        X509_free(x509ss);
                    877:        ASN1_INTEGER_free(serial);
                    878:        if (passargin && passin)
                    879:                free(passin);
                    880:        if (passargout && passout)
                    881:                free(passout);
                    882:        OBJ_cleanup();
                    883:
                    884:        return (ex);
                    885: }
                    886:
                    887: static int
                    888: make_REQ(X509_REQ * req, EVP_PKEY * pkey, char *subj, int multirdn,
                    889:     int attribs, unsigned long chtype)
                    890: {
                    891:        int ret = 0, i;
                    892:        char no_prompt = 0;
                    893:        STACK_OF(CONF_VALUE) * dn_sk, *attr_sk = NULL;
                    894:        char *tmp, *dn_sect, *attr_sect;
                    895:
                    896:        tmp = NCONF_get_string(req_conf, SECTION, PROMPT);
                    897:        if (tmp == NULL)
                    898:                ERR_clear_error();
                    899:        if ((tmp != NULL) && !strcmp(tmp, "no"))
                    900:                no_prompt = 1;
                    901:
                    902:        dn_sect = NCONF_get_string(req_conf, SECTION, DISTINGUISHED_NAME);
                    903:        if (dn_sect == NULL) {
                    904:                BIO_printf(bio_err, "unable to find '%s' in config\n",
                    905:                    DISTINGUISHED_NAME);
                    906:                goto err;
                    907:        }
                    908:        dn_sk = NCONF_get_section(req_conf, dn_sect);
                    909:        if (dn_sk == NULL) {
                    910:                BIO_printf(bio_err, "unable to get '%s' section\n", dn_sect);
                    911:                goto err;
                    912:        }
                    913:        attr_sect = NCONF_get_string(req_conf, SECTION, ATTRIBUTES);
                    914:        if (attr_sect == NULL) {
                    915:                ERR_clear_error();
                    916:                attr_sk = NULL;
                    917:        } else {
                    918:                attr_sk = NCONF_get_section(req_conf, attr_sect);
                    919:                if (attr_sk == NULL) {
                    920:                        BIO_printf(bio_err, "unable to get '%s' section\n", attr_sect);
                    921:                        goto err;
                    922:                }
                    923:        }
                    924:
                    925:        /* setup version number */
                    926:        if (!X509_REQ_set_version(req, 0L))
                    927:                goto err;       /* version 1 */
                    928:
                    929:        if (no_prompt)
                    930:                i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
                    931:        else {
                    932:                if (subj)
                    933:                        i = build_subject(req, subj, chtype, multirdn);
                    934:                else
                    935:                        i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype);
                    936:        }
                    937:        if (!i)
                    938:                goto err;
                    939:
                    940:        if (!X509_REQ_set_pubkey(req, pkey))
                    941:                goto err;
                    942:
                    943:        ret = 1;
                    944: err:
                    945:        return (ret);
                    946: }
                    947:
                    948: /*
                    949:  * subject is expected to be in the format /type0=value0/type1=value1/type2=...
                    950:  * where characters may be escaped by \
                    951:  */
                    952: static int
                    953: build_subject(X509_REQ * req, char *subject, unsigned long chtype, int multirdn)
                    954: {
                    955:        X509_NAME *n;
                    956:
                    957:        if (!(n = parse_name(subject, chtype, multirdn)))
                    958:                return 0;
                    959:
                    960:        if (!X509_REQ_set_subject_name(req, n)) {
                    961:                X509_NAME_free(n);
                    962:                return 0;
                    963:        }
                    964:        X509_NAME_free(n);
                    965:        return 1;
                    966: }
                    967:
                    968:
                    969: static int
                    970: prompt_info(X509_REQ * req,
                    971:     STACK_OF(CONF_VALUE) * dn_sk, char *dn_sect,
                    972:     STACK_OF(CONF_VALUE) * attr_sk, char *attr_sect, int attribs,
                    973:     unsigned long chtype)
                    974: {
                    975:        int i;
                    976:        char *p, *q;
                    977:        char buf[100];
                    978:        int nid, mval;
                    979:        long n_min, n_max;
                    980:        char *type, *value;
                    981:        const char *def;
                    982:        CONF_VALUE *v;
                    983:        X509_NAME *subj;
                    984:        subj = X509_REQ_get_subject_name(req);
                    985:
                    986:        if (!batch) {
                    987:                BIO_printf(bio_err, "You are about to be asked to enter information that will be incorporated\n");
                    988:                BIO_printf(bio_err, "into your certificate request.\n");
                    989:                BIO_printf(bio_err, "What you are about to enter is what is called a Distinguished Name or a DN.\n");
                    990:                BIO_printf(bio_err, "There are quite a few fields but you can leave some blank\n");
                    991:                BIO_printf(bio_err, "For some fields there will be a default value,\n");
                    992:                BIO_printf(bio_err, "If you enter '.', the field will be left blank.\n");
                    993:                BIO_printf(bio_err, "-----\n");
                    994:        }
                    995:        if (sk_CONF_VALUE_num(dn_sk)) {
                    996:                i = -1;
                    997: start:         for (;;) {
                    998:                        int ret;
                    999:                        i++;
                   1000:                        if (sk_CONF_VALUE_num(dn_sk) <= i)
                   1001:                                break;
                   1002:
                   1003:                        v = sk_CONF_VALUE_value(dn_sk, i);
                   1004:                        p = q = NULL;
                   1005:                        type = v->name;
                   1006:                        if (!check_end(type, "_min") || !check_end(type, "_max") ||
                   1007:                            !check_end(type, "_default") ||
                   1008:                            !check_end(type, "_value"))
                   1009:                                continue;
                   1010:                        /*
                   1011:                         * Skip past any leading X. X: X, etc to allow for
                   1012:                         * multiple instances
                   1013:                         */
                   1014:                        for (p = v->name; *p; p++)
                   1015:                                if ((*p == ':') || (*p == ',') ||
                   1016:                                    (*p == '.')) {
                   1017:                                        p++;
                   1018:                                        if (*p)
                   1019:                                                type = p;
                   1020:                                        break;
                   1021:                                }
                   1022:                        if (*type == '+') {
                   1023:                                mval = -1;
                   1024:                                type++;
                   1025:                        } else
                   1026:                                mval = 0;
                   1027:                        /* If OBJ not recognised ignore it */
                   1028:                        if ((nid = OBJ_txt2nid(type)) == NID_undef)
                   1029:                                goto start;
                   1030:                        ret = snprintf(buf, sizeof buf, "%s_default", v->name);
                   1031:                        if (ret == -1 || ret >= sizeof(buf)) {
                   1032:                                BIO_printf(bio_err, "Name '%s' too long for default\n",
                   1033:                                    v->name);
                   1034:                                return 0;
                   1035:                        }
                   1036:                        if ((def = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
                   1037:                                ERR_clear_error();
                   1038:                                def = "";
                   1039:                        }
                   1040:                        ret = snprintf(buf, sizeof buf, "%s_value", v->name);
                   1041:                        if (ret == -1 || ret >= sizeof(buf)) {
                   1042:                                BIO_printf(bio_err, "Name '%s' too long for value\n",
                   1043:                                    v->name);
                   1044:                                return 0;
                   1045:                        }
                   1046:                        if ((value = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
                   1047:                                ERR_clear_error();
                   1048:                                value = NULL;
                   1049:                        }
                   1050:                        ret = snprintf(buf, sizeof buf, "%s_min", v->name);
                   1051:                        if (ret == -1 || ret >= sizeof(buf)) {
                   1052:                                BIO_printf(bio_err, "Name '%s' too long for min\n",
                   1053:                                    v->name);
                   1054:                                return 0;
                   1055:                        }
                   1056:                        if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) {
                   1057:                                ERR_clear_error();
                   1058:                                n_min = -1;
                   1059:                        }
                   1060:                        ret = snprintf(buf, sizeof buf, "%s_max", v->name);
                   1061:                        if (ret == -1 || ret >= sizeof(buf)) {
                   1062:                                BIO_printf(bio_err, "Name '%s' too long for max\n",
                   1063:                                    v->name);
                   1064:                                return 0;
                   1065:                        }
                   1066:                        if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) {
                   1067:                                ERR_clear_error();
                   1068:                                n_max = -1;
                   1069:                        }
                   1070:                        if (!add_DN_object(subj, v->value, def, value, nid,
                   1071:                                n_min, n_max, chtype, mval))
                   1072:                                return 0;
                   1073:                }
                   1074:                if (X509_NAME_entry_count(subj) == 0) {
                   1075:                        BIO_printf(bio_err, "error, no objects specified in config file\n");
                   1076:                        return 0;
                   1077:                }
                   1078:                if (attribs) {
                   1079:                        if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) &&
                   1080:                            (!batch)) {
                   1081:                                BIO_printf(bio_err,
                   1082:                                    "\nPlease enter the following 'extra' attributes\n");
                   1083:                                BIO_printf(bio_err,
                   1084:                                    "to be sent with your certificate request\n");
                   1085:                        }
                   1086:                        i = -1;
                   1087: start2:                        for (;;) {
                   1088:                                int ret;
                   1089:                                i++;
                   1090:                                if ((attr_sk == NULL) ||
                   1091:                                    (sk_CONF_VALUE_num(attr_sk) <= i))
                   1092:                                        break;
                   1093:
                   1094:                                v = sk_CONF_VALUE_value(attr_sk, i);
                   1095:                                type = v->name;
                   1096:                                if ((nid = OBJ_txt2nid(type)) == NID_undef)
                   1097:                                        goto start2;
                   1098:                                ret = snprintf(buf, sizeof buf, "%s_default", type);
                   1099:                                if (ret == -1 || ret >= sizeof(buf)) {
                   1100:                                        BIO_printf(bio_err, "Name '%s' too long for default\n",
                   1101:                                            v->name);
                   1102:                                        return 0;
                   1103:                                }
                   1104:                                if ((def = NCONF_get_string(req_conf, attr_sect, buf))
                   1105:                                    == NULL) {
                   1106:                                        ERR_clear_error();
                   1107:                                        def = "";
                   1108:                                }
                   1109:                                ret = snprintf(buf, sizeof buf, "%s_value", type);
                   1110:                                if (ret == -1 || ret >= sizeof(buf)) {
                   1111:                                        BIO_printf(bio_err, "Name '%s' too long for value\n",
                   1112:                                            v->name);
                   1113:                                        return 0;
                   1114:                                }
                   1115:                                if ((value = NCONF_get_string(req_conf, attr_sect, buf))
                   1116:                                    == NULL) {
                   1117:                                        ERR_clear_error();
                   1118:                                        value = NULL;
                   1119:                                }
                   1120:                                ret = snprintf(buf, sizeof buf, "%s_min", type);
                   1121:                                if (ret == -1 || ret >= sizeof(buf)) {
                   1122:                                        BIO_printf(bio_err, "Name '%s' too long for min\n",
                   1123:                                            v->name);
                   1124:                                        return 0;
                   1125:                                }
                   1126:                                if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) {
                   1127:                                        ERR_clear_error();
                   1128:                                        n_min = -1;
                   1129:                                }
                   1130:                                ret = snprintf(buf, sizeof buf, "%s_max", type);
                   1131:                                if (ret == -1 || ret >= sizeof(buf)) {
                   1132:                                        BIO_printf(bio_err, "Name '%s' too long for max\n",
                   1133:                                            v->name);
                   1134:                                        return 0;
                   1135:                                }
                   1136:                                if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) {
                   1137:                                        ERR_clear_error();
                   1138:                                        n_max = -1;
                   1139:                                }
                   1140:                                if (!add_attribute_object(req,
                   1141:                                        v->value, def, value, nid, n_min, n_max, chtype))
                   1142:                                        return 0;
                   1143:                        }
                   1144:                }
                   1145:        } else {
                   1146:                BIO_printf(bio_err, "No template, please set one up.\n");
                   1147:                return 0;
                   1148:        }
                   1149:
                   1150:        return 1;
                   1151:
                   1152: }
                   1153:
                   1154: static int
                   1155: auto_info(X509_REQ * req, STACK_OF(CONF_VALUE) * dn_sk,
                   1156:     STACK_OF(CONF_VALUE) * attr_sk, int attribs, unsigned long chtype)
                   1157: {
                   1158:        int i;
                   1159:        char *p, *q;
                   1160:        char *type;
                   1161:        CONF_VALUE *v;
                   1162:        X509_NAME *subj;
                   1163:
                   1164:        subj = X509_REQ_get_subject_name(req);
                   1165:
                   1166:        for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
                   1167:                int mval;
                   1168:                v = sk_CONF_VALUE_value(dn_sk, i);
                   1169:                p = q = NULL;
                   1170:                type = v->name;
                   1171:                /*
                   1172:                 * Skip past any leading X. X: X, etc to allow for multiple
                   1173:                 * instances
                   1174:                 */
                   1175:                for (p = v->name; *p; p++)
                   1176:                        if ((*p == ':') || (*p == ',') || (*p == '.')) {
                   1177:                                p++;
                   1178:                                if (*p)
                   1179:                                        type = p;
                   1180:                                break;
                   1181:                        }
                   1182:                if (*p == '+') {
                   1183:                        p++;
                   1184:                        mval = -1;
                   1185:                } else
                   1186:                        mval = 0;
                   1187:                if (!X509_NAME_add_entry_by_txt(subj, type, chtype,
                   1188:                        (unsigned char *) v->value, -1, -1, mval))
                   1189:                        return 0;
                   1190:
                   1191:        }
                   1192:
                   1193:        if (!X509_NAME_entry_count(subj)) {
                   1194:                BIO_printf(bio_err, "error, no objects specified in config file\n");
                   1195:                return 0;
                   1196:        }
                   1197:        if (attribs) {
                   1198:                for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
                   1199:                        v = sk_CONF_VALUE_value(attr_sk, i);
                   1200:                        if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
                   1201:                                (unsigned char *) v->value, -1))
                   1202:                                return 0;
                   1203:                }
                   1204:        }
                   1205:        return 1;
                   1206: }
                   1207:
                   1208:
                   1209: static int
                   1210: add_DN_object(X509_NAME * n, char *text, const char *def, char *value,
                   1211:     int nid, int n_min, int n_max, unsigned long chtype, int mval)
                   1212: {
                   1213:        int i, ret = 0;
                   1214:        char buf[1024];
                   1215: start:
                   1216:        if (!batch)
                   1217:                BIO_printf(bio_err, "%s [%s]:", text, def);
                   1218:        (void) BIO_flush(bio_err);
                   1219:        if (value != NULL) {
                   1220:                strlcpy(buf, value, sizeof buf);
                   1221:                strlcat(buf, "\n", sizeof buf);
                   1222:                BIO_printf(bio_err, "%s\n", value);
                   1223:        } else {
                   1224:                buf[0] = '\0';
                   1225:                if (!batch) {
                   1226:                        if (!fgets(buf, sizeof buf, stdin))
                   1227:                                return 0;
                   1228:                } else {
                   1229:                        buf[0] = '\n';
                   1230:                        buf[1] = '\0';
                   1231:                }
                   1232:        }
                   1233:
                   1234:        if (buf[0] == '\0')
                   1235:                return (0);
                   1236:        else if (buf[0] == '\n') {
                   1237:                if ((def == NULL) || (def[0] == '\0'))
                   1238:                        return (1);
                   1239:                strlcpy(buf, def, sizeof buf);
                   1240:                strlcat(buf, "\n", sizeof buf);
                   1241:        } else if ((buf[0] == '.') && (buf[1] == '\n'))
                   1242:                return (1);
                   1243:
                   1244:        i = strlen(buf);
                   1245:        if (buf[i - 1] != '\n') {
                   1246:                BIO_printf(bio_err, "weird input :-(\n");
                   1247:                return (0);
                   1248:        }
                   1249:        buf[--i] = '\0';
                   1250:        if (!req_check_len(i, n_min, n_max))
                   1251:                goto start;
                   1252:        if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
                   1253:                (unsigned char *) buf, -1, -1, mval))
                   1254:                goto err;
                   1255:        ret = 1;
                   1256: err:
                   1257:        return (ret);
                   1258: }
                   1259:
                   1260: static int
                   1261: add_attribute_object(X509_REQ * req, char *text, const char *def,
                   1262:     char *value, int nid, int n_min,
                   1263:     int n_max, unsigned long chtype)
                   1264: {
                   1265:        int i;
                   1266:        static char buf[1024];
                   1267:
                   1268: start:
                   1269:        if (!batch)
                   1270:                BIO_printf(bio_err, "%s [%s]:", text, def);
                   1271:        (void) BIO_flush(bio_err);
                   1272:        if (value != NULL) {
                   1273:                strlcpy(buf, value, sizeof buf);
                   1274:                strlcat(buf, "\n", sizeof buf);
                   1275:                BIO_printf(bio_err, "%s\n", value);
                   1276:        } else {
                   1277:                buf[0] = '\0';
                   1278:                if (!batch) {
                   1279:                        if (!fgets(buf, sizeof buf, stdin))
                   1280:                                return 0;
                   1281:                } else {
                   1282:                        buf[0] = '\n';
                   1283:                        buf[1] = '\0';
                   1284:                }
                   1285:        }
                   1286:
                   1287:        if (buf[0] == '\0')
                   1288:                return (0);
                   1289:        else if (buf[0] == '\n') {
                   1290:                if ((def == NULL) || (def[0] == '\0'))
                   1291:                        return (1);
                   1292:                strlcpy(buf, def, sizeof buf);
                   1293:                strlcat(buf, "\n", sizeof buf);
                   1294:        } else if ((buf[0] == '.') && (buf[1] == '\n'))
                   1295:                return (1);
                   1296:
                   1297:        i = strlen(buf);
                   1298:        if (buf[i - 1] != '\n') {
                   1299:                BIO_printf(bio_err, "weird input :-(\n");
                   1300:                return (0);
                   1301:        }
                   1302:        buf[--i] = '\0';
                   1303:        if (!req_check_len(i, n_min, n_max))
                   1304:                goto start;
                   1305:
                   1306:        if (!X509_REQ_add1_attr_by_NID(req, nid, chtype,
                   1307:                (unsigned char *) buf, -1)) {
                   1308:                BIO_printf(bio_err, "Error adding attribute\n");
                   1309:                ERR_print_errors(bio_err);
                   1310:                goto err;
                   1311:        }
                   1312:        return (1);
                   1313: err:
                   1314:        return (0);
                   1315: }
                   1316:
                   1317: static int
                   1318: req_check_len(int len, int n_min, int n_max)
                   1319: {
                   1320:        if ((n_min > 0) && (len < n_min)) {
                   1321:                BIO_printf(bio_err, "string is too short, it needs to be at least %d bytes long\n", n_min);
                   1322:                return (0);
                   1323:        }
                   1324:        if ((n_max >= 0) && (len > n_max)) {
                   1325:                BIO_printf(bio_err, "string is too long, it needs to be less than  %d bytes long\n", n_max);
                   1326:                return (0);
                   1327:        }
                   1328:        return (1);
                   1329: }
                   1330:
                   1331: /* Check if the end of a string matches 'end' */
                   1332: static int
                   1333: check_end(const char *str, const char *end)
                   1334: {
                   1335:        int elen, slen;
                   1336:        const char *tmp;
                   1337:        elen = strlen(end);
                   1338:        slen = strlen(str);
                   1339:        if (elen > slen)
                   1340:                return 1;
                   1341:        tmp = str + slen - elen;
                   1342:        return strcmp(tmp, end);
                   1343: }
                   1344:
                   1345: static EVP_PKEY_CTX *
                   1346: set_keygen_ctx(BIO * err, const char *gstr, int *pkey_type,
1.7       bcook    1347:     long *pkeylen, char **palgnam)
1.1       jsing    1348: {
                   1349:        EVP_PKEY_CTX *gctx = NULL;
                   1350:        EVP_PKEY *param = NULL;
                   1351:        long keylen = -1;
                   1352:        BIO *pbio = NULL;
                   1353:        const char *paramfile = NULL;
                   1354:        const char *errstr;
                   1355:
                   1356:        if (gstr == NULL) {
                   1357:                *pkey_type = EVP_PKEY_RSA;
                   1358:                keylen = *pkeylen;
                   1359:        } else if (gstr[0] >= '0' && gstr[0] <= '9') {
                   1360:                *pkey_type = EVP_PKEY_RSA;
                   1361:                keylen = strtonum(gstr, 0, LONG_MAX, &errstr);
                   1362:                if (errstr) {
                   1363:                        BIO_printf(err, "bad algorithm %s: %s\n", gstr, errstr);
                   1364:                        return NULL;
                   1365:                }
                   1366:                *pkeylen = keylen;
                   1367:        } else if (!strncmp(gstr, "param:", 6))
                   1368:                paramfile = gstr + 6;
                   1369:        else {
                   1370:                const char *p = strchr(gstr, ':');
                   1371:                int len;
                   1372:                const EVP_PKEY_ASN1_METHOD *ameth;
                   1373:
                   1374:                if (p)
                   1375:                        len = p - gstr;
                   1376:                else
                   1377:                        len = strlen(gstr);
                   1378:
1.7       bcook    1379:                ameth = EVP_PKEY_asn1_find_str(NULL, gstr, len);
1.1       jsing    1380:
                   1381:                if (!ameth) {
                   1382:                        BIO_printf(err, "Unknown algorithm %.*s\n", len, gstr);
                   1383:                        return NULL;
                   1384:                }
                   1385:                EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL,
                   1386:                    ameth);
                   1387:                if (*pkey_type == EVP_PKEY_RSA) {
                   1388:                        if (p) {
                   1389:                                keylen = strtonum(p + 1, 0, LONG_MAX, &errstr);
                   1390:                                if (errstr) {
                   1391:                                        BIO_printf(err, "bad algorithm %s: %s\n",
                   1392:                                            p + 1, errstr);
                   1393:                                        return NULL;
                   1394:                                }
                   1395:                                *pkeylen = keylen;
                   1396:                        } else
                   1397:                                keylen = *pkeylen;
                   1398:                } else if (p)
                   1399:                        paramfile = p + 1;
                   1400:        }
                   1401:
                   1402:        if (paramfile) {
                   1403:                pbio = BIO_new_file(paramfile, "r");
                   1404:                if (!pbio) {
                   1405:                        BIO_printf(err, "Can't open parameter file %s\n",
                   1406:                            paramfile);
                   1407:                        return NULL;
                   1408:                }
                   1409:                param = PEM_read_bio_Parameters(pbio, NULL);
                   1410:
                   1411:                if (!param) {
                   1412:                        X509 *x;
                   1413:                        (void) BIO_reset(pbio);
                   1414:                        x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
                   1415:                        if (x) {
                   1416:                                param = X509_get_pubkey(x);
                   1417:                                X509_free(x);
                   1418:                        }
                   1419:                }
                   1420:                BIO_free(pbio);
                   1421:
                   1422:                if (!param) {
                   1423:                        BIO_printf(err, "Error reading parameter file %s\n",
                   1424:                            paramfile);
                   1425:                        return NULL;
                   1426:                }
                   1427:                if (*pkey_type == -1)
                   1428:                        *pkey_type = EVP_PKEY_id(param);
                   1429:                else if (*pkey_type != EVP_PKEY_base_id(param)) {
                   1430:                        BIO_printf(err, "Key Type does not match parameters\n");
                   1431:                        EVP_PKEY_free(param);
                   1432:                        return NULL;
                   1433:                }
                   1434:        }
                   1435:        if (palgnam) {
                   1436:                const EVP_PKEY_ASN1_METHOD *ameth;
                   1437:                const char *anam;
1.7       bcook    1438:                ameth = EVP_PKEY_asn1_find(NULL, *pkey_type);
1.1       jsing    1439:                if (!ameth) {
                   1440:                        BIO_puts(err, "Internal error: can't find key algorithm\n");
                   1441:                        return NULL;
                   1442:                }
                   1443:                EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth);
1.4       jsing    1444:                *palgnam = strdup(anam);
1.1       jsing    1445:        }
                   1446:        if (param) {
1.7       bcook    1447:                gctx = EVP_PKEY_CTX_new(param, NULL);
1.1       jsing    1448:                *pkeylen = EVP_PKEY_bits(param);
                   1449:                EVP_PKEY_free(param);
                   1450:        } else
1.7       bcook    1451:                gctx = EVP_PKEY_CTX_new_id(*pkey_type, NULL);
1.1       jsing    1452:
                   1453:        if (!gctx) {
                   1454:                BIO_puts(err, "Error allocating keygen context\n");
                   1455:                ERR_print_errors(err);
                   1456:                return NULL;
                   1457:        }
                   1458:        if (EVP_PKEY_keygen_init(gctx) <= 0) {
                   1459:                BIO_puts(err, "Error initializing keygen context\n");
                   1460:                ERR_print_errors(err);
                   1461:                return NULL;
                   1462:        }
                   1463:        if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1)) {
                   1464:                if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0) {
                   1465:                        BIO_puts(err, "Error setting RSA keysize\n");
                   1466:                        ERR_print_errors(err);
                   1467:                        EVP_PKEY_CTX_free(gctx);
                   1468:                        return NULL;
                   1469:                }
                   1470:        }
                   1471:
                   1472:        return gctx;
                   1473: }
                   1474:
                   1475: static int
                   1476: genpkey_cb(EVP_PKEY_CTX * ctx)
                   1477: {
                   1478:        char c = '*';
                   1479:        BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
                   1480:        int p;
                   1481:        p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
                   1482:        if (p == 0)
                   1483:                c = '.';
                   1484:        if (p == 1)
                   1485:                c = '+';
                   1486:        if (p == 2)
                   1487:                c = '*';
                   1488:        if (p == 3)
                   1489:                c = '\n';
                   1490:        BIO_write(b, &c, 1);
                   1491:        (void) BIO_flush(b);
                   1492:        return 1;
                   1493: }
                   1494:
                   1495: static int
                   1496: do_sign_init(BIO * err, EVP_MD_CTX * ctx, EVP_PKEY * pkey,
                   1497:     const EVP_MD * md, STACK_OF(OPENSSL_STRING) * sigopts)
                   1498: {
                   1499:        EVP_PKEY_CTX *pkctx = NULL;
                   1500:        int i;
                   1501:        EVP_MD_CTX_init(ctx);
                   1502:        if (!EVP_DigestSignInit(ctx, &pkctx, md, NULL, pkey))
                   1503:                return 0;
                   1504:        for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
                   1505:                char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
                   1506:                if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
                   1507:                        BIO_printf(err, "parameter error \"%s\"\n", sigopt);
                   1508:                        ERR_print_errors(bio_err);
                   1509:                        return 0;
                   1510:                }
                   1511:        }
                   1512:        return 1;
                   1513: }
                   1514:
                   1515: int
                   1516: do_X509_sign(BIO * err, X509 * x, EVP_PKEY * pkey, const EVP_MD * md,
                   1517:     STACK_OF(OPENSSL_STRING) * sigopts)
                   1518: {
                   1519:        int rv;
                   1520:        EVP_MD_CTX mctx;
                   1521:        EVP_MD_CTX_init(&mctx);
                   1522:        rv = do_sign_init(err, &mctx, pkey, md, sigopts);
                   1523:        if (rv > 0)
                   1524:                rv = X509_sign_ctx(x, &mctx);
                   1525:        EVP_MD_CTX_cleanup(&mctx);
                   1526:        return rv > 0 ? 1 : 0;
                   1527: }
                   1528:
                   1529:
                   1530: int
                   1531: do_X509_REQ_sign(BIO * err, X509_REQ * x, EVP_PKEY * pkey, const EVP_MD * md,
                   1532:     STACK_OF(OPENSSL_STRING) * sigopts)
                   1533: {
                   1534:        int rv;
                   1535:        EVP_MD_CTX mctx;
                   1536:        EVP_MD_CTX_init(&mctx);
                   1537:        rv = do_sign_init(err, &mctx, pkey, md, sigopts);
                   1538:        if (rv > 0)
                   1539:                rv = X509_REQ_sign_ctx(x, &mctx);
                   1540:        EVP_MD_CTX_cleanup(&mctx);
                   1541:        return rv > 0 ? 1 : 0;
                   1542: }
                   1543:
                   1544:
                   1545:
                   1546: int
                   1547: do_X509_CRL_sign(BIO * err, X509_CRL * x, EVP_PKEY * pkey, const EVP_MD * md,
                   1548:     STACK_OF(OPENSSL_STRING) * sigopts)
                   1549: {
                   1550:        int rv;
                   1551:        EVP_MD_CTX mctx;
                   1552:        EVP_MD_CTX_init(&mctx);
                   1553:        rv = do_sign_init(err, &mctx, pkey, md, sigopts);
                   1554:        if (rv > 0)
                   1555:                rv = X509_CRL_sign_ctx(x, &mctx);
                   1556:        EVP_MD_CTX_cleanup(&mctx);
                   1557:        return rv > 0 ? 1 : 0;
                   1558: }