Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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