tool/pkcs12.cc
Go to the documentation of this file.
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 static const struct argument kArguments[] = {
44  {
45  "-dump", kOptionalArgument,
46  "Dump the key and contents of the given file to stdout",
47  },
48  {
49  "", kOptionalArgument, "",
50  },
51 };
52 
53 bool DoPKCS12(const std::vector<std::string> &args) {
54  std::map<std::string, std::string> args_map;
55 
56  if (!ParseKeyValueArguments(&args_map, args, kArguments) ||
57  args_map["-dump"].empty()) {
59  return false;
60  }
61 
62  ScopedFD fd = OpenFD(args_map["-dump"].c_str(), O_RDONLY);
63  if (!fd) {
64  perror("open");
65  return false;
66  }
67 
68  struct stat st;
69  if (fstat(fd.get(), &st)) {
70  perror("fstat");
71  return false;
72  }
73  const size_t size = st.st_size;
74 
75  std::unique_ptr<uint8_t[]> contents(new uint8_t[size]);
76  size_t off = 0;
77  while (off < size) {
78  size_t bytes_read;
79  if (!ReadFromFD(fd.get(), &bytes_read, contents.get() + off, size - off)) {
80  perror("read");
81  return false;
82  }
83  if (bytes_read == 0) {
84  fprintf(stderr, "Unexpected EOF\n");
85  return false;
86  }
87  off += bytes_read;
88  }
89 
90  printf("Enter password: ");
91  fflush(stdout);
92 
93  char password[256];
94  off = 0;
95  while (off < sizeof(password) - 1) {
96  size_t bytes_read;
97  if (!ReadFromFD(0, &bytes_read, password + off,
98  sizeof(password) - 1 - off)) {
99  perror("read");
100  return false;
101  }
102 
103  off += bytes_read;
104  if (bytes_read == 0 || OPENSSL_memchr(password, '\n', off) != nullptr) {
105  break;
106  }
107  }
108 
109  char *newline = reinterpret_cast<char *>(OPENSSL_memchr(password, '\n', off));
110  if (newline == NULL) {
111  return false;
112  }
113  *newline = 0;
114 
115  CBS pkcs12;
116  CBS_init(&pkcs12, contents.get(), size);
117 
118  EVP_PKEY *key;
120 
121  if (!PKCS12_get_key_and_certs(&key, certs.get(), &pkcs12, password)) {
122  fprintf(stderr, "Failed to parse PKCS#12 data:\n");
124  return false;
125  }
126  bssl::UniquePtr<EVP_PKEY> key_owned(key);
127 
128  if (key != NULL) {
129  PEM_write_PrivateKey(stdout, key, NULL, NULL, 0, NULL, NULL);
130  }
131 
132  for (size_t i = 0; i < sk_X509_num(certs.get()); i++) {
133  PEM_write_X509(stdout, sk_X509_value(certs.get(), i));
134  }
135 
136  return true;
137 }
ParseKeyValueArguments
bool ParseKeyValueArguments(std::map< std::string, std::string > *out_args, const std::vector< std::string > &args, const struct argument *templates)
Definition: args.cc:27
cbs_st
Definition: bytestring.h:39
PKCS12_get_key_and_certs
#define PKCS12_get_key_and_certs
Definition: boringssl_prefix_symbols.h:2006
demumble_test.stdout
stdout
Definition: demumble_test.py:38
internal.h
string.h
sk_X509_num
#define sk_X509_num
Definition: boringssl_prefix_symbols.h:587
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
ERR_print_errors_fp
#define ERR_print_errors_fp
Definition: boringssl_prefix_symbols.h:1437
OPENSSL_memchr
static void * OPENSSL_memchr(const void *s, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:801
pem.h
pkcs8.h
CBS_init
#define CBS_init
Definition: boringssl_prefix_symbols.h:1085
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ReadFromFD
bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num)
Definition: fd.cc:53
base.h
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
PEM_write_PrivateKey
int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u)
Definition: pem_pkey.c:166
bytestring.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
evp_pkey_st
Definition: evp.h:1046
STACK_OF
#define STACK_OF(type)
Definition: stack.h:125
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
ScopedFD
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:47
bytes_read
static size_t bytes_read
Definition: test-ipc-heavy-traffic-deadlock-bug.c:47
argument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:108
err.h
kArguments
static const struct argument kArguments[]
Definition: tool/pkcs12.cc:43
PEM_write_X509
#define PEM_write_X509
Definition: boringssl_prefix_symbols.h:1976
grpc_core::UniquePtr
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: src/core/lib/gprpp/memory.h:43
stdint.h
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
PrintUsage
void PrintUsage(const struct argument *templates)
Definition: args.cc:75
ScopedFD::get
int get() const
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:65
contents
string_view contents
Definition: elf.cc:597
DoPKCS12
bool DoPKCS12(const std::vector< std::string > &args)
Definition: tool/pkcs12.cc:53
key
const char * key
Definition: hpack_parser_table.cc:164
x509_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:139
sk_X509_value
#define sk_X509_value
Definition: boringssl_prefix_symbols.h:590
sk_X509_new_null
#define sk_X509_new_null
Definition: boringssl_prefix_symbols.h:586
kOptionalArgument
@ kOptionalArgument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:104
OpenFD
ScopedFD OpenFD(const char *path, int flags)
Definition: fd.cc:33
stat
#define stat
Definition: test-fs.c:50
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
errno.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
stack.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:43