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

Annotation of src/usr.bin/openssl/genpkey.c, Revision 1.2

1.2     ! jsing       1: /* $OpenBSD: genpkey.c,v 1.1 2014/08/26 17:47:24 jsing Exp $ */
1.1       jsing       2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
                      3:  * project 2006
                      4:  */
                      5: /* ====================================================================
                      6:  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  *
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
                     14:  *
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in
                     17:  *    the documentation and/or other materials provided with the
                     18:  *    distribution.
                     19:  *
                     20:  * 3. All advertising materials mentioning features or use of this
                     21:  *    software must display the following acknowledgment:
                     22:  *    "This product includes software developed by the OpenSSL Project
                     23:  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
                     24:  *
                     25:  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
                     26:  *    endorse or promote products derived from this software without
                     27:  *    prior written permission. For written permission, please contact
                     28:  *    licensing@OpenSSL.org.
                     29:  *
                     30:  * 5. Products derived from this software may not be called "OpenSSL"
                     31:  *    nor may "OpenSSL" appear in their names without prior written
                     32:  *    permission of the OpenSSL Project.
                     33:  *
                     34:  * 6. Redistributions of any form whatsoever must retain the following
                     35:  *    acknowledgment:
                     36:  *    "This product includes software developed by the OpenSSL Project
                     37:  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
                     38:  *
                     39:  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
                     40:  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     41:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     42:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
                     43:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     44:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     45:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
                     46:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     47:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
                     48:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     49:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
                     50:  * OF THE POSSIBILITY OF SUCH DAMAGE.
                     51:  * ====================================================================
                     52:  *
                     53:  * This product includes cryptographic software written by Eric Young
                     54:  * (eay@cryptsoft.com).  This product includes software written by Tim
                     55:  * Hudson (tjh@cryptsoft.com).
                     56:  *
                     57:  */
                     58:
                     59: #include <stdio.h>
                     60: #include <string.h>
                     61:
                     62: #include "apps.h"
                     63:
                     64: #include <openssl/err.h>
                     65: #include <openssl/evp.h>
                     66: #include <openssl/pem.h>
                     67:
                     68: #ifndef OPENSSL_NO_ENGINE
                     69: #include <openssl/engine.h>
                     70: #endif
                     71:
                     72: static int
                     73: init_keygen_file(BIO * err, EVP_PKEY_CTX ** pctx, const char *file,
                     74:     ENGINE * e);
                     75: static int genpkey_cb(EVP_PKEY_CTX * ctx);
                     76:
                     77:
                     78: int genpkey_main(int, char **);
                     79:
                     80: int
                     81: genpkey_main(int argc, char **argv)
                     82: {
                     83:        ENGINE *e = NULL;
                     84:        char **args, *outfile = NULL;
                     85:        char *passarg = NULL;
                     86:        BIO *in = NULL, *out = NULL;
                     87:        const EVP_CIPHER *cipher = NULL;
                     88:        int outformat;
                     89:        int text = 0;
                     90:        EVP_PKEY *pkey = NULL;
                     91:        EVP_PKEY_CTX *ctx = NULL;
                     92:        char *pass = NULL;
                     93:        int badarg = 0;
                     94:        int ret = 1, rv;
                     95:
                     96:        int do_param = 0;
                     97:
                     98:        outformat = FORMAT_PEM;
                     99:
                    100:        OpenSSL_add_all_algorithms();
                    101:        args = argv + 1;
                    102:        while (!badarg && *args && *args[0] == '-') {
                    103:                if (!strcmp(*args, "-outform")) {
                    104:                        if (args[1]) {
                    105:                                args++;
                    106:                                outformat = str2fmt(*args);
                    107:                        } else
                    108:                                badarg = 1;
                    109:                } else if (!strcmp(*args, "-pass")) {
                    110:                        if (!args[1])
                    111:                                goto bad;
                    112:                        passarg = *(++args);
                    113:                }
                    114: #ifndef OPENSSL_NO_ENGINE
                    115:                else if (strcmp(*args, "-engine") == 0) {
                    116:                        if (!args[1])
                    117:                                goto bad;
                    118:                        e = setup_engine(bio_err, *(++args), 0);
                    119:                }
                    120: #endif
                    121:                else if (!strcmp(*args, "-paramfile")) {
                    122:                        if (!args[1])
                    123:                                goto bad;
                    124:                        args++;
                    125:                        if (do_param == 1)
                    126:                                goto bad;
                    127:                        if (!init_keygen_file(bio_err, &ctx, *args, e))
                    128:                                goto end;
                    129:                } else if (!strcmp(*args, "-out")) {
                    130:                        if (args[1]) {
                    131:                                args++;
                    132:                                outfile = *args;
                    133:                        } else
                    134:                                badarg = 1;
                    135:                } else if (strcmp(*args, "-algorithm") == 0) {
                    136:                        if (!args[1])
                    137:                                goto bad;
                    138:                        if (!init_gen_str(bio_err, &ctx, *(++args), e, do_param))
                    139:                                goto end;
                    140:                } else if (strcmp(*args, "-pkeyopt") == 0) {
                    141:                        if (!args[1])
                    142:                                goto bad;
                    143:                        if (!ctx) {
                    144:                                BIO_puts(bio_err, "No keytype specified\n");
                    145:                                goto bad;
                    146:                        } else if (pkey_ctrl_string(ctx, *(++args)) <= 0) {
                    147:                                BIO_puts(bio_err, "parameter setting error\n");
                    148:                                ERR_print_errors(bio_err);
                    149:                                goto end;
                    150:                        }
                    151:                } else if (strcmp(*args, "-genparam") == 0) {
                    152:                        if (ctx)
                    153:                                goto bad;
                    154:                        do_param = 1;
                    155:                } else if (strcmp(*args, "-text") == 0)
                    156:                        text = 1;
                    157:                else {
                    158:                        cipher = EVP_get_cipherbyname(*args + 1);
                    159:                        if (!cipher) {
                    160:                                BIO_printf(bio_err, "Unknown cipher %s\n",
                    161:                                    *args + 1);
                    162:                                badarg = 1;
                    163:                        }
                    164:                        if (do_param == 1)
                    165:                                badarg = 1;
                    166:                }
                    167:                args++;
                    168:        }
                    169:
                    170:        if (!ctx)
                    171:                badarg = 1;
                    172:
                    173:        if (badarg) {
                    174: bad:
                    175:                BIO_printf(bio_err, "Usage: genpkey [options]\n");
                    176:                BIO_printf(bio_err, "where options may be\n");
                    177:                BIO_printf(bio_err, "-out file          output file\n");
                    178:                BIO_printf(bio_err, "-outform X         output format (DER or PEM)\n");
                    179:                BIO_printf(bio_err, "-pass arg          output file pass phrase source\n");
                    180:                BIO_printf(bio_err, "-<cipher>          use cipher <cipher> to encrypt the key\n");
                    181: #ifndef OPENSSL_NO_ENGINE
                    182:                BIO_printf(bio_err, "-engine e          use engine e, possibly a hardware device.\n");
                    183: #endif
                    184:                BIO_printf(bio_err, "-paramfile file    parameters file\n");
                    185:                BIO_printf(bio_err, "-algorithm alg     the public key algorithm\n");
                    186:                BIO_printf(bio_err, "-pkeyopt opt:value set the public key algorithm option <opt>\n"
                    187:                    "                   to value <value>\n");
                    188:                BIO_printf(bio_err, "-genparam          generate parameters, not key\n");
                    189:                BIO_printf(bio_err, "-text              print the in text\n");
                    190:                BIO_printf(bio_err, "NB: options order may be important!  See the manual page.\n");
                    191:                goto end;
                    192:        }
                    193:        if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
                    194:                BIO_puts(bio_err, "Error getting password\n");
                    195:                goto end;
                    196:        }
                    197:        if (outfile) {
                    198:                if (!(out = BIO_new_file(outfile, "wb"))) {
                    199:                        BIO_printf(bio_err,
                    200:                            "Can't open output file %s\n", outfile);
                    201:                        goto end;
                    202:                }
                    203:        } else {
                    204:                out = BIO_new_fp(stdout, BIO_NOCLOSE);
                    205:        }
                    206:
                    207:        EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
                    208:        EVP_PKEY_CTX_set_app_data(ctx, bio_err);
                    209:
                    210:        if (do_param) {
                    211:                if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
                    212:                        BIO_puts(bio_err, "Error generating parameters\n");
                    213:                        ERR_print_errors(bio_err);
                    214:                        goto end;
                    215:                }
                    216:        } else {
                    217:                if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
                    218:                        BIO_puts(bio_err, "Error generating key\n");
                    219:                        ERR_print_errors(bio_err);
                    220:                        goto end;
                    221:                }
                    222:        }
                    223:
                    224:        if (do_param)
                    225:                rv = PEM_write_bio_Parameters(out, pkey);
                    226:        else if (outformat == FORMAT_PEM)
                    227:                rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0,
                    228:                    NULL, pass);
                    229:        else if (outformat == FORMAT_ASN1)
                    230:                rv = i2d_PrivateKey_bio(out, pkey);
                    231:        else {
                    232:                BIO_printf(bio_err, "Bad format specified for key\n");
                    233:                goto end;
                    234:        }
                    235:
                    236:        if (rv <= 0) {
                    237:                BIO_puts(bio_err, "Error writing key\n");
                    238:                ERR_print_errors(bio_err);
                    239:        }
                    240:        if (text) {
                    241:                if (do_param)
                    242:                        rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
                    243:                else
                    244:                        rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
                    245:
                    246:                if (rv <= 0) {
                    247:                        BIO_puts(bio_err, "Error printing key\n");
                    248:                        ERR_print_errors(bio_err);
                    249:                }
                    250:        }
                    251:        ret = 0;
                    252:
                    253: end:
                    254:        if (pkey)
                    255:                EVP_PKEY_free(pkey);
                    256:        if (ctx)
                    257:                EVP_PKEY_CTX_free(ctx);
                    258:        if (out)
                    259:                BIO_free_all(out);
                    260:        BIO_free(in);
                    261:        free(pass);
                    262:
                    263:        return ret;
                    264: }
                    265:
                    266: static int
                    267: init_keygen_file(BIO * err, EVP_PKEY_CTX ** pctx,
                    268:     const char *file, ENGINE * e)
                    269: {
                    270:        BIO *pbio;
                    271:        EVP_PKEY *pkey = NULL;
                    272:        EVP_PKEY_CTX *ctx = NULL;
                    273:        if (*pctx) {
                    274:                BIO_puts(err, "Parameters already set!\n");
                    275:                return 0;
                    276:        }
                    277:        pbio = BIO_new_file(file, "r");
                    278:        if (!pbio) {
                    279:                BIO_printf(err, "Can't open parameter file %s\n", file);
                    280:                return 0;
                    281:        }
                    282:        pkey = PEM_read_bio_Parameters(pbio, NULL);
                    283:        BIO_free(pbio);
                    284:
                    285:        if (!pkey) {
                    286:                BIO_printf(bio_err, "Error reading parameter file %s\n", file);
                    287:                return 0;
                    288:        }
                    289:        ctx = EVP_PKEY_CTX_new(pkey, e);
                    290:        if (!ctx)
                    291:                goto err;
                    292:        if (EVP_PKEY_keygen_init(ctx) <= 0)
                    293:                goto err;
                    294:        EVP_PKEY_free(pkey);
                    295:        *pctx = ctx;
                    296:        return 1;
                    297:
                    298: err:
                    299:        BIO_puts(err, "Error initializing context\n");
                    300:        ERR_print_errors(err);
                    301:        if (ctx)
                    302:                EVP_PKEY_CTX_free(ctx);
                    303:        if (pkey)
                    304:                EVP_PKEY_free(pkey);
                    305:        return 0;
                    306:
                    307: }
                    308:
                    309: int
                    310: init_gen_str(BIO * err, EVP_PKEY_CTX ** pctx,
                    311:     const char *algname, ENGINE * e, int do_param)
                    312: {
                    313:        EVP_PKEY_CTX *ctx = NULL;
                    314:        const EVP_PKEY_ASN1_METHOD *ameth;
                    315:        ENGINE *tmpeng = NULL;
                    316:        int pkey_id;
                    317:
                    318:        if (*pctx) {
                    319:                BIO_puts(err, "Algorithm already set!\n");
                    320:                return 0;
                    321:        }
                    322:        ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
                    323:
                    324: #ifndef OPENSSL_NO_ENGINE
                    325:        if (!ameth && e)
                    326:                ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
                    327: #endif
                    328:
                    329:        if (!ameth) {
                    330:                BIO_printf(bio_err, "Algorithm %s not found\n", algname);
                    331:                return 0;
                    332:        }
                    333:        ERR_clear_error();
                    334:
                    335:        EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
                    336: #ifndef OPENSSL_NO_ENGINE
                    337:        if (tmpeng)
                    338:                ENGINE_finish(tmpeng);
                    339: #endif
                    340:        ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
                    341:
                    342:        if (!ctx)
                    343:                goto err;
                    344:        if (do_param) {
                    345:                if (EVP_PKEY_paramgen_init(ctx) <= 0)
                    346:                        goto err;
                    347:        } else {
                    348:                if (EVP_PKEY_keygen_init(ctx) <= 0)
                    349:                        goto err;
                    350:        }
                    351:
                    352:        *pctx = ctx;
                    353:        return 1;
                    354:
                    355: err:
                    356:        BIO_printf(err, "Error initializing %s context\n", algname);
                    357:        ERR_print_errors(err);
                    358:        if (ctx)
                    359:                EVP_PKEY_CTX_free(ctx);
                    360:        return 0;
                    361:
                    362: }
                    363:
                    364: static int
                    365: genpkey_cb(EVP_PKEY_CTX * ctx)
                    366: {
                    367:        char c = '*';
                    368:        BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
                    369:        int p;
                    370:        p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
                    371:        if (p == 0)
                    372:                c = '.';
                    373:        if (p == 1)
                    374:                c = '+';
                    375:        if (p == 2)
                    376:                c = '*';
                    377:        if (p == 3)
                    378:                c = '\n';
                    379:        BIO_write(b, &c, 1);
                    380:        (void) BIO_flush(b);
                    381:        return 1;
                    382: }