Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

138 Zeilen
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 <sys/stat.h>
  22. #include <sys/types.h>
  23. #if defined(OPENSSL_WINDOWS)
  24. #include <io.h>
  25. #else
  26. #include <unistd.h>
  27. #endif
  28. #include <openssl/bytestring.h>
  29. #include <openssl/pem.h>
  30. #include <openssl/pkcs8.h>
  31. #include <openssl/stack.h>
  32. #include "internal.h"
  33. #if defined(OPENSSL_WINDOWS)
  34. typedef int read_result_t;
  35. #else
  36. typedef ssize_t read_result_t;
  37. #endif
  38. static const struct argument kArguments[] = {
  39. {
  40. "-dump", false, "Dump the key and contents of the given file to stdout",
  41. },
  42. {
  43. "", false, "",
  44. },
  45. };
  46. bool DoPKCS12(const std::vector<std::string> &args) {
  47. std::map<std::string, std::string> args_map;
  48. if (!ParseKeyValueArguments(&args_map, args, kArguments) ||
  49. args_map["-dump"].empty()) {
  50. PrintUsage(kArguments);
  51. return false;
  52. }
  53. int fd = open(args_map["-dump"].c_str(), O_RDONLY);
  54. if (fd < 0) {
  55. perror("open");
  56. return false;
  57. }
  58. struct stat st;
  59. if (fstat(fd, &st)) {
  60. perror("fstat");
  61. close(fd);
  62. return false;
  63. }
  64. const size_t size = st.st_size;
  65. std::unique_ptr<uint8_t[]> contents(new uint8_t[size]);
  66. read_result_t n;
  67. size_t off = 0;
  68. do {
  69. n = read(fd, &contents[off], size - off);
  70. if (n >= 0) {
  71. off += static_cast<size_t>(n);
  72. }
  73. } while ((n > 0 && off < size) || (n == -1 && errno == EINTR));
  74. if (off != size) {
  75. perror("read");
  76. close(fd);
  77. return false;
  78. }
  79. close(fd);
  80. printf("Enter password: ");
  81. fflush(stdout);
  82. char password[256];
  83. off = 0;
  84. do {
  85. n = read(0, &password[off], sizeof(password) - 1 - off);
  86. if (n >= 0) {
  87. off += static_cast<size_t>(n);
  88. }
  89. } while ((n > 0 && memchr(password, '\n', off) == NULL &&
  90. off < sizeof(password) - 1) ||
  91. (n == -1 && errno == EINTR));
  92. char *newline = reinterpret_cast<char*>(memchr(password, '\n', off));
  93. if (newline == NULL) {
  94. return false;
  95. }
  96. *newline = 0;
  97. CBS pkcs12;
  98. CBS_init(&pkcs12, contents.get(), size);
  99. EVP_PKEY *key;
  100. STACK_OF(X509) *certs = sk_X509_new_null();
  101. if (!PKCS12_get_key_and_certs(&key, certs, &pkcs12, password)) {
  102. fprintf(stderr, "Failed to parse PKCS#12 data:\n");
  103. BIO_print_errors_fp(stderr);
  104. return false;
  105. }
  106. PEM_write_PrivateKey(stdout, key, NULL, NULL, 0, NULL, NULL);
  107. EVP_PKEY_free(key);
  108. for (size_t i = 0; i < sk_X509_num(certs); i++) {
  109. PEM_write_X509(stdout, sk_X509_value(certs, i));
  110. }
  111. sk_X509_pop_free(certs, X509_free);
  112. return true;
  113. }