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.
 
 
 
 
 
 

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