]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | |
27068df7 | 3 | * project. |
5a9a4b29 DSH |
4 | */ |
5 | /* ==================================================================== | |
5d7c222d | 6 | * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved. |
5a9a4b29 DSH |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
5a9a4b29 DSH |
14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * licensing@OpenSSL.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
58 | ||
59 | /* S/MIME utility function */ | |
60 | ||
61 | #include <stdio.h> | |
62 | #include <string.h> | |
36217a94 | 63 | #include "apps.h" |
11afb40c | 64 | #include <openssl/crypto.h> |
5a9a4b29 DSH |
65 | #include <openssl/pem.h> |
66 | #include <openssl/err.h> | |
5d7c222d DSH |
67 | #include <openssl/x509_vfy.h> |
68 | #include <openssl/x509v3.h> | |
5a9a4b29 | 69 | |
55ec5861 | 70 | static int save_certs(char *signerfile, STACK_OF(X509) *signers); |
5d7c222d | 71 | static int smime_cb(int ok, X509_STORE_CTX *ctx); |
5a9a4b29 | 72 | |
0f113f3e MC |
73 | #define SMIME_OP 0x10 |
74 | #define SMIME_IP 0x20 | |
75 | #define SMIME_SIGNERS 0x40 | |
76 | #define SMIME_ENCRYPT (1 | SMIME_OP) | |
77 | #define SMIME_DECRYPT (2 | SMIME_IP) | |
78 | #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS) | |
79 | #define SMIME_VERIFY (4 | SMIME_IP) | |
80 | #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP) | |
81 | #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS) | |
5a9a4b29 | 82 | |
7e1b7485 RS |
83 | typedef enum OPTION_choice { |
84 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
85 | OPT_ENCRYPT, OPT_DECRYPT, OPT_SIGN, OPT_RESIGN, OPT_VERIFY, | |
86 | OPT_PK7OUT, OPT_TEXT, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCHAIN, | |
87 | OPT_NOCERTS, OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP, | |
88 | OPT_BINARY, OPT_NOSIGS, OPT_STREAM, OPT_INDEF, OPT_NOINDEF, | |
89 | OPT_NOOLDMIME, OPT_CRLFEOL, OPT_RAND, OPT_ENGINE, OPT_PASSIN, | |
90 | OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, OPT_MD, | |
91 | OPT_CIPHER, OPT_INKEY, OPT_KEYFORM, OPT_CERTFILE, OPT_CAFILE, | |
92 | OPT_V_ENUM, | |
93 | OPT_CAPATH, OPT_IN, OPT_INFORM, OPT_OUT, OPT_OUTFORM, OPT_CONTENT | |
94 | } OPTION_CHOICE; | |
95 | ||
96 | OPTIONS smime_options[] = { | |
97 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"}, | |
98 | {OPT_HELP_STR, 1, '-', | |
99 | " cert.pem... recipient certs for encryption\n"}, | |
100 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, | |
101 | {"help", OPT_HELP, '-', "Display this summary"}, | |
102 | {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"}, | |
103 | {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"}, | |
104 | {"sign", OPT_SIGN, '-', "Sign message"}, | |
105 | {"verify", OPT_VERIFY, '-', "Verify signed message"}, | |
106 | {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"}, | |
107 | {"nointern", OPT_NOINTERN, '-', | |
108 | "Don't search certificates in message for signer"}, | |
109 | {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"}, | |
110 | {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"}, | |
111 | {"nocerts", OPT_NOCERTS, '-', | |
112 | "Don't include signers certificate when signing"}, | |
113 | {"nodetach", OPT_NODETACH, '-', "Use opaque signing"}, | |
114 | {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"}, | |
115 | {"binary", OPT_BINARY, '-', "Don't translate message to text"}, | |
116 | {"certfile", OPT_CERTFILE, '<', "Other certificates file"}, | |
117 | {"signer", OPT_SIGNER, '<', "Signer certificate file"}, | |
118 | {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"}, | |
119 | {"in", OPT_IN, '<', "Input file"}, | |
120 | {"inform", OPT_INFORM, 'F', "Input format SMIME (default), PEM or DER"}, | |
121 | {"inkey", OPT_INKEY, '<', | |
122 | "Input private key (if not signer or recipient)"}, | |
123 | {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"}, | |
124 | {"out", OPT_OUT, '>', "Output file"}, | |
125 | {"outform", OPT_OUTFORM, 'F', | |
126 | "Output format SMIME (default), PEM or DER"}, | |
127 | {"content", OPT_CONTENT, '<', | |
128 | "Supply or override content for detached signature"}, | |
129 | {"to", OPT_TO, 's', "To address"}, | |
130 | {"from", OPT_FROM, 's', "From address"}, | |
131 | {"subject", OPT_SUBJECT, 's', "Subject"}, | |
132 | {"text", OPT_TEXT, '-', "Include or delete text MIME headers"}, | |
133 | {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"}, | |
134 | {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"}, | |
135 | {"resign", OPT_RESIGN, '-'}, | |
136 | {"nochain", OPT_NOCHAIN, '-'}, | |
137 | {"nosmimecap", OPT_NOSMIMECAP, '-'}, | |
138 | {"stream", OPT_STREAM, '-'}, | |
139 | {"indef", OPT_INDEF, '-'}, | |
140 | {"noindef", OPT_NOINDEF, '-'}, | |
141 | {"nooldmime", OPT_NOOLDMIME, '-'}, | |
142 | {"crlfeol", OPT_CRLFEOL, '-'}, | |
143 | {"rand", OPT_RAND, 's', | |
144 | "Load the file(s) into the random number generator"}, | |
145 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, | |
146 | {"md", OPT_MD, 's'}, | |
147 | {"", OPT_CIPHER, '-', "Any supported cipher"}, | |
148 | OPT_V_OPTIONS, | |
149 | #ifndef OPENSSL_NO_ENGINE | |
150 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
151 | #endif | |
152 | {NULL} | |
153 | }; | |
667ac4ec | 154 | |
7e1b7485 | 155 | int smime_main(int argc, char **argv) |
0f113f3e | 156 | { |
7e1b7485 | 157 | BIO *in = NULL, *out = NULL, *indata = NULL; |
0f113f3e | 158 | EVP_PKEY *key = NULL; |
7e1b7485 RS |
159 | PKCS7 *p7 = NULL; |
160 | STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL; | |
0f113f3e | 161 | STACK_OF(X509) *encerts = NULL, *other = NULL; |
7e1b7485 RS |
162 | X509 *cert = NULL, *recip = NULL, *signer = NULL; |
163 | X509_STORE *store = NULL; | |
164 | X509_VERIFY_PARAM *vpm = NULL; | |
165 | const EVP_CIPHER *cipher = NULL; | |
0f113f3e | 166 | const EVP_MD *sign_md = NULL; |
333b070e | 167 | char *CAfile = NULL, *CApath = NULL, *inrand = NULL; |
7e1b7485 RS |
168 | char *certfile = NULL, *keyfile = NULL, *contfile = NULL, *prog; |
169 | char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = | |
170 | NULL; | |
171 | char *passinarg = NULL, *passin = NULL, *to = NULL, *from = | |
172 | NULL, *subject = NULL; | |
173 | const char *inmode = "r", *outmode = "w"; | |
174 | OPTION_CHOICE o; | |
175 | int flags = PKCS7_DETACHED, operation = 0, ret = 0, need_rand = 0, indef = | |
176 | 0; | |
177 | int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform = | |
178 | FORMAT_PEM; | |
179 | int vpmtouched = 0, rv = 0; | |
7e1b7485 | 180 | ENGINE *e = NULL; |
5a9a4b29 | 181 | |
7e1b7485 RS |
182 | if ((vpm = X509_VERIFY_PARAM_new()) == NULL) |
183 | return 1; | |
0f113f3e | 184 | |
7e1b7485 RS |
185 | prog = opt_init(argc, argv, smime_options); |
186 | while ((o = opt_next()) != OPT_EOF) { | |
187 | switch (o) { | |
188 | case OPT_EOF: | |
189 | case OPT_ERR: | |
190 | opthelp: | |
191 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
192 | goto end; | |
193 | case OPT_HELP: | |
194 | opt_help(smime_options); | |
195 | ret = 0; | |
196 | goto end; | |
197 | case OPT_INFORM: | |
198 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
199 | goto opthelp; | |
200 | break; | |
201 | case OPT_IN: | |
202 | infile = opt_arg(); | |
203 | break; | |
204 | case OPT_OUTFORM: | |
205 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
206 | goto opthelp; | |
207 | break; | |
208 | case OPT_OUT: | |
209 | outfile = opt_arg(); | |
210 | break; | |
211 | case OPT_ENCRYPT: | |
0f113f3e | 212 | operation = SMIME_ENCRYPT; |
7e1b7485 RS |
213 | break; |
214 | case OPT_DECRYPT: | |
0f113f3e | 215 | operation = SMIME_DECRYPT; |
7e1b7485 RS |
216 | break; |
217 | case OPT_SIGN: | |
0f113f3e | 218 | operation = SMIME_SIGN; |
7e1b7485 RS |
219 | break; |
220 | case OPT_RESIGN: | |
0f113f3e | 221 | operation = SMIME_RESIGN; |
7e1b7485 RS |
222 | break; |
223 | case OPT_VERIFY: | |
0f113f3e | 224 | operation = SMIME_VERIFY; |
7e1b7485 RS |
225 | break; |
226 | case OPT_PK7OUT: | |
0f113f3e | 227 | operation = SMIME_PK7OUT; |
7e1b7485 RS |
228 | break; |
229 | case OPT_TEXT: | |
0f113f3e | 230 | flags |= PKCS7_TEXT; |
7e1b7485 RS |
231 | break; |
232 | case OPT_NOINTERN: | |
0f113f3e | 233 | flags |= PKCS7_NOINTERN; |
7e1b7485 RS |
234 | break; |
235 | case OPT_NOVERIFY: | |
0f113f3e | 236 | flags |= PKCS7_NOVERIFY; |
7e1b7485 RS |
237 | break; |
238 | case OPT_NOCHAIN: | |
0f113f3e | 239 | flags |= PKCS7_NOCHAIN; |
7e1b7485 RS |
240 | break; |
241 | case OPT_NOCERTS: | |
0f113f3e | 242 | flags |= PKCS7_NOCERTS; |
7e1b7485 RS |
243 | break; |
244 | case OPT_NOATTR: | |
0f113f3e | 245 | flags |= PKCS7_NOATTR; |
7e1b7485 RS |
246 | break; |
247 | case OPT_NODETACH: | |
0f113f3e | 248 | flags &= ~PKCS7_DETACHED; |
7e1b7485 RS |
249 | break; |
250 | case OPT_NOSMIMECAP: | |
0f113f3e | 251 | flags |= PKCS7_NOSMIMECAP; |
7e1b7485 RS |
252 | break; |
253 | case OPT_BINARY: | |
0f113f3e | 254 | flags |= PKCS7_BINARY; |
7e1b7485 RS |
255 | break; |
256 | case OPT_NOSIGS: | |
0f113f3e | 257 | flags |= PKCS7_NOSIGS; |
7e1b7485 RS |
258 | break; |
259 | case OPT_STREAM: | |
260 | case OPT_INDEF: | |
0f113f3e | 261 | indef = 1; |
7e1b7485 RS |
262 | break; |
263 | case OPT_NOINDEF: | |
0f113f3e | 264 | indef = 0; |
7e1b7485 RS |
265 | break; |
266 | case OPT_NOOLDMIME: | |
0f113f3e | 267 | flags |= PKCS7_NOOLDMIMETYPE; |
7e1b7485 RS |
268 | break; |
269 | case OPT_CRLFEOL: | |
0f113f3e | 270 | flags |= PKCS7_CRLFEOL; |
7e1b7485 RS |
271 | break; |
272 | case OPT_RAND: | |
273 | inrand = opt_arg(); | |
0f113f3e | 274 | need_rand = 1; |
7e1b7485 RS |
275 | break; |
276 | case OPT_ENGINE: | |
333b070e | 277 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
278 | break; |
279 | case OPT_PASSIN: | |
280 | passinarg = opt_arg(); | |
281 | break; | |
282 | case OPT_TO: | |
283 | to = opt_arg(); | |
284 | break; | |
285 | case OPT_FROM: | |
286 | from = opt_arg(); | |
287 | break; | |
288 | case OPT_SUBJECT: | |
289 | subject = opt_arg(); | |
290 | break; | |
291 | case OPT_SIGNER: | |
0f113f3e | 292 | /* If previous -signer argument add signer to list */ |
0f113f3e | 293 | if (signerfile) { |
7e1b7485 RS |
294 | if (sksigners == NULL |
295 | && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) | |
296 | goto end; | |
0f113f3e | 297 | sk_OPENSSL_STRING_push(sksigners, signerfile); |
7e1b7485 | 298 | if (keyfile == NULL) |
0f113f3e | 299 | keyfile = signerfile; |
7e1b7485 RS |
300 | if (skkeys == NULL |
301 | && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) | |
302 | goto end; | |
0f113f3e MC |
303 | sk_OPENSSL_STRING_push(skkeys, keyfile); |
304 | keyfile = NULL; | |
305 | } | |
7e1b7485 RS |
306 | signerfile = opt_arg(); |
307 | break; | |
308 | case OPT_RECIP: | |
309 | recipfile = opt_arg(); | |
310 | break; | |
311 | case OPT_MD: | |
312 | if (!opt_md(opt_arg(), &sign_md)) | |
313 | goto opthelp; | |
314 | break; | |
315 | case OPT_CIPHER: | |
316 | if (!opt_cipher(opt_unknown(), &cipher)) | |
317 | goto opthelp; | |
318 | break; | |
319 | case OPT_INKEY: | |
0f113f3e MC |
320 | /* If previous -inkey arument add signer to list */ |
321 | if (keyfile) { | |
7e1b7485 RS |
322 | if (signerfile == NULL) { |
323 | BIO_printf(bio_err, | |
324 | "%s: Must have -signer before -inkey\n", prog); | |
325 | goto opthelp; | |
0f113f3e | 326 | } |
7e1b7485 RS |
327 | if (sksigners == NULL |
328 | && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) | |
329 | goto end; | |
0f113f3e MC |
330 | sk_OPENSSL_STRING_push(sksigners, signerfile); |
331 | signerfile = NULL; | |
7e1b7485 RS |
332 | if (skkeys == NULL |
333 | && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) | |
334 | goto end; | |
0f113f3e MC |
335 | sk_OPENSSL_STRING_push(skkeys, keyfile); |
336 | } | |
7e1b7485 RS |
337 | keyfile = opt_arg(); |
338 | break; | |
339 | case OPT_KEYFORM: | |
340 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform)) | |
341 | goto opthelp; | |
342 | break; | |
343 | case OPT_CERTFILE: | |
344 | certfile = opt_arg(); | |
345 | break; | |
346 | case OPT_CAFILE: | |
347 | CAfile = opt_arg(); | |
348 | break; | |
349 | case OPT_CAPATH: | |
350 | CApath = opt_arg(); | |
351 | break; | |
352 | case OPT_CONTENT: | |
353 | contfile = opt_arg(); | |
354 | break; | |
355 | case OPT_V_CASES: | |
356 | if (!opt_verify(o, vpm)) | |
357 | goto opthelp; | |
358 | vpmtouched++; | |
359 | break; | |
360 | } | |
0f113f3e | 361 | } |
7e1b7485 RS |
362 | argc = opt_num_rest(); |
363 | argv = opt_rest(); | |
0f113f3e MC |
364 | |
365 | if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) { | |
366 | BIO_puts(bio_err, "Multiple signers or keys not allowed\n"); | |
7e1b7485 | 367 | goto opthelp; |
0f113f3e MC |
368 | } |
369 | ||
370 | if (operation & SMIME_SIGNERS) { | |
371 | /* Check to see if any final signer needs to be appended */ | |
372 | if (keyfile && !signerfile) { | |
373 | BIO_puts(bio_err, "Illegal -inkey without -signer\n"); | |
7e1b7485 | 374 | goto opthelp; |
0f113f3e MC |
375 | } |
376 | if (signerfile) { | |
7e1b7485 RS |
377 | if (!sksigners |
378 | && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) | |
379 | goto end; | |
0f113f3e | 380 | sk_OPENSSL_STRING_push(sksigners, signerfile); |
7e1b7485 RS |
381 | if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) |
382 | goto end; | |
0f113f3e MC |
383 | if (!keyfile) |
384 | keyfile = signerfile; | |
385 | sk_OPENSSL_STRING_push(skkeys, keyfile); | |
386 | } | |
387 | if (!sksigners) { | |
388 | BIO_printf(bio_err, "No signer certificate specified\n"); | |
7e1b7485 | 389 | goto opthelp; |
0f113f3e MC |
390 | } |
391 | signerfile = NULL; | |
392 | keyfile = NULL; | |
393 | need_rand = 1; | |
394 | } else if (operation == SMIME_DECRYPT) { | |
395 | if (!recipfile && !keyfile) { | |
396 | BIO_printf(bio_err, | |
397 | "No recipient certificate or key specified\n"); | |
7e1b7485 | 398 | goto opthelp; |
0f113f3e MC |
399 | } |
400 | } else if (operation == SMIME_ENCRYPT) { | |
7e1b7485 | 401 | if (argc == 0) { |
0f113f3e | 402 | BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n"); |
7e1b7485 | 403 | goto opthelp; |
0f113f3e MC |
404 | } |
405 | need_rand = 1; | |
406 | } else if (!operation) | |
7e1b7485 RS |
407 | goto opthelp; |
408 | ||
7e1b7485 | 409 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
410 | BIO_printf(bio_err, "Error getting password\n"); |
411 | goto end; | |
412 | } | |
413 | ||
414 | if (need_rand) { | |
7e1b7485 | 415 | app_RAND_load_file(NULL, (inrand != NULL)); |
0f113f3e MC |
416 | if (inrand != NULL) |
417 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", | |
418 | app_RAND_load_files(inrand)); | |
419 | } | |
420 | ||
421 | ret = 2; | |
422 | ||
423 | if (!(operation & SMIME_SIGNERS)) | |
424 | flags &= ~PKCS7_DETACHED; | |
425 | ||
426 | if (operation & SMIME_OP) { | |
427 | if (outformat == FORMAT_ASN1) | |
428 | outmode = "wb"; | |
429 | } else { | |
430 | if (flags & PKCS7_BINARY) | |
431 | outmode = "wb"; | |
432 | } | |
433 | ||
434 | if (operation & SMIME_IP) { | |
435 | if (informat == FORMAT_ASN1) | |
436 | inmode = "rb"; | |
437 | } else { | |
438 | if (flags & PKCS7_BINARY) | |
439 | inmode = "rb"; | |
440 | } | |
441 | ||
442 | if (operation == SMIME_ENCRYPT) { | |
443 | if (!cipher) { | |
444 | #ifndef OPENSSL_NO_DES | |
445 | cipher = EVP_des_ede3_cbc(); | |
63da21c0 | 446 | #else |
0f113f3e MC |
447 | BIO_printf(bio_err, "No cipher selected\n"); |
448 | goto end; | |
63da21c0 | 449 | #endif |
0f113f3e MC |
450 | } |
451 | encerts = sk_X509_new_null(); | |
7e1b7485 RS |
452 | if (!encerts) |
453 | goto end; | |
454 | while (*argv) { | |
455 | cert = load_cert(*argv, FORMAT_PEM, | |
456 | NULL, e, "recipient certificate file"); | |
457 | if (cert == NULL) | |
0f113f3e | 458 | goto end; |
0f113f3e MC |
459 | sk_X509_push(encerts, cert); |
460 | cert = NULL; | |
7e1b7485 | 461 | argv++; |
0f113f3e MC |
462 | } |
463 | } | |
464 | ||
465 | if (certfile) { | |
7e1b7485 | 466 | if (!(other = load_certs(certfile, FORMAT_PEM, NULL, |
0f113f3e MC |
467 | e, "certificate file"))) { |
468 | ERR_print_errors(bio_err); | |
469 | goto end; | |
470 | } | |
471 | } | |
472 | ||
473 | if (recipfile && (operation == SMIME_DECRYPT)) { | |
7e1b7485 | 474 | if (!(recip = load_cert(recipfile, FORMAT_PEM, NULL, |
0f113f3e MC |
475 | e, "recipient certificate file"))) { |
476 | ERR_print_errors(bio_err); | |
477 | goto end; | |
478 | } | |
479 | } | |
480 | ||
481 | if (operation == SMIME_DECRYPT) { | |
482 | if (!keyfile) | |
483 | keyfile = recipfile; | |
484 | } else if (operation == SMIME_SIGN) { | |
485 | if (!keyfile) | |
486 | keyfile = signerfile; | |
487 | } else | |
488 | keyfile = NULL; | |
489 | ||
490 | if (keyfile) { | |
7e1b7485 | 491 | key = load_key(keyfile, keyform, 0, passin, e, "signing key file"); |
0f113f3e MC |
492 | if (!key) |
493 | goto end; | |
494 | } | |
495 | ||
7e1b7485 RS |
496 | in = bio_open_default(infile, inmode); |
497 | if (in == NULL) | |
498 | goto end; | |
0f113f3e MC |
499 | |
500 | if (operation & SMIME_IP) { | |
501 | if (informat == FORMAT_SMIME) | |
502 | p7 = SMIME_read_PKCS7(in, &indata); | |
503 | else if (informat == FORMAT_PEM) | |
504 | p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL); | |
505 | else if (informat == FORMAT_ASN1) | |
506 | p7 = d2i_PKCS7_bio(in, NULL); | |
507 | else { | |
508 | BIO_printf(bio_err, "Bad input format for PKCS#7 file\n"); | |
509 | goto end; | |
510 | } | |
511 | ||
512 | if (!p7) { | |
513 | BIO_printf(bio_err, "Error reading S/MIME message\n"); | |
514 | goto end; | |
515 | } | |
516 | if (contfile) { | |
517 | BIO_free(indata); | |
518 | if (!(indata = BIO_new_file(contfile, "rb"))) { | |
519 | BIO_printf(bio_err, "Can't read content file %s\n", contfile); | |
520 | goto end; | |
521 | } | |
522 | } | |
523 | } | |
524 | ||
7e1b7485 RS |
525 | out = bio_open_default(outfile, outmode); |
526 | if (out == NULL) | |
527 | goto end; | |
0f113f3e MC |
528 | |
529 | if (operation == SMIME_VERIFY) { | |
7e1b7485 | 530 | if (!(store = setup_verify(CAfile, CApath))) |
0f113f3e MC |
531 | goto end; |
532 | X509_STORE_set_verify_cb(store, smime_cb); | |
7e1b7485 | 533 | if (vpmtouched) |
0f113f3e MC |
534 | X509_STORE_set1_param(store, vpm); |
535 | } | |
536 | ||
537 | ret = 3; | |
538 | ||
539 | if (operation == SMIME_ENCRYPT) { | |
540 | if (indef) | |
541 | flags |= PKCS7_STREAM; | |
542 | p7 = PKCS7_encrypt(encerts, in, cipher, flags); | |
543 | } else if (operation & SMIME_SIGNERS) { | |
544 | int i; | |
545 | /* | |
546 | * If detached data content we only enable streaming if S/MIME output | |
547 | * format. | |
548 | */ | |
549 | if (operation == SMIME_SIGN) { | |
550 | if (flags & PKCS7_DETACHED) { | |
551 | if (outformat == FORMAT_SMIME) | |
552 | flags |= PKCS7_STREAM; | |
553 | } else if (indef) | |
554 | flags |= PKCS7_STREAM; | |
555 | flags |= PKCS7_PARTIAL; | |
556 | p7 = PKCS7_sign(NULL, NULL, other, in, flags); | |
557 | if (!p7) | |
558 | goto end; | |
559 | if (flags & PKCS7_NOCERTS) { | |
560 | for (i = 0; i < sk_X509_num(other); i++) { | |
561 | X509 *x = sk_X509_value(other, i); | |
562 | PKCS7_add_certificate(p7, x); | |
563 | } | |
564 | } | |
565 | } else | |
566 | flags |= PKCS7_REUSE_DIGEST; | |
567 | for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) { | |
568 | signerfile = sk_OPENSSL_STRING_value(sksigners, i); | |
569 | keyfile = sk_OPENSSL_STRING_value(skkeys, i); | |
7e1b7485 | 570 | signer = load_cert(signerfile, FORMAT_PEM, NULL, |
0f113f3e MC |
571 | e, "signer certificate"); |
572 | if (!signer) | |
573 | goto end; | |
7e1b7485 | 574 | key = load_key(keyfile, keyform, 0, passin, e, "signing key file"); |
0f113f3e MC |
575 | if (!key) |
576 | goto end; | |
577 | if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags)) | |
578 | goto end; | |
579 | X509_free(signer); | |
580 | signer = NULL; | |
581 | EVP_PKEY_free(key); | |
582 | key = NULL; | |
583 | } | |
584 | /* If not streaming or resigning finalize structure */ | |
585 | if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) { | |
586 | if (!PKCS7_final(p7, in, flags)) | |
587 | goto end; | |
588 | } | |
589 | } | |
590 | ||
591 | if (!p7) { | |
592 | BIO_printf(bio_err, "Error creating PKCS#7 structure\n"); | |
593 | goto end; | |
594 | } | |
595 | ||
596 | ret = 4; | |
597 | if (operation == SMIME_DECRYPT) { | |
598 | if (!PKCS7_decrypt(p7, key, recip, out, flags)) { | |
599 | BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n"); | |
600 | goto end; | |
601 | } | |
602 | } else if (operation == SMIME_VERIFY) { | |
603 | STACK_OF(X509) *signers; | |
604 | if (PKCS7_verify(p7, other, store, indata, out, flags)) | |
605 | BIO_printf(bio_err, "Verification successful\n"); | |
606 | else { | |
607 | BIO_printf(bio_err, "Verification failure\n"); | |
608 | goto end; | |
609 | } | |
610 | signers = PKCS7_get0_signers(p7, other, flags); | |
611 | if (!save_certs(signerfile, signers)) { | |
612 | BIO_printf(bio_err, "Error writing signers to %s\n", signerfile); | |
613 | ret = 5; | |
614 | goto end; | |
615 | } | |
616 | sk_X509_free(signers); | |
617 | } else if (operation == SMIME_PK7OUT) | |
618 | PEM_write_bio_PKCS7(out, p7); | |
619 | else { | |
620 | if (to) | |
621 | BIO_printf(out, "To: %s\n", to); | |
622 | if (from) | |
623 | BIO_printf(out, "From: %s\n", from); | |
624 | if (subject) | |
625 | BIO_printf(out, "Subject: %s\n", subject); | |
626 | if (outformat == FORMAT_SMIME) { | |
627 | if (operation == SMIME_RESIGN) | |
7e1b7485 | 628 | rv = SMIME_write_PKCS7(out, p7, indata, flags); |
0f113f3e | 629 | else |
7e1b7485 | 630 | rv = SMIME_write_PKCS7(out, p7, in, flags); |
0f113f3e | 631 | } else if (outformat == FORMAT_PEM) |
7e1b7485 | 632 | rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags); |
0f113f3e | 633 | else if (outformat == FORMAT_ASN1) |
7e1b7485 | 634 | rv = i2d_PKCS7_bio_stream(out, p7, in, flags); |
0f113f3e MC |
635 | else { |
636 | BIO_printf(bio_err, "Bad output format for PKCS#7 file\n"); | |
637 | goto end; | |
638 | } | |
7e1b7485 RS |
639 | if (rv == 0) { |
640 | BIO_printf(bio_err, "Error writing output\n"); | |
641 | ret = 3; | |
642 | goto end; | |
643 | } | |
0f113f3e MC |
644 | } |
645 | ret = 0; | |
646 | end: | |
647 | if (need_rand) | |
7e1b7485 | 648 | app_RAND_write_file(NULL); |
0f113f3e MC |
649 | if (ret) |
650 | ERR_print_errors(bio_err); | |
651 | sk_X509_pop_free(encerts, X509_free); | |
652 | sk_X509_pop_free(other, X509_free); | |
222561fe | 653 | X509_VERIFY_PARAM_free(vpm); |
0f113f3e MC |
654 | if (sksigners) |
655 | sk_OPENSSL_STRING_free(sksigners); | |
656 | if (skkeys) | |
657 | sk_OPENSSL_STRING_free(skkeys); | |
658 | X509_STORE_free(store); | |
659 | X509_free(cert); | |
660 | X509_free(recip); | |
661 | X509_free(signer); | |
662 | EVP_PKEY_free(key); | |
663 | PKCS7_free(p7); | |
664 | BIO_free(in); | |
665 | BIO_free(indata); | |
666 | BIO_free_all(out); | |
667 | if (passin) | |
668 | OPENSSL_free(passin); | |
669 | return (ret); | |
5a9a4b29 DSH |
670 | } |
671 | ||
a91451ef | 672 | static int save_certs(char *signerfile, STACK_OF(X509) *signers) |
0f113f3e MC |
673 | { |
674 | int i; | |
675 | BIO *tmp; | |
676 | if (!signerfile) | |
677 | return 1; | |
678 | tmp = BIO_new_file(signerfile, "w"); | |
679 | if (!tmp) | |
680 | return 0; | |
681 | for (i = 0; i < sk_X509_num(signers); i++) | |
682 | PEM_write_bio_X509(tmp, sk_X509_value(signers, i)); | |
683 | BIO_free(tmp); | |
684 | return 1; | |
685 | } | |
5d7c222d | 686 | |
5d7c222d DSH |
687 | /* Minimal callback just to output policy info (if any) */ |
688 | ||
689 | static int smime_cb(int ok, X509_STORE_CTX *ctx) | |
0f113f3e MC |
690 | { |
691 | int error; | |
5d7c222d | 692 | |
0f113f3e | 693 | error = X509_STORE_CTX_get_error(ctx); |
5d7c222d | 694 | |
0f113f3e MC |
695 | if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) |
696 | && ((error != X509_V_OK) || (ok != 2))) | |
697 | return ok; | |
5d7c222d | 698 | |
ecf3a1fb | 699 | policies_print(ctx); |
5d7c222d | 700 | |
0f113f3e | 701 | return ok; |
5d7c222d | 702 | |
0f113f3e | 703 | } |