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