]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
2234212c | 2 | * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved. |
5a9a4b29 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (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 | |
5a9a4b29 DSH |
8 | */ |
9 | ||
10 | /* S/MIME utility function */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <string.h> | |
36217a94 | 14 | #include "apps.h" |
11afb40c | 15 | #include <openssl/crypto.h> |
5a9a4b29 DSH |
16 | #include <openssl/pem.h> |
17 | #include <openssl/err.h> | |
5d7c222d DSH |
18 | #include <openssl/x509_vfy.h> |
19 | #include <openssl/x509v3.h> | |
5a9a4b29 | 20 | |
55ec5861 | 21 | static int save_certs(char *signerfile, STACK_OF(X509) *signers); |
5d7c222d | 22 | static int smime_cb(int ok, X509_STORE_CTX *ctx); |
5a9a4b29 | 23 | |
0f113f3e MC |
24 | #define SMIME_OP 0x10 |
25 | #define SMIME_IP 0x20 | |
26 | #define SMIME_SIGNERS 0x40 | |
27 | #define SMIME_ENCRYPT (1 | SMIME_OP) | |
28 | #define SMIME_DECRYPT (2 | SMIME_IP) | |
29 | #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS) | |
30 | #define SMIME_VERIFY (4 | SMIME_IP) | |
31 | #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP) | |
32 | #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS) | |
5a9a4b29 | 33 | |
7e1b7485 RS |
34 | typedef enum OPTION_choice { |
35 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
36 | OPT_ENCRYPT, OPT_DECRYPT, OPT_SIGN, OPT_RESIGN, OPT_VERIFY, | |
37 | OPT_PK7OUT, OPT_TEXT, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCHAIN, | |
38 | OPT_NOCERTS, OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP, | |
39 | OPT_BINARY, OPT_NOSIGS, OPT_STREAM, OPT_INDEF, OPT_NOINDEF, | |
3ee1eac2 | 40 | OPT_CRLFEOL, OPT_ENGINE, OPT_PASSIN, |
7e1b7485 RS |
41 | OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, OPT_MD, |
42 | OPT_CIPHER, OPT_INKEY, OPT_KEYFORM, OPT_CERTFILE, OPT_CAFILE, | |
3ee1eac2 | 43 | OPT_R_ENUM, |
7e1b7485 | 44 | OPT_V_ENUM, |
2b6bcb70 MC |
45 | OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH, OPT_IN, OPT_INFORM, OPT_OUT, |
46 | OPT_OUTFORM, OPT_CONTENT | |
7e1b7485 RS |
47 | } OPTION_CHOICE; |
48 | ||
44c83ebd | 49 | const OPTIONS smime_options[] = { |
7e1b7485 RS |
50 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"}, |
51 | {OPT_HELP_STR, 1, '-', | |
52 | " cert.pem... recipient certs for encryption\n"}, | |
53 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, | |
54 | {"help", OPT_HELP, '-', "Display this summary"}, | |
55 | {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"}, | |
56 | {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"}, | |
57 | {"sign", OPT_SIGN, '-', "Sign message"}, | |
58 | {"verify", OPT_VERIFY, '-', "Verify signed message"}, | |
59 | {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"}, | |
60 | {"nointern", OPT_NOINTERN, '-', | |
61 | "Don't search certificates in message for signer"}, | |
62 | {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"}, | |
63 | {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"}, | |
64 | {"nocerts", OPT_NOCERTS, '-', | |
65 | "Don't include signers certificate when signing"}, | |
66 | {"nodetach", OPT_NODETACH, '-', "Use opaque signing"}, | |
67 | {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"}, | |
68 | {"binary", OPT_BINARY, '-', "Don't translate message to text"}, | |
69 | {"certfile", OPT_CERTFILE, '<', "Other certificates file"}, | |
f47e5647 | 70 | {"signer", OPT_SIGNER, 's', "Signer certificate file"}, |
7e1b7485 RS |
71 | {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"}, |
72 | {"in", OPT_IN, '<', "Input file"}, | |
f47e5647 | 73 | {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"}, |
48b53522 | 74 | {"inkey", OPT_INKEY, 's', |
7e1b7485 RS |
75 | "Input private key (if not signer or recipient)"}, |
76 | {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"}, | |
77 | {"out", OPT_OUT, '>', "Output file"}, | |
f47e5647 | 78 | {"outform", OPT_OUTFORM, 'c', |
7e1b7485 RS |
79 | "Output format SMIME (default), PEM or DER"}, |
80 | {"content", OPT_CONTENT, '<', | |
81 | "Supply or override content for detached signature"}, | |
82 | {"to", OPT_TO, 's', "To address"}, | |
83 | {"from", OPT_FROM, 's', "From address"}, | |
84 | {"subject", OPT_SUBJECT, 's', "Subject"}, | |
85 | {"text", OPT_TEXT, '-', "Include or delete text MIME headers"}, | |
86 | {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"}, | |
87 | {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"}, | |
2b6bcb70 MC |
88 | {"no-CAfile", OPT_NOCAFILE, '-', |
89 | "Do not load the default certificates file"}, | |
90 | {"no-CApath", OPT_NOCAPATH, '-', | |
91 | "Do not load certificates from the default certificates directory"}, | |
12d56b29 | 92 | {"resign", OPT_RESIGN, '-', "Resign a signed message"}, |
609b0852 | 93 | {"nochain", OPT_NOCHAIN, '-', |
12d56b29 F |
94 | "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" }, |
95 | {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"}, | |
96 | {"stream", OPT_STREAM, '-', "Enable CMS streaming" }, | |
97 | {"indef", OPT_INDEF, '-', "Same as -stream" }, | |
98 | {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"}, | |
12d56b29 | 99 | {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only"}, |
3ee1eac2 | 100 | OPT_R_OPTIONS, |
7e1b7485 | 101 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, |
16e1b281 | 102 | {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"}, |
7e1b7485 RS |
103 | {"", OPT_CIPHER, '-', "Any supported cipher"}, |
104 | OPT_V_OPTIONS, | |
105 | #ifndef OPENSSL_NO_ENGINE | |
106 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
107 | #endif | |
108 | {NULL} | |
109 | }; | |
667ac4ec | 110 | |
7e1b7485 | 111 | int smime_main(int argc, char **argv) |
0f113f3e | 112 | { |
7e1b7485 | 113 | BIO *in = NULL, *out = NULL, *indata = NULL; |
0f113f3e | 114 | EVP_PKEY *key = NULL; |
7e1b7485 RS |
115 | PKCS7 *p7 = NULL; |
116 | STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL; | |
0f113f3e | 117 | STACK_OF(X509) *encerts = NULL, *other = NULL; |
7e1b7485 RS |
118 | X509 *cert = NULL, *recip = NULL, *signer = NULL; |
119 | X509_STORE *store = NULL; | |
120 | X509_VERIFY_PARAM *vpm = NULL; | |
121 | const EVP_CIPHER *cipher = NULL; | |
0f113f3e | 122 | const EVP_MD *sign_md = NULL; |
cc696296 | 123 | const char *CAfile = NULL, *CApath = NULL, *prog = NULL; |
3ee1eac2 RS |
124 | char *certfile = NULL, *keyfile = NULL, *contfile = NULL; |
125 | char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL; | |
126 | char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL, *subject = NULL; | |
7e1b7485 | 127 | OPTION_CHOICE o; |
2b6bcb70 | 128 | int noCApath = 0, noCAfile = 0; |
3ee1eac2 | 129 | int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0; |
7e1b7485 RS |
130 | int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform = |
131 | FORMAT_PEM; | |
132 | int vpmtouched = 0, rv = 0; | |
7e1b7485 | 133 | ENGINE *e = NULL; |
2197494d | 134 | const char *mime_eol = "\n"; |
5a9a4b29 | 135 | |
7e1b7485 RS |
136 | if ((vpm = X509_VERIFY_PARAM_new()) == NULL) |
137 | return 1; | |
0f113f3e | 138 | |
7e1b7485 RS |
139 | prog = opt_init(argc, argv, smime_options); |
140 | while ((o = opt_next()) != OPT_EOF) { | |
141 | switch (o) { | |
142 | case OPT_EOF: | |
143 | case OPT_ERR: | |
144 | opthelp: | |
145 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
146 | goto end; | |
147 | case OPT_HELP: | |
148 | opt_help(smime_options); | |
149 | ret = 0; | |
150 | goto end; | |
151 | case OPT_INFORM: | |
f47e5647 | 152 | if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat)) |
7e1b7485 RS |
153 | goto opthelp; |
154 | break; | |
155 | case OPT_IN: | |
156 | infile = opt_arg(); | |
157 | break; | |
158 | case OPT_OUTFORM: | |
f47e5647 | 159 | if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat)) |
7e1b7485 RS |
160 | goto opthelp; |
161 | break; | |
162 | case OPT_OUT: | |
163 | outfile = opt_arg(); | |
164 | break; | |
165 | case OPT_ENCRYPT: | |
0f113f3e | 166 | operation = SMIME_ENCRYPT; |
7e1b7485 RS |
167 | break; |
168 | case OPT_DECRYPT: | |
0f113f3e | 169 | operation = SMIME_DECRYPT; |
7e1b7485 RS |
170 | break; |
171 | case OPT_SIGN: | |
0f113f3e | 172 | operation = SMIME_SIGN; |
7e1b7485 RS |
173 | break; |
174 | case OPT_RESIGN: | |
0f113f3e | 175 | operation = SMIME_RESIGN; |
7e1b7485 RS |
176 | break; |
177 | case OPT_VERIFY: | |
0f113f3e | 178 | operation = SMIME_VERIFY; |
7e1b7485 RS |
179 | break; |
180 | case OPT_PK7OUT: | |
0f113f3e | 181 | operation = SMIME_PK7OUT; |
7e1b7485 RS |
182 | break; |
183 | case OPT_TEXT: | |
0f113f3e | 184 | flags |= PKCS7_TEXT; |
7e1b7485 RS |
185 | break; |
186 | case OPT_NOINTERN: | |
0f113f3e | 187 | flags |= PKCS7_NOINTERN; |
7e1b7485 RS |
188 | break; |
189 | case OPT_NOVERIFY: | |
0f113f3e | 190 | flags |= PKCS7_NOVERIFY; |
7e1b7485 RS |
191 | break; |
192 | case OPT_NOCHAIN: | |
0f113f3e | 193 | flags |= PKCS7_NOCHAIN; |
7e1b7485 RS |
194 | break; |
195 | case OPT_NOCERTS: | |
0f113f3e | 196 | flags |= PKCS7_NOCERTS; |
7e1b7485 RS |
197 | break; |
198 | case OPT_NOATTR: | |
0f113f3e | 199 | flags |= PKCS7_NOATTR; |
7e1b7485 RS |
200 | break; |
201 | case OPT_NODETACH: | |
0f113f3e | 202 | flags &= ~PKCS7_DETACHED; |
7e1b7485 RS |
203 | break; |
204 | case OPT_NOSMIMECAP: | |
0f113f3e | 205 | flags |= PKCS7_NOSMIMECAP; |
7e1b7485 RS |
206 | break; |
207 | case OPT_BINARY: | |
0f113f3e | 208 | flags |= PKCS7_BINARY; |
7e1b7485 RS |
209 | break; |
210 | case OPT_NOSIGS: | |
0f113f3e | 211 | flags |= PKCS7_NOSIGS; |
7e1b7485 RS |
212 | break; |
213 | case OPT_STREAM: | |
214 | case OPT_INDEF: | |
0f113f3e | 215 | indef = 1; |
7e1b7485 RS |
216 | break; |
217 | case OPT_NOINDEF: | |
0f113f3e | 218 | indef = 0; |
7e1b7485 | 219 | break; |
7e1b7485 | 220 | case OPT_CRLFEOL: |
0f113f3e | 221 | flags |= PKCS7_CRLFEOL; |
2197494d | 222 | mime_eol = "\r\n"; |
7e1b7485 | 223 | break; |
3ee1eac2 RS |
224 | case OPT_R_CASES: |
225 | if (!opt_rand(o)) | |
226 | goto end; | |
7e1b7485 RS |
227 | break; |
228 | case OPT_ENGINE: | |
333b070e | 229 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
230 | break; |
231 | case OPT_PASSIN: | |
232 | passinarg = opt_arg(); | |
233 | break; | |
234 | case OPT_TO: | |
235 | to = opt_arg(); | |
236 | break; | |
237 | case OPT_FROM: | |
238 | from = opt_arg(); | |
239 | break; | |
240 | case OPT_SUBJECT: | |
241 | subject = opt_arg(); | |
242 | break; | |
243 | case OPT_SIGNER: | |
0f113f3e | 244 | /* If previous -signer argument add signer to list */ |
2234212c | 245 | if (signerfile != NULL) { |
7e1b7485 RS |
246 | if (sksigners == NULL |
247 | && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) | |
248 | goto end; | |
0f113f3e | 249 | sk_OPENSSL_STRING_push(sksigners, signerfile); |
7e1b7485 | 250 | if (keyfile == NULL) |
0f113f3e | 251 | keyfile = signerfile; |
7e1b7485 RS |
252 | if (skkeys == NULL |
253 | && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) | |
254 | goto end; | |
0f113f3e MC |
255 | sk_OPENSSL_STRING_push(skkeys, keyfile); |
256 | keyfile = NULL; | |
257 | } | |
7e1b7485 RS |
258 | signerfile = opt_arg(); |
259 | break; | |
260 | case OPT_RECIP: | |
261 | recipfile = opt_arg(); | |
262 | break; | |
263 | case OPT_MD: | |
264 | if (!opt_md(opt_arg(), &sign_md)) | |
265 | goto opthelp; | |
266 | break; | |
267 | case OPT_CIPHER: | |
268 | if (!opt_cipher(opt_unknown(), &cipher)) | |
269 | goto opthelp; | |
270 | break; | |
271 | case OPT_INKEY: | |
ceab33e2 | 272 | /* If previous -inkey argument add signer to list */ |
2234212c | 273 | if (keyfile != NULL) { |
7e1b7485 RS |
274 | if (signerfile == NULL) { |
275 | BIO_printf(bio_err, | |
276 | "%s: Must have -signer before -inkey\n", prog); | |
277 | goto opthelp; | |
0f113f3e | 278 | } |
7e1b7485 RS |
279 | if (sksigners == NULL |
280 | && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) | |
281 | goto end; | |
0f113f3e MC |
282 | sk_OPENSSL_STRING_push(sksigners, signerfile); |
283 | signerfile = NULL; | |
7e1b7485 RS |
284 | if (skkeys == NULL |
285 | && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) | |
286 | goto end; | |
0f113f3e MC |
287 | sk_OPENSSL_STRING_push(skkeys, keyfile); |
288 | } | |
7e1b7485 RS |
289 | keyfile = opt_arg(); |
290 | break; | |
291 | case OPT_KEYFORM: | |
292 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform)) | |
293 | goto opthelp; | |
294 | break; | |
295 | case OPT_CERTFILE: | |
296 | certfile = opt_arg(); | |
297 | break; | |
298 | case OPT_CAFILE: | |
299 | CAfile = opt_arg(); | |
300 | break; | |
301 | case OPT_CAPATH: | |
302 | CApath = opt_arg(); | |
303 | break; | |
2b6bcb70 MC |
304 | case OPT_NOCAFILE: |
305 | noCAfile = 1; | |
306 | break; | |
307 | case OPT_NOCAPATH: | |
308 | noCApath = 1; | |
309 | break; | |
7e1b7485 RS |
310 | case OPT_CONTENT: |
311 | contfile = opt_arg(); | |
312 | break; | |
313 | case OPT_V_CASES: | |
314 | if (!opt_verify(o, vpm)) | |
315 | goto opthelp; | |
316 | vpmtouched++; | |
317 | break; | |
318 | } | |
0f113f3e | 319 | } |
7e1b7485 RS |
320 | argc = opt_num_rest(); |
321 | argv = opt_rest(); | |
0f113f3e | 322 | |
2234212c | 323 | if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) { |
0f113f3e | 324 | BIO_puts(bio_err, "Multiple signers or keys not allowed\n"); |
7e1b7485 | 325 | goto opthelp; |
0f113f3e MC |
326 | } |
327 | ||
328 | if (operation & SMIME_SIGNERS) { | |
329 | /* Check to see if any final signer needs to be appended */ | |
330 | if (keyfile && !signerfile) { | |
331 | BIO_puts(bio_err, "Illegal -inkey without -signer\n"); | |
7e1b7485 | 332 | goto opthelp; |
0f113f3e | 333 | } |
2234212c PY |
334 | if (signerfile != NULL) { |
335 | if (sksigners == NULL | |
7e1b7485 RS |
336 | && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) |
337 | goto end; | |
0f113f3e | 338 | sk_OPENSSL_STRING_push(sksigners, signerfile); |
7e1b7485 RS |
339 | if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) |
340 | goto end; | |
0f113f3e MC |
341 | if (!keyfile) |
342 | keyfile = signerfile; | |
343 | sk_OPENSSL_STRING_push(skkeys, keyfile); | |
344 | } | |
2234212c | 345 | if (sksigners == NULL) { |
0f113f3e | 346 | BIO_printf(bio_err, "No signer certificate specified\n"); |
7e1b7485 | 347 | goto opthelp; |
0f113f3e MC |
348 | } |
349 | signerfile = NULL; | |
350 | keyfile = NULL; | |
0f113f3e | 351 | } else if (operation == SMIME_DECRYPT) { |
2234212c | 352 | if (recipfile == NULL && keyfile == NULL) { |
0f113f3e MC |
353 | BIO_printf(bio_err, |
354 | "No recipient certificate or key specified\n"); | |
7e1b7485 | 355 | goto opthelp; |
0f113f3e MC |
356 | } |
357 | } else if (operation == SMIME_ENCRYPT) { | |
7e1b7485 | 358 | if (argc == 0) { |
0f113f3e | 359 | BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n"); |
7e1b7485 | 360 | goto opthelp; |
0f113f3e | 361 | } |
2234212c | 362 | } else if (!operation) { |
7e1b7485 | 363 | goto opthelp; |
2234212c | 364 | } |
7e1b7485 | 365 | |
7e1b7485 | 366 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
367 | BIO_printf(bio_err, "Error getting password\n"); |
368 | goto end; | |
369 | } | |
370 | ||
0f113f3e MC |
371 | ret = 2; |
372 | ||
373 | if (!(operation & SMIME_SIGNERS)) | |
374 | flags &= ~PKCS7_DETACHED; | |
375 | ||
bdd58d98 | 376 | if (!(operation & SMIME_OP)) { |
0f113f3e | 377 | if (flags & PKCS7_BINARY) |
bdd58d98 | 378 | outformat = FORMAT_BINARY; |
0f113f3e MC |
379 | } |
380 | ||
bdd58d98 | 381 | if (!(operation & SMIME_IP)) { |
0f113f3e | 382 | if (flags & PKCS7_BINARY) |
bdd58d98 | 383 | informat = FORMAT_BINARY; |
0f113f3e MC |
384 | } |
385 | ||
386 | if (operation == SMIME_ENCRYPT) { | |
2234212c | 387 | if (cipher == NULL) { |
0f113f3e MC |
388 | #ifndef OPENSSL_NO_DES |
389 | cipher = EVP_des_ede3_cbc(); | |
63da21c0 | 390 | #else |
0f113f3e MC |
391 | BIO_printf(bio_err, "No cipher selected\n"); |
392 | goto end; | |
63da21c0 | 393 | #endif |
0f113f3e MC |
394 | } |
395 | encerts = sk_X509_new_null(); | |
2234212c | 396 | if (encerts == NULL) |
7e1b7485 | 397 | goto end; |
2234212c | 398 | while (*argv != NULL) { |
7e1b7485 | 399 | cert = load_cert(*argv, FORMAT_PEM, |
a773b52a | 400 | "recipient certificate file"); |
7e1b7485 | 401 | if (cert == NULL) |
0f113f3e | 402 | goto end; |
0f113f3e MC |
403 | sk_X509_push(encerts, cert); |
404 | cert = NULL; | |
7e1b7485 | 405 | argv++; |
0f113f3e MC |
406 | } |
407 | } | |
408 | ||
2234212c | 409 | if (certfile != NULL) { |
a773b52a | 410 | if (!load_certs(certfile, &other, FORMAT_PEM, NULL, |
0996dc54 | 411 | "certificate file")) { |
0f113f3e MC |
412 | ERR_print_errors(bio_err); |
413 | goto end; | |
414 | } | |
415 | } | |
416 | ||
2234212c | 417 | if (recipfile != NULL && (operation == SMIME_DECRYPT)) { |
a773b52a RS |
418 | if ((recip = load_cert(recipfile, FORMAT_PEM, |
419 | "recipient certificate file")) == NULL) { | |
0f113f3e MC |
420 | ERR_print_errors(bio_err); |
421 | goto end; | |
422 | } | |
423 | } | |
424 | ||
425 | if (operation == SMIME_DECRYPT) { | |
2234212c | 426 | if (keyfile == NULL) |
0f113f3e MC |
427 | keyfile = recipfile; |
428 | } else if (operation == SMIME_SIGN) { | |
2234212c | 429 | if (keyfile == NULL) |
0f113f3e | 430 | keyfile = signerfile; |
2234212c | 431 | } else { |
0f113f3e | 432 | keyfile = NULL; |
2234212c | 433 | } |
0f113f3e | 434 | |
2234212c | 435 | if (keyfile != NULL) { |
7e1b7485 | 436 | key = load_key(keyfile, keyform, 0, passin, e, "signing key file"); |
2234212c | 437 | if (key == NULL) |
0f113f3e MC |
438 | goto end; |
439 | } | |
440 | ||
bdd58d98 | 441 | in = bio_open_default(infile, 'r', informat); |
7e1b7485 RS |
442 | if (in == NULL) |
443 | goto end; | |
0f113f3e MC |
444 | |
445 | if (operation & SMIME_IP) { | |
2234212c | 446 | if (informat == FORMAT_SMIME) { |
0f113f3e | 447 | p7 = SMIME_read_PKCS7(in, &indata); |
2234212c | 448 | } else if (informat == FORMAT_PEM) { |
0f113f3e | 449 | p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL); |
2234212c | 450 | } else if (informat == FORMAT_ASN1) { |
0f113f3e | 451 | p7 = d2i_PKCS7_bio(in, NULL); |
2234212c | 452 | } else { |
0f113f3e MC |
453 | BIO_printf(bio_err, "Bad input format for PKCS#7 file\n"); |
454 | goto end; | |
455 | } | |
456 | ||
2234212c | 457 | if (p7 == NULL) { |
0f113f3e MC |
458 | BIO_printf(bio_err, "Error reading S/MIME message\n"); |
459 | goto end; | |
460 | } | |
2234212c | 461 | if (contfile != NULL) { |
0f113f3e | 462 | BIO_free(indata); |
75ebbd9a | 463 | if ((indata = BIO_new_file(contfile, "rb")) == NULL) { |
0f113f3e MC |
464 | BIO_printf(bio_err, "Can't read content file %s\n", contfile); |
465 | goto end; | |
466 | } | |
467 | } | |
468 | } | |
469 | ||
bdd58d98 | 470 | out = bio_open_default(outfile, 'w', outformat); |
7e1b7485 RS |
471 | if (out == NULL) |
472 | goto end; | |
0f113f3e MC |
473 | |
474 | if (operation == SMIME_VERIFY) { | |
2b6bcb70 | 475 | if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL) |
0f113f3e MC |
476 | goto end; |
477 | X509_STORE_set_verify_cb(store, smime_cb); | |
7e1b7485 | 478 | if (vpmtouched) |
0f113f3e MC |
479 | X509_STORE_set1_param(store, vpm); |
480 | } | |
481 | ||
482 | ret = 3; | |
483 | ||
484 | if (operation == SMIME_ENCRYPT) { | |
485 | if (indef) | |
486 | flags |= PKCS7_STREAM; | |
487 | p7 = PKCS7_encrypt(encerts, in, cipher, flags); | |
488 | } else if (operation & SMIME_SIGNERS) { | |
489 | int i; | |
490 | /* | |
491 | * If detached data content we only enable streaming if S/MIME output | |
492 | * format. | |
493 | */ | |
494 | if (operation == SMIME_SIGN) { | |
495 | if (flags & PKCS7_DETACHED) { | |
496 | if (outformat == FORMAT_SMIME) | |
497 | flags |= PKCS7_STREAM; | |
2234212c | 498 | } else if (indef) { |
0f113f3e | 499 | flags |= PKCS7_STREAM; |
2234212c | 500 | } |
0f113f3e MC |
501 | flags |= PKCS7_PARTIAL; |
502 | p7 = PKCS7_sign(NULL, NULL, other, in, flags); | |
2234212c | 503 | if (p7 == NULL) |
0f113f3e MC |
504 | goto end; |
505 | if (flags & PKCS7_NOCERTS) { | |
506 | for (i = 0; i < sk_X509_num(other); i++) { | |
507 | X509 *x = sk_X509_value(other, i); | |
508 | PKCS7_add_certificate(p7, x); | |
509 | } | |
510 | } | |
2234212c | 511 | } else { |
0f113f3e | 512 | flags |= PKCS7_REUSE_DIGEST; |
2234212c | 513 | } |
0f113f3e MC |
514 | for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) { |
515 | signerfile = sk_OPENSSL_STRING_value(sksigners, i); | |
516 | keyfile = sk_OPENSSL_STRING_value(skkeys, i); | |
a773b52a RS |
517 | signer = load_cert(signerfile, FORMAT_PEM, |
518 | "signer certificate"); | |
2234212c | 519 | if (signer == NULL) |
0f113f3e | 520 | goto end; |
7e1b7485 | 521 | key = load_key(keyfile, keyform, 0, passin, e, "signing key file"); |
2234212c | 522 | if (key == NULL) |
0f113f3e MC |
523 | goto end; |
524 | if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags)) | |
525 | goto end; | |
526 | X509_free(signer); | |
527 | signer = NULL; | |
528 | EVP_PKEY_free(key); | |
529 | key = NULL; | |
530 | } | |
531 | /* If not streaming or resigning finalize structure */ | |
532 | if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) { | |
533 | if (!PKCS7_final(p7, in, flags)) | |
534 | goto end; | |
535 | } | |
536 | } | |
537 | ||
2234212c | 538 | if (p7 == NULL) { |
0f113f3e MC |
539 | BIO_printf(bio_err, "Error creating PKCS#7 structure\n"); |
540 | goto end; | |
541 | } | |
542 | ||
543 | ret = 4; | |
544 | if (operation == SMIME_DECRYPT) { | |
545 | if (!PKCS7_decrypt(p7, key, recip, out, flags)) { | |
546 | BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n"); | |
547 | goto end; | |
548 | } | |
549 | } else if (operation == SMIME_VERIFY) { | |
550 | STACK_OF(X509) *signers; | |
551 | if (PKCS7_verify(p7, other, store, indata, out, flags)) | |
552 | BIO_printf(bio_err, "Verification successful\n"); | |
553 | else { | |
554 | BIO_printf(bio_err, "Verification failure\n"); | |
555 | goto end; | |
556 | } | |
557 | signers = PKCS7_get0_signers(p7, other, flags); | |
558 | if (!save_certs(signerfile, signers)) { | |
559 | BIO_printf(bio_err, "Error writing signers to %s\n", signerfile); | |
560 | ret = 5; | |
561 | goto end; | |
562 | } | |
563 | sk_X509_free(signers); | |
2234212c | 564 | } else if (operation == SMIME_PK7OUT) { |
0f113f3e | 565 | PEM_write_bio_PKCS7(out, p7); |
2234212c | 566 | } else { |
0f113f3e | 567 | if (to) |
2197494d | 568 | BIO_printf(out, "To: %s%s", to, mime_eol); |
0f113f3e | 569 | if (from) |
2197494d | 570 | BIO_printf(out, "From: %s%s", from, mime_eol); |
0f113f3e | 571 | if (subject) |
2197494d | 572 | BIO_printf(out, "Subject: %s%s", subject, mime_eol); |
0f113f3e MC |
573 | if (outformat == FORMAT_SMIME) { |
574 | if (operation == SMIME_RESIGN) | |
7e1b7485 | 575 | rv = SMIME_write_PKCS7(out, p7, indata, flags); |
0f113f3e | 576 | else |
7e1b7485 | 577 | rv = SMIME_write_PKCS7(out, p7, in, flags); |
2234212c | 578 | } else if (outformat == FORMAT_PEM) { |
7e1b7485 | 579 | rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags); |
2234212c | 580 | } else if (outformat == FORMAT_ASN1) { |
7e1b7485 | 581 | rv = i2d_PKCS7_bio_stream(out, p7, in, flags); |
2234212c | 582 | } else { |
0f113f3e MC |
583 | BIO_printf(bio_err, "Bad output format for PKCS#7 file\n"); |
584 | goto end; | |
585 | } | |
7e1b7485 RS |
586 | if (rv == 0) { |
587 | BIO_printf(bio_err, "Error writing output\n"); | |
588 | ret = 3; | |
589 | goto end; | |
590 | } | |
0f113f3e MC |
591 | } |
592 | ret = 0; | |
593 | end: | |
0f113f3e MC |
594 | if (ret) |
595 | ERR_print_errors(bio_err); | |
596 | sk_X509_pop_free(encerts, X509_free); | |
597 | sk_X509_pop_free(other, X509_free); | |
222561fe | 598 | X509_VERIFY_PARAM_free(vpm); |
25aaa98a RS |
599 | sk_OPENSSL_STRING_free(sksigners); |
600 | sk_OPENSSL_STRING_free(skkeys); | |
0f113f3e MC |
601 | X509_STORE_free(store); |
602 | X509_free(cert); | |
603 | X509_free(recip); | |
604 | X509_free(signer); | |
605 | EVP_PKEY_free(key); | |
606 | PKCS7_free(p7); | |
dd1abd44 | 607 | release_engine(e); |
0f113f3e MC |
608 | BIO_free(in); |
609 | BIO_free(indata); | |
610 | BIO_free_all(out); | |
b548a1f1 | 611 | OPENSSL_free(passin); |
0f113f3e | 612 | return (ret); |
5a9a4b29 DSH |
613 | } |
614 | ||
a91451ef | 615 | static int save_certs(char *signerfile, STACK_OF(X509) *signers) |
0f113f3e MC |
616 | { |
617 | int i; | |
618 | BIO *tmp; | |
2234212c PY |
619 | |
620 | if (signerfile == NULL) | |
0f113f3e MC |
621 | return 1; |
622 | tmp = BIO_new_file(signerfile, "w"); | |
2234212c | 623 | if (tmp == NULL) |
0f113f3e MC |
624 | return 0; |
625 | for (i = 0; i < sk_X509_num(signers); i++) | |
626 | PEM_write_bio_X509(tmp, sk_X509_value(signers, i)); | |
627 | BIO_free(tmp); | |
628 | return 1; | |
629 | } | |
5d7c222d | 630 | |
5d7c222d DSH |
631 | /* Minimal callback just to output policy info (if any) */ |
632 | ||
633 | static int smime_cb(int ok, X509_STORE_CTX *ctx) | |
0f113f3e MC |
634 | { |
635 | int error; | |
5d7c222d | 636 | |
0f113f3e | 637 | error = X509_STORE_CTX_get_error(ctx); |
5d7c222d | 638 | |
0f113f3e MC |
639 | if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) |
640 | && ((error != X509_V_OK) || (ok != 2))) | |
641 | return ok; | |
5d7c222d | 642 | |
ecf3a1fb | 643 | policies_print(ctx); |
5d7c222d | 644 | |
0f113f3e | 645 | return ok; |
0f113f3e | 646 | } |