1 | /*
|
---|
2 | * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <openssl/opensslconf.h>
|
---|
11 |
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <stdlib.h>
|
---|
14 | #include <string.h>
|
---|
15 | #include "apps.h"
|
---|
16 | #include "progs.h"
|
---|
17 | #include <openssl/crypto.h>
|
---|
18 | #include <openssl/err.h>
|
---|
19 | #include <openssl/pem.h>
|
---|
20 | #include <openssl/pkcs12.h>
|
---|
21 | #include <openssl/provider.h>
|
---|
22 | #include <openssl/kdf.h>
|
---|
23 |
|
---|
24 | #define NOKEYS 0x1
|
---|
25 | #define NOCERTS 0x2
|
---|
26 | #define INFO 0x4
|
---|
27 | #define CLCERTS 0x8
|
---|
28 | #define CACERTS 0x10
|
---|
29 |
|
---|
30 | #define PASSWD_BUF_SIZE 2048
|
---|
31 |
|
---|
32 | #define WARN_EXPORT(opt) \
|
---|
33 | BIO_printf(bio_err, "Warning: -%s option ignored with -export\n", opt);
|
---|
34 | #define WARN_NO_EXPORT(opt) \
|
---|
35 | BIO_printf(bio_err, "Warning: -%s option ignored without -export\n", opt);
|
---|
36 |
|
---|
37 | static int get_cert_chain(X509 *cert, X509_STORE *store,
|
---|
38 | STACK_OF(X509) *untrusted_certs,
|
---|
39 | STACK_OF(X509) **chain);
|
---|
40 | int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
|
---|
41 | const char *pass, int passlen, int options,
|
---|
42 | char *pempass, const EVP_CIPHER *enc);
|
---|
43 | int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
|
---|
44 | const char *pass, int passlen, int options,
|
---|
45 | char *pempass, const EVP_CIPHER *enc);
|
---|
46 | int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
|
---|
47 | const char *pass, int passlen,
|
---|
48 | int options, char *pempass, const EVP_CIPHER *enc);
|
---|
49 | void print_attribute(BIO *out, const ASN1_TYPE *av);
|
---|
50 | int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
|
---|
51 | const char *name);
|
---|
52 | void hex_prin(BIO *out, unsigned char *buf, int len);
|
---|
53 | static int alg_print(const X509_ALGOR *alg);
|
---|
54 | int cert_load(BIO *in, STACK_OF(X509) *sk);
|
---|
55 | static int set_pbe(int *ppbe, const char *str);
|
---|
56 |
|
---|
57 | typedef enum OPTION_choice {
|
---|
58 | OPT_COMMON,
|
---|
59 | OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
|
---|
60 | OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
|
---|
61 | #ifndef OPENSSL_NO_DES
|
---|
62 | OPT_DESCERT,
|
---|
63 | #endif
|
---|
64 | OPT_EXPORT, OPT_ITER, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
|
---|
65 | OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_NOENC, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
|
---|
66 | OPT_INKEY, OPT_CERTFILE, OPT_UNTRUSTED, OPT_PASSCERTS,
|
---|
67 | OPT_NAME, OPT_CSP, OPT_CANAME,
|
---|
68 | OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
|
---|
69 | OPT_CAFILE, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_ENGINE,
|
---|
70 | OPT_R_ENUM, OPT_PROV_ENUM,
|
---|
71 | #ifndef OPENSSL_NO_DES
|
---|
72 | OPT_LEGACY_ALG
|
---|
73 | #endif
|
---|
74 | } OPTION_CHOICE;
|
---|
75 |
|
---|
76 | const OPTIONS pkcs12_options[] = {
|
---|
77 | OPT_SECTION("General"),
|
---|
78 | {"help", OPT_HELP, '-', "Display this summary"},
|
---|
79 | {"in", OPT_IN, '<', "Input file"},
|
---|
80 | {"out", OPT_OUT, '>', "Output file"},
|
---|
81 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
|
---|
82 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
|
---|
83 | {"password", OPT_PASSWORD, 's', "Set PKCS#12 import/export password source"},
|
---|
84 | {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
|
---|
85 | {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
|
---|
86 | {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
|
---|
87 | {"noout", OPT_NOOUT, '-', "Don't output anything, just verify PKCS#12 input"},
|
---|
88 | #ifndef OPENSSL_NO_DES
|
---|
89 | {"legacy", OPT_LEGACY_ALG, '-',
|
---|
90 | # ifdef OPENSSL_NO_RC2
|
---|
91 | "Use legacy encryption algorithm 3DES_CBC for keys and certs"
|
---|
92 | # else
|
---|
93 | "Use legacy encryption: 3DES_CBC for keys, RC2_CBC for certs"
|
---|
94 | # endif
|
---|
95 | },
|
---|
96 | #endif
|
---|
97 | #ifndef OPENSSL_NO_ENGINE
|
---|
98 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
|
---|
99 | #endif
|
---|
100 | OPT_PROV_OPTIONS,
|
---|
101 | OPT_R_OPTIONS,
|
---|
102 |
|
---|
103 | OPT_SECTION("PKCS#12 import (parsing PKCS#12)"),
|
---|
104 | {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
|
---|
105 | {"nomacver", OPT_NOMACVER, '-', "Don't verify integrity MAC"},
|
---|
106 | {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
|
---|
107 | {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
|
---|
108 | {"", OPT_CIPHER, '-', "Any supported cipher for output encryption"},
|
---|
109 | {"noenc", OPT_NOENC, '-', "Don't encrypt private keys"},
|
---|
110 | {"nodes", OPT_NODES, '-', "Don't encrypt private keys; deprecated"},
|
---|
111 |
|
---|
112 | OPT_SECTION("PKCS#12 output (export)"),
|
---|
113 | {"export", OPT_EXPORT, '-', "Create PKCS12 file"},
|
---|
114 | {"inkey", OPT_INKEY, 's', "Private key, else read from -in input file"},
|
---|
115 | {"certfile", OPT_CERTFILE, '<', "Extra certificates for PKCS12 output"},
|
---|
116 | {"passcerts", OPT_PASSCERTS, 's', "Certificate file pass phrase source"},
|
---|
117 | {"chain", OPT_CHAIN, '-', "Build and add certificate chain for EE cert,"},
|
---|
118 | {OPT_MORE_STR, 0, 0,
|
---|
119 | "which is the 1st cert from -in matching the private key (if given)"},
|
---|
120 | {"untrusted", OPT_UNTRUSTED, '<', "Untrusted certificates for chain building"},
|
---|
121 | {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
|
---|
122 | {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
|
---|
123 | {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
|
---|
124 | {"no-CAfile", OPT_NOCAFILE, '-',
|
---|
125 | "Do not load the default certificates file"},
|
---|
126 | {"no-CApath", OPT_NOCAPATH, '-',
|
---|
127 | "Do not load certificates from the default certificates directory"},
|
---|
128 | {"no-CAstore", OPT_NOCASTORE, '-',
|
---|
129 | "Do not load certificates from the default certificates store"},
|
---|
130 | {"name", OPT_NAME, 's', "Use name as friendly name"},
|
---|
131 | {"caname", OPT_CANAME, 's',
|
---|
132 | "Use name as CA friendly name (can be repeated)"},
|
---|
133 | {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
|
---|
134 | {"LMK", OPT_LMK, '-',
|
---|
135 | "Add local machine keyset attribute to private key"},
|
---|
136 | {"keyex", OPT_KEYEX, '-', "Set key type to MS key exchange"},
|
---|
137 | {"keysig", OPT_KEYSIG, '-', "Set key type to MS key signature"},
|
---|
138 | {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default AES-256 CBC)"},
|
---|
139 | {"certpbe", OPT_CERTPBE, 's',
|
---|
140 | "Certificate PBE algorithm (default PBES2 with PBKDF2 and AES-256 CBC)"},
|
---|
141 | #ifndef OPENSSL_NO_DES
|
---|
142 | {"descert", OPT_DESCERT, '-',
|
---|
143 | "Encrypt output with 3DES (default PBES2 with PBKDF2 and AES-256 CBC)"},
|
---|
144 | #endif
|
---|
145 | {"macalg", OPT_MACALG, 's',
|
---|
146 | "Digest algorithm to use in MAC (default SHA256)"},
|
---|
147 | {"iter", OPT_ITER, 'p', "Specify the iteration count for encryption and MAC"},
|
---|
148 | {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
|
---|
149 | {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration)"},
|
---|
150 | {"maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility"},
|
---|
151 | {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
|
---|
152 | {NULL}
|
---|
153 | };
|
---|
154 |
|
---|
155 | int pkcs12_main(int argc, char **argv)
|
---|
156 | {
|
---|
157 | char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
|
---|
158 | char *untrusted = NULL, *ciphername = NULL, *enc_flag = NULL;
|
---|
159 | char *passcertsarg = NULL, *passcerts = NULL;
|
---|
160 | char *name = NULL, *csp_name = NULL;
|
---|
161 | char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
|
---|
162 | int export_pkcs12 = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
|
---|
163 | #ifndef OPENSSL_NO_DES
|
---|
164 | int use_legacy = 0;
|
---|
165 | #endif
|
---|
166 | /* use library defaults for the iter, maciter, cert, and key PBE */
|
---|
167 | int iter = 0, maciter = 0;
|
---|
168 | int cert_pbe = NID_undef;
|
---|
169 | int key_pbe = NID_undef;
|
---|
170 | int ret = 1, macver = 1, add_lmk = 0, private = 0;
|
---|
171 | int noprompt = 0;
|
---|
172 | char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
|
---|
173 | char *passin = NULL, *passout = NULL, *macalg = NULL;
|
---|
174 | char *cpass = NULL, *mpass = NULL, *badpass = NULL;
|
---|
175 | const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog;
|
---|
176 | int noCApath = 0, noCAfile = 0, noCAstore = 0;
|
---|
177 | ENGINE *e = NULL;
|
---|
178 | BIO *in = NULL, *out = NULL;
|
---|
179 | PKCS12 *p12 = NULL;
|
---|
180 | STACK_OF(OPENSSL_STRING) *canames = NULL;
|
---|
181 | EVP_CIPHER *default_enc = (EVP_CIPHER *)EVP_aes_256_cbc();
|
---|
182 | EVP_CIPHER *enc = (EVP_CIPHER *)default_enc;
|
---|
183 | OPTION_CHOICE o;
|
---|
184 |
|
---|
185 | prog = opt_init(argc, argv, pkcs12_options);
|
---|
186 | while ((o = opt_next()) != OPT_EOF) {
|
---|
187 | switch (o) {
|
---|
188 | case OPT_EOF:
|
---|
189 | case OPT_ERR:
|
---|
190 | opthelp:
|
---|
191 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
|
---|
192 | goto end;
|
---|
193 | case OPT_HELP:
|
---|
194 | opt_help(pkcs12_options);
|
---|
195 | ret = 0;
|
---|
196 | goto end;
|
---|
197 | case OPT_NOKEYS:
|
---|
198 | options |= NOKEYS;
|
---|
199 | break;
|
---|
200 | case OPT_KEYEX:
|
---|
201 | keytype = KEY_EX;
|
---|
202 | break;
|
---|
203 | case OPT_KEYSIG:
|
---|
204 | keytype = KEY_SIG;
|
---|
205 | break;
|
---|
206 | case OPT_NOCERTS:
|
---|
207 | options |= NOCERTS;
|
---|
208 | break;
|
---|
209 | case OPT_CLCERTS:
|
---|
210 | options |= CLCERTS;
|
---|
211 | break;
|
---|
212 | case OPT_CACERTS:
|
---|
213 | options |= CACERTS;
|
---|
214 | break;
|
---|
215 | case OPT_NOOUT:
|
---|
216 | options |= (NOKEYS | NOCERTS);
|
---|
217 | break;
|
---|
218 | case OPT_INFO:
|
---|
219 | options |= INFO;
|
---|
220 | break;
|
---|
221 | case OPT_CHAIN:
|
---|
222 | chain = 1;
|
---|
223 | break;
|
---|
224 | case OPT_TWOPASS:
|
---|
225 | twopass = 1;
|
---|
226 | break;
|
---|
227 | case OPT_NOMACVER:
|
---|
228 | macver = 0;
|
---|
229 | break;
|
---|
230 | #ifndef OPENSSL_NO_DES
|
---|
231 | case OPT_DESCERT:
|
---|
232 | cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
|
---|
233 | break;
|
---|
234 | #endif
|
---|
235 | case OPT_EXPORT:
|
---|
236 | export_pkcs12 = 1;
|
---|
237 | break;
|
---|
238 | case OPT_NODES:
|
---|
239 | case OPT_NOENC:
|
---|
240 | /*
|
---|
241 | * |enc_flag| stores the name of the option used so it
|
---|
242 | * can be printed if an error message is output.
|
---|
243 | */
|
---|
244 | enc_flag = opt_flag() + 1;
|
---|
245 | enc = NULL;
|
---|
246 | ciphername = NULL;
|
---|
247 | break;
|
---|
248 | case OPT_CIPHER:
|
---|
249 | ciphername = opt_unknown();
|
---|
250 | enc_flag = opt_unknown();
|
---|
251 | break;
|
---|
252 | case OPT_ITER:
|
---|
253 | maciter = iter = opt_int_arg();
|
---|
254 | break;
|
---|
255 | case OPT_NOITER:
|
---|
256 | iter = 1;
|
---|
257 | break;
|
---|
258 | case OPT_MACITER:
|
---|
259 | /* no-op */
|
---|
260 | break;
|
---|
261 | case OPT_NOMACITER:
|
---|
262 | maciter = 1;
|
---|
263 | break;
|
---|
264 | case OPT_NOMAC:
|
---|
265 | cert_pbe = -1;
|
---|
266 | maciter = -1;
|
---|
267 | break;
|
---|
268 | case OPT_MACALG:
|
---|
269 | macalg = opt_arg();
|
---|
270 | break;
|
---|
271 | case OPT_CERTPBE:
|
---|
272 | if (!set_pbe(&cert_pbe, opt_arg()))
|
---|
273 | goto opthelp;
|
---|
274 | break;
|
---|
275 | case OPT_KEYPBE:
|
---|
276 | if (!set_pbe(&key_pbe, opt_arg()))
|
---|
277 | goto opthelp;
|
---|
278 | break;
|
---|
279 | case OPT_R_CASES:
|
---|
280 | if (!opt_rand(o))
|
---|
281 | goto end;
|
---|
282 | break;
|
---|
283 | case OPT_INKEY:
|
---|
284 | keyname = opt_arg();
|
---|
285 | break;
|
---|
286 | case OPT_CERTFILE:
|
---|
287 | certfile = opt_arg();
|
---|
288 | break;
|
---|
289 | case OPT_UNTRUSTED:
|
---|
290 | untrusted = opt_arg();
|
---|
291 | break;
|
---|
292 | case OPT_PASSCERTS:
|
---|
293 | passcertsarg = opt_arg();
|
---|
294 | break;
|
---|
295 | case OPT_NAME:
|
---|
296 | name = opt_arg();
|
---|
297 | break;
|
---|
298 | case OPT_LMK:
|
---|
299 | add_lmk = 1;
|
---|
300 | break;
|
---|
301 | case OPT_CSP:
|
---|
302 | csp_name = opt_arg();
|
---|
303 | break;
|
---|
304 | case OPT_CANAME:
|
---|
305 | if (canames == NULL
|
---|
306 | && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
|
---|
307 | goto end;
|
---|
308 | sk_OPENSSL_STRING_push(canames, opt_arg());
|
---|
309 | break;
|
---|
310 | case OPT_IN:
|
---|
311 | infile = opt_arg();
|
---|
312 | break;
|
---|
313 | case OPT_OUT:
|
---|
314 | outfile = opt_arg();
|
---|
315 | break;
|
---|
316 | case OPT_PASSIN:
|
---|
317 | passinarg = opt_arg();
|
---|
318 | break;
|
---|
319 | case OPT_PASSOUT:
|
---|
320 | passoutarg = opt_arg();
|
---|
321 | break;
|
---|
322 | case OPT_PASSWORD:
|
---|
323 | passarg = opt_arg();
|
---|
324 | break;
|
---|
325 | case OPT_CAPATH:
|
---|
326 | CApath = opt_arg();
|
---|
327 | break;
|
---|
328 | case OPT_CASTORE:
|
---|
329 | CAstore = opt_arg();
|
---|
330 | break;
|
---|
331 | case OPT_CAFILE:
|
---|
332 | CAfile = opt_arg();
|
---|
333 | break;
|
---|
334 | case OPT_NOCAPATH:
|
---|
335 | noCApath = 1;
|
---|
336 | break;
|
---|
337 | case OPT_NOCASTORE:
|
---|
338 | noCAstore = 1;
|
---|
339 | break;
|
---|
340 | case OPT_NOCAFILE:
|
---|
341 | noCAfile = 1;
|
---|
342 | break;
|
---|
343 | case OPT_ENGINE:
|
---|
344 | e = setup_engine(opt_arg(), 0);
|
---|
345 | break;
|
---|
346 | #ifndef OPENSSL_NO_DES
|
---|
347 | case OPT_LEGACY_ALG:
|
---|
348 | use_legacy = 1;
|
---|
349 | break;
|
---|
350 | #endif
|
---|
351 | case OPT_PROV_CASES:
|
---|
352 | if (!opt_provider(o))
|
---|
353 | goto end;
|
---|
354 | break;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* No extra arguments. */
|
---|
359 | argc = opt_num_rest();
|
---|
360 | if (argc != 0)
|
---|
361 | goto opthelp;
|
---|
362 |
|
---|
363 | if (!app_RAND_load())
|
---|
364 | goto end;
|
---|
365 |
|
---|
366 | if (ciphername != NULL) {
|
---|
367 | if (!opt_cipher_any(ciphername, &enc))
|
---|
368 | goto opthelp;
|
---|
369 | }
|
---|
370 | if (export_pkcs12) {
|
---|
371 | if ((options & INFO) != 0)
|
---|
372 | WARN_EXPORT("info");
|
---|
373 | if (macver == 0)
|
---|
374 | WARN_EXPORT("nomacver");
|
---|
375 | if ((options & CLCERTS) != 0)
|
---|
376 | WARN_EXPORT("clcerts");
|
---|
377 | if ((options & CACERTS) != 0)
|
---|
378 | WARN_EXPORT("cacerts");
|
---|
379 | if (enc != default_enc)
|
---|
380 | BIO_printf(bio_err,
|
---|
381 | "Warning: output encryption option -%s ignored with -export\n", enc_flag);
|
---|
382 | } else {
|
---|
383 | if (keyname != NULL)
|
---|
384 | WARN_NO_EXPORT("inkey");
|
---|
385 | if (certfile != NULL)
|
---|
386 | WARN_NO_EXPORT("certfile");
|
---|
387 | if (passcertsarg != NULL)
|
---|
388 | WARN_NO_EXPORT("passcerts");
|
---|
389 | if (chain)
|
---|
390 | WARN_NO_EXPORT("chain");
|
---|
391 | if (untrusted != NULL)
|
---|
392 | WARN_NO_EXPORT("untrusted");
|
---|
393 | if (CAfile != NULL)
|
---|
394 | WARN_NO_EXPORT("CAfile");
|
---|
395 | if (CApath != NULL)
|
---|
396 | WARN_NO_EXPORT("CApath");
|
---|
397 | if (CAstore != NULL)
|
---|
398 | WARN_NO_EXPORT("CAstore");
|
---|
399 | if (noCAfile)
|
---|
400 | WARN_NO_EXPORT("no-CAfile");
|
---|
401 | if (noCApath)
|
---|
402 | WARN_NO_EXPORT("no-CApath");
|
---|
403 | if (noCAstore)
|
---|
404 | WARN_NO_EXPORT("no-CAstore");
|
---|
405 | if (name != NULL)
|
---|
406 | WARN_NO_EXPORT("name");
|
---|
407 | if (canames != NULL)
|
---|
408 | WARN_NO_EXPORT("caname");
|
---|
409 | if (csp_name != NULL)
|
---|
410 | WARN_NO_EXPORT("CSP");
|
---|
411 | if (add_lmk)
|
---|
412 | WARN_NO_EXPORT("LMK");
|
---|
413 | if (keytype == KEY_EX)
|
---|
414 | WARN_NO_EXPORT("keyex");
|
---|
415 | if (keytype == KEY_SIG)
|
---|
416 | WARN_NO_EXPORT("keysig");
|
---|
417 | if (key_pbe != NID_undef)
|
---|
418 | WARN_NO_EXPORT("keypbe");
|
---|
419 | if (cert_pbe != NID_undef && cert_pbe != -1)
|
---|
420 | WARN_NO_EXPORT("certpbe and -descert");
|
---|
421 | if (macalg != NULL)
|
---|
422 | WARN_NO_EXPORT("macalg");
|
---|
423 | if (iter != 0)
|
---|
424 | WARN_NO_EXPORT("iter and -noiter");
|
---|
425 | if (maciter == 1)
|
---|
426 | WARN_NO_EXPORT("nomaciter");
|
---|
427 | if (cert_pbe == -1 && maciter == -1)
|
---|
428 | WARN_NO_EXPORT("nomac");
|
---|
429 | }
|
---|
430 | #ifndef OPENSSL_NO_DES
|
---|
431 | if (use_legacy) {
|
---|
432 | /* load the legacy provider if not loaded already*/
|
---|
433 | if (!OSSL_PROVIDER_available(app_get0_libctx(), "legacy")) {
|
---|
434 | if (!app_provider_load(app_get0_libctx(), "legacy"))
|
---|
435 | goto end;
|
---|
436 | /* load the default provider explicitly */
|
---|
437 | if (!app_provider_load(app_get0_libctx(), "default"))
|
---|
438 | goto end;
|
---|
439 | }
|
---|
440 | if (cert_pbe == NID_undef) {
|
---|
441 | /* Adapt default algorithm */
|
---|
442 | # ifndef OPENSSL_NO_RC2
|
---|
443 | cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
|
---|
444 | # else
|
---|
445 | cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
|
---|
446 | # endif
|
---|
447 | }
|
---|
448 |
|
---|
449 | if (key_pbe == NID_undef)
|
---|
450 | key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
|
---|
451 | if (enc == default_enc)
|
---|
452 | enc = (EVP_CIPHER *)EVP_des_ede3_cbc();
|
---|
453 | if (macalg == NULL)
|
---|
454 | macalg = "sha1";
|
---|
455 | }
|
---|
456 | #endif
|
---|
457 |
|
---|
458 | private = 1;
|
---|
459 |
|
---|
460 | if (!app_passwd(passcertsarg, NULL, &passcerts, NULL)) {
|
---|
461 | BIO_printf(bio_err, "Error getting certificate file password\n");
|
---|
462 | goto end;
|
---|
463 | }
|
---|
464 |
|
---|
465 | if (passarg != NULL) {
|
---|
466 | if (export_pkcs12)
|
---|
467 | passoutarg = passarg;
|
---|
468 | else
|
---|
469 | passinarg = passarg;
|
---|
470 | }
|
---|
471 |
|
---|
472 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
|
---|
473 | BIO_printf(bio_err, "Error getting passwords\n");
|
---|
474 | goto end;
|
---|
475 | }
|
---|
476 |
|
---|
477 | if (cpass == NULL) {
|
---|
478 | if (export_pkcs12)
|
---|
479 | cpass = passout;
|
---|
480 | else
|
---|
481 | cpass = passin;
|
---|
482 | }
|
---|
483 |
|
---|
484 | if (cpass != NULL) {
|
---|
485 | mpass = cpass;
|
---|
486 | noprompt = 1;
|
---|
487 | if (twopass) {
|
---|
488 | if (export_pkcs12)
|
---|
489 | BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
|
---|
490 | else
|
---|
491 | BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
|
---|
492 | goto end;
|
---|
493 | }
|
---|
494 | } else {
|
---|
495 | cpass = pass;
|
---|
496 | mpass = macpass;
|
---|
497 | }
|
---|
498 |
|
---|
499 | if (twopass) {
|
---|
500 | /* To avoid bit rot */
|
---|
501 | if (1) {
|
---|
502 | #ifndef OPENSSL_NO_UI_CONSOLE
|
---|
503 | if (EVP_read_pw_string(
|
---|
504 | macpass, sizeof(macpass), "Enter MAC Password:", export_pkcs12)) {
|
---|
505 | BIO_printf(bio_err, "Can't read Password\n");
|
---|
506 | goto end;
|
---|
507 | }
|
---|
508 | } else {
|
---|
509 | #endif
|
---|
510 | BIO_printf(bio_err, "Unsupported option -twopass\n");
|
---|
511 | goto end;
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | if (export_pkcs12) {
|
---|
516 | EVP_PKEY *key = NULL;
|
---|
517 | X509 *ee_cert = NULL, *x = NULL;
|
---|
518 | STACK_OF(X509) *certs = NULL;
|
---|
519 | STACK_OF(X509) *untrusted_certs = NULL;
|
---|
520 | EVP_MD *macmd = NULL;
|
---|
521 | unsigned char *catmp = NULL;
|
---|
522 | int i;
|
---|
523 |
|
---|
524 | if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
|
---|
525 | BIO_printf(bio_err, "Nothing to export due to -noout or -nocerts and -nokeys\n");
|
---|
526 | goto export_end;
|
---|
527 | }
|
---|
528 |
|
---|
529 | if ((options & NOCERTS) != 0) {
|
---|
530 | chain = 0;
|
---|
531 | BIO_printf(bio_err, "Warning: -chain option ignored with -nocerts\n");
|
---|
532 | }
|
---|
533 |
|
---|
534 | if (!(options & NOKEYS)) {
|
---|
535 | key = load_key(keyname ? keyname : infile,
|
---|
536 | FORMAT_PEM, 1, passin, e,
|
---|
537 | keyname ?
|
---|
538 | "private key from -inkey file" :
|
---|
539 | "private key from -in file");
|
---|
540 | if (key == NULL)
|
---|
541 | goto export_end;
|
---|
542 | }
|
---|
543 |
|
---|
544 | /* Load all certs in input file */
|
---|
545 | if (!(options & NOCERTS)) {
|
---|
546 | if (!load_certs(infile, 1, &certs, passin,
|
---|
547 | "certificates from -in file"))
|
---|
548 | goto export_end;
|
---|
549 | if (sk_X509_num(certs) < 1) {
|
---|
550 | BIO_printf(bio_err, "No certificate in -in file %s\n", infile);
|
---|
551 | goto export_end;
|
---|
552 | }
|
---|
553 |
|
---|
554 | if (key != NULL) {
|
---|
555 | /* Look for matching private key */
|
---|
556 | for (i = 0; i < sk_X509_num(certs); i++) {
|
---|
557 | x = sk_X509_value(certs, i);
|
---|
558 | if (X509_check_private_key(x, key)) {
|
---|
559 | ee_cert = x;
|
---|
560 | /* Zero keyid and alias */
|
---|
561 | X509_keyid_set1(ee_cert, NULL, 0);
|
---|
562 | X509_alias_set1(ee_cert, NULL, 0);
|
---|
563 | /* Remove from list */
|
---|
564 | (void)sk_X509_delete(certs, i);
|
---|
565 | break;
|
---|
566 | }
|
---|
567 | }
|
---|
568 | if (ee_cert == NULL) {
|
---|
569 | BIO_printf(bio_err,
|
---|
570 | "No cert in -in file '%s' matches private key\n",
|
---|
571 | infile);
|
---|
572 | goto export_end;
|
---|
573 | }
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | /* Load any untrusted certificates for chain building */
|
---|
578 | if (untrusted != NULL) {
|
---|
579 | if (!load_certs(untrusted, 0, &untrusted_certs, passcerts,
|
---|
580 | "untrusted certificates"))
|
---|
581 | goto export_end;
|
---|
582 | }
|
---|
583 |
|
---|
584 | /* If chaining get chain from end entity cert */
|
---|
585 | if (chain) {
|
---|
586 | int vret;
|
---|
587 | STACK_OF(X509) *chain2;
|
---|
588 | X509_STORE *store;
|
---|
589 | X509 *ee_cert_tmp = ee_cert;
|
---|
590 |
|
---|
591 | /* Assume the first cert if we haven't got anything else */
|
---|
592 | if (ee_cert_tmp == NULL && certs != NULL)
|
---|
593 | ee_cert_tmp = sk_X509_value(certs, 0);
|
---|
594 |
|
---|
595 | if (ee_cert_tmp == NULL) {
|
---|
596 | BIO_printf(bio_err,
|
---|
597 | "No end entity certificate to check with -chain\n");
|
---|
598 | goto export_end;
|
---|
599 | }
|
---|
600 |
|
---|
601 | if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
|
---|
602 | CAstore, noCAstore))
|
---|
603 | == NULL)
|
---|
604 | goto export_end;
|
---|
605 |
|
---|
606 | vret = get_cert_chain(ee_cert_tmp, store, untrusted_certs, &chain2);
|
---|
607 | X509_STORE_free(store);
|
---|
608 |
|
---|
609 | if (vret == X509_V_OK) {
|
---|
610 | int add_certs;
|
---|
611 | /* Remove from chain2 the first (end entity) certificate */
|
---|
612 | X509_free(sk_X509_shift(chain2));
|
---|
613 | /* Add the remaining certs (except for duplicates) */
|
---|
614 | add_certs = X509_add_certs(certs, chain2, X509_ADD_FLAG_UP_REF
|
---|
615 | | X509_ADD_FLAG_NO_DUP);
|
---|
616 | sk_X509_pop_free(chain2, X509_free);
|
---|
617 | if (!add_certs)
|
---|
618 | goto export_end;
|
---|
619 | } else {
|
---|
620 | if (vret != X509_V_ERR_UNSPECIFIED)
|
---|
621 | BIO_printf(bio_err, "Error getting chain: %s\n",
|
---|
622 | X509_verify_cert_error_string(vret));
|
---|
623 | goto export_end;
|
---|
624 | }
|
---|
625 | }
|
---|
626 |
|
---|
627 | /* Add any extra certificates asked for */
|
---|
628 | if (certfile != NULL) {
|
---|
629 | if (!load_certs(certfile, 0, &certs, passcerts,
|
---|
630 | "extra certificates from -certfile"))
|
---|
631 | goto export_end;
|
---|
632 | }
|
---|
633 |
|
---|
634 | /* Add any CA names */
|
---|
635 | for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
|
---|
636 | catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
|
---|
637 | X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
|
---|
638 | }
|
---|
639 |
|
---|
640 | if (csp_name != NULL && key != NULL)
|
---|
641 | EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
|
---|
642 | MBSTRING_ASC, (unsigned char *)csp_name,
|
---|
643 | -1);
|
---|
644 |
|
---|
645 | if (add_lmk && key != NULL)
|
---|
646 | EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
|
---|
647 |
|
---|
648 | if (!noprompt && !(enc == NULL && maciter == -1)) {
|
---|
649 | /* To avoid bit rot */
|
---|
650 | if (1) {
|
---|
651 | #ifndef OPENSSL_NO_UI_CONSOLE
|
---|
652 | if (EVP_read_pw_string(pass, sizeof(pass),
|
---|
653 | "Enter Export Password:", 1)) {
|
---|
654 | BIO_printf(bio_err, "Can't read Password\n");
|
---|
655 | goto export_end;
|
---|
656 | }
|
---|
657 | } else {
|
---|
658 | #endif
|
---|
659 | BIO_printf(bio_err, "Password required\n");
|
---|
660 | goto export_end;
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | if (!twopass)
|
---|
665 | OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
|
---|
666 |
|
---|
667 | p12 = PKCS12_create_ex(cpass, name, key, ee_cert, certs,
|
---|
668 | key_pbe, cert_pbe, iter, -1, keytype,
|
---|
669 | app_get0_libctx(), app_get0_propq());
|
---|
670 |
|
---|
671 | if (p12 == NULL) {
|
---|
672 | BIO_printf(bio_err, "Error creating PKCS12 structure for %s\n",
|
---|
673 | outfile);
|
---|
674 | goto export_end;
|
---|
675 | }
|
---|
676 |
|
---|
677 | if (macalg != NULL) {
|
---|
678 | if (!opt_md(macalg, &macmd))
|
---|
679 | goto opthelp;
|
---|
680 | }
|
---|
681 |
|
---|
682 | if (maciter != -1)
|
---|
683 | if (!PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd)) {
|
---|
684 | BIO_printf(bio_err, "Error creating PKCS12 MAC; no PKCS12KDF support?\n");
|
---|
685 | BIO_printf(bio_err, "Use -nomac if MAC not required and PKCS12KDF support not available.\n");
|
---|
686 | goto export_end;
|
---|
687 | }
|
---|
688 |
|
---|
689 | assert(private);
|
---|
690 |
|
---|
691 | out = bio_open_owner(outfile, FORMAT_PKCS12, private);
|
---|
692 | if (out == NULL)
|
---|
693 | goto end;
|
---|
694 |
|
---|
695 | i2d_PKCS12_bio(out, p12);
|
---|
696 |
|
---|
697 | ret = 0;
|
---|
698 |
|
---|
699 | export_end:
|
---|
700 |
|
---|
701 | EVP_PKEY_free(key);
|
---|
702 | EVP_MD_free(macmd);
|
---|
703 | sk_X509_pop_free(certs, X509_free);
|
---|
704 | sk_X509_pop_free(untrusted_certs, X509_free);
|
---|
705 | X509_free(ee_cert);
|
---|
706 |
|
---|
707 | ERR_print_errors(bio_err);
|
---|
708 | goto end;
|
---|
709 |
|
---|
710 | }
|
---|
711 |
|
---|
712 | in = bio_open_default(infile, 'r', FORMAT_PKCS12);
|
---|
713 | if (in == NULL)
|
---|
714 | goto end;
|
---|
715 | out = bio_open_owner(outfile, FORMAT_PEM, private);
|
---|
716 | if (out == NULL)
|
---|
717 | goto end;
|
---|
718 |
|
---|
719 | p12 = PKCS12_init_ex(NID_pkcs7_data, app_get0_libctx(), app_get0_propq());
|
---|
720 | if (p12 == NULL) {
|
---|
721 | ERR_print_errors(bio_err);
|
---|
722 | goto end;
|
---|
723 | }
|
---|
724 | if ((p12 = d2i_PKCS12_bio(in, &p12)) == NULL) {
|
---|
725 | ERR_print_errors(bio_err);
|
---|
726 | goto end;
|
---|
727 | }
|
---|
728 |
|
---|
729 | if (!noprompt) {
|
---|
730 | if (1) {
|
---|
731 | #ifndef OPENSSL_NO_UI_CONSOLE
|
---|
732 | if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
|
---|
733 | 0)) {
|
---|
734 | BIO_printf(bio_err, "Can't read Password\n");
|
---|
735 | goto end;
|
---|
736 | }
|
---|
737 | } else {
|
---|
738 | #endif
|
---|
739 | BIO_printf(bio_err, "Password required\n");
|
---|
740 | goto end;
|
---|
741 | }
|
---|
742 | }
|
---|
743 |
|
---|
744 | if (!twopass)
|
---|
745 | OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
|
---|
746 |
|
---|
747 | if ((options & INFO) && PKCS12_mac_present(p12)) {
|
---|
748 | const ASN1_INTEGER *tmaciter;
|
---|
749 | const X509_ALGOR *macalgid;
|
---|
750 | const ASN1_OBJECT *macobj;
|
---|
751 | const ASN1_OCTET_STRING *tmac;
|
---|
752 | const ASN1_OCTET_STRING *tsalt;
|
---|
753 |
|
---|
754 | PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
|
---|
755 | /* current hash algorithms do not use parameters so extract just name,
|
---|
756 | in future alg_print() may be needed */
|
---|
757 | X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
|
---|
758 | BIO_puts(bio_err, "MAC: ");
|
---|
759 | i2a_ASN1_OBJECT(bio_err, macobj);
|
---|
760 | BIO_printf(bio_err, ", Iteration %ld\n",
|
---|
761 | tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
|
---|
762 | BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
|
---|
763 | tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
|
---|
764 | tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
|
---|
765 | }
|
---|
766 | if (macver) {
|
---|
767 | EVP_KDF *pkcs12kdf;
|
---|
768 |
|
---|
769 | pkcs12kdf = EVP_KDF_fetch(app_get0_libctx(), "PKCS12KDF",
|
---|
770 | app_get0_propq());
|
---|
771 | if (pkcs12kdf == NULL) {
|
---|
772 | BIO_printf(bio_err, "Error verifying PKCS12 MAC; no PKCS12KDF support.\n");
|
---|
773 | BIO_printf(bio_err, "Use -nomacver if MAC verification is not required.\n");
|
---|
774 | goto end;
|
---|
775 | }
|
---|
776 | EVP_KDF_free(pkcs12kdf);
|
---|
777 | /* If we enter empty password try no password first */
|
---|
778 | if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
|
---|
779 | /* If mac and crypto pass the same set it to NULL too */
|
---|
780 | if (!twopass)
|
---|
781 | cpass = NULL;
|
---|
782 | } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
|
---|
783 | /*
|
---|
784 | * May be UTF8 from previous version of OpenSSL:
|
---|
785 | * convert to a UTF8 form which will translate
|
---|
786 | * to the same Unicode password.
|
---|
787 | */
|
---|
788 | unsigned char *utmp;
|
---|
789 | int utmplen;
|
---|
790 | unsigned long err = ERR_peek_error();
|
---|
791 |
|
---|
792 | if (ERR_GET_LIB(err) == ERR_LIB_PKCS12
|
---|
793 | && ERR_GET_REASON(err) == PKCS12_R_MAC_ABSENT) {
|
---|
794 | BIO_printf(bio_err, "Warning: MAC is absent!\n");
|
---|
795 | goto dump;
|
---|
796 | }
|
---|
797 |
|
---|
798 | utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
|
---|
799 | if (utmp == NULL)
|
---|
800 | goto end;
|
---|
801 | badpass = OPENSSL_uni2utf8(utmp, utmplen);
|
---|
802 | OPENSSL_free(utmp);
|
---|
803 | if (!PKCS12_verify_mac(p12, badpass, -1)) {
|
---|
804 | BIO_printf(bio_err, "Mac verify error: invalid password?\n");
|
---|
805 | ERR_print_errors(bio_err);
|
---|
806 | goto end;
|
---|
807 | } else {
|
---|
808 | BIO_printf(bio_err, "Warning: using broken algorithm\n");
|
---|
809 | if (!twopass)
|
---|
810 | cpass = badpass;
|
---|
811 | }
|
---|
812 | }
|
---|
813 | }
|
---|
814 |
|
---|
815 | dump:
|
---|
816 | assert(private);
|
---|
817 | if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
|
---|
818 | BIO_printf(bio_err, "Error outputting keys and certificates\n");
|
---|
819 | ERR_print_errors(bio_err);
|
---|
820 | goto end;
|
---|
821 | }
|
---|
822 | ret = 0;
|
---|
823 | end:
|
---|
824 | PKCS12_free(p12);
|
---|
825 | release_engine(e);
|
---|
826 | BIO_free(in);
|
---|
827 | BIO_free_all(out);
|
---|
828 | sk_OPENSSL_STRING_free(canames);
|
---|
829 | OPENSSL_free(badpass);
|
---|
830 | OPENSSL_free(passcerts);
|
---|
831 | OPENSSL_free(passin);
|
---|
832 | OPENSSL_free(passout);
|
---|
833 | return ret;
|
---|
834 | }
|
---|
835 |
|
---|
836 | int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
|
---|
837 | int passlen, int options, char *pempass,
|
---|
838 | const EVP_CIPHER *enc)
|
---|
839 | {
|
---|
840 | STACK_OF(PKCS7) *asafes = NULL;
|
---|
841 | STACK_OF(PKCS12_SAFEBAG) *bags;
|
---|
842 | int i, bagnid;
|
---|
843 | int ret = 0;
|
---|
844 | PKCS7 *p7;
|
---|
845 |
|
---|
846 | if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
|
---|
847 | return 0;
|
---|
848 | for (i = 0; i < sk_PKCS7_num(asafes); i++) {
|
---|
849 | p7 = sk_PKCS7_value(asafes, i);
|
---|
850 | bagnid = OBJ_obj2nid(p7->type);
|
---|
851 | if (bagnid == NID_pkcs7_data) {
|
---|
852 | bags = PKCS12_unpack_p7data(p7);
|
---|
853 | if (options & INFO)
|
---|
854 | BIO_printf(bio_err, "PKCS7 Data\n");
|
---|
855 | } else if (bagnid == NID_pkcs7_encrypted) {
|
---|
856 | if (options & INFO) {
|
---|
857 | BIO_printf(bio_err, "PKCS7 Encrypted data: ");
|
---|
858 | alg_print(p7->d.encrypted->enc_data->algorithm);
|
---|
859 | }
|
---|
860 | bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
|
---|
861 | } else {
|
---|
862 | continue;
|
---|
863 | }
|
---|
864 | if (!bags)
|
---|
865 | goto err;
|
---|
866 | if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
|
---|
867 | options, pempass, enc)) {
|
---|
868 | sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
|
---|
869 | goto err;
|
---|
870 | }
|
---|
871 | sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
|
---|
872 | bags = NULL;
|
---|
873 | }
|
---|
874 | ret = 1;
|
---|
875 |
|
---|
876 | err:
|
---|
877 | sk_PKCS7_pop_free(asafes, PKCS7_free);
|
---|
878 | return ret;
|
---|
879 | }
|
---|
880 |
|
---|
881 | int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
|
---|
882 | const char *pass, int passlen, int options,
|
---|
883 | char *pempass, const EVP_CIPHER *enc)
|
---|
884 | {
|
---|
885 | int i;
|
---|
886 | for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
|
---|
887 | if (!dump_certs_pkeys_bag(out,
|
---|
888 | sk_PKCS12_SAFEBAG_value(bags, i),
|
---|
889 | pass, passlen, options, pempass, enc))
|
---|
890 | return 0;
|
---|
891 | }
|
---|
892 | return 1;
|
---|
893 | }
|
---|
894 |
|
---|
895 | int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
|
---|
896 | const char *pass, int passlen, int options,
|
---|
897 | char *pempass, const EVP_CIPHER *enc)
|
---|
898 | {
|
---|
899 | EVP_PKEY *pkey;
|
---|
900 | PKCS8_PRIV_KEY_INFO *p8;
|
---|
901 | const PKCS8_PRIV_KEY_INFO *p8c;
|
---|
902 | X509 *x509;
|
---|
903 | const STACK_OF(X509_ATTRIBUTE) *attrs;
|
---|
904 | int ret = 0;
|
---|
905 |
|
---|
906 | attrs = PKCS12_SAFEBAG_get0_attrs(bag);
|
---|
907 |
|
---|
908 | switch (PKCS12_SAFEBAG_get_nid(bag)) {
|
---|
909 | case NID_keyBag:
|
---|
910 | if (options & INFO)
|
---|
911 | BIO_printf(bio_err, "Key bag\n");
|
---|
912 | if (options & NOKEYS)
|
---|
913 | return 1;
|
---|
914 | print_attribs(out, attrs, "Bag Attributes");
|
---|
915 | p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
|
---|
916 | if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
|
---|
917 | return 0;
|
---|
918 | print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
|
---|
919 | ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
|
---|
920 | EVP_PKEY_free(pkey);
|
---|
921 | break;
|
---|
922 |
|
---|
923 | case NID_pkcs8ShroudedKeyBag:
|
---|
924 | if (options & INFO) {
|
---|
925 | const X509_SIG *tp8;
|
---|
926 | const X509_ALGOR *tp8alg;
|
---|
927 |
|
---|
928 | BIO_printf(bio_err, "Shrouded Keybag: ");
|
---|
929 | tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
|
---|
930 | X509_SIG_get0(tp8, &tp8alg, NULL);
|
---|
931 | alg_print(tp8alg);
|
---|
932 | }
|
---|
933 | if (options & NOKEYS)
|
---|
934 | return 1;
|
---|
935 | print_attribs(out, attrs, "Bag Attributes");
|
---|
936 | if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
|
---|
937 | return 0;
|
---|
938 | if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
|
---|
939 | PKCS8_PRIV_KEY_INFO_free(p8);
|
---|
940 | return 0;
|
---|
941 | }
|
---|
942 | print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
|
---|
943 | PKCS8_PRIV_KEY_INFO_free(p8);
|
---|
944 | ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
|
---|
945 | EVP_PKEY_free(pkey);
|
---|
946 | break;
|
---|
947 |
|
---|
948 | case NID_certBag:
|
---|
949 | if (options & INFO)
|
---|
950 | BIO_printf(bio_err, "Certificate bag\n");
|
---|
951 | if (options & NOCERTS)
|
---|
952 | return 1;
|
---|
953 | if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
|
---|
954 | if (options & CACERTS)
|
---|
955 | return 1;
|
---|
956 | } else if (options & CLCERTS)
|
---|
957 | return 1;
|
---|
958 | print_attribs(out, attrs, "Bag Attributes");
|
---|
959 | if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
|
---|
960 | return 1;
|
---|
961 | if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
|
---|
962 | return 0;
|
---|
963 | dump_cert_text(out, x509);
|
---|
964 | ret = PEM_write_bio_X509(out, x509);
|
---|
965 | X509_free(x509);
|
---|
966 | break;
|
---|
967 |
|
---|
968 | case NID_secretBag:
|
---|
969 | if (options & INFO)
|
---|
970 | BIO_printf(bio_err, "Secret bag\n");
|
---|
971 | print_attribs(out, attrs, "Bag Attributes");
|
---|
972 | BIO_printf(bio_err, "Bag Type: ");
|
---|
973 | i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
|
---|
974 | BIO_printf(bio_err, "\nBag Value: ");
|
---|
975 | print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
|
---|
976 | return 1;
|
---|
977 |
|
---|
978 | case NID_safeContentsBag:
|
---|
979 | if (options & INFO)
|
---|
980 | BIO_printf(bio_err, "Safe Contents bag\n");
|
---|
981 | print_attribs(out, attrs, "Bag Attributes");
|
---|
982 | return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
|
---|
983 | pass, passlen, options, pempass, enc);
|
---|
984 |
|
---|
985 | default:
|
---|
986 | BIO_printf(bio_err, "Warning unsupported bag type: ");
|
---|
987 | i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
|
---|
988 | BIO_printf(bio_err, "\n");
|
---|
989 | return 1;
|
---|
990 | }
|
---|
991 | return ret;
|
---|
992 | }
|
---|
993 |
|
---|
994 | /* Given a single certificate return a verified chain or NULL if error */
|
---|
995 |
|
---|
996 | static int get_cert_chain(X509 *cert, X509_STORE *store,
|
---|
997 | STACK_OF(X509) *untrusted_certs,
|
---|
998 | STACK_OF(X509) **chain)
|
---|
999 | {
|
---|
1000 | X509_STORE_CTX *store_ctx = NULL;
|
---|
1001 | STACK_OF(X509) *chn = NULL;
|
---|
1002 | int i = 0;
|
---|
1003 |
|
---|
1004 | store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
|
---|
1005 | if (store_ctx == NULL) {
|
---|
1006 | i = X509_V_ERR_UNSPECIFIED;
|
---|
1007 | goto end;
|
---|
1008 | }
|
---|
1009 | if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
|
---|
1010 | i = X509_V_ERR_UNSPECIFIED;
|
---|
1011 | goto end;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 |
|
---|
1015 | if (X509_verify_cert(store_ctx) > 0)
|
---|
1016 | chn = X509_STORE_CTX_get1_chain(store_ctx);
|
---|
1017 | else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
|
---|
1018 | i = X509_V_ERR_UNSPECIFIED;
|
---|
1019 |
|
---|
1020 | end:
|
---|
1021 | X509_STORE_CTX_free(store_ctx);
|
---|
1022 | *chain = chn;
|
---|
1023 | return i;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | static int alg_print(const X509_ALGOR *alg)
|
---|
1027 | {
|
---|
1028 | int pbenid, aparamtype;
|
---|
1029 | const ASN1_OBJECT *aoid;
|
---|
1030 | const void *aparam;
|
---|
1031 | PBEPARAM *pbe = NULL;
|
---|
1032 |
|
---|
1033 | X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
|
---|
1034 |
|
---|
1035 | pbenid = OBJ_obj2nid(aoid);
|
---|
1036 |
|
---|
1037 | BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
|
---|
1038 |
|
---|
1039 | /*
|
---|
1040 | * If PBE algorithm is PBES2 decode algorithm parameters
|
---|
1041 | * for additional details.
|
---|
1042 | */
|
---|
1043 | if (pbenid == NID_pbes2) {
|
---|
1044 | PBE2PARAM *pbe2 = NULL;
|
---|
1045 | int encnid;
|
---|
1046 | if (aparamtype == V_ASN1_SEQUENCE)
|
---|
1047 | pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
|
---|
1048 | if (pbe2 == NULL) {
|
---|
1049 | BIO_puts(bio_err, ", <unsupported parameters>");
|
---|
1050 | goto done;
|
---|
1051 | }
|
---|
1052 | X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
|
---|
1053 | pbenid = OBJ_obj2nid(aoid);
|
---|
1054 | X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
|
---|
1055 | encnid = OBJ_obj2nid(aoid);
|
---|
1056 | BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
|
---|
1057 | OBJ_nid2sn(encnid));
|
---|
1058 | /* If KDF is PBKDF2 decode parameters */
|
---|
1059 | if (pbenid == NID_id_pbkdf2) {
|
---|
1060 | PBKDF2PARAM *kdf = NULL;
|
---|
1061 | int prfnid;
|
---|
1062 | if (aparamtype == V_ASN1_SEQUENCE)
|
---|
1063 | kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
|
---|
1064 | if (kdf == NULL) {
|
---|
1065 | BIO_puts(bio_err, ", <unsupported parameters>");
|
---|
1066 | goto done;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | if (kdf->prf == NULL) {
|
---|
1070 | prfnid = NID_hmacWithSHA1;
|
---|
1071 | } else {
|
---|
1072 | X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
|
---|
1073 | prfnid = OBJ_obj2nid(aoid);
|
---|
1074 | }
|
---|
1075 | BIO_printf(bio_err, ", Iteration %ld, PRF %s",
|
---|
1076 | ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
|
---|
1077 | PBKDF2PARAM_free(kdf);
|
---|
1078 | #ifndef OPENSSL_NO_SCRYPT
|
---|
1079 | } else if (pbenid == NID_id_scrypt) {
|
---|
1080 | SCRYPT_PARAMS *kdf = NULL;
|
---|
1081 |
|
---|
1082 | if (aparamtype == V_ASN1_SEQUENCE)
|
---|
1083 | kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
|
---|
1084 | if (kdf == NULL) {
|
---|
1085 | BIO_puts(bio_err, ", <unsupported parameters>");
|
---|
1086 | goto done;
|
---|
1087 | }
|
---|
1088 | BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
|
---|
1089 | "Block size(r): %ld, Parallelism(p): %ld",
|
---|
1090 | ASN1_STRING_length(kdf->salt),
|
---|
1091 | ASN1_INTEGER_get(kdf->costParameter),
|
---|
1092 | ASN1_INTEGER_get(kdf->blockSize),
|
---|
1093 | ASN1_INTEGER_get(kdf->parallelizationParameter));
|
---|
1094 | SCRYPT_PARAMS_free(kdf);
|
---|
1095 | #endif
|
---|
1096 | }
|
---|
1097 | PBE2PARAM_free(pbe2);
|
---|
1098 | } else {
|
---|
1099 | if (aparamtype == V_ASN1_SEQUENCE)
|
---|
1100 | pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
|
---|
1101 | if (pbe == NULL) {
|
---|
1102 | BIO_puts(bio_err, ", <unsupported parameters>");
|
---|
1103 | goto done;
|
---|
1104 | }
|
---|
1105 | BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
|
---|
1106 | PBEPARAM_free(pbe);
|
---|
1107 | }
|
---|
1108 | done:
|
---|
1109 | BIO_puts(bio_err, "\n");
|
---|
1110 | return 1;
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | /* Load all certificates from a given file */
|
---|
1114 |
|
---|
1115 | int cert_load(BIO *in, STACK_OF(X509) *sk)
|
---|
1116 | {
|
---|
1117 | int ret = 0;
|
---|
1118 | X509 *cert;
|
---|
1119 |
|
---|
1120 | while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
|
---|
1121 | ret = 1;
|
---|
1122 | if (!sk_X509_push(sk, cert))
|
---|
1123 | return 0;
|
---|
1124 | }
|
---|
1125 | if (ret)
|
---|
1126 | ERR_clear_error();
|
---|
1127 | return ret;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | /* Generalised x509 attribute value print */
|
---|
1131 |
|
---|
1132 | void print_attribute(BIO *out, const ASN1_TYPE *av)
|
---|
1133 | {
|
---|
1134 | char *value;
|
---|
1135 |
|
---|
1136 | switch (av->type) {
|
---|
1137 | case V_ASN1_BMPSTRING:
|
---|
1138 | value = OPENSSL_uni2asc(av->value.bmpstring->data,
|
---|
1139 | av->value.bmpstring->length);
|
---|
1140 | BIO_printf(out, "%s\n", value);
|
---|
1141 | OPENSSL_free(value);
|
---|
1142 | break;
|
---|
1143 |
|
---|
1144 | case V_ASN1_UTF8STRING:
|
---|
1145 | BIO_printf(out, "%.*s\n", av->value.utf8string->length,
|
---|
1146 | av->value.utf8string->data);
|
---|
1147 | break;
|
---|
1148 |
|
---|
1149 | case V_ASN1_OCTET_STRING:
|
---|
1150 | hex_prin(out, av->value.octet_string->data,
|
---|
1151 | av->value.octet_string->length);
|
---|
1152 | BIO_printf(out, "\n");
|
---|
1153 | break;
|
---|
1154 |
|
---|
1155 | case V_ASN1_BIT_STRING:
|
---|
1156 | hex_prin(out, av->value.bit_string->data,
|
---|
1157 | av->value.bit_string->length);
|
---|
1158 | BIO_printf(out, "\n");
|
---|
1159 | break;
|
---|
1160 |
|
---|
1161 | default:
|
---|
1162 | BIO_printf(out, "<Unsupported tag %d>\n", av->type);
|
---|
1163 | break;
|
---|
1164 | }
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | /* Generalised attribute print: handle PKCS#8 and bag attributes */
|
---|
1168 |
|
---|
1169 | int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
|
---|
1170 | const char *name)
|
---|
1171 | {
|
---|
1172 | X509_ATTRIBUTE *attr;
|
---|
1173 | ASN1_TYPE *av;
|
---|
1174 | int i, j, attr_nid;
|
---|
1175 | if (!attrlst) {
|
---|
1176 | BIO_printf(out, "%s: <No Attributes>\n", name);
|
---|
1177 | return 1;
|
---|
1178 | }
|
---|
1179 | if (!sk_X509_ATTRIBUTE_num(attrlst)) {
|
---|
1180 | BIO_printf(out, "%s: <Empty Attributes>\n", name);
|
---|
1181 | return 1;
|
---|
1182 | }
|
---|
1183 | BIO_printf(out, "%s\n", name);
|
---|
1184 | for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
|
---|
1185 | ASN1_OBJECT *attr_obj;
|
---|
1186 | attr = sk_X509_ATTRIBUTE_value(attrlst, i);
|
---|
1187 | attr_obj = X509_ATTRIBUTE_get0_object(attr);
|
---|
1188 | attr_nid = OBJ_obj2nid(attr_obj);
|
---|
1189 | BIO_printf(out, " ");
|
---|
1190 | if (attr_nid == NID_undef) {
|
---|
1191 | i2a_ASN1_OBJECT(out, attr_obj);
|
---|
1192 | BIO_printf(out, ": ");
|
---|
1193 | } else {
|
---|
1194 | BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | if (X509_ATTRIBUTE_count(attr)) {
|
---|
1198 | for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
|
---|
1199 | {
|
---|
1200 | av = X509_ATTRIBUTE_get0_type(attr, j);
|
---|
1201 | print_attribute(out, av);
|
---|
1202 | }
|
---|
1203 | } else {
|
---|
1204 | BIO_printf(out, "<No Values>\n");
|
---|
1205 | }
|
---|
1206 | }
|
---|
1207 | return 1;
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 | void hex_prin(BIO *out, unsigned char *buf, int len)
|
---|
1211 | {
|
---|
1212 | int i;
|
---|
1213 | for (i = 0; i < len; i++)
|
---|
1214 | BIO_printf(out, "%02X ", buf[i]);
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | static int set_pbe(int *ppbe, const char *str)
|
---|
1218 | {
|
---|
1219 | if (!str)
|
---|
1220 | return 0;
|
---|
1221 | if (strcmp(str, "NONE") == 0) {
|
---|
1222 | *ppbe = -1;
|
---|
1223 | return 1;
|
---|
1224 | }
|
---|
1225 | *ppbe = OBJ_txt2nid(str);
|
---|
1226 | if (*ppbe == NID_undef) {
|
---|
1227 | BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
|
---|
1228 | return 0;
|
---|
1229 | }
|
---|
1230 | return 1;
|
---|
1231 | }
|
---|