pkwrite.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/err.h>
  13. #include <openssl/pkcs12.h>
  14. /* Simple PKCS#12 file creator */
  15. int main(int argc, char **argv)
  16. {
  17. FILE *fp;
  18. EVP_PKEY *pkey;
  19. X509 *cert;
  20. PKCS12 *p12;
  21. if (argc != 5) {
  22. fprintf(stderr, "Usage: pkwrite infile password name p12file\n");
  23. exit(EXIT_FAILURE);
  24. }
  25. OpenSSL_add_all_algorithms();
  26. ERR_load_crypto_strings();
  27. if ((fp = fopen(argv[1], "r")) == NULL) {
  28. fprintf(stderr, "Error opening file %s\n", argv[1]);
  29. exit(EXIT_FAILURE);
  30. }
  31. cert = PEM_read_X509(fp, NULL, NULL, NULL);
  32. rewind(fp);
  33. pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
  34. fclose(fp);
  35. p12 = PKCS12_create(argv[2], argv[3], pkey, cert, NULL, 0, 0, 0, 0, 0);
  36. if (!p12) {
  37. fprintf(stderr, "Error creating PKCS#12 structure\n");
  38. ERR_print_errors_fp(stderr);
  39. exit(EXIT_FAILURE);
  40. }
  41. if ((fp = fopen(argv[4], "wb")) == NULL) {
  42. fprintf(stderr, "Error opening file %s\n", argv[4]);
  43. ERR_print_errors_fp(stderr);
  44. exit(EXIT_FAILURE);
  45. }
  46. i2d_PKCS12_fp(fp, p12);
  47. PKCS12_free(p12);
  48. fclose(fp);
  49. return EXIT_SUCCESS;
  50. }