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

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

1.11    ! inoguchi    1: /* $OpenBSD: pkey.c,v 1.10 2018/02/07 05:47:55 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:
1.11    ! inoguchi   68: static struct {
        !            69:        const EVP_CIPHER *cipher;
        !            70:        char *infile;
        !            71:        int informat;
        !            72:        int noout;
        !            73:        char *outfile;
        !            74:        int outformat;
        !            75:        char *passargin;
        !            76:        char *passargout;
        !            77:        int pubin;
        !            78:        int pubout;
        !            79:        int pubtext;
        !            80:        int text;
        !            81: } pkey_config;
        !            82:
        !            83: static int
        !            84: pkey_opt_cipher(int argc, char **argv, int *argsused)
        !            85: {
        !            86:        char *name = argv[0];
        !            87:
        !            88:        if (*name++ != '-')
        !            89:                return (1);
        !            90:
        !            91:        if ((pkey_config.cipher = EVP_get_cipherbyname(name)) == NULL) {
        !            92:                BIO_printf(bio_err, "Unknown cipher %s\n", name);
        !            93:                return (1);
        !            94:        }
        !            95:
        !            96:        *argsused = 1;
        !            97:        return (0);
        !            98: }
        !            99:
        !           100: static struct option pkey_options[] = {
        !           101:        {
        !           102:                .name = "in",
        !           103:                .argname = "file",
        !           104:                .desc = "Input file (default stdin)",
        !           105:                .type = OPTION_ARG,
        !           106:                .opt.arg = &pkey_config.infile,
        !           107:        },
        !           108:        {
        !           109:                .name = "inform",
        !           110:                .argname = "format",
        !           111:                .desc = "Input format (DER or PEM (default))",
        !           112:                .type = OPTION_ARG_FORMAT,
        !           113:                .opt.value = &pkey_config.informat,
        !           114:        },
        !           115:        {
        !           116:                .name = "noout",
        !           117:                .desc = "Do not print encoded version of the key",
        !           118:                .type = OPTION_FLAG,
        !           119:                .opt.flag = &pkey_config.noout,
        !           120:        },
        !           121:        {
        !           122:                .name = "out",
        !           123:                .argname = "file",
        !           124:                .desc = "Output file (default stdout)",
        !           125:                .type = OPTION_ARG,
        !           126:                .opt.arg = &pkey_config.outfile,
        !           127:        },
        !           128:        {
        !           129:                .name = "outform",
        !           130:                .argname = "format",
        !           131:                .desc = "Output format (DER or PEM (default))",
        !           132:                .type = OPTION_ARG_FORMAT,
        !           133:                .opt.value = &pkey_config.outformat,
        !           134:        },
        !           135:        {
        !           136:                .name = "passin",
        !           137:                .argname = "src",
        !           138:                .desc = "Input file passphrase source",
        !           139:                .type = OPTION_ARG,
        !           140:                .opt.arg = &pkey_config.passargin,
        !           141:        },
        !           142:        {
        !           143:                .name = "passout",
        !           144:                .argname = "src",
        !           145:                .desc = "Output file passphrase source",
        !           146:                .type = OPTION_ARG,
        !           147:                .opt.arg = &pkey_config.passargout,
        !           148:        },
        !           149:        {
        !           150:                .name = "pubin",
        !           151:                .desc = "Expect a public key (default private key)",
        !           152:                .type = OPTION_VALUE,
        !           153:                .value = 1,
        !           154:                .opt.value = &pkey_config.pubin,
        !           155:        },
        !           156:        {
        !           157:                .name = "pubout",
        !           158:                .desc = "Output a public key (default private key)",
        !           159:                .type = OPTION_VALUE,
        !           160:                .value = 1,
        !           161:                .opt.value = &pkey_config.pubout,
        !           162:        },
        !           163:        {
        !           164:                .name = "text",
        !           165:                .desc = "Print the public/private key in plain text",
        !           166:                .type = OPTION_FLAG,
        !           167:                .opt.flag = &pkey_config.text,
        !           168:        },
        !           169:        {
        !           170:                .name = "text_pub",
        !           171:                .desc = "Print out only public key in plain text",
        !           172:                .type = OPTION_FLAG,
        !           173:                .opt.flag = &pkey_config.pubtext,
        !           174:        },
        !           175:        {
        !           176:                .name = NULL,
        !           177:                .type = OPTION_ARGV_FUNC,
        !           178:                .opt.argvfunc = pkey_opt_cipher,
        !           179:        },
        !           180:        { NULL }
        !           181: };
        !           182:
        !           183: static void
        !           184: show_ciphers(const OBJ_NAME *name, void *arg)
        !           185: {
        !           186:        static int n;
        !           187:
        !           188:        fprintf(stderr, " -%-24s%s", name->name, (++n % 3 ? "" : "\n"));
        !           189: }
        !           190:
        !           191: static void
        !           192: pkey_usage()
        !           193: {
        !           194:        fprintf(stderr,
        !           195:            "usage: pkey [-ciphername] [-in file] [-inform fmt] [-noout] "
        !           196:            "[-out file]\n"
        !           197:            "    [-outform fmt] [-passin src] [-passout src] [-pubin] "
        !           198:            "[-pubout] [-text]\n"
        !           199:            "    [-text_pub]\n\n");
        !           200:        options_usage(pkey_options);
        !           201:        fprintf(stderr, "\n");
        !           202:
        !           203:        fprintf(stderr, "Valid ciphername values:\n\n");
        !           204:        OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, show_ciphers, NULL);
        !           205:        fprintf(stderr, "\n");
        !           206: }
        !           207:
1.1       jsing     208: int
                    209: pkey_main(int argc, char **argv)
                    210: {
                    211:        BIO *in = NULL, *out = NULL;
                    212:        EVP_PKEY *pkey = NULL;
                    213:        char *passin = NULL, *passout = NULL;
                    214:        int ret = 1;
1.6       doug      215:
                    216:        if (single_execution) {
1.9       deraadt   217:                if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
1.6       doug      218:                        perror("pledge");
1.8       doug      219:                        exit(1);
                    220:                }
1.6       doug      221:        }
1.1       jsing     222:
1.11    ! inoguchi  223:        memset(&pkey_config, 0, sizeof(pkey_config));
        !           224:        pkey_config.informat = FORMAT_PEM;
        !           225:        pkey_config.outformat = FORMAT_PEM;
1.1       jsing     226:
1.11    ! inoguchi  227:        if (options_parse(argc, argv, pkey_options, NULL, NULL) != 0) {
        !           228:                pkey_usage();
        !           229:                goto end;
1.1       jsing     230:        }
                    231:
1.11    ! inoguchi  232:        if (pkey_config.pubtext)
        !           233:                pkey_config.text = 1;
        !           234:        if (pkey_config.pubin)
        !           235:                pkey_config.pubout = pkey_config.pubtext = 1;
1.1       jsing     236:
1.11    ! inoguchi  237:        if (!app_passwd(bio_err, pkey_config.passargin, pkey_config.passargout,
        !           238:            &passin, &passout)) {
1.1       jsing     239:                BIO_printf(bio_err, "Error getting passwords\n");
                    240:                goto end;
                    241:        }
1.11    ! inoguchi  242:        if (pkey_config.outfile) {
        !           243:                if (!(out = BIO_new_file(pkey_config.outfile, "wb"))) {
1.1       jsing     244:                        BIO_printf(bio_err,
1.11    ! inoguchi  245:                            "Can't open output file %s\n", pkey_config.outfile);
1.1       jsing     246:                        goto end;
                    247:                }
                    248:        } else {
                    249:                out = BIO_new_fp(stdout, BIO_NOCLOSE);
                    250:        }
                    251:
1.11    ! inoguchi  252:        if (pkey_config.pubin)
        !           253:                pkey = load_pubkey(bio_err, pkey_config.infile,
        !           254:                    pkey_config.informat, 1, passin, "Public Key");
1.1       jsing     255:        else
1.11    ! inoguchi  256:                pkey = load_key(bio_err, pkey_config.infile,
        !           257:                    pkey_config.informat, 1, passin, "key");
1.1       jsing     258:        if (!pkey)
                    259:                goto end;
                    260:
1.11    ! inoguchi  261:        if (!pkey_config.noout) {
        !           262:                if (pkey_config.outformat == FORMAT_PEM) {
        !           263:                        if (pkey_config.pubout)
1.1       jsing     264:                                PEM_write_bio_PUBKEY(out, pkey);
                    265:                        else
1.11    ! inoguchi  266:                                PEM_write_bio_PrivateKey(out, pkey,
        !           267:                                    pkey_config.cipher, NULL, 0, NULL, passout);
        !           268:                } else if (pkey_config.outformat == FORMAT_ASN1) {
        !           269:                        if (pkey_config.pubout)
1.1       jsing     270:                                i2d_PUBKEY_bio(out, pkey);
                    271:                        else
                    272:                                i2d_PrivateKey_bio(out, pkey);
                    273:                } else {
                    274:                        BIO_printf(bio_err, "Bad format specified for key\n");
                    275:                        goto end;
                    276:                }
                    277:
                    278:        }
1.11    ! inoguchi  279:        if (pkey_config.text) {
        !           280:                if (pkey_config.pubtext)
1.1       jsing     281:                        EVP_PKEY_print_public(out, pkey, 0, NULL);
                    282:                else
                    283:                        EVP_PKEY_print_private(out, pkey, 0, NULL);
                    284:        }
                    285:        ret = 0;
                    286:
1.10      jsing     287:  end:
1.1       jsing     288:        EVP_PKEY_free(pkey);
                    289:        BIO_free_all(out);
                    290:        BIO_free(in);
                    291:        free(passin);
                    292:        free(passout);
                    293:
                    294:        return ret;
                    295: }