2 * Copyright 1999-2018 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>
11 #if defined(OPENSSL_NO_DES)
12 NON_EMPTY_TRANSLATION_UNIT
20 # include <openssl/crypto.h>
21 # include <openssl/err.h>
22 # include <openssl/pem.h>
23 # include <openssl/pkcs12.h>
31 #define PASSWD_BUF_SIZE 2048
33 static int get_cert_chain(X509 *cert, X509_STORE *store,
34 STACK_OF(X509) **chain);
35 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
36 const char *pass, int passlen, int options,
37 char *pempass, const EVP_CIPHER *enc);
38 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
39 const char *pass, int passlen, int options,
40 char *pempass, const EVP_CIPHER *enc);
41 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
42 const char *pass, int passlen,
43 int options, char *pempass, const EVP_CIPHER *enc);
44 void print_attribute(BIO *out, const ASN1_TYPE *av);
45 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
47 void hex_prin(BIO *out, unsigned char *buf, int len);
48 static int alg_print(const X509_ALGOR *alg);
49 int cert_load(BIO *in, STACK_OF(X509) *sk);
50 static int set_pbe(int *ppbe, const char *str);
52 typedef enum OPTION_choice {
53 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
54 OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
55 OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
56 OPT_DESCERT, OPT_EXPORT, OPT_ITER, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
57 OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
58 OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME,
59 OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
60 OPT_CAFILE, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_ENGINE,
61 OPT_R_ENUM, OPT_PROV_ENUM
64 const OPTIONS pkcs12_options[] = {
65 OPT_SECTION("General"),
66 {"help", OPT_HELP, '-', "Display this summary"},
67 # ifndef OPENSSL_NO_ENGINE
68 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
72 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
73 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
74 {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
75 {"no-CAfile", OPT_NOCAFILE, '-',
76 "Do not load the default certificates file"},
77 {"no-CApath", OPT_NOCAPATH, '-',
78 "Do not load certificates from the default certificates directory"},
79 {"no-CAstore", OPT_NOCASTORE, '-',
80 "Do not load certificates from the default certificates store"},
83 {"inkey", OPT_INKEY, 's', "Private key if not infile"},
84 {"certfile", OPT_CERTFILE, '<', "Load certs from file"},
85 {"name", OPT_NAME, 's', "Use name as friendly name"},
86 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
87 {"caname", OPT_CANAME, 's',
88 "Use name as CA friendly name (can be repeated)"},
89 {"in", OPT_IN, '<', "Input filename"},
90 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
92 OPT_SECTION("Output"),
93 {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
95 "Add local machine keyset attribute to private key"},
96 {"macalg", OPT_MACALG, 's',
97 "Digest algorithm used in MAC (default SHA1)"},
98 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
99 {"out", OPT_OUT, '>', "Output filename"},
100 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
101 {"password", OPT_PASSWORD, 's', "Set import/export password source"},
102 {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
103 {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
104 {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
105 {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"},
106 {"chain", OPT_CHAIN, '-', "Add certificate chain"},
107 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
108 {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
109 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
110 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
111 {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"},
112 {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"},
114 OPT_SECTION("Encryption"),
115 # ifndef OPENSSL_NO_RC2
116 {"descert", OPT_DESCERT, '-',
117 "Encrypt output with 3DES (default RC2-40)"},
118 {"certpbe", OPT_CERTPBE, 's',
119 "Certificate PBE algorithm (default RC2-40)"},
121 {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
122 {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
124 {"iter", OPT_ITER, 'p', "Specify the iteration count for encryption key and MAC"},
125 {"noiter", OPT_NOITER, '-', "Don't use encryption key iteration"},
126 {"maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility"},
127 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
128 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
129 {"nodes", OPT_NODES, '-', "Don't encrypt private keys"},
130 {"", OPT_CIPHER, '-', "Any supported cipher"},
137 int pkcs12_main(int argc, char **argv)
139 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
140 char *name = NULL, *csp_name = NULL;
141 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
142 int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
143 int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
144 # ifndef OPENSSL_NO_RC2
145 int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
147 int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
149 int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
150 int ret = 1, macver = 1, add_lmk = 0, private = 0;
152 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
153 char *passin = NULL, *passout = NULL, *macalg = NULL;
154 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
155 const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog;
156 int noCApath = 0, noCAfile = 0, noCAstore = 0;
158 BIO *in = NULL, *out = NULL;
160 STACK_OF(OPENSSL_STRING) *canames = NULL;
161 const EVP_CIPHER *enc = EVP_des_ede3_cbc();
164 prog = opt_init(argc, argv, pkcs12_options);
165 while ((o = opt_next()) != OPT_EOF) {
170 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
173 opt_help(pkcs12_options);
195 options |= (NOKEYS | NOCERTS);
210 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
216 if (!opt_cipher(opt_unknown(), &enc))
220 if (!opt_int(opt_arg(), &iter))
243 if (!set_pbe(&cert_pbe, opt_arg()))
247 if (!set_pbe(&key_pbe, opt_arg()))
258 certfile = opt_arg();
267 csp_name = opt_arg();
271 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
273 sk_OPENSSL_STRING_push(canames, opt_arg());
282 passinarg = opt_arg();
285 passoutarg = opt_arg();
309 e = setup_engine(opt_arg(), 0);
312 if (!opt_provider(o))
317 argc = opt_num_rest();
323 if (passarg != NULL) {
325 passoutarg = passarg;
330 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
331 BIO_printf(bio_err, "Error getting passwords\n");
347 BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
349 BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
358 /* To avoid bit rot */
360 #ifndef OPENSSL_NO_UI_CONSOLE
361 if (EVP_read_pw_string(
362 macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) {
363 BIO_printf(bio_err, "Can't read Password\n");
368 BIO_printf(bio_err, "Unsupported option -twopass\n");
374 EVP_PKEY *key = NULL;
375 X509 *ucert = NULL, *x = NULL;
376 STACK_OF(X509) *certs = NULL;
377 const EVP_MD *macmd = NULL;
378 unsigned char *catmp = NULL;
381 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
382 BIO_printf(bio_err, "Nothing to do!\n");
386 if (options & NOCERTS)
389 if (!(options & NOKEYS)) {
390 key = load_key(keyname ? keyname : infile,
391 FORMAT_PEM, 1, passin, e, "private key");
396 /* Load in all certs in input file */
397 if (!(options & NOCERTS)) {
398 if (!load_certs(infile, &certs, FORMAT_PEM, NULL,
403 /* Look for matching private key */
404 for (i = 0; i < sk_X509_num(certs); i++) {
405 x = sk_X509_value(certs, i);
406 if (X509_check_private_key(x, key)) {
408 /* Zero keyid and alias */
409 X509_keyid_set1(ucert, NULL, 0);
410 X509_alias_set1(ucert, NULL, 0);
411 /* Remove from list */
412 (void)sk_X509_delete(certs, i);
418 "No certificate matches private key\n");
425 /* Add any more certificates asked for */
426 if (certfile != NULL) {
427 if (!load_certs(certfile, &certs, FORMAT_PEM, NULL,
428 "certificates from certfile"))
432 /* If chaining get chain from user cert */
435 STACK_OF(X509) *chain2;
437 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
442 vret = get_cert_chain(ucert, store, &chain2);
443 X509_STORE_free(store);
445 if (vret == X509_V_OK) {
446 /* Exclude verified certificate */
447 for (i = 1; i < sk_X509_num(chain2); i++)
448 sk_X509_push(certs, sk_X509_value(chain2, i));
449 /* Free first certificate */
450 X509_free(sk_X509_value(chain2, 0));
451 sk_X509_free(chain2);
453 if (vret != X509_V_ERR_UNSPECIFIED)
454 BIO_printf(bio_err, "Error %s getting chain.\n",
455 X509_verify_cert_error_string(vret));
457 ERR_print_errors(bio_err);
462 /* Add any CA names */
464 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
465 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
466 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
469 if (csp_name != NULL && key != NULL)
470 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
471 MBSTRING_ASC, (unsigned char *)csp_name,
474 if (add_lmk && key != NULL)
475 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
478 /* To avoid bit rot */
480 #ifndef OPENSSL_NO_UI_CONSOLE
481 if (EVP_read_pw_string(pass, sizeof(pass),
482 "Enter Export Password:", 1)) {
483 BIO_printf(bio_err, "Can't read Password\n");
488 BIO_printf(bio_err, "Password required\n");
494 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
496 p12 = PKCS12_create(cpass, name, key, ucert, certs,
497 key_pbe, cert_pbe, iter, -1, keytype);
500 ERR_print_errors(bio_err);
505 if (!opt_md(macalg, &macmd))
510 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
514 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
518 i2d_PKCS12_bio(out, p12);
525 sk_X509_pop_free(certs, X509_free);
532 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
535 out = bio_open_owner(outfile, FORMAT_PEM, private);
539 if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
540 ERR_print_errors(bio_err);
546 #ifndef OPENSSL_NO_UI_CONSOLE
547 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
549 BIO_printf(bio_err, "Can't read Password\n");
554 BIO_printf(bio_err, "Password required\n");
560 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
562 if ((options & INFO) && PKCS12_mac_present(p12)) {
563 const ASN1_INTEGER *tmaciter;
564 const X509_ALGOR *macalgid;
565 const ASN1_OBJECT *macobj;
566 const ASN1_OCTET_STRING *tmac;
567 const ASN1_OCTET_STRING *tsalt;
569 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
570 /* current hash algorithms do not use parameters so extract just name,
571 in future alg_print() may be needed */
572 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
573 BIO_puts(bio_err, "MAC: ");
574 i2a_ASN1_OBJECT(bio_err, macobj);
575 BIO_printf(bio_err, ", Iteration %ld\n",
576 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
577 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
578 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
579 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
582 /* If we enter empty password try no password first */
583 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
584 /* If mac and crypto pass the same set it to NULL too */
587 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
589 * May be UTF8 from previous version of OpenSSL:
590 * convert to a UTF8 form which will translate
591 * to the same Unicode password.
595 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
598 badpass = OPENSSL_uni2utf8(utmp, utmplen);
600 if (!PKCS12_verify_mac(p12, badpass, -1)) {
601 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
602 ERR_print_errors(bio_err);
605 BIO_printf(bio_err, "Warning: using broken algorithm\n");
613 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
614 BIO_printf(bio_err, "Error outputting keys and certificates\n");
615 ERR_print_errors(bio_err);
624 sk_OPENSSL_STRING_free(canames);
625 OPENSSL_free(badpass);
626 OPENSSL_free(passin);
627 OPENSSL_free(passout);
631 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
632 int passlen, int options, char *pempass,
633 const EVP_CIPHER *enc)
635 STACK_OF(PKCS7) *asafes = NULL;
636 STACK_OF(PKCS12_SAFEBAG) *bags;
641 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
643 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
644 p7 = sk_PKCS7_value(asafes, i);
645 bagnid = OBJ_obj2nid(p7->type);
646 if (bagnid == NID_pkcs7_data) {
647 bags = PKCS12_unpack_p7data(p7);
649 BIO_printf(bio_err, "PKCS7 Data\n");
650 } else if (bagnid == NID_pkcs7_encrypted) {
651 if (options & INFO) {
652 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
653 alg_print(p7->d.encrypted->enc_data->algorithm);
655 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
661 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
662 options, pempass, enc)) {
663 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
666 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
672 sk_PKCS7_pop_free(asafes, PKCS7_free);
676 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
677 const char *pass, int passlen, int options,
678 char *pempass, const EVP_CIPHER *enc)
681 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
682 if (!dump_certs_pkeys_bag(out,
683 sk_PKCS12_SAFEBAG_value(bags, i),
684 pass, passlen, options, pempass, enc))
690 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
691 const char *pass, int passlen, int options,
692 char *pempass, const EVP_CIPHER *enc)
695 PKCS8_PRIV_KEY_INFO *p8;
696 const PKCS8_PRIV_KEY_INFO *p8c;
698 const STACK_OF(X509_ATTRIBUTE) *attrs;
701 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
703 switch (PKCS12_SAFEBAG_get_nid(bag)) {
706 BIO_printf(bio_err, "Key bag\n");
707 if (options & NOKEYS)
709 print_attribs(out, attrs, "Bag Attributes");
710 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
711 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
713 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
714 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
718 case NID_pkcs8ShroudedKeyBag:
719 if (options & INFO) {
721 const X509_ALGOR *tp8alg;
723 BIO_printf(bio_err, "Shrouded Keybag: ");
724 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
725 X509_SIG_get0(tp8, &tp8alg, NULL);
728 if (options & NOKEYS)
730 print_attribs(out, attrs, "Bag Attributes");
731 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
733 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
734 PKCS8_PRIV_KEY_INFO_free(p8);
737 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
738 PKCS8_PRIV_KEY_INFO_free(p8);
739 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
745 BIO_printf(bio_err, "Certificate bag\n");
746 if (options & NOCERTS)
748 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
749 if (options & CACERTS)
751 } else if (options & CLCERTS)
753 print_attribs(out, attrs, "Bag Attributes");
754 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
756 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
758 dump_cert_text(out, x509);
759 ret = PEM_write_bio_X509(out, x509);
763 case NID_safeContentsBag:
765 BIO_printf(bio_err, "Safe Contents bag\n");
766 print_attribs(out, attrs, "Bag Attributes");
767 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
768 pass, passlen, options, pempass, enc);
771 BIO_printf(bio_err, "Warning unsupported bag type: ");
772 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
773 BIO_printf(bio_err, "\n");
779 /* Given a single certificate return a verified chain or NULL if error */
781 static int get_cert_chain(X509 *cert, X509_STORE *store,
782 STACK_OF(X509) **chain)
784 X509_STORE_CTX *store_ctx = NULL;
785 STACK_OF(X509) *chn = NULL;
788 store_ctx = X509_STORE_CTX_new();
789 if (store_ctx == NULL) {
790 i = X509_V_ERR_UNSPECIFIED;
793 if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {
794 i = X509_V_ERR_UNSPECIFIED;
799 if (X509_verify_cert(store_ctx) > 0)
800 chn = X509_STORE_CTX_get1_chain(store_ctx);
801 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
802 i = X509_V_ERR_UNSPECIFIED;
805 X509_STORE_CTX_free(store_ctx);
810 static int alg_print(const X509_ALGOR *alg)
812 int pbenid, aparamtype;
813 const ASN1_OBJECT *aoid;
815 PBEPARAM *pbe = NULL;
817 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
819 pbenid = OBJ_obj2nid(aoid);
821 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
824 * If PBE algorithm is PBES2 decode algorithm parameters
825 * for additional details.
827 if (pbenid == NID_pbes2) {
828 PBE2PARAM *pbe2 = NULL;
830 if (aparamtype == V_ASN1_SEQUENCE)
831 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
833 BIO_puts(bio_err, ", <unsupported parameters>");
836 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
837 pbenid = OBJ_obj2nid(aoid);
838 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
839 encnid = OBJ_obj2nid(aoid);
840 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
842 /* If KDF is PBKDF2 decode parameters */
843 if (pbenid == NID_id_pbkdf2) {
844 PBKDF2PARAM *kdf = NULL;
846 if (aparamtype == V_ASN1_SEQUENCE)
847 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
849 BIO_puts(bio_err, ", <unsupported parameters>");
853 if (kdf->prf == NULL) {
854 prfnid = NID_hmacWithSHA1;
856 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
857 prfnid = OBJ_obj2nid(aoid);
859 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
860 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
861 PBKDF2PARAM_free(kdf);
862 #ifndef OPENSSL_NO_SCRYPT
863 } else if (pbenid == NID_id_scrypt) {
864 SCRYPT_PARAMS *kdf = NULL;
866 if (aparamtype == V_ASN1_SEQUENCE)
867 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
869 BIO_puts(bio_err, ", <unsupported parameters>");
872 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
873 "Block size(r): %ld, Parallelism(p): %ld",
874 ASN1_STRING_length(kdf->salt),
875 ASN1_INTEGER_get(kdf->costParameter),
876 ASN1_INTEGER_get(kdf->blockSize),
877 ASN1_INTEGER_get(kdf->parallelizationParameter));
878 SCRYPT_PARAMS_free(kdf);
881 PBE2PARAM_free(pbe2);
883 if (aparamtype == V_ASN1_SEQUENCE)
884 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
886 BIO_puts(bio_err, ", <unsupported parameters>");
889 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
893 BIO_puts(bio_err, "\n");
897 /* Load all certificates from a given file */
899 int cert_load(BIO *in, STACK_OF(X509) *sk)
904 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
906 if (!sk_X509_push(sk, cert))
914 /* Generalised x509 attribute value print */
916 void print_attribute(BIO *out, const ASN1_TYPE *av)
921 case V_ASN1_BMPSTRING:
922 value = OPENSSL_uni2asc(av->value.bmpstring->data,
923 av->value.bmpstring->length);
924 BIO_printf(out, "%s\n", value);
928 case V_ASN1_OCTET_STRING:
929 hex_prin(out, av->value.octet_string->data,
930 av->value.octet_string->length);
931 BIO_printf(out, "\n");
934 case V_ASN1_BIT_STRING:
935 hex_prin(out, av->value.bit_string->data,
936 av->value.bit_string->length);
937 BIO_printf(out, "\n");
941 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
946 /* Generalised attribute print: handle PKCS#8 and bag attributes */
948 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
951 X509_ATTRIBUTE *attr;
955 BIO_printf(out, "%s: <No Attributes>\n", name);
958 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
959 BIO_printf(out, "%s: <Empty Attributes>\n", name);
962 BIO_printf(out, "%s\n", name);
963 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
964 ASN1_OBJECT *attr_obj;
965 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
966 attr_obj = X509_ATTRIBUTE_get0_object(attr);
967 attr_nid = OBJ_obj2nid(attr_obj);
968 BIO_printf(out, " ");
969 if (attr_nid == NID_undef) {
970 i2a_ASN1_OBJECT(out, attr_obj);
971 BIO_printf(out, ": ");
973 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
976 if (X509_ATTRIBUTE_count(attr)) {
977 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
979 av = X509_ATTRIBUTE_get0_type(attr, j);
980 print_attribute(out, av);
983 BIO_printf(out, "<No Values>\n");
989 void hex_prin(BIO *out, unsigned char *buf, int len)
992 for (i = 0; i < len; i++)
993 BIO_printf(out, "%02X ", buf[i]);
996 static int set_pbe(int *ppbe, const char *str)
1000 if (strcmp(str, "NONE") == 0) {
1004 *ppbe = OBJ_txt2nid(str);
1005 if (*ppbe == NID_undef) {
1006 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);