2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
17 #include <openssl/crypto.h>
18 #include <openssl/err.h>
19 #include <openssl/pem.h>
20 #include <openssl/pkcs12.h>
23 DEFINE_STACK_OF(PKCS7)
24 DEFINE_STACK_OF(PKCS12_SAFEBAG)
25 DEFINE_STACK_OF(X509_ATTRIBUTE)
26 DEFINE_STACK_OF_STRING()
34 #define PASSWD_BUF_SIZE 2048
36 static int get_cert_chain(X509 *cert, X509_STORE *store,
37 STACK_OF(X509) **chain);
38 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
39 const char *pass, int passlen, int options,
40 char *pempass, const EVP_CIPHER *enc);
41 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
42 const char *pass, int passlen, int options,
43 char *pempass, const EVP_CIPHER *enc);
44 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
45 const char *pass, int passlen,
46 int options, char *pempass, const EVP_CIPHER *enc);
47 void print_attribute(BIO *out, const ASN1_TYPE *av);
48 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
50 void hex_prin(BIO *out, unsigned char *buf, int len);
51 static int alg_print(const X509_ALGOR *alg);
52 int cert_load(BIO *in, STACK_OF(X509) *sk);
53 static int set_pbe(int *ppbe, const char *str);
55 typedef enum OPTION_choice {
56 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
57 OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
58 OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
59 OPT_DESCERT, OPT_EXPORT, OPT_ITER, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
60 OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_NOENC, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
61 OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME,
62 OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
63 OPT_CAFILE, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_ENGINE,
64 OPT_R_ENUM, OPT_PROV_ENUM
67 const OPTIONS pkcs12_options[] = {
68 OPT_SECTION("General"),
69 {"help", OPT_HELP, '-', "Display this summary"},
70 #ifndef OPENSSL_NO_ENGINE
71 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
75 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
76 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
77 {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
78 {"no-CAfile", OPT_NOCAFILE, '-',
79 "Do not load the default certificates file"},
80 {"no-CApath", OPT_NOCAPATH, '-',
81 "Do not load certificates from the default certificates directory"},
82 {"no-CAstore", OPT_NOCASTORE, '-',
83 "Do not load certificates from the default certificates store"},
86 {"inkey", OPT_INKEY, 's', "Private key if not infile"},
87 {"certfile", OPT_CERTFILE, '<', "Load certs from file"},
88 {"name", OPT_NAME, 's', "Use name as friendly name"},
89 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
90 {"caname", OPT_CANAME, 's',
91 "Use name as CA friendly name (can be repeated)"},
92 {"in", OPT_IN, '<', "Input filename"},
93 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
95 OPT_SECTION("Output"),
96 {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
98 "Add local machine keyset attribute to private key"},
99 {"macalg", OPT_MACALG, 's',
100 "Digest algorithm used in MAC (default SHA1)"},
101 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
102 {"out", OPT_OUT, '>', "Output filename"},
103 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
104 {"password", OPT_PASSWORD, 's', "Set import/export password source"},
105 {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
106 {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
107 {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
108 {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"},
109 {"chain", OPT_CHAIN, '-', "Add certificate chain"},
110 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
111 {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
112 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
113 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
114 {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"},
115 {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"},
117 OPT_SECTION("Encryption"),
118 #ifndef OPENSSL_NO_RC2
119 {"descert", OPT_DESCERT, '-',
120 "Encrypt output with 3DES (default RC2-40)"},
121 {"certpbe", OPT_CERTPBE, 's',
122 "Certificate PBE algorithm (default RC2-40)"},
124 {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
125 {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
127 {"iter", OPT_ITER, 'p', "Specify the iteration count for encryption key and MAC"},
128 {"noiter", OPT_NOITER, '-', "Don't use encryption key iteration"},
129 {"maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility"},
130 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
131 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
132 {"noenc", OPT_NOENC, '-', "Don't encrypt private keys"},
133 {"nodes", OPT_NODES, '-', "Don't encrypt private keys; deprecated"},
134 {"", OPT_CIPHER, '-', "Any supported cipher"},
141 int pkcs12_main(int argc, char **argv)
143 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
144 char *name = NULL, *csp_name = NULL;
145 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
146 int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
147 int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
148 #ifndef OPENSSL_NO_RC2
149 int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
151 int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
153 int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
154 int ret = 1, macver = 1, add_lmk = 0, private = 0;
156 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
157 char *passin = NULL, *passout = NULL, *macalg = NULL;
158 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
159 const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog;
160 int noCApath = 0, noCAfile = 0, noCAstore = 0;
162 BIO *in = NULL, *out = NULL;
164 STACK_OF(OPENSSL_STRING) *canames = NULL;
165 const EVP_CIPHER *enc = EVP_des_ede3_cbc();
168 prog = opt_init(argc, argv, pkcs12_options);
169 while ((o = opt_next()) != OPT_EOF) {
174 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
177 opt_help(pkcs12_options);
199 options |= (NOKEYS | NOCERTS);
214 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
220 if (!opt_cipher(opt_unknown(), &enc))
224 if (!opt_int(opt_arg(), &iter))
248 if (!set_pbe(&cert_pbe, opt_arg()))
252 if (!set_pbe(&key_pbe, opt_arg()))
263 certfile = opt_arg();
272 csp_name = opt_arg();
276 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
278 sk_OPENSSL_STRING_push(canames, opt_arg());
287 passinarg = opt_arg();
290 passoutarg = opt_arg();
314 e = setup_engine(opt_arg(), 0);
317 if (!opt_provider(o))
322 argc = opt_num_rest();
328 if (passarg != NULL) {
330 passoutarg = passarg;
335 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
336 BIO_printf(bio_err, "Error getting passwords\n");
352 BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
354 BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
363 /* To avoid bit rot */
365 #ifndef OPENSSL_NO_UI_CONSOLE
366 if (EVP_read_pw_string(
367 macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) {
368 BIO_printf(bio_err, "Can't read Password\n");
373 BIO_printf(bio_err, "Unsupported option -twopass\n");
379 EVP_PKEY *key = NULL;
380 X509 *ucert = NULL, *x = NULL;
381 STACK_OF(X509) *certs = NULL;
382 const EVP_MD *macmd = NULL;
383 unsigned char *catmp = NULL;
386 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
387 BIO_printf(bio_err, "Nothing to do!\n");
391 if (options & NOCERTS)
394 if (!(options & NOKEYS)) {
395 key = load_key(keyname ? keyname : infile,
396 FORMAT_PEM, 1, passin, e, "private key");
401 /* Load in all certs in input file */
402 if (!(options & NOCERTS)) {
403 if (!load_certs(infile, &certs, FORMAT_PEM, NULL,
408 /* Look for matching private key */
409 for (i = 0; i < sk_X509_num(certs); i++) {
410 x = sk_X509_value(certs, i);
411 if (X509_check_private_key(x, key)) {
413 /* Zero keyid and alias */
414 X509_keyid_set1(ucert, NULL, 0);
415 X509_alias_set1(ucert, NULL, 0);
416 /* Remove from list */
417 (void)sk_X509_delete(certs, i);
423 "No certificate matches private key\n");
430 /* Add any more certificates asked for */
431 if (certfile != NULL) {
432 if (!load_certs(certfile, &certs, FORMAT_PEM, NULL,
433 "certificates from certfile"))
437 /* If chaining get chain from user cert */
440 STACK_OF(X509) *chain2;
442 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
447 vret = get_cert_chain(ucert, store, &chain2);
448 X509_STORE_free(store);
450 if (vret == X509_V_OK) {
451 /* Exclude verified certificate */
452 for (i = 1; i < sk_X509_num(chain2); i++)
453 sk_X509_push(certs, sk_X509_value(chain2, i));
454 /* Free first certificate */
455 X509_free(sk_X509_value(chain2, 0));
456 sk_X509_free(chain2);
458 if (vret != X509_V_ERR_UNSPECIFIED)
459 BIO_printf(bio_err, "Error %s getting chain.\n",
460 X509_verify_cert_error_string(vret));
462 ERR_print_errors(bio_err);
467 /* Add any CA names */
469 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
470 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
471 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
474 if (csp_name != NULL && key != NULL)
475 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
476 MBSTRING_ASC, (unsigned char *)csp_name,
479 if (add_lmk && key != NULL)
480 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
483 /* To avoid bit rot */
485 #ifndef OPENSSL_NO_UI_CONSOLE
486 if (EVP_read_pw_string(pass, sizeof(pass),
487 "Enter Export Password:", 1)) {
488 BIO_printf(bio_err, "Can't read Password\n");
493 BIO_printf(bio_err, "Password required\n");
499 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
501 p12 = PKCS12_create(cpass, name, key, ucert, certs,
502 key_pbe, cert_pbe, iter, -1, keytype);
505 ERR_print_errors(bio_err);
510 if (!opt_md(macalg, &macmd))
515 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
519 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
523 i2d_PKCS12_bio(out, p12);
530 sk_X509_pop_free(certs, X509_free);
537 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
540 out = bio_open_owner(outfile, FORMAT_PEM, private);
544 if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
545 ERR_print_errors(bio_err);
551 #ifndef OPENSSL_NO_UI_CONSOLE
552 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
554 BIO_printf(bio_err, "Can't read Password\n");
559 BIO_printf(bio_err, "Password required\n");
565 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
567 if ((options & INFO) && PKCS12_mac_present(p12)) {
568 const ASN1_INTEGER *tmaciter;
569 const X509_ALGOR *macalgid;
570 const ASN1_OBJECT *macobj;
571 const ASN1_OCTET_STRING *tmac;
572 const ASN1_OCTET_STRING *tsalt;
574 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
575 /* current hash algorithms do not use parameters so extract just name,
576 in future alg_print() may be needed */
577 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
578 BIO_puts(bio_err, "MAC: ");
579 i2a_ASN1_OBJECT(bio_err, macobj);
580 BIO_printf(bio_err, ", Iteration %ld\n",
581 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
582 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
583 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
584 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
587 /* If we enter empty password try no password first */
588 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
589 /* If mac and crypto pass the same set it to NULL too */
592 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
594 * May be UTF8 from previous version of OpenSSL:
595 * convert to a UTF8 form which will translate
596 * to the same Unicode password.
600 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
603 badpass = OPENSSL_uni2utf8(utmp, utmplen);
605 if (!PKCS12_verify_mac(p12, badpass, -1)) {
606 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
607 ERR_print_errors(bio_err);
610 BIO_printf(bio_err, "Warning: using broken algorithm\n");
618 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
619 BIO_printf(bio_err, "Error outputting keys and certificates\n");
620 ERR_print_errors(bio_err);
629 sk_OPENSSL_STRING_free(canames);
630 OPENSSL_free(badpass);
631 OPENSSL_free(passin);
632 OPENSSL_free(passout);
636 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
637 int passlen, int options, char *pempass,
638 const EVP_CIPHER *enc)
640 STACK_OF(PKCS7) *asafes = NULL;
641 STACK_OF(PKCS12_SAFEBAG) *bags;
646 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
648 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
649 p7 = sk_PKCS7_value(asafes, i);
650 bagnid = OBJ_obj2nid(p7->type);
651 if (bagnid == NID_pkcs7_data) {
652 bags = PKCS12_unpack_p7data(p7);
654 BIO_printf(bio_err, "PKCS7 Data\n");
655 } else if (bagnid == NID_pkcs7_encrypted) {
656 if (options & INFO) {
657 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
658 alg_print(p7->d.encrypted->enc_data->algorithm);
660 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
666 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
667 options, pempass, enc)) {
668 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
671 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
677 sk_PKCS7_pop_free(asafes, PKCS7_free);
681 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
682 const char *pass, int passlen, int options,
683 char *pempass, const EVP_CIPHER *enc)
686 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
687 if (!dump_certs_pkeys_bag(out,
688 sk_PKCS12_SAFEBAG_value(bags, i),
689 pass, passlen, options, pempass, enc))
695 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
696 const char *pass, int passlen, int options,
697 char *pempass, const EVP_CIPHER *enc)
700 PKCS8_PRIV_KEY_INFO *p8;
701 const PKCS8_PRIV_KEY_INFO *p8c;
703 const STACK_OF(X509_ATTRIBUTE) *attrs;
706 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
708 switch (PKCS12_SAFEBAG_get_nid(bag)) {
711 BIO_printf(bio_err, "Key bag\n");
712 if (options & NOKEYS)
714 print_attribs(out, attrs, "Bag Attributes");
715 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
716 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
718 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
719 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
723 case NID_pkcs8ShroudedKeyBag:
724 if (options & INFO) {
726 const X509_ALGOR *tp8alg;
728 BIO_printf(bio_err, "Shrouded Keybag: ");
729 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
730 X509_SIG_get0(tp8, &tp8alg, NULL);
733 if (options & NOKEYS)
735 print_attribs(out, attrs, "Bag Attributes");
736 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
738 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
739 PKCS8_PRIV_KEY_INFO_free(p8);
742 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
743 PKCS8_PRIV_KEY_INFO_free(p8);
744 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
750 BIO_printf(bio_err, "Certificate bag\n");
751 if (options & NOCERTS)
753 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
754 if (options & CACERTS)
756 } else if (options & CLCERTS)
758 print_attribs(out, attrs, "Bag Attributes");
759 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
761 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
763 dump_cert_text(out, x509);
764 ret = PEM_write_bio_X509(out, x509);
768 case NID_safeContentsBag:
770 BIO_printf(bio_err, "Safe Contents bag\n");
771 print_attribs(out, attrs, "Bag Attributes");
772 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
773 pass, passlen, options, pempass, enc);
776 BIO_printf(bio_err, "Warning unsupported bag type: ");
777 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
778 BIO_printf(bio_err, "\n");
784 /* Given a single certificate return a verified chain or NULL if error */
786 static int get_cert_chain(X509 *cert, X509_STORE *store,
787 STACK_OF(X509) **chain)
789 X509_STORE_CTX *store_ctx = NULL;
790 STACK_OF(X509) *chn = NULL;
793 store_ctx = X509_STORE_CTX_new();
794 if (store_ctx == NULL) {
795 i = X509_V_ERR_UNSPECIFIED;
798 if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {
799 i = X509_V_ERR_UNSPECIFIED;
804 if (X509_verify_cert(store_ctx) > 0)
805 chn = X509_STORE_CTX_get1_chain(store_ctx);
806 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
807 i = X509_V_ERR_UNSPECIFIED;
810 X509_STORE_CTX_free(store_ctx);
815 static int alg_print(const X509_ALGOR *alg)
817 int pbenid, aparamtype;
818 const ASN1_OBJECT *aoid;
820 PBEPARAM *pbe = NULL;
822 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
824 pbenid = OBJ_obj2nid(aoid);
826 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
829 * If PBE algorithm is PBES2 decode algorithm parameters
830 * for additional details.
832 if (pbenid == NID_pbes2) {
833 PBE2PARAM *pbe2 = NULL;
835 if (aparamtype == V_ASN1_SEQUENCE)
836 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
838 BIO_puts(bio_err, ", <unsupported parameters>");
841 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
842 pbenid = OBJ_obj2nid(aoid);
843 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
844 encnid = OBJ_obj2nid(aoid);
845 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
847 /* If KDF is PBKDF2 decode parameters */
848 if (pbenid == NID_id_pbkdf2) {
849 PBKDF2PARAM *kdf = NULL;
851 if (aparamtype == V_ASN1_SEQUENCE)
852 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
854 BIO_puts(bio_err, ", <unsupported parameters>");
858 if (kdf->prf == NULL) {
859 prfnid = NID_hmacWithSHA1;
861 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
862 prfnid = OBJ_obj2nid(aoid);
864 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
865 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
866 PBKDF2PARAM_free(kdf);
867 #ifndef OPENSSL_NO_SCRYPT
868 } else if (pbenid == NID_id_scrypt) {
869 SCRYPT_PARAMS *kdf = NULL;
871 if (aparamtype == V_ASN1_SEQUENCE)
872 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
874 BIO_puts(bio_err, ", <unsupported parameters>");
877 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
878 "Block size(r): %ld, Parallelism(p): %ld",
879 ASN1_STRING_length(kdf->salt),
880 ASN1_INTEGER_get(kdf->costParameter),
881 ASN1_INTEGER_get(kdf->blockSize),
882 ASN1_INTEGER_get(kdf->parallelizationParameter));
883 SCRYPT_PARAMS_free(kdf);
886 PBE2PARAM_free(pbe2);
888 if (aparamtype == V_ASN1_SEQUENCE)
889 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
891 BIO_puts(bio_err, ", <unsupported parameters>");
894 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
898 BIO_puts(bio_err, "\n");
902 /* Load all certificates from a given file */
904 int cert_load(BIO *in, STACK_OF(X509) *sk)
909 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
911 if (!sk_X509_push(sk, cert))
919 /* Generalised x509 attribute value print */
921 void print_attribute(BIO *out, const ASN1_TYPE *av)
926 case V_ASN1_BMPSTRING:
927 value = OPENSSL_uni2asc(av->value.bmpstring->data,
928 av->value.bmpstring->length);
929 BIO_printf(out, "%s\n", value);
933 case V_ASN1_OCTET_STRING:
934 hex_prin(out, av->value.octet_string->data,
935 av->value.octet_string->length);
936 BIO_printf(out, "\n");
939 case V_ASN1_BIT_STRING:
940 hex_prin(out, av->value.bit_string->data,
941 av->value.bit_string->length);
942 BIO_printf(out, "\n");
946 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
951 /* Generalised attribute print: handle PKCS#8 and bag attributes */
953 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
956 X509_ATTRIBUTE *attr;
960 BIO_printf(out, "%s: <No Attributes>\n", name);
963 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
964 BIO_printf(out, "%s: <Empty Attributes>\n", name);
967 BIO_printf(out, "%s\n", name);
968 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
969 ASN1_OBJECT *attr_obj;
970 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
971 attr_obj = X509_ATTRIBUTE_get0_object(attr);
972 attr_nid = OBJ_obj2nid(attr_obj);
973 BIO_printf(out, " ");
974 if (attr_nid == NID_undef) {
975 i2a_ASN1_OBJECT(out, attr_obj);
976 BIO_printf(out, ": ");
978 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
981 if (X509_ATTRIBUTE_count(attr)) {
982 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
984 av = X509_ATTRIBUTE_get0_type(attr, j);
985 print_attribute(out, av);
988 BIO_printf(out, "<No Values>\n");
994 void hex_prin(BIO *out, unsigned char *buf, int len)
997 for (i = 0; i < len; i++)
998 BIO_printf(out, "%02X ", buf[i]);
1001 static int set_pbe(int *ppbe, const char *str)
1005 if (strcmp(str, "NONE") == 0) {
1009 *ppbe = OBJ_txt2nid(str);
1010 if (*ppbe == NID_undef) {
1011 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);