6160e5a381d0bca0b23d8c9542646550a5d57dbb
[openssl.git] / apps / pkey.c
1 /*
2  * Copyright 2006-2017 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, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK
22 } OPTION_CHOICE;
23
24 const 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     {"traditional", OPT_TRADITIONAL, '-',
40      "Use traditional format for private keys"},
41 #ifndef OPENSSL_NO_ENGINE
42     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
43 #endif
44     {"check", OPT_CHECK, '-', "Check key consistency"},
45     {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
46     {NULL}
47 };
48
49 int pkey_main(int argc, char **argv)
50 {
51     BIO *in = NULL, *out = NULL;
52     ENGINE *e = NULL;
53     EVP_PKEY *pkey = NULL;
54     const EVP_CIPHER *cipher = NULL;
55     char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
56     char *passinarg = NULL, *passoutarg = NULL, *prog;
57     OPTION_CHOICE o;
58     int informat = FORMAT_PEM, outformat = FORMAT_PEM;
59     int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
60     int private = 0, traditional = 0, check = 0, pub_check = 0;
61
62     prog = opt_init(argc, argv, pkey_options);
63     while ((o = opt_next()) != OPT_EOF) {
64         switch (o) {
65         case OPT_EOF:
66         case OPT_ERR:
67  opthelp:
68             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
69             goto end;
70         case OPT_HELP:
71             opt_help(pkey_options);
72             ret = 0;
73             goto end;
74         case OPT_INFORM:
75             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
76                 goto opthelp;
77             break;
78         case OPT_OUTFORM:
79             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
80                 goto opthelp;
81             break;
82         case OPT_PASSIN:
83             passinarg = opt_arg();
84             break;
85         case OPT_PASSOUT:
86             passoutarg = opt_arg();
87             break;
88         case OPT_ENGINE:
89             e = setup_engine(opt_arg(), 0);
90             break;
91         case OPT_IN:
92             infile = opt_arg();
93             break;
94         case OPT_OUT:
95             outfile = opt_arg();
96             break;
97         case OPT_PUBIN:
98             pubin = pubout = pubtext = 1;
99             break;
100         case OPT_PUBOUT:
101             pubout = 1;
102             break;
103         case OPT_TEXT_PUB:
104             pubtext = text = 1;
105             break;
106         case OPT_TEXT:
107             text = 1;
108             break;
109         case OPT_NOOUT:
110             noout = 1;
111             break;
112         case OPT_TRADITIONAL:
113             traditional = 1;
114             break;
115         case OPT_CHECK:
116             check = 1;
117             break;
118         case OPT_PUB_CHECK:
119             pub_check = 1;
120             break;
121         case OPT_MD:
122             if (!opt_cipher(opt_unknown(), &cipher))
123                 goto opthelp;
124         }
125     }
126     argc = opt_num_rest();
127     if (argc != 0)
128         goto opthelp;
129
130     private = !noout && !pubout ? 1 : 0;
131     if (text && !pubtext)
132         private = 1;
133
134     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
135         BIO_printf(bio_err, "Error getting passwords\n");
136         goto end;
137     }
138
139     out = bio_open_owner(outfile, outformat, private);
140     if (out == NULL)
141         goto end;
142
143     if (pubin)
144         pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
145     else
146         pkey = load_key(infile, informat, 1, passin, e, "key");
147     if (pkey == NULL)
148         goto end;
149
150     if (check || pub_check) {
151         int r;
152         EVP_PKEY_CTX *ctx;
153
154         ctx = EVP_PKEY_CTX_new(pkey, e);
155         if (ctx == NULL) {
156             ERR_print_errors(bio_err);
157             goto end;
158         }
159
160         if (check)
161             r = EVP_PKEY_check(ctx);
162         else
163             r = EVP_PKEY_public_check(ctx);
164
165         if (r == 1) {
166             BIO_printf(out, "Key is valid\n");
167         } else {
168             /*
169              * Note: at least for RSA keys if this function returns
170              * -1, there will be no error reasons.
171              */
172             unsigned long err;
173
174             BIO_printf(out, "Key is invalid\n");
175
176             while ((err = ERR_peek_error()) != 0) {
177                 BIO_printf(out, "Detailed error: %s\n",
178                            ERR_reason_error_string(err));
179                 ERR_get_error(); /* remove err from error stack */
180             }
181         }
182         EVP_PKEY_CTX_free(ctx);
183     }
184
185     if (!noout) {
186         if (outformat == FORMAT_PEM) {
187             if (pubout) {
188                 PEM_write_bio_PUBKEY(out, pkey);
189             } else {
190                 assert(private);
191                 if (traditional)
192                     PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
193                                                          NULL, 0, NULL,
194                                                          passout);
195                 else
196                     PEM_write_bio_PrivateKey(out, pkey, cipher,
197                                              NULL, 0, NULL, passout);
198             }
199         } else if (outformat == FORMAT_ASN1) {
200             if (pubout) {
201                 i2d_PUBKEY_bio(out, pkey);
202             } else {
203                 assert(private);
204                 i2d_PrivateKey_bio(out, pkey);
205             }
206         } else {
207             BIO_printf(bio_err, "Bad format specified for key\n");
208             goto end;
209         }
210     }
211
212     if (text) {
213         if (pubtext) {
214             EVP_PKEY_print_public(out, pkey, 0, NULL);
215         } else {
216             assert(private);
217             EVP_PKEY_print_private(out, pkey, 0, NULL);
218         }
219     }
220
221     ret = 0;
222
223  end:
224     if (ret != 0)
225         ERR_print_errors(bio_err);
226     EVP_PKEY_free(pkey);
227     release_engine(e);
228     BIO_free_all(out);
229     BIO_free(in);
230     OPENSSL_free(passin);
231     OPENSSL_free(passout);
232
233     return ret;
234 }