1 |
|
|
/* $OpenBSD: pkcs8.c,v 1.10 2017/01/20 08:57:12 deraadt Exp $ */ |
2 |
|
|
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
3 |
|
|
* project 1999-2004. |
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 |
13 |
|
|
* notice, this list of conditions and the following disclaimer. |
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 |
|
|
|
59 |
|
|
#include <stdio.h> |
60 |
|
|
#include <string.h> |
61 |
|
|
|
62 |
|
|
#include "apps.h" |
63 |
|
|
#include "progs.h" |
64 |
|
|
|
65 |
|
|
#include <openssl/err.h> |
66 |
|
|
#include <openssl/evp.h> |
67 |
|
|
#include <openssl/pem.h> |
68 |
|
|
#include <openssl/pkcs12.h> |
69 |
|
|
|
70 |
|
|
static struct { |
71 |
|
|
const EVP_CIPHER *cipher; |
72 |
|
|
char *infile; |
73 |
|
|
int informat; |
74 |
|
|
int iter; |
75 |
|
|
int nocrypt; |
76 |
|
|
char *outfile; |
77 |
|
|
int outformat; |
78 |
|
|
int p8_broken; |
79 |
|
|
char *passargin; |
80 |
|
|
char *passargout; |
81 |
|
|
int pbe_nid; |
82 |
|
|
int topk8; |
83 |
|
|
} pkcs8_config; |
84 |
|
|
|
85 |
|
|
static int |
86 |
|
|
pkcs8_opt_v1(char *arg) |
87 |
|
|
{ |
88 |
✗✓ |
16 |
if ((pkcs8_config.pbe_nid = OBJ_txt2nid(arg)) == NID_undef) { |
89 |
|
|
fprintf(stderr, "Unknown PBE algorithm '%s'\n", arg); |
90 |
|
|
return (1); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
8 |
return (0); |
94 |
|
8 |
} |
95 |
|
|
|
96 |
|
|
static int |
97 |
|
|
pkcs8_opt_v2(char *arg) |
98 |
|
|
{ |
99 |
✗✓ |
16 |
if ((pkcs8_config.cipher = EVP_get_cipherbyname(arg)) == NULL) { |
100 |
|
|
fprintf(stderr, "Unknown cipher '%s'\n", arg); |
101 |
|
|
return (1); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
8 |
return (0); |
105 |
|
8 |
} |
106 |
|
|
|
107 |
|
|
static struct option pkcs8_options[] = { |
108 |
|
|
{ |
109 |
|
|
.name = "embed", |
110 |
|
|
.desc = "Generate DSA keys in a broken format", |
111 |
|
|
.type = OPTION_VALUE, |
112 |
|
|
.value = PKCS8_EMBEDDED_PARAM, |
113 |
|
|
.opt.value = &pkcs8_config.p8_broken, |
114 |
|
|
}, |
115 |
|
|
{ |
116 |
|
|
.name = "in", |
117 |
|
|
.argname = "file", |
118 |
|
|
.desc = "Input file (default stdin)", |
119 |
|
|
.type = OPTION_ARG, |
120 |
|
|
.opt.arg = &pkcs8_config.infile, |
121 |
|
|
}, |
122 |
|
|
{ |
123 |
|
|
.name = "inform", |
124 |
|
|
.argname = "format", |
125 |
|
|
.desc = "Input format (DER or PEM (default))", |
126 |
|
|
.type = OPTION_ARG_FORMAT, |
127 |
|
|
.opt.value = &pkcs8_config.informat, |
128 |
|
|
}, |
129 |
|
|
{ |
130 |
|
|
.name = "nocrypt", |
131 |
|
|
.desc = "Use or expect unencrypted private key", |
132 |
|
|
.type = OPTION_FLAG, |
133 |
|
|
.opt.flag = &pkcs8_config.nocrypt, |
134 |
|
|
}, |
135 |
|
|
{ |
136 |
|
|
.name = "noiter", |
137 |
|
|
.desc = "Use 1 as iteration count", |
138 |
|
|
.type = OPTION_VALUE, |
139 |
|
|
.value = 1, |
140 |
|
|
.opt.value = &pkcs8_config.iter, |
141 |
|
|
}, |
142 |
|
|
{ |
143 |
|
|
.name = "nooct", |
144 |
|
|
.desc = "Generate RSA keys in a broken format (no octet)", |
145 |
|
|
.type = OPTION_VALUE, |
146 |
|
|
.value = PKCS8_NO_OCTET, |
147 |
|
|
.opt.value = &pkcs8_config.p8_broken, |
148 |
|
|
}, |
149 |
|
|
{ |
150 |
|
|
.name = "nsdb", |
151 |
|
|
.desc = "Generate DSA keys in the broken Netscape DB format", |
152 |
|
|
.type = OPTION_VALUE, |
153 |
|
|
.value = PKCS8_NS_DB, |
154 |
|
|
.opt.value = &pkcs8_config.p8_broken, |
155 |
|
|
}, |
156 |
|
|
{ |
157 |
|
|
.name = "out", |
158 |
|
|
.argname = "file", |
159 |
|
|
.desc = "Output file (default stdout)", |
160 |
|
|
.type = OPTION_ARG, |
161 |
|
|
.opt.arg = &pkcs8_config.outfile, |
162 |
|
|
}, |
163 |
|
|
{ |
164 |
|
|
.name = "outform", |
165 |
|
|
.argname = "format", |
166 |
|
|
.desc = "Output format (DER or PEM (default))", |
167 |
|
|
.type = OPTION_ARG_FORMAT, |
168 |
|
|
.opt.value = &pkcs8_config.outformat, |
169 |
|
|
}, |
170 |
|
|
{ |
171 |
|
|
.name = "passin", |
172 |
|
|
.argname = "source", |
173 |
|
|
.desc = "Input file passphrase source", |
174 |
|
|
.type = OPTION_ARG, |
175 |
|
|
.opt.arg = &pkcs8_config.passargin, |
176 |
|
|
}, |
177 |
|
|
{ |
178 |
|
|
.name = "passout", |
179 |
|
|
.argname = "source", |
180 |
|
|
.desc = "Output file passphrase source", |
181 |
|
|
.type = OPTION_ARG, |
182 |
|
|
.opt.arg = &pkcs8_config.passargout, |
183 |
|
|
}, |
184 |
|
|
{ |
185 |
|
|
.name = "topk8", |
186 |
|
|
.desc = "Read traditional format key and write PKCS#8 format" |
187 |
|
|
" key", |
188 |
|
|
.type = OPTION_FLAG, |
189 |
|
|
.opt.flag = &pkcs8_config.topk8, |
190 |
|
|
}, |
191 |
|
|
{ |
192 |
|
|
.name = "v1", |
193 |
|
|
.argname = "algorithm", |
194 |
|
|
.desc = "Use PKCS#5 v1.5 or PKCS#12 with given algorithm", |
195 |
|
|
.type = OPTION_ARG_FUNC, |
196 |
|
|
.opt.argfunc = pkcs8_opt_v1, |
197 |
|
|
}, |
198 |
|
|
{ |
199 |
|
|
.name = "v2", |
200 |
|
|
.argname = "cipher", |
201 |
|
|
.desc = "Use PKCS#5 v2.0 with given cipher", |
202 |
|
|
.type = OPTION_ARG_FUNC, |
203 |
|
|
.opt.argfunc = pkcs8_opt_v2, |
204 |
|
|
}, |
205 |
|
|
{ NULL }, |
206 |
|
|
}; |
207 |
|
|
|
208 |
|
|
static void |
209 |
|
|
pkcs8_usage() |
210 |
|
|
{ |
211 |
|
16 |
fprintf(stderr, "usage: pkcs8 [-embed] [-in file] " |
212 |
|
|
"[-inform fmt] [-nocrypt]\n" |
213 |
|
|
" [-noiter] [-nooct] [-nsdb] [-out file] [-outform fmt] " |
214 |
|
|
"[-passin src]\n" |
215 |
|
|
" [-passout src] [-topk8] [-v1 alg] [-v2 alg]\n\n"); |
216 |
|
8 |
options_usage(pkcs8_options); |
217 |
|
8 |
} |
218 |
|
|
|
219 |
|
|
int |
220 |
|
|
pkcs8_main(int argc, char **argv) |
221 |
|
|
{ |
222 |
|
|
BIO *in = NULL, *out = NULL; |
223 |
|
|
X509_SIG *p8 = NULL; |
224 |
|
|
PKCS8_PRIV_KEY_INFO *p8inf = NULL; |
225 |
|
|
EVP_PKEY *pkey = NULL; |
226 |
|
48 |
char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL; |
227 |
|
|
int ret = 1; |
228 |
|
|
|
229 |
✓✗ |
24 |
if (single_execution) { |
230 |
✗✓ |
24 |
if (pledge("stdio cpath wpath rpath tty flock", NULL) == -1) { |
231 |
|
|
perror("pledge"); |
232 |
|
|
exit(1); |
233 |
|
|
} |
234 |
|
|
} |
235 |
|
|
|
236 |
|
24 |
memset(&pkcs8_config, 0, sizeof(pkcs8_config)); |
237 |
|
|
|
238 |
|
24 |
pkcs8_config.iter = PKCS12_DEFAULT_ITER; |
239 |
|
24 |
pkcs8_config.informat = FORMAT_PEM; |
240 |
|
24 |
pkcs8_config.outformat = FORMAT_PEM; |
241 |
|
24 |
pkcs8_config.p8_broken = PKCS8_OK; |
242 |
|
24 |
pkcs8_config.pbe_nid = -1; |
243 |
|
|
|
244 |
✓✓ |
24 |
if (options_parse(argc, argv, pkcs8_options, NULL, NULL) != 0) { |
245 |
|
8 |
pkcs8_usage(); |
246 |
|
8 |
return (1); |
247 |
|
|
} |
248 |
|
|
|
249 |
✗✓ |
32 |
if (!app_passwd(bio_err, pkcs8_config.passargin, |
250 |
|
16 |
pkcs8_config.passargout, &passin, &passout)) { |
251 |
|
|
BIO_printf(bio_err, "Error getting passwords\n"); |
252 |
|
|
goto end; |
253 |
|
|
} |
254 |
✓✓ |
16 |
if ((pkcs8_config.pbe_nid == -1) && !pkcs8_config.cipher) |
255 |
|
8 |
pkcs8_config.pbe_nid = NID_pbeWithMD5AndDES_CBC; |
256 |
|
|
|
257 |
✓✗ |
16 |
if (pkcs8_config.infile) { |
258 |
✗✓ |
16 |
if (!(in = BIO_new_file(pkcs8_config.infile, "rb"))) { |
259 |
|
|
BIO_printf(bio_err, |
260 |
|
|
"Can't open input file '%s'\n", |
261 |
|
|
pkcs8_config.infile); |
262 |
|
|
goto end; |
263 |
|
|
} |
264 |
|
|
} else |
265 |
|
|
in = BIO_new_fp(stdin, BIO_NOCLOSE); |
266 |
|
|
|
267 |
✓✗ |
16 |
if (pkcs8_config.outfile) { |
268 |
✗✓ |
16 |
if (!(out = BIO_new_file(pkcs8_config.outfile, "wb"))) { |
269 |
|
|
BIO_printf(bio_err, "Can't open output file '%s'\n", |
270 |
|
|
pkcs8_config.outfile); |
271 |
|
|
goto end; |
272 |
|
|
} |
273 |
|
|
} else { |
274 |
|
|
out = BIO_new_fp(stdout, BIO_NOCLOSE); |
275 |
|
|
} |
276 |
✓✓ |
16 |
if (pkcs8_config.topk8) { |
277 |
|
16 |
pkey = load_key(bio_err, pkcs8_config.infile, |
278 |
|
8 |
pkcs8_config.informat, 1, passin, "key"); |
279 |
✓✗ |
8 |
if (!pkey) |
280 |
|
|
goto end; |
281 |
✗✓ |
8 |
if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, |
282 |
|
8 |
pkcs8_config.p8_broken))) { |
283 |
|
|
BIO_printf(bio_err, "Error converting key\n"); |
284 |
|
|
ERR_print_errors(bio_err); |
285 |
|
|
goto end; |
286 |
|
|
} |
287 |
✗✓ |
8 |
if (pkcs8_config.nocrypt) { |
288 |
|
|
if (pkcs8_config.outformat == FORMAT_PEM) |
289 |
|
|
PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf); |
290 |
|
|
else if (pkcs8_config.outformat == FORMAT_ASN1) |
291 |
|
|
i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf); |
292 |
|
|
else { |
293 |
|
|
BIO_printf(bio_err, |
294 |
|
|
"Bad format specified for key\n"); |
295 |
|
|
goto end; |
296 |
|
|
} |
297 |
|
|
} else { |
298 |
✓✗ |
8 |
if (passout) |
299 |
|
8 |
p8pass = passout; |
300 |
|
|
else { |
301 |
|
|
p8pass = pass; |
302 |
|
|
if (EVP_read_pw_string(pass, sizeof pass, |
303 |
|
|
"Enter Encryption Password:", 1)) |
304 |
|
|
goto end; |
305 |
|
|
} |
306 |
✗✓ |
16 |
if (!(p8 = PKCS8_encrypt(pkcs8_config.pbe_nid, |
307 |
|
8 |
pkcs8_config.cipher, p8pass, strlen(p8pass), |
308 |
|
8 |
NULL, 0, pkcs8_config.iter, p8inf))) { |
309 |
|
|
BIO_printf(bio_err, "Error encrypting key\n"); |
310 |
|
|
ERR_print_errors(bio_err); |
311 |
|
|
goto end; |
312 |
|
|
} |
313 |
✓✗ |
8 |
if (pkcs8_config.outformat == FORMAT_PEM) |
314 |
|
8 |
PEM_write_bio_PKCS8(out, p8); |
315 |
|
|
else if (pkcs8_config.outformat == FORMAT_ASN1) |
316 |
|
|
i2d_PKCS8_bio(out, p8); |
317 |
|
|
else { |
318 |
|
|
BIO_printf(bio_err, |
319 |
|
|
"Bad format specified for key\n"); |
320 |
|
|
goto end; |
321 |
|
|
} |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
ret = 0; |
325 |
|
8 |
goto end; |
326 |
|
|
} |
327 |
✗✓ |
8 |
if (pkcs8_config.nocrypt) { |
328 |
|
|
if (pkcs8_config.informat == FORMAT_PEM) |
329 |
|
|
p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, |
330 |
|
|
NULL, NULL); |
331 |
|
|
else if (pkcs8_config.informat == FORMAT_ASN1) |
332 |
|
|
p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL); |
333 |
|
|
else { |
334 |
|
|
BIO_printf(bio_err, "Bad format specified for key\n"); |
335 |
|
|
goto end; |
336 |
|
|
} |
337 |
|
|
} else { |
338 |
✓✗ |
8 |
if (pkcs8_config.informat == FORMAT_PEM) |
339 |
|
8 |
p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL); |
340 |
|
|
else if (pkcs8_config.informat == FORMAT_ASN1) |
341 |
|
|
p8 = d2i_PKCS8_bio(in, NULL); |
342 |
|
|
else { |
343 |
|
|
BIO_printf(bio_err, "Bad format specified for key\n"); |
344 |
|
|
goto end; |
345 |
|
|
} |
346 |
|
|
|
347 |
✗✓ |
8 |
if (!p8) { |
348 |
|
|
BIO_printf(bio_err, "Error reading key\n"); |
349 |
|
|
ERR_print_errors(bio_err); |
350 |
|
|
goto end; |
351 |
|
|
} |
352 |
✓✗ |
8 |
if (passin) |
353 |
|
8 |
p8pass = passin; |
354 |
|
|
else { |
355 |
|
|
p8pass = pass; |
356 |
|
|
EVP_read_pw_string(pass, sizeof pass, |
357 |
|
|
"Enter Password:", 0); |
358 |
|
|
} |
359 |
|
8 |
p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass)); |
360 |
|
|
} |
361 |
|
|
|
362 |
✗✓ |
8 |
if (!p8inf) { |
363 |
|
|
BIO_printf(bio_err, "Error decrypting key\n"); |
364 |
|
|
ERR_print_errors(bio_err); |
365 |
|
|
goto end; |
366 |
|
|
} |
367 |
✗✓ |
8 |
if (!(pkey = EVP_PKCS82PKEY(p8inf))) { |
368 |
|
|
BIO_printf(bio_err, "Error converting key\n"); |
369 |
|
|
ERR_print_errors(bio_err); |
370 |
|
|
goto end; |
371 |
|
|
} |
372 |
✗✓ |
8 |
if (p8inf->broken) { |
373 |
|
|
BIO_printf(bio_err, "Warning: broken key encoding: "); |
374 |
|
|
switch (p8inf->broken) { |
375 |
|
|
case PKCS8_NO_OCTET: |
376 |
|
|
BIO_printf(bio_err, "No Octet String in PrivateKey\n"); |
377 |
|
|
break; |
378 |
|
|
|
379 |
|
|
case PKCS8_EMBEDDED_PARAM: |
380 |
|
|
BIO_printf(bio_err, |
381 |
|
|
"DSA parameters included in PrivateKey\n"); |
382 |
|
|
break; |
383 |
|
|
|
384 |
|
|
case PKCS8_NS_DB: |
385 |
|
|
BIO_printf(bio_err, |
386 |
|
|
"DSA public key include in PrivateKey\n"); |
387 |
|
|
break; |
388 |
|
|
|
389 |
|
|
case PKCS8_NEG_PRIVKEY: |
390 |
|
|
BIO_printf(bio_err, "DSA private key value is negative\n"); |
391 |
|
|
break; |
392 |
|
|
|
393 |
|
|
default: |
394 |
|
|
BIO_printf(bio_err, "Unknown broken type\n"); |
395 |
|
|
break; |
396 |
|
|
} |
397 |
|
|
} |
398 |
✗✓ |
8 |
if (pkcs8_config.outformat == FORMAT_PEM) |
399 |
|
|
PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, |
400 |
|
|
passout); |
401 |
✓✗ |
8 |
else if (pkcs8_config.outformat == FORMAT_ASN1) |
402 |
|
8 |
i2d_PrivateKey_bio(out, pkey); |
403 |
|
|
else { |
404 |
|
|
BIO_printf(bio_err, "Bad format specified for key\n"); |
405 |
|
|
goto end; |
406 |
|
|
} |
407 |
|
8 |
ret = 0; |
408 |
|
|
|
409 |
|
|
end: |
410 |
|
16 |
X509_SIG_free(p8); |
411 |
|
16 |
PKCS8_PRIV_KEY_INFO_free(p8inf); |
412 |
|
16 |
EVP_PKEY_free(pkey); |
413 |
|
16 |
BIO_free_all(out); |
414 |
|
16 |
BIO_free(in); |
415 |
|
16 |
free(passin); |
416 |
|
16 |
free(passout); |
417 |
|
|
|
418 |
|
16 |
return ret; |
419 |
|
24 |
} |