pkread.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 reader */
  15. static char *find_friendly_name(PKCS12 *p12)
  16. {
  17. STACK_OF(PKCS7) *safes = PKCS12_unpack_authsafes(p12);
  18. int n, m;
  19. char *name = NULL;
  20. PKCS7 *safe;
  21. STACK_OF(PKCS12_SAFEBAG) *bags;
  22. PKCS12_SAFEBAG *bag;
  23. if ((safes = PKCS12_unpack_authsafes(p12)) == NULL)
  24. return NULL;
  25. for (n = 0; n < sk_PKCS7_num(safes) && name == NULL; n++) {
  26. safe = sk_PKCS7_value(safes, n);
  27. if (OBJ_obj2nid(safe->type) != NID_pkcs7_data
  28. || (bags = PKCS12_unpack_p7data(safe)) == NULL)
  29. continue;
  30. for (m = 0; m < sk_PKCS12_SAFEBAG_num(bags) && name == NULL; m++) {
  31. bag = sk_PKCS12_SAFEBAG_value(bags, m);
  32. name = PKCS12_get_friendlyname(bag);
  33. }
  34. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  35. }
  36. sk_PKCS7_pop_free(safes, PKCS7_free);
  37. return name;
  38. }
  39. int main(int argc, char **argv)
  40. {
  41. FILE *fp;
  42. EVP_PKEY *pkey;
  43. X509 *cert;
  44. STACK_OF(X509) *ca = NULL;
  45. PKCS12 *p12;
  46. const char *name;
  47. int i;
  48. if (argc != 4) {
  49. fprintf(stderr, "Usage: pkread p12file password opfile\n");
  50. exit(1);
  51. }
  52. OpenSSL_add_all_algorithms();
  53. ERR_load_crypto_strings();
  54. if ((fp = fopen(argv[1], "rb")) == NULL) {
  55. fprintf(stderr, "Error opening file %s\n", argv[1]);
  56. exit(1);
  57. }
  58. p12 = d2i_PKCS12_fp(fp, NULL);
  59. fclose(fp);
  60. if (!p12) {
  61. fprintf(stderr, "Error reading PKCS#12 file\n");
  62. ERR_print_errors_fp(stderr);
  63. exit(1);
  64. }
  65. if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) {
  66. fprintf(stderr, "Error parsing PKCS#12 file\n");
  67. ERR_print_errors_fp(stderr);
  68. exit(1);
  69. }
  70. name = find_friendly_name(p12);
  71. PKCS12_free(p12);
  72. if ((fp = fopen(argv[3], "w")) == NULL) {
  73. fprintf(stderr, "Error opening file %s\n", argv[1]);
  74. exit(1);
  75. }
  76. if (name)
  77. fprintf(fp, "***Friendly Name***\n%s\n", name);
  78. if (pkey) {
  79. fprintf(fp, "***Private Key***\n");
  80. PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
  81. }
  82. if (cert) {
  83. fprintf(fp, "***User Certificate***\n");
  84. PEM_write_X509_AUX(fp, cert);
  85. }
  86. if (ca && sk_X509_num(ca)) {
  87. fprintf(fp, "***Other Certificates***\n");
  88. for (i = 0; i < sk_X509_num(ca); i++)
  89. PEM_write_X509_AUX(fp, sk_X509_value(ca, i));
  90. }
  91. fclose(fp);
  92. return 0;
  93. }