Copyright consolidation 01/10
[openssl.git] / apps / pkey.c
1 /*
2  * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include "apps.h"
13 #include <openssl/pem.h>
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16
17 typedef enum OPTION_choice {
18     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
19     OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
20     OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
21     OPT_TEXT, OPT_NOOUT, OPT_MD
22 } OPTION_CHOICE;
23
24 OPTIONS pkey_options[] = {
25     {"help", OPT_HELP, '-', "Display this summary"},
26     {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"},
27     {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
28     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
29     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
30     {"in", OPT_IN, 's', "Input key"},
31     {"out", OPT_OUT, '>', "Output file"},
32     {"pubin", OPT_PUBIN, '-',
33      "Read public key from input (default is private key)"},
34     {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
35     {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"},
36     {"text", OPT_TEXT, '-', "Output in plaintext as well"},
37     {"noout", OPT_NOOUT, '-', "Don't output the key"},
38     {"", OPT_MD, '-', "Any supported cipher"},
39 #ifndef OPENSSL_NO_ENGINE
40     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
41 #endif
42     {NULL}
43 };
44
45 int pkey_main(int argc, char **argv)
46 {
47     BIO *in = NULL, *out = NULL;
48     ENGINE *e = NULL;
49     EVP_PKEY *pkey = NULL;
50     const EVP_CIPHER *cipher = NULL;
51     char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
52     char *passinarg = NULL, *passoutarg = NULL, *prog;
53     OPTION_CHOICE o;
54     int informat = FORMAT_PEM, outformat = FORMAT_PEM;
55     int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
56     int private = 0;
57
58     prog = opt_init(argc, argv, pkey_options);
59     while ((o = opt_next()) != OPT_EOF) {
60         switch (o) {
61         case OPT_EOF:
62         case OPT_ERR:
63  opthelp:
64             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
65             goto end;
66         case OPT_HELP:
67             opt_help(pkey_options);
68             ret = 0;
69             goto end;
70         case OPT_INFORM:
71             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
72                 goto opthelp;
73             break;
74         case OPT_OUTFORM:
75             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
76                 goto opthelp;
77             break;
78         case OPT_PASSIN:
79             passinarg = opt_arg();
80             break;
81         case OPT_PASSOUT:
82             passoutarg = opt_arg();
83             break;
84         case OPT_ENGINE:
85             e = setup_engine(opt_arg(), 0);
86             break;
87         case OPT_IN:
88             infile = opt_arg();
89             break;
90         case OPT_OUT:
91             outfile = opt_arg();
92             break;
93         case OPT_PUBIN:
94             pubin = pubout = pubtext = 1;
95             break;
96         case OPT_PUBOUT:
97             pubout = 1;
98             break;
99         case OPT_TEXT_PUB:
100             pubtext = text = 1;
101             break;
102         case OPT_TEXT:
103             text = 1;
104             break;
105         case OPT_NOOUT:
106             noout = 1;
107             break;
108         case OPT_MD:
109             if (!opt_cipher(opt_unknown(), &cipher))
110                 goto opthelp;
111         }
112     }
113     argc = opt_num_rest();
114     if (argc != 0)
115         goto opthelp;
116
117     private = !noout && !pubout ? 1 : 0;
118     if (text && !pubtext)
119         private = 1;
120
121     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
122         BIO_printf(bio_err, "Error getting passwords\n");
123         goto end;
124     }
125
126     out = bio_open_owner(outfile, outformat, private);
127     if (out == NULL)
128         goto end;
129
130     if (pubin)
131         pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
132     else
133         pkey = load_key(infile, informat, 1, passin, e, "key");
134     if (!pkey)
135         goto end;
136
137     if (!noout) {
138         if (outformat == FORMAT_PEM) {
139             if (pubout)
140                 PEM_write_bio_PUBKEY(out, pkey);
141             else {
142                 assert(private);
143                 PEM_write_bio_PrivateKey(out, pkey, cipher,
144                                          NULL, 0, NULL, passout);
145             }
146         } else if (outformat == FORMAT_ASN1) {
147             if (pubout)
148                 i2d_PUBKEY_bio(out, pkey);
149             else {
150                 assert(private);
151                 i2d_PrivateKey_bio(out, pkey);
152             }
153         } else {
154             BIO_printf(bio_err, "Bad format specified for key\n");
155             goto end;
156         }
157
158     }
159
160     if (text) {
161         if (pubtext)
162             EVP_PKEY_print_public(out, pkey, 0, NULL);
163         else {
164             assert(private);
165             EVP_PKEY_print_private(out, pkey, 0, NULL);
166         }
167     }
168
169     ret = 0;
170
171  end:
172     EVP_PKEY_free(pkey);
173     BIO_free_all(out);
174     BIO_free(in);
175     OPENSSL_free(passin);
176     OPENSSL_free(passout);
177
178     return ret;
179 }