3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
55 /* CMS utility function */
61 #ifndef OPENSSL_NO_CMS
63 # include <openssl/crypto.h>
64 # include <openssl/pem.h>
65 # include <openssl/err.h>
66 # include <openssl/x509_vfy.h>
67 # include <openssl/x509v3.h>
68 # include <openssl/cms.h>
71 # define PROG cms_main
72 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
73 static int cms_cb(int ok, X509_STORE_CTX *ctx);
74 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms);
75 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
76 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
79 # define SMIME_OP 0x10
80 # define SMIME_IP 0x20
81 # define SMIME_SIGNERS 0x40
82 # define SMIME_ENCRYPT (1 | SMIME_OP)
83 # define SMIME_DECRYPT (2 | SMIME_IP)
84 # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
85 # define SMIME_VERIFY (4 | SMIME_IP)
86 # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
87 # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
88 # define SMIME_DATAOUT (7 | SMIME_IP)
89 # define SMIME_DATA_CREATE (8 | SMIME_OP)
90 # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
91 # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
92 # define SMIME_UNCOMPRESS (11 | SMIME_IP)
93 # define SMIME_COMPRESS (12 | SMIME_OP)
94 # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
95 # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
96 # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
97 # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
101 int MAIN(int, char **);
103 int MAIN(int argc, char **argv)
109 const char *inmode = "r", *outmode = "w";
110 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
111 char *signerfile = NULL, *recipfile = NULL;
112 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
113 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
114 char *certsoutfile = NULL;
115 const EVP_CIPHER *cipher = NULL;
116 CMS_ContentInfo *cms = NULL, *rcms = NULL;
117 X509_STORE *store = NULL;
118 X509 *cert = NULL, *recip = NULL, *signer = NULL;
119 EVP_PKEY *key = NULL;
120 STACK_OF(X509) *encerts = NULL, *other = NULL;
121 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
123 int flags = CMS_DETACHED, noout = 0, print = 0;
124 int verify_retcode = 0;
125 int rr_print = 0, rr_allorfirst = -1;
126 STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
127 CMS_ReceiptRequest *rr = NULL;
128 char *to = NULL, *from = NULL, *subject = NULL;
129 char *CAfile = NULL, *CApath = NULL;
130 char *passargin = NULL, *passin = NULL;
133 const EVP_MD *sign_md = NULL;
134 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
135 int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
136 # ifndef OPENSSL_NO_ENGINE
139 unsigned char *secret_key = NULL, *secret_keyid = NULL;
140 unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
141 size_t secret_keylen = 0, secret_keyidlen = 0;
143 ASN1_OBJECT *econtent_type = NULL;
145 X509_VERIFY_PARAM *vpm = NULL;
152 if (bio_err == NULL) {
153 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
154 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
157 if (!load_config(bio_err, NULL))
160 while (!badarg && *args && *args[0] == '-') {
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;
167 else if (!strcmp(*args, "-sign_receipt"))
168 operation = SMIME_SIGN_RECEIPT;
169 else if (!strcmp(*args, "-resign"))
170 operation = SMIME_RESIGN;
171 else if (!strcmp(*args, "-verify"))
172 operation = SMIME_VERIFY;
173 else if (!strcmp(*args, "-verify_retcode"))
175 else if (!strcmp(*args, "-verify_receipt")) {
176 operation = SMIME_VERIFY_RECEIPT;
181 } else if (!strcmp(*args, "-cmsout"))
182 operation = SMIME_CMSOUT;
183 else if (!strcmp(*args, "-data_out"))
184 operation = SMIME_DATAOUT;
185 else if (!strcmp(*args, "-data_create"))
186 operation = SMIME_DATA_CREATE;
187 else if (!strcmp(*args, "-digest_verify"))
188 operation = SMIME_DIGEST_VERIFY;
189 else if (!strcmp(*args, "-digest_create"))
190 operation = SMIME_DIGEST_CREATE;
191 else if (!strcmp(*args, "-compress"))
192 operation = SMIME_COMPRESS;
193 else if (!strcmp(*args, "-uncompress"))
194 operation = SMIME_UNCOMPRESS;
195 else if (!strcmp(*args, "-EncryptedData_decrypt"))
196 operation = SMIME_ENCRYPTED_DECRYPT;
197 else if (!strcmp(*args, "-EncryptedData_encrypt"))
198 operation = SMIME_ENCRYPTED_ENCRYPT;
199 # ifndef OPENSSL_NO_DES
200 else if (!strcmp(*args, "-des3"))
201 cipher = EVP_des_ede3_cbc();
202 else if (!strcmp(*args, "-des"))
203 cipher = EVP_des_cbc();
205 # ifndef OPENSSL_NO_SEED
206 else if (!strcmp(*args, "-seed"))
207 cipher = EVP_seed_cbc();
209 # ifndef OPENSSL_NO_RC2
210 else if (!strcmp(*args, "-rc2-40"))
211 cipher = EVP_rc2_40_cbc();
212 else if (!strcmp(*args, "-rc2-128"))
213 cipher = EVP_rc2_cbc();
214 else if (!strcmp(*args, "-rc2-64"))
215 cipher = EVP_rc2_64_cbc();
217 # ifndef OPENSSL_NO_AES
218 else if (!strcmp(*args, "-aes128"))
219 cipher = EVP_aes_128_cbc();
220 else if (!strcmp(*args, "-aes192"))
221 cipher = EVP_aes_192_cbc();
222 else if (!strcmp(*args, "-aes256"))
223 cipher = EVP_aes_256_cbc();
225 # ifndef OPENSSL_NO_CAMELLIA
226 else if (!strcmp(*args, "-camellia128"))
227 cipher = EVP_camellia_128_cbc();
228 else if (!strcmp(*args, "-camellia192"))
229 cipher = EVP_camellia_192_cbc();
230 else if (!strcmp(*args, "-camellia256"))
231 cipher = EVP_camellia_256_cbc();
233 else if (!strcmp(*args, "-debug_decrypt"))
234 flags |= CMS_DEBUG_DECRYPT;
235 else if (!strcmp(*args, "-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"))
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"))
252 else if (!strcmp(*args, "-keyid"))
253 flags |= CMS_USE_KEYID;
254 else if (!strcmp(*args, "-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"))
262 else if (!strcmp(*args, "-indef"))
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, "-noout"))
272 else if (!strcmp(*args, "-receipt_request_print"))
274 else if (!strcmp(*args, "-receipt_request_all"))
276 else if (!strcmp(*args, "-receipt_request_first"))
278 else if (!strcmp(*args, "-receipt_request_from")) {
283 rr_from = sk_OPENSSL_STRING_new_null();
284 sk_OPENSSL_STRING_push(rr_from, *args);
285 } else if (!strcmp(*args, "-receipt_request_to")) {
290 rr_to = sk_OPENSSL_STRING_new_null();
291 sk_OPENSSL_STRING_push(rr_to, *args);
292 } else if (!strcmp(*args, "-print")) {
295 } else if (!strcmp(*args, "-secretkey")) {
300 secret_key = string_to_hex(*args, <mp);
302 BIO_printf(bio_err, "Invalid key %s\n", *args);
305 secret_keylen = (size_t)ltmp;
306 } else if (!strcmp(*args, "-secretkeyid")) {
311 secret_keyid = string_to_hex(*args, <mp);
313 BIO_printf(bio_err, "Invalid id %s\n", *args);
316 secret_keyidlen = (size_t)ltmp;
317 } else if (!strcmp(*args, "-pwri_password")) {
321 pwri_pass = (unsigned char *)*args;
322 } else if (!strcmp(*args, "-econtent_type")) {
326 econtent_type = OBJ_txt2obj(*args, 0);
327 if (!econtent_type) {
328 BIO_printf(bio_err, "Invalid OID %s\n", *args);
331 } else if (!strcmp(*args, "-rand")) {
338 # ifndef OPENSSL_NO_ENGINE
339 else if (!strcmp(*args, "-engine")) {
345 else if (!strcmp(*args, "-passin")) {
349 } else if (!strcmp(*args, "-to")) {
353 } else if (!strcmp(*args, "-from")) {
357 } else if (!strcmp(*args, "-subject")) {
361 } else if (!strcmp(*args, "-signer")) {
364 /* If previous -signer argument add signer to list */
368 sksigners = sk_OPENSSL_STRING_new_null();
369 sk_OPENSSL_STRING_push(sksigners, signerfile);
371 keyfile = signerfile;
373 skkeys = sk_OPENSSL_STRING_new_null();
374 sk_OPENSSL_STRING_push(skkeys, keyfile);
377 signerfile = *++args;
378 } else if (!strcmp(*args, "-recip")) {
382 } else if (!strcmp(*args, "-certsout")) {
385 certsoutfile = *++args;
386 } else if (!strcmp(*args, "-md")) {
389 sign_md = EVP_get_digestbyname(*++args);
390 if (sign_md == NULL) {
391 BIO_printf(bio_err, "Unknown digest %s\n", *args);
394 } else if (!strcmp(*args, "-inkey")) {
397 /* If previous -inkey arument add signer to list */
400 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
404 sksigners = sk_OPENSSL_STRING_new_null();
405 sk_OPENSSL_STRING_push(sksigners, signerfile);
408 skkeys = sk_OPENSSL_STRING_new_null();
409 sk_OPENSSL_STRING_push(skkeys, keyfile);
412 } else if (!strcmp(*args, "-keyform")) {
415 keyform = str2fmt(*++args);
416 } else if (!strcmp(*args, "-rctform")) {
419 rctformat = str2fmt(*++args);
420 } else if (!strcmp(*args, "-certfile")) {
424 } else if (!strcmp(*args, "-CAfile")) {
428 } else if (!strcmp(*args, "-CApath")) {
432 } else if (!strcmp(*args, "-in")) {
436 } else if (!strcmp(*args, "-inform")) {
439 informat = str2fmt(*++args);
440 } else if (!strcmp(*args, "-outform")) {
443 outformat = str2fmt(*++args);
444 } else if (!strcmp(*args, "-out")) {
448 } else if (!strcmp(*args, "-content")) {
452 } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
454 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
459 if (((rr_allorfirst != -1) || rr_from) && !rr_to) {
460 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
464 if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
465 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
468 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
469 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
473 if (operation & SMIME_SIGNERS) {
474 if (keyfile && !signerfile) {
475 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
478 /* Check to see if any final signer needs to be appended */
481 sksigners = sk_OPENSSL_STRING_new_null();
482 sk_OPENSSL_STRING_push(sksigners, signerfile);
484 skkeys = sk_OPENSSL_STRING_new_null();
486 keyfile = signerfile;
487 sk_OPENSSL_STRING_push(skkeys, keyfile);
490 BIO_printf(bio_err, "No signer certificate specified\n");
498 else if (operation == SMIME_DECRYPT) {
499 if (!recipfile && !keyfile && !secret_key && !pwri_pass) {
501 "No recipient certificate or key specified\n");
504 } else if (operation == SMIME_ENCRYPT) {
505 if (!*args && !secret_key && !pwri_pass) {
506 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
510 } else if (!operation)
515 BIO_printf(bio_err, "Usage cms [options] cert.pem ...\n");
516 BIO_printf(bio_err, "where options are\n");
517 BIO_printf(bio_err, "-encrypt encrypt message\n");
518 BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
519 BIO_printf(bio_err, "-sign sign message\n");
520 BIO_printf(bio_err, "-verify verify signed message\n");
521 BIO_printf(bio_err, "-cmsout output CMS structure\n");
522 # ifndef OPENSSL_NO_DES
523 BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
524 BIO_printf(bio_err, "-des encrypt with DES\n");
526 # ifndef OPENSSL_NO_SEED
527 BIO_printf(bio_err, "-seed encrypt with SEED\n");
529 # ifndef OPENSSL_NO_RC2
530 BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
531 BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
532 BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
534 # ifndef OPENSSL_NO_AES
535 BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
537 " encrypt PEM output with cbc aes\n");
539 # ifndef OPENSSL_NO_CAMELLIA
540 BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
542 " encrypt PEM output with cbc camellia\n");
545 "-nointern don't search certificates in message for signer\n");
547 "-nosigs don't verify message signature\n");
549 "-noverify don't verify signers certificate\n");
551 "-nocerts don't include signers certificate when signing\n");
552 BIO_printf(bio_err, "-nodetach use opaque signing\n");
554 "-noattr don't include any signed attributes\n");
556 "-binary don't translate message to text\n");
557 BIO_printf(bio_err, "-certfile file other certificates file\n");
558 BIO_printf(bio_err, "-certsout file certificate output file\n");
559 BIO_printf(bio_err, "-signer file signer certificate file\n");
561 "-recip file recipient certificate file for decryption\n");
562 BIO_printf(bio_err, "-keyid use subject key identifier\n");
563 BIO_printf(bio_err, "-in file input file\n");
565 "-inform arg input format SMIME (default), PEM or DER\n");
567 "-inkey file input private key (if not signer or recipient)\n");
569 "-keyform arg input private key format (PEM or ENGINE)\n");
570 BIO_printf(bio_err, "-out file output file\n");
572 "-outform arg output format SMIME (default), PEM or DER\n");
574 "-content file supply or override content for detached signature\n");
575 BIO_printf(bio_err, "-to addr to address\n");
576 BIO_printf(bio_err, "-from ad from address\n");
577 BIO_printf(bio_err, "-subject s subject\n");
579 "-text include or delete text MIME headers\n");
581 "-CApath dir trusted certificates directory\n");
582 BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
584 "-no_alt_chains only ever use the first certificate chain found\n");
586 "-crl_check check revocation status of signer's certificate using CRLs\n");
588 "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
589 # ifndef OPENSSL_NO_ENGINE
591 "-engine e use engine e, possibly a hardware device.\n");
593 BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
594 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
595 LIST_SEPARATOR_CHAR);
597 " load the file (or the files in the directory) into\n");
598 BIO_printf(bio_err, " the random number generator\n");
600 "cert.pem recipient certificate(s) for encryption\n");
603 # ifndef OPENSSL_NO_ENGINE
604 e = setup_engine(bio_err, engine, 0);
607 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
608 BIO_printf(bio_err, "Error getting password\n");
613 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
615 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
616 app_RAND_load_files(inrand));
621 if (!(operation & SMIME_SIGNERS))
622 flags &= ~CMS_DETACHED;
624 if (operation & SMIME_OP) {
625 if (outformat == FORMAT_ASN1)
628 if (flags & CMS_BINARY)
632 if (operation & SMIME_IP) {
633 if (informat == FORMAT_ASN1)
636 if (flags & CMS_BINARY)
640 if (operation == SMIME_ENCRYPT) {
642 # ifndef OPENSSL_NO_DES
643 cipher = EVP_des_ede3_cbc();
645 BIO_printf(bio_err, "No cipher selected\n");
650 if (secret_key && !secret_keyid) {
651 BIO_printf(bio_err, "No secret key id\n");
656 encerts = sk_X509_new_null();
658 if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
659 NULL, e, "recipient certificate file")))
661 sk_X509_push(encerts, cert);
668 if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
669 e, "certificate file"))) {
670 ERR_print_errors(bio_err);
675 if (recipfile && (operation == SMIME_DECRYPT)) {
676 if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
677 e, "recipient certificate file"))) {
678 ERR_print_errors(bio_err);
683 if (operation == SMIME_SIGN_RECEIPT) {
684 if (!(signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
685 e, "receipt signer certificate file"))) {
686 ERR_print_errors(bio_err);
691 if (operation == SMIME_DECRYPT) {
694 } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
696 keyfile = signerfile;
701 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
708 if (!(in = BIO_new_file(infile, inmode))) {
709 BIO_printf(bio_err, "Can't open input file %s\n", infile);
713 in = BIO_new_fp(stdin, BIO_NOCLOSE);
715 if (operation & SMIME_IP) {
716 if (informat == FORMAT_SMIME)
717 cms = SMIME_read_CMS(in, &indata);
718 else if (informat == FORMAT_PEM)
719 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
720 else if (informat == FORMAT_ASN1)
721 cms = d2i_CMS_bio(in, NULL);
723 BIO_printf(bio_err, "Bad input format for CMS file\n");
728 BIO_printf(bio_err, "Error reading S/MIME message\n");
733 if (!(indata = BIO_new_file(contfile, "rb"))) {
734 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
739 STACK_OF(X509) *allcerts;
740 allcerts = CMS_get1_certs(cms);
741 if (!save_certs(certsoutfile, allcerts)) {
743 "Error writing certs to %s\n", certsoutfile);
747 sk_X509_pop_free(allcerts, X509_free);
752 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
753 if (!(rctin = BIO_new_file(rctfile, rctmode))) {
754 BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
758 if (rctformat == FORMAT_SMIME)
759 rcms = SMIME_read_CMS(rctin, NULL);
760 else if (rctformat == FORMAT_PEM)
761 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
762 else if (rctformat == FORMAT_ASN1)
763 rcms = d2i_CMS_bio(rctin, NULL);
765 BIO_printf(bio_err, "Bad input format for receipt\n");
770 BIO_printf(bio_err, "Error reading receipt\n");
776 if (!(out = BIO_new_file(outfile, outmode))) {
777 BIO_printf(bio_err, "Can't open output file %s\n", outfile);
781 out = BIO_new_fp(stdout, BIO_NOCLOSE);
782 # ifdef OPENSSL_SYS_VMS
784 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
785 out = BIO_push(tmpbio, out);
790 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
791 if (!(store = setup_verify(bio_err, CAfile, CApath)))
793 X509_STORE_set_verify_cb(store, cms_cb);
795 X509_STORE_set1_param(store, vpm);
800 if (operation == SMIME_DATA_CREATE) {
801 cms = CMS_data_create(in, flags);
802 } else if (operation == SMIME_DIGEST_CREATE) {
803 cms = CMS_digest_create(in, sign_md, flags);
804 } else if (operation == SMIME_COMPRESS) {
805 cms = CMS_compress(in, -1, flags);
806 } else if (operation == SMIME_ENCRYPT) {
807 flags |= CMS_PARTIAL;
808 cms = CMS_encrypt(encerts, in, cipher, flags);
812 if (!CMS_add0_recipient_key(cms, NID_undef,
813 secret_key, secret_keylen,
814 secret_keyid, secret_keyidlen,
817 /* NULL these because call absorbs them */
822 pwri_tmp = (unsigned char *)BUF_strdup((char *)pwri_pass);
825 if (!CMS_add0_recipient_password(cms,
826 -1, NID_undef, NID_undef,
831 if (!(flags & CMS_STREAM)) {
832 if (!CMS_final(cms, in, NULL, flags))
835 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
836 cms = CMS_EncryptedData_encrypt(in, cipher,
837 secret_key, secret_keylen, flags);
839 } else if (operation == SMIME_SIGN_RECEIPT) {
840 CMS_ContentInfo *srcms = NULL;
841 STACK_OF(CMS_SignerInfo) *sis;
843 sis = CMS_get0_SignerInfos(cms);
846 si = sk_CMS_SignerInfo_value(sis, 0);
847 srcms = CMS_sign_receipt(si, signer, key, other, flags);
850 CMS_ContentInfo_free(cms);
852 } else if (operation & SMIME_SIGNERS) {
855 * If detached data content we enable streaming if S/MIME output
858 if (operation == SMIME_SIGN) {
860 if (flags & CMS_DETACHED) {
861 if (outformat == FORMAT_SMIME)
864 flags |= CMS_PARTIAL;
865 cms = CMS_sign(NULL, NULL, other, in, flags);
869 CMS_set1_eContentType(cms, econtent_type);
872 rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
875 "Signed Receipt Request Creation Error\n");
880 flags |= CMS_REUSE_DIGEST;
881 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
883 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
884 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
885 signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
886 e, "signer certificate");
889 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
893 si = CMS_add1_signer(cms, signer, key, sign_md, flags);
896 if (rr && !CMS_add1_ReceiptRequest(si, rr))
903 /* If not streaming or resigning finalize structure */
904 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
905 if (!CMS_final(cms, in, NULL, flags))
911 BIO_printf(bio_err, "Error creating CMS structure\n");
916 if (operation == SMIME_DECRYPT) {
917 if (flags & CMS_DEBUG_DECRYPT)
918 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
921 if (!CMS_decrypt_set1_key(cms,
922 secret_key, secret_keylen,
923 secret_keyid, secret_keyidlen)) {
924 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
930 if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
931 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
937 if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
938 BIO_puts(bio_err, "Error decrypting CMS using password\n");
943 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
944 BIO_printf(bio_err, "Error decrypting CMS structure\n");
947 } else if (operation == SMIME_DATAOUT) {
948 if (!CMS_data(cms, out, flags))
950 } else if (operation == SMIME_UNCOMPRESS) {
951 if (!CMS_uncompress(cms, indata, out, flags))
953 } else if (operation == SMIME_DIGEST_VERIFY) {
954 if (CMS_digest_verify(cms, indata, out, flags) > 0)
955 BIO_printf(bio_err, "Verification successful\n");
957 BIO_printf(bio_err, "Verification failure\n");
960 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
961 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
964 } else if (operation == SMIME_VERIFY) {
965 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
966 BIO_printf(bio_err, "Verification successful\n");
968 BIO_printf(bio_err, "Verification failure\n");
970 ret = verify_err + 32;
974 STACK_OF(X509) *signers;
975 signers = CMS_get0_signers(cms);
976 if (!save_certs(signerfile, signers)) {
978 "Error writing signers to %s\n", signerfile);
982 sk_X509_free(signers);
985 receipt_request_print(bio_err, cms);
987 } else if (operation == SMIME_VERIFY_RECEIPT) {
988 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
989 BIO_printf(bio_err, "Verification successful\n");
991 BIO_printf(bio_err, "Verification failure\n");
997 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
998 } else if (outformat == FORMAT_SMIME) {
1000 BIO_printf(out, "To: %s\n", to);
1002 BIO_printf(out, "From: %s\n", from);
1004 BIO_printf(out, "Subject: %s\n", subject);
1005 if (operation == SMIME_RESIGN)
1006 ret = SMIME_write_CMS(out, cms, indata, flags);
1008 ret = SMIME_write_CMS(out, cms, in, flags);
1009 } else if (outformat == FORMAT_PEM)
1010 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1011 else if (outformat == FORMAT_ASN1)
1012 ret = i2d_CMS_bio_stream(out, cms, in, flags);
1014 BIO_printf(bio_err, "Bad output format for CMS file\n");
1025 ERR_print_errors(bio_err);
1027 app_RAND_write_file(NULL, bio_err);
1028 sk_X509_pop_free(encerts, X509_free);
1029 sk_X509_pop_free(other, X509_free);
1031 X509_VERIFY_PARAM_free(vpm);
1033 sk_OPENSSL_STRING_free(sksigners);
1035 sk_OPENSSL_STRING_free(skkeys);
1037 OPENSSL_free(secret_key);
1039 OPENSSL_free(secret_keyid);
1041 OPENSSL_free(pwri_tmp);
1043 ASN1_OBJECT_free(econtent_type);
1045 CMS_ReceiptRequest_free(rr);
1047 sk_OPENSSL_STRING_free(rr_to);
1049 sk_OPENSSL_STRING_free(rr_from);
1050 X509_STORE_free(store);
1055 CMS_ContentInfo_free(cms);
1056 CMS_ContentInfo_free(rcms);
1062 OPENSSL_free(passin);
1066 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1072 tmp = BIO_new_file(signerfile, "w");
1075 for (i = 0; i < sk_X509_num(signers); i++)
1076 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1081 /* Minimal callback just to output policy info (if any) */
1083 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1087 error = X509_STORE_CTX_get_error(ctx);
1091 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1092 && ((error != X509_V_OK) || (ok != 2)))
1095 policies_print(NULL, ctx);
1101 static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
1103 STACK_OF(GENERAL_NAME) *gens;
1106 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1107 gens = sk_GENERAL_NAMES_value(gns, i);
1108 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1109 gen = sk_GENERAL_NAME_value(gens, j);
1111 GENERAL_NAME_print(out, gen);
1112 BIO_puts(out, "\n");
1118 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
1120 STACK_OF(CMS_SignerInfo) *sis;
1122 CMS_ReceiptRequest *rr;
1124 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1127 sis = CMS_get0_SignerInfos(cms);
1128 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1129 si = sk_CMS_SignerInfo_value(sis, i);
1130 rv = CMS_get1_ReceiptRequest(si, &rr);
1131 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1133 BIO_puts(bio_err, " No Receipt Request\n");
1135 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1136 ERR_print_errors(bio_err);
1140 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1142 BIO_puts(out, " Signed Content ID:\n");
1143 idlen = ASN1_STRING_length(scid);
1144 id = (char *)ASN1_STRING_data(scid);
1145 BIO_dump_indent(out, id, idlen, 4);
1146 BIO_puts(out, " Receipts From");
1148 BIO_puts(out, " List:\n");
1149 gnames_stack_print(out, rlist);
1150 } else if (allorfirst == 1)
1151 BIO_puts(out, ": First Tier\n");
1152 else if (allorfirst == 0)
1153 BIO_puts(out, ": All\n");
1155 BIO_printf(out, " Unknown (%d)\n", allorfirst);
1156 BIO_puts(out, " Receipts To:\n");
1157 gnames_stack_print(out, rto);
1160 CMS_ReceiptRequest_free(rr);
1164 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1167 STACK_OF(GENERAL_NAMES) *ret;
1168 GENERAL_NAMES *gens = NULL;
1169 GENERAL_NAME *gen = NULL;
1170 ret = sk_GENERAL_NAMES_new_null();
1173 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1174 char *str = sk_OPENSSL_STRING_value(ns, i);
1175 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1178 gens = GENERAL_NAMES_new();
1181 if (!sk_GENERAL_NAME_push(gens, gen))
1184 if (!sk_GENERAL_NAMES_push(ret, gens))
1193 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1195 GENERAL_NAMES_free(gens);
1197 GENERAL_NAME_free(gen);
1201 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
1202 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
1205 STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1206 CMS_ReceiptRequest *rr;
1207 rct_to = make_names_stack(rr_to);
1211 rct_from = make_names_stack(rr_from);
1216 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,