]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
3 | * 1999-2004. | |
600dec15 DSH |
4 | */ |
5 | /* ==================================================================== | |
6 | * Copyright (c) 1999 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 | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
600dec15 DSH |
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 | #include <stdio.h> | |
2d7153e8 | 59 | #include <stdlib.h> |
8c197cc5 | 60 | #include <string.h> |
20432eae | 61 | #include "apps.h" |
600dec15 DSH |
62 | #include <openssl/pem.h> |
63 | #include <openssl/err.h> | |
64 | #include <openssl/evp.h> | |
65 | #include <openssl/pkcs12.h> | |
66 | ||
7e1b7485 RS |
67 | typedef enum OPTION_choice { |
68 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
69 | OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT, | |
54dbf423 | 70 | OPT_TOPK8, OPT_NOITER, OPT_NOCRYPT, |
b0809bc8 RS |
71 | #ifndef OPENSSL_NO_SCRYPT |
72 | OPT_SCRYPT, OPT_SCRYPT_N, OPT_SCRYPT_R, OPT_SCRYPT_P, | |
73 | #endif | |
74 | OPT_V2, OPT_V1, OPT_V2PRF, OPT_ITER, OPT_PASSIN, OPT_PASSOUT | |
7e1b7485 | 75 | } OPTION_CHOICE; |
600dec15 | 76 | |
7e1b7485 RS |
77 | OPTIONS pkcs8_options[] = { |
78 | {"help", OPT_HELP, '-', "Display this summary"}, | |
79 | {"inform", OPT_INFORM, 'F', "Input format (DER or PEM)"}, | |
80 | {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"}, | |
81 | {"in", OPT_IN, '<', "Input file"}, | |
82 | {"out", OPT_OUT, '>', "Output file"}, | |
83 | {"topk8", OPT_TOPK8, '-', "Output PKCS8 file"}, | |
84 | {"noiter", OPT_NOITER, '-', "Use 1 as iteration count"}, | |
85 | {"nocrypt", OPT_NOCRYPT, '-', "Use or expect unencrypted private key"}, | |
7e1b7485 RS |
86 | {"v2", OPT_V2, 's', "Use PKCS#5 v2.0 and cipher"}, |
87 | {"v1", OPT_V1, 's', "Use PKCS#5 v1.5 and cipher"}, | |
88 | {"v2prf", OPT_V2PRF, 's'}, | |
89 | {"iter", OPT_ITER, 'p', "Specify the iteration count"}, | |
90 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, | |
91 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, | |
92 | #ifndef OPENSSL_NO_ENGINE | |
93 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
94 | #endif | |
b0809bc8 | 95 | #ifndef OPENSSL_NO_SCRYPT |
0ceb8b74 DSH |
96 | {"scrypt", OPT_SCRYPT, '-', "Use scrypt algorithm"}, |
97 | {"scrypt_N", OPT_SCRYPT_N, 's', "Set scrypt N parameter"}, | |
98 | {"scrypt_r", OPT_SCRYPT_R, 's', "Set scrypt r parameter"}, | |
99 | {"scrypt_p", OPT_SCRYPT_P, 's', "Set scrypt p parameter"}, | |
b0809bc8 | 100 | #endif |
7e1b7485 RS |
101 | {NULL} |
102 | }; | |
600dec15 | 103 | |
7e1b7485 | 104 | int pkcs8_main(int argc, char **argv) |
0f113f3e | 105 | { |
0f113f3e | 106 | BIO *in = NULL, *out = NULL; |
7e1b7485 | 107 | ENGINE *e = NULL; |
0f113f3e | 108 | EVP_PKEY *pkey = NULL; |
7e1b7485 RS |
109 | PKCS8_PRIV_KEY_INFO *p8inf = NULL; |
110 | X509_SIG *p8 = NULL; | |
111 | const EVP_CIPHER *cipher = NULL; | |
333b070e | 112 | char *infile = NULL, *outfile = NULL; |
7e1b7485 | 113 | char *passinarg = NULL, *passoutarg = NULL, *prog; |
0f113f3e | 114 | char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL; |
7e1b7485 | 115 | OPTION_CHOICE o; |
54dbf423 | 116 | int nocrypt = 0, ret = 1, iter = PKCS12_DEFAULT_ITER; |
333b070e | 117 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, topk8 = 0, pbe_nid = -1; |
3b061a00 | 118 | int private = 0; |
b0809bc8 | 119 | #ifndef OPENSSL_NO_SCRYPT |
bd4850df | 120 | long scrypt_N = 0, scrypt_r = 0, scrypt_p = 0; |
b0809bc8 | 121 | #endif |
3647bee2 | 122 | |
7e1b7485 RS |
123 | prog = opt_init(argc, argv, pkcs8_options); |
124 | while ((o = opt_next()) != OPT_EOF) { | |
125 | switch (o) { | |
126 | case OPT_EOF: | |
127 | case OPT_ERR: | |
128 | opthelp: | |
129 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
130 | goto end; | |
131 | case OPT_HELP: | |
132 | opt_help(pkcs8_options); | |
133 | ret = 0; | |
134 | goto end; | |
135 | case OPT_INFORM: | |
136 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
137 | goto opthelp; | |
138 | break; | |
139 | case OPT_IN: | |
140 | infile = opt_arg(); | |
141 | break; | |
142 | case OPT_OUTFORM: | |
143 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
144 | goto opthelp; | |
145 | break; | |
146 | case OPT_OUT: | |
147 | outfile = opt_arg(); | |
148 | break; | |
149 | case OPT_TOPK8: | |
0f113f3e | 150 | topk8 = 1; |
7e1b7485 RS |
151 | break; |
152 | case OPT_NOITER: | |
0f113f3e | 153 | iter = 1; |
7e1b7485 RS |
154 | break; |
155 | case OPT_NOCRYPT: | |
0f113f3e | 156 | nocrypt = 1; |
7e1b7485 | 157 | break; |
7e1b7485 RS |
158 | case OPT_V2: |
159 | if (!opt_cipher(opt_arg(), &cipher)) | |
160 | goto opthelp; | |
161 | break; | |
162 | case OPT_V1: | |
163 | pbe_nid = OBJ_txt2nid(opt_arg()); | |
164 | if (pbe_nid == NID_undef) { | |
165 | BIO_printf(bio_err, | |
166 | "%s: Unknown PBE algorithm %s\n", prog, opt_arg()); | |
167 | goto opthelp; | |
168 | } | |
169 | break; | |
170 | case OPT_V2PRF: | |
171 | pbe_nid = OBJ_txt2nid(opt_arg()); | |
172 | if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) { | |
173 | BIO_printf(bio_err, | |
174 | "%s: Unknown PRF algorithm %s\n", prog, opt_arg()); | |
175 | goto opthelp; | |
176 | } | |
177 | break; | |
178 | case OPT_ITER: | |
179 | if (!opt_int(opt_arg(), &iter)) | |
180 | goto opthelp; | |
181 | break; | |
182 | case OPT_PASSIN: | |
183 | passinarg = opt_arg(); | |
184 | break; | |
185 | case OPT_PASSOUT: | |
186 | passoutarg = opt_arg(); | |
187 | break; | |
188 | case OPT_ENGINE: | |
333b070e | 189 | e = setup_engine(opt_arg(), 0); |
7e1b7485 | 190 | break; |
b0809bc8 | 191 | #ifndef OPENSSL_NO_SCRYPT |
0ceb8b74 | 192 | case OPT_SCRYPT: |
5fc3ee4b | 193 | scrypt_N = 16384; |
0ceb8b74 | 194 | scrypt_r = 8; |
5fc3ee4b | 195 | scrypt_p = 1; |
0ceb8b74 DSH |
196 | if (cipher == NULL) |
197 | cipher = EVP_aes_256_cbc(); | |
198 | break; | |
199 | case OPT_SCRYPT_N: | |
bd4850df | 200 | if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0) |
0ceb8b74 DSH |
201 | goto opthelp; |
202 | break; | |
203 | case OPT_SCRYPT_R: | |
bd4850df | 204 | if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0) |
0ceb8b74 DSH |
205 | goto opthelp; |
206 | break; | |
207 | case OPT_SCRYPT_P: | |
bd4850df | 208 | if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0) |
0ceb8b74 DSH |
209 | goto opthelp; |
210 | break; | |
b0809bc8 | 211 | #endif |
0f113f3e | 212 | } |
0f113f3e | 213 | } |
7e1b7485 | 214 | argc = opt_num_rest(); |
03358517 KR |
215 | if (argc != 0) |
216 | goto opthelp; | |
217 | ||
3b061a00 | 218 | private = 1; |
600dec15 | 219 | |
7e1b7485 | 220 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
0f113f3e MC |
221 | BIO_printf(bio_err, "Error getting passwords\n"); |
222 | goto end; | |
223 | } | |
a3fe382e | 224 | |
0f113f3e MC |
225 | if ((pbe_nid == -1) && !cipher) |
226 | pbe_nid = NID_pbeWithMD5AndDES_CBC; | |
600dec15 | 227 | |
bdd58d98 | 228 | in = bio_open_default(infile, 'r', informat); |
7e1b7485 RS |
229 | if (in == NULL) |
230 | goto end; | |
bdd58d98 | 231 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 RS |
232 | if (out == NULL) |
233 | goto end; | |
3b061a00 | 234 | |
0f113f3e | 235 | if (topk8) { |
7e1b7485 | 236 | pkey = load_key(infile, informat, 1, passin, e, "key"); |
0f113f3e MC |
237 | if (!pkey) |
238 | goto end; | |
54dbf423 | 239 | if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) { |
0f113f3e MC |
240 | BIO_printf(bio_err, "Error converting key\n"); |
241 | ERR_print_errors(bio_err); | |
242 | goto end; | |
243 | } | |
244 | if (nocrypt) { | |
3b061a00 | 245 | assert(private); |
0f113f3e MC |
246 | if (outformat == FORMAT_PEM) |
247 | PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf); | |
248 | else if (outformat == FORMAT_ASN1) | |
249 | i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf); | |
250 | else { | |
251 | BIO_printf(bio_err, "Bad format specified for key\n"); | |
252 | goto end; | |
253 | } | |
254 | } else { | |
6355d315 | 255 | X509_ALGOR *pbe; |
0ceb8b74 | 256 | if (cipher) { |
b0809bc8 | 257 | #ifndef OPENSSL_NO_SCRYPT |
0ceb8b74 DSH |
258 | if (scrypt_N && scrypt_r && scrypt_p) |
259 | pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, 0, NULL, | |
260 | scrypt_N, scrypt_r, scrypt_p); | |
261 | else | |
b0809bc8 | 262 | #endif |
0ceb8b74 DSH |
263 | pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL, |
264 | pbe_nid); | |
265 | } else { | |
6355d315 | 266 | pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0); |
0ceb8b74 | 267 | } |
6355d315 DSH |
268 | if (pbe == NULL) { |
269 | BIO_printf(bio_err, "Error setting PBE algorithm\n"); | |
270 | ERR_print_errors(bio_err); | |
271 | goto end; | |
272 | } | |
0f113f3e MC |
273 | if (passout) |
274 | p8pass = passout; | |
275 | else { | |
276 | p8pass = pass; | |
277 | if (EVP_read_pw_string | |
6355d315 DSH |
278 | (pass, sizeof pass, "Enter Encryption Password:", 1)) { |
279 | X509_ALGOR_free(pbe); | |
0f113f3e | 280 | goto end; |
6355d315 | 281 | } |
0f113f3e | 282 | } |
7e1b7485 | 283 | app_RAND_load_file(NULL, 0); |
6355d315 DSH |
284 | p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe); |
285 | if (p8 == NULL) { | |
286 | X509_ALGOR_free(pbe); | |
0f113f3e MC |
287 | BIO_printf(bio_err, "Error encrypting key\n"); |
288 | ERR_print_errors(bio_err); | |
289 | goto end; | |
290 | } | |
7e1b7485 | 291 | app_RAND_write_file(NULL); |
3b061a00 | 292 | assert(private); |
0f113f3e MC |
293 | if (outformat == FORMAT_PEM) |
294 | PEM_write_bio_PKCS8(out, p8); | |
295 | else if (outformat == FORMAT_ASN1) | |
296 | i2d_PKCS8_bio(out, p8); | |
297 | else { | |
298 | BIO_printf(bio_err, "Bad format specified for key\n"); | |
299 | goto end; | |
300 | } | |
301 | } | |
302 | ||
303 | ret = 0; | |
304 | goto end; | |
305 | } | |
306 | ||
307 | if (nocrypt) { | |
308 | if (informat == FORMAT_PEM) | |
309 | p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL); | |
310 | else if (informat == FORMAT_ASN1) | |
311 | p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL); | |
312 | else { | |
313 | BIO_printf(bio_err, "Bad format specified for key\n"); | |
314 | goto end; | |
315 | } | |
316 | } else { | |
317 | if (informat == FORMAT_PEM) | |
318 | p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL); | |
319 | else if (informat == FORMAT_ASN1) | |
320 | p8 = d2i_PKCS8_bio(in, NULL); | |
321 | else { | |
322 | BIO_printf(bio_err, "Bad format specified for key\n"); | |
323 | goto end; | |
324 | } | |
8a605478 | 325 | |
0f113f3e MC |
326 | if (!p8) { |
327 | BIO_printf(bio_err, "Error reading key\n"); | |
328 | ERR_print_errors(bio_err); | |
329 | goto end; | |
330 | } | |
331 | if (passin) | |
332 | p8pass = passin; | |
333 | else { | |
334 | p8pass = pass; | |
335 | EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0); | |
336 | } | |
337 | p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass)); | |
338 | } | |
600dec15 | 339 | |
0f113f3e MC |
340 | if (!p8inf) { |
341 | BIO_printf(bio_err, "Error decrypting key\n"); | |
342 | ERR_print_errors(bio_err); | |
343 | goto end; | |
344 | } | |
3cbb7937 | 345 | |
75ebbd9a | 346 | if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) { |
0f113f3e MC |
347 | BIO_printf(bio_err, "Error converting key\n"); |
348 | ERR_print_errors(bio_err); | |
349 | goto end; | |
350 | } | |
e7871ffa | 351 | |
3b061a00 | 352 | assert(private); |
0f113f3e MC |
353 | if (outformat == FORMAT_PEM) |
354 | PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout); | |
355 | else if (outformat == FORMAT_ASN1) | |
356 | i2d_PrivateKey_bio(out, pkey); | |
357 | else { | |
358 | BIO_printf(bio_err, "Bad format specified for key\n"); | |
359 | goto end; | |
360 | } | |
361 | ret = 0; | |
600dec15 | 362 | |
0f113f3e MC |
363 | end: |
364 | X509_SIG_free(p8); | |
365 | PKCS8_PRIV_KEY_INFO_free(p8inf); | |
366 | EVP_PKEY_free(pkey); | |
367 | BIO_free_all(out); | |
368 | BIO_free(in); | |
b548a1f1 RS |
369 | OPENSSL_free(passin); |
370 | OPENSSL_free(passout); | |
8eb57af5 | 371 | |
0f113f3e MC |
372 | return ret; |
373 | } |