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_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,
64 const OPTIONS pkcs12_options[] = {
65 {"help", OPT_HELP, '-', "Display this summary"},
66 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
67 {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"},
68 {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"},
69 {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
70 {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
71 {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
72 {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"},
73 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
74 {"chain", OPT_CHAIN, '-', "Add certificate chain"},
75 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
76 {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
77 # ifndef OPENSSL_NO_RC2
78 {"descert", OPT_DESCERT, '-',
79 "Encrypt output with 3DES (default RC2-40)"},
80 {"certpbe", OPT_CERTPBE, 's',
81 "Certificate PBE algorithm (default RC2-40)"},
83 {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
84 {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
86 {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
87 {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
88 {"maciter", OPT_MACITER, '-', "Use MAC iteration"},
89 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
90 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
92 "Add local machine keyset attribute to private key"},
93 {"nodes", OPT_NODES, '-', "Don't encrypt private keys"},
94 {"macalg", OPT_MACALG, 's',
95 "Digest algorithm used in MAC (default SHA1)"},
96 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
98 {"inkey", OPT_INKEY, 's', "Private key if not infile"},
99 {"certfile", OPT_CERTFILE, '<', "Load certs from file"},
100 {"name", OPT_NAME, 's', "Use name as friendly name"},
101 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
102 {"caname", OPT_CANAME, 's',
103 "Use name as CA friendly name (can be repeated)"},
104 {"in", OPT_IN, '<', "Input filename"},
105 {"out", OPT_OUT, '>', "Output filename"},
106 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
107 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
108 {"password", OPT_PASSWORD, 's', "Set import/export password source"},
109 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
110 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
111 {"CAstore", OPT_CASTORE, ':', "URI to store if CA's"},
112 {"no-CAfile", OPT_NOCAFILE, '-',
113 "Do not load the default certificates file"},
114 {"no-CApath", OPT_NOCAPATH, '-',
115 "Do not load certificates from the default certificates directory"},
116 {"no-CAstore", OPT_NOCASTORE, '-',
117 "Do not load certificates from the default certificates store"},
118 {"", OPT_CIPHER, '-', "Any supported cipher"},
119 # ifndef OPENSSL_NO_ENGINE
120 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
125 int pkcs12_main(int argc, char **argv)
127 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
128 char *name = NULL, *csp_name = NULL;
129 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
130 int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
131 int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
132 # ifndef OPENSSL_NO_RC2
133 int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
135 int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
137 int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
138 int ret = 1, macver = 1, add_lmk = 0, private = 0;
140 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
141 char *passin = NULL, *passout = NULL, *macalg = NULL;
142 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
143 const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog;
144 int noCApath = 0, noCAfile = 0, noCAstore = 0;
146 BIO *in = NULL, *out = NULL;
148 STACK_OF(OPENSSL_STRING) *canames = NULL;
149 const EVP_CIPHER *enc = EVP_des_ede3_cbc();
152 prog = opt_init(argc, argv, pkcs12_options);
153 while ((o = opt_next()) != OPT_EOF) {
158 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
161 opt_help(pkcs12_options);
183 options |= (NOKEYS | NOCERTS);
198 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
204 if (!opt_cipher(opt_unknown(), &enc))
211 maciter = PKCS12_DEFAULT_ITER;
226 if (!set_pbe(&cert_pbe, opt_arg()))
230 if (!set_pbe(&key_pbe, opt_arg()))
241 certfile = opt_arg();
250 csp_name = opt_arg();
254 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
256 sk_OPENSSL_STRING_push(canames, opt_arg());
265 passinarg = opt_arg();
268 passoutarg = opt_arg();
292 e = setup_engine(opt_arg(), 0);
296 argc = opt_num_rest();
302 if (passarg != NULL) {
304 passoutarg = passarg;
309 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
310 BIO_printf(bio_err, "Error getting passwords\n");
326 BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
328 BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
337 /* To avoid bit rot */
339 #ifndef OPENSSL_NO_UI_CONSOLE
340 if (EVP_read_pw_string(
341 macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) {
342 BIO_printf(bio_err, "Can't read Password\n");
347 BIO_printf(bio_err, "Unsupported option -twopass\n");
353 EVP_PKEY *key = NULL;
354 X509 *ucert = NULL, *x = NULL;
355 STACK_OF(X509) *certs = NULL;
356 const EVP_MD *macmd = NULL;
357 unsigned char *catmp = NULL;
360 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
361 BIO_printf(bio_err, "Nothing to do!\n");
365 if (options & NOCERTS)
368 if (!(options & NOKEYS)) {
369 key = load_key(keyname ? keyname : infile,
370 FORMAT_PEM, 1, passin, e, "private key");
375 /* Load in all certs in input file */
376 if (!(options & NOCERTS)) {
377 if (!load_certs(infile, &certs, FORMAT_PEM, NULL,
382 /* Look for matching private key */
383 for (i = 0; i < sk_X509_num(certs); i++) {
384 x = sk_X509_value(certs, i);
385 if (X509_check_private_key(x, key)) {
387 /* Zero keyid and alias */
388 X509_keyid_set1(ucert, NULL, 0);
389 X509_alias_set1(ucert, NULL, 0);
390 /* Remove from list */
391 (void)sk_X509_delete(certs, i);
397 "No certificate matches private key\n");
404 /* Add any more certificates asked for */
405 if (certfile != NULL) {
406 if (!load_certs(certfile, &certs, FORMAT_PEM, NULL,
407 "certificates from certfile"))
411 /* If chaining get chain from user cert */
414 STACK_OF(X509) *chain2;
416 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
421 vret = get_cert_chain(ucert, store, &chain2);
422 X509_STORE_free(store);
424 if (vret == X509_V_OK) {
425 /* Exclude verified certificate */
426 for (i = 1; i < sk_X509_num(chain2); i++)
427 sk_X509_push(certs, sk_X509_value(chain2, i));
428 /* Free first certificate */
429 X509_free(sk_X509_value(chain2, 0));
430 sk_X509_free(chain2);
432 if (vret != X509_V_ERR_UNSPECIFIED)
433 BIO_printf(bio_err, "Error %s getting chain.\n",
434 X509_verify_cert_error_string(vret));
436 ERR_print_errors(bio_err);
441 /* Add any CA names */
443 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
444 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
445 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
448 if (csp_name != NULL && key != NULL)
449 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
450 MBSTRING_ASC, (unsigned char *)csp_name,
453 if (add_lmk && key != NULL)
454 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
457 /* To avoid bit rot */
459 #ifndef OPENSSL_NO_UI_CONSOLE
460 if (EVP_read_pw_string(pass, sizeof(pass),
461 "Enter Export Password:", 1)) {
462 BIO_printf(bio_err, "Can't read Password\n");
467 BIO_printf(bio_err, "Password required\n");
473 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
475 p12 = PKCS12_create(cpass, name, key, ucert, certs,
476 key_pbe, cert_pbe, iter, -1, keytype);
479 ERR_print_errors(bio_err);
484 if (!opt_md(macalg, &macmd))
489 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
493 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
497 i2d_PKCS12_bio(out, p12);
504 sk_X509_pop_free(certs, X509_free);
511 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
514 out = bio_open_owner(outfile, FORMAT_PEM, private);
518 if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
519 ERR_print_errors(bio_err);
525 #ifndef OPENSSL_NO_UI_CONSOLE
526 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
528 BIO_printf(bio_err, "Can't read Password\n");
533 BIO_printf(bio_err, "Password required\n");
539 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
541 if ((options & INFO) && PKCS12_mac_present(p12)) {
542 const ASN1_INTEGER *tmaciter;
543 const X509_ALGOR *macalgid;
544 const ASN1_OBJECT *macobj;
545 const ASN1_OCTET_STRING *tmac;
546 const ASN1_OCTET_STRING *tsalt;
548 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
549 /* current hash algorithms do not use parameters so extract just name,
550 in future alg_print() may be needed */
551 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
552 BIO_puts(bio_err, "MAC: ");
553 i2a_ASN1_OBJECT(bio_err, macobj);
554 BIO_printf(bio_err, ", Iteration %ld\n",
555 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
556 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
557 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
558 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
561 /* If we enter empty password try no password first */
562 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
563 /* If mac and crypto pass the same set it to NULL too */
566 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
568 * May be UTF8 from previous version of OpenSSL:
569 * convert to a UTF8 form which will translate
570 * to the same Unicode password.
574 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
577 badpass = OPENSSL_uni2utf8(utmp, utmplen);
579 if (!PKCS12_verify_mac(p12, badpass, -1)) {
580 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
581 ERR_print_errors(bio_err);
584 BIO_printf(bio_err, "Warning: using broken algorithm\n");
592 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
593 BIO_printf(bio_err, "Error outputting keys and certificates\n");
594 ERR_print_errors(bio_err);
603 sk_OPENSSL_STRING_free(canames);
604 OPENSSL_free(badpass);
605 OPENSSL_free(passin);
606 OPENSSL_free(passout);
610 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
611 int passlen, int options, char *pempass,
612 const EVP_CIPHER *enc)
614 STACK_OF(PKCS7) *asafes = NULL;
615 STACK_OF(PKCS12_SAFEBAG) *bags;
620 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
622 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
623 p7 = sk_PKCS7_value(asafes, i);
624 bagnid = OBJ_obj2nid(p7->type);
625 if (bagnid == NID_pkcs7_data) {
626 bags = PKCS12_unpack_p7data(p7);
628 BIO_printf(bio_err, "PKCS7 Data\n");
629 } else if (bagnid == NID_pkcs7_encrypted) {
630 if (options & INFO) {
631 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
632 alg_print(p7->d.encrypted->enc_data->algorithm);
634 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
640 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
641 options, pempass, enc)) {
642 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
645 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
651 sk_PKCS7_pop_free(asafes, PKCS7_free);
655 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
656 const char *pass, int passlen, int options,
657 char *pempass, const EVP_CIPHER *enc)
660 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
661 if (!dump_certs_pkeys_bag(out,
662 sk_PKCS12_SAFEBAG_value(bags, i),
663 pass, passlen, options, pempass, enc))
669 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
670 const char *pass, int passlen, int options,
671 char *pempass, const EVP_CIPHER *enc)
674 PKCS8_PRIV_KEY_INFO *p8;
675 const PKCS8_PRIV_KEY_INFO *p8c;
677 const STACK_OF(X509_ATTRIBUTE) *attrs;
680 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
682 switch (PKCS12_SAFEBAG_get_nid(bag)) {
685 BIO_printf(bio_err, "Key bag\n");
686 if (options & NOKEYS)
688 print_attribs(out, attrs, "Bag Attributes");
689 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
690 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
692 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
693 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
697 case NID_pkcs8ShroudedKeyBag:
698 if (options & INFO) {
700 const X509_ALGOR *tp8alg;
702 BIO_printf(bio_err, "Shrouded Keybag: ");
703 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
704 X509_SIG_get0(tp8, &tp8alg, NULL);
707 if (options & NOKEYS)
709 print_attribs(out, attrs, "Bag Attributes");
710 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
712 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
713 PKCS8_PRIV_KEY_INFO_free(p8);
716 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
717 PKCS8_PRIV_KEY_INFO_free(p8);
718 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
724 BIO_printf(bio_err, "Certificate bag\n");
725 if (options & NOCERTS)
727 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
728 if (options & CACERTS)
730 } else if (options & CLCERTS)
732 print_attribs(out, attrs, "Bag Attributes");
733 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
735 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
737 dump_cert_text(out, x509);
738 ret = PEM_write_bio_X509(out, x509);
742 case NID_safeContentsBag:
744 BIO_printf(bio_err, "Safe Contents bag\n");
745 print_attribs(out, attrs, "Bag Attributes");
746 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
747 pass, passlen, options, pempass, enc);
750 BIO_printf(bio_err, "Warning unsupported bag type: ");
751 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
752 BIO_printf(bio_err, "\n");
758 /* Given a single certificate return a verified chain or NULL if error */
760 static int get_cert_chain(X509 *cert, X509_STORE *store,
761 STACK_OF(X509) **chain)
763 X509_STORE_CTX *store_ctx = NULL;
764 STACK_OF(X509) *chn = NULL;
767 store_ctx = X509_STORE_CTX_new();
768 if (store_ctx == NULL) {
769 i = X509_V_ERR_UNSPECIFIED;
772 if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {
773 i = X509_V_ERR_UNSPECIFIED;
778 if (X509_verify_cert(store_ctx) > 0)
779 chn = X509_STORE_CTX_get1_chain(store_ctx);
780 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
781 i = X509_V_ERR_UNSPECIFIED;
784 X509_STORE_CTX_free(store_ctx);
789 static int alg_print(const X509_ALGOR *alg)
791 int pbenid, aparamtype;
792 const ASN1_OBJECT *aoid;
794 PBEPARAM *pbe = NULL;
796 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
798 pbenid = OBJ_obj2nid(aoid);
800 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
803 * If PBE algorithm is PBES2 decode algorithm parameters
804 * for additional details.
806 if (pbenid == NID_pbes2) {
807 PBE2PARAM *pbe2 = NULL;
809 if (aparamtype == V_ASN1_SEQUENCE)
810 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
812 BIO_puts(bio_err, ", <unsupported parameters>");
815 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
816 pbenid = OBJ_obj2nid(aoid);
817 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
818 encnid = OBJ_obj2nid(aoid);
819 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
821 /* If KDF is PBKDF2 decode parameters */
822 if (pbenid == NID_id_pbkdf2) {
823 PBKDF2PARAM *kdf = NULL;
825 if (aparamtype == V_ASN1_SEQUENCE)
826 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
828 BIO_puts(bio_err, ", <unsupported parameters>");
832 if (kdf->prf == NULL) {
833 prfnid = NID_hmacWithSHA1;
835 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
836 prfnid = OBJ_obj2nid(aoid);
838 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
839 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
840 PBKDF2PARAM_free(kdf);
841 #ifndef OPENSSL_NO_SCRYPT
842 } else if (pbenid == NID_id_scrypt) {
843 SCRYPT_PARAMS *kdf = NULL;
845 if (aparamtype == V_ASN1_SEQUENCE)
846 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
848 BIO_puts(bio_err, ", <unsupported parameters>");
851 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
852 "Block size(r): %ld, Parallelism(p): %ld",
853 ASN1_STRING_length(kdf->salt),
854 ASN1_INTEGER_get(kdf->costParameter),
855 ASN1_INTEGER_get(kdf->blockSize),
856 ASN1_INTEGER_get(kdf->parallelizationParameter));
857 SCRYPT_PARAMS_free(kdf);
860 PBE2PARAM_free(pbe2);
862 if (aparamtype == V_ASN1_SEQUENCE)
863 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
865 BIO_puts(bio_err, ", <unsupported parameters>");
868 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
872 BIO_puts(bio_err, "\n");
876 /* Load all certificates from a given file */
878 int cert_load(BIO *in, STACK_OF(X509) *sk)
883 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
885 sk_X509_push(sk, cert);
892 /* Generalised x509 attribute value print */
894 void print_attribute(BIO *out, const ASN1_TYPE *av)
899 case V_ASN1_BMPSTRING:
900 value = OPENSSL_uni2asc(av->value.bmpstring->data,
901 av->value.bmpstring->length);
902 BIO_printf(out, "%s\n", value);
906 case V_ASN1_OCTET_STRING:
907 hex_prin(out, av->value.octet_string->data,
908 av->value.octet_string->length);
909 BIO_printf(out, "\n");
912 case V_ASN1_BIT_STRING:
913 hex_prin(out, av->value.bit_string->data,
914 av->value.bit_string->length);
915 BIO_printf(out, "\n");
919 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
924 /* Generalised attribute print: handle PKCS#8 and bag attributes */
926 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
929 X509_ATTRIBUTE *attr;
933 BIO_printf(out, "%s: <No Attributes>\n", name);
936 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
937 BIO_printf(out, "%s: <Empty Attributes>\n", name);
940 BIO_printf(out, "%s\n", name);
941 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
942 ASN1_OBJECT *attr_obj;
943 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
944 attr_obj = X509_ATTRIBUTE_get0_object(attr);
945 attr_nid = OBJ_obj2nid(attr_obj);
946 BIO_printf(out, " ");
947 if (attr_nid == NID_undef) {
948 i2a_ASN1_OBJECT(out, attr_obj);
949 BIO_printf(out, ": ");
951 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
954 if (X509_ATTRIBUTE_count(attr)) {
955 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
957 av = X509_ATTRIBUTE_get0_type(attr, j);
958 print_attribute(out, av);
961 BIO_printf(out, "<No Values>\n");
967 void hex_prin(BIO *out, unsigned char *buf, int len)
970 for (i = 0; i < len; i++)
971 BIO_printf(out, "%02X ", buf[i]);
974 static int set_pbe(int *ppbe, const char *str)
978 if (strcmp(str, "NONE") == 0) {
982 *ppbe = OBJ_txt2nid(str);
983 if (*ppbe == NID_undef) {
984 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);