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