]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
6738bf14 | 2 | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. |
3e84b6e1 | 3 | * |
846e33c7 RS |
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 | |
3e84b6e1 | 8 | */ |
846e33c7 | 9 | |
3e84b6e1 DSH |
10 | #include <stdio.h> |
11 | #include <string.h> | |
12 | #include "apps.h" | |
dab2cd68 | 13 | #include "progs.h" |
3e84b6e1 DSH |
14 | #include <openssl/pem.h> |
15 | #include <openssl/err.h> | |
16 | #include <openssl/evp.h> | |
17 | ||
7e1b7485 RS |
18 | typedef enum OPTION_choice { |
19 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
20 | OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE, | |
21 | OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB, | |
b0004708 | 22 | OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK |
7e1b7485 RS |
23 | } OPTION_CHOICE; |
24 | ||
44c83ebd | 25 | const OPTIONS pkey_options[] = { |
7e1b7485 | 26 | {"help", OPT_HELP, '-', "Display this summary"}, |
d18ba3cc | 27 | {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"}, |
7e1b7485 RS |
28 | {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"}, |
29 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, | |
30 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, | |
dd958974 | 31 | {"in", OPT_IN, 's', "Input key"}, |
7e1b7485 RS |
32 | {"out", OPT_OUT, '>', "Output file"}, |
33 | {"pubin", OPT_PUBIN, '-', | |
34 | "Read public key from input (default is private key)"}, | |
35 | {"pubout", OPT_PUBOUT, '-', "Output public key, not private"}, | |
36 | {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"}, | |
37 | {"text", OPT_TEXT, '-', "Output in plaintext as well"}, | |
38 | {"noout", OPT_NOOUT, '-', "Don't output the key"}, | |
39 | {"", OPT_MD, '-', "Any supported cipher"}, | |
05dba815 DSH |
40 | {"traditional", OPT_TRADITIONAL, '-', |
41 | "Use traditional format for private keys"}, | |
7e1b7485 RS |
42 | #ifndef OPENSSL_NO_ENGINE |
43 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
44 | #endif | |
2aee35d3 | 45 | {"check", OPT_CHECK, '-', "Check key consistency"}, |
b0004708 | 46 | {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"}, |
7e1b7485 RS |
47 | {NULL} |
48 | }; | |
3e84b6e1 | 49 | |
7e1b7485 | 50 | int pkey_main(int argc, char **argv) |
0f113f3e | 51 | { |
0f113f3e | 52 | BIO *in = NULL, *out = NULL; |
7e1b7485 | 53 | ENGINE *e = NULL; |
0f113f3e | 54 | EVP_PKEY *pkey = NULL; |
7e1b7485 RS |
55 | const EVP_CIPHER *cipher = NULL; |
56 | char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL; | |
333b070e | 57 | char *passinarg = NULL, *passoutarg = NULL, *prog; |
7e1b7485 RS |
58 | OPTION_CHOICE o; |
59 | int informat = FORMAT_PEM, outformat = FORMAT_PEM; | |
60 | int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1; | |
b0004708 | 61 | int private = 0, traditional = 0, check = 0, pub_check = 0; |
7e1b7485 RS |
62 | |
63 | prog = opt_init(argc, argv, pkey_options); | |
64 | while ((o = opt_next()) != OPT_EOF) { | |
65 | switch (o) { | |
66 | case OPT_EOF: | |
67 | case OPT_ERR: | |
68 | opthelp: | |
69 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
70 | goto end; | |
71 | case OPT_HELP: | |
72 | opt_help(pkey_options); | |
73 | ret = 0; | |
74 | goto end; | |
75 | case OPT_INFORM: | |
dd958974 | 76 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) |
7e1b7485 RS |
77 | goto opthelp; |
78 | break; | |
79 | case OPT_OUTFORM: | |
80 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
81 | goto opthelp; | |
82 | break; | |
83 | case OPT_PASSIN: | |
84 | passinarg = opt_arg(); | |
85 | break; | |
86 | case OPT_PASSOUT: | |
87 | passoutarg = opt_arg(); | |
88 | break; | |
89 | case OPT_ENGINE: | |
333b070e | 90 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
91 | break; |
92 | case OPT_IN: | |
93 | infile = opt_arg(); | |
94 | break; | |
95 | case OPT_OUT: | |
96 | outfile = opt_arg(); | |
97 | break; | |
98 | case OPT_PUBIN: | |
99 | pubin = pubout = pubtext = 1; | |
100 | break; | |
101 | case OPT_PUBOUT: | |
0f113f3e | 102 | pubout = 1; |
7e1b7485 RS |
103 | break; |
104 | case OPT_TEXT_PUB: | |
105 | pubtext = text = 1; | |
106 | break; | |
107 | case OPT_TEXT: | |
0f113f3e | 108 | text = 1; |
7e1b7485 RS |
109 | break; |
110 | case OPT_NOOUT: | |
0f113f3e | 111 | noout = 1; |
7e1b7485 | 112 | break; |
05dba815 DSH |
113 | case OPT_TRADITIONAL: |
114 | traditional = 1; | |
115 | break; | |
2aee35d3 PY |
116 | case OPT_CHECK: |
117 | check = 1; | |
118 | break; | |
b0004708 PY |
119 | case OPT_PUB_CHECK: |
120 | pub_check = 1; | |
121 | break; | |
7e1b7485 RS |
122 | case OPT_MD: |
123 | if (!opt_cipher(opt_unknown(), &cipher)) | |
124 | goto opthelp; | |
0f113f3e | 125 | } |
0f113f3e | 126 | } |
7e1b7485 | 127 | argc = opt_num_rest(); |
03358517 KR |
128 | if (argc != 0) |
129 | goto opthelp; | |
130 | ||
3b061a00 RS |
131 | private = !noout && !pubout ? 1 : 0; |
132 | if (text && !pubtext) | |
133 | private = 1; | |
0f113f3e | 134 | |
7e1b7485 | 135 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
0f113f3e MC |
136 | BIO_printf(bio_err, "Error getting passwords\n"); |
137 | goto end; | |
138 | } | |
139 | ||
bdd58d98 | 140 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 RS |
141 | if (out == NULL) |
142 | goto end; | |
0f113f3e MC |
143 | |
144 | if (pubin) | |
7e1b7485 | 145 | pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key"); |
0f113f3e | 146 | else |
7e1b7485 | 147 | pkey = load_key(infile, informat, 1, passin, e, "key"); |
2234212c | 148 | if (pkey == NULL) |
0f113f3e MC |
149 | goto end; |
150 | ||
b0004708 | 151 | if (check || pub_check) { |
2aee35d3 PY |
152 | int r; |
153 | EVP_PKEY_CTX *ctx; | |
154 | ||
155 | ctx = EVP_PKEY_CTX_new(pkey, e); | |
156 | if (ctx == NULL) { | |
157 | ERR_print_errors(bio_err); | |
158 | goto end; | |
159 | } | |
160 | ||
b0004708 PY |
161 | if (check) |
162 | r = EVP_PKEY_check(ctx); | |
163 | else | |
164 | r = EVP_PKEY_public_check(ctx); | |
2aee35d3 PY |
165 | |
166 | if (r == 1) { | |
167 | BIO_printf(out, "Key is valid\n"); | |
168 | } else { | |
169 | /* | |
170 | * Note: at least for RSA keys if this function returns | |
171 | * -1, there will be no error reasons. | |
172 | */ | |
173 | unsigned long err; | |
174 | ||
175 | BIO_printf(out, "Key is invalid\n"); | |
176 | ||
177 | while ((err = ERR_peek_error()) != 0) { | |
178 | BIO_printf(out, "Detailed error: %s\n", | |
179 | ERR_reason_error_string(err)); | |
eff1752b | 180 | ERR_get_error(); /* remove err from error stack */ |
2aee35d3 PY |
181 | } |
182 | } | |
183 | EVP_PKEY_CTX_free(ctx); | |
184 | } | |
185 | ||
0f113f3e MC |
186 | if (!noout) { |
187 | if (outformat == FORMAT_PEM) { | |
2234212c | 188 | if (pubout) { |
c87af534 DB |
189 | if (!PEM_write_bio_PUBKEY(out, pkey)) |
190 | goto end; | |
2234212c | 191 | } else { |
7eff6aa0 | 192 | assert(private); |
c87af534 DB |
193 | if (traditional) { |
194 | if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher, | |
195 | NULL, 0, NULL, | |
196 | passout)) | |
197 | goto end; | |
198 | } else { | |
199 | if (!PEM_write_bio_PrivateKey(out, pkey, cipher, | |
200 | NULL, 0, NULL, passout)) | |
201 | goto end; | |
202 | } | |
7eff6aa0 | 203 | } |
0f113f3e | 204 | } else if (outformat == FORMAT_ASN1) { |
2234212c | 205 | if (pubout) { |
c87af534 DB |
206 | if (!i2d_PUBKEY_bio(out, pkey)) |
207 | goto end; | |
2234212c | 208 | } else { |
7eff6aa0 | 209 | assert(private); |
c87af534 DB |
210 | if (!i2d_PrivateKey_bio(out, pkey)) |
211 | goto end; | |
7eff6aa0 | 212 | } |
0f113f3e MC |
213 | } else { |
214 | BIO_printf(bio_err, "Bad format specified for key\n"); | |
215 | goto end; | |
216 | } | |
0f113f3e MC |
217 | } |
218 | ||
219 | if (text) { | |
2234212c | 220 | if (pubtext) { |
c87af534 DB |
221 | if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0) |
222 | goto end; | |
2234212c | 223 | } else { |
3b061a00 | 224 | assert(private); |
c87af534 DB |
225 | if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0) |
226 | goto end; | |
3b061a00 | 227 | } |
0f113f3e MC |
228 | } |
229 | ||
230 | ret = 0; | |
231 | ||
232 | end: | |
0396a447 DSH |
233 | if (ret != 0) |
234 | ERR_print_errors(bio_err); | |
0f113f3e | 235 | EVP_PKEY_free(pkey); |
dd1abd44 | 236 | release_engine(e); |
0f113f3e MC |
237 | BIO_free_all(out); |
238 | BIO_free(in); | |
b548a1f1 RS |
239 | OPENSSL_free(passin); |
240 | OPENSSL_free(passout); | |
0f113f3e MC |
241 | |
242 | return ret; | |
243 | } |