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