2 * Copyright 1999-2020 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>
16 #include <openssl/crypto.h>
17 #include <openssl/err.h>
18 #include <openssl/pem.h>
19 #include <openssl/pkcs12.h>
27 #define PASSWD_BUF_SIZE 2048
29 static int get_cert_chain(X509 *cert, X509_STORE *store,
30 STACK_OF(X509) **chain);
31 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
32 const char *pass, int passlen, int options,
33 char *pempass, const EVP_CIPHER *enc);
34 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
35 const char *pass, int passlen, int options,
36 char *pempass, const EVP_CIPHER *enc);
37 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
38 const char *pass, int passlen,
39 int options, char *pempass, const EVP_CIPHER *enc);
40 void print_attribute(BIO *out, const ASN1_TYPE *av);
41 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
43 void hex_prin(BIO *out, unsigned char *buf, int len);
44 static int alg_print(const X509_ALGOR *alg);
45 int cert_load(BIO *in, STACK_OF(X509) *sk);
46 static int set_pbe(int *ppbe, const char *str);
48 typedef enum OPTION_choice {
49 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
50 OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
51 OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
52 OPT_DESCERT, OPT_EXPORT, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
53 OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
54 OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME,
55 OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
56 OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_ENGINE,
60 const OPTIONS pkcs12_options[] = {
61 {"help", OPT_HELP, '-', "Display this summary"},
62 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
63 {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"},
64 {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"},
65 {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
66 {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
67 {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
68 {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"},
69 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
70 {"chain", OPT_CHAIN, '-', "Add certificate chain"},
71 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
72 {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
73 #ifndef OPENSSL_NO_RC2
74 {"descert", OPT_DESCERT, '-',
75 "Encrypt output with 3DES (default RC2-40)"},
76 {"certpbe", OPT_CERTPBE, 's',
77 "Certificate PBE algorithm (default RC2-40)"},
79 {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
80 {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
82 {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
83 {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
84 {"maciter", OPT_MACITER, '-', "Use MAC iteration"},
85 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
86 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
88 "Add local machine keyset attribute to private key"},
89 {"nodes", OPT_NODES, '-', "Don't encrypt private keys"},
90 {"macalg", OPT_MACALG, 's',
91 "Digest algorithm used in MAC (default SHA1)"},
92 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
94 {"inkey", OPT_INKEY, 's', "Private key if not infile"},
95 {"certfile", OPT_CERTFILE, '<', "Load certs from file"},
96 {"name", OPT_NAME, 's', "Use name as friendly name"},
97 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
98 {"caname", OPT_CANAME, 's',
99 "Use name as CA friendly name (can be repeated)"},
100 {"in", OPT_IN, '<', "Input filename"},
101 {"out", OPT_OUT, '>', "Output filename"},
102 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
103 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
104 {"password", OPT_PASSWORD, 's', "Set import/export password source"},
105 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
106 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
107 {"no-CAfile", OPT_NOCAFILE, '-',
108 "Do not load the default certificates file"},
109 {"no-CApath", OPT_NOCAPATH, '-',
110 "Do not load certificates from the default certificates directory"},
111 {"", OPT_CIPHER, '-', "Any supported cipher"},
112 #ifndef OPENSSL_NO_ENGINE
113 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
118 int pkcs12_main(int argc, char **argv)
120 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
121 char *name = NULL, *csp_name = NULL;
122 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
123 int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
124 int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
125 #ifndef OPENSSL_NO_RC2
126 int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
128 int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
130 int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
131 int ret = 1, macver = 1, add_lmk = 0, private = 0;
133 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
134 char *passin = NULL, *passout = NULL, *macalg = NULL;
135 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
136 const char *CApath = NULL, *CAfile = NULL, *prog;
137 int noCApath = 0, noCAfile = 0;
139 BIO *in = NULL, *out = NULL;
141 STACK_OF(OPENSSL_STRING) *canames = NULL;
142 const EVP_CIPHER *enc = EVP_des_ede3_cbc();
145 prog = opt_init(argc, argv, pkcs12_options);
146 while ((o = opt_next()) != OPT_EOF) {
151 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
154 opt_help(pkcs12_options);
176 options |= (NOKEYS | NOCERTS);
191 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
197 if (!opt_cipher(opt_unknown(), &enc))
204 maciter = PKCS12_DEFAULT_ITER;
219 if (!set_pbe(&cert_pbe, opt_arg()))
223 if (!set_pbe(&key_pbe, opt_arg()))
234 certfile = opt_arg();
243 csp_name = opt_arg();
247 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
249 sk_OPENSSL_STRING_push(canames, opt_arg());
258 passinarg = opt_arg();
261 passoutarg = opt_arg();
279 e = setup_engine(opt_arg(), 0);
283 argc = opt_num_rest();
289 if (passarg != NULL) {
291 passoutarg = passarg;
296 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
297 BIO_printf(bio_err, "Error getting passwords\n");
313 BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
315 BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
324 /* To avoid bit rot */
326 #ifndef OPENSSL_NO_UI_CONSOLE
327 if (EVP_read_pw_string(
328 macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) {
329 BIO_printf(bio_err, "Can't read Password\n");
334 BIO_printf(bio_err, "Unsupported option -twopass\n");
340 EVP_PKEY *key = NULL;
341 X509 *ucert = NULL, *x = NULL;
342 STACK_OF(X509) *certs = NULL;
343 const EVP_MD *macmd = NULL;
344 unsigned char *catmp = NULL;
347 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
348 BIO_printf(bio_err, "Nothing to do!\n");
352 if (options & NOCERTS)
355 if (!(options & NOKEYS)) {
356 key = load_key(keyname ? keyname : infile,
357 FORMAT_PEM, 1, passin, e, "private key");
362 /* Load in all certs in input file */
363 if (!(options & NOCERTS)) {
364 if (!load_certs(infile, &certs, FORMAT_PEM, NULL,
369 /* Look for matching private key */
370 for (i = 0; i < sk_X509_num(certs); i++) {
371 x = sk_X509_value(certs, i);
372 if (X509_check_private_key(x, key)) {
374 /* Zero keyid and alias */
375 X509_keyid_set1(ucert, NULL, 0);
376 X509_alias_set1(ucert, NULL, 0);
377 /* Remove from list */
378 (void)sk_X509_delete(certs, i);
384 "No certificate matches private key\n");
391 /* Add any more certificates asked for */
392 if (certfile != NULL) {
393 if (!load_certs(certfile, &certs, FORMAT_PEM, NULL,
394 "certificates from certfile"))
398 /* If chaining get chain from user cert */
401 STACK_OF(X509) *chain2;
403 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath))
407 vret = get_cert_chain(ucert, store, &chain2);
408 X509_STORE_free(store);
410 if (vret == X509_V_OK) {
411 /* Exclude verified certificate */
412 for (i = 1; i < sk_X509_num(chain2); i++)
413 sk_X509_push(certs, sk_X509_value(chain2, i));
414 /* Free first certificate */
415 X509_free(sk_X509_value(chain2, 0));
416 sk_X509_free(chain2);
418 if (vret != X509_V_ERR_UNSPECIFIED)
419 BIO_printf(bio_err, "Error %s getting chain.\n",
420 X509_verify_cert_error_string(vret));
422 ERR_print_errors(bio_err);
427 /* Add any CA names */
429 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
430 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
431 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
434 if (csp_name != NULL && key != NULL)
435 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
436 MBSTRING_ASC, (unsigned char *)csp_name,
439 if (add_lmk && key != NULL)
440 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
443 /* To avoid bit rot */
445 #ifndef OPENSSL_NO_UI_CONSOLE
446 if (EVP_read_pw_string(pass, sizeof(pass),
447 "Enter Export Password:", 1)) {
448 BIO_printf(bio_err, "Can't read Password\n");
453 BIO_printf(bio_err, "Password required\n");
459 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
461 p12 = PKCS12_create(cpass, name, key, ucert, certs,
462 key_pbe, cert_pbe, iter, -1, keytype);
465 ERR_print_errors(bio_err);
470 if (!opt_md(macalg, &macmd))
475 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
479 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
483 i2d_PKCS12_bio(out, p12);
490 sk_X509_pop_free(certs, X509_free);
497 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
500 out = bio_open_owner(outfile, FORMAT_PEM, private);
504 if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
505 ERR_print_errors(bio_err);
511 #ifndef OPENSSL_NO_UI_CONSOLE
512 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
514 BIO_printf(bio_err, "Can't read Password\n");
519 BIO_printf(bio_err, "Password required\n");
525 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
527 if ((options & INFO) && PKCS12_mac_present(p12)) {
528 const ASN1_INTEGER *tmaciter;
529 const X509_ALGOR *macalgid;
530 const ASN1_OBJECT *macobj;
531 const ASN1_OCTET_STRING *tmac;
532 const ASN1_OCTET_STRING *tsalt;
534 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
535 /* current hash algorithms do not use parameters so extract just name,
536 in future alg_print() may be needed */
537 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
538 BIO_puts(bio_err, "MAC: ");
539 i2a_ASN1_OBJECT(bio_err, macobj);
540 BIO_printf(bio_err, ", Iteration %ld\n",
541 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
542 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
543 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
544 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
547 /* If we enter empty password try no password first */
548 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
549 /* If mac and crypto pass the same set it to NULL too */
552 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
554 * May be UTF8 from previous version of OpenSSL:
555 * convert to a UTF8 form which will translate
556 * to the same Unicode password.
560 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
563 badpass = OPENSSL_uni2utf8(utmp, utmplen);
565 if (!PKCS12_verify_mac(p12, badpass, -1)) {
566 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
567 ERR_print_errors(bio_err);
570 BIO_printf(bio_err, "Warning: using broken algorithm\n");
578 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
579 BIO_printf(bio_err, "Error outputting keys and certificates\n");
580 ERR_print_errors(bio_err);
589 sk_OPENSSL_STRING_free(canames);
590 OPENSSL_free(badpass);
591 OPENSSL_free(passin);
592 OPENSSL_free(passout);
596 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
597 int passlen, int options, char *pempass,
598 const EVP_CIPHER *enc)
600 STACK_OF(PKCS7) *asafes = NULL;
601 STACK_OF(PKCS12_SAFEBAG) *bags;
606 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
608 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
609 p7 = sk_PKCS7_value(asafes, i);
610 bagnid = OBJ_obj2nid(p7->type);
611 if (bagnid == NID_pkcs7_data) {
612 bags = PKCS12_unpack_p7data(p7);
614 BIO_printf(bio_err, "PKCS7 Data\n");
615 } else if (bagnid == NID_pkcs7_encrypted) {
616 if (options & INFO) {
617 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
618 alg_print(p7->d.encrypted->enc_data->algorithm);
620 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
626 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
627 options, pempass, enc)) {
628 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
631 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
637 sk_PKCS7_pop_free(asafes, PKCS7_free);
641 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
642 const char *pass, int passlen, int options,
643 char *pempass, const EVP_CIPHER *enc)
646 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
647 if (!dump_certs_pkeys_bag(out,
648 sk_PKCS12_SAFEBAG_value(bags, i),
649 pass, passlen, options, pempass, enc))
655 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
656 const char *pass, int passlen, int options,
657 char *pempass, const EVP_CIPHER *enc)
660 PKCS8_PRIV_KEY_INFO *p8;
661 const PKCS8_PRIV_KEY_INFO *p8c;
663 const STACK_OF(X509_ATTRIBUTE) *attrs;
666 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
668 switch (PKCS12_SAFEBAG_get_nid(bag)) {
671 BIO_printf(bio_err, "Key bag\n");
672 if (options & NOKEYS)
674 print_attribs(out, attrs, "Bag Attributes");
675 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
676 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
678 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
679 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
683 case NID_pkcs8ShroudedKeyBag:
684 if (options & INFO) {
686 const X509_ALGOR *tp8alg;
688 BIO_printf(bio_err, "Shrouded Keybag: ");
689 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
690 X509_SIG_get0(tp8, &tp8alg, NULL);
693 if (options & NOKEYS)
695 print_attribs(out, attrs, "Bag Attributes");
696 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
698 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
699 PKCS8_PRIV_KEY_INFO_free(p8);
702 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
703 PKCS8_PRIV_KEY_INFO_free(p8);
704 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
710 BIO_printf(bio_err, "Certificate bag\n");
711 if (options & NOCERTS)
713 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
714 if (options & CACERTS)
716 } else if (options & CLCERTS)
718 print_attribs(out, attrs, "Bag Attributes");
719 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
721 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
723 dump_cert_text(out, x509);
724 ret = PEM_write_bio_X509(out, x509);
728 case NID_safeContentsBag:
730 BIO_printf(bio_err, "Safe Contents bag\n");
731 print_attribs(out, attrs, "Bag Attributes");
732 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
733 pass, passlen, options, pempass, enc);
736 BIO_printf(bio_err, "Warning unsupported bag type: ");
737 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
738 BIO_printf(bio_err, "\n");
744 /* Given a single certificate return a verified chain or NULL if error */
746 static int get_cert_chain(X509 *cert, X509_STORE *store,
747 STACK_OF(X509) **chain)
749 X509_STORE_CTX *store_ctx = NULL;
750 STACK_OF(X509) *chn = NULL;
753 store_ctx = X509_STORE_CTX_new();
754 if (store_ctx == NULL) {
755 i = X509_V_ERR_UNSPECIFIED;
758 if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {
759 i = X509_V_ERR_UNSPECIFIED;
764 if (X509_verify_cert(store_ctx) > 0)
765 chn = X509_STORE_CTX_get1_chain(store_ctx);
766 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
767 i = X509_V_ERR_UNSPECIFIED;
770 X509_STORE_CTX_free(store_ctx);
775 static int alg_print(const X509_ALGOR *alg)
777 int pbenid, aparamtype;
778 const ASN1_OBJECT *aoid;
780 PBEPARAM *pbe = NULL;
782 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
784 pbenid = OBJ_obj2nid(aoid);
786 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
789 * If PBE algorithm is PBES2 decode algorithm parameters
790 * for additional details.
792 if (pbenid == NID_pbes2) {
793 PBE2PARAM *pbe2 = NULL;
795 if (aparamtype == V_ASN1_SEQUENCE)
796 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
798 BIO_puts(bio_err, ", <unsupported parameters>");
801 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
802 pbenid = OBJ_obj2nid(aoid);
803 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
804 encnid = OBJ_obj2nid(aoid);
805 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
807 /* If KDF is PBKDF2 decode parameters */
808 if (pbenid == NID_id_pbkdf2) {
809 PBKDF2PARAM *kdf = NULL;
811 if (aparamtype == V_ASN1_SEQUENCE)
812 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
814 BIO_puts(bio_err, ", <unsupported parameters>");
818 if (kdf->prf == NULL) {
819 prfnid = NID_hmacWithSHA1;
821 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
822 prfnid = OBJ_obj2nid(aoid);
824 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
825 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
826 PBKDF2PARAM_free(kdf);
827 #ifndef OPENSSL_NO_SCRYPT
828 } else if (pbenid == NID_id_scrypt) {
829 SCRYPT_PARAMS *kdf = NULL;
831 if (aparamtype == V_ASN1_SEQUENCE)
832 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
834 BIO_puts(bio_err, ", <unsupported parameters>");
837 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
838 "Block size(r): %ld, Parallelism(p): %ld",
839 ASN1_STRING_length(kdf->salt),
840 ASN1_INTEGER_get(kdf->costParameter),
841 ASN1_INTEGER_get(kdf->blockSize),
842 ASN1_INTEGER_get(kdf->parallelizationParameter));
843 SCRYPT_PARAMS_free(kdf);
846 PBE2PARAM_free(pbe2);
848 if (aparamtype == V_ASN1_SEQUENCE)
849 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
851 BIO_puts(bio_err, ", <unsupported parameters>");
854 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
858 BIO_puts(bio_err, "\n");
862 /* Load all certificates from a given file */
864 int cert_load(BIO *in, STACK_OF(X509) *sk)
869 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
871 sk_X509_push(sk, cert);
878 /* Generalised x509 attribute value print */
880 void print_attribute(BIO *out, const ASN1_TYPE *av)
885 case V_ASN1_BMPSTRING:
886 value = OPENSSL_uni2asc(av->value.bmpstring->data,
887 av->value.bmpstring->length);
888 BIO_printf(out, "%s\n", value);
892 case V_ASN1_OCTET_STRING:
893 hex_prin(out, av->value.octet_string->data,
894 av->value.octet_string->length);
895 BIO_printf(out, "\n");
898 case V_ASN1_BIT_STRING:
899 hex_prin(out, av->value.bit_string->data,
900 av->value.bit_string->length);
901 BIO_printf(out, "\n");
905 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
910 /* Generalised attribute print: handle PKCS#8 and bag attributes */
912 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
915 X509_ATTRIBUTE *attr;
919 BIO_printf(out, "%s: <No Attributes>\n", name);
922 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
923 BIO_printf(out, "%s: <Empty Attributes>\n", name);
926 BIO_printf(out, "%s\n", name);
927 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
928 ASN1_OBJECT *attr_obj;
929 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
930 attr_obj = X509_ATTRIBUTE_get0_object(attr);
931 attr_nid = OBJ_obj2nid(attr_obj);
932 BIO_printf(out, " ");
933 if (attr_nid == NID_undef) {
934 i2a_ASN1_OBJECT(out, attr_obj);
935 BIO_printf(out, ": ");
937 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
940 if (X509_ATTRIBUTE_count(attr)) {
941 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
943 av = X509_ATTRIBUTE_get0_type(attr, j);
944 print_attribute(out, av);
947 BIO_printf(out, "<No Values>\n");
953 void hex_prin(BIO *out, unsigned char *buf, int len)
956 for (i = 0; i < len; i++)
957 BIO_printf(out, "%02X ", buf[i]);
960 static int set_pbe(int *ppbe, const char *str)
964 if (strcmp(str, "NONE") == 0) {
968 *ppbe = OBJ_txt2nid(str);
969 if (*ppbe == NID_undef) {
970 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);