pkcs12.cc 3.5 KB

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