You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

139 line
3.3 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/base.h>
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #if defined(OPENSSL_WINDOWS)
  25. #include <io.h>
  26. #else
  27. #include <unistd.h>
  28. #endif
  29. #include <openssl/bytestring.h>
  30. #include <openssl/pem.h>
  31. #include <openssl/pkcs8.h>
  32. #include <openssl/stack.h>
  33. #include "internal.h"
  34. #if defined(OPENSSL_WINDOWS)
  35. typedef int read_result_t;
  36. #else
  37. typedef ssize_t read_result_t;
  38. #endif
  39. static const struct argument kArguments[] = {
  40. {
  41. "-dump", false, "Dump the key and contents of the given file to stdout",
  42. },
  43. {
  44. "", false, "",
  45. },
  46. };
  47. bool DoPKCS12(const std::vector<std::string> &args) {
  48. std::map<std::string, std::string> args_map;
  49. if (!ParseKeyValueArguments(&args_map, args, kArguments) ||
  50. args_map["-dump"].empty()) {
  51. PrintUsage(kArguments);
  52. return false;
  53. }
  54. int fd = open(args_map["-dump"].c_str(), O_RDONLY);
  55. if (fd < 0) {
  56. perror("open");
  57. return false;
  58. }
  59. struct stat st;
  60. if (fstat(fd, &st)) {
  61. perror("fstat");
  62. close(fd);
  63. return false;
  64. }
  65. const size_t size = st.st_size;
  66. std::unique_ptr<uint8_t[]> contents(new uint8_t[size]);
  67. read_result_t n;
  68. size_t off = 0;
  69. do {
  70. n = read(fd, &contents[off], size - off);
  71. if (n >= 0) {
  72. off += static_cast<size_t>(n);
  73. }
  74. } while ((n > 0 && off < size) || (n == -1 && errno == EINTR));
  75. if (off != size) {
  76. perror("read");
  77. close(fd);
  78. return false;
  79. }
  80. close(fd);
  81. printf("Enter password: ");
  82. fflush(stdout);
  83. char password[256];
  84. off = 0;
  85. do {
  86. n = read(0, &password[off], sizeof(password) - 1 - off);
  87. if (n >= 0) {
  88. off += static_cast<size_t>(n);
  89. }
  90. } while ((n > 0 && memchr(password, '\n', off) == NULL &&
  91. off < sizeof(password) - 1) ||
  92. (n == -1 && errno == EINTR));
  93. char *newline = reinterpret_cast<char*>(memchr(password, '\n', off));
  94. if (newline == NULL) {
  95. return false;
  96. }
  97. *newline = 0;
  98. CBS pkcs12;
  99. CBS_init(&pkcs12, contents.get(), size);
  100. EVP_PKEY *key;
  101. STACK_OF(X509) *certs = sk_X509_new_null();
  102. if (!PKCS12_get_key_and_certs(&key, certs, &pkcs12, password)) {
  103. fprintf(stderr, "Failed to parse PKCS#12 data:\n");
  104. BIO_print_errors_fp(stderr);
  105. return false;
  106. }
  107. PEM_write_PrivateKey(stdout, key, NULL, NULL, 0, NULL, NULL);
  108. EVP_PKEY_free(key);
  109. for (size_t i = 0; i < sk_X509_num(certs); i++) {
  110. PEM_write_X509(stdout, sk_X509_value(certs, i));
  111. }
  112. sk_X509_pop_free(certs, X509_free);
  113. return true;
  114. }