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

1.5     ! bcook       1: /* $OpenBSD: genpkey.c,v 1.4 2015/08/22 16:36:05 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: static int
1.5     ! bcook      69: init_keygen_file(BIO * err, EVP_PKEY_CTX ** pctx, const char *file);
1.1       jsing      70: static int genpkey_cb(EVP_PKEY_CTX * ctx);
                     71:
                     72: int
                     73: genpkey_main(int argc, char **argv)
                     74: {
                     75:        char **args, *outfile = NULL;
                     76:        char *passarg = NULL;
                     77:        BIO *in = NULL, *out = NULL;
                     78:        const EVP_CIPHER *cipher = NULL;
                     79:        int outformat;
                     80:        int text = 0;
                     81:        EVP_PKEY *pkey = NULL;
                     82:        EVP_PKEY_CTX *ctx = NULL;
                     83:        char *pass = NULL;
                     84:        int badarg = 0;
                     85:        int ret = 1, rv;
                     86:
                     87:        int do_param = 0;
                     88:
                     89:        outformat = FORMAT_PEM;
                     90:
                     91:        args = argv + 1;
                     92:        while (!badarg && *args && *args[0] == '-') {
                     93:                if (!strcmp(*args, "-outform")) {
                     94:                        if (args[1]) {
                     95:                                args++;
                     96:                                outformat = str2fmt(*args);
                     97:                        } else
                     98:                                badarg = 1;
                     99:                } else if (!strcmp(*args, "-pass")) {
                    100:                        if (!args[1])
                    101:                                goto bad;
                    102:                        passarg = *(++args);
                    103:                }
                    104:                else if (!strcmp(*args, "-paramfile")) {
                    105:                        if (!args[1])
                    106:                                goto bad;
                    107:                        args++;
                    108:                        if (do_param == 1)
                    109:                                goto bad;
1.5     ! bcook     110:                        if (!init_keygen_file(bio_err, &ctx, *args))
1.1       jsing     111:                                goto end;
                    112:                } else if (!strcmp(*args, "-out")) {
                    113:                        if (args[1]) {
                    114:                                args++;
                    115:                                outfile = *args;
                    116:                        } else
                    117:                                badarg = 1;
                    118:                } else if (strcmp(*args, "-algorithm") == 0) {
                    119:                        if (!args[1])
                    120:                                goto bad;
1.5     ! bcook     121:                        if (!init_gen_str(bio_err, &ctx, *(++args), do_param))
1.1       jsing     122:                                goto end;
                    123:                } else if (strcmp(*args, "-pkeyopt") == 0) {
                    124:                        if (!args[1])
                    125:                                goto bad;
                    126:                        if (!ctx) {
                    127:                                BIO_puts(bio_err, "No keytype specified\n");
                    128:                                goto bad;
                    129:                        } else if (pkey_ctrl_string(ctx, *(++args)) <= 0) {
                    130:                                BIO_puts(bio_err, "parameter setting error\n");
                    131:                                ERR_print_errors(bio_err);
                    132:                                goto end;
                    133:                        }
                    134:                } else if (strcmp(*args, "-genparam") == 0) {
                    135:                        if (ctx)
                    136:                                goto bad;
                    137:                        do_param = 1;
                    138:                } else if (strcmp(*args, "-text") == 0)
                    139:                        text = 1;
                    140:                else {
                    141:                        cipher = EVP_get_cipherbyname(*args + 1);
                    142:                        if (!cipher) {
                    143:                                BIO_printf(bio_err, "Unknown cipher %s\n",
                    144:                                    *args + 1);
                    145:                                badarg = 1;
                    146:                        }
                    147:                        if (do_param == 1)
                    148:                                badarg = 1;
                    149:                }
                    150:                args++;
                    151:        }
                    152:
                    153:        if (!ctx)
                    154:                badarg = 1;
                    155:
                    156:        if (badarg) {
                    157: bad:
                    158:                BIO_printf(bio_err, "Usage: genpkey [options]\n");
                    159:                BIO_printf(bio_err, "where options may be\n");
                    160:                BIO_printf(bio_err, "-out file          output file\n");
                    161:                BIO_printf(bio_err, "-outform X         output format (DER or PEM)\n");
                    162:                BIO_printf(bio_err, "-pass arg          output file pass phrase source\n");
                    163:                BIO_printf(bio_err, "-<cipher>          use cipher <cipher> to encrypt the key\n");
                    164:                BIO_printf(bio_err, "-paramfile file    parameters file\n");
                    165:                BIO_printf(bio_err, "-algorithm alg     the public key algorithm\n");
                    166:                BIO_printf(bio_err, "-pkeyopt opt:value set the public key algorithm option <opt>\n"
                    167:                    "                   to value <value>\n");
                    168:                BIO_printf(bio_err, "-genparam          generate parameters, not key\n");
                    169:                BIO_printf(bio_err, "-text              print the in text\n");
                    170:                BIO_printf(bio_err, "NB: options order may be important!  See the manual page.\n");
                    171:                goto end;
                    172:        }
                    173:        if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
                    174:                BIO_puts(bio_err, "Error getting password\n");
                    175:                goto end;
                    176:        }
                    177:        if (outfile) {
                    178:                if (!(out = BIO_new_file(outfile, "wb"))) {
                    179:                        BIO_printf(bio_err,
                    180:                            "Can't open output file %s\n", outfile);
                    181:                        goto end;
                    182:                }
                    183:        } else {
                    184:                out = BIO_new_fp(stdout, BIO_NOCLOSE);
                    185:        }
                    186:
                    187:        EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
                    188:        EVP_PKEY_CTX_set_app_data(ctx, bio_err);
                    189:
                    190:        if (do_param) {
                    191:                if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
                    192:                        BIO_puts(bio_err, "Error generating parameters\n");
                    193:                        ERR_print_errors(bio_err);
                    194:                        goto end;
                    195:                }
                    196:        } else {
                    197:                if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
                    198:                        BIO_puts(bio_err, "Error generating key\n");
                    199:                        ERR_print_errors(bio_err);
                    200:                        goto end;
                    201:                }
                    202:        }
                    203:
                    204:        if (do_param)
                    205:                rv = PEM_write_bio_Parameters(out, pkey);
                    206:        else if (outformat == FORMAT_PEM)
                    207:                rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0,
                    208:                    NULL, pass);
                    209:        else if (outformat == FORMAT_ASN1)
                    210:                rv = i2d_PrivateKey_bio(out, pkey);
                    211:        else {
                    212:                BIO_printf(bio_err, "Bad format specified for key\n");
                    213:                goto end;
                    214:        }
                    215:
                    216:        if (rv <= 0) {
                    217:                BIO_puts(bio_err, "Error writing key\n");
                    218:                ERR_print_errors(bio_err);
                    219:        }
                    220:        if (text) {
                    221:                if (do_param)
                    222:                        rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
                    223:                else
                    224:                        rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
                    225:
                    226:                if (rv <= 0) {
                    227:                        BIO_puts(bio_err, "Error printing key\n");
                    228:                        ERR_print_errors(bio_err);
                    229:                }
                    230:        }
                    231:        ret = 0;
                    232:
                    233: end:
                    234:        if (pkey)
                    235:                EVP_PKEY_free(pkey);
                    236:        if (ctx)
                    237:                EVP_PKEY_CTX_free(ctx);
                    238:        if (out)
                    239:                BIO_free_all(out);
                    240:        BIO_free(in);
                    241:        free(pass);
                    242:
                    243:        return ret;
                    244: }
                    245:
                    246: static int
                    247: init_keygen_file(BIO * err, EVP_PKEY_CTX ** pctx,
1.5     ! bcook     248:     const char *file)
1.1       jsing     249: {
                    250:        BIO *pbio;
                    251:        EVP_PKEY *pkey = NULL;
                    252:        EVP_PKEY_CTX *ctx = NULL;
                    253:        if (*pctx) {
                    254:                BIO_puts(err, "Parameters already set!\n");
                    255:                return 0;
                    256:        }
                    257:        pbio = BIO_new_file(file, "r");
                    258:        if (!pbio) {
                    259:                BIO_printf(err, "Can't open parameter file %s\n", file);
                    260:                return 0;
                    261:        }
                    262:        pkey = PEM_read_bio_Parameters(pbio, NULL);
                    263:        BIO_free(pbio);
                    264:
                    265:        if (!pkey) {
                    266:                BIO_printf(bio_err, "Error reading parameter file %s\n", file);
                    267:                return 0;
                    268:        }
1.5     ! bcook     269:        ctx = EVP_PKEY_CTX_new(pkey, NULL);
1.1       jsing     270:        if (!ctx)
                    271:                goto err;
                    272:        if (EVP_PKEY_keygen_init(ctx) <= 0)
                    273:                goto err;
                    274:        EVP_PKEY_free(pkey);
                    275:        *pctx = ctx;
                    276:        return 1;
                    277:
                    278: err:
                    279:        BIO_puts(err, "Error initializing context\n");
                    280:        ERR_print_errors(err);
                    281:        if (ctx)
                    282:                EVP_PKEY_CTX_free(ctx);
                    283:        if (pkey)
                    284:                EVP_PKEY_free(pkey);
                    285:        return 0;
                    286:
                    287: }
                    288:
                    289: int
                    290: init_gen_str(BIO * err, EVP_PKEY_CTX ** pctx,
1.5     ! bcook     291:     const char *algname, int do_param)
1.1       jsing     292: {
                    293:        EVP_PKEY_CTX *ctx = NULL;
                    294:        const EVP_PKEY_ASN1_METHOD *ameth;
                    295:        int pkey_id;
                    296:
                    297:        if (*pctx) {
                    298:                BIO_puts(err, "Algorithm already set!\n");
                    299:                return 0;
                    300:        }
1.5     ! bcook     301:        ameth = EVP_PKEY_asn1_find_str(NULL, algname, -1);
1.1       jsing     302:
                    303:        if (!ameth) {
                    304:                BIO_printf(bio_err, "Algorithm %s not found\n", algname);
                    305:                return 0;
                    306:        }
                    307:        ERR_clear_error();
                    308:
                    309:        EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
1.5     ! bcook     310:        ctx = EVP_PKEY_CTX_new_id(pkey_id, NULL);
1.1       jsing     311:
                    312:        if (!ctx)
                    313:                goto err;
                    314:        if (do_param) {
                    315:                if (EVP_PKEY_paramgen_init(ctx) <= 0)
                    316:                        goto err;
                    317:        } else {
                    318:                if (EVP_PKEY_keygen_init(ctx) <= 0)
                    319:                        goto err;
                    320:        }
                    321:
                    322:        *pctx = ctx;
                    323:        return 1;
                    324:
                    325: err:
                    326:        BIO_printf(err, "Error initializing %s context\n", algname);
                    327:        ERR_print_errors(err);
                    328:        if (ctx)
                    329:                EVP_PKEY_CTX_free(ctx);
                    330:        return 0;
                    331:
                    332: }
                    333:
                    334: static int
                    335: genpkey_cb(EVP_PKEY_CTX * ctx)
                    336: {
                    337:        char c = '*';
                    338:        BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
                    339:        int p;
                    340:        p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
                    341:        if (p == 0)
                    342:                c = '.';
                    343:        if (p == 1)
                    344:                c = '+';
                    345:        if (p == 2)
                    346:                c = '*';
                    347:        if (p == 3)
                    348:                c = '\n';
                    349:        BIO_write(b, &c, 1);
                    350:        (void) BIO_flush(b);
                    351:        return 1;
                    352: }