pkey.c 5.6 KB
Newer Older
1
/*
R
Rich Salz 已提交
2
 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
8
 */
R
Rich Salz 已提交
9

10 11 12 13 14 15 16
#include <stdio.h>
#include <string.h>
#include "apps.h"
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/evp.h>

17 18 19 20
typedef enum OPTION_choice {
    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
    OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
    OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
21
    OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL
22 23
} OPTION_CHOICE;

F
FdaSilvaYY 已提交
24
const OPTIONS pkey_options[] = {
25
    {"help", OPT_HELP, '-', "Display this summary"},
26
    {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"},
27 28 29
    {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
    {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
    {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
30
    {"in", OPT_IN, 's', "Input key"},
31 32 33 34 35 36 37 38
    {"out", OPT_OUT, '>', "Output file"},
    {"pubin", OPT_PUBIN, '-',
     "Read public key from input (default is private key)"},
    {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
    {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"},
    {"text", OPT_TEXT, '-', "Output in plaintext as well"},
    {"noout", OPT_NOOUT, '-', "Don't output the key"},
    {"", OPT_MD, '-', "Any supported cipher"},
39 40
    {"traditional", OPT_TRADITIONAL, '-',
     "Use traditional format for private keys"},
41 42 43 44 45
#ifndef OPENSSL_NO_ENGINE
    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
#endif
    {NULL}
};
46

47
int pkey_main(int argc, char **argv)
48 49
{
    BIO *in = NULL, *out = NULL;
50
    ENGINE *e = NULL;
51
    EVP_PKEY *pkey = NULL;
52 53
    const EVP_CIPHER *cipher = NULL;
    char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
R
Rich Salz 已提交
54
    char *passinarg = NULL, *passoutarg = NULL, *prog;
55 56 57
    OPTION_CHOICE o;
    int informat = FORMAT_PEM, outformat = FORMAT_PEM;
    int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
58
    int private = 0, traditional = 0;
59 60 61 62 63 64 65 66 67 68 69 70 71 72

    prog = opt_init(argc, argv, pkey_options);
    while ((o = opt_next()) != OPT_EOF) {
        switch (o) {
        case OPT_EOF:
        case OPT_ERR:
 opthelp:
            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
            goto end;
        case OPT_HELP:
            opt_help(pkey_options);
            ret = 0;
            goto end;
        case OPT_INFORM:
73
            if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
74 75 76 77 78 79 80 81 82 83 84 85 86
                goto opthelp;
            break;
        case OPT_OUTFORM:
            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
                goto opthelp;
            break;
        case OPT_PASSIN:
            passinarg = opt_arg();
            break;
        case OPT_PASSOUT:
            passoutarg = opt_arg();
            break;
        case OPT_ENGINE:
R
Rich Salz 已提交
87
            e = setup_engine(opt_arg(), 0);
88 89 90 91 92 93 94 95 96 97 98
            break;
        case OPT_IN:
            infile = opt_arg();
            break;
        case OPT_OUT:
            outfile = opt_arg();
            break;
        case OPT_PUBIN:
            pubin = pubout = pubtext = 1;
            break;
        case OPT_PUBOUT:
99
            pubout = 1;
100 101 102 103 104
            break;
        case OPT_TEXT_PUB:
            pubtext = text = 1;
            break;
        case OPT_TEXT:
105
            text = 1;
106 107
            break;
        case OPT_NOOUT:
108
            noout = 1;
109
            break;
110 111 112
        case OPT_TRADITIONAL:
            traditional = 1;
            break;
113 114 115
        case OPT_MD:
            if (!opt_cipher(opt_unknown(), &cipher))
                goto opthelp;
116 117
        }
    }
118
    argc = opt_num_rest();
K
Kurt Roeckx 已提交
119 120 121
    if (argc != 0)
        goto opthelp;

122 123 124
    private = !noout && !pubout ? 1 : 0;
    if (text && !pubtext)
        private = 1;
125

126
    if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
127 128 129 130
        BIO_printf(bio_err, "Error getting passwords\n");
        goto end;
    }

131
    out = bio_open_owner(outfile, outformat, private);
132 133
    if (out == NULL)
        goto end;
134 135

    if (pubin)
136
        pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
137
    else
138
        pkey = load_key(infile, informat, 1, passin, e, "key");
139 140 141 142 143 144 145
    if (!pkey)
        goto end;

    if (!noout) {
        if (outformat == FORMAT_PEM) {
            if (pubout)
                PEM_write_bio_PUBKEY(out, pkey);
146 147
            else {
                assert(private);
148 149 150 151 152 153 154
                if (traditional)
                    PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
                                                         NULL, 0, NULL,
                                                         passout);
                else
                    PEM_write_bio_PrivateKey(out, pkey, cipher,
                                             NULL, 0, NULL, passout);
155
            }
156 157 158
        } else if (outformat == FORMAT_ASN1) {
            if (pubout)
                i2d_PUBKEY_bio(out, pkey);
159 160
            else {
                assert(private);
161
                i2d_PrivateKey_bio(out, pkey);
162
            }
163 164 165 166 167 168 169 170 171 172
        } else {
            BIO_printf(bio_err, "Bad format specified for key\n");
            goto end;
        }

    }

    if (text) {
        if (pubtext)
            EVP_PKEY_print_public(out, pkey, 0, NULL);
173 174
        else {
            assert(private);
175
            EVP_PKEY_print_private(out, pkey, 0, NULL);
176
        }
177 178 179 180 181 182 183 184
    }

    ret = 0;

 end:
    EVP_PKEY_free(pkey);
    BIO_free_all(out);
    BIO_free(in);
R
Rich Salz 已提交
185 186
    OPENSSL_free(passin);
    OPENSSL_free(passout);
187 188 189

    return ret;
}