Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 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 | |
David Benjamin | eee7306 | 2014-10-31 16:01:29 -0400 | [diff] [blame] | 15 | #include <openssl/base.h> |
| 16 | |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 17 | #include <memory> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
Piotr Sikora | bbac844 | 2014-08-26 16:18:20 -0700 | [diff] [blame] | 21 | #include <errno.h> |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <stdint.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 24 | #include <string.h> |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
David Benjamin | eee7306 | 2014-10-31 16:01:29 -0400 | [diff] [blame] | 27 | #if defined(OPENSSL_WINDOWS) |
| 28 | #include <io.h> |
| 29 | #else |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 30 | #include <unistd.h> |
David Benjamin | eee7306 | 2014-10-31 16:01:29 -0400 | [diff] [blame] | 31 | #endif |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 32 | |
| 33 | #include <openssl/bytestring.h> |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 34 | #include <openssl/err.h> |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 35 | #include <openssl/pem.h> |
| 36 | #include <openssl/pkcs8.h> |
| 37 | #include <openssl/stack.h> |
| 38 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 39 | #include "../crypto/internal.h" |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 40 | #include "internal.h" |
| 41 | |
| 42 | |
David Benjamin | eee7306 | 2014-10-31 16:01:29 -0400 | [diff] [blame] | 43 | #if defined(OPENSSL_WINDOWS) |
| 44 | typedef int read_result_t; |
| 45 | #else |
| 46 | typedef ssize_t read_result_t; |
| 47 | #endif |
| 48 | |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 49 | static const struct argument kArguments[] = { |
| 50 | { |
David Benjamin | 0570923 | 2015-03-23 19:01:33 -0400 | [diff] [blame] | 51 | "-dump", kOptionalArgument, |
| 52 | "Dump the key and contents of the given file to stdout", |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 53 | }, |
| 54 | { |
David Benjamin | 0570923 | 2015-03-23 19:01:33 -0400 | [diff] [blame] | 55 | "", kOptionalArgument, "", |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 56 | }, |
| 57 | }; |
| 58 | |
Adam Langley | 5127db3 | 2014-09-19 10:49:56 -0700 | [diff] [blame] | 59 | bool DoPKCS12(const std::vector<std::string> &args) { |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 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 | |
nmittler | f0322b2 | 2016-05-19 08:49:59 -0700 | [diff] [blame] | 68 | int fd = BORINGSSL_OPEN(args_map["-dump"].c_str(), O_RDONLY); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 69 | if (fd < 0) { |
| 70 | perror("open"); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | struct stat st; |
| 75 | if (fstat(fd, &st)) { |
| 76 | perror("fstat"); |
nmittler | f0322b2 | 2016-05-19 08:49:59 -0700 | [diff] [blame] | 77 | BORINGSSL_CLOSE(fd); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | const size_t size = st.st_size; |
| 81 | |
| 82 | std::unique_ptr<uint8_t[]> contents(new uint8_t[size]); |
David Benjamin | eee7306 | 2014-10-31 16:01:29 -0400 | [diff] [blame] | 83 | read_result_t n; |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 84 | size_t off = 0; |
| 85 | do { |
nmittler | f0322b2 | 2016-05-19 08:49:59 -0700 | [diff] [blame] | 86 | n = BORINGSSL_READ(fd, &contents[off], size - off); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 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"); |
nmittler | f0322b2 | 2016-05-19 08:49:59 -0700 | [diff] [blame] | 94 | BORINGSSL_CLOSE(fd); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
nmittler | f0322b2 | 2016-05-19 08:49:59 -0700 | [diff] [blame] | 98 | BORINGSSL_CLOSE(fd); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 99 | |
| 100 | printf("Enter password: "); |
| 101 | fflush(stdout); |
| 102 | |
| 103 | char password[256]; |
| 104 | off = 0; |
| 105 | do { |
nmittler | f0322b2 | 2016-05-19 08:49:59 -0700 | [diff] [blame] | 106 | n = BORINGSSL_READ(0, &password[off], sizeof(password) - 1 - off); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 107 | if (n >= 0) { |
| 108 | off += static_cast<size_t>(n); |
| 109 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 110 | } while ((n > 0 && OPENSSL_memchr(password, '\n', off) == NULL && |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 111 | off < sizeof(password) - 1) || |
| 112 | (n == -1 && errno == EINTR)); |
| 113 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 114 | char *newline = reinterpret_cast<char *>(OPENSSL_memchr(password, '\n', off)); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 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; |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 124 | bssl::UniquePtr<STACK_OF(X509)> certs(sk_X509_new_null()); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 125 | |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 126 | if (!PKCS12_get_key_and_certs(&key, certs.get(), &pkcs12, password)) { |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 127 | fprintf(stderr, "Failed to parse PKCS#12 data:\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 128 | ERR_print_errors_fp(stderr); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 129 | return false; |
| 130 | } |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 131 | bssl::UniquePtr<EVP_PKEY> key_owned(key); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 132 | |
David Benjamin | f606f98 | 2015-11-07 18:32:39 -0500 | [diff] [blame] | 133 | if (key != NULL) { |
| 134 | PEM_write_PrivateKey(stdout, key, NULL, NULL, 0, NULL, NULL); |
David Benjamin | f606f98 | 2015-11-07 18:32:39 -0500 | [diff] [blame] | 135 | } |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 136 | |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 137 | for (size_t i = 0; i < sk_X509_num(certs.get()); i++) { |
| 138 | PEM_write_X509(stdout, sk_X509_value(certs.get(), i)); |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 139 | } |
Adam Langley | 8e16b6e | 2014-08-21 14:11:39 -0700 | [diff] [blame] | 140 | |
| 141 | return true; |
| 142 | } |