1 /*
2 * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
8 */
9
10 #include <openssl/opensslconf.h>
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "apps.h"
16 #include "progs.h"
17 #include <openssl/crypto.h>
18 #include <openssl/err.h>
19 #include <openssl/pem.h>
20 #include <openssl/pkcs12.h>
21 #include <openssl/provider.h>
22 #include <openssl/kdf.h>
23 #include <openssl/rand.h>
24
25 #define NOKEYS 0x1
26 #define NOCERTS 0x2
27 #define INFO 0x4
28 #define CLCERTS 0x8
29 #define CACERTS 0x10
30
31 #define PASSWD_BUF_SIZE 2048
32
33 #define WARN_EXPORT(opt) \
34 BIO_printf(bio_err, "Warning: -%s option ignored with -export\n", opt);
35 #define WARN_NO_EXPORT(opt) \
36 BIO_printf(bio_err, "Warning: -%s option ignored without -export\n", opt);
37
38 static int get_cert_chain(X509 *cert, X509_STORE *store,
39 STACK_OF(X509) *untrusted_certs,
40 STACK_OF(X509) **chain);
41 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
42 const char *pass, int passlen, int options,
43 char *pempass, const EVP_CIPHER *enc);
44 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
45 const char *pass, int passlen, int options,
46 char *pempass, const EVP_CIPHER *enc);
47 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
48 const char *pass, int passlen,
49 int options, char *pempass, const EVP_CIPHER *enc);
50 void print_attribute(BIO *out, const ASN1_TYPE *av);
51 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
52 const char *name);
53 void hex_prin(BIO *out, unsigned char *buf, int len);
54 static int alg_print(const X509_ALGOR *alg);
55 int cert_load(BIO *in, STACK_OF(X509) *sk);
56 static int set_pbe(int *ppbe, const char *str);
57
58 typedef enum OPTION_choice {
59 OPT_COMMON,
60 OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
61 OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
62 #ifndef OPENSSL_NO_DES
63 OPT_DESCERT,
64 #endif
65 OPT_EXPORT, OPT_ITER, OPT_NOITER, OPT_MACITER, OPT_NOMACITER, OPT_MACSALTLEN,
66 OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_NOENC, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
67 OPT_INKEY, OPT_CERTFILE, OPT_UNTRUSTED, OPT_PASSCERTS,
68 OPT_NAME, OPT_CSP, OPT_CANAME,
69 OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
70 OPT_CAFILE, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_ENGINE,
71 OPT_R_ENUM, OPT_PROV_ENUM,
72 #ifndef OPENSSL_NO_DES
73 OPT_LEGACY_ALG
74 #endif
75 } OPTION_CHOICE;
76
77 const OPTIONS pkcs12_options[] = {
78 OPT_SECTION("General"),
79 {"help", OPT_HELP, '-', "Display this summary"},
80 {"in", OPT_IN, '<', "Input file"},
81 {"out", OPT_OUT, '>', "Output file"},
82 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
83 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
84 {"password", OPT_PASSWORD, 's', "Set PKCS#12 import/export password source"},
85 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
86 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
87 {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
88 {"noout", OPT_NOOUT, '-', "Don't output anything, just verify PKCS#12 input"},
89 #ifndef OPENSSL_NO_DES
90 {"legacy", OPT_LEGACY_ALG, '-',
91 # ifdef OPENSSL_NO_RC2
92 "Use legacy encryption algorithm 3DES_CBC for keys and certs"
93 # else
94 "Use legacy encryption: 3DES_CBC for keys, RC2_CBC for certs"
95 # endif
96 },
97 #endif
98 #ifndef OPENSSL_NO_ENGINE
99 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
100 #endif
101 OPT_PROV_OPTIONS,
102 OPT_R_OPTIONS,
103
104 OPT_SECTION("PKCS#12 import (parsing PKCS#12)"),
105 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
106 {"nomacver", OPT_NOMACVER, '-', "Don't verify integrity MAC"},
107 {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
108 {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
109 {"", OPT_CIPHER, '-', "Any supported cipher for output encryption"},
110 {"noenc", OPT_NOENC, '-', "Don't encrypt private keys"},
111 {"nodes", OPT_NODES, '-', "Don't encrypt private keys; deprecated"},
112
113 OPT_SECTION("PKCS#12 output (export)"),
114 {"export", OPT_EXPORT, '-', "Create PKCS12 file"},
115 {"inkey", OPT_INKEY, 's', "Private key, else read from -in input file"},
116 {"certfile", OPT_CERTFILE, '<', "Extra certificates for PKCS12 output"},
117 {"passcerts", OPT_PASSCERTS, 's', "Certificate file pass phrase source"},
118 {"chain", OPT_CHAIN, '-', "Build and add certificate chain for EE cert,"},
119 {OPT_MORE_STR, 0, 0,
120 "which is the 1st cert from -in matching the private key (if given)"},
121 {"untrusted", OPT_UNTRUSTED, '<', "Untrusted certificates for chain building"},
122 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
123 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
124 {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
125 {"no-CAfile", OPT_NOCAFILE, '-',
126 "Do not load the default certificates file"},
127 {"no-CApath", OPT_NOCAPATH, '-',
128 "Do not load certificates from the default certificates directory"},
129 {"no-CAstore", OPT_NOCASTORE, '-',
130 "Do not load certificates from the default certificates store"},
131 {"name", OPT_NAME, 's', "Use name as friendly name"},
132 {"caname", OPT_CANAME, 's',
133 "Use name as CA friendly name (can be repeated)"},
134 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
135 {"LMK", OPT_LMK, '-',
136 "Add local machine keyset attribute to private key"},
137 {"keyex", OPT_KEYEX, '-', "Set key type to MS key exchange"},
138 {"keysig", OPT_KEYSIG, '-', "Set key type to MS key signature"},
139 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default AES-256 CBC)"},
140 {"certpbe", OPT_CERTPBE, 's',
141 "Certificate PBE algorithm (default PBES2 with PBKDF2 and AES-256 CBC)"},
142 #ifndef OPENSSL_NO_DES
143 {"descert", OPT_DESCERT, '-',
144 "Encrypt output with 3DES (default PBES2 with PBKDF2 and AES-256 CBC)"},
145 #endif
146 {"macalg", OPT_MACALG, 's',
147 "Digest algorithm to use in MAC (default SHA256)"},
148 {"iter", OPT_ITER, 'p', "Specify the iteration count for encryption and MAC"},
149 {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
150 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration)"},
151 {"maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility"},
152 {"macsaltlen", OPT_MACSALTLEN, '-', "Specify the salt len for MAC"},
153 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
154 {NULL}
155 };
156
pkcs12_main(int argc,char ** argv)157 int pkcs12_main(int argc, char **argv)
158 {
159 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
160 char *untrusted = NULL, *ciphername = NULL, *enc_name = NULL;
161 char *passcertsarg = NULL, *passcerts = NULL;
162 char *name = NULL, *csp_name = NULL;
163 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
164 int export_pkcs12 = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
165 #ifndef OPENSSL_NO_DES
166 int use_legacy = 0;
167 #endif
168 /* use library defaults for the iter, maciter, cert, and key PBE */
169 int iter = 0, maciter = 0;
170 int macsaltlen = PKCS12_SALT_LEN;
171 int cert_pbe = NID_undef;
172 int key_pbe = NID_undef;
173 int ret = 1, macver = 1, add_lmk = 0, private = 0;
174 int noprompt = 0;
175 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
176 char *passin = NULL, *passout = NULL, *macalg = NULL;
177 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
178 const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog;
179 int noCApath = 0, noCAfile = 0, noCAstore = 0;
180 ENGINE *e = NULL;
181 BIO *in = NULL, *out = NULL;
182 PKCS12 *p12 = NULL;
183 STACK_OF(OPENSSL_STRING) *canames = NULL;
184 EVP_CIPHER *default_enc = (EVP_CIPHER *)EVP_aes_256_cbc();
185 EVP_CIPHER *enc = (EVP_CIPHER *)default_enc;
186 OPTION_CHOICE o;
187
188 opt_set_unknown_name("cipher");
189 prog = opt_init(argc, argv, pkcs12_options);
190 while ((o = opt_next()) != OPT_EOF) {
191 switch (o) {
192 case OPT_EOF:
193 case OPT_ERR:
194 opthelp:
195 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
196 goto end;
197 case OPT_HELP:
198 opt_help(pkcs12_options);
199 ret = 0;
200 goto end;
201 case OPT_NOKEYS:
202 options |= NOKEYS;
203 break;
204 case OPT_KEYEX:
205 keytype = KEY_EX;
206 break;
207 case OPT_KEYSIG:
208 keytype = KEY_SIG;
209 break;
210 case OPT_NOCERTS:
211 options |= NOCERTS;
212 break;
213 case OPT_CLCERTS:
214 options |= CLCERTS;
215 break;
216 case OPT_CACERTS:
217 options |= CACERTS;
218 break;
219 case OPT_NOOUT:
220 options |= (NOKEYS | NOCERTS);
221 break;
222 case OPT_INFO:
223 options |= INFO;
224 break;
225 case OPT_CHAIN:
226 chain = 1;
227 break;
228 case OPT_TWOPASS:
229 twopass = 1;
230 break;
231 case OPT_NOMACVER:
232 macver = 0;
233 break;
234 #ifndef OPENSSL_NO_DES
235 case OPT_DESCERT:
236 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
237 break;
238 #endif
239 case OPT_EXPORT:
240 export_pkcs12 = 1;
241 break;
242 case OPT_NODES:
243 case OPT_NOENC:
244 /*
245 * |enc_name| stores the name of the option used so it
246 * can be printed if an error message is output.
247 */
248 enc_name = opt_flag() + 1;
249 enc = NULL;
250 ciphername = NULL;
251 break;
252 case OPT_CIPHER:
253 enc_name = ciphername = opt_unknown();
254 break;
255 case OPT_ITER:
256 maciter = iter = opt_int_arg();
257 break;
258 case OPT_NOITER:
259 iter = 1;
260 break;
261 case OPT_MACITER:
262 /* no-op */
263 break;
264 case OPT_NOMACITER:
265 maciter = 1;
266 break;
267 case OPT_MACSALTLEN:
268 macsaltlen = opt_int_arg();
269 break;
270 case OPT_NOMAC:
271 cert_pbe = -1;
272 maciter = -1;
273 break;
274 case OPT_MACALG:
275 macalg = opt_arg();
276 break;
277 case OPT_CERTPBE:
278 if (!set_pbe(&cert_pbe, opt_arg()))
279 goto opthelp;
280 break;
281 case OPT_KEYPBE:
282 if (!set_pbe(&key_pbe, opt_arg()))
283 goto opthelp;
284 break;
285 case OPT_R_CASES:
286 if (!opt_rand(o))
287 goto end;
288 break;
289 case OPT_INKEY:
290 keyname = opt_arg();
291 break;
292 case OPT_CERTFILE:
293 certfile = opt_arg();
294 break;
295 case OPT_UNTRUSTED:
296 untrusted = opt_arg();
297 break;
298 case OPT_PASSCERTS:
299 passcertsarg = opt_arg();
300 break;
301 case OPT_NAME:
302 name = opt_arg();
303 break;
304 case OPT_LMK:
305 add_lmk = 1;
306 break;
307 case OPT_CSP:
308 csp_name = opt_arg();
309 break;
310 case OPT_CANAME:
311 if (canames == NULL
312 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
313 goto end;
314 sk_OPENSSL_STRING_push(canames, opt_arg());
315 break;
316 case OPT_IN:
317 infile = opt_arg();
318 break;
319 case OPT_OUT:
320 outfile = opt_arg();
321 break;
322 case OPT_PASSIN:
323 passinarg = opt_arg();
324 break;
325 case OPT_PASSOUT:
326 passoutarg = opt_arg();
327 break;
328 case OPT_PASSWORD:
329 passarg = opt_arg();
330 break;
331 case OPT_CAPATH:
332 CApath = opt_arg();
333 break;
334 case OPT_CASTORE:
335 CAstore = opt_arg();
336 break;
337 case OPT_CAFILE:
338 CAfile = opt_arg();
339 break;
340 case OPT_NOCAPATH:
341 noCApath = 1;
342 break;
343 case OPT_NOCASTORE:
344 noCAstore = 1;
345 break;
346 case OPT_NOCAFILE:
347 noCAfile = 1;
348 break;
349 case OPT_ENGINE:
350 e = setup_engine(opt_arg(), 0);
351 break;
352 #ifndef OPENSSL_NO_DES
353 case OPT_LEGACY_ALG:
354 use_legacy = 1;
355 break;
356 #endif
357 case OPT_PROV_CASES:
358 if (!opt_provider(o))
359 goto end;
360 break;
361 }
362 }
363
364 /* No extra arguments. */
365 if (!opt_check_rest_arg(NULL))
366 goto opthelp;
367
368 if (!app_RAND_load())
369 goto end;
370
371 if (!opt_cipher_any(ciphername, &enc))
372 goto opthelp;
373 if (export_pkcs12) {
374 if ((options & INFO) != 0)
375 WARN_EXPORT("info");
376 if (macver == 0)
377 WARN_EXPORT("nomacver");
378 if ((options & CLCERTS) != 0)
379 WARN_EXPORT("clcerts");
380 if ((options & CACERTS) != 0)
381 WARN_EXPORT("cacerts");
382 if (enc != default_enc)
383 BIO_printf(bio_err,
384 "Warning: output encryption option -%s ignored with -export\n", enc_name);
385 } else {
386 if (keyname != NULL)
387 WARN_NO_EXPORT("inkey");
388 if (certfile != NULL)
389 WARN_NO_EXPORT("certfile");
390 if (passcertsarg != NULL)
391 WARN_NO_EXPORT("passcerts");
392 if (chain)
393 WARN_NO_EXPORT("chain");
394 if (untrusted != NULL)
395 WARN_NO_EXPORT("untrusted");
396 if (CAfile != NULL)
397 WARN_NO_EXPORT("CAfile");
398 if (CApath != NULL)
399 WARN_NO_EXPORT("CApath");
400 if (CAstore != NULL)
401 WARN_NO_EXPORT("CAstore");
402 if (noCAfile)
403 WARN_NO_EXPORT("no-CAfile");
404 if (noCApath)
405 WARN_NO_EXPORT("no-CApath");
406 if (noCAstore)
407 WARN_NO_EXPORT("no-CAstore");
408 if (name != NULL)
409 WARN_NO_EXPORT("name");
410 if (canames != NULL)
411 WARN_NO_EXPORT("caname");
412 if (csp_name != NULL)
413 WARN_NO_EXPORT("CSP");
414 if (add_lmk)
415 WARN_NO_EXPORT("LMK");
416 if (keytype == KEY_EX)
417 WARN_NO_EXPORT("keyex");
418 if (keytype == KEY_SIG)
419 WARN_NO_EXPORT("keysig");
420 if (key_pbe != NID_undef)
421 WARN_NO_EXPORT("keypbe");
422 if (cert_pbe != NID_undef && cert_pbe != -1)
423 WARN_NO_EXPORT("certpbe and -descert");
424 if (macalg != NULL)
425 WARN_NO_EXPORT("macalg");
426 if (iter != 0)
427 WARN_NO_EXPORT("iter and -noiter");
428 if (maciter == 1)
429 WARN_NO_EXPORT("nomaciter");
430 if (cert_pbe == -1 && maciter == -1)
431 WARN_NO_EXPORT("nomac");
432 if (macsaltlen != 0)
433 WARN_NO_EXPORT("macsaltlen");
434 }
435 #ifndef OPENSSL_NO_DES
436 if (use_legacy) {
437 /* load the legacy provider if not loaded already*/
438 if (!OSSL_PROVIDER_available(app_get0_libctx(), "legacy")) {
439 if (!app_provider_load(app_get0_libctx(), "legacy"))
440 goto end;
441 /* load the default provider explicitly */
442 if (!app_provider_load(app_get0_libctx(), "default"))
443 goto end;
444 }
445 if (cert_pbe == NID_undef) {
446 /* Adapt default algorithm */
447 # ifndef OPENSSL_NO_RC2
448 cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
449 # else
450 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
451 # endif
452 }
453
454 if (key_pbe == NID_undef)
455 key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
456 if (enc == default_enc)
457 enc = (EVP_CIPHER *)EVP_des_ede3_cbc();
458 if (macalg == NULL)
459 macalg = "sha1";
460 }
461 #endif
462
463 private = 1;
464
465 if (!app_passwd(passcertsarg, NULL, &passcerts, NULL)) {
466 BIO_printf(bio_err, "Error getting certificate file password\n");
467 goto end;
468 }
469
470 if (passarg != NULL) {
471 if (export_pkcs12)
472 passoutarg = passarg;
473 else
474 passinarg = passarg;
475 }
476
477 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
478 BIO_printf(bio_err, "Error getting passwords\n");
479 goto end;
480 }
481
482 if (cpass == NULL) {
483 if (export_pkcs12)
484 cpass = passout;
485 else
486 cpass = passin;
487 }
488
489 if (cpass != NULL) {
490 mpass = cpass;
491 noprompt = 1;
492 if (twopass) {
493 if (export_pkcs12)
494 BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
495 else
496 BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
497 goto end;
498 }
499 } else {
500 cpass = pass;
501 mpass = macpass;
502 }
503
504 if (twopass) {
505 /* To avoid bit rot */
506 if (1) {
507 #ifndef OPENSSL_NO_UI_CONSOLE
508 if (EVP_read_pw_string(
509 macpass, sizeof(macpass), "Enter MAC Password:", export_pkcs12)) {
510 BIO_printf(bio_err, "Can't read Password\n");
511 goto end;
512 }
513 } else {
514 #endif
515 BIO_printf(bio_err, "Unsupported option -twopass\n");
516 goto end;
517 }
518 }
519
520 if (export_pkcs12) {
521 EVP_PKEY *key = NULL;
522 X509 *ee_cert = NULL, *x = NULL;
523 STACK_OF(X509) *certs = NULL;
524 STACK_OF(X509) *untrusted_certs = NULL;
525 EVP_MD *macmd = NULL;
526 unsigned char *catmp = NULL;
527 int i;
528
529 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
530 BIO_printf(bio_err, "Nothing to export due to -noout or -nocerts and -nokeys\n");
531 goto export_end;
532 }
533
534 if ((options & NOCERTS) != 0) {
535 chain = 0;
536 BIO_printf(bio_err, "Warning: -chain option ignored with -nocerts\n");
537 }
538
539 if (!(options & NOKEYS)) {
540 key = load_key(keyname ? keyname : infile,
541 FORMAT_PEM, 1, passin, e,
542 keyname ?
543 "private key from -inkey file" :
544 "private key from -in file");
545 if (key == NULL)
546 goto export_end;
547 }
548
549 /* Load all certs in input file */
550 if (!(options & NOCERTS)) {
551 if (!load_certs(infile, 1, &certs, passin,
552 "certificates from -in file"))
553 goto export_end;
554 if (sk_X509_num(certs) < 1) {
555 BIO_printf(bio_err, "No certificate in -in file %s\n", infile);
556 goto export_end;
557 }
558
559 if (key != NULL) {
560 /* Look for matching private key */
561 for (i = 0; i < sk_X509_num(certs); i++) {
562 x = sk_X509_value(certs, i);
563 if (cert_matches_key(x, key)) {
564 ee_cert = x;
565 /* Zero keyid and alias */
566 X509_keyid_set1(ee_cert, NULL, 0);
567 X509_alias_set1(ee_cert, NULL, 0);
568 /* Remove from list */
569 (void)sk_X509_delete(certs, i);
570 break;
571 }
572 }
573 if (ee_cert == NULL) {
574 BIO_printf(bio_err,
575 "No cert in -in file '%s' matches private key\n",
576 infile);
577 goto export_end;
578 }
579 }
580 }
581
582 /* Load any untrusted certificates for chain building */
583 if (untrusted != NULL) {
584 if (!load_certs(untrusted, 0, &untrusted_certs, passcerts,
585 "untrusted certificates"))
586 goto export_end;
587 }
588
589 /* If chaining get chain from end entity cert */
590 if (chain) {
591 int vret;
592 STACK_OF(X509) *chain2;
593 X509_STORE *store;
594 X509 *ee_cert_tmp = ee_cert;
595
596 /* Assume the first cert if we haven't got anything else */
597 if (ee_cert_tmp == NULL && certs != NULL)
598 ee_cert_tmp = sk_X509_value(certs, 0);
599
600 if (ee_cert_tmp == NULL) {
601 BIO_printf(bio_err,
602 "No end entity certificate to check with -chain\n");
603 goto export_end;
604 }
605
606 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
607 CAstore, noCAstore))
608 == NULL)
609 goto export_end;
610
611 vret = get_cert_chain(ee_cert_tmp, store, untrusted_certs, &chain2);
612 X509_STORE_free(store);
613
614 if (vret == X509_V_OK) {
615 int add_certs;
616 /* Remove from chain2 the first (end entity) certificate */
617 X509_free(sk_X509_shift(chain2));
618 /* Add the remaining certs (except for duplicates) */
619 add_certs = X509_add_certs(certs, chain2, X509_ADD_FLAG_UP_REF
620 | X509_ADD_FLAG_NO_DUP);
621 OSSL_STACK_OF_X509_free(chain2);
622 if (!add_certs)
623 goto export_end;
624 } else {
625 if (vret != X509_V_ERR_UNSPECIFIED)
626 BIO_printf(bio_err, "Error getting chain: %s\n",
627 X509_verify_cert_error_string(vret));
628 goto export_end;
629 }
630 }
631
632 /* Add any extra certificates asked for */
633 if (certfile != NULL) {
634 if (!load_certs(certfile, 0, &certs, passcerts,
635 "extra certificates from -certfile"))
636 goto export_end;
637 }
638
639 /* Add any CA names */
640 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
641 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
642 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
643 }
644
645 if (csp_name != NULL && key != NULL)
646 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
647 MBSTRING_ASC, (unsigned char *)csp_name,
648 -1);
649
650 if (add_lmk && key != NULL)
651 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
652
653 if (!noprompt && !(enc == NULL && maciter == -1)) {
654 /* To avoid bit rot */
655 if (1) {
656 #ifndef OPENSSL_NO_UI_CONSOLE
657 if (EVP_read_pw_string(pass, sizeof(pass),
658 "Enter Export Password:", 1)) {
659 BIO_printf(bio_err, "Can't read Password\n");
660 goto export_end;
661 }
662 } else {
663 #endif
664 BIO_printf(bio_err, "Password required\n");
665 goto export_end;
666 }
667 }
668
669 if (!twopass)
670 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
671
672 p12 = PKCS12_create_ex(cpass, name, key, ee_cert, certs,
673 key_pbe, cert_pbe, iter, -1, keytype,
674 app_get0_libctx(), app_get0_propq());
675
676 if (p12 == NULL) {
677 BIO_printf(bio_err, "Error creating PKCS12 structure for %s\n",
678 outfile);
679 goto export_end;
680 }
681
682 if (macalg != NULL) {
683 if (!opt_md(macalg, &macmd))
684 goto opthelp;
685 }
686
687 if (maciter != -1) {
688 if (!PKCS12_set_mac(p12, mpass, -1, NULL, macsaltlen, maciter, macmd)) {
689 BIO_printf(bio_err, "Error creating PKCS12 MAC; no PKCS12KDF support?\n");
690 BIO_printf(bio_err, "Use -nomac if MAC not required and PKCS12KDF support not available.\n");
691 goto export_end;
692 }
693 }
694 assert(private);
695
696 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
697 if (out == NULL)
698 goto end;
699
700 i2d_PKCS12_bio(out, p12);
701
702 ret = 0;
703
704 export_end:
705
706 EVP_PKEY_free(key);
707 EVP_MD_free(macmd);
708 OSSL_STACK_OF_X509_free(certs);
709 OSSL_STACK_OF_X509_free(untrusted_certs);
710 X509_free(ee_cert);
711
712 ERR_print_errors(bio_err);
713 goto end;
714
715 }
716
717 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
718 if (in == NULL)
719 goto end;
720 out = bio_open_owner(outfile, FORMAT_PEM, private);
721 if (out == NULL)
722 goto end;
723
724 p12 = PKCS12_init_ex(NID_pkcs7_data, app_get0_libctx(), app_get0_propq());
725 if (p12 == NULL) {
726 ERR_print_errors(bio_err);
727 goto end;
728 }
729 if ((p12 = d2i_PKCS12_bio(in, &p12)) == NULL) {
730 ERR_print_errors(bio_err);
731 goto end;
732 }
733
734 if (!noprompt) {
735 if (1) {
736 #ifndef OPENSSL_NO_UI_CONSOLE
737 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
738 0)) {
739 BIO_printf(bio_err, "Can't read Password\n");
740 goto end;
741 }
742 } else {
743 #endif
744 BIO_printf(bio_err, "Password required\n");
745 goto end;
746 }
747 }
748
749 if (!twopass)
750 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
751
752 if ((options & INFO) && PKCS12_mac_present(p12)) {
753 const ASN1_INTEGER *tmaciter;
754 const X509_ALGOR *macalgid;
755 const ASN1_OBJECT *macobj;
756 const ASN1_OCTET_STRING *tmac;
757 const ASN1_OCTET_STRING *tsalt;
758
759 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
760 /* current hash algorithms do not use parameters so extract just name,
761 in future alg_print() may be needed */
762 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
763 BIO_puts(bio_err, "MAC: ");
764 i2a_ASN1_OBJECT(bio_err, macobj);
765 BIO_printf(bio_err, ", Iteration %ld\n",
766 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
767 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
768 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
769 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
770 }
771 if (macver) {
772 EVP_KDF *pkcs12kdf;
773
774 pkcs12kdf = EVP_KDF_fetch(app_get0_libctx(), "PKCS12KDF",
775 app_get0_propq());
776 if (pkcs12kdf == NULL) {
777 BIO_printf(bio_err, "Error verifying PKCS12 MAC; no PKCS12KDF support.\n");
778 BIO_printf(bio_err, "Use -nomacver if MAC verification is not required.\n");
779 goto end;
780 }
781 EVP_KDF_free(pkcs12kdf);
782 /* If we enter empty password try no password first */
783 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
784 /* If mac and crypto pass the same set it to NULL too */
785 if (!twopass)
786 cpass = NULL;
787 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
788 /*
789 * May be UTF8 from previous version of OpenSSL:
790 * convert to a UTF8 form which will translate
791 * to the same Unicode password.
792 */
793 unsigned char *utmp;
794 int utmplen;
795 unsigned long err = ERR_peek_error();
796
797 if (ERR_GET_LIB(err) == ERR_LIB_PKCS12
798 && ERR_GET_REASON(err) == PKCS12_R_MAC_ABSENT) {
799 BIO_printf(bio_err, "Warning: MAC is absent!\n");
800 goto dump;
801 }
802
803 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
804 if (utmp == NULL)
805 goto end;
806 badpass = OPENSSL_uni2utf8(utmp, utmplen);
807 OPENSSL_free(utmp);
808 if (!PKCS12_verify_mac(p12, badpass, -1)) {
809 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
810 ERR_print_errors(bio_err);
811 goto end;
812 } else {
813 BIO_printf(bio_err, "Warning: using broken algorithm\n");
814 if (!twopass)
815 cpass = badpass;
816 }
817 }
818 }
819
820 dump:
821 assert(private);
822 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
823 BIO_printf(bio_err, "Error outputting keys and certificates\n");
824 ERR_print_errors(bio_err);
825 goto end;
826 }
827 ret = 0;
828 end:
829 PKCS12_free(p12);
830 release_engine(e);
831 BIO_free(in);
832 BIO_free_all(out);
833 sk_OPENSSL_STRING_free(canames);
834 OPENSSL_free(badpass);
835 OPENSSL_free(passcerts);
836 OPENSSL_free(passin);
837 OPENSSL_free(passout);
838 return ret;
839 }
840
dump_certs_keys_p12(BIO * out,const PKCS12 * p12,const char * pass,int passlen,int options,char * pempass,const EVP_CIPHER * enc)841 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
842 int passlen, int options, char *pempass,
843 const EVP_CIPHER *enc)
844 {
845 STACK_OF(PKCS7) *asafes = NULL;
846 STACK_OF(PKCS12_SAFEBAG) *bags;
847 int i, bagnid;
848 int ret = 0;
849 PKCS7 *p7;
850
851 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
852 return 0;
853 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
854 p7 = sk_PKCS7_value(asafes, i);
855 bagnid = OBJ_obj2nid(p7->type);
856 if (bagnid == NID_pkcs7_data) {
857 bags = PKCS12_unpack_p7data(p7);
858 if (options & INFO)
859 BIO_printf(bio_err, "PKCS7 Data\n");
860 } else if (bagnid == NID_pkcs7_encrypted) {
861 if (options & INFO) {
862 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
863 alg_print(p7->d.encrypted->enc_data->algorithm);
864 }
865 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
866 } else {
867 continue;
868 }
869 if (!bags)
870 goto err;
871 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
872 options, pempass, enc)) {
873 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
874 goto err;
875 }
876 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
877 bags = NULL;
878 }
879 ret = 1;
880
881 err:
882 sk_PKCS7_pop_free(asafes, PKCS7_free);
883 return ret;
884 }
885
dump_certs_pkeys_bags(BIO * out,const STACK_OF (PKCS12_SAFEBAG)* bags,const char * pass,int passlen,int options,char * pempass,const EVP_CIPHER * enc)886 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
887 const char *pass, int passlen, int options,
888 char *pempass, const EVP_CIPHER *enc)
889 {
890 int i;
891 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
892 if (!dump_certs_pkeys_bag(out,
893 sk_PKCS12_SAFEBAG_value(bags, i),
894 pass, passlen, options, pempass, enc))
895 return 0;
896 }
897 return 1;
898 }
899
dump_certs_pkeys_bag(BIO * out,const PKCS12_SAFEBAG * bag,const char * pass,int passlen,int options,char * pempass,const EVP_CIPHER * enc)900 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
901 const char *pass, int passlen, int options,
902 char *pempass, const EVP_CIPHER *enc)
903 {
904 EVP_PKEY *pkey;
905 PKCS8_PRIV_KEY_INFO *p8;
906 const PKCS8_PRIV_KEY_INFO *p8c;
907 X509 *x509;
908 const STACK_OF(X509_ATTRIBUTE) *attrs;
909 int ret = 0;
910
911 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
912
913 switch (PKCS12_SAFEBAG_get_nid(bag)) {
914 case NID_keyBag:
915 if (options & INFO)
916 BIO_printf(bio_err, "Key bag\n");
917 if (options & NOKEYS)
918 return 1;
919 print_attribs(out, attrs, "Bag Attributes");
920 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
921 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
922 return 0;
923 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
924 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
925 EVP_PKEY_free(pkey);
926 break;
927
928 case NID_pkcs8ShroudedKeyBag:
929 if (options & INFO) {
930 const X509_SIG *tp8;
931 const X509_ALGOR *tp8alg;
932
933 BIO_printf(bio_err, "Shrouded Keybag: ");
934 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
935 X509_SIG_get0(tp8, &tp8alg, NULL);
936 alg_print(tp8alg);
937 }
938 if (options & NOKEYS)
939 return 1;
940 print_attribs(out, attrs, "Bag Attributes");
941 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
942 return 0;
943 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
944 PKCS8_PRIV_KEY_INFO_free(p8);
945 return 0;
946 }
947 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
948 PKCS8_PRIV_KEY_INFO_free(p8);
949 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
950 EVP_PKEY_free(pkey);
951 break;
952
953 case NID_certBag:
954 if (options & INFO)
955 BIO_printf(bio_err, "Certificate bag\n");
956 if (options & NOCERTS)
957 return 1;
958 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
959 if (options & CACERTS)
960 return 1;
961 } else if (options & CLCERTS)
962 return 1;
963 print_attribs(out, attrs, "Bag Attributes");
964 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
965 return 1;
966 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
967 return 0;
968 dump_cert_text(out, x509);
969 ret = PEM_write_bio_X509(out, x509);
970 X509_free(x509);
971 break;
972
973 case NID_secretBag:
974 if (options & INFO)
975 BIO_printf(bio_err, "Secret bag\n");
976 print_attribs(out, attrs, "Bag Attributes");
977 BIO_printf(bio_err, "Bag Type: ");
978 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
979 BIO_printf(bio_err, "\nBag Value: ");
980 print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
981 return 1;
982
983 case NID_safeContentsBag:
984 if (options & INFO)
985 BIO_printf(bio_err, "Safe Contents bag\n");
986 print_attribs(out, attrs, "Bag Attributes");
987 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
988 pass, passlen, options, pempass, enc);
989
990 default:
991 BIO_printf(bio_err, "Warning unsupported bag type: ");
992 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
993 BIO_printf(bio_err, "\n");
994 return 1;
995 }
996 return ret;
997 }
998
999 /* Given a single certificate return a verified chain or NULL if error */
1000
get_cert_chain(X509 * cert,X509_STORE * store,STACK_OF (X509)* untrusted_certs,STACK_OF (X509)** chain)1001 static int get_cert_chain(X509 *cert, X509_STORE *store,
1002 STACK_OF(X509) *untrusted_certs,
1003 STACK_OF(X509) **chain)
1004 {
1005 X509_STORE_CTX *store_ctx = NULL;
1006 STACK_OF(X509) *chn = NULL;
1007 int i = 0;
1008
1009 store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
1010 if (store_ctx == NULL) {
1011 i = X509_V_ERR_UNSPECIFIED;
1012 goto end;
1013 }
1014 if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
1015 i = X509_V_ERR_UNSPECIFIED;
1016 goto end;
1017 }
1018
1019
1020 if (X509_verify_cert(store_ctx) > 0)
1021 chn = X509_STORE_CTX_get1_chain(store_ctx);
1022 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
1023 i = X509_V_ERR_UNSPECIFIED;
1024
1025 end:
1026 X509_STORE_CTX_free(store_ctx);
1027 *chain = chn;
1028 return i;
1029 }
1030
alg_print(const X509_ALGOR * alg)1031 static int alg_print(const X509_ALGOR *alg)
1032 {
1033 int pbenid, aparamtype;
1034 const ASN1_OBJECT *aoid;
1035 const void *aparam;
1036 PBEPARAM *pbe = NULL;
1037
1038 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
1039
1040 pbenid = OBJ_obj2nid(aoid);
1041
1042 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
1043
1044 /*
1045 * If PBE algorithm is PBES2 decode algorithm parameters
1046 * for additional details.
1047 */
1048 if (pbenid == NID_pbes2) {
1049 PBE2PARAM *pbe2 = NULL;
1050 int encnid;
1051 if (aparamtype == V_ASN1_SEQUENCE)
1052 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
1053 if (pbe2 == NULL) {
1054 BIO_puts(bio_err, ", <unsupported parameters>");
1055 goto done;
1056 }
1057 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
1058 pbenid = OBJ_obj2nid(aoid);
1059 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
1060 encnid = OBJ_obj2nid(aoid);
1061 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
1062 OBJ_nid2sn(encnid));
1063 /* If KDF is PBKDF2 decode parameters */
1064 if (pbenid == NID_id_pbkdf2) {
1065 PBKDF2PARAM *kdf = NULL;
1066 int prfnid;
1067 if (aparamtype == V_ASN1_SEQUENCE)
1068 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
1069 if (kdf == NULL) {
1070 BIO_puts(bio_err, ", <unsupported parameters>");
1071 goto done;
1072 }
1073
1074 if (kdf->prf == NULL) {
1075 prfnid = NID_hmacWithSHA1;
1076 } else {
1077 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
1078 prfnid = OBJ_obj2nid(aoid);
1079 }
1080 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
1081 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
1082 PBKDF2PARAM_free(kdf);
1083 #ifndef OPENSSL_NO_SCRYPT
1084 } else if (pbenid == NID_id_scrypt) {
1085 SCRYPT_PARAMS *kdf = NULL;
1086
1087 if (aparamtype == V_ASN1_SEQUENCE)
1088 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
1089 if (kdf == NULL) {
1090 BIO_puts(bio_err, ", <unsupported parameters>");
1091 goto done;
1092 }
1093 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
1094 "Block size(r): %ld, Parallelism(p): %ld",
1095 ASN1_STRING_length(kdf->salt),
1096 ASN1_INTEGER_get(kdf->costParameter),
1097 ASN1_INTEGER_get(kdf->blockSize),
1098 ASN1_INTEGER_get(kdf->parallelizationParameter));
1099 SCRYPT_PARAMS_free(kdf);
1100 #endif
1101 }
1102 PBE2PARAM_free(pbe2);
1103 } else {
1104 if (aparamtype == V_ASN1_SEQUENCE)
1105 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
1106 if (pbe == NULL) {
1107 BIO_puts(bio_err, ", <unsupported parameters>");
1108 goto done;
1109 }
1110 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
1111 PBEPARAM_free(pbe);
1112 }
1113 done:
1114 BIO_puts(bio_err, "\n");
1115 return 1;
1116 }
1117
1118 /* Load all certificates from a given file */
1119
cert_load(BIO * in,STACK_OF (X509)* sk)1120 int cert_load(BIO *in, STACK_OF(X509) *sk)
1121 {
1122 int ret = 0;
1123 X509 *cert;
1124
1125 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
1126 ret = 1;
1127 if (!sk_X509_push(sk, cert))
1128 return 0;
1129 }
1130 if (ret)
1131 ERR_clear_error();
1132 return ret;
1133 }
1134
1135 /* Generalised x509 attribute value print */
1136
print_attribute(BIO * out,const ASN1_TYPE * av)1137 void print_attribute(BIO *out, const ASN1_TYPE *av)
1138 {
1139 char *value;
1140
1141 switch (av->type) {
1142 case V_ASN1_BMPSTRING:
1143 value = OPENSSL_uni2asc(av->value.bmpstring->data,
1144 av->value.bmpstring->length);
1145 BIO_printf(out, "%s\n", value);
1146 OPENSSL_free(value);
1147 break;
1148
1149 case V_ASN1_UTF8STRING:
1150 BIO_printf(out, "%.*s\n", av->value.utf8string->length,
1151 av->value.utf8string->data);
1152 break;
1153
1154 case V_ASN1_OCTET_STRING:
1155 hex_prin(out, av->value.octet_string->data,
1156 av->value.octet_string->length);
1157 BIO_printf(out, "\n");
1158 break;
1159
1160 case V_ASN1_BIT_STRING:
1161 hex_prin(out, av->value.bit_string->data,
1162 av->value.bit_string->length);
1163 BIO_printf(out, "\n");
1164 break;
1165
1166 default:
1167 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
1168 break;
1169 }
1170 }
1171
1172 /* Generalised attribute print: handle PKCS#8 and bag attributes */
1173
print_attribs(BIO * out,const STACK_OF (X509_ATTRIBUTE)* attrlst,const char * name)1174 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
1175 const char *name)
1176 {
1177 X509_ATTRIBUTE *attr;
1178 ASN1_TYPE *av;
1179 int i, j, attr_nid;
1180 if (!attrlst) {
1181 BIO_printf(out, "%s: <No Attributes>\n", name);
1182 return 1;
1183 }
1184 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
1185 BIO_printf(out, "%s: <Empty Attributes>\n", name);
1186 return 1;
1187 }
1188 BIO_printf(out, "%s\n", name);
1189 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
1190 ASN1_OBJECT *attr_obj;
1191 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
1192 attr_obj = X509_ATTRIBUTE_get0_object(attr);
1193 attr_nid = OBJ_obj2nid(attr_obj);
1194 BIO_printf(out, " ");
1195 if (attr_nid == NID_undef) {
1196 i2a_ASN1_OBJECT(out, attr_obj);
1197 BIO_printf(out, ": ");
1198 } else {
1199 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1200 }
1201
1202 if (X509_ATTRIBUTE_count(attr)) {
1203 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
1204 {
1205 av = X509_ATTRIBUTE_get0_type(attr, j);
1206 print_attribute(out, av);
1207 }
1208 } else {
1209 BIO_printf(out, "<No Values>\n");
1210 }
1211 }
1212 return 1;
1213 }
1214
hex_prin(BIO * out,unsigned char * buf,int len)1215 void hex_prin(BIO *out, unsigned char *buf, int len)
1216 {
1217 int i;
1218 for (i = 0; i < len; i++)
1219 BIO_printf(out, "%02X ", buf[i]);
1220 }
1221
set_pbe(int * ppbe,const char * str)1222 static int set_pbe(int *ppbe, const char *str)
1223 {
1224 if (!str)
1225 return 0;
1226 if (strcmp(str, "NONE") == 0) {
1227 *ppbe = -1;
1228 return 1;
1229 }
1230 *ppbe = OBJ_txt2nid(str);
1231 if (*ppbe == NID_undef) {
1232 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
1233 return 0;
1234 }
1235 return 1;
1236 }
1237