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