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