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