5 #include <openssl/pem.h>
6 #include <openssl/err.h>
7 #include <openssl/pkcs12.h>
9 /* Simple PKCS#12 file reader */
11 int main(int argc
, char **argv
)
16 STACK_OF(X509
) *ca
= NULL
;
20 fprintf(stderr
, "Usage: pkread p12file password opfile\n");
23 SSLeay_add_all_algorithms();
24 ERR_load_crypto_strings();
25 if (!(fp
= fopen(argv
[1], "rb"))) {
26 fprintf(stderr
, "Error opening file %s\n", argv
[1]);
29 p12
= d2i_PKCS12_fp(fp
, NULL
);
32 fprintf(stderr
, "Error reading PKCS#12 file\n");
33 ERR_print_errors_fp(stderr
);
36 if (!PKCS12_parse(p12
, argv
[2], &pkey
, &cert
, &ca
)) {
37 fprintf(stderr
, "Error parsing PKCS#12 file\n");
38 ERR_print_errors_fp(stderr
);
42 if (!(fp
= fopen(argv
[3], "w"))) {
43 fprintf(stderr
, "Error opening file %s\n", argv
[1]);
47 fprintf(fp
, "***Private Key***\n");
48 PEM_write_PrivateKey(fp
, pkey
, NULL
, NULL
, 0, NULL
, NULL
);
51 fprintf(fp
, "***User Certificate***\n");
52 PEM_write_X509_AUX(fp
, cert
);
54 if (ca
&& sk_num(ca
)) {
55 fprintf(fp
, "***Other Certificates***\n");
56 for (i
= 0; i
< sk_X509_num(ca
); i
++)
57 PEM_write_X509_AUX(fp
, sk_X509_value(ca
, i
));