4 #include <openssl/pem.h>
5 #include <openssl/err.h>
6 #include <openssl/pkcs12.h>
8 /* Simple PKCS#12 file reader */
10 int main(int argc, char **argv)
15 STACK_OF(X509) *ca = NULL;
19 fprintf(stderr, "Usage: pkread p12file password opfile\n");
22 OpenSSL_add_all_algorithms();
23 ERR_load_crypto_strings();
24 if ((fp = fopen(argv[1], "rb")) == NULL) {
25 fprintf(stderr, "Error opening file %s\n", argv[1]);
28 p12 = d2i_PKCS12_fp(fp, NULL);
31 fprintf(stderr, "Error reading PKCS#12 file\n");
32 ERR_print_errors_fp(stderr);
35 if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) {
36 fprintf(stderr, "Error parsing PKCS#12 file\n");
37 ERR_print_errors_fp(stderr);
41 if ((fp = fopen(argv[3], "w")) == NULL) {
42 fprintf(stderr, "Error opening file %s\n", argv[1]);
46 fprintf(fp, "***Private Key***\n");
47 PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
50 fprintf(fp, "***User Certificate***\n");
51 PEM_write_X509_AUX(fp, cert);
53 if (ca && sk_X509_num(ca)) {
54 fprintf(fp, "***Other Certificates***\n");
55 for (i = 0; i < sk_X509_num(ca); i++)
56 PEM_write_X509_AUX(fp, sk_X509_value(ca, i));