blob: a8ddb0e01d836e15a304d1296a4a92847b1e34d0 [file] [log] [blame]
Adam Langley8e16b6e2014-08-21 14:11:39 -07001/* 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
David Benjamineee73062014-10-31 16:01:29 -040015#include <openssl/base.h>
16
Adam Langley8e16b6e2014-08-21 14:11:39 -070017#include <memory>
18#include <string>
19#include <vector>
20
Piotr Sikorabbac8442014-08-26 16:18:20 -070021#include <errno.h>
Adam Langley8e16b6e2014-08-21 14:11:39 -070022#include <fcntl.h>
23#include <stdint.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080024#include <string.h>
Adam Langley8e16b6e2014-08-21 14:11:39 -070025#include <sys/stat.h>
26#include <sys/types.h>
David Benjamineee73062014-10-31 16:01:29 -040027#if defined(OPENSSL_WINDOWS)
28#include <io.h>
29#else
Adam Langley8e16b6e2014-08-21 14:11:39 -070030#include <unistd.h>
David Benjamineee73062014-10-31 16:01:29 -040031#endif
Adam Langley8e16b6e2014-08-21 14:11:39 -070032
33#include <openssl/bytestring.h>
Brian Smith83a82982015-04-09 16:21:10 -100034#include <openssl/err.h>
Adam Langley8e16b6e2014-08-21 14:11:39 -070035#include <openssl/pem.h>
36#include <openssl/pkcs8.h>
37#include <openssl/stack.h>
38
David Benjamin17cf2cb2016-12-13 01:07:13 -050039#include "../crypto/internal.h"
Adam Langley8e16b6e2014-08-21 14:11:39 -070040#include "internal.h"
41
42
David Benjamineee73062014-10-31 16:01:29 -040043#if defined(OPENSSL_WINDOWS)
44typedef int read_result_t;
45#else
46typedef ssize_t read_result_t;
47#endif
48
Adam Langley8e16b6e2014-08-21 14:11:39 -070049static const struct argument kArguments[] = {
50 {
David Benjamin05709232015-03-23 19:01:33 -040051 "-dump", kOptionalArgument,
52 "Dump the key and contents of the given file to stdout",
Adam Langley8e16b6e2014-08-21 14:11:39 -070053 },
54 {
David Benjamin05709232015-03-23 19:01:33 -040055 "", kOptionalArgument, "",
Adam Langley8e16b6e2014-08-21 14:11:39 -070056 },
57};
58
Adam Langley5127db32014-09-19 10:49:56 -070059bool DoPKCS12(const std::vector<std::string> &args) {
Adam Langley8e16b6e2014-08-21 14:11:39 -070060 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
nmittlerf0322b22016-05-19 08:49:59 -070068 int fd = BORINGSSL_OPEN(args_map["-dump"].c_str(), O_RDONLY);
Adam Langley8e16b6e2014-08-21 14:11:39 -070069 if (fd < 0) {
70 perror("open");
71 return false;
72 }
73
74 struct stat st;
75 if (fstat(fd, &st)) {
76 perror("fstat");
nmittlerf0322b22016-05-19 08:49:59 -070077 BORINGSSL_CLOSE(fd);
Adam Langley8e16b6e2014-08-21 14:11:39 -070078 return false;
79 }
80 const size_t size = st.st_size;
81
82 std::unique_ptr<uint8_t[]> contents(new uint8_t[size]);
David Benjamineee73062014-10-31 16:01:29 -040083 read_result_t n;
Adam Langley8e16b6e2014-08-21 14:11:39 -070084 size_t off = 0;
85 do {
nmittlerf0322b22016-05-19 08:49:59 -070086 n = BORINGSSL_READ(fd, &contents[off], size - off);
Adam Langley8e16b6e2014-08-21 14:11:39 -070087 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");
nmittlerf0322b22016-05-19 08:49:59 -070094 BORINGSSL_CLOSE(fd);
Adam Langley8e16b6e2014-08-21 14:11:39 -070095 return false;
96 }
97
nmittlerf0322b22016-05-19 08:49:59 -070098 BORINGSSL_CLOSE(fd);
Adam Langley8e16b6e2014-08-21 14:11:39 -070099
100 printf("Enter password: ");
101 fflush(stdout);
102
103 char password[256];
104 off = 0;
105 do {
nmittlerf0322b22016-05-19 08:49:59 -0700106 n = BORINGSSL_READ(0, &password[off], sizeof(password) - 1 - off);
Adam Langley8e16b6e2014-08-21 14:11:39 -0700107 if (n >= 0) {
108 off += static_cast<size_t>(n);
109 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500110 } while ((n > 0 && OPENSSL_memchr(password, '\n', off) == NULL &&
Adam Langley8e16b6e2014-08-21 14:11:39 -0700111 off < sizeof(password) - 1) ||
112 (n == -1 && errno == EINTR));
113
David Benjamin17cf2cb2016-12-13 01:07:13 -0500114 char *newline = reinterpret_cast<char *>(OPENSSL_memchr(password, '\n', off));
Adam Langley8e16b6e2014-08-21 14:11:39 -0700115 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;
David Benjamin0cce8632016-10-20 15:13:26 -0400124 bssl::UniquePtr<STACK_OF(X509)> certs(sk_X509_new_null());
Adam Langley8e16b6e2014-08-21 14:11:39 -0700125
David Benjamin0cce8632016-10-20 15:13:26 -0400126 if (!PKCS12_get_key_and_certs(&key, certs.get(), &pkcs12, password)) {
Adam Langley8e16b6e2014-08-21 14:11:39 -0700127 fprintf(stderr, "Failed to parse PKCS#12 data:\n");
Brian Smith83a82982015-04-09 16:21:10 -1000128 ERR_print_errors_fp(stderr);
Adam Langley8e16b6e2014-08-21 14:11:39 -0700129 return false;
130 }
David Benjamin0cce8632016-10-20 15:13:26 -0400131 bssl::UniquePtr<EVP_PKEY> key_owned(key);
Adam Langley8e16b6e2014-08-21 14:11:39 -0700132
David Benjaminf606f982015-11-07 18:32:39 -0500133 if (key != NULL) {
134 PEM_write_PrivateKey(stdout, key, NULL, NULL, 0, NULL, NULL);
David Benjaminf606f982015-11-07 18:32:39 -0500135 }
Adam Langley8e16b6e2014-08-21 14:11:39 -0700136
David Benjamin0cce8632016-10-20 15:13:26 -0400137 for (size_t i = 0; i < sk_X509_num(certs.get()); i++) {
138 PEM_write_X509(stdout, sk_X509_value(certs.get(), i));
Adam Langley8e16b6e2014-08-21 14:11:39 -0700139 }
Adam Langley8e16b6e2014-08-21 14:11:39 -0700140
141 return true;
142}