]>
Commit | Line | Data |
---|---|---|
8931b30d DSH |
1 | /* apps/cms.c */ |
2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | |
3 | * project. | |
4 | */ | |
5 | /* ==================================================================== | |
6 | * Copyright (c) 2008 The OpenSSL Project. All rights reserved. | |
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 | |
13 | * notice, this list of conditions and the following disclaimer. | |
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 | ||
54 | /* CMS utility function */ | |
55 | ||
56 | #include <stdio.h> | |
57 | #include <string.h> | |
58 | #include "apps.h" | |
59 | ||
60 | #ifndef OPENSSL_NO_CMS | |
61 | ||
62 | #include <openssl/crypto.h> | |
63 | #include <openssl/pem.h> | |
64 | #include <openssl/err.h> | |
65 | #include <openssl/x509_vfy.h> | |
66 | #include <openssl/x509v3.h> | |
67 | #include <openssl/cms.h> | |
68 | ||
69 | #undef PROG | |
70 | #define PROG cms_main | |
71 | static int save_certs(char *signerfile, STACK_OF(X509) *signers); | |
7f50d9a4 | 72 | static int cms_cb(int ok, X509_STORE_CTX *ctx); |
f4cc56f4 | 73 | static void receipt_request_print(BIO *out, CMS_ContentInfo *cms); |
5ce278a7 BL |
74 | static CMS_ReceiptRequest *make_receipt_request(STACK_OF(STRING) *rr_to, |
75 | int rr_allorfirst, | |
76 | STACK_OF(STRING) *rr_from); | |
8931b30d DSH |
77 | |
78 | #define SMIME_OP 0x10 | |
79 | #define SMIME_IP 0x20 | |
80 | #define SMIME_SIGNERS 0x40 | |
81 | #define SMIME_ENCRYPT (1 | SMIME_OP) | |
82 | #define SMIME_DECRYPT (2 | SMIME_IP) | |
83 | #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS) | |
84 | #define SMIME_VERIFY (4 | SMIME_IP) | |
85 | #define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP) | |
86 | #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS) | |
87 | #define SMIME_DATAOUT (7 | SMIME_IP) | |
88 | #define SMIME_DATA_CREATE (8 | SMIME_OP) | |
89 | #define SMIME_DIGEST_VERIFY (9 | SMIME_IP) | |
90 | #define SMIME_DIGEST_CREATE (10 | SMIME_OP) | |
91 | #define SMIME_UNCOMPRESS (11 | SMIME_IP) | |
92 | #define SMIME_COMPRESS (12 | SMIME_OP) | |
b820455c | 93 | #define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP) |
d9f5f07e | 94 | #define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP) |
36309aa2 | 95 | #define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP) |
eb9d8d8c | 96 | #define SMIME_VERIFY_RECEIPT (16 | SMIME_IP) |
8931b30d | 97 | |
db50661f DSH |
98 | int verify_err = 0; |
99 | ||
8931b30d DSH |
100 | int MAIN(int, char **); |
101 | ||
102 | int MAIN(int argc, char **argv) | |
103 | { | |
104 | ENGINE *e = NULL; | |
105 | int operation = 0; | |
106 | int ret = 0; | |
107 | char **args; | |
108 | const char *inmode = "r", *outmode = "w"; | |
eb9d8d8c | 109 | char *infile = NULL, *outfile = NULL, *rctfile = NULL; |
8931b30d | 110 | char *signerfile = NULL, *recipfile = NULL; |
5ce278a7 | 111 | STACK_OF(STRING) *sksigners = NULL, *skkeys = NULL; |
8931b30d | 112 | char *certfile = NULL, *keyfile = NULL, *contfile=NULL; |
4a954b56 | 113 | char *certsoutfile = NULL; |
8931b30d | 114 | const EVP_CIPHER *cipher = NULL; |
eb9d8d8c | 115 | CMS_ContentInfo *cms = NULL, *rcms = NULL; |
8931b30d DSH |
116 | X509_STORE *store = NULL; |
117 | X509 *cert = NULL, *recip = NULL, *signer = NULL; | |
118 | EVP_PKEY *key = NULL; | |
119 | STACK_OF(X509) *encerts = NULL, *other = NULL; | |
eb9d8d8c | 120 | BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL; |
8931b30d | 121 | int badarg = 0; |
62051713 | 122 | int flags = CMS_DETACHED, noout = 0, print = 0; |
db50661f | 123 | int verify_retcode = 0; |
f5e2354c | 124 | int rr_print = 0, rr_allorfirst = -1; |
5ce278a7 | 125 | STACK_OF(STRING) *rr_to = NULL, *rr_from = NULL; |
f5e2354c | 126 | CMS_ReceiptRequest *rr = NULL; |
8931b30d DSH |
127 | char *to = NULL, *from = NULL, *subject = NULL; |
128 | char *CAfile = NULL, *CApath = NULL; | |
129 | char *passargin = NULL, *passin = NULL; | |
130 | char *inrand = NULL; | |
131 | int need_rand = 0; | |
8931b30d DSH |
132 | const EVP_MD *sign_md = NULL; |
133 | int informat = FORMAT_SMIME, outformat = FORMAT_SMIME; | |
eb9d8d8c | 134 | int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM; |
8931b30d DSH |
135 | #ifndef OPENSSL_NO_ENGINE |
136 | char *engine=NULL; | |
137 | #endif | |
ab124380 DSH |
138 | unsigned char *secret_key = NULL, *secret_keyid = NULL; |
139 | size_t secret_keylen = 0, secret_keyidlen = 0; | |
8931b30d | 140 | |
054307e7 DSH |
141 | ASN1_OBJECT *econtent_type = NULL; |
142 | ||
8931b30d DSH |
143 | X509_VERIFY_PARAM *vpm = NULL; |
144 | ||
145 | args = argv + 1; | |
146 | ret = 1; | |
147 | ||
148 | apps_startup(); | |
149 | ||
150 | if (bio_err == NULL) | |
151 | { | |
152 | if ((bio_err = BIO_new(BIO_s_file())) != NULL) | |
153 | BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT); | |
154 | } | |
155 | ||
156 | if (!load_config(bio_err, NULL)) | |
157 | goto end; | |
158 | ||
159 | while (!badarg && *args && *args[0] == '-') | |
160 | { | |
161 | if (!strcmp (*args, "-encrypt")) | |
162 | operation = SMIME_ENCRYPT; | |
163 | else if (!strcmp (*args, "-decrypt")) | |
164 | operation = SMIME_DECRYPT; | |
165 | else if (!strcmp (*args, "-sign")) | |
166 | operation = SMIME_SIGN; | |
36309aa2 DSH |
167 | else if (!strcmp (*args, "-sign_receipt")) |
168 | operation = SMIME_SIGN_RECEIPT; | |
8931b30d DSH |
169 | else if (!strcmp (*args, "-resign")) |
170 | operation = SMIME_RESIGN; | |
171 | else if (!strcmp (*args, "-verify")) | |
172 | operation = SMIME_VERIFY; | |
db50661f DSH |
173 | else if (!strcmp (*args, "-verify_retcode")) |
174 | verify_retcode = 1; | |
eb9d8d8c DSH |
175 | else if (!strcmp(*args,"-verify_receipt")) |
176 | { | |
177 | operation = SMIME_VERIFY_RECEIPT; | |
178 | if (!args[1]) | |
179 | goto argerr; | |
180 | args++; | |
181 | rctfile = *args; | |
182 | } | |
8931b30d DSH |
183 | else if (!strcmp (*args, "-cmsout")) |
184 | operation = SMIME_CMSOUT; | |
185 | else if (!strcmp (*args, "-data_out")) | |
186 | operation = SMIME_DATAOUT; | |
187 | else if (!strcmp (*args, "-data_create")) | |
188 | operation = SMIME_DATA_CREATE; | |
189 | else if (!strcmp (*args, "-digest_verify")) | |
190 | operation = SMIME_DIGEST_VERIFY; | |
191 | else if (!strcmp (*args, "-digest_create")) | |
192 | operation = SMIME_DIGEST_CREATE; | |
193 | else if (!strcmp (*args, "-compress")) | |
194 | operation = SMIME_COMPRESS; | |
195 | else if (!strcmp (*args, "-uncompress")) | |
196 | operation = SMIME_UNCOMPRESS; | |
1021f9aa | 197 | else if (!strcmp (*args, "-EncryptedData_decrypt")) |
b820455c | 198 | operation = SMIME_ENCRYPTED_DECRYPT; |
d9f5f07e DSH |
199 | else if (!strcmp (*args, "-EncryptedData_encrypt")) |
200 | operation = SMIME_ENCRYPTED_ENCRYPT; | |
8931b30d DSH |
201 | #ifndef OPENSSL_NO_DES |
202 | else if (!strcmp (*args, "-des3")) | |
203 | cipher = EVP_des_ede3_cbc(); | |
204 | else if (!strcmp (*args, "-des")) | |
205 | cipher = EVP_des_cbc(); | |
206 | #endif | |
207 | #ifndef OPENSSL_NO_SEED | |
208 | else if (!strcmp (*args, "-seed")) | |
209 | cipher = EVP_seed_cbc(); | |
210 | #endif | |
211 | #ifndef OPENSSL_NO_RC2 | |
212 | else if (!strcmp (*args, "-rc2-40")) | |
213 | cipher = EVP_rc2_40_cbc(); | |
214 | else if (!strcmp (*args, "-rc2-128")) | |
215 | cipher = EVP_rc2_cbc(); | |
216 | else if (!strcmp (*args, "-rc2-64")) | |
217 | cipher = EVP_rc2_64_cbc(); | |
218 | #endif | |
219 | #ifndef OPENSSL_NO_AES | |
220 | else if (!strcmp(*args,"-aes128")) | |
221 | cipher = EVP_aes_128_cbc(); | |
222 | else if (!strcmp(*args,"-aes192")) | |
223 | cipher = EVP_aes_192_cbc(); | |
224 | else if (!strcmp(*args,"-aes256")) | |
225 | cipher = EVP_aes_256_cbc(); | |
226 | #endif | |
227 | #ifndef OPENSSL_NO_CAMELLIA | |
228 | else if (!strcmp(*args,"-camellia128")) | |
229 | cipher = EVP_camellia_128_cbc(); | |
230 | else if (!strcmp(*args,"-camellia192")) | |
231 | cipher = EVP_camellia_192_cbc(); | |
232 | else if (!strcmp(*args,"-camellia256")) | |
233 | cipher = EVP_camellia_256_cbc(); | |
234 | #endif | |
235 | else if (!strcmp (*args, "-text")) | |
236 | flags |= CMS_TEXT; | |
237 | else if (!strcmp (*args, "-nointern")) | |
238 | flags |= CMS_NOINTERN; | |
239 | else if (!strcmp (*args, "-noverify") | |
240 | || !strcmp (*args, "-no_signer_cert_verify")) | |
241 | flags |= CMS_NO_SIGNER_CERT_VERIFY; | |
242 | else if (!strcmp (*args, "-nocerts")) | |
243 | flags |= CMS_NOCERTS; | |
244 | else if (!strcmp (*args, "-noattr")) | |
245 | flags |= CMS_NOATTR; | |
246 | else if (!strcmp (*args, "-nodetach")) | |
247 | flags &= ~CMS_DETACHED; | |
248 | else if (!strcmp (*args, "-nosmimecap")) | |
249 | flags |= CMS_NOSMIMECAP; | |
250 | else if (!strcmp (*args, "-binary")) | |
251 | flags |= CMS_BINARY; | |
252 | else if (!strcmp (*args, "-keyid")) | |
253 | flags |= CMS_USE_KEYID; | |
254 | else if (!strcmp (*args, "-nosigs")) | |
255 | flags |= CMS_NOSIGS; | |
256 | else if (!strcmp (*args, "-no_content_verify")) | |
257 | flags |= CMS_NO_CONTENT_VERIFY; | |
258 | else if (!strcmp (*args, "-no_attr_verify")) | |
259 | flags |= CMS_NO_ATTR_VERIFY; | |
260 | else if (!strcmp (*args, "-stream")) | |
e4f0e40e | 261 | flags |= CMS_STREAM; |
8931b30d | 262 | else if (!strcmp (*args, "-indef")) |
e4f0e40e | 263 | flags |= CMS_STREAM; |
8931b30d | 264 | else if (!strcmp (*args, "-noindef")) |
e4f0e40e | 265 | flags &= ~CMS_STREAM; |
8931b30d DSH |
266 | else if (!strcmp (*args, "-nooldmime")) |
267 | flags |= CMS_NOOLDMIMETYPE; | |
268 | else if (!strcmp (*args, "-crlfeol")) | |
269 | flags |= CMS_CRLFEOL; | |
62051713 DSH |
270 | else if (!strcmp (*args, "-noout")) |
271 | noout = 1; | |
f4cc56f4 DSH |
272 | else if (!strcmp (*args, "-receipt_request_print")) |
273 | rr_print = 1; | |
f5e2354c DSH |
274 | else if (!strcmp (*args, "-receipt_request_all")) |
275 | rr_allorfirst = 0; | |
276 | else if (!strcmp (*args, "-receipt_request_first")) | |
277 | rr_allorfirst = 1; | |
278 | else if (!strcmp(*args,"-receipt_request_from")) | |
279 | { | |
280 | if (!args[1]) | |
281 | goto argerr; | |
282 | args++; | |
283 | if (!rr_from) | |
5ce278a7 BL |
284 | rr_from = sk_STRING_new_null(); |
285 | sk_STRING_push(rr_from, *args); | |
f5e2354c DSH |
286 | } |
287 | else if (!strcmp(*args,"-receipt_request_to")) | |
288 | { | |
289 | if (!args[1]) | |
290 | goto argerr; | |
291 | args++; | |
292 | if (!rr_to) | |
5ce278a7 BL |
293 | rr_to = sk_STRING_new_null(); |
294 | sk_STRING_push(rr_to, *args); | |
f5e2354c | 295 | } |
62051713 DSH |
296 | else if (!strcmp (*args, "-print")) |
297 | { | |
298 | noout = 1; | |
299 | print = 1; | |
300 | } | |
b820455c DSH |
301 | else if (!strcmp(*args,"-secretkey")) |
302 | { | |
303 | long ltmp; | |
304 | if (!args[1]) | |
305 | goto argerr; | |
306 | args++; | |
307 | secret_key = string_to_hex(*args, <mp); | |
308 | if (!secret_key) | |
309 | { | |
310 | BIO_printf(bio_err, "Invalid key %s\n", *args); | |
311 | goto argerr; | |
312 | } | |
313 | secret_keylen = (size_t)ltmp; | |
314 | } | |
ab124380 DSH |
315 | else if (!strcmp(*args,"-secretkeyid")) |
316 | { | |
317 | long ltmp; | |
318 | if (!args[1]) | |
319 | goto argerr; | |
320 | args++; | |
321 | secret_keyid = string_to_hex(*args, <mp); | |
322 | if (!secret_keyid) | |
323 | { | |
324 | BIO_printf(bio_err, "Invalid id %s\n", *args); | |
325 | goto argerr; | |
326 | } | |
327 | secret_keyidlen = (size_t)ltmp; | |
328 | } | |
054307e7 DSH |
329 | else if (!strcmp(*args,"-econtent_type")) |
330 | { | |
331 | if (!args[1]) | |
332 | goto argerr; | |
333 | args++; | |
334 | econtent_type = OBJ_txt2obj(*args, 0); | |
335 | if (!econtent_type) | |
336 | { | |
337 | BIO_printf(bio_err, "Invalid OID %s\n", *args); | |
338 | goto argerr; | |
339 | } | |
340 | } | |
8931b30d DSH |
341 | else if (!strcmp(*args,"-rand")) |
342 | { | |
343 | if (!args[1]) | |
344 | goto argerr; | |
345 | args++; | |
346 | inrand = *args; | |
347 | need_rand = 1; | |
348 | } | |
349 | #ifndef OPENSSL_NO_ENGINE | |
350 | else if (!strcmp(*args,"-engine")) | |
351 | { | |
352 | if (!args[1]) | |
353 | goto argerr; | |
354 | engine = *++args; | |
355 | } | |
356 | #endif | |
357 | else if (!strcmp(*args,"-passin")) | |
358 | { | |
359 | if (!args[1]) | |
360 | goto argerr; | |
361 | passargin = *++args; | |
362 | } | |
363 | else if (!strcmp (*args, "-to")) | |
364 | { | |
365 | if (!args[1]) | |
366 | goto argerr; | |
367 | to = *++args; | |
368 | } | |
369 | else if (!strcmp (*args, "-from")) | |
370 | { | |
371 | if (!args[1]) | |
372 | goto argerr; | |
373 | from = *++args; | |
374 | } | |
375 | else if (!strcmp (*args, "-subject")) | |
376 | { | |
377 | if (!args[1]) | |
378 | goto argerr; | |
379 | subject = *++args; | |
380 | } | |
381 | else if (!strcmp (*args, "-signer")) | |
382 | { | |
383 | if (!args[1]) | |
384 | goto argerr; | |
385 | /* If previous -signer argument add signer to list */ | |
386 | ||
387 | if (signerfile) | |
388 | { | |
389 | if (!sksigners) | |
5ce278a7 BL |
390 | sksigners = sk_STRING_new_null(); |
391 | sk_STRING_push(sksigners, signerfile); | |
8931b30d DSH |
392 | if (!keyfile) |
393 | keyfile = signerfile; | |
394 | if (!skkeys) | |
5ce278a7 BL |
395 | skkeys = sk_STRING_new_null(); |
396 | sk_STRING_push(skkeys, keyfile); | |
8931b30d DSH |
397 | keyfile = NULL; |
398 | } | |
399 | signerfile = *++args; | |
400 | } | |
401 | else if (!strcmp (*args, "-recip")) | |
402 | { | |
403 | if (!args[1]) | |
404 | goto argerr; | |
405 | recipfile = *++args; | |
406 | } | |
4a954b56 DSH |
407 | else if (!strcmp (*args, "-certsout")) |
408 | { | |
409 | if (!args[1]) | |
410 | goto argerr; | |
411 | certsoutfile = *++args; | |
412 | } | |
8931b30d DSH |
413 | else if (!strcmp (*args, "-md")) |
414 | { | |
415 | if (!args[1]) | |
416 | goto argerr; | |
417 | sign_md = EVP_get_digestbyname(*++args); | |
418 | if (sign_md == NULL) | |
419 | { | |
420 | BIO_printf(bio_err, "Unknown digest %s\n", | |
421 | *args); | |
422 | goto argerr; | |
423 | } | |
424 | } | |
425 | else if (!strcmp (*args, "-inkey")) | |
426 | { | |
427 | if (!args[1]) | |
428 | goto argerr; | |
429 | /* If previous -inkey arument add signer to list */ | |
430 | if (keyfile) | |
431 | { | |
432 | if (!signerfile) | |
433 | { | |
434 | BIO_puts(bio_err, "Illegal -inkey without -signer\n"); | |
435 | goto argerr; | |
436 | } | |
437 | if (!sksigners) | |
5ce278a7 BL |
438 | sksigners = sk_STRING_new_null(); |
439 | sk_STRING_push(sksigners, signerfile); | |
8931b30d DSH |
440 | signerfile = NULL; |
441 | if (!skkeys) | |
5ce278a7 BL |
442 | skkeys = sk_STRING_new_null(); |
443 | sk_STRING_push(skkeys, keyfile); | |
8931b30d DSH |
444 | } |
445 | keyfile = *++args; | |
446 | } | |
447 | else if (!strcmp (*args, "-keyform")) | |
448 | { | |
449 | if (!args[1]) | |
450 | goto argerr; | |
451 | keyform = str2fmt(*++args); | |
452 | } | |
eb9d8d8c DSH |
453 | else if (!strcmp (*args, "-rctform")) |
454 | { | |
455 | if (!args[1]) | |
456 | goto argerr; | |
457 | rctformat = str2fmt(*++args); | |
458 | } | |
8931b30d DSH |
459 | else if (!strcmp (*args, "-certfile")) |
460 | { | |
461 | if (!args[1]) | |
462 | goto argerr; | |
463 | certfile = *++args; | |
464 | } | |
465 | else if (!strcmp (*args, "-CAfile")) | |
466 | { | |
467 | if (!args[1]) | |
468 | goto argerr; | |
469 | CAfile = *++args; | |
470 | } | |
471 | else if (!strcmp (*args, "-CApath")) | |
472 | { | |
473 | if (!args[1]) | |
474 | goto argerr; | |
475 | CApath = *++args; | |
476 | } | |
477 | else if (!strcmp (*args, "-in")) | |
478 | { | |
479 | if (!args[1]) | |
480 | goto argerr; | |
481 | infile = *++args; | |
482 | } | |
483 | else if (!strcmp (*args, "-inform")) | |
484 | { | |
485 | if (!args[1]) | |
486 | goto argerr; | |
487 | informat = str2fmt(*++args); | |
488 | } | |
489 | else if (!strcmp (*args, "-outform")) | |
490 | { | |
491 | if (!args[1]) | |
492 | goto argerr; | |
493 | outformat = str2fmt(*++args); | |
494 | } | |
495 | else if (!strcmp (*args, "-out")) | |
496 | { | |
497 | if (!args[1]) | |
498 | goto argerr; | |
499 | outfile = *++args; | |
500 | } | |
501 | else if (!strcmp (*args, "-content")) | |
502 | { | |
503 | if (!args[1]) | |
504 | goto argerr; | |
505 | contfile = *++args; | |
506 | } | |
507 | else if (args_verify(&args, NULL, &badarg, bio_err, &vpm)) | |
508 | continue; | |
509 | else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL) | |
510 | badarg = 1; | |
511 | args++; | |
512 | } | |
513 | ||
f5e2354c DSH |
514 | if (((rr_allorfirst != -1) || rr_from) && !rr_to) |
515 | { | |
516 | BIO_puts(bio_err, "No Signed Receipts Recipients\n"); | |
517 | goto argerr; | |
518 | } | |
519 | ||
520 | if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) | |
521 | { | |
522 | BIO_puts(bio_err, "Signed receipts only allowed with -sign\n"); | |
523 | goto argerr; | |
524 | } | |
8931b30d DSH |
525 | if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) |
526 | { | |
527 | BIO_puts(bio_err, "Multiple signers or keys not allowed\n"); | |
528 | goto argerr; | |
529 | } | |
530 | ||
531 | if (operation & SMIME_SIGNERS) | |
532 | { | |
8931b30d DSH |
533 | if (keyfile && !signerfile) |
534 | { | |
535 | BIO_puts(bio_err, "Illegal -inkey without -signer\n"); | |
536 | goto argerr; | |
537 | } | |
f5e2354c | 538 | /* Check to see if any final signer needs to be appended */ |
8931b30d DSH |
539 | if (signerfile) |
540 | { | |
541 | if (!sksigners) | |
5ce278a7 BL |
542 | sksigners = sk_STRING_new_null(); |
543 | sk_STRING_push(sksigners, signerfile); | |
8931b30d | 544 | if (!skkeys) |
5ce278a7 | 545 | skkeys = sk_STRING_new_null(); |
8931b30d DSH |
546 | if (!keyfile) |
547 | keyfile = signerfile; | |
5ce278a7 | 548 | sk_STRING_push(skkeys, keyfile); |
8931b30d DSH |
549 | } |
550 | if (!sksigners) | |
551 | { | |
552 | BIO_printf(bio_err, "No signer certificate specified\n"); | |
553 | badarg = 1; | |
554 | } | |
555 | signerfile = NULL; | |
556 | keyfile = NULL; | |
557 | need_rand = 1; | |
558 | } | |
36309aa2 | 559 | |
8931b30d DSH |
560 | else if (operation == SMIME_DECRYPT) |
561 | { | |
eeb9cdfc | 562 | if (!recipfile && !keyfile && !secret_key) |
8931b30d DSH |
563 | { |
564 | BIO_printf(bio_err, "No recipient certificate or key specified\n"); | |
565 | badarg = 1; | |
566 | } | |
567 | } | |
568 | else if (operation == SMIME_ENCRYPT) | |
569 | { | |
ab124380 | 570 | if (!*args && !secret_key) |
8931b30d DSH |
571 | { |
572 | BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n"); | |
573 | badarg = 1; | |
574 | } | |
575 | need_rand = 1; | |
576 | } | |
577 | else if (!operation) | |
578 | badarg = 1; | |
579 | ||
580 | if (badarg) | |
581 | { | |
582 | argerr: | |
7f50d9a4 | 583 | BIO_printf (bio_err, "Usage cms [options] cert.pem ...\n"); |
8931b30d DSH |
584 | BIO_printf (bio_err, "where options are\n"); |
585 | BIO_printf (bio_err, "-encrypt encrypt message\n"); | |
586 | BIO_printf (bio_err, "-decrypt decrypt encrypted message\n"); | |
587 | BIO_printf (bio_err, "-sign sign message\n"); | |
588 | BIO_printf (bio_err, "-verify verify signed message\n"); | |
589 | BIO_printf (bio_err, "-cmsout output CMS structure\n"); | |
590 | #ifndef OPENSSL_NO_DES | |
591 | BIO_printf (bio_err, "-des3 encrypt with triple DES\n"); | |
592 | BIO_printf (bio_err, "-des encrypt with DES\n"); | |
593 | #endif | |
594 | #ifndef OPENSSL_NO_SEED | |
595 | BIO_printf (bio_err, "-seed encrypt with SEED\n"); | |
596 | #endif | |
597 | #ifndef OPENSSL_NO_RC2 | |
598 | BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n"); | |
599 | BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n"); | |
600 | BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n"); | |
601 | #endif | |
602 | #ifndef OPENSSL_NO_AES | |
603 | BIO_printf (bio_err, "-aes128, -aes192, -aes256\n"); | |
604 | BIO_printf (bio_err, " encrypt PEM output with cbc aes\n"); | |
605 | #endif | |
606 | #ifndef OPENSSL_NO_CAMELLIA | |
607 | BIO_printf (bio_err, "-camellia128, -camellia192, -camellia256\n"); | |
608 | BIO_printf (bio_err, " encrypt PEM output with cbc camellia\n"); | |
609 | #endif | |
610 | BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n"); | |
611 | BIO_printf (bio_err, "-nosigs don't verify message signature\n"); | |
612 | BIO_printf (bio_err, "-noverify don't verify signers certificate\n"); | |
613 | BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n"); | |
614 | BIO_printf (bio_err, "-nodetach use opaque signing\n"); | |
615 | BIO_printf (bio_err, "-noattr don't include any signed attributes\n"); | |
616 | BIO_printf (bio_err, "-binary don't translate message to text\n"); | |
617 | BIO_printf (bio_err, "-certfile file other certificates file\n"); | |
4a954b56 | 618 | BIO_printf (bio_err, "-certsout file certificate output file\n"); |
8931b30d DSH |
619 | BIO_printf (bio_err, "-signer file signer certificate file\n"); |
620 | BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n"); | |
621 | BIO_printf (bio_err, "-skeyid use subject key identifier\n"); | |
622 | BIO_printf (bio_err, "-in file input file\n"); | |
623 | BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n"); | |
624 | BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n"); | |
625 | BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n"); | |
626 | BIO_printf (bio_err, "-out file output file\n"); | |
627 | BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n"); | |
628 | BIO_printf (bio_err, "-content file supply or override content for detached signature\n"); | |
629 | BIO_printf (bio_err, "-to addr to address\n"); | |
630 | BIO_printf (bio_err, "-from ad from address\n"); | |
631 | BIO_printf (bio_err, "-subject s subject\n"); | |
632 | BIO_printf (bio_err, "-text include or delete text MIME headers\n"); | |
633 | BIO_printf (bio_err, "-CApath dir trusted certificates directory\n"); | |
634 | BIO_printf (bio_err, "-CAfile file trusted certificates file\n"); | |
635 | BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n"); | |
636 | BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n"); | |
637 | #ifndef OPENSSL_NO_ENGINE | |
638 | BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n"); | |
639 | #endif | |
640 | BIO_printf (bio_err, "-passin arg input file pass phrase source\n"); | |
641 | BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); | |
642 | BIO_printf(bio_err, " load the file (or the files in the directory) into\n"); | |
643 | BIO_printf(bio_err, " the random number generator\n"); | |
644 | BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n"); | |
645 | goto end; | |
646 | } | |
647 | ||
648 | #ifndef OPENSSL_NO_ENGINE | |
649 | e = setup_engine(bio_err, engine, 0); | |
650 | #endif | |
651 | ||
652 | if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) | |
653 | { | |
654 | BIO_printf(bio_err, "Error getting password\n"); | |
655 | goto end; | |
656 | } | |
657 | ||
658 | if (need_rand) | |
659 | { | |
660 | app_RAND_load_file(NULL, bio_err, (inrand != NULL)); | |
661 | if (inrand != NULL) | |
662 | BIO_printf(bio_err,"%ld semi-random bytes loaded\n", | |
663 | app_RAND_load_files(inrand)); | |
664 | } | |
665 | ||
666 | ret = 2; | |
667 | ||
668 | if (!(operation & SMIME_SIGNERS)) | |
669 | flags &= ~CMS_DETACHED; | |
670 | ||
671 | if (operation & SMIME_OP) | |
672 | { | |
673 | if (outformat == FORMAT_ASN1) | |
674 | outmode = "wb"; | |
675 | } | |
676 | else | |
677 | { | |
678 | if (flags & CMS_BINARY) | |
679 | outmode = "wb"; | |
680 | } | |
681 | ||
682 | if (operation & SMIME_IP) | |
683 | { | |
684 | if (informat == FORMAT_ASN1) | |
685 | inmode = "rb"; | |
686 | } | |
687 | else | |
688 | { | |
689 | if (flags & CMS_BINARY) | |
690 | inmode = "rb"; | |
691 | } | |
692 | ||
693 | if (operation == SMIME_ENCRYPT) | |
694 | { | |
695 | if (!cipher) | |
696 | { | |
c220e58f DSH |
697 | #ifndef OPENSSL_NO_DES |
698 | cipher = EVP_des_ede3_cbc(); | |
8931b30d DSH |
699 | #else |
700 | BIO_printf(bio_err, "No cipher selected\n"); | |
701 | goto end; | |
702 | #endif | |
703 | } | |
ab124380 DSH |
704 | |
705 | if (secret_key && !secret_keyid) | |
706 | { | |
9990cb75 | 707 | BIO_printf(bio_err, "No secret key id\n"); |
ab124380 DSH |
708 | goto end; |
709 | } | |
710 | ||
711 | if (*args) | |
712 | encerts = sk_X509_new_null(); | |
8931b30d DSH |
713 | while (*args) |
714 | { | |
715 | if (!(cert = load_cert(bio_err,*args,FORMAT_PEM, | |
716 | NULL, e, "recipient certificate file"))) | |
8931b30d | 717 | goto end; |
8931b30d DSH |
718 | sk_X509_push(encerts, cert); |
719 | cert = NULL; | |
720 | args++; | |
721 | } | |
722 | } | |
723 | ||
724 | if (certfile) | |
725 | { | |
726 | if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL, | |
727 | e, "certificate file"))) | |
728 | { | |
729 | ERR_print_errors(bio_err); | |
730 | goto end; | |
731 | } | |
732 | } | |
733 | ||
734 | if (recipfile && (operation == SMIME_DECRYPT)) | |
735 | { | |
736 | if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL, | |
737 | e, "recipient certificate file"))) | |
738 | { | |
739 | ERR_print_errors(bio_err); | |
740 | goto end; | |
741 | } | |
742 | } | |
743 | ||
36309aa2 DSH |
744 | if (operation == SMIME_SIGN_RECEIPT) |
745 | { | |
746 | if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM,NULL, | |
747 | e, "receipt signer certificate file"))) | |
748 | { | |
749 | ERR_print_errors(bio_err); | |
750 | goto end; | |
751 | } | |
752 | } | |
753 | ||
8931b30d DSH |
754 | if (operation == SMIME_DECRYPT) |
755 | { | |
756 | if (!keyfile) | |
757 | keyfile = recipfile; | |
758 | } | |
36309aa2 | 759 | else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) |
8931b30d DSH |
760 | { |
761 | if (!keyfile) | |
762 | keyfile = signerfile; | |
763 | } | |
764 | else keyfile = NULL; | |
765 | ||
766 | if (keyfile) | |
767 | { | |
768 | key = load_key(bio_err, keyfile, keyform, 0, passin, e, | |
769 | "signing key file"); | |
770 | if (!key) | |
771 | goto end; | |
772 | } | |
773 | ||
774 | if (infile) | |
775 | { | |
776 | if (!(in = BIO_new_file(infile, inmode))) | |
777 | { | |
778 | BIO_printf (bio_err, | |
779 | "Can't open input file %s\n", infile); | |
780 | goto end; | |
781 | } | |
782 | } | |
783 | else | |
784 | in = BIO_new_fp(stdin, BIO_NOCLOSE); | |
785 | ||
786 | if (operation & SMIME_IP) | |
787 | { | |
788 | if (informat == FORMAT_SMIME) | |
789 | cms = SMIME_read_CMS(in, &indata); | |
790 | else if (informat == FORMAT_PEM) | |
791 | cms = PEM_read_bio_CMS(in, NULL, NULL, NULL); | |
792 | else if (informat == FORMAT_ASN1) | |
793 | cms = d2i_CMS_bio(in, NULL); | |
794 | else | |
795 | { | |
796 | BIO_printf(bio_err, "Bad input format for CMS file\n"); | |
797 | goto end; | |
798 | } | |
799 | ||
800 | if (!cms) | |
801 | { | |
802 | BIO_printf(bio_err, "Error reading S/MIME message\n"); | |
803 | goto end; | |
804 | } | |
805 | if (contfile) | |
806 | { | |
807 | BIO_free(indata); | |
808 | if (!(indata = BIO_new_file(contfile, "rb"))) | |
809 | { | |
810 | BIO_printf(bio_err, "Can't read content file %s\n", contfile); | |
811 | goto end; | |
812 | } | |
813 | } | |
4a954b56 DSH |
814 | if (certsoutfile) |
815 | { | |
816 | STACK_OF(X509) *allcerts; | |
817 | allcerts = CMS_get1_certs(cms); | |
818 | if (!save_certs(certsoutfile, allcerts)) | |
819 | { | |
820 | BIO_printf(bio_err, | |
821 | "Error writing certs to %s\n", | |
822 | certsoutfile); | |
823 | ret = 5; | |
824 | goto end; | |
825 | } | |
826 | sk_X509_pop_free(allcerts, X509_free); | |
827 | } | |
8931b30d DSH |
828 | } |
829 | ||
eb9d8d8c DSH |
830 | if (rctfile) |
831 | { | |
832 | char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r"; | |
833 | if (!(rctin = BIO_new_file(rctfile, rctmode))) | |
834 | { | |
835 | BIO_printf (bio_err, | |
836 | "Can't open receipt file %s\n", rctfile); | |
837 | goto end; | |
838 | } | |
839 | ||
840 | if (rctformat == FORMAT_SMIME) | |
841 | rcms = SMIME_read_CMS(rctin, NULL); | |
842 | else if (rctformat == FORMAT_PEM) | |
843 | rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL); | |
844 | else if (rctformat == FORMAT_ASN1) | |
845 | rcms = d2i_CMS_bio(rctin, NULL); | |
846 | else | |
847 | { | |
848 | BIO_printf(bio_err, "Bad input format for receipt\n"); | |
849 | goto end; | |
850 | } | |
851 | ||
852 | if (!rcms) | |
853 | { | |
854 | BIO_printf(bio_err, "Error reading receipt\n"); | |
855 | goto end; | |
856 | } | |
857 | } | |
858 | ||
8931b30d DSH |
859 | if (outfile) |
860 | { | |
861 | if (!(out = BIO_new_file(outfile, outmode))) | |
862 | { | |
863 | BIO_printf (bio_err, | |
864 | "Can't open output file %s\n", outfile); | |
865 | goto end; | |
866 | } | |
867 | } | |
868 | else | |
869 | { | |
870 | out = BIO_new_fp(stdout, BIO_NOCLOSE); | |
871 | #ifdef OPENSSL_SYS_VMS | |
872 | { | |
873 | BIO *tmpbio = BIO_new(BIO_f_linebuffer()); | |
874 | out = BIO_push(tmpbio, out); | |
875 | } | |
876 | #endif | |
877 | } | |
878 | ||
eb9d8d8c | 879 | if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) |
8931b30d DSH |
880 | { |
881 | if (!(store = setup_verify(bio_err, CAfile, CApath))) | |
882 | goto end; | |
7f50d9a4 | 883 | X509_STORE_set_verify_cb_func(store, cms_cb); |
8931b30d DSH |
884 | if (vpm) |
885 | X509_STORE_set1_param(store, vpm); | |
886 | } | |
887 | ||
888 | ||
889 | ret = 3; | |
890 | ||
891 | if (operation == SMIME_DATA_CREATE) | |
892 | { | |
8931b30d DSH |
893 | cms = CMS_data_create(in, flags); |
894 | } | |
895 | else if (operation == SMIME_DIGEST_CREATE) | |
896 | { | |
8931b30d DSH |
897 | cms = CMS_digest_create(in, sign_md, flags); |
898 | } | |
899 | else if (operation == SMIME_COMPRESS) | |
900 | { | |
8931b30d DSH |
901 | cms = CMS_compress(in, -1, flags); |
902 | } | |
903 | else if (operation == SMIME_ENCRYPT) | |
904 | { | |
ab124380 | 905 | flags |= CMS_PARTIAL; |
8931b30d | 906 | cms = CMS_encrypt(encerts, in, cipher, flags); |
ab124380 DSH |
907 | if (!cms) |
908 | goto end; | |
909 | if (secret_key) | |
910 | { | |
911 | if (!CMS_add0_recipient_key(cms, NID_undef, | |
912 | secret_key, secret_keylen, | |
913 | secret_keyid, secret_keyidlen, | |
914 | NULL, NULL, NULL)) | |
915 | goto end; | |
916 | /* NULL these because call absorbs them */ | |
917 | secret_key = NULL; | |
918 | secret_keyid = NULL; | |
919 | } | |
920 | if (!(flags & CMS_STREAM)) | |
921 | { | |
e0fbd073 | 922 | if (!CMS_final(cms, in, NULL, flags)) |
ab124380 DSH |
923 | goto end; |
924 | } | |
8931b30d | 925 | } |
d9f5f07e DSH |
926 | else if (operation == SMIME_ENCRYPTED_ENCRYPT) |
927 | { | |
d9f5f07e DSH |
928 | cms = CMS_EncryptedData_encrypt(in, cipher, |
929 | secret_key, secret_keylen, | |
930 | flags); | |
ab124380 | 931 | |
36309aa2 DSH |
932 | } |
933 | else if (operation == SMIME_SIGN_RECEIPT) | |
934 | { | |
935 | CMS_ContentInfo *srcms = NULL; | |
936 | STACK_OF(CMS_SignerInfo) *sis; | |
937 | CMS_SignerInfo *si; | |
938 | sis = CMS_get0_SignerInfos(cms); | |
939 | if (!sis) | |
940 | goto end; | |
941 | si = sk_CMS_SignerInfo_value(sis, 0); | |
942 | srcms = CMS_sign_receipt(si, signer, key, other, flags); | |
943 | if (!srcms) | |
944 | goto end; | |
945 | CMS_ContentInfo_free(cms); | |
946 | cms = srcms; | |
d9f5f07e | 947 | } |
8931b30d DSH |
948 | else if (operation & SMIME_SIGNERS) |
949 | { | |
950 | int i; | |
f5e2354c | 951 | /* If detached data content we enable streaming if |
8931b30d DSH |
952 | * S/MIME output format. |
953 | */ | |
954 | if (operation == SMIME_SIGN) | |
955 | { | |
f5e2354c | 956 | |
8931b30d DSH |
957 | if (flags & CMS_DETACHED) |
958 | { | |
f5e2354c DSH |
959 | if (outformat == FORMAT_SMIME) |
960 | flags |= CMS_STREAM; | |
8931b30d | 961 | } |
8931b30d DSH |
962 | flags |= CMS_PARTIAL; |
963 | cms = CMS_sign(NULL, NULL, other, in, flags); | |
964 | if (!cms) | |
965 | goto end; | |
f5e2354c DSH |
966 | if (econtent_type) |
967 | CMS_set1_eContentType(cms, econtent_type); | |
968 | ||
969 | if (rr_to) | |
970 | { | |
971 | rr = make_receipt_request(rr_to, rr_allorfirst, | |
972 | rr_from); | |
973 | if (!rr) | |
974 | { | |
975 | BIO_puts(bio_err, | |
976 | "Signed Receipt Request Creation Error\n"); | |
977 | goto end; | |
978 | } | |
979 | } | |
8931b30d DSH |
980 | } |
981 | else | |
982 | flags |= CMS_REUSE_DIGEST; | |
5ce278a7 | 983 | for (i = 0; i < sk_STRING_num(sksigners); i++) |
8931b30d | 984 | { |
f5e2354c | 985 | CMS_SignerInfo *si; |
5ce278a7 BL |
986 | signerfile = sk_STRING_value(sksigners, i); |
987 | keyfile = sk_STRING_value(skkeys, i); | |
8931b30d DSH |
988 | signer = load_cert(bio_err, signerfile,FORMAT_PEM, NULL, |
989 | e, "signer certificate"); | |
990 | if (!signer) | |
991 | goto end; | |
992 | key = load_key(bio_err, keyfile, keyform, 0, passin, e, | |
993 | "signing key file"); | |
994 | if (!key) | |
995 | goto end; | |
f5e2354c DSH |
996 | si = CMS_add1_signer(cms, signer, key, sign_md, flags); |
997 | if (!si) | |
998 | goto end; | |
999 | if (rr && !CMS_add1_ReceiptRequest(si, rr)) | |
8931b30d DSH |
1000 | goto end; |
1001 | X509_free(signer); | |
1002 | signer = NULL; | |
1003 | EVP_PKEY_free(key); | |
1004 | key = NULL; | |
1005 | } | |
1006 | /* If not streaming or resigning finalize structure */ | |
1007 | if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) | |
1008 | { | |
e0fbd073 | 1009 | if (!CMS_final(cms, in, NULL, flags)) |
8931b30d DSH |
1010 | goto end; |
1011 | } | |
1012 | } | |
1013 | ||
1014 | if (!cms) | |
1015 | { | |
1016 | BIO_printf(bio_err, "Error creating CMS structure\n"); | |
1017 | goto end; | |
1018 | } | |
1019 | ||
1020 | ret = 4; | |
1021 | if (operation == SMIME_DECRYPT) | |
1022 | { | |
eeb9cdfc DSH |
1023 | |
1024 | if (secret_key) | |
1025 | { | |
1026 | if (!CMS_decrypt_set1_key(cms, | |
1027 | secret_key, secret_keylen, | |
1028 | secret_keyid, secret_keyidlen)) | |
1029 | { | |
1030 | BIO_puts(bio_err, | |
1031 | "Error decrypting CMS using secret key\n"); | |
1032 | goto end; | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | if (key) | |
1037 | { | |
1038 | if (!CMS_decrypt_set1_pkey(cms, key, recip)) | |
1039 | { | |
1040 | BIO_puts(bio_err, | |
1041 | "Error decrypting CMS using private key\n"); | |
1042 | goto end; | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) | |
8931b30d DSH |
1047 | { |
1048 | BIO_printf(bio_err, "Error decrypting CMS structure\n"); | |
1049 | goto end; | |
1050 | } | |
1051 | } | |
1052 | else if (operation == SMIME_DATAOUT) | |
1053 | { | |
1054 | if (!CMS_data(cms, out, flags)) | |
1055 | goto end; | |
1056 | } | |
1057 | else if (operation == SMIME_UNCOMPRESS) | |
1058 | { | |
a5db50d0 | 1059 | if (!CMS_uncompress(cms, indata, out, flags)) |
8931b30d DSH |
1060 | goto end; |
1061 | } | |
1062 | else if (operation == SMIME_DIGEST_VERIFY) | |
1063 | { | |
1064 | if (CMS_digest_verify(cms, indata, out, flags) > 0) | |
1065 | BIO_printf(bio_err, "Verification successful\n"); | |
1066 | else | |
1067 | { | |
1068 | BIO_printf(bio_err, "Verification failure\n"); | |
1069 | goto end; | |
1070 | } | |
1071 | } | |
b820455c DSH |
1072 | else if (operation == SMIME_ENCRYPTED_DECRYPT) |
1073 | { | |
1074 | if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen, | |
1075 | indata, out, flags)) | |
1076 | goto end; | |
1077 | } | |
8931b30d DSH |
1078 | else if (operation == SMIME_VERIFY) |
1079 | { | |
1080 | if (CMS_verify(cms, other, store, indata, out, flags) > 0) | |
1081 | BIO_printf(bio_err, "Verification successful\n"); | |
1082 | else | |
1083 | { | |
1084 | BIO_printf(bio_err, "Verification failure\n"); | |
db50661f DSH |
1085 | if (verify_retcode) |
1086 | ret = verify_err + 32; | |
8931b30d DSH |
1087 | goto end; |
1088 | } | |
1089 | if (signerfile) | |
1090 | { | |
1091 | STACK_OF(X509) *signers; | |
1092 | signers = CMS_get0_signers(cms); | |
1093 | if (!save_certs(signerfile, signers)) | |
1094 | { | |
1095 | BIO_printf(bio_err, | |
1096 | "Error writing signers to %s\n", | |
1097 | signerfile); | |
1098 | ret = 5; | |
1099 | goto end; | |
1100 | } | |
1101 | sk_X509_free(signers); | |
1102 | } | |
f4cc56f4 DSH |
1103 | if (rr_print) |
1104 | receipt_request_print(bio_err, cms); | |
1105 | ||
8931b30d | 1106 | } |
eb9d8d8c DSH |
1107 | else if (operation == SMIME_VERIFY_RECEIPT) |
1108 | { | |
1109 | if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) | |
1110 | BIO_printf(bio_err, "Verification successful\n"); | |
1111 | else | |
1112 | { | |
1113 | BIO_printf(bio_err, "Verification failure\n"); | |
1114 | goto end; | |
1115 | } | |
1116 | } | |
8931b30d DSH |
1117 | else |
1118 | { | |
62051713 DSH |
1119 | if (noout) |
1120 | { | |
1121 | if (print) | |
1122 | CMS_ContentInfo_print_ctx(out, cms, 0, NULL); | |
1123 | } | |
1124 | else if (outformat == FORMAT_SMIME) | |
8931b30d DSH |
1125 | { |
1126 | if (to) | |
1127 | BIO_printf(out, "To: %s\n", to); | |
1128 | if (from) | |
1129 | BIO_printf(out, "From: %s\n", from); | |
1130 | if (subject) | |
1131 | BIO_printf(out, "Subject: %s\n", subject); | |
1132 | if (operation == SMIME_RESIGN) | |
1133 | ret = SMIME_write_CMS(out, cms, indata, flags); | |
1134 | else | |
1135 | ret = SMIME_write_CMS(out, cms, in, flags); | |
1136 | } | |
1137 | else if (outformat == FORMAT_PEM) | |
1138 | ret = PEM_write_bio_CMS_stream(out, cms, in, flags); | |
1139 | else if (outformat == FORMAT_ASN1) | |
1140 | ret = i2d_CMS_bio_stream(out,cms, in, flags); | |
1141 | else | |
1142 | { | |
1143 | BIO_printf(bio_err, "Bad output format for CMS file\n"); | |
1144 | goto end; | |
1145 | } | |
1146 | if (ret <= 0) | |
1147 | { | |
1148 | ret = 6; | |
1149 | goto end; | |
1150 | } | |
1151 | } | |
1152 | ret = 0; | |
1153 | end: | |
1154 | if (ret) | |
1155 | ERR_print_errors(bio_err); | |
1156 | if (need_rand) | |
1157 | app_RAND_write_file(NULL, bio_err); | |
1158 | sk_X509_pop_free(encerts, X509_free); | |
1159 | sk_X509_pop_free(other, X509_free); | |
1160 | if (vpm) | |
1161 | X509_VERIFY_PARAM_free(vpm); | |
1162 | if (sksigners) | |
5ce278a7 | 1163 | sk_STRING_free(sksigners); |
8931b30d | 1164 | if (skkeys) |
5ce278a7 | 1165 | sk_STRING_free(skkeys); |
b820455c DSH |
1166 | if (secret_key) |
1167 | OPENSSL_free(secret_key); | |
ab124380 DSH |
1168 | if (secret_keyid) |
1169 | OPENSSL_free(secret_keyid); | |
054307e7 DSH |
1170 | if (econtent_type) |
1171 | ASN1_OBJECT_free(econtent_type); | |
f5e2354c DSH |
1172 | if (rr) |
1173 | CMS_ReceiptRequest_free(rr); | |
1174 | if (rr_to) | |
5ce278a7 | 1175 | sk_STRING_free(rr_to); |
f5e2354c | 1176 | if (rr_from) |
5ce278a7 | 1177 | sk_STRING_free(rr_from); |
8931b30d DSH |
1178 | X509_STORE_free(store); |
1179 | X509_free(cert); | |
1180 | X509_free(recip); | |
1181 | X509_free(signer); | |
1182 | EVP_PKEY_free(key); | |
1183 | CMS_ContentInfo_free(cms); | |
eb9d8d8c DSH |
1184 | CMS_ContentInfo_free(rcms); |
1185 | BIO_free(rctin); | |
8931b30d DSH |
1186 | BIO_free(in); |
1187 | BIO_free(indata); | |
1188 | BIO_free_all(out); | |
1189 | if (passin) OPENSSL_free(passin); | |
1190 | return (ret); | |
1191 | } | |
1192 | ||
1193 | static int save_certs(char *signerfile, STACK_OF(X509) *signers) | |
1194 | { | |
1195 | int i; | |
1196 | BIO *tmp; | |
1197 | if (!signerfile) | |
1198 | return 1; | |
1199 | tmp = BIO_new_file(signerfile, "w"); | |
1200 | if (!tmp) return 0; | |
1201 | for(i = 0; i < sk_X509_num(signers); i++) | |
1202 | PEM_write_bio_X509(tmp, sk_X509_value(signers, i)); | |
1203 | BIO_free(tmp); | |
1204 | return 1; | |
1205 | } | |
1206 | ||
1207 | ||
1208 | /* Minimal callback just to output policy info (if any) */ | |
1209 | ||
7f50d9a4 | 1210 | static int cms_cb(int ok, X509_STORE_CTX *ctx) |
8931b30d DSH |
1211 | { |
1212 | int error; | |
1213 | ||
1214 | error = X509_STORE_CTX_get_error(ctx); | |
1215 | ||
db50661f DSH |
1216 | verify_err = error; |
1217 | ||
8931b30d DSH |
1218 | if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) |
1219 | && ((error != X509_V_OK) || (ok != 2))) | |
1220 | return ok; | |
1221 | ||
1222 | policies_print(NULL, ctx); | |
1223 | ||
1224 | return ok; | |
1225 | ||
1226 | } | |
1227 | ||
f4cc56f4 DSH |
1228 | static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns) |
1229 | { | |
1230 | STACK_OF(GENERAL_NAME) *gens; | |
1231 | GENERAL_NAME *gen; | |
1232 | int i, j; | |
1233 | for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) | |
1234 | { | |
1235 | gens = sk_GENERAL_NAMES_value(gns, i); | |
1236 | for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) | |
1237 | { | |
1238 | gen = sk_GENERAL_NAME_value(gens, j); | |
1239 | BIO_puts(out, " "); | |
1240 | GENERAL_NAME_print(out, gen); | |
1241 | BIO_puts(out, "\n"); | |
1242 | } | |
1243 | } | |
1244 | return; | |
1245 | } | |
1246 | ||
1247 | static void receipt_request_print(BIO *out, CMS_ContentInfo *cms) | |
1248 | { | |
1249 | STACK_OF(CMS_SignerInfo) *sis; | |
1250 | CMS_SignerInfo *si; | |
1251 | CMS_ReceiptRequest *rr; | |
1252 | int allorfirst; | |
1253 | STACK_OF(GENERAL_NAMES) *rto, *rlist; | |
1254 | ASN1_STRING *scid; | |
1255 | int i, rv; | |
1256 | sis = CMS_get0_SignerInfos(cms); | |
1257 | for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) | |
1258 | { | |
1259 | si = sk_CMS_SignerInfo_value(sis, i); | |
1260 | rv = CMS_get1_ReceiptRequest(si, &rr); | |
1261 | BIO_printf(bio_err, "Signer %d:\n", i + 1); | |
1262 | if (rv == 0) | |
1263 | BIO_puts(bio_err, " No Receipt Request\n"); | |
1264 | else if (rv < 0) | |
1265 | { | |
1266 | BIO_puts(bio_err, " Receipt Request Parse Error\n"); | |
1267 | ERR_print_errors(bio_err); | |
1268 | } | |
1269 | else | |
1270 | { | |
1271 | char *id; | |
1272 | int idlen; | |
1273 | CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst, | |
1274 | &rlist, &rto); | |
1275 | BIO_puts(out, " Signed Content ID:\n"); | |
1276 | idlen = ASN1_STRING_length(scid); | |
1277 | id = (char *)ASN1_STRING_data(scid); | |
1278 | BIO_dump_indent(out, id, idlen, 4); | |
1279 | BIO_puts(out, " Receipts From"); | |
1280 | if (rlist) | |
1281 | { | |
1282 | BIO_puts(out, " List:\n"); | |
1283 | gnames_stack_print(out, rlist); | |
1284 | } | |
1285 | else if (allorfirst == 1) | |
1286 | BIO_puts(out, ": First Tier\n"); | |
1287 | else if (allorfirst == 0) | |
1288 | BIO_puts(out, ": All\n"); | |
1289 | else | |
1290 | BIO_printf(out, " Unknown (%d)\n", allorfirst); | |
1291 | BIO_puts(out, " Receipts To:\n"); | |
1292 | gnames_stack_print(out, rto); | |
1293 | } | |
1294 | if (rr) | |
1295 | CMS_ReceiptRequest_free(rr); | |
1296 | } | |
1297 | } | |
1298 | ||
5ce278a7 | 1299 | static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(STRING) *ns) |
f5e2354c DSH |
1300 | { |
1301 | int i; | |
1302 | STACK_OF(GENERAL_NAMES) *ret; | |
1303 | GENERAL_NAMES *gens = NULL; | |
1304 | GENERAL_NAME *gen = NULL; | |
1305 | ret = sk_GENERAL_NAMES_new_null(); | |
1306 | if (!ret) | |
1307 | goto err; | |
5ce278a7 | 1308 | for (i = 0; i < sk_STRING_num(ns); i++) |
f5e2354c | 1309 | { |
5ce278a7 | 1310 | char *str = sk_STRING_value(ns, i); |
f5e2354c DSH |
1311 | gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0); |
1312 | if (!gen) | |
1313 | goto err; | |
1314 | gens = GENERAL_NAMES_new(); | |
1315 | if (!gens) | |
1316 | goto err; | |
1317 | if (!sk_GENERAL_NAME_push(gens, gen)) | |
1318 | goto err; | |
1319 | gen = NULL; | |
1320 | if (!sk_GENERAL_NAMES_push(ret, gens)) | |
1321 | goto err; | |
1322 | gens = NULL; | |
1323 | } | |
1324 | ||
1325 | return ret; | |
1326 | ||
1327 | err: | |
1328 | if (ret) | |
1329 | sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free); | |
1330 | if (gens) | |
1331 | GENERAL_NAMES_free(gens); | |
1332 | if (gen) | |
1333 | GENERAL_NAME_free(gen); | |
1334 | return NULL; | |
1335 | } | |
1336 | ||
1337 | ||
5ce278a7 BL |
1338 | static CMS_ReceiptRequest *make_receipt_request(STACK_OF(STRING) *rr_to, |
1339 | int rr_allorfirst, | |
1340 | STACK_OF(STRING) *rr_from) | |
f5e2354c DSH |
1341 | { |
1342 | STACK_OF(GENERAL_NAMES) *rct_to, *rct_from; | |
1343 | CMS_ReceiptRequest *rr; | |
1344 | rct_to = make_names_stack(rr_to); | |
1345 | if (!rct_to) | |
1346 | goto err; | |
1347 | if (rr_from) | |
1348 | { | |
1349 | rct_from = make_names_stack(rr_from); | |
1350 | if (!rct_from) | |
1351 | goto err; | |
1352 | } | |
1353 | else | |
1354 | rct_from = NULL; | |
1355 | rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from, | |
1356 | rct_to); | |
1357 | return rr; | |
1358 | err: | |
1359 | return NULL; | |
1360 | } | |
1361 | ||
8931b30d | 1362 | #endif |