pkwrite.c 1.2 KB

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