]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
6738bf14 | 2 | * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. |
ee0508d4 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
ee0508d4 DSH |
8 | */ |
9 | ||
6e04afb8 | 10 | #include <openssl/opensslconf.h> |
96bea000 MC |
11 | #if defined(OPENSSL_NO_DES) |
12 | NON_EMPTY_TRANSLATION_UNIT | |
13 | #else | |
6e04afb8 | 14 | |
0f113f3e MC |
15 | # include <stdio.h> |
16 | # include <stdlib.h> | |
17 | # include <string.h> | |
18 | # include "apps.h" | |
dab2cd68 | 19 | # include "progs.h" |
0f113f3e MC |
20 | # include <openssl/crypto.h> |
21 | # include <openssl/err.h> | |
22 | # include <openssl/pem.h> | |
23 | # include <openssl/pkcs12.h> | |
ee0508d4 | 24 | |
0f113f3e MC |
25 | # define NOKEYS 0x1 |
26 | # define NOCERTS 0x2 | |
27 | # define INFO 0x4 | |
28 | # define CLCERTS 0x8 | |
29 | # define CACERTS 0x10 | |
30 | ||
eee95522 P |
31 | #define PASSWD_BUF_SIZE 2048 |
32 | ||
e29c73c9 VD |
33 | static int get_cert_chain(X509 *cert, X509_STORE *store, |
34 | STACK_OF(X509) **chain); | |
28da1455 MC |
35 | int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, |
36 | const char *pass, int passlen, int options, | |
37 | char *pempass, const EVP_CIPHER *enc); | |
38 | int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags, | |
39 | const char *pass, int passlen, int options, | |
40 | char *pempass, const EVP_CIPHER *enc); | |
41 | int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags, | |
42 | const char *pass, int passlen, | |
43 | int options, char *pempass, const EVP_CIPHER *enc); | |
dbcc7b45 | 44 | void print_attribute(BIO *out, const ASN1_TYPE *av); |
b2e57e09 | 45 | int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst, |
0f113f3e | 46 | const char *name); |
ee0508d4 | 47 | void hex_prin(BIO *out, unsigned char *buf, int len); |
59b4da05 | 48 | static int alg_print(const X509_ALGOR *alg); |
84c15db5 | 49 | int cert_load(BIO *in, STACK_OF(X509) *sk); |
7e1b7485 RS |
50 | static int set_pbe(int *ppbe, const char *str); |
51 | ||
52 | typedef enum OPTION_choice { | |
53 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
54 | OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS, | |
55 | OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER, | |
1fdde917 | 56 | OPT_DESCERT, OPT_EXPORT, OPT_ITER, OPT_NOITER, OPT_MACITER, OPT_NOMACITER, |
7e1b7485 | 57 | OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE, |
3ee1eac2 | 58 | OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME, |
7e1b7485 | 59 | OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH, |
fd3397fc | 60 | OPT_CAFILE, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_ENGINE, |
6bd4e3f2 | 61 | OPT_R_ENUM, OPT_PROV_ENUM |
7e1b7485 RS |
62 | } OPTION_CHOICE; |
63 | ||
44c83ebd | 64 | const OPTIONS pkcs12_options[] = { |
5388f986 | 65 | OPT_SECTION("General"), |
7e1b7485 | 66 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
67 | # ifndef OPENSSL_NO_ENGINE |
68 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
69 | # endif | |
70 | ||
71 | OPT_SECTION("CA"), | |
72 | {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"}, | |
73 | {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"}, | |
74 | {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"}, | |
75 | {"no-CAfile", OPT_NOCAFILE, '-', | |
76 | "Do not load the default certificates file"}, | |
77 | {"no-CApath", OPT_NOCAPATH, '-', | |
78 | "Do not load certificates from the default certificates directory"}, | |
79 | {"no-CAstore", OPT_NOCASTORE, '-', | |
80 | "Do not load certificates from the default certificates store"}, | |
81 | ||
82 | OPT_SECTION("Input"), | |
83 | {"inkey", OPT_INKEY, 's', "Private key if not infile"}, | |
84 | {"certfile", OPT_CERTFILE, '<', "Load certs from file"}, | |
85 | {"name", OPT_NAME, 's', "Use name as friendly name"}, | |
86 | {"CSP", OPT_CSP, 's', "Microsoft CSP name"}, | |
87 | {"caname", OPT_CANAME, 's', | |
88 | "Use name as CA friendly name (can be repeated)"}, | |
89 | {"in", OPT_IN, '<', "Input filename"}, | |
90 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, | |
91 | ||
92 | OPT_SECTION("Output"), | |
93 | {"export", OPT_EXPORT, '-', "Output PKCS12 file"}, | |
94 | {"LMK", OPT_LMK, '-', | |
95 | "Add local machine keyset attribute to private key"}, | |
96 | {"macalg", OPT_MACALG, 's', | |
97 | "Digest algorithm used in MAC (default SHA1)"}, | |
98 | {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"}, | |
99 | {"out", OPT_OUT, '>', "Output filename"}, | |
100 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, | |
101 | {"password", OPT_PASSWORD, 's', "Set import/export password source"}, | |
7e1b7485 RS |
102 | {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"}, |
103 | {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"}, | |
104 | {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"}, | |
105 | {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"}, | |
7e1b7485 RS |
106 | {"chain", OPT_CHAIN, '-', "Add certificate chain"}, |
107 | {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"}, | |
108 | {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"}, | |
5388f986 RS |
109 | {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"}, |
110 | {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"}, | |
111 | {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"}, | |
112 | {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"}, | |
113 | ||
114 | OPT_SECTION("Encryption"), | |
7e1b7485 RS |
115 | # ifndef OPENSSL_NO_RC2 |
116 | {"descert", OPT_DESCERT, '-', | |
117 | "Encrypt output with 3DES (default RC2-40)"}, | |
118 | {"certpbe", OPT_CERTPBE, 's', | |
119 | "Certificate PBE algorithm (default RC2-40)"}, | |
120 | # else | |
121 | {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"}, | |
122 | {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"}, | |
123 | # endif | |
1fdde917 IG |
124 | {"iter", OPT_ITER, 'p', "Specify the iteration count for encryption key and MAC"}, |
125 | {"noiter", OPT_NOITER, '-', "Don't use encryption key iteration"}, | |
126 | {"maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility"}, | |
7e1b7485 RS |
127 | {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"}, |
128 | {"nomac", OPT_NOMAC, '-', "Don't generate MAC"}, | |
7e1b7485 | 129 | {"nodes", OPT_NODES, '-', "Don't encrypt private keys"}, |
9c3bcfa0 | 130 | {"", OPT_CIPHER, '-', "Any supported cipher"}, |
5388f986 RS |
131 | |
132 | OPT_R_OPTIONS, | |
6bd4e3f2 | 133 | OPT_PROV_OPTIONS, |
7e1b7485 RS |
134 | {NULL} |
135 | }; | |
667ac4ec | 136 | |
7e1b7485 | 137 | int pkcs12_main(int argc, char **argv) |
ee0508d4 | 138 | { |
7e1b7485 RS |
139 | char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL; |
140 | char *name = NULL, *csp_name = NULL; | |
eee95522 | 141 | char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = ""; |
7e1b7485 RS |
142 | int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0; |
143 | int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER; | |
144 | # ifndef OPENSSL_NO_RC2 | |
ee0508d4 | 145 | int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC; |
7e1b7485 RS |
146 | # else |
147 | int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; | |
148 | # endif | |
525f51f6 | 149 | int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; |
923b1857 RL |
150 | int ret = 1, macver = 1, add_lmk = 0, private = 0; |
151 | int noprompt = 0; | |
7e1b7485 | 152 | char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL; |
3ee1eac2 | 153 | char *passin = NULL, *passout = NULL, *macalg = NULL; |
647ac8d3 | 154 | char *cpass = NULL, *mpass = NULL, *badpass = NULL; |
fd3397fc RL |
155 | const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog; |
156 | int noCApath = 0, noCAfile = 0, noCAstore = 0; | |
7e1b7485 RS |
157 | ENGINE *e = NULL; |
158 | BIO *in = NULL, *out = NULL; | |
159 | PKCS12 *p12 = NULL; | |
c869da88 | 160 | STACK_OF(OPENSSL_STRING) *canames = NULL; |
7e1b7485 RS |
161 | const EVP_CIPHER *enc = EVP_des_ede3_cbc(); |
162 | OPTION_CHOICE o; | |
163 | ||
164 | prog = opt_init(argc, argv, pkcs12_options); | |
165 | while ((o = opt_next()) != OPT_EOF) { | |
166 | switch (o) { | |
167 | case OPT_EOF: | |
168 | case OPT_ERR: | |
169 | opthelp: | |
170 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
171 | goto end; | |
172 | case OPT_HELP: | |
173 | opt_help(pkcs12_options); | |
174 | ret = 0; | |
175 | goto end; | |
176 | case OPT_NOKEYS: | |
177 | options |= NOKEYS; | |
178 | break; | |
179 | case OPT_KEYEX: | |
180 | keytype = KEY_EX; | |
181 | break; | |
182 | case OPT_KEYSIG: | |
183 | keytype = KEY_SIG; | |
184 | break; | |
185 | case OPT_NOCERTS: | |
186 | options |= NOCERTS; | |
187 | break; | |
188 | case OPT_CLCERTS: | |
189 | options |= CLCERTS; | |
190 | break; | |
191 | case OPT_CACERTS: | |
192 | options |= CACERTS; | |
193 | break; | |
194 | case OPT_NOOUT: | |
195 | options |= (NOKEYS | NOCERTS); | |
196 | break; | |
197 | case OPT_INFO: | |
198 | options |= INFO; | |
199 | break; | |
200 | case OPT_CHAIN: | |
201 | chain = 1; | |
202 | break; | |
203 | case OPT_TWOPASS: | |
204 | twopass = 1; | |
205 | break; | |
206 | case OPT_NOMACVER: | |
207 | macver = 0; | |
208 | break; | |
209 | case OPT_DESCERT: | |
210 | cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; | |
211 | break; | |
212 | case OPT_EXPORT: | |
213 | export_cert = 1; | |
214 | break; | |
215 | case OPT_CIPHER: | |
216 | if (!opt_cipher(opt_unknown(), &enc)) | |
217 | goto opthelp; | |
218 | break; | |
1fdde917 IG |
219 | case OPT_ITER: |
220 | if (!opt_int(opt_arg(), &iter)) | |
221 | goto opthelp; | |
222 | maciter = iter; | |
223 | break; | |
7e1b7485 RS |
224 | case OPT_NOITER: |
225 | iter = 1; | |
226 | break; | |
227 | case OPT_MACITER: | |
1fdde917 | 228 | /* no-op */ |
7e1b7485 RS |
229 | break; |
230 | case OPT_NOMACITER: | |
231 | maciter = 1; | |
232 | break; | |
233 | case OPT_NOMAC: | |
234 | maciter = -1; | |
235 | break; | |
236 | case OPT_MACALG: | |
237 | macalg = opt_arg(); | |
238 | break; | |
239 | case OPT_NODES: | |
240 | enc = NULL; | |
241 | break; | |
242 | case OPT_CERTPBE: | |
243 | if (!set_pbe(&cert_pbe, opt_arg())) | |
244 | goto opthelp; | |
245 | break; | |
246 | case OPT_KEYPBE: | |
247 | if (!set_pbe(&key_pbe, opt_arg())) | |
248 | goto opthelp; | |
249 | break; | |
3ee1eac2 RS |
250 | case OPT_R_CASES: |
251 | if (!opt_rand(o)) | |
252 | goto end; | |
7e1b7485 RS |
253 | break; |
254 | case OPT_INKEY: | |
255 | keyname = opt_arg(); | |
256 | break; | |
257 | case OPT_CERTFILE: | |
258 | certfile = opt_arg(); | |
259 | break; | |
260 | case OPT_NAME: | |
261 | name = opt_arg(); | |
262 | break; | |
263 | case OPT_LMK: | |
264 | add_lmk = 1; | |
265 | break; | |
266 | case OPT_CSP: | |
267 | csp_name = opt_arg(); | |
268 | break; | |
269 | case OPT_CANAME: | |
270 | if (canames == NULL | |
271 | && (canames = sk_OPENSSL_STRING_new_null()) == NULL) | |
272 | goto end; | |
273 | sk_OPENSSL_STRING_push(canames, opt_arg()); | |
274 | break; | |
275 | case OPT_IN: | |
276 | infile = opt_arg(); | |
277 | break; | |
278 | case OPT_OUT: | |
279 | outfile = opt_arg(); | |
280 | break; | |
281 | case OPT_PASSIN: | |
282 | passinarg = opt_arg(); | |
283 | break; | |
284 | case OPT_PASSOUT: | |
285 | passoutarg = opt_arg(); | |
286 | break; | |
287 | case OPT_PASSWORD: | |
288 | passarg = opt_arg(); | |
289 | break; | |
290 | case OPT_CAPATH: | |
291 | CApath = opt_arg(); | |
292 | break; | |
fd3397fc RL |
293 | case OPT_CASTORE: |
294 | CAstore = opt_arg(); | |
295 | break; | |
7e1b7485 RS |
296 | case OPT_CAFILE: |
297 | CAfile = opt_arg(); | |
298 | break; | |
2b6bcb70 MC |
299 | case OPT_NOCAPATH: |
300 | noCApath = 1; | |
301 | break; | |
fd3397fc RL |
302 | case OPT_NOCASTORE: |
303 | noCAstore = 1; | |
304 | break; | |
2b6bcb70 MC |
305 | case OPT_NOCAFILE: |
306 | noCAfile = 1; | |
307 | break; | |
7e1b7485 | 308 | case OPT_ENGINE: |
333b070e | 309 | e = setup_engine(opt_arg(), 0); |
7e1b7485 | 310 | break; |
6bd4e3f2 P |
311 | case OPT_PROV_CASES: |
312 | if (!opt_provider(o)) | |
313 | goto end; | |
314 | break; | |
7e1b7485 | 315 | } |
ee0508d4 | 316 | } |
7e1b7485 | 317 | argc = opt_num_rest(); |
03358517 KR |
318 | if (argc != 0) |
319 | goto opthelp; | |
320 | ||
3b061a00 | 321 | private = 1; |
ee0508d4 | 322 | |
2234212c | 323 | if (passarg != NULL) { |
0f113f3e | 324 | if (export_cert) |
7e1b7485 | 325 | passoutarg = passarg; |
0f113f3e | 326 | else |
7e1b7485 | 327 | passinarg = passarg; |
a3fe382e DSH |
328 | } |
329 | ||
7e1b7485 | 330 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
0f113f3e MC |
331 | BIO_printf(bio_err, "Error getting passwords\n"); |
332 | goto end; | |
a3fe382e DSH |
333 | } |
334 | ||
2234212c | 335 | if (cpass == NULL) { |
0f113f3e MC |
336 | if (export_cert) |
337 | cpass = passout; | |
338 | else | |
339 | cpass = passin; | |
f07fb9b2 DSH |
340 | } |
341 | ||
2234212c | 342 | if (cpass != NULL) { |
0f113f3e MC |
343 | mpass = cpass; |
344 | noprompt = 1; | |
40b64553 MC |
345 | if (twopass) { |
346 | if (export_cert) | |
347 | BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n"); | |
348 | else | |
349 | BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n"); | |
350 | goto end; | |
351 | } | |
f07fb9b2 | 352 | } else { |
0f113f3e MC |
353 | cpass = pass; |
354 | mpass = macpass; | |
e40b7abe DSH |
355 | } |
356 | ||
ee0508d4 | 357 | if (twopass) { |
2234212c | 358 | /* To avoid bit rot */ |
923b1857 | 359 | if (1) { |
48feaceb | 360 | #ifndef OPENSSL_NO_UI_CONSOLE |
cbe29648 RS |
361 | if (EVP_read_pw_string( |
362 | macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) { | |
923b1857 RL |
363 | BIO_printf(bio_err, "Can't read Password\n"); |
364 | goto end; | |
365 | } | |
366 | } else { | |
367 | #endif | |
368 | BIO_printf(bio_err, "Unsupported option -twopass\n"); | |
0f113f3e MC |
369 | goto end; |
370 | } | |
ee0508d4 DSH |
371 | } |
372 | ||
2d681b77 | 373 | if (export_cert) { |
0f113f3e MC |
374 | EVP_PKEY *key = NULL; |
375 | X509 *ucert = NULL, *x = NULL; | |
376 | STACK_OF(X509) *certs = NULL; | |
377 | const EVP_MD *macmd = NULL; | |
378 | unsigned char *catmp = NULL; | |
379 | int i; | |
380 | ||
381 | if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) { | |
382 | BIO_printf(bio_err, "Nothing to do!\n"); | |
383 | goto export_end; | |
384 | } | |
385 | ||
386 | if (options & NOCERTS) | |
387 | chain = 0; | |
388 | ||
0f113f3e | 389 | if (!(options & NOKEYS)) { |
7e1b7485 | 390 | key = load_key(keyname ? keyname : infile, |
0f113f3e | 391 | FORMAT_PEM, 1, passin, e, "private key"); |
2234212c | 392 | if (key == NULL) |
0f113f3e MC |
393 | goto export_end; |
394 | } | |
0f113f3e MC |
395 | |
396 | /* Load in all certs in input file */ | |
397 | if (!(options & NOCERTS)) { | |
a773b52a | 398 | if (!load_certs(infile, &certs, FORMAT_PEM, NULL, |
0996dc54 | 399 | "certificates")) |
0f113f3e MC |
400 | goto export_end; |
401 | ||
2234212c | 402 | if (key != NULL) { |
0f113f3e MC |
403 | /* Look for matching private key */ |
404 | for (i = 0; i < sk_X509_num(certs); i++) { | |
405 | x = sk_X509_value(certs, i); | |
406 | if (X509_check_private_key(x, key)) { | |
407 | ucert = x; | |
408 | /* Zero keyid and alias */ | |
409 | X509_keyid_set1(ucert, NULL, 0); | |
410 | X509_alias_set1(ucert, NULL, 0); | |
411 | /* Remove from list */ | |
412 | (void)sk_X509_delete(certs, i); | |
413 | break; | |
414 | } | |
415 | } | |
2234212c | 416 | if (ucert == NULL) { |
0f113f3e MC |
417 | BIO_printf(bio_err, |
418 | "No certificate matches private key\n"); | |
419 | goto export_end; | |
420 | } | |
421 | } | |
422 | ||
423 | } | |
0f113f3e MC |
424 | |
425 | /* Add any more certificates asked for */ | |
2234212c | 426 | if (certfile != NULL) { |
a773b52a | 427 | if (!load_certs(certfile, &certs, FORMAT_PEM, NULL, |
0996dc54 | 428 | "certificates from certfile")) |
0f113f3e | 429 | goto export_end; |
0f113f3e | 430 | } |
0f113f3e MC |
431 | |
432 | /* If chaining get chain from user cert */ | |
433 | if (chain) { | |
434 | int vret; | |
435 | STACK_OF(X509) *chain2; | |
7e1b7485 | 436 | X509_STORE *store; |
fd3397fc RL |
437 | if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath, |
438 | CAstore, noCAstore)) | |
2b6bcb70 | 439 | == NULL) |
0f113f3e | 440 | goto export_end; |
0f113f3e MC |
441 | |
442 | vret = get_cert_chain(ucert, store, &chain2); | |
443 | X509_STORE_free(store); | |
444 | ||
e29c73c9 | 445 | if (vret == X509_V_OK) { |
0f113f3e MC |
446 | /* Exclude verified certificate */ |
447 | for (i = 1; i < sk_X509_num(chain2); i++) | |
448 | sk_X509_push(certs, sk_X509_value(chain2, i)); | |
449 | /* Free first certificate */ | |
450 | X509_free(sk_X509_value(chain2, 0)); | |
451 | sk_X509_free(chain2); | |
452 | } else { | |
e29c73c9 | 453 | if (vret != X509_V_ERR_UNSPECIFIED) |
0f113f3e MC |
454 | BIO_printf(bio_err, "Error %s getting chain.\n", |
455 | X509_verify_cert_error_string(vret)); | |
456 | else | |
457 | ERR_print_errors(bio_err); | |
458 | goto export_end; | |
459 | } | |
460 | } | |
461 | ||
462 | /* Add any CA names */ | |
463 | ||
464 | for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) { | |
465 | catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i); | |
466 | X509_alias_set1(sk_X509_value(certs, i), catmp, -1); | |
467 | } | |
468 | ||
2234212c | 469 | if (csp_name != NULL && key != NULL) |
0f113f3e MC |
470 | EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name, |
471 | MBSTRING_ASC, (unsigned char *)csp_name, | |
472 | -1); | |
473 | ||
2234212c | 474 | if (add_lmk && key != NULL) |
0f113f3e MC |
475 | EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1); |
476 | ||
923b1857 | 477 | if (!noprompt) { |
2234212c | 478 | /* To avoid bit rot */ |
923b1857 | 479 | if (1) { |
48feaceb | 480 | #ifndef OPENSSL_NO_UI_CONSOLE |
cbe29648 RS |
481 | if (EVP_read_pw_string(pass, sizeof(pass), |
482 | "Enter Export Password:", 1)) { | |
923b1857 RL |
483 | BIO_printf(bio_err, "Can't read Password\n"); |
484 | goto export_end; | |
485 | } | |
486 | } else { | |
487 | #endif | |
488 | BIO_printf(bio_err, "Password required\n"); | |
489 | goto export_end; | |
490 | } | |
0f113f3e | 491 | } |
923b1857 | 492 | |
0f113f3e | 493 | if (!twopass) |
eee95522 | 494 | OPENSSL_strlcpy(macpass, pass, sizeof(macpass)); |
0f113f3e | 495 | |
0f113f3e MC |
496 | p12 = PKCS12_create(cpass, name, key, ucert, certs, |
497 | key_pbe, cert_pbe, iter, -1, keytype); | |
498 | ||
12a765a5 | 499 | if (p12 == NULL) { |
0f113f3e MC |
500 | ERR_print_errors(bio_err); |
501 | goto export_end; | |
502 | } | |
503 | ||
504 | if (macalg) { | |
7e1b7485 RS |
505 | if (!opt_md(macalg, &macmd)) |
506 | goto opthelp; | |
0f113f3e MC |
507 | } |
508 | ||
509 | if (maciter != -1) | |
510 | PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd); | |
511 | ||
3b061a00 | 512 | assert(private); |
bdd58d98 RL |
513 | |
514 | out = bio_open_owner(outfile, FORMAT_PKCS12, private); | |
515 | if (out == NULL) | |
516 | goto end; | |
517 | ||
0f113f3e MC |
518 | i2d_PKCS12_bio(out, p12); |
519 | ||
520 | ret = 0; | |
521 | ||
522 | export_end: | |
0f113f3e | 523 | |
c5ba2d99 | 524 | EVP_PKEY_free(key); |
222561fe RS |
525 | sk_X509_pop_free(certs, X509_free); |
526 | X509_free(ucert); | |
0f113f3e | 527 | |
0f113f3e | 528 | goto end; |
ee0508d4 | 529 | |
ee86c3f5 | 530 | } |
ee0508d4 | 531 | |
bdd58d98 RL |
532 | in = bio_open_default(infile, 'r', FORMAT_PKCS12); |
533 | if (in == NULL) | |
534 | goto end; | |
535 | out = bio_open_owner(outfile, FORMAT_PEM, private); | |
536 | if (out == NULL) | |
537 | goto end; | |
538 | ||
75ebbd9a | 539 | if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) { |
0f113f3e MC |
540 | ERR_print_errors(bio_err); |
541 | goto end; | |
ee0508d4 | 542 | } |
7e1b7485 | 543 | |
923b1857 RL |
544 | if (!noprompt) { |
545 | if (1) { | |
48feaceb | 546 | #ifndef OPENSSL_NO_UI_CONSOLE |
cbe29648 | 547 | if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:", |
923b1857 RL |
548 | 0)) { |
549 | BIO_printf(bio_err, "Can't read Password\n"); | |
550 | goto end; | |
551 | } | |
552 | } else { | |
553 | #endif | |
554 | BIO_printf(bio_err, "Password required\n"); | |
555 | goto end; | |
556 | } | |
ee0508d4 | 557 | } |
0f113f3e MC |
558 | |
559 | if (!twopass) | |
cbe29648 | 560 | OPENSSL_strlcpy(macpass, pass, sizeof(macpass)); |
0f113f3e | 561 | |
776cfa9b | 562 | if ((options & INFO) && PKCS12_mac_present(p12)) { |
59b4da05 DSH |
563 | const ASN1_INTEGER *tmaciter; |
564 | const X509_ALGOR *macalgid; | |
ac4e2577 | 565 | const ASN1_OBJECT *macobj; |
11a25d34 HK |
566 | const ASN1_OCTET_STRING *tmac; |
567 | const ASN1_OCTET_STRING *tsalt; | |
568 | ||
569 | PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12); | |
570 | /* current hash algorithms do not use parameters so extract just name, | |
571 | in future alg_print() may be needed */ | |
44c248b5 | 572 | X509_ALGOR_get0(&macobj, NULL, NULL, macalgid); |
7eb370ee | 573 | BIO_puts(bio_err, "MAC: "); |
44c248b5 | 574 | i2a_ASN1_OBJECT(bio_err, macobj); |
7eb370ee | 575 | BIO_printf(bio_err, ", Iteration %ld\n", |
11a25d34 HK |
576 | tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L); |
577 | BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n", | |
578 | tmac != NULL ? ASN1_STRING_length(tmac) : 0L, | |
579 | tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L); | |
776cfa9b | 580 | } |
0f113f3e | 581 | if (macver) { |
0f113f3e MC |
582 | /* If we enter empty password try no password first */ |
583 | if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) { | |
584 | /* If mac and crypto pass the same set it to NULL too */ | |
585 | if (!twopass) | |
586 | cpass = NULL; | |
587 | } else if (!PKCS12_verify_mac(p12, mpass, -1)) { | |
647ac8d3 DSH |
588 | /* |
589 | * May be UTF8 from previous version of OpenSSL: | |
590 | * convert to a UTF8 form which will translate | |
591 | * to the same Unicode password. | |
592 | */ | |
593 | unsigned char *utmp; | |
594 | int utmplen; | |
595 | utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen); | |
596 | if (utmp == NULL) | |
597 | goto end; | |
598 | badpass = OPENSSL_uni2utf8(utmp, utmplen); | |
599 | OPENSSL_free(utmp); | |
600 | if (!PKCS12_verify_mac(p12, badpass, -1)) { | |
601 | BIO_printf(bio_err, "Mac verify error: invalid password?\n"); | |
602 | ERR_print_errors(bio_err); | |
603 | goto end; | |
604 | } else { | |
605 | BIO_printf(bio_err, "Warning: using broken algorithm\n"); | |
606 | if (!twopass) | |
607 | cpass = badpass; | |
608 | } | |
0f113f3e | 609 | } |
ee0508d4 | 610 | } |
7e1b7485 | 611 | |
3b061a00 | 612 | assert(private); |
7e1b7485 | 613 | if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) { |
0f113f3e MC |
614 | BIO_printf(bio_err, "Error outputting keys and certificates\n"); |
615 | ERR_print_errors(bio_err); | |
616 | goto end; | |
ee0508d4 | 617 | } |
ee0508d4 | 618 | ret = 0; |
88364bc2 | 619 | end: |
e0e920b1 | 620 | PKCS12_free(p12); |
dd1abd44 | 621 | release_engine(e); |
a873356c | 622 | BIO_free(in); |
645749ef | 623 | BIO_free_all(out); |
25aaa98a | 624 | sk_OPENSSL_STRING_free(canames); |
647ac8d3 | 625 | OPENSSL_free(badpass); |
b548a1f1 RS |
626 | OPENSSL_free(passin); |
627 | OPENSSL_free(passout); | |
eee95522 | 628 | return ret; |
ee0508d4 DSH |
629 | } |
630 | ||
28da1455 | 631 | int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass, |
7e1b7485 RS |
632 | int passlen, int options, char *pempass, |
633 | const EVP_CIPHER *enc) | |
ee0508d4 | 634 | { |
0f113f3e MC |
635 | STACK_OF(PKCS7) *asafes = NULL; |
636 | STACK_OF(PKCS12_SAFEBAG) *bags; | |
637 | int i, bagnid; | |
638 | int ret = 0; | |
639 | PKCS7 *p7; | |
640 | ||
75ebbd9a | 641 | if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL) |
0f113f3e MC |
642 | return 0; |
643 | for (i = 0; i < sk_PKCS7_num(asafes); i++) { | |
644 | p7 = sk_PKCS7_value(asafes, i); | |
645 | bagnid = OBJ_obj2nid(p7->type); | |
646 | if (bagnid == NID_pkcs7_data) { | |
647 | bags = PKCS12_unpack_p7data(p7); | |
648 | if (options & INFO) | |
649 | BIO_printf(bio_err, "PKCS7 Data\n"); | |
650 | } else if (bagnid == NID_pkcs7_encrypted) { | |
651 | if (options & INFO) { | |
652 | BIO_printf(bio_err, "PKCS7 Encrypted data: "); | |
ecf3a1fb | 653 | alg_print(p7->d.encrypted->enc_data->algorithm); |
0f113f3e MC |
654 | } |
655 | bags = PKCS12_unpack_p7encdata(p7, pass, passlen); | |
2234212c | 656 | } else { |
0f113f3e | 657 | continue; |
2234212c | 658 | } |
0f113f3e MC |
659 | if (!bags) |
660 | goto err; | |
661 | if (!dump_certs_pkeys_bags(out, bags, pass, passlen, | |
7e1b7485 | 662 | options, pempass, enc)) { |
0f113f3e MC |
663 | sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); |
664 | goto err; | |
665 | } | |
666 | sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); | |
667 | bags = NULL; | |
668 | } | |
669 | ret = 1; | |
670 | ||
671 | err: | |
e0e920b1 | 672 | sk_PKCS7_pop_free(asafes, PKCS7_free); |
0f113f3e | 673 | return ret; |
ee0508d4 DSH |
674 | } |
675 | ||
28da1455 MC |
676 | int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags, |
677 | const char *pass, int passlen, int options, | |
678 | char *pempass, const EVP_CIPHER *enc) | |
ee0508d4 | 679 | { |
0f113f3e MC |
680 | int i; |
681 | for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) { | |
682 | if (!dump_certs_pkeys_bag(out, | |
683 | sk_PKCS12_SAFEBAG_value(bags, i), | |
7e1b7485 | 684 | pass, passlen, options, pempass, enc)) |
0f113f3e MC |
685 | return 0; |
686 | } | |
687 | return 1; | |
ee0508d4 DSH |
688 | } |
689 | ||
28da1455 MC |
690 | int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag, |
691 | const char *pass, int passlen, int options, | |
692 | char *pempass, const EVP_CIPHER *enc) | |
ee0508d4 | 693 | { |
0f113f3e MC |
694 | EVP_PKEY *pkey; |
695 | PKCS8_PRIV_KEY_INFO *p8; | |
28da1455 | 696 | const PKCS8_PRIV_KEY_INFO *p8c; |
0f113f3e | 697 | X509 *x509; |
28da1455 | 698 | const STACK_OF(X509_ATTRIBUTE) *attrs; |
c73aa309 | 699 | int ret = 0; |
776cfa9b DSH |
700 | |
701 | attrs = PKCS12_SAFEBAG_get0_attrs(bag); | |
0f113f3e | 702 | |
762ee38d | 703 | switch (PKCS12_SAFEBAG_get_nid(bag)) { |
0f113f3e MC |
704 | case NID_keyBag: |
705 | if (options & INFO) | |
706 | BIO_printf(bio_err, "Key bag\n"); | |
707 | if (options & NOKEYS) | |
708 | return 1; | |
776cfa9b | 709 | print_attribs(out, attrs, "Bag Attributes"); |
28da1455 MC |
710 | p8c = PKCS12_SAFEBAG_get0_p8inf(bag); |
711 | if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL) | |
0f113f3e | 712 | return 0; |
28da1455 | 713 | print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes"); |
c73aa309 | 714 | ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass); |
0f113f3e MC |
715 | EVP_PKEY_free(pkey); |
716 | break; | |
717 | ||
718 | case NID_pkcs8ShroudedKeyBag: | |
719 | if (options & INFO) { | |
59b4da05 DSH |
720 | const X509_SIG *tp8; |
721 | const X509_ALGOR *tp8alg; | |
c9018bdf | 722 | |
0f113f3e | 723 | BIO_printf(bio_err, "Shrouded Keybag: "); |
776cfa9b | 724 | tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag); |
59b4da05 | 725 | X509_SIG_get0(tp8, &tp8alg, NULL); |
a6eb1ce6 | 726 | alg_print(tp8alg); |
0f113f3e MC |
727 | } |
728 | if (options & NOKEYS) | |
729 | return 1; | |
776cfa9b | 730 | print_attribs(out, attrs, "Bag Attributes"); |
75ebbd9a | 731 | if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL) |
0f113f3e | 732 | return 0; |
75ebbd9a | 733 | if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) { |
0f113f3e MC |
734 | PKCS8_PRIV_KEY_INFO_free(p8); |
735 | return 0; | |
736 | } | |
54dbf423 | 737 | print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes"); |
0f113f3e | 738 | PKCS8_PRIV_KEY_INFO_free(p8); |
c73aa309 | 739 | ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass); |
0f113f3e MC |
740 | EVP_PKEY_free(pkey); |
741 | break; | |
742 | ||
743 | case NID_certBag: | |
744 | if (options & INFO) | |
745 | BIO_printf(bio_err, "Certificate bag\n"); | |
746 | if (options & NOCERTS) | |
747 | return 1; | |
762ee38d | 748 | if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) { |
0f113f3e MC |
749 | if (options & CACERTS) |
750 | return 1; | |
751 | } else if (options & CLCERTS) | |
752 | return 1; | |
776cfa9b | 753 | print_attribs(out, attrs, "Bag Attributes"); |
762ee38d | 754 | if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate) |
0f113f3e | 755 | return 1; |
762ee38d | 756 | if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL) |
0f113f3e MC |
757 | return 0; |
758 | dump_cert_text(out, x509); | |
c73aa309 | 759 | ret = PEM_write_bio_X509(out, x509); |
0f113f3e MC |
760 | X509_free(x509); |
761 | break; | |
762 | ||
763 | case NID_safeContentsBag: | |
764 | if (options & INFO) | |
765 | BIO_printf(bio_err, "Safe Contents bag\n"); | |
776cfa9b DSH |
766 | print_attribs(out, attrs, "Bag Attributes"); |
767 | return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag), | |
768 | pass, passlen, options, pempass, enc); | |
0f113f3e MC |
769 | |
770 | default: | |
771 | BIO_printf(bio_err, "Warning unsupported bag type: "); | |
776cfa9b | 772 | i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag)); |
0f113f3e MC |
773 | BIO_printf(bio_err, "\n"); |
774 | return 1; | |
0f113f3e | 775 | } |
c73aa309 | 776 | return ret; |
ee0508d4 DSH |
777 | } |
778 | ||
779 | /* Given a single certificate return a verified chain or NULL if error */ | |
780 | ||
e29c73c9 VD |
781 | static int get_cert_chain(X509 *cert, X509_STORE *store, |
782 | STACK_OF(X509) **chain) | |
ee0508d4 | 783 | { |
f0e0fd51 | 784 | X509_STORE_CTX *store_ctx = NULL; |
e29c73c9 | 785 | STACK_OF(X509) *chn = NULL; |
0f113f3e MC |
786 | int i = 0; |
787 | ||
f0e0fd51 RS |
788 | store_ctx = X509_STORE_CTX_new(); |
789 | if (store_ctx == NULL) { | |
790 | i = X509_V_ERR_UNSPECIFIED; | |
791 | goto end; | |
792 | } | |
793 | if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) { | |
794 | i = X509_V_ERR_UNSPECIFIED; | |
795 | goto end; | |
e29c73c9 VD |
796 | } |
797 | ||
f0e0fd51 RS |
798 | |
799 | if (X509_verify_cert(store_ctx) > 0) | |
800 | chn = X509_STORE_CTX_get1_chain(store_ctx); | |
801 | else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0) | |
e29c73c9 VD |
802 | i = X509_V_ERR_UNSPECIFIED; |
803 | ||
f0e0fd51 RS |
804 | end: |
805 | X509_STORE_CTX_free(store_ctx); | |
0f113f3e | 806 | *chain = chn; |
0f113f3e MC |
807 | return i; |
808 | } | |
809 | ||
59b4da05 | 810 | static int alg_print(const X509_ALGOR *alg) |
ee0508d4 | 811 | { |
ab6a591c | 812 | int pbenid, aparamtype; |
ac4e2577 DSH |
813 | const ASN1_OBJECT *aoid; |
814 | const void *aparam; | |
ab6a591c DSH |
815 | PBEPARAM *pbe = NULL; |
816 | ||
817 | X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg); | |
818 | ||
819 | pbenid = OBJ_obj2nid(aoid); | |
820 | ||
821 | BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid)); | |
822 | ||
823 | /* | |
824 | * If PBE algorithm is PBES2 decode algorithm parameters | |
825 | * for additional details. | |
826 | */ | |
827 | if (pbenid == NID_pbes2) { | |
828 | PBE2PARAM *pbe2 = NULL; | |
829 | int encnid; | |
830 | if (aparamtype == V_ASN1_SEQUENCE) | |
831 | pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM)); | |
832 | if (pbe2 == NULL) { | |
dc46fc25 | 833 | BIO_puts(bio_err, ", <unsupported parameters>"); |
ab6a591c DSH |
834 | goto done; |
835 | } | |
836 | X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc); | |
837 | pbenid = OBJ_obj2nid(aoid); | |
838 | X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption); | |
839 | encnid = OBJ_obj2nid(aoid); | |
840 | BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid), | |
841 | OBJ_nid2sn(encnid)); | |
842 | /* If KDF is PBKDF2 decode parameters */ | |
843 | if (pbenid == NID_id_pbkdf2) { | |
844 | PBKDF2PARAM *kdf = NULL; | |
845 | int prfnid; | |
846 | if (aparamtype == V_ASN1_SEQUENCE) | |
847 | kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM)); | |
848 | if (kdf == NULL) { | |
dc46fc25 | 849 | BIO_puts(bio_err, ", <unsupported parameters>"); |
ab6a591c DSH |
850 | goto done; |
851 | } | |
ecf3a1fb | 852 | |
ab6a591c DSH |
853 | if (kdf->prf == NULL) { |
854 | prfnid = NID_hmacWithSHA1; | |
855 | } else { | |
856 | X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf); | |
857 | prfnid = OBJ_obj2nid(aoid); | |
858 | } | |
859 | BIO_printf(bio_err, ", Iteration %ld, PRF %s", | |
860 | ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid)); | |
861 | PBKDF2PARAM_free(kdf); | |
402f26e6 | 862 | #ifndef OPENSSL_NO_SCRYPT |
75f163d3 HK |
863 | } else if (pbenid == NID_id_scrypt) { |
864 | SCRYPT_PARAMS *kdf = NULL; | |
865 | ||
866 | if (aparamtype == V_ASN1_SEQUENCE) | |
867 | kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS)); | |
868 | if (kdf == NULL) { | |
869 | BIO_puts(bio_err, ", <unsupported parameters>"); | |
870 | goto done; | |
871 | } | |
872 | BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, " | |
c2969ff6 | 873 | "Block size(r): %ld, Parallelism(p): %ld", |
75f163d3 HK |
874 | ASN1_STRING_length(kdf->salt), |
875 | ASN1_INTEGER_get(kdf->costParameter), | |
876 | ASN1_INTEGER_get(kdf->blockSize), | |
877 | ASN1_INTEGER_get(kdf->parallelizationParameter)); | |
878 | SCRYPT_PARAMS_free(kdf); | |
402f26e6 | 879 | #endif |
ab6a591c DSH |
880 | } |
881 | PBE2PARAM_free(pbe2); | |
882 | } else { | |
883 | if (aparamtype == V_ASN1_SEQUENCE) | |
884 | pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM)); | |
885 | if (pbe == NULL) { | |
dc46fc25 | 886 | BIO_puts(bio_err, ", <unsupported parameters>"); |
ab6a591c DSH |
887 | goto done; |
888 | } | |
889 | BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter)); | |
890 | PBEPARAM_free(pbe); | |
891 | } | |
892 | done: | |
893 | BIO_puts(bio_err, "\n"); | |
0f113f3e | 894 | return 1; |
ee0508d4 DSH |
895 | } |
896 | ||
897 | /* Load all certificates from a given file */ | |
898 | ||
84c15db5 | 899 | int cert_load(BIO *in, STACK_OF(X509) *sk) |
ee0508d4 | 900 | { |
d5e66eab | 901 | int ret = 0; |
0f113f3e | 902 | X509 *cert; |
d5e66eab | 903 | |
0f113f3e | 904 | while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
0f113f3e | 905 | ret = 1; |
d5e66eab SL |
906 | if (!sk_X509_push(sk, cert)) |
907 | return 0; | |
0f113f3e | 908 | } |
0f113f3e MC |
909 | if (ret) |
910 | ERR_clear_error(); | |
911 | return ret; | |
ee0508d4 DSH |
912 | } |
913 | ||
dbcc7b45 JS |
914 | /* Generalised x509 attribute value print */ |
915 | ||
916 | void print_attribute(BIO *out, const ASN1_TYPE *av) | |
917 | { | |
918 | char *value; | |
919 | ||
920 | switch (av->type) { | |
921 | case V_ASN1_BMPSTRING: | |
922 | value = OPENSSL_uni2asc(av->value.bmpstring->data, | |
923 | av->value.bmpstring->length); | |
924 | BIO_printf(out, "%s\n", value); | |
925 | OPENSSL_free(value); | |
926 | break; | |
927 | ||
928 | case V_ASN1_OCTET_STRING: | |
929 | hex_prin(out, av->value.octet_string->data, | |
930 | av->value.octet_string->length); | |
931 | BIO_printf(out, "\n"); | |
932 | break; | |
933 | ||
934 | case V_ASN1_BIT_STRING: | |
935 | hex_prin(out, av->value.bit_string->data, | |
936 | av->value.bit_string->length); | |
937 | BIO_printf(out, "\n"); | |
938 | break; | |
939 | ||
940 | default: | |
941 | BIO_printf(out, "<Unsupported tag %d>\n", av->type); | |
942 | break; | |
943 | } | |
944 | } | |
945 | ||
ee0508d4 DSH |
946 | /* Generalised attribute print: handle PKCS#8 and bag attributes */ |
947 | ||
b2e57e09 | 948 | int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst, |
0f113f3e | 949 | const char *name) |
ee0508d4 | 950 | { |
0f113f3e MC |
951 | X509_ATTRIBUTE *attr; |
952 | ASN1_TYPE *av; | |
dbcc7b45 | 953 | int i, j, attr_nid; |
0f113f3e MC |
954 | if (!attrlst) { |
955 | BIO_printf(out, "%s: <No Attributes>\n", name); | |
956 | return 1; | |
957 | } | |
958 | if (!sk_X509_ATTRIBUTE_num(attrlst)) { | |
959 | BIO_printf(out, "%s: <Empty Attributes>\n", name); | |
960 | return 1; | |
961 | } | |
962 | BIO_printf(out, "%s\n", name); | |
963 | for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) { | |
9b0a4531 | 964 | ASN1_OBJECT *attr_obj; |
0f113f3e | 965 | attr = sk_X509_ATTRIBUTE_value(attrlst, i); |
9b0a4531 DSH |
966 | attr_obj = X509_ATTRIBUTE_get0_object(attr); |
967 | attr_nid = OBJ_obj2nid(attr_obj); | |
0f113f3e MC |
968 | BIO_printf(out, " "); |
969 | if (attr_nid == NID_undef) { | |
9b0a4531 | 970 | i2a_ASN1_OBJECT(out, attr_obj); |
0f113f3e | 971 | BIO_printf(out, ": "); |
2234212c | 972 | } else { |
0f113f3e | 973 | BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid)); |
2234212c | 974 | } |
0f113f3e | 975 | |
9b0a4531 | 976 | if (X509_ATTRIBUTE_count(attr)) { |
dbcc7b45 JS |
977 | for (j = 0; j < X509_ATTRIBUTE_count(attr); j++) |
978 | { | |
979 | av = X509_ATTRIBUTE_get0_type(attr, j); | |
980 | print_attribute(out, av); | |
0f113f3e | 981 | } |
2234212c | 982 | } else { |
0f113f3e | 983 | BIO_printf(out, "<No Values>\n"); |
2234212c | 984 | } |
0f113f3e MC |
985 | } |
986 | return 1; | |
ee0508d4 DSH |
987 | } |
988 | ||
6b691a5c | 989 | void hex_prin(BIO *out, unsigned char *buf, int len) |
ee0508d4 | 990 | { |
0f113f3e MC |
991 | int i; |
992 | for (i = 0; i < len; i++) | |
993 | BIO_printf(out, "%02X ", buf[i]); | |
ee0508d4 | 994 | } |
a8515441 | 995 | |
7e1b7485 | 996 | static int set_pbe(int *ppbe, const char *str) |
0f113f3e MC |
997 | { |
998 | if (!str) | |
999 | return 0; | |
86885c28 | 1000 | if (strcmp(str, "NONE") == 0) { |
0f113f3e MC |
1001 | *ppbe = -1; |
1002 | return 1; | |
1003 | } | |
1004 | *ppbe = OBJ_txt2nid(str); | |
1005 | if (*ppbe == NID_undef) { | |
1006 | BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str); | |
1007 | return 0; | |
1008 | } | |
1009 | return 1; | |
1010 | } | |
1011 | ||
a8515441 | 1012 | #endif |