2 * Copyright 1999-2017 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
19 # include <openssl/crypto.h>
20 # include <openssl/err.h>
21 # include <openssl/pem.h>
22 # include <openssl/pkcs12.h>
30 #define PASSWD_BUF_SIZE 2048
32 static int get_cert_chain(X509 *cert, X509_STORE *store,
33 STACK_OF(X509) **chain);
34 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
35 const char *pass, int passlen, int options,
36 char *pempass, const EVP_CIPHER *enc);
37 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
38 const char *pass, int passlen, int options,
39 char *pempass, const EVP_CIPHER *enc);
40 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
41 const char *pass, int passlen,
42 int options, char *pempass, const EVP_CIPHER *enc);
43 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
45 void hex_prin(BIO *out, unsigned char *buf, int len);
46 static int alg_print(const X509_ALGOR *alg);
47 int cert_load(BIO *in, STACK_OF(X509) *sk);
48 static int set_pbe(int *ppbe, const char *str);
50 typedef enum OPTION_choice {
51 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
52 OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
53 OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
54 OPT_DESCERT, OPT_EXPORT, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
55 OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
56 OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME,
57 OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
58 OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_ENGINE,
62 const OPTIONS pkcs12_options[] = {
63 {"help", OPT_HELP, '-', "Display this summary"},
64 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
65 {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"},
66 {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"},
67 {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
68 {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
69 {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
70 {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"},
71 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
72 {"chain", OPT_CHAIN, '-', "Add certificate chain"},
73 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
74 {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
75 # ifndef OPENSSL_NO_RC2
76 {"descert", OPT_DESCERT, '-',
77 "Encrypt output with 3DES (default RC2-40)"},
78 {"certpbe", OPT_CERTPBE, 's',
79 "Certificate PBE algorithm (default RC2-40)"},
81 {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
82 {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
84 {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
85 {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
86 {"maciter", OPT_MACITER, '-', "Use MAC iteration"},
87 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
88 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
90 "Add local machine keyset attribute to private key"},
91 {"nodes", OPT_NODES, '-', "Don't encrypt private keys"},
92 {"macalg", OPT_MACALG, 's',
93 "Digest algorithm used in MAC (default SHA1)"},
94 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
96 {"inkey", OPT_INKEY, 's', "Private key if not infile"},
97 {"certfile", OPT_CERTFILE, '<', "Load certs from file"},
98 {"name", OPT_NAME, 's', "Use name as friendly name"},
99 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
100 {"caname", OPT_CANAME, 's',
101 "Use name as CA friendly name (can be repeated)"},
102 {"in", OPT_IN, '<', "Input filename"},
103 {"out", OPT_OUT, '>', "Output filename"},
104 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
105 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
106 {"password", OPT_PASSWORD, 's', "Set import/export password source"},
107 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
108 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
109 {"no-CAfile", OPT_NOCAFILE, '-',
110 "Do not load the default certificates file"},
111 {"no-CApath", OPT_NOCAPATH, '-',
112 "Do not load certificates from the default certificates directory"},
113 {"", OPT_CIPHER, '-', "Any supported cipher"},
114 # ifndef OPENSSL_NO_ENGINE
115 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
120 int pkcs12_main(int argc, char **argv)
122 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
123 char *name = NULL, *csp_name = NULL;
124 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
125 int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
126 int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
127 # ifndef OPENSSL_NO_RC2
128 int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
130 int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
132 int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
133 int ret = 1, macver = 1, add_lmk = 0, private = 0;
135 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
136 char *passin = NULL, *passout = NULL, *macalg = NULL;
137 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
138 const char *CApath = NULL, *CAfile = NULL, *prog;
139 int noCApath = 0, noCAfile = 0;
141 BIO *in = NULL, *out = NULL;
143 STACK_OF(OPENSSL_STRING) *canames = NULL;
144 const EVP_CIPHER *enc = EVP_des_ede3_cbc();
147 prog = opt_init(argc, argv, pkcs12_options);
148 while ((o = opt_next()) != OPT_EOF) {
153 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
156 opt_help(pkcs12_options);
178 options |= (NOKEYS | NOCERTS);
193 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
199 if (!opt_cipher(opt_unknown(), &enc))
206 maciter = PKCS12_DEFAULT_ITER;
221 if (!set_pbe(&cert_pbe, opt_arg()))
225 if (!set_pbe(&key_pbe, opt_arg()))
236 certfile = opt_arg();
245 csp_name = opt_arg();
249 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
251 sk_OPENSSL_STRING_push(canames, opt_arg());
260 passinarg = opt_arg();
263 passoutarg = opt_arg();
281 e = setup_engine(opt_arg(), 0);
285 argc = opt_num_rest();
291 if (passarg != NULL) {
293 passoutarg = passarg;
298 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
299 BIO_printf(bio_err, "Error getting passwords\n");
319 /* To avoid bit rot */
321 #ifndef OPENSSL_NO_UI_CONSOLE
322 if (EVP_read_pw_string
323 (macpass, sizeof macpass, "Enter MAC Password:", export_cert)) {
324 BIO_printf(bio_err, "Can't read Password\n");
329 BIO_printf(bio_err, "Unsupported option -twopass\n");
335 EVP_PKEY *key = NULL;
336 X509 *ucert = NULL, *x = NULL;
337 STACK_OF(X509) *certs = NULL;
338 const EVP_MD *macmd = NULL;
339 unsigned char *catmp = NULL;
342 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
343 BIO_printf(bio_err, "Nothing to do!\n");
347 if (options & NOCERTS)
350 if (!(options & NOKEYS)) {
351 key = load_key(keyname ? keyname : infile,
352 FORMAT_PEM, 1, passin, e, "private key");
357 /* Load in all certs in input file */
358 if (!(options & NOCERTS)) {
359 if (!load_certs(infile, &certs, FORMAT_PEM, NULL,
364 /* Look for matching private key */
365 for (i = 0; i < sk_X509_num(certs); i++) {
366 x = sk_X509_value(certs, i);
367 if (X509_check_private_key(x, key)) {
369 /* Zero keyid and alias */
370 X509_keyid_set1(ucert, NULL, 0);
371 X509_alias_set1(ucert, NULL, 0);
372 /* Remove from list */
373 (void)sk_X509_delete(certs, i);
379 "No certificate matches private key\n");
386 /* Add any more certificates asked for */
387 if (certfile != NULL) {
388 if (!load_certs(certfile, &certs, FORMAT_PEM, NULL,
389 "certificates from certfile"))
393 /* If chaining get chain from user cert */
396 STACK_OF(X509) *chain2;
398 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath))
402 vret = get_cert_chain(ucert, store, &chain2);
403 X509_STORE_free(store);
405 if (vret == X509_V_OK) {
406 /* Exclude verified certificate */
407 for (i = 1; i < sk_X509_num(chain2); i++)
408 sk_X509_push(certs, sk_X509_value(chain2, i));
409 /* Free first certificate */
410 X509_free(sk_X509_value(chain2, 0));
411 sk_X509_free(chain2);
413 if (vret != X509_V_ERR_UNSPECIFIED)
414 BIO_printf(bio_err, "Error %s getting chain.\n",
415 X509_verify_cert_error_string(vret));
417 ERR_print_errors(bio_err);
422 /* Add any CA names */
424 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
425 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
426 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
429 if (csp_name != NULL && key != NULL)
430 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
431 MBSTRING_ASC, (unsigned char *)csp_name,
434 if (add_lmk && key != NULL)
435 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
438 /* To avoid bit rot */
440 #ifndef OPENSSL_NO_UI_CONSOLE
441 if (EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:",
443 BIO_printf(bio_err, "Can't read Password\n");
448 BIO_printf(bio_err, "Password required\n");
454 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
456 p12 = PKCS12_create(cpass, name, key, ucert, certs,
457 key_pbe, cert_pbe, iter, -1, keytype);
460 ERR_print_errors(bio_err);
465 if (!opt_md(macalg, &macmd))
470 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
474 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
478 i2d_PKCS12_bio(out, p12);
485 sk_X509_pop_free(certs, X509_free);
492 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
495 out = bio_open_owner(outfile, FORMAT_PEM, private);
499 if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
500 ERR_print_errors(bio_err);
506 #ifndef OPENSSL_NO_UI_CONSOLE
507 if (EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:",
509 BIO_printf(bio_err, "Can't read Password\n");
514 BIO_printf(bio_err, "Password required\n");
520 OPENSSL_strlcpy(macpass, pass, sizeof macpass);
522 if ((options & INFO) && PKCS12_mac_present(p12)) {
523 const ASN1_INTEGER *tmaciter;
524 const X509_ALGOR *macalgid;
525 const ASN1_OBJECT *macobj;
526 const ASN1_OCTET_STRING *tmac;
527 const ASN1_OCTET_STRING *tsalt;
529 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
530 /* current hash algorithms do not use parameters so extract just name,
531 in future alg_print() may be needed */
532 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
533 BIO_puts(bio_err, "MAC: ");
534 i2a_ASN1_OBJECT(bio_err, macobj);
535 BIO_printf(bio_err, ", Iteration %ld\n",
536 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
537 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
538 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
539 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
542 /* If we enter empty password try no password first */
543 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
544 /* If mac and crypto pass the same set it to NULL too */
547 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
549 * May be UTF8 from previous version of OpenSSL:
550 * convert to a UTF8 form which will translate
551 * to the same Unicode password.
555 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
558 badpass = OPENSSL_uni2utf8(utmp, utmplen);
560 if (!PKCS12_verify_mac(p12, badpass, -1)) {
561 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
562 ERR_print_errors(bio_err);
565 BIO_printf(bio_err, "Warning: using broken algorithm\n");
573 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
574 BIO_printf(bio_err, "Error outputting keys and certificates\n");
575 ERR_print_errors(bio_err);
584 sk_OPENSSL_STRING_free(canames);
585 OPENSSL_free(badpass);
586 OPENSSL_free(passin);
587 OPENSSL_free(passout);
591 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
592 int passlen, int options, char *pempass,
593 const EVP_CIPHER *enc)
595 STACK_OF(PKCS7) *asafes = NULL;
596 STACK_OF(PKCS12_SAFEBAG) *bags;
601 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
603 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
604 p7 = sk_PKCS7_value(asafes, i);
605 bagnid = OBJ_obj2nid(p7->type);
606 if (bagnid == NID_pkcs7_data) {
607 bags = PKCS12_unpack_p7data(p7);
609 BIO_printf(bio_err, "PKCS7 Data\n");
610 } else if (bagnid == NID_pkcs7_encrypted) {
611 if (options & INFO) {
612 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
613 alg_print(p7->d.encrypted->enc_data->algorithm);
615 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
621 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
622 options, pempass, enc)) {
623 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
626 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
632 sk_PKCS7_pop_free(asafes, PKCS7_free);
636 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
637 const char *pass, int passlen, int options,
638 char *pempass, const EVP_CIPHER *enc)
641 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
642 if (!dump_certs_pkeys_bag(out,
643 sk_PKCS12_SAFEBAG_value(bags, i),
644 pass, passlen, options, pempass, enc))
650 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
651 const char *pass, int passlen, int options,
652 char *pempass, const EVP_CIPHER *enc)
655 PKCS8_PRIV_KEY_INFO *p8;
656 const PKCS8_PRIV_KEY_INFO *p8c;
658 const STACK_OF(X509_ATTRIBUTE) *attrs;
661 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
663 switch (PKCS12_SAFEBAG_get_nid(bag)) {
666 BIO_printf(bio_err, "Key bag\n");
667 if (options & NOKEYS)
669 print_attribs(out, attrs, "Bag Attributes");
670 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
671 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
673 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
674 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
678 case NID_pkcs8ShroudedKeyBag:
679 if (options & INFO) {
681 const X509_ALGOR *tp8alg;
683 BIO_printf(bio_err, "Shrouded Keybag: ");
684 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
685 X509_SIG_get0(tp8, &tp8alg, NULL);
688 if (options & NOKEYS)
690 print_attribs(out, attrs, "Bag Attributes");
691 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
693 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
694 PKCS8_PRIV_KEY_INFO_free(p8);
697 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
698 PKCS8_PRIV_KEY_INFO_free(p8);
699 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
705 BIO_printf(bio_err, "Certificate bag\n");
706 if (options & NOCERTS)
708 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
709 if (options & CACERTS)
711 } else if (options & CLCERTS)
713 print_attribs(out, attrs, "Bag Attributes");
714 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
716 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
718 dump_cert_text(out, x509);
719 ret = PEM_write_bio_X509(out, x509);
723 case NID_safeContentsBag:
725 BIO_printf(bio_err, "Safe Contents bag\n");
726 print_attribs(out, attrs, "Bag Attributes");
727 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
728 pass, passlen, options, pempass, enc);
731 BIO_printf(bio_err, "Warning unsupported bag type: ");
732 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
733 BIO_printf(bio_err, "\n");
739 /* Given a single certificate return a verified chain or NULL if error */
741 static int get_cert_chain(X509 *cert, X509_STORE *store,
742 STACK_OF(X509) **chain)
744 X509_STORE_CTX *store_ctx = NULL;
745 STACK_OF(X509) *chn = NULL;
748 store_ctx = X509_STORE_CTX_new();
749 if (store_ctx == NULL) {
750 i = X509_V_ERR_UNSPECIFIED;
753 if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {
754 i = X509_V_ERR_UNSPECIFIED;
759 if (X509_verify_cert(store_ctx) > 0)
760 chn = X509_STORE_CTX_get1_chain(store_ctx);
761 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
762 i = X509_V_ERR_UNSPECIFIED;
765 X509_STORE_CTX_free(store_ctx);
770 static int alg_print(const X509_ALGOR *alg)
772 int pbenid, aparamtype;
773 const ASN1_OBJECT *aoid;
775 PBEPARAM *pbe = NULL;
777 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
779 pbenid = OBJ_obj2nid(aoid);
781 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
784 * If PBE algorithm is PBES2 decode algorithm parameters
785 * for additional details.
787 if (pbenid == NID_pbes2) {
788 PBE2PARAM *pbe2 = NULL;
790 if (aparamtype == V_ASN1_SEQUENCE)
791 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
793 BIO_puts(bio_err, ", <unsupported parameters>");
796 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
797 pbenid = OBJ_obj2nid(aoid);
798 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
799 encnid = OBJ_obj2nid(aoid);
800 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
802 /* If KDF is PBKDF2 decode parameters */
803 if (pbenid == NID_id_pbkdf2) {
804 PBKDF2PARAM *kdf = NULL;
806 if (aparamtype == V_ASN1_SEQUENCE)
807 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
809 BIO_puts(bio_err, ", <unsupported parameters>");
813 if (kdf->prf == NULL) {
814 prfnid = NID_hmacWithSHA1;
816 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
817 prfnid = OBJ_obj2nid(aoid);
819 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
820 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
821 PBKDF2PARAM_free(kdf);
822 #ifndef OPENSSL_NO_SCRYPT
823 } else if (pbenid == NID_id_scrypt) {
824 SCRYPT_PARAMS *kdf = NULL;
826 if (aparamtype == V_ASN1_SEQUENCE)
827 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
829 BIO_puts(bio_err, ", <unsupported parameters>");
832 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
833 "Block size(r): %ld, Paralelizm(p): %ld",
834 ASN1_STRING_length(kdf->salt),
835 ASN1_INTEGER_get(kdf->costParameter),
836 ASN1_INTEGER_get(kdf->blockSize),
837 ASN1_INTEGER_get(kdf->parallelizationParameter));
838 SCRYPT_PARAMS_free(kdf);
841 PBE2PARAM_free(pbe2);
843 if (aparamtype == V_ASN1_SEQUENCE)
844 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
846 BIO_puts(bio_err, ", <unsupported parameters>");
849 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
853 BIO_puts(bio_err, "\n");
857 /* Load all certificates from a given file */
859 int cert_load(BIO *in, STACK_OF(X509) *sk)
864 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
866 sk_X509_push(sk, cert);
873 /* Generalised attribute print: handle PKCS#8 and bag attributes */
875 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
878 X509_ATTRIBUTE *attr;
883 BIO_printf(out, "%s: <No Attributes>\n", name);
886 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
887 BIO_printf(out, "%s: <Empty Attributes>\n", name);
890 BIO_printf(out, "%s\n", name);
891 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
892 ASN1_OBJECT *attr_obj;
893 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
894 attr_obj = X509_ATTRIBUTE_get0_object(attr);
895 attr_nid = OBJ_obj2nid(attr_obj);
896 BIO_printf(out, " ");
897 if (attr_nid == NID_undef) {
898 i2a_ASN1_OBJECT(out, attr_obj);
899 BIO_printf(out, ": ");
901 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
904 if (X509_ATTRIBUTE_count(attr)) {
905 av = X509_ATTRIBUTE_get0_type(attr, 0);
907 case V_ASN1_BMPSTRING:
908 value = OPENSSL_uni2asc(av->value.bmpstring->data,
909 av->value.bmpstring->length);
910 BIO_printf(out, "%s\n", value);
914 case V_ASN1_OCTET_STRING:
915 hex_prin(out, av->value.octet_string->data,
916 av->value.octet_string->length);
917 BIO_printf(out, "\n");
920 case V_ASN1_BIT_STRING:
921 hex_prin(out, av->value.bit_string->data,
922 av->value.bit_string->length);
923 BIO_printf(out, "\n");
927 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
931 BIO_printf(out, "<No Values>\n");
937 void hex_prin(BIO *out, unsigned char *buf, int len)
940 for (i = 0; i < len; i++)
941 BIO_printf(out, "%02X ", buf[i]);
944 static int set_pbe(int *ppbe, const char *str)
948 if (strcmp(str, "NONE") == 0) {
952 *ppbe = OBJ_txt2nid(str);
953 if (*ppbe == NID_undef) {
954 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);