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");
316 BIO_printf(bio_err
, "Option -twopass cannot be used with -passout or -password\n");
318 BIO_printf(bio_err
, "Option -twopass cannot be used with -passin or -password\n");
327 /* To avoid bit rot */
329 #ifndef OPENSSL_NO_UI_CONSOLE
330 if (EVP_read_pw_string(
331 macpass
, sizeof(macpass
), "Enter MAC Password:", export_cert
)) {
332 BIO_printf(bio_err
, "Can't read Password\n");
337 BIO_printf(bio_err
, "Unsupported option -twopass\n");
343 EVP_PKEY
*key
= NULL
;
344 X509
*ucert
= NULL
, *x
= NULL
;
345 STACK_OF(X509
) *certs
= NULL
;
346 const EVP_MD
*macmd
= NULL
;
347 unsigned char *catmp
= NULL
;
350 if ((options
& (NOCERTS
| NOKEYS
)) == (NOCERTS
| NOKEYS
)) {
351 BIO_printf(bio_err
, "Nothing to do!\n");
355 if (options
& NOCERTS
)
358 if (!(options
& NOKEYS
)) {
359 key
= load_key(keyname
? keyname
: infile
,
360 FORMAT_PEM
, 1, passin
, e
, "private key");
365 /* Load in all certs in input file */
366 if (!(options
& NOCERTS
)) {
367 if (!load_certs(infile
, &certs
, FORMAT_PEM
, NULL
,
372 /* Look for matching private key */
373 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
374 x
= sk_X509_value(certs
, i
);
375 if (X509_check_private_key(x
, key
)) {
377 /* Zero keyid and alias */
378 X509_keyid_set1(ucert
, NULL
, 0);
379 X509_alias_set1(ucert
, NULL
, 0);
380 /* Remove from list */
381 (void)sk_X509_delete(certs
, i
);
387 "No certificate matches private key\n");
394 /* Add any more certificates asked for */
395 if (certfile
!= NULL
) {
396 if (!load_certs(certfile
, &certs
, FORMAT_PEM
, NULL
,
397 "certificates from certfile"))
401 /* If chaining get chain from user cert */
404 STACK_OF(X509
) *chain2
;
406 if ((store
= setup_verify(CAfile
, CApath
, noCAfile
, noCApath
))
410 vret
= get_cert_chain(ucert
, store
, &chain2
);
411 X509_STORE_free(store
);
413 if (vret
== X509_V_OK
) {
414 /* Exclude verified certificate */
415 for (i
= 1; i
< sk_X509_num(chain2
); i
++)
416 sk_X509_push(certs
, sk_X509_value(chain2
, i
));
417 /* Free first certificate */
418 X509_free(sk_X509_value(chain2
, 0));
419 sk_X509_free(chain2
);
421 if (vret
!= X509_V_ERR_UNSPECIFIED
)
422 BIO_printf(bio_err
, "Error %s getting chain.\n",
423 X509_verify_cert_error_string(vret
));
425 ERR_print_errors(bio_err
);
430 /* Add any CA names */
432 for (i
= 0; i
< sk_OPENSSL_STRING_num(canames
); i
++) {
433 catmp
= (unsigned char *)sk_OPENSSL_STRING_value(canames
, i
);
434 X509_alias_set1(sk_X509_value(certs
, i
), catmp
, -1);
437 if (csp_name
!= NULL
&& key
!= NULL
)
438 EVP_PKEY_add1_attr_by_NID(key
, NID_ms_csp_name
,
439 MBSTRING_ASC
, (unsigned char *)csp_name
,
442 if (add_lmk
&& key
!= NULL
)
443 EVP_PKEY_add1_attr_by_NID(key
, NID_LocalKeySet
, 0, NULL
, -1);
446 /* To avoid bit rot */
448 #ifndef OPENSSL_NO_UI_CONSOLE
449 if (EVP_read_pw_string(pass
, sizeof(pass
),
450 "Enter Export Password:", 1)) {
451 BIO_printf(bio_err
, "Can't read Password\n");
456 BIO_printf(bio_err
, "Password required\n");
462 OPENSSL_strlcpy(macpass
, pass
, sizeof(macpass
));
464 p12
= PKCS12_create(cpass
, name
, key
, ucert
, certs
,
465 key_pbe
, cert_pbe
, iter
, -1, keytype
);
468 ERR_print_errors(bio_err
);
473 if (!opt_md(macalg
, &macmd
))
478 PKCS12_set_mac(p12
, mpass
, -1, NULL
, 0, maciter
, macmd
);
482 out
= bio_open_owner(outfile
, FORMAT_PKCS12
, private);
486 i2d_PKCS12_bio(out
, p12
);
493 sk_X509_pop_free(certs
, X509_free
);
500 in
= bio_open_default(infile
, 'r', FORMAT_PKCS12
);
503 out
= bio_open_owner(outfile
, FORMAT_PEM
, private);
507 if ((p12
= d2i_PKCS12_bio(in
, NULL
)) == NULL
) {
508 ERR_print_errors(bio_err
);
514 #ifndef OPENSSL_NO_UI_CONSOLE
515 if (EVP_read_pw_string(pass
, sizeof(pass
), "Enter Import Password:",
517 BIO_printf(bio_err
, "Can't read Password\n");
522 BIO_printf(bio_err
, "Password required\n");
528 OPENSSL_strlcpy(macpass
, pass
, sizeof(macpass
));
530 if ((options
& INFO
) && PKCS12_mac_present(p12
)) {
531 const ASN1_INTEGER
*tmaciter
;
532 const X509_ALGOR
*macalgid
;
533 const ASN1_OBJECT
*macobj
;
534 const ASN1_OCTET_STRING
*tmac
;
535 const ASN1_OCTET_STRING
*tsalt
;
537 PKCS12_get0_mac(&tmac
, &macalgid
, &tsalt
, &tmaciter
, p12
);
538 /* current hash algorithms do not use parameters so extract just name,
539 in future alg_print() may be needed */
540 X509_ALGOR_get0(&macobj
, NULL
, NULL
, macalgid
);
541 BIO_puts(bio_err
, "MAC: ");
542 i2a_ASN1_OBJECT(bio_err
, macobj
);
543 BIO_printf(bio_err
, ", Iteration %ld\n",
544 tmaciter
!= NULL
? ASN1_INTEGER_get(tmaciter
) : 1L);
545 BIO_printf(bio_err
, "MAC length: %ld, salt length: %ld\n",
546 tmac
!= NULL
? ASN1_STRING_length(tmac
) : 0L,
547 tsalt
!= NULL
? ASN1_STRING_length(tsalt
) : 0L);
550 /* If we enter empty password try no password first */
551 if (!mpass
[0] && PKCS12_verify_mac(p12
, NULL
, 0)) {
552 /* If mac and crypto pass the same set it to NULL too */
555 } else if (!PKCS12_verify_mac(p12
, mpass
, -1)) {
557 * May be UTF8 from previous version of OpenSSL:
558 * convert to a UTF8 form which will translate
559 * to the same Unicode password.
563 utmp
= OPENSSL_asc2uni(mpass
, -1, NULL
, &utmplen
);
566 badpass
= OPENSSL_uni2utf8(utmp
, utmplen
);
568 if (!PKCS12_verify_mac(p12
, badpass
, -1)) {
569 BIO_printf(bio_err
, "Mac verify error: invalid password?\n");
570 ERR_print_errors(bio_err
);
573 BIO_printf(bio_err
, "Warning: using broken algorithm\n");
581 if (!dump_certs_keys_p12(out
, p12
, cpass
, -1, options
, passout
, enc
)) {
582 BIO_printf(bio_err
, "Error outputting keys and certificates\n");
583 ERR_print_errors(bio_err
);
592 sk_OPENSSL_STRING_free(canames
);
593 OPENSSL_free(badpass
);
594 OPENSSL_free(passin
);
595 OPENSSL_free(passout
);
599 int dump_certs_keys_p12(BIO
*out
, const PKCS12
*p12
, const char *pass
,
600 int passlen
, int options
, char *pempass
,
601 const EVP_CIPHER
*enc
)
603 STACK_OF(PKCS7
) *asafes
= NULL
;
604 STACK_OF(PKCS12_SAFEBAG
) *bags
;
609 if ((asafes
= PKCS12_unpack_authsafes(p12
)) == NULL
)
611 for (i
= 0; i
< sk_PKCS7_num(asafes
); i
++) {
612 p7
= sk_PKCS7_value(asafes
, i
);
613 bagnid
= OBJ_obj2nid(p7
->type
);
614 if (bagnid
== NID_pkcs7_data
) {
615 bags
= PKCS12_unpack_p7data(p7
);
617 BIO_printf(bio_err
, "PKCS7 Data\n");
618 } else if (bagnid
== NID_pkcs7_encrypted
) {
619 if (options
& INFO
) {
620 BIO_printf(bio_err
, "PKCS7 Encrypted data: ");
621 alg_print(p7
->d
.encrypted
->enc_data
->algorithm
);
623 bags
= PKCS12_unpack_p7encdata(p7
, pass
, passlen
);
629 if (!dump_certs_pkeys_bags(out
, bags
, pass
, passlen
,
630 options
, pempass
, enc
)) {
631 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
634 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
640 sk_PKCS7_pop_free(asafes
, PKCS7_free
);
644 int dump_certs_pkeys_bags(BIO
*out
, const STACK_OF(PKCS12_SAFEBAG
) *bags
,
645 const char *pass
, int passlen
, int options
,
646 char *pempass
, const EVP_CIPHER
*enc
)
649 for (i
= 0; i
< sk_PKCS12_SAFEBAG_num(bags
); i
++) {
650 if (!dump_certs_pkeys_bag(out
,
651 sk_PKCS12_SAFEBAG_value(bags
, i
),
652 pass
, passlen
, options
, pempass
, enc
))
658 int dump_certs_pkeys_bag(BIO
*out
, const PKCS12_SAFEBAG
*bag
,
659 const char *pass
, int passlen
, int options
,
660 char *pempass
, const EVP_CIPHER
*enc
)
663 PKCS8_PRIV_KEY_INFO
*p8
;
664 const PKCS8_PRIV_KEY_INFO
*p8c
;
666 const STACK_OF(X509_ATTRIBUTE
) *attrs
;
669 attrs
= PKCS12_SAFEBAG_get0_attrs(bag
);
671 switch (PKCS12_SAFEBAG_get_nid(bag
)) {
674 BIO_printf(bio_err
, "Key bag\n");
675 if (options
& NOKEYS
)
677 print_attribs(out
, attrs
, "Bag Attributes");
678 p8c
= PKCS12_SAFEBAG_get0_p8inf(bag
);
679 if ((pkey
= EVP_PKCS82PKEY(p8c
)) == NULL
)
681 print_attribs(out
, PKCS8_pkey_get0_attrs(p8c
), "Key Attributes");
682 ret
= PEM_write_bio_PrivateKey(out
, pkey
, enc
, NULL
, 0, NULL
, pempass
);
686 case NID_pkcs8ShroudedKeyBag
:
687 if (options
& INFO
) {
689 const X509_ALGOR
*tp8alg
;
691 BIO_printf(bio_err
, "Shrouded Keybag: ");
692 tp8
= PKCS12_SAFEBAG_get0_pkcs8(bag
);
693 X509_SIG_get0(tp8
, &tp8alg
, NULL
);
696 if (options
& NOKEYS
)
698 print_attribs(out
, attrs
, "Bag Attributes");
699 if ((p8
= PKCS12_decrypt_skey(bag
, pass
, passlen
)) == NULL
)
701 if ((pkey
= EVP_PKCS82PKEY(p8
)) == NULL
) {
702 PKCS8_PRIV_KEY_INFO_free(p8
);
705 print_attribs(out
, PKCS8_pkey_get0_attrs(p8
), "Key Attributes");
706 PKCS8_PRIV_KEY_INFO_free(p8
);
707 ret
= PEM_write_bio_PrivateKey(out
, pkey
, enc
, NULL
, 0, NULL
, pempass
);
713 BIO_printf(bio_err
, "Certificate bag\n");
714 if (options
& NOCERTS
)
716 if (PKCS12_SAFEBAG_get0_attr(bag
, NID_localKeyID
)) {
717 if (options
& CACERTS
)
719 } else if (options
& CLCERTS
)
721 print_attribs(out
, attrs
, "Bag Attributes");
722 if (PKCS12_SAFEBAG_get_bag_nid(bag
) != NID_x509Certificate
)
724 if ((x509
= PKCS12_SAFEBAG_get1_cert(bag
)) == NULL
)
726 dump_cert_text(out
, x509
);
727 ret
= PEM_write_bio_X509(out
, x509
);
731 case NID_safeContentsBag
:
733 BIO_printf(bio_err
, "Safe Contents bag\n");
734 print_attribs(out
, attrs
, "Bag Attributes");
735 return dump_certs_pkeys_bags(out
, PKCS12_SAFEBAG_get0_safes(bag
),
736 pass
, passlen
, options
, pempass
, enc
);
739 BIO_printf(bio_err
, "Warning unsupported bag type: ");
740 i2a_ASN1_OBJECT(bio_err
, PKCS12_SAFEBAG_get0_type(bag
));
741 BIO_printf(bio_err
, "\n");
747 /* Given a single certificate return a verified chain or NULL if error */
749 static int get_cert_chain(X509
*cert
, X509_STORE
*store
,
750 STACK_OF(X509
) **chain
)
752 X509_STORE_CTX
*store_ctx
= NULL
;
753 STACK_OF(X509
) *chn
= NULL
;
756 store_ctx
= X509_STORE_CTX_new();
757 if (store_ctx
== NULL
) {
758 i
= X509_V_ERR_UNSPECIFIED
;
761 if (!X509_STORE_CTX_init(store_ctx
, store
, cert
, NULL
)) {
762 i
= X509_V_ERR_UNSPECIFIED
;
767 if (X509_verify_cert(store_ctx
) > 0)
768 chn
= X509_STORE_CTX_get1_chain(store_ctx
);
769 else if ((i
= X509_STORE_CTX_get_error(store_ctx
)) == 0)
770 i
= X509_V_ERR_UNSPECIFIED
;
773 X509_STORE_CTX_free(store_ctx
);
778 static int alg_print(const X509_ALGOR
*alg
)
780 int pbenid
, aparamtype
;
781 const ASN1_OBJECT
*aoid
;
783 PBEPARAM
*pbe
= NULL
;
785 X509_ALGOR_get0(&aoid
, &aparamtype
, &aparam
, alg
);
787 pbenid
= OBJ_obj2nid(aoid
);
789 BIO_printf(bio_err
, "%s", OBJ_nid2ln(pbenid
));
792 * If PBE algorithm is PBES2 decode algorithm parameters
793 * for additional details.
795 if (pbenid
== NID_pbes2
) {
796 PBE2PARAM
*pbe2
= NULL
;
798 if (aparamtype
== V_ASN1_SEQUENCE
)
799 pbe2
= ASN1_item_unpack(aparam
, ASN1_ITEM_rptr(PBE2PARAM
));
801 BIO_puts(bio_err
, ", <unsupported parameters>");
804 X509_ALGOR_get0(&aoid
, &aparamtype
, &aparam
, pbe2
->keyfunc
);
805 pbenid
= OBJ_obj2nid(aoid
);
806 X509_ALGOR_get0(&aoid
, NULL
, NULL
, pbe2
->encryption
);
807 encnid
= OBJ_obj2nid(aoid
);
808 BIO_printf(bio_err
, ", %s, %s", OBJ_nid2ln(pbenid
),
810 /* If KDF is PBKDF2 decode parameters */
811 if (pbenid
== NID_id_pbkdf2
) {
812 PBKDF2PARAM
*kdf
= NULL
;
814 if (aparamtype
== V_ASN1_SEQUENCE
)
815 kdf
= ASN1_item_unpack(aparam
, ASN1_ITEM_rptr(PBKDF2PARAM
));
817 BIO_puts(bio_err
, ", <unsupported parameters>");
821 if (kdf
->prf
== NULL
) {
822 prfnid
= NID_hmacWithSHA1
;
824 X509_ALGOR_get0(&aoid
, NULL
, NULL
, kdf
->prf
);
825 prfnid
= OBJ_obj2nid(aoid
);
827 BIO_printf(bio_err
, ", Iteration %ld, PRF %s",
828 ASN1_INTEGER_get(kdf
->iter
), OBJ_nid2sn(prfnid
));
829 PBKDF2PARAM_free(kdf
);
830 #ifndef OPENSSL_NO_SCRYPT
831 } else if (pbenid
== NID_id_scrypt
) {
832 SCRYPT_PARAMS
*kdf
= NULL
;
834 if (aparamtype
== V_ASN1_SEQUENCE
)
835 kdf
= ASN1_item_unpack(aparam
, ASN1_ITEM_rptr(SCRYPT_PARAMS
));
837 BIO_puts(bio_err
, ", <unsupported parameters>");
840 BIO_printf(bio_err
, ", Salt length: %d, Cost(N): %ld, "
841 "Block size(r): %ld, Paralelizm(p): %ld",
842 ASN1_STRING_length(kdf
->salt
),
843 ASN1_INTEGER_get(kdf
->costParameter
),
844 ASN1_INTEGER_get(kdf
->blockSize
),
845 ASN1_INTEGER_get(kdf
->parallelizationParameter
));
846 SCRYPT_PARAMS_free(kdf
);
849 PBE2PARAM_free(pbe2
);
851 if (aparamtype
== V_ASN1_SEQUENCE
)
852 pbe
= ASN1_item_unpack(aparam
, ASN1_ITEM_rptr(PBEPARAM
));
854 BIO_puts(bio_err
, ", <unsupported parameters>");
857 BIO_printf(bio_err
, ", Iteration %ld", ASN1_INTEGER_get(pbe
->iter
));
861 BIO_puts(bio_err
, "\n");
865 /* Load all certificates from a given file */
867 int cert_load(BIO
*in
, STACK_OF(X509
) *sk
)
872 while ((cert
= PEM_read_bio_X509(in
, NULL
, NULL
, NULL
))) {
874 sk_X509_push(sk
, cert
);
881 /* Generalised attribute print: handle PKCS#8 and bag attributes */
883 int print_attribs(BIO
*out
, const STACK_OF(X509_ATTRIBUTE
) *attrlst
,
886 X509_ATTRIBUTE
*attr
;
891 BIO_printf(out
, "%s: <No Attributes>\n", name
);
894 if (!sk_X509_ATTRIBUTE_num(attrlst
)) {
895 BIO_printf(out
, "%s: <Empty Attributes>\n", name
);
898 BIO_printf(out
, "%s\n", name
);
899 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(attrlst
); i
++) {
900 ASN1_OBJECT
*attr_obj
;
901 attr
= sk_X509_ATTRIBUTE_value(attrlst
, i
);
902 attr_obj
= X509_ATTRIBUTE_get0_object(attr
);
903 attr_nid
= OBJ_obj2nid(attr_obj
);
904 BIO_printf(out
, " ");
905 if (attr_nid
== NID_undef
) {
906 i2a_ASN1_OBJECT(out
, attr_obj
);
907 BIO_printf(out
, ": ");
909 BIO_printf(out
, "%s: ", OBJ_nid2ln(attr_nid
));
912 if (X509_ATTRIBUTE_count(attr
)) {
913 av
= X509_ATTRIBUTE_get0_type(attr
, 0);
915 case V_ASN1_BMPSTRING
:
916 value
= OPENSSL_uni2asc(av
->value
.bmpstring
->data
,
917 av
->value
.bmpstring
->length
);
918 BIO_printf(out
, "%s\n", value
);
922 case V_ASN1_OCTET_STRING
:
923 hex_prin(out
, av
->value
.octet_string
->data
,
924 av
->value
.octet_string
->length
);
925 BIO_printf(out
, "\n");
928 case V_ASN1_BIT_STRING
:
929 hex_prin(out
, av
->value
.bit_string
->data
,
930 av
->value
.bit_string
->length
);
931 BIO_printf(out
, "\n");
935 BIO_printf(out
, "<Unsupported tag %d>\n", av
->type
);
939 BIO_printf(out
, "<No Values>\n");
945 void hex_prin(BIO
*out
, unsigned char *buf
, int len
)
948 for (i
= 0; i
< len
; i
++)
949 BIO_printf(out
, "%02X ", buf
[i
]);
952 static int set_pbe(int *ppbe
, const char *str
)
956 if (strcmp(str
, "NONE") == 0) {
960 *ppbe
= OBJ_txt2nid(str
);
961 if (*ppbe
== NID_undef
) {
962 BIO_printf(bio_err
, "Unknown PBE algorithm %s\n", str
);