2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
46 void hex_prin(BIO *out, unsigned char *buf, int len);
47 static int alg_print(const X509_ALGOR *alg);
48 int cert_load(BIO *in, STACK_OF(X509) *sk);
49 static int set_pbe(int *ppbe, const char *str);
51 typedef enum OPTION_choice {
52 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
53 OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
54 OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
55 OPT_DESCERT, OPT_EXPORT, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
56 OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
57 OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME,
58 OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
59 OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_ENGINE,
63 const OPTIONS pkcs12_options[] = {
64 {"help", OPT_HELP, '-', "Display this summary"},
65 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
66 {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"},
67 {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"},
68 {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
69 {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
70 {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
71 {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"},
72 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
73 {"chain", OPT_CHAIN, '-', "Add certificate chain"},
74 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
75 {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
76 # ifndef OPENSSL_NO_RC2
77 {"descert", OPT_DESCERT, '-',
78 "Encrypt output with 3DES (default RC2-40)"},
79 {"certpbe", OPT_CERTPBE, 's',
80 "Certificate PBE algorithm (default RC2-40)"},
82 {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
83 {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
85 {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
86 {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
87 {"maciter", OPT_MACITER, '-', "Use MAC iteration"},
88 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
89 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
91 "Add local machine keyset attribute to private key"},
92 {"nodes", OPT_NODES, '-', "Don't encrypt private keys"},
93 {"macalg", OPT_MACALG, 's',
94 "Digest algorithm used in MAC (default SHA1)"},
95 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
97 {"inkey", OPT_INKEY, 's', "Private key if not infile"},
98 {"certfile", OPT_CERTFILE, '<', "Load certs from file"},
99 {"name", OPT_NAME, 's', "Use name as friendly name"},
100 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
101 {"caname", OPT_CANAME, 's',
102 "Use name as CA friendly name (can be repeated)"},
103 {"in", OPT_IN, '<', "Input filename"},
104 {"out", OPT_OUT, '>', "Output filename"},
105 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
106 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
107 {"password", OPT_PASSWORD, 's', "Set import/export password source"},
108 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
109 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
110 {"no-CAfile", OPT_NOCAFILE, '-',
111 "Do not load the default certificates file"},
112 {"no-CApath", OPT_NOCAPATH, '-',
113 "Do not load certificates from the default certificates directory"},
114 {"", OPT_CIPHER, '-', "Any supported cipher"},
115 # ifndef OPENSSL_NO_ENGINE
116 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
121 int pkcs12_main(int argc, char **argv)
123 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
124 char *name = NULL, *csp_name = NULL;
125 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
126 int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
127 int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
128 # ifndef OPENSSL_NO_RC2
129 int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
131 int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
133 int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
134 int ret = 1, macver = 1, add_lmk = 0, private = 0;
136 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
137 char *passin = NULL, *passout = NULL, *macalg = NULL;
138 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
139 const char *CApath = NULL, *CAfile = NULL, *prog;
140 int noCApath = 0, noCAfile = 0;
142 BIO *in = NULL, *out = NULL;
144 STACK_OF(OPENSSL_STRING) *canames = NULL;
145 const EVP_CIPHER *enc = EVP_des_ede3_cbc();
148 prog = opt_init(argc, argv, pkcs12_options);
149 while ((o = opt_next()) != OPT_EOF) {
154 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
157 opt_help(pkcs12_options);
179 options |= (NOKEYS | NOCERTS);
194 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
200 if (!opt_cipher(opt_unknown(), &enc))
207 maciter = PKCS12_DEFAULT_ITER;
222 if (!set_pbe(&cert_pbe, opt_arg()))
226 if (!set_pbe(&key_pbe, opt_arg()))
237 certfile = opt_arg();
246 csp_name = opt_arg();
250 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
252 sk_OPENSSL_STRING_push(canames, opt_arg());
261 passinarg = opt_arg();
264 passoutarg = opt_arg();
282 e = setup_engine(opt_arg(), 0);
286 argc = opt_num_rest();
292 if (passarg != NULL) {
294 passoutarg = passarg;
299 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
300 BIO_printf(bio_err, "Error getting passwords\n");
320 /* To avoid bit rot */
322 #ifndef OPENSSL_NO_UI_CONSOLE
323 if (EVP_read_pw_string(
324 macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) {
325 BIO_printf(bio_err, "Can't read Password\n");
330 BIO_printf(bio_err, "Unsupported option -twopass\n");
336 EVP_PKEY *key = NULL;
337 X509 *ucert = NULL, *x = NULL;
338 STACK_OF(X509) *certs = NULL;
339 const EVP_MD *macmd = NULL;
340 unsigned char *catmp = NULL;
343 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
344 BIO_printf(bio_err, "Nothing to do!\n");
348 if (options & NOCERTS)
351 if (!(options & NOKEYS)) {
352 key = load_key(keyname ? keyname : infile,
353 FORMAT_PEM, 1, passin, e, "private key");
358 /* Load in all certs in input file */
359 if (!(options & NOCERTS)) {
360 if (!load_certs(infile, &certs, FORMAT_PEM, NULL,
365 /* Look for matching private key */
366 for (i = 0; i < sk_X509_num(certs); i++) {
367 x = sk_X509_value(certs, i);
368 if (X509_check_private_key(x, key)) {
370 /* Zero keyid and alias */
371 X509_keyid_set1(ucert, NULL, 0);
372 X509_alias_set1(ucert, NULL, 0);
373 /* Remove from list */
374 (void)sk_X509_delete(certs, i);
380 "No certificate matches private key\n");
387 /* Add any more certificates asked for */
388 if (certfile != NULL) {
389 if (!load_certs(certfile, &certs, FORMAT_PEM, NULL,
390 "certificates from certfile"))
394 /* If chaining get chain from user cert */
397 STACK_OF(X509) *chain2;
399 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath))
403 vret = get_cert_chain(ucert, store, &chain2);
404 X509_STORE_free(store);
406 if (vret == X509_V_OK) {
407 /* Exclude verified certificate */
408 for (i = 1; i < sk_X509_num(chain2); i++)
409 sk_X509_push(certs, sk_X509_value(chain2, i));
410 /* Free first certificate */
411 X509_free(sk_X509_value(chain2, 0));
412 sk_X509_free(chain2);
414 if (vret != X509_V_ERR_UNSPECIFIED)
415 BIO_printf(bio_err, "Error %s getting chain.\n",
416 X509_verify_cert_error_string(vret));
418 ERR_print_errors(bio_err);
423 /* Add any CA names */
425 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
426 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
427 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
430 if (csp_name != NULL && key != NULL)
431 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
432 MBSTRING_ASC, (unsigned char *)csp_name,
435 if (add_lmk && key != NULL)
436 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
439 /* To avoid bit rot */
441 #ifndef OPENSSL_NO_UI_CONSOLE
442 if (EVP_read_pw_string(pass, sizeof(pass),
443 "Enter Export Password:", 1)) {
444 BIO_printf(bio_err, "Can't read Password\n");
449 BIO_printf(bio_err, "Password required\n");
455 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
457 p12 = PKCS12_create(cpass, name, key, ucert, certs,
458 key_pbe, cert_pbe, iter, -1, keytype);
461 ERR_print_errors(bio_err);
466 if (!opt_md(macalg, &macmd))
471 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
475 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
479 i2d_PKCS12_bio(out, p12);
486 sk_X509_pop_free(certs, X509_free);
493 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
496 out = bio_open_owner(outfile, FORMAT_PEM, private);
500 if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
501 ERR_print_errors(bio_err);
507 #ifndef OPENSSL_NO_UI_CONSOLE
508 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
510 BIO_printf(bio_err, "Can't read Password\n");
515 BIO_printf(bio_err, "Password required\n");
521 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
523 if ((options & INFO) && PKCS12_mac_present(p12)) {
524 const ASN1_INTEGER *tmaciter;
525 const X509_ALGOR *macalgid;
526 const ASN1_OBJECT *macobj;
527 const ASN1_OCTET_STRING *tmac;
528 const ASN1_OCTET_STRING *tsalt;
530 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
531 /* current hash algorithms do not use parameters so extract just name,
532 in future alg_print() may be needed */
533 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
534 BIO_puts(bio_err, "MAC: ");
535 i2a_ASN1_OBJECT(bio_err, macobj);
536 BIO_printf(bio_err, ", Iteration %ld\n",
537 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
538 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
539 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
540 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
543 /* If we enter empty password try no password first */
544 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
545 /* If mac and crypto pass the same set it to NULL too */
548 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
550 * May be UTF8 from previous version of OpenSSL:
551 * convert to a UTF8 form which will translate
552 * to the same Unicode password.
556 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
559 badpass = OPENSSL_uni2utf8(utmp, utmplen);
561 if (!PKCS12_verify_mac(p12, badpass, -1)) {
562 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
563 ERR_print_errors(bio_err);
566 BIO_printf(bio_err, "Warning: using broken algorithm\n");
574 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
575 BIO_printf(bio_err, "Error outputting keys and certificates\n");
576 ERR_print_errors(bio_err);
585 sk_OPENSSL_STRING_free(canames);
586 OPENSSL_free(badpass);
587 OPENSSL_free(passin);
588 OPENSSL_free(passout);
592 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
593 int passlen, int options, char *pempass,
594 const EVP_CIPHER *enc)
596 STACK_OF(PKCS7) *asafes = NULL;
597 STACK_OF(PKCS12_SAFEBAG) *bags;
602 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
604 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
605 p7 = sk_PKCS7_value(asafes, i);
606 bagnid = OBJ_obj2nid(p7->type);
607 if (bagnid == NID_pkcs7_data) {
608 bags = PKCS12_unpack_p7data(p7);
610 BIO_printf(bio_err, "PKCS7 Data\n");
611 } else if (bagnid == NID_pkcs7_encrypted) {
612 if (options & INFO) {
613 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
614 alg_print(p7->d.encrypted->enc_data->algorithm);
616 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
622 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
623 options, pempass, enc)) {
624 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
627 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
633 sk_PKCS7_pop_free(asafes, PKCS7_free);
637 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
638 const char *pass, int passlen, int options,
639 char *pempass, const EVP_CIPHER *enc)
642 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
643 if (!dump_certs_pkeys_bag(out,
644 sk_PKCS12_SAFEBAG_value(bags, i),
645 pass, passlen, options, pempass, enc))
651 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
652 const char *pass, int passlen, int options,
653 char *pempass, const EVP_CIPHER *enc)
656 PKCS8_PRIV_KEY_INFO *p8;
657 const PKCS8_PRIV_KEY_INFO *p8c;
659 const STACK_OF(X509_ATTRIBUTE) *attrs;
662 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
664 switch (PKCS12_SAFEBAG_get_nid(bag)) {
667 BIO_printf(bio_err, "Key bag\n");
668 if (options & NOKEYS)
670 print_attribs(out, attrs, "Bag Attributes");
671 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
672 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
674 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
675 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
679 case NID_pkcs8ShroudedKeyBag:
680 if (options & INFO) {
682 const X509_ALGOR *tp8alg;
684 BIO_printf(bio_err, "Shrouded Keybag: ");
685 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
686 X509_SIG_get0(tp8, &tp8alg, NULL);
689 if (options & NOKEYS)
691 print_attribs(out, attrs, "Bag Attributes");
692 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
694 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
695 PKCS8_PRIV_KEY_INFO_free(p8);
698 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
699 PKCS8_PRIV_KEY_INFO_free(p8);
700 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
706 BIO_printf(bio_err, "Certificate bag\n");
707 if (options & NOCERTS)
709 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
710 if (options & CACERTS)
712 } else if (options & CLCERTS)
714 print_attribs(out, attrs, "Bag Attributes");
715 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
717 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
719 dump_cert_text(out, x509);
720 ret = PEM_write_bio_X509(out, x509);
724 case NID_safeContentsBag:
726 BIO_printf(bio_err, "Safe Contents bag\n");
727 print_attribs(out, attrs, "Bag Attributes");
728 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
729 pass, passlen, options, pempass, enc);
732 BIO_printf(bio_err, "Warning unsupported bag type: ");
733 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
734 BIO_printf(bio_err, "\n");
740 /* Given a single certificate return a verified chain or NULL if error */
742 static int get_cert_chain(X509 *cert, X509_STORE *store,
743 STACK_OF(X509) **chain)
745 X509_STORE_CTX *store_ctx = NULL;
746 STACK_OF(X509) *chn = NULL;
749 store_ctx = X509_STORE_CTX_new();
750 if (store_ctx == NULL) {
751 i = X509_V_ERR_UNSPECIFIED;
754 if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {
755 i = X509_V_ERR_UNSPECIFIED;
760 if (X509_verify_cert(store_ctx) > 0)
761 chn = X509_STORE_CTX_get1_chain(store_ctx);
762 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
763 i = X509_V_ERR_UNSPECIFIED;
766 X509_STORE_CTX_free(store_ctx);
771 static int alg_print(const X509_ALGOR *alg)
773 int pbenid, aparamtype;
774 const ASN1_OBJECT *aoid;
776 PBEPARAM *pbe = NULL;
778 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
780 pbenid = OBJ_obj2nid(aoid);
782 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
785 * If PBE algorithm is PBES2 decode algorithm parameters
786 * for additional details.
788 if (pbenid == NID_pbes2) {
789 PBE2PARAM *pbe2 = NULL;
791 if (aparamtype == V_ASN1_SEQUENCE)
792 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
794 BIO_puts(bio_err, ", <unsupported parameters>");
797 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
798 pbenid = OBJ_obj2nid(aoid);
799 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
800 encnid = OBJ_obj2nid(aoid);
801 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
803 /* If KDF is PBKDF2 decode parameters */
804 if (pbenid == NID_id_pbkdf2) {
805 PBKDF2PARAM *kdf = NULL;
807 if (aparamtype == V_ASN1_SEQUENCE)
808 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
810 BIO_puts(bio_err, ", <unsupported parameters>");
814 if (kdf->prf == NULL) {
815 prfnid = NID_hmacWithSHA1;
817 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
818 prfnid = OBJ_obj2nid(aoid);
820 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
821 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
822 PBKDF2PARAM_free(kdf);
823 #ifndef OPENSSL_NO_SCRYPT
824 } else if (pbenid == NID_id_scrypt) {
825 SCRYPT_PARAMS *kdf = NULL;
827 if (aparamtype == V_ASN1_SEQUENCE)
828 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
830 BIO_puts(bio_err, ", <unsupported parameters>");
833 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
834 "Block size(r): %ld, Paralelizm(p): %ld",
835 ASN1_STRING_length(kdf->salt),
836 ASN1_INTEGER_get(kdf->costParameter),
837 ASN1_INTEGER_get(kdf->blockSize),
838 ASN1_INTEGER_get(kdf->parallelizationParameter));
839 SCRYPT_PARAMS_free(kdf);
842 PBE2PARAM_free(pbe2);
844 if (aparamtype == V_ASN1_SEQUENCE)
845 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
847 BIO_puts(bio_err, ", <unsupported parameters>");
850 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
854 BIO_puts(bio_err, "\n");
858 /* Load all certificates from a given file */
860 int cert_load(BIO *in, STACK_OF(X509) *sk)
865 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
867 sk_X509_push(sk, cert);
874 /* Generalised attribute print: handle PKCS#8 and bag attributes */
876 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
879 X509_ATTRIBUTE *attr;
884 BIO_printf(out, "%s: <No Attributes>\n", name);
887 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
888 BIO_printf(out, "%s: <Empty Attributes>\n", name);
891 BIO_printf(out, "%s\n", name);
892 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
893 ASN1_OBJECT *attr_obj;
894 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
895 attr_obj = X509_ATTRIBUTE_get0_object(attr);
896 attr_nid = OBJ_obj2nid(attr_obj);
897 BIO_printf(out, " ");
898 if (attr_nid == NID_undef) {
899 i2a_ASN1_OBJECT(out, attr_obj);
900 BIO_printf(out, ": ");
902 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
905 if (X509_ATTRIBUTE_count(attr)) {
906 av = X509_ATTRIBUTE_get0_type(attr, 0);
908 case V_ASN1_BMPSTRING:
909 value = OPENSSL_uni2asc(av->value.bmpstring->data,
910 av->value.bmpstring->length);
911 BIO_printf(out, "%s\n", value);
915 case V_ASN1_OCTET_STRING:
916 hex_prin(out, av->value.octet_string->data,
917 av->value.octet_string->length);
918 BIO_printf(out, "\n");
921 case V_ASN1_BIT_STRING:
922 hex_prin(out, av->value.bit_string->data,
923 av->value.bit_string->length);
924 BIO_printf(out, "\n");
928 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
932 BIO_printf(out, "<No Values>\n");
938 void hex_prin(BIO *out, unsigned char *buf, int len)
941 for (i = 0; i < len; i++)
942 BIO_printf(out, "%02X ", buf[i]);
945 static int set_pbe(int *ppbe, const char *str)
949 if (strcmp(str, "NONE") == 0) {
953 *ppbe = OBJ_txt2nid(str);
954 if (*ppbe == NID_undef) {
955 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);