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