1 /* pkwrite.c */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <openssl/pem.h> 6 #include <openssl/err.h> 7 #include <openssl/pkcs12.h> 8 9 /* Simple PKCS#12 file creator */ 10 11 int main(int argc, char **argv) 12 { 13 FILE *fp; 14 EVP_PKEY *pkey; 15 X509 *cert; 16 PKCS12 *p12; 17 if (argc != 5) { 18 fprintf(stderr, "Usage: pkwrite infile password name p12file\n"); 19 exit(1); 20 } 21 SSLeay_add_all_algorithms(); 22 ERR_load_crypto_strings(); 23 if (!(fp = fopen(argv[1], "r"))) { 24 fprintf(stderr, "Error opening file %s\n", argv[1]); 25 exit(1); 26 } 27 cert = PEM_read_X509(fp, NULL, NULL, NULL); 28 rewind(fp); 29 pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL); 30 fclose(fp); 31 p12 = PKCS12_create(argv[2], argv[3], pkey, cert, NULL, 0,0,0,0,0); 32 if(!p12) { 33 fprintf(stderr, "Error creating PKCS#12 structure\n"); 34 ERR_print_errors_fp(stderr); 35 exit(1); 36 } 37 if (!(fp = fopen(argv[4], "wb"))) { 38 fprintf(stderr, "Error opening file %s\n", argv[1]); 39 ERR_print_errors_fp(stderr); 40 exit(1); 41 } 42 i2d_PKCS12_fp(fp, p12); 43 PKCS12_free(p12); 44 fclose(fp); 45 return 0; 46 } 47