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