test/amd64: update to qemu-0.10.6
[dragonfly.git] / crypto / openssl / apps / cms.c
blob6d227acabe82957b71b2488cf845538705bd3201
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 * ====================================================================
54 /* CMS utility function */
56 #include <stdio.h>
57 #include <string.h>
58 #include "apps.h"
60 #ifndef OPENSSL_NO_CMS
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>
69 #undef PROG
70 #define PROG cms_main
71 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
72 static int cms_cb(int ok, X509_STORE_CTX *ctx);
73 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms);
74 static CMS_ReceiptRequest *make_receipt_request(STACK *rr_to, int rr_allorfirst,
75 STACK *rr_from);
77 #define SMIME_OP 0x10
78 #define SMIME_IP 0x20
79 #define SMIME_SIGNERS 0x40
80 #define SMIME_ENCRYPT (1 | SMIME_OP)
81 #define SMIME_DECRYPT (2 | SMIME_IP)
82 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
83 #define SMIME_VERIFY (4 | SMIME_IP)
84 #define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
85 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
86 #define SMIME_DATAOUT (7 | SMIME_IP)
87 #define SMIME_DATA_CREATE (8 | SMIME_OP)
88 #define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
89 #define SMIME_DIGEST_CREATE (10 | SMIME_OP)
90 #define SMIME_UNCOMPRESS (11 | SMIME_IP)
91 #define SMIME_COMPRESS (12 | SMIME_OP)
92 #define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
93 #define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
94 #define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
95 #define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
97 int MAIN(int, char **);
99 int MAIN(int argc, char **argv)
101 ENGINE *e = NULL;
102 int operation = 0;
103 int ret = 0;
104 char **args;
105 const char *inmode = "r", *outmode = "w";
106 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
107 char *signerfile = NULL, *recipfile = NULL;
108 STACK *sksigners = NULL, *skkeys = NULL;
109 char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
110 char *certsoutfile = NULL;
111 const EVP_CIPHER *cipher = NULL;
112 CMS_ContentInfo *cms = NULL, *rcms = NULL;
113 X509_STORE *store = NULL;
114 X509 *cert = NULL, *recip = NULL, *signer = NULL;
115 EVP_PKEY *key = NULL;
116 STACK_OF(X509) *encerts = NULL, *other = NULL;
117 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
118 int badarg = 0;
119 int flags = CMS_DETACHED;
120 int rr_print = 0, rr_allorfirst = -1;
121 STACK *rr_to = NULL, *rr_from = NULL;
122 CMS_ReceiptRequest *rr = NULL;
123 char *to = NULL, *from = NULL, *subject = NULL;
124 char *CAfile = NULL, *CApath = NULL;
125 char *passargin = NULL, *passin = NULL;
126 char *inrand = NULL;
127 int need_rand = 0;
128 const EVP_MD *sign_md = NULL;
129 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
130 int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
131 #ifndef OPENSSL_NO_ENGINE
132 char *engine=NULL;
133 #endif
134 unsigned char *secret_key = NULL, *secret_keyid = NULL;
135 size_t secret_keylen = 0, secret_keyidlen = 0;
137 ASN1_OBJECT *econtent_type = NULL;
139 X509_VERIFY_PARAM *vpm = NULL;
141 args = argv + 1;
142 ret = 1;
144 apps_startup();
146 if (bio_err == NULL)
148 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
149 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
152 if (!load_config(bio_err, NULL))
153 goto end;
155 while (!badarg && *args && *args[0] == '-')
157 if (!strcmp (*args, "-encrypt"))
158 operation = SMIME_ENCRYPT;
159 else if (!strcmp (*args, "-decrypt"))
160 operation = SMIME_DECRYPT;
161 else if (!strcmp (*args, "-sign"))
162 operation = SMIME_SIGN;
163 else if (!strcmp (*args, "-sign_receipt"))
164 operation = SMIME_SIGN_RECEIPT;
165 else if (!strcmp (*args, "-resign"))
166 operation = SMIME_RESIGN;
167 else if (!strcmp (*args, "-verify"))
168 operation = SMIME_VERIFY;
169 else if (!strcmp(*args,"-verify_receipt"))
171 operation = SMIME_VERIFY_RECEIPT;
172 if (!args[1])
173 goto argerr;
174 args++;
175 rctfile = *args;
177 else if (!strcmp (*args, "-cmsout"))
178 operation = SMIME_CMSOUT;
179 else if (!strcmp (*args, "-data_out"))
180 operation = SMIME_DATAOUT;
181 else if (!strcmp (*args, "-data_create"))
182 operation = SMIME_DATA_CREATE;
183 else if (!strcmp (*args, "-digest_verify"))
184 operation = SMIME_DIGEST_VERIFY;
185 else if (!strcmp (*args, "-digest_create"))
186 operation = SMIME_DIGEST_CREATE;
187 else if (!strcmp (*args, "-compress"))
188 operation = SMIME_COMPRESS;
189 else if (!strcmp (*args, "-uncompress"))
190 operation = SMIME_UNCOMPRESS;
191 else if (!strcmp (*args, "-EncryptedData_decrypt"))
192 operation = SMIME_ENCRYPTED_DECRYPT;
193 else if (!strcmp (*args, "-EncryptedData_encrypt"))
194 operation = SMIME_ENCRYPTED_ENCRYPT;
195 #ifndef OPENSSL_NO_DES
196 else if (!strcmp (*args, "-des3"))
197 cipher = EVP_des_ede3_cbc();
198 else if (!strcmp (*args, "-des"))
199 cipher = EVP_des_cbc();
200 #endif
201 #ifndef OPENSSL_NO_SEED
202 else if (!strcmp (*args, "-seed"))
203 cipher = EVP_seed_cbc();
204 #endif
205 #ifndef OPENSSL_NO_RC2
206 else if (!strcmp (*args, "-rc2-40"))
207 cipher = EVP_rc2_40_cbc();
208 else if (!strcmp (*args, "-rc2-128"))
209 cipher = EVP_rc2_cbc();
210 else if (!strcmp (*args, "-rc2-64"))
211 cipher = EVP_rc2_64_cbc();
212 #endif
213 #ifndef OPENSSL_NO_AES
214 else if (!strcmp(*args,"-aes128"))
215 cipher = EVP_aes_128_cbc();
216 else if (!strcmp(*args,"-aes192"))
217 cipher = EVP_aes_192_cbc();
218 else if (!strcmp(*args,"-aes256"))
219 cipher = EVP_aes_256_cbc();
220 #endif
221 #ifndef OPENSSL_NO_CAMELLIA
222 else if (!strcmp(*args,"-camellia128"))
223 cipher = EVP_camellia_128_cbc();
224 else if (!strcmp(*args,"-camellia192"))
225 cipher = EVP_camellia_192_cbc();
226 else if (!strcmp(*args,"-camellia256"))
227 cipher = EVP_camellia_256_cbc();
228 #endif
229 else if (!strcmp (*args, "-text"))
230 flags |= CMS_TEXT;
231 else if (!strcmp (*args, "-nointern"))
232 flags |= CMS_NOINTERN;
233 else if (!strcmp (*args, "-noverify")
234 || !strcmp (*args, "-no_signer_cert_verify"))
235 flags |= CMS_NO_SIGNER_CERT_VERIFY;
236 else if (!strcmp (*args, "-nocerts"))
237 flags |= CMS_NOCERTS;
238 else if (!strcmp (*args, "-noattr"))
239 flags |= CMS_NOATTR;
240 else if (!strcmp (*args, "-nodetach"))
241 flags &= ~CMS_DETACHED;
242 else if (!strcmp (*args, "-nosmimecap"))
243 flags |= CMS_NOSMIMECAP;
244 else if (!strcmp (*args, "-binary"))
245 flags |= CMS_BINARY;
246 else if (!strcmp (*args, "-keyid"))
247 flags |= CMS_USE_KEYID;
248 else if (!strcmp (*args, "-nosigs"))
249 flags |= CMS_NOSIGS;
250 else if (!strcmp (*args, "-no_content_verify"))
251 flags |= CMS_NO_CONTENT_VERIFY;
252 else if (!strcmp (*args, "-no_attr_verify"))
253 flags |= CMS_NO_ATTR_VERIFY;
254 else if (!strcmp (*args, "-stream"))
256 args++;
257 continue;
259 else if (!strcmp (*args, "-indef"))
261 args++;
262 continue;
264 else if (!strcmp (*args, "-noindef"))
265 flags &= ~CMS_STREAM;
266 else if (!strcmp (*args, "-nooldmime"))
267 flags |= CMS_NOOLDMIMETYPE;
268 else if (!strcmp (*args, "-crlfeol"))
269 flags |= CMS_CRLFEOL;
270 else if (!strcmp (*args, "-receipt_request_print"))
271 rr_print = 1;
272 else if (!strcmp (*args, "-receipt_request_all"))
273 rr_allorfirst = 0;
274 else if (!strcmp (*args, "-receipt_request_first"))
275 rr_allorfirst = 1;
276 else if (!strcmp(*args,"-receipt_request_from"))
278 if (!args[1])
279 goto argerr;
280 args++;
281 if (!rr_from)
282 rr_from = sk_new_null();
283 sk_push(rr_from, *args);
285 else if (!strcmp(*args,"-receipt_request_to"))
287 if (!args[1])
288 goto argerr;
289 args++;
290 if (!rr_to)
291 rr_to = sk_new_null();
292 sk_push(rr_to, *args);
294 else if (!strcmp(*args,"-secretkey"))
296 long ltmp;
297 if (!args[1])
298 goto argerr;
299 args++;
300 secret_key = string_to_hex(*args, &ltmp);
301 if (!secret_key)
303 BIO_printf(bio_err, "Invalid key %s\n", *args);
304 goto argerr;
306 secret_keylen = (size_t)ltmp;
308 else if (!strcmp(*args,"-secretkeyid"))
310 long ltmp;
311 if (!args[1])
312 goto argerr;
313 args++;
314 secret_keyid = string_to_hex(*args, &ltmp);
315 if (!secret_keyid)
317 BIO_printf(bio_err, "Invalid id %s\n", *args);
318 goto argerr;
320 secret_keyidlen = (size_t)ltmp;
322 else if (!strcmp(*args,"-econtent_type"))
324 if (!args[1])
325 goto argerr;
326 args++;
327 econtent_type = OBJ_txt2obj(*args, 0);
328 if (!econtent_type)
330 BIO_printf(bio_err, "Invalid OID %s\n", *args);
331 goto argerr;
334 else if (!strcmp(*args,"-rand"))
336 if (!args[1])
337 goto argerr;
338 args++;
339 inrand = *args;
340 need_rand = 1;
342 #ifndef OPENSSL_NO_ENGINE
343 else if (!strcmp(*args,"-engine"))
345 if (!args[1])
346 goto argerr;
347 engine = *++args;
349 #endif
350 else if (!strcmp(*args,"-passin"))
352 if (!args[1])
353 goto argerr;
354 passargin = *++args;
356 else if (!strcmp (*args, "-to"))
358 if (!args[1])
359 goto argerr;
360 to = *++args;
362 else if (!strcmp (*args, "-from"))
364 if (!args[1])
365 goto argerr;
366 from = *++args;
368 else if (!strcmp (*args, "-subject"))
370 if (!args[1])
371 goto argerr;
372 subject = *++args;
374 else if (!strcmp (*args, "-signer"))
376 if (!args[1])
377 goto argerr;
378 /* If previous -signer argument add signer to list */
380 if (signerfile)
382 if (!sksigners)
383 sksigners = sk_new_null();
384 sk_push(sksigners, signerfile);
385 if (!keyfile)
386 keyfile = signerfile;
387 if (!skkeys)
388 skkeys = sk_new_null();
389 sk_push(skkeys, keyfile);
390 keyfile = NULL;
392 signerfile = *++args;
394 else if (!strcmp (*args, "-recip"))
396 if (!args[1])
397 goto argerr;
398 recipfile = *++args;
400 else if (!strcmp (*args, "-certsout"))
402 if (!args[1])
403 goto argerr;
404 certsoutfile = *++args;
406 else if (!strcmp (*args, "-md"))
408 if (!args[1])
409 goto argerr;
410 sign_md = EVP_get_digestbyname(*++args);
411 if (sign_md == NULL)
413 BIO_printf(bio_err, "Unknown digest %s\n",
414 *args);
415 goto argerr;
418 else if (!strcmp (*args, "-inkey"))
420 if (!args[1])
421 goto argerr;
422 /* If previous -inkey arument add signer to list */
423 if (keyfile)
425 if (!signerfile)
427 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
428 goto argerr;
430 if (!sksigners)
431 sksigners = sk_new_null();
432 sk_push(sksigners, signerfile);
433 signerfile = NULL;
434 if (!skkeys)
435 skkeys = sk_new_null();
436 sk_push(skkeys, keyfile);
438 keyfile = *++args;
440 else if (!strcmp (*args, "-keyform"))
442 if (!args[1])
443 goto argerr;
444 keyform = str2fmt(*++args);
446 else if (!strcmp (*args, "-rctform"))
448 if (!args[1])
449 goto argerr;
450 rctformat = str2fmt(*++args);
452 else if (!strcmp (*args, "-certfile"))
454 if (!args[1])
455 goto argerr;
456 certfile = *++args;
458 else if (!strcmp (*args, "-CAfile"))
460 if (!args[1])
461 goto argerr;
462 CAfile = *++args;
464 else if (!strcmp (*args, "-CApath"))
466 if (!args[1])
467 goto argerr;
468 CApath = *++args;
470 else if (!strcmp (*args, "-in"))
472 if (!args[1])
473 goto argerr;
474 infile = *++args;
476 else if (!strcmp (*args, "-inform"))
478 if (!args[1])
479 goto argerr;
480 informat = str2fmt(*++args);
482 else if (!strcmp (*args, "-outform"))
484 if (!args[1])
485 goto argerr;
486 outformat = str2fmt(*++args);
488 else if (!strcmp (*args, "-out"))
490 if (!args[1])
491 goto argerr;
492 outfile = *++args;
494 else if (!strcmp (*args, "-content"))
496 if (!args[1])
497 goto argerr;
498 contfile = *++args;
500 else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
501 continue;
502 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
503 badarg = 1;
504 args++;
507 if (((rr_allorfirst != -1) || rr_from) && !rr_to)
509 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
510 goto argerr;
513 if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from))
515 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
516 goto argerr;
518 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners))
520 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
521 goto argerr;
524 if (operation & SMIME_SIGNERS)
526 if (keyfile && !signerfile)
528 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
529 goto argerr;
531 /* Check to see if any final signer needs to be appended */
532 if (signerfile)
534 if (!sksigners)
535 sksigners = sk_new_null();
536 sk_push(sksigners, signerfile);
537 if (!skkeys)
538 skkeys = sk_new_null();
539 if (!keyfile)
540 keyfile = signerfile;
541 sk_push(skkeys, keyfile);
543 if (!sksigners)
545 BIO_printf(bio_err, "No signer certificate specified\n");
546 badarg = 1;
548 signerfile = NULL;
549 keyfile = NULL;
550 need_rand = 1;
553 else if (operation == SMIME_DECRYPT)
555 if (!recipfile && !keyfile && !secret_key)
557 BIO_printf(bio_err, "No recipient certificate or key specified\n");
558 badarg = 1;
561 else if (operation == SMIME_ENCRYPT)
563 if (!*args && !secret_key)
565 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
566 badarg = 1;
568 need_rand = 1;
570 else if (!operation)
571 badarg = 1;
573 if (badarg)
575 argerr:
576 BIO_printf (bio_err, "Usage cms [options] cert.pem ...\n");
577 BIO_printf (bio_err, "where options are\n");
578 BIO_printf (bio_err, "-encrypt encrypt message\n");
579 BIO_printf (bio_err, "-decrypt decrypt encrypted message\n");
580 BIO_printf (bio_err, "-sign sign message\n");
581 BIO_printf (bio_err, "-verify verify signed message\n");
582 BIO_printf (bio_err, "-cmsout output CMS structure\n");
583 #ifndef OPENSSL_NO_DES
584 BIO_printf (bio_err, "-des3 encrypt with triple DES\n");
585 BIO_printf (bio_err, "-des encrypt with DES\n");
586 #endif
587 #ifndef OPENSSL_NO_SEED
588 BIO_printf (bio_err, "-seed encrypt with SEED\n");
589 #endif
590 #ifndef OPENSSL_NO_RC2
591 BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
592 BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n");
593 BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n");
594 #endif
595 #ifndef OPENSSL_NO_AES
596 BIO_printf (bio_err, "-aes128, -aes192, -aes256\n");
597 BIO_printf (bio_err, " encrypt PEM output with cbc aes\n");
598 #endif
599 #ifndef OPENSSL_NO_CAMELLIA
600 BIO_printf (bio_err, "-camellia128, -camellia192, -camellia256\n");
601 BIO_printf (bio_err, " encrypt PEM output with cbc camellia\n");
602 #endif
603 BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n");
604 BIO_printf (bio_err, "-nosigs don't verify message signature\n");
605 BIO_printf (bio_err, "-noverify don't verify signers certificate\n");
606 BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n");
607 BIO_printf (bio_err, "-nodetach use opaque signing\n");
608 BIO_printf (bio_err, "-noattr don't include any signed attributes\n");
609 BIO_printf (bio_err, "-binary don't translate message to text\n");
610 BIO_printf (bio_err, "-certfile file other certificates file\n");
611 BIO_printf (bio_err, "-certsout file certificate output file\n");
612 BIO_printf (bio_err, "-signer file signer certificate file\n");
613 BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n");
614 BIO_printf (bio_err, "-skeyid use subject key identifier\n");
615 BIO_printf (bio_err, "-in file input file\n");
616 BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
617 BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n");
618 BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n");
619 BIO_printf (bio_err, "-out file output file\n");
620 BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
621 BIO_printf (bio_err, "-content file supply or override content for detached signature\n");
622 BIO_printf (bio_err, "-to addr to address\n");
623 BIO_printf (bio_err, "-from ad from address\n");
624 BIO_printf (bio_err, "-subject s subject\n");
625 BIO_printf (bio_err, "-text include or delete text MIME headers\n");
626 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
627 BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
628 BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
629 BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
630 #ifndef OPENSSL_NO_ENGINE
631 BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n");
632 #endif
633 BIO_printf (bio_err, "-passin arg input file pass phrase source\n");
634 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
635 BIO_printf(bio_err, " load the file (or the files in the directory) into\n");
636 BIO_printf(bio_err, " the random number generator\n");
637 BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n");
638 goto end;
641 #ifndef OPENSSL_NO_ENGINE
642 e = setup_engine(bio_err, engine, 0);
643 #endif
645 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
647 BIO_printf(bio_err, "Error getting password\n");
648 goto end;
651 if (need_rand)
653 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
654 if (inrand != NULL)
655 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
656 app_RAND_load_files(inrand));
659 ret = 2;
661 if (!(operation & SMIME_SIGNERS))
662 flags &= ~CMS_DETACHED;
664 if (operation & SMIME_OP)
666 if (outformat == FORMAT_ASN1)
667 outmode = "wb";
669 else
671 if (flags & CMS_BINARY)
672 outmode = "wb";
675 if (operation & SMIME_IP)
677 if (informat == FORMAT_ASN1)
678 inmode = "rb";
680 else
682 if (flags & CMS_BINARY)
683 inmode = "rb";
686 if (operation == SMIME_ENCRYPT)
688 if (!cipher)
690 #ifndef OPENSSL_NO_DES
691 cipher = EVP_des_ede3_cbc();
692 #else
693 BIO_printf(bio_err, "No cipher selected\n");
694 goto end;
695 #endif
698 if (secret_key && !secret_keyid)
700 BIO_printf(bio_err, "No sectre key id\n");
701 goto end;
704 if (*args)
705 encerts = sk_X509_new_null();
706 while (*args)
708 if (!(cert = load_cert(bio_err,*args,FORMAT_PEM,
709 NULL, e, "recipient certificate file")))
710 goto end;
711 sk_X509_push(encerts, cert);
712 cert = NULL;
713 args++;
717 if (certfile)
719 if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL,
720 e, "certificate file")))
722 ERR_print_errors(bio_err);
723 goto end;
727 if (recipfile && (operation == SMIME_DECRYPT))
729 if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL,
730 e, "recipient certificate file")))
732 ERR_print_errors(bio_err);
733 goto end;
737 if (operation == SMIME_SIGN_RECEIPT)
739 if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM,NULL,
740 e, "receipt signer certificate file")))
742 ERR_print_errors(bio_err);
743 goto end;
747 if (operation == SMIME_DECRYPT)
749 if (!keyfile)
750 keyfile = recipfile;
752 else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT))
754 if (!keyfile)
755 keyfile = signerfile;
757 else keyfile = NULL;
759 if (keyfile)
761 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
762 "signing key file");
763 if (!key)
764 goto end;
767 if (infile)
769 if (!(in = BIO_new_file(infile, inmode)))
771 BIO_printf (bio_err,
772 "Can't open input file %s\n", infile);
773 goto end;
776 else
777 in = BIO_new_fp(stdin, BIO_NOCLOSE);
779 if (operation & SMIME_IP)
781 if (informat == FORMAT_SMIME)
782 cms = SMIME_read_CMS(in, &indata);
783 else if (informat == FORMAT_PEM)
784 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
785 else if (informat == FORMAT_ASN1)
786 cms = d2i_CMS_bio(in, NULL);
787 else
789 BIO_printf(bio_err, "Bad input format for CMS file\n");
790 goto end;
793 if (!cms)
795 BIO_printf(bio_err, "Error reading S/MIME message\n");
796 goto end;
798 if (contfile)
800 BIO_free(indata);
801 if (!(indata = BIO_new_file(contfile, "rb")))
803 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
804 goto end;
807 if (certsoutfile)
809 STACK_OF(X509) *allcerts;
810 allcerts = CMS_get1_certs(cms);
811 if (!save_certs(certsoutfile, allcerts))
813 BIO_printf(bio_err,
814 "Error writing certs to %s\n",
815 certsoutfile);
816 ret = 5;
817 goto end;
819 sk_X509_pop_free(allcerts, X509_free);
823 if (rctfile)
825 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
826 if (!(rctin = BIO_new_file(rctfile, rctmode)))
828 BIO_printf (bio_err,
829 "Can't open receipt file %s\n", rctfile);
830 goto end;
833 if (rctformat == FORMAT_SMIME)
834 rcms = SMIME_read_CMS(rctin, NULL);
835 else if (rctformat == FORMAT_PEM)
836 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
837 else if (rctformat == FORMAT_ASN1)
838 rcms = d2i_CMS_bio(rctin, NULL);
839 else
841 BIO_printf(bio_err, "Bad input format for receipt\n");
842 goto end;
845 if (!rcms)
847 BIO_printf(bio_err, "Error reading receipt\n");
848 goto end;
852 if (outfile)
854 if (!(out = BIO_new_file(outfile, outmode)))
856 BIO_printf (bio_err,
857 "Can't open output file %s\n", outfile);
858 goto end;
861 else
863 out = BIO_new_fp(stdout, BIO_NOCLOSE);
864 #ifdef OPENSSL_SYS_VMS
866 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
867 out = BIO_push(tmpbio, out);
869 #endif
872 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT))
874 if (!(store = setup_verify(bio_err, CAfile, CApath)))
875 goto end;
876 X509_STORE_set_verify_cb_func(store, cms_cb);
877 if (vpm)
878 X509_STORE_set1_param(store, vpm);
882 ret = 3;
884 if (operation == SMIME_DATA_CREATE)
886 cms = CMS_data_create(in, flags);
888 else if (operation == SMIME_DIGEST_CREATE)
890 cms = CMS_digest_create(in, sign_md, flags);
892 else if (operation == SMIME_COMPRESS)
894 cms = CMS_compress(in, -1, flags);
896 else if (operation == SMIME_ENCRYPT)
898 flags |= CMS_PARTIAL;
899 cms = CMS_encrypt(encerts, in, cipher, flags);
900 if (!cms)
901 goto end;
902 if (secret_key)
904 if (!CMS_add0_recipient_key(cms, NID_undef,
905 secret_key, secret_keylen,
906 secret_keyid, secret_keyidlen,
907 NULL, NULL, NULL))
908 goto end;
909 /* NULL these because call absorbs them */
910 secret_key = NULL;
911 secret_keyid = NULL;
913 if (!(flags & CMS_STREAM))
915 if (!CMS_final(cms, in, NULL, flags))
916 goto end;
919 else if (operation == SMIME_ENCRYPTED_ENCRYPT)
921 cms = CMS_EncryptedData_encrypt(in, cipher,
922 secret_key, secret_keylen,
923 flags);
926 else if (operation == SMIME_SIGN_RECEIPT)
928 CMS_ContentInfo *srcms = NULL;
929 STACK_OF(CMS_SignerInfo) *sis;
930 CMS_SignerInfo *si;
931 sis = CMS_get0_SignerInfos(cms);
932 if (!sis)
933 goto end;
934 si = sk_CMS_SignerInfo_value(sis, 0);
935 srcms = CMS_sign_receipt(si, signer, key, other, flags);
936 if (!srcms)
937 goto end;
938 CMS_ContentInfo_free(cms);
939 cms = srcms;
941 else if (operation & SMIME_SIGNERS)
943 int i;
944 /* If detached data content we enable streaming if
945 * S/MIME output format.
947 if (operation == SMIME_SIGN)
950 if (flags & CMS_DETACHED)
952 if (outformat == FORMAT_SMIME)
953 flags |= CMS_STREAM;
955 flags |= CMS_PARTIAL;
956 cms = CMS_sign(NULL, NULL, other, in, flags);
957 if (!cms)
958 goto end;
959 if (econtent_type)
960 CMS_set1_eContentType(cms, econtent_type);
962 if (rr_to)
964 rr = make_receipt_request(rr_to, rr_allorfirst,
965 rr_from);
966 if (!rr)
968 BIO_puts(bio_err,
969 "Signed Receipt Request Creation Error\n");
970 goto end;
974 else
975 flags |= CMS_REUSE_DIGEST;
976 for (i = 0; i < sk_num(sksigners); i++)
978 CMS_SignerInfo *si;
979 signerfile = sk_value(sksigners, i);
980 keyfile = sk_value(skkeys, i);
981 signer = load_cert(bio_err, signerfile,FORMAT_PEM, NULL,
982 e, "signer certificate");
983 if (!signer)
984 goto end;
985 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
986 "signing key file");
987 if (!key)
988 goto end;
989 si = CMS_add1_signer(cms, signer, key, sign_md, flags);
990 if (!si)
991 goto end;
992 if (rr && !CMS_add1_ReceiptRequest(si, rr))
993 goto end;
994 X509_free(signer);
995 signer = NULL;
996 EVP_PKEY_free(key);
997 key = NULL;
999 /* If not streaming or resigning finalize structure */
1000 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM))
1002 if (!CMS_final(cms, in, NULL, flags))
1003 goto end;
1007 if (!cms)
1009 BIO_printf(bio_err, "Error creating CMS structure\n");
1010 goto end;
1013 ret = 4;
1014 if (operation == SMIME_DECRYPT)
1017 if (secret_key)
1019 if (!CMS_decrypt_set1_key(cms,
1020 secret_key, secret_keylen,
1021 secret_keyid, secret_keyidlen))
1023 BIO_puts(bio_err,
1024 "Error decrypting CMS using secret key\n");
1025 goto end;
1029 if (key)
1031 if (!CMS_decrypt_set1_pkey(cms, key, recip))
1033 BIO_puts(bio_err,
1034 "Error decrypting CMS using private key\n");
1035 goto end;
1039 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags))
1041 BIO_printf(bio_err, "Error decrypting CMS structure\n");
1042 goto end;
1045 else if (operation == SMIME_DATAOUT)
1047 if (!CMS_data(cms, out, flags))
1048 goto end;
1050 else if (operation == SMIME_UNCOMPRESS)
1052 if (!CMS_uncompress(cms, indata, out, flags))
1053 goto end;
1055 else if (operation == SMIME_DIGEST_VERIFY)
1057 if (CMS_digest_verify(cms, indata, out, flags) > 0)
1058 BIO_printf(bio_err, "Verification successful\n");
1059 else
1061 BIO_printf(bio_err, "Verification failure\n");
1062 goto end;
1065 else if (operation == SMIME_ENCRYPTED_DECRYPT)
1067 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1068 indata, out, flags))
1069 goto end;
1071 else if (operation == SMIME_VERIFY)
1073 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
1074 BIO_printf(bio_err, "Verification successful\n");
1075 else
1077 BIO_printf(bio_err, "Verification failure\n");
1078 goto end;
1080 if (signerfile)
1082 STACK_OF(X509) *signers;
1083 signers = CMS_get0_signers(cms);
1084 if (!save_certs(signerfile, signers))
1086 BIO_printf(bio_err,
1087 "Error writing signers to %s\n",
1088 signerfile);
1089 ret = 5;
1090 goto end;
1092 sk_X509_free(signers);
1094 if (rr_print)
1095 receipt_request_print(bio_err, cms);
1098 else if (operation == SMIME_VERIFY_RECEIPT)
1100 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
1101 BIO_printf(bio_err, "Verification successful\n");
1102 else
1104 BIO_printf(bio_err, "Verification failure\n");
1105 goto end;
1108 else
1110 if (outformat == FORMAT_SMIME)
1112 if (to)
1113 BIO_printf(out, "To: %s\n", to);
1114 if (from)
1115 BIO_printf(out, "From: %s\n", from);
1116 if (subject)
1117 BIO_printf(out, "Subject: %s\n", subject);
1118 if (operation == SMIME_RESIGN)
1119 ret = SMIME_write_CMS(out, cms, indata, flags);
1120 else
1121 ret = SMIME_write_CMS(out, cms, in, flags);
1123 else if (outformat == FORMAT_PEM)
1124 ret = PEM_write_bio_CMS(out, cms);
1125 else if (outformat == FORMAT_ASN1)
1126 ret = i2d_CMS_bio(out,cms);
1127 else
1129 BIO_printf(bio_err, "Bad output format for CMS file\n");
1130 goto end;
1132 if (ret <= 0)
1134 ret = 6;
1135 goto end;
1138 ret = 0;
1139 end:
1140 if (ret)
1141 ERR_print_errors(bio_err);
1142 if (need_rand)
1143 app_RAND_write_file(NULL, bio_err);
1144 sk_X509_pop_free(encerts, X509_free);
1145 sk_X509_pop_free(other, X509_free);
1146 if (vpm)
1147 X509_VERIFY_PARAM_free(vpm);
1148 if (sksigners)
1149 sk_free(sksigners);
1150 if (skkeys)
1151 sk_free(skkeys);
1152 if (secret_key)
1153 OPENSSL_free(secret_key);
1154 if (secret_keyid)
1155 OPENSSL_free(secret_keyid);
1156 if (econtent_type)
1157 ASN1_OBJECT_free(econtent_type);
1158 if (rr)
1159 CMS_ReceiptRequest_free(rr);
1160 if (rr_to)
1161 sk_free(rr_to);
1162 if (rr_from)
1163 sk_free(rr_from);
1164 X509_STORE_free(store);
1165 X509_free(cert);
1166 X509_free(recip);
1167 X509_free(signer);
1168 EVP_PKEY_free(key);
1169 CMS_ContentInfo_free(cms);
1170 CMS_ContentInfo_free(rcms);
1171 BIO_free(rctin);
1172 BIO_free(in);
1173 BIO_free(indata);
1174 BIO_free_all(out);
1175 if (passin) OPENSSL_free(passin);
1176 return (ret);
1179 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1181 int i;
1182 BIO *tmp;
1183 if (!signerfile)
1184 return 1;
1185 tmp = BIO_new_file(signerfile, "w");
1186 if (!tmp) return 0;
1187 for(i = 0; i < sk_X509_num(signers); i++)
1188 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1189 BIO_free(tmp);
1190 return 1;
1194 /* Minimal callback just to output policy info (if any) */
1196 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1198 int error;
1200 error = X509_STORE_CTX_get_error(ctx);
1202 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1203 && ((error != X509_V_OK) || (ok != 2)))
1204 return ok;
1206 policies_print(NULL, ctx);
1208 return ok;
1212 static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
1214 STACK_OF(GENERAL_NAME) *gens;
1215 GENERAL_NAME *gen;
1216 int i, j;
1217 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++)
1219 gens = sk_GENERAL_NAMES_value(gns, i);
1220 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++)
1222 gen = sk_GENERAL_NAME_value(gens, j);
1223 BIO_puts(out, " ");
1224 GENERAL_NAME_print(out, gen);
1225 BIO_puts(out, "\n");
1228 return;
1231 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
1233 STACK_OF(CMS_SignerInfo) *sis;
1234 CMS_SignerInfo *si;
1235 CMS_ReceiptRequest *rr;
1236 int allorfirst;
1237 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1238 ASN1_STRING *scid;
1239 int i, rv;
1240 sis = CMS_get0_SignerInfos(cms);
1241 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++)
1243 si = sk_CMS_SignerInfo_value(sis, i);
1244 rv = CMS_get1_ReceiptRequest(si, &rr);
1245 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1246 if (rv == 0)
1247 BIO_puts(bio_err, " No Receipt Request\n");
1248 else if (rv < 0)
1250 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1251 ERR_print_errors(bio_err);
1253 else
1255 char *id;
1256 int idlen;
1257 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1258 &rlist, &rto);
1259 BIO_puts(out, " Signed Content ID:\n");
1260 idlen = ASN1_STRING_length(scid);
1261 id = (char *)ASN1_STRING_data(scid);
1262 BIO_dump_indent(out, id, idlen, 4);
1263 BIO_puts(out, " Receipts From");
1264 if (rlist)
1266 BIO_puts(out, " List:\n");
1267 gnames_stack_print(out, rlist);
1269 else if (allorfirst == 1)
1270 BIO_puts(out, ": First Tier\n");
1271 else if (allorfirst == 0)
1272 BIO_puts(out, ": All\n");
1273 else
1274 BIO_printf(out, " Unknown (%d)\n", allorfirst);
1275 BIO_puts(out, " Receipts To:\n");
1276 gnames_stack_print(out, rto);
1278 if (rr)
1279 CMS_ReceiptRequest_free(rr);
1283 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK *ns)
1285 int i;
1286 STACK_OF(GENERAL_NAMES) *ret;
1287 GENERAL_NAMES *gens = NULL;
1288 GENERAL_NAME *gen = NULL;
1289 ret = sk_GENERAL_NAMES_new_null();
1290 if (!ret)
1291 goto err;
1292 for (i = 0; i < sk_num(ns); i++)
1294 CONF_VALUE cnf;
1295 cnf.name = "email";
1296 cnf.value = sk_value(ns, i);
1297 gen = v2i_GENERAL_NAME(NULL, NULL, &cnf);
1298 if (!gen)
1299 goto err;
1300 gens = GENERAL_NAMES_new();
1301 if (!gens)
1302 goto err;
1303 if (!sk_GENERAL_NAME_push(gens, gen))
1304 goto err;
1305 gen = NULL;
1306 if (!sk_GENERAL_NAMES_push(ret, gens))
1307 goto err;
1308 gens = NULL;
1311 return ret;
1313 err:
1314 if (ret)
1315 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1316 if (gens)
1317 GENERAL_NAMES_free(gens);
1318 if (gen)
1319 GENERAL_NAME_free(gen);
1320 return NULL;
1324 static CMS_ReceiptRequest *make_receipt_request(STACK *rr_to, int rr_allorfirst,
1325 STACK *rr_from)
1327 STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1328 CMS_ReceiptRequest *rr;
1329 rct_to = make_names_stack(rr_to);
1330 if (!rct_to)
1331 goto err;
1332 if (rr_from)
1334 rct_from = make_names_stack(rr_from);
1335 if (!rct_from)
1336 goto err;
1338 else
1339 rct_from = NULL;
1340 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
1341 rct_to);
1342 return rr;
1343 err:
1344 return NULL;
1347 #endif