diff options
Diffstat (limited to 'src/usr.bin/openssl/pkcs8.c')
-rw-r--r-- | src/usr.bin/openssl/pkcs8.c | 367 |
1 files changed, 367 insertions, 0 deletions
diff --git a/src/usr.bin/openssl/pkcs8.c b/src/usr.bin/openssl/pkcs8.c new file mode 100644 index 0000000000..1715fe1d6b --- /dev/null +++ b/src/usr.bin/openssl/pkcs8.c | |||
@@ -0,0 +1,367 @@ | |||
1 | /* $OpenBSD: pkcs8.c,v 1.1 2014/08/26 17:47:25 jsing 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 | |||
64 | #include <openssl/err.h> | ||
65 | #include <openssl/evp.h> | ||
66 | #include <openssl/pem.h> | ||
67 | #include <openssl/pkcs12.h> | ||
68 | |||
69 | int pkcs8_main(int, char **); | ||
70 | |||
71 | int | ||
72 | pkcs8_main(int argc, char **argv) | ||
73 | { | ||
74 | ENGINE *e = NULL; | ||
75 | char **args, *infile = NULL, *outfile = NULL; | ||
76 | char *passargin = NULL, *passargout = NULL; | ||
77 | BIO *in = NULL, *out = NULL; | ||
78 | int topk8 = 0; | ||
79 | int pbe_nid = -1; | ||
80 | const EVP_CIPHER *cipher = NULL; | ||
81 | int iter = PKCS12_DEFAULT_ITER; | ||
82 | int informat, outformat; | ||
83 | int p8_broken = PKCS8_OK; | ||
84 | int nocrypt = 0; | ||
85 | X509_SIG *p8 = NULL; | ||
86 | PKCS8_PRIV_KEY_INFO *p8inf = NULL; | ||
87 | EVP_PKEY *pkey = NULL; | ||
88 | char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL; | ||
89 | int badarg = 0; | ||
90 | int ret = 1; | ||
91 | #ifndef OPENSSL_NO_ENGINE | ||
92 | char *engine = NULL; | ||
93 | #endif | ||
94 | |||
95 | informat = FORMAT_PEM; | ||
96 | outformat = FORMAT_PEM; | ||
97 | |||
98 | ERR_load_crypto_strings(); | ||
99 | OpenSSL_add_all_algorithms(); | ||
100 | args = argv + 1; | ||
101 | while (!badarg && *args && *args[0] == '-') { | ||
102 | if (!strcmp(*args, "-v2")) { | ||
103 | if (args[1]) { | ||
104 | args++; | ||
105 | cipher = EVP_get_cipherbyname(*args); | ||
106 | if (!cipher) { | ||
107 | BIO_printf(bio_err, | ||
108 | "Unknown cipher %s\n", *args); | ||
109 | badarg = 1; | ||
110 | } | ||
111 | } else | ||
112 | badarg = 1; | ||
113 | } else if (!strcmp(*args, "-v1")) { | ||
114 | if (args[1]) { | ||
115 | args++; | ||
116 | pbe_nid = OBJ_txt2nid(*args); | ||
117 | if (pbe_nid == NID_undef) { | ||
118 | BIO_printf(bio_err, | ||
119 | "Unknown PBE algorithm %s\n", *args); | ||
120 | badarg = 1; | ||
121 | } | ||
122 | } else | ||
123 | badarg = 1; | ||
124 | } else if (!strcmp(*args, "-inform")) { | ||
125 | if (args[1]) { | ||
126 | args++; | ||
127 | informat = str2fmt(*args); | ||
128 | } else | ||
129 | badarg = 1; | ||
130 | } else if (!strcmp(*args, "-outform")) { | ||
131 | if (args[1]) { | ||
132 | args++; | ||
133 | outformat = str2fmt(*args); | ||
134 | } else | ||
135 | badarg = 1; | ||
136 | } else if (!strcmp(*args, "-topk8")) | ||
137 | topk8 = 1; | ||
138 | else if (!strcmp(*args, "-noiter")) | ||
139 | iter = 1; | ||
140 | else if (!strcmp(*args, "-nocrypt")) | ||
141 | nocrypt = 1; | ||
142 | else if (!strcmp(*args, "-nooct")) | ||
143 | p8_broken = PKCS8_NO_OCTET; | ||
144 | else if (!strcmp(*args, "-nsdb")) | ||
145 | p8_broken = PKCS8_NS_DB; | ||
146 | else if (!strcmp(*args, "-embed")) | ||
147 | p8_broken = PKCS8_EMBEDDED_PARAM; | ||
148 | else if (!strcmp(*args, "-passin")) { | ||
149 | if (!args[1]) | ||
150 | goto bad; | ||
151 | passargin = *(++args); | ||
152 | } else if (!strcmp(*args, "-passout")) { | ||
153 | if (!args[1]) | ||
154 | goto bad; | ||
155 | passargout = *(++args); | ||
156 | } | ||
157 | #ifndef OPENSSL_NO_ENGINE | ||
158 | else if (strcmp(*args, "-engine") == 0) { | ||
159 | if (!args[1]) | ||
160 | goto bad; | ||
161 | engine = *(++args); | ||
162 | } | ||
163 | #endif | ||
164 | else if (!strcmp(*args, "-in")) { | ||
165 | if (args[1]) { | ||
166 | args++; | ||
167 | infile = *args; | ||
168 | } else | ||
169 | badarg = 1; | ||
170 | } else if (!strcmp(*args, "-out")) { | ||
171 | if (args[1]) { | ||
172 | args++; | ||
173 | outfile = *args; | ||
174 | } else | ||
175 | badarg = 1; | ||
176 | } else | ||
177 | badarg = 1; | ||
178 | args++; | ||
179 | } | ||
180 | |||
181 | if (badarg) { | ||
182 | bad: | ||
183 | BIO_printf(bio_err, "Usage pkcs8 [options]\n"); | ||
184 | BIO_printf(bio_err, "where options are\n"); | ||
185 | BIO_printf(bio_err, "-in file input file\n"); | ||
186 | BIO_printf(bio_err, "-inform X input format (DER or PEM)\n"); | ||
187 | BIO_printf(bio_err, "-passin arg input file pass phrase source\n"); | ||
188 | BIO_printf(bio_err, "-outform X output format (DER or PEM)\n"); | ||
189 | BIO_printf(bio_err, "-out file output file\n"); | ||
190 | BIO_printf(bio_err, "-passout arg output file pass phrase source\n"); | ||
191 | BIO_printf(bio_err, "-topk8 output PKCS8 file\n"); | ||
192 | BIO_printf(bio_err, "-nooct use (nonstandard) no octet format\n"); | ||
193 | BIO_printf(bio_err, "-embed use (nonstandard) embedded DSA parameters format\n"); | ||
194 | BIO_printf(bio_err, "-nsdb use (nonstandard) DSA Netscape DB format\n"); | ||
195 | BIO_printf(bio_err, "-noiter use 1 as iteration count\n"); | ||
196 | BIO_printf(bio_err, "-nocrypt use or expect unencrypted private key\n"); | ||
197 | BIO_printf(bio_err, "-v2 alg use PKCS#5 v2.0 and cipher \"alg\"\n"); | ||
198 | BIO_printf(bio_err, "-v1 obj use PKCS#5 v1.5 and cipher \"alg\"\n"); | ||
199 | #ifndef OPENSSL_NO_ENGINE | ||
200 | BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n"); | ||
201 | #endif | ||
202 | goto end; | ||
203 | } | ||
204 | #ifndef OPENSSL_NO_ENGINE | ||
205 | e = setup_engine(bio_err, engine, 0); | ||
206 | #endif | ||
207 | |||
208 | if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { | ||
209 | BIO_printf(bio_err, "Error getting passwords\n"); | ||
210 | goto end; | ||
211 | } | ||
212 | if ((pbe_nid == -1) && !cipher) | ||
213 | pbe_nid = NID_pbeWithMD5AndDES_CBC; | ||
214 | |||
215 | if (infile) { | ||
216 | if (!(in = BIO_new_file(infile, "rb"))) { | ||
217 | BIO_printf(bio_err, | ||
218 | "Can't open input file %s\n", infile); | ||
219 | goto end; | ||
220 | } | ||
221 | } else | ||
222 | in = BIO_new_fp(stdin, BIO_NOCLOSE); | ||
223 | |||
224 | if (outfile) { | ||
225 | if (!(out = BIO_new_file(outfile, "wb"))) { | ||
226 | BIO_printf(bio_err, | ||
227 | "Can't open output file %s\n", outfile); | ||
228 | goto end; | ||
229 | } | ||
230 | } else { | ||
231 | out = BIO_new_fp(stdout, BIO_NOCLOSE); | ||
232 | } | ||
233 | if (topk8) { | ||
234 | pkey = load_key(bio_err, infile, informat, 1, | ||
235 | passin, e, "key"); | ||
236 | if (!pkey) | ||
237 | goto end; | ||
238 | if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken))) { | ||
239 | BIO_printf(bio_err, "Error converting key\n"); | ||
240 | ERR_print_errors(bio_err); | ||
241 | goto end; | ||
242 | } | ||
243 | if (nocrypt) { | ||
244 | if (outformat == FORMAT_PEM) | ||
245 | PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf); | ||
246 | else if (outformat == FORMAT_ASN1) | ||
247 | i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf); | ||
248 | else { | ||
249 | BIO_printf(bio_err, "Bad format specified for key\n"); | ||
250 | goto end; | ||
251 | } | ||
252 | } else { | ||
253 | if (passout) | ||
254 | p8pass = passout; | ||
255 | else { | ||
256 | p8pass = pass; | ||
257 | if (EVP_read_pw_string(pass, sizeof pass, "Enter Encryption Password:", 1)) | ||
258 | goto end; | ||
259 | } | ||
260 | if (!(p8 = PKCS8_encrypt(pbe_nid, cipher, | ||
261 | p8pass, strlen(p8pass), | ||
262 | NULL, 0, iter, p8inf))) { | ||
263 | BIO_printf(bio_err, "Error encrypting key\n"); | ||
264 | ERR_print_errors(bio_err); | ||
265 | goto end; | ||
266 | } | ||
267 | if (outformat == FORMAT_PEM) | ||
268 | PEM_write_bio_PKCS8(out, p8); | ||
269 | else if (outformat == FORMAT_ASN1) | ||
270 | i2d_PKCS8_bio(out, p8); | ||
271 | else { | ||
272 | BIO_printf(bio_err, "Bad format specified for key\n"); | ||
273 | goto end; | ||
274 | } | ||
275 | } | ||
276 | |||
277 | ret = 0; | ||
278 | goto end; | ||
279 | } | ||
280 | if (nocrypt) { | ||
281 | if (informat == FORMAT_PEM) | ||
282 | p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL); | ||
283 | else if (informat == FORMAT_ASN1) | ||
284 | p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL); | ||
285 | else { | ||
286 | BIO_printf(bio_err, "Bad format specified for key\n"); | ||
287 | goto end; | ||
288 | } | ||
289 | } else { | ||
290 | if (informat == FORMAT_PEM) | ||
291 | p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL); | ||
292 | else if (informat == FORMAT_ASN1) | ||
293 | p8 = d2i_PKCS8_bio(in, NULL); | ||
294 | else { | ||
295 | BIO_printf(bio_err, "Bad format specified for key\n"); | ||
296 | goto end; | ||
297 | } | ||
298 | |||
299 | if (!p8) { | ||
300 | BIO_printf(bio_err, "Error reading key\n"); | ||
301 | ERR_print_errors(bio_err); | ||
302 | goto end; | ||
303 | } | ||
304 | if (passin) | ||
305 | p8pass = passin; | ||
306 | else { | ||
307 | p8pass = pass; | ||
308 | EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0); | ||
309 | } | ||
310 | p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass)); | ||
311 | } | ||
312 | |||
313 | if (!p8inf) { | ||
314 | BIO_printf(bio_err, "Error decrypting key\n"); | ||
315 | ERR_print_errors(bio_err); | ||
316 | goto end; | ||
317 | } | ||
318 | if (!(pkey = EVP_PKCS82PKEY(p8inf))) { | ||
319 | BIO_printf(bio_err, "Error converting key\n"); | ||
320 | ERR_print_errors(bio_err); | ||
321 | goto end; | ||
322 | } | ||
323 | if (p8inf->broken) { | ||
324 | BIO_printf(bio_err, "Warning: broken key encoding: "); | ||
325 | switch (p8inf->broken) { | ||
326 | case PKCS8_NO_OCTET: | ||
327 | BIO_printf(bio_err, "No Octet String in PrivateKey\n"); | ||
328 | break; | ||
329 | |||
330 | case PKCS8_EMBEDDED_PARAM: | ||
331 | BIO_printf(bio_err, "DSA parameters included in PrivateKey\n"); | ||
332 | break; | ||
333 | |||
334 | case PKCS8_NS_DB: | ||
335 | BIO_printf(bio_err, "DSA public key include in PrivateKey\n"); | ||
336 | break; | ||
337 | |||
338 | case PKCS8_NEG_PRIVKEY: | ||
339 | BIO_printf(bio_err, "DSA private key value is negative\n"); | ||
340 | break; | ||
341 | |||
342 | default: | ||
343 | BIO_printf(bio_err, "Unknown broken type\n"); | ||
344 | break; | ||
345 | } | ||
346 | } | ||
347 | if (outformat == FORMAT_PEM) | ||
348 | PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout); | ||
349 | else if (outformat == FORMAT_ASN1) | ||
350 | i2d_PrivateKey_bio(out, pkey); | ||
351 | else { | ||
352 | BIO_printf(bio_err, "Bad format specified for key\n"); | ||
353 | goto end; | ||
354 | } | ||
355 | ret = 0; | ||
356 | |||
357 | end: | ||
358 | X509_SIG_free(p8); | ||
359 | PKCS8_PRIV_KEY_INFO_free(p8inf); | ||
360 | EVP_PKEY_free(pkey); | ||
361 | BIO_free_all(out); | ||
362 | BIO_free(in); | ||
363 | free(passin); | ||
364 | free(passout); | ||
365 | |||
366 | return ret; | ||
367 | } | ||