1 /* $OpenBSD: cms.c,v 1.5 2015/10/17 07:51:10 semarie Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
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
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
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
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 */
61 #ifndef OPENSSL_NO_CMS
63 #include <openssl/cms.h>
64 #include <openssl/crypto.h>
65 #include <openssl/err.h>
66 #include <openssl/pem.h>
67 #include <openssl/x509_vfy.h>
68 #include <openssl/x509v3.h>
70 static int save_certs(char *signerfile, STACK_OF(X509) * signers);
71 static int cms_cb(int ok, X509_STORE_CTX * ctx);
72 static void receipt_request_print(BIO * out, CMS_ContentInfo * cms);
73 static CMS_ReceiptRequest * make_receipt_request(
74 STACK_OF(OPENSSL_STRING) * rr_to, int rr_allorfirst,
75 STACK_OF(OPENSSL_STRING) * rr_from);
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)
100 cms_main(int argc, char **argv)
105 const char *inmode = "r", *outmode = "w";
106 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
107 char *signerfile = NULL, *recipfile = NULL;
108 STACK_OF(OPENSSL_STRING) * 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;
119 int flags = CMS_DETACHED, noout = 0, print = 0;
120 int verify_retcode = 0;
121 int rr_print = 0, rr_allorfirst = -1;
122 STACK_OF(OPENSSL_STRING) * rr_to = NULL, *rr_from = NULL;
123 CMS_ReceiptRequest *rr = NULL;
124 char *to = NULL, *from = NULL, *subject = NULL;
125 char *CAfile = NULL, *CApath = NULL;
126 char *passargin = NULL, *passin = NULL;
127 const EVP_MD *sign_md = NULL;
128 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
129 int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
130 unsigned char *secret_key = NULL, *secret_keyid = NULL;
131 unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
132 size_t secret_keylen = 0, secret_keyidlen = 0;
134 ASN1_OBJECT *econtent_type = NULL;
136 X509_VERIFY_PARAM *vpm = NULL;
138 if (single_execution) {
139 if (pledge("stdio rpath wpath cpath tty", NULL) == -1) {
148 while (!badarg && *args && *args[0] == '-') {
149 if (!strcmp(*args, "-encrypt"))
150 operation = SMIME_ENCRYPT;
151 else if (!strcmp(*args, "-decrypt"))
152 operation = SMIME_DECRYPT;
153 else if (!strcmp(*args, "-sign"))
154 operation = SMIME_SIGN;
155 else if (!strcmp(*args, "-sign_receipt"))
156 operation = SMIME_SIGN_RECEIPT;
157 else if (!strcmp(*args, "-resign"))
158 operation = SMIME_RESIGN;
159 else if (!strcmp(*args, "-verify"))
160 operation = SMIME_VERIFY;
161 else if (!strcmp(*args, "-verify_retcode"))
163 else if (!strcmp(*args, "-verify_receipt")) {
164 operation = SMIME_VERIFY_RECEIPT;
169 } else if (!strcmp(*args, "-cmsout"))
170 operation = SMIME_CMSOUT;
171 else if (!strcmp(*args, "-data_out"))
172 operation = SMIME_DATAOUT;
173 else if (!strcmp(*args, "-data_create"))
174 operation = SMIME_DATA_CREATE;
175 else if (!strcmp(*args, "-digest_verify"))
176 operation = SMIME_DIGEST_VERIFY;
177 else if (!strcmp(*args, "-digest_create"))
178 operation = SMIME_DIGEST_CREATE;
179 else if (!strcmp(*args, "-compress"))
180 operation = SMIME_COMPRESS;
181 else if (!strcmp(*args, "-uncompress"))
182 operation = SMIME_UNCOMPRESS;
183 else if (!strcmp(*args, "-EncryptedData_decrypt"))
184 operation = SMIME_ENCRYPTED_DECRYPT;
185 else if (!strcmp(*args, "-EncryptedData_encrypt"))
186 operation = SMIME_ENCRYPTED_ENCRYPT;
187 #ifndef OPENSSL_NO_DES
188 else if (!strcmp(*args, "-des3"))
189 cipher = EVP_des_ede3_cbc();
190 else if (!strcmp(*args, "-des"))
191 cipher = EVP_des_cbc();
193 #ifndef OPENSSL_NO_RC2
194 else if (!strcmp(*args, "-rc2-40"))
195 cipher = EVP_rc2_40_cbc();
196 else if (!strcmp(*args, "-rc2-128"))
197 cipher = EVP_rc2_cbc();
198 else if (!strcmp(*args, "-rc2-64"))
199 cipher = EVP_rc2_64_cbc();
201 #ifndef OPENSSL_NO_AES
202 else if (!strcmp(*args, "-aes128"))
203 cipher = EVP_aes_128_cbc();
204 else if (!strcmp(*args, "-aes192"))
205 cipher = EVP_aes_192_cbc();
206 else if (!strcmp(*args, "-aes256"))
207 cipher = EVP_aes_256_cbc();
209 #ifndef OPENSSL_NO_CAMELLIA
210 else if (!strcmp(*args, "-camellia128"))
211 cipher = EVP_camellia_128_cbc();
212 else if (!strcmp(*args, "-camellia192"))
213 cipher = EVP_camellia_192_cbc();
214 else if (!strcmp(*args, "-camellia256"))
215 cipher = EVP_camellia_256_cbc();
217 else if (!strcmp(*args, "-debug_decrypt"))
218 flags |= CMS_DEBUG_DECRYPT;
219 else if (!strcmp(*args, "-text"))
221 else if (!strcmp(*args, "-nointern"))
222 flags |= CMS_NOINTERN;
223 else if (!strcmp(*args, "-noverify") ||
224 !strcmp(*args, "-no_signer_cert_verify"))
225 flags |= CMS_NO_SIGNER_CERT_VERIFY;
226 else if (!strcmp(*args, "-nocerts"))
227 flags |= CMS_NOCERTS;
228 else if (!strcmp(*args, "-noattr"))
230 else if (!strcmp(*args, "-nodetach"))
231 flags &= ~CMS_DETACHED;
232 else if (!strcmp(*args, "-nosmimecap"))
233 flags |= CMS_NOSMIMECAP;
234 else if (!strcmp(*args, "-binary"))
236 else if (!strcmp(*args, "-keyid"))
237 flags |= CMS_USE_KEYID;
238 else if (!strcmp(*args, "-nosigs"))
240 else if (!strcmp(*args, "-no_content_verify"))
241 flags |= CMS_NO_CONTENT_VERIFY;
242 else if (!strcmp(*args, "-no_attr_verify"))
243 flags |= CMS_NO_ATTR_VERIFY;
244 else if (!strcmp(*args, "-stream"))
246 else if (!strcmp(*args, "-indef"))
248 else if (!strcmp(*args, "-noindef"))
249 flags &= ~CMS_STREAM;
250 else if (!strcmp(*args, "-nooldmime"))
251 flags |= CMS_NOOLDMIMETYPE;
252 else if (!strcmp(*args, "-crlfeol"))
253 flags |= CMS_CRLFEOL;
254 else if (!strcmp(*args, "-noout"))
256 else if (!strcmp(*args, "-receipt_request_print"))
258 else if (!strcmp(*args, "-receipt_request_all"))
260 else if (!strcmp(*args, "-receipt_request_first"))
262 else if (!strcmp(*args, "-receipt_request_from")) {
267 rr_from = sk_OPENSSL_STRING_new_null();
268 sk_OPENSSL_STRING_push(rr_from, *args);
269 } else if (!strcmp(*args, "-receipt_request_to")) {
274 rr_to = sk_OPENSSL_STRING_new_null();
275 sk_OPENSSL_STRING_push(rr_to, *args);
276 } else if (!strcmp(*args, "-print")) {
279 } else if (!strcmp(*args, "-secretkey")) {
284 secret_key = string_to_hex(*args, <mp);
286 BIO_printf(bio_err, "Invalid key %s\n", *args);
289 secret_keylen = (size_t) ltmp;
290 } else if (!strcmp(*args, "-secretkeyid")) {
295 secret_keyid = string_to_hex(*args, <mp);
297 BIO_printf(bio_err, "Invalid id %s\n", *args);
300 secret_keyidlen = (size_t) ltmp;
301 } else if (!strcmp(*args, "-pwri_password")) {
305 pwri_pass = (unsigned char *) *args;
306 } else if (!strcmp(*args, "-econtent_type")) {
310 econtent_type = OBJ_txt2obj(*args, 0);
311 if (!econtent_type) {
312 BIO_printf(bio_err, "Invalid OID %s\n", *args);
316 else if (!strcmp(*args, "-passin")) {
320 } else if (!strcmp(*args, "-to")) {
324 } else if (!strcmp(*args, "-from")) {
328 } else if (!strcmp(*args, "-subject")) {
332 } else if (!strcmp(*args, "-signer")) {
335 /* If previous -signer argument add signer to list */
340 sk_OPENSSL_STRING_new_null();
341 sk_OPENSSL_STRING_push(sksigners, signerfile);
343 keyfile = signerfile;
345 skkeys = sk_OPENSSL_STRING_new_null();
346 sk_OPENSSL_STRING_push(skkeys, keyfile);
349 signerfile = *++args;
350 } else if (!strcmp(*args, "-recip")) {
354 } else if (!strcmp(*args, "-certsout")) {
357 certsoutfile = *++args;
358 } else if (!strcmp(*args, "-md")) {
361 sign_md = EVP_get_digestbyname(*++args);
362 if (sign_md == NULL) {
363 BIO_printf(bio_err, "Unknown digest %s\n",
367 } else if (!strcmp(*args, "-inkey")) {
370 /* If previous -inkey arument add signer to list */
374 "Illegal -inkey without -signer\n");
379 sk_OPENSSL_STRING_new_null();
380 sk_OPENSSL_STRING_push(sksigners, signerfile);
383 skkeys = sk_OPENSSL_STRING_new_null();
384 sk_OPENSSL_STRING_push(skkeys, keyfile);
387 } else if (!strcmp(*args, "-keyform")) {
390 keyform = str2fmt(*++args);
391 } else if (!strcmp(*args, "-rctform")) {
394 rctformat = str2fmt(*++args);
395 } else if (!strcmp(*args, "-certfile")) {
399 } else if (!strcmp(*args, "-CAfile")) {
403 } else if (!strcmp(*args, "-CApath")) {
407 } else if (!strcmp(*args, "-in")) {
411 } else if (!strcmp(*args, "-inform")) {
414 informat = str2fmt(*++args);
415 } else if (!strcmp(*args, "-outform")) {
418 outformat = str2fmt(*++args);
419 } else if (!strcmp(*args, "-out")) {
423 } else if (!strcmp(*args, "-content")) {
427 } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
429 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
434 if (((rr_allorfirst != -1) || rr_from) && !rr_to) {
435 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
438 if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
439 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
442 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
443 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
446 if (operation & SMIME_SIGNERS) {
447 if (keyfile && !signerfile) {
448 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
451 /* Check to see if any final signer needs to be appended */
454 sksigners = sk_OPENSSL_STRING_new_null();
455 sk_OPENSSL_STRING_push(sksigners, signerfile);
457 skkeys = sk_OPENSSL_STRING_new_null();
459 keyfile = signerfile;
460 sk_OPENSSL_STRING_push(skkeys, keyfile);
464 "No signer certificate specified\n");
469 } else if (operation == SMIME_DECRYPT) {
470 if (!recipfile && !keyfile && !secret_key && !pwri_pass) {
472 "No recipient certificate or key specified\n");
475 } else if (operation == SMIME_ENCRYPT) {
476 if (!*args && !secret_key && !pwri_pass) {
478 "No recipient(s) certificate(s) specified\n");
481 } else if (!operation)
486 BIO_printf(bio_err, "Usage cms [options] cert.pem ...\n");
487 BIO_printf(bio_err, "where options are\n");
488 BIO_printf(bio_err, "-encrypt encrypt message\n");
489 BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
490 BIO_printf(bio_err, "-sign sign message\n");
491 BIO_printf(bio_err, "-verify verify signed message\n");
492 BIO_printf(bio_err, "-cmsout output CMS structure\n");
493 #ifndef OPENSSL_NO_DES
494 BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
495 BIO_printf(bio_err, "-des encrypt with DES\n");
497 #ifndef OPENSSL_NO_RC2
498 BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
499 BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
500 BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
502 #ifndef OPENSSL_NO_AES
503 BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
504 BIO_printf(bio_err, " encrypt PEM output with cbc aes\n");
506 #ifndef OPENSSL_NO_CAMELLIA
507 BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
508 BIO_printf(bio_err, " encrypt PEM output with cbc camellia\n");
510 BIO_printf(bio_err, "-nointern don't search certificates in message for signer\n");
511 BIO_printf(bio_err, "-nosigs don't verify message signature\n");
512 BIO_printf(bio_err, "-noverify don't verify signers certificate\n");
513 BIO_printf(bio_err, "-nocerts don't include signers certificate when signing\n");
514 BIO_printf(bio_err, "-nodetach use opaque signing\n");
515 BIO_printf(bio_err, "-noattr don't include any signed attributes\n");
516 BIO_printf(bio_err, "-binary don't translate message to text\n");
517 BIO_printf(bio_err, "-certfile file other certificates file\n");
518 BIO_printf(bio_err, "-certsout file certificate output file\n");
519 BIO_printf(bio_err, "-signer file signer certificate file\n");
520 BIO_printf(bio_err, "-recip file recipient certificate file for decryption\n");
521 BIO_printf(bio_err, "-keyid use subject key identifier\n");
522 BIO_printf(bio_err, "-in file input file\n");
523 BIO_printf(bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
524 BIO_printf(bio_err, "-inkey file input private key (if not signer or recipient)\n");
525 BIO_printf(bio_err, "-keyform arg input private key format (PEM)\n");
526 BIO_printf(bio_err, "-out file output file\n");
527 BIO_printf(bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
528 BIO_printf(bio_err, "-content file supply or override content for detached signature\n");
529 BIO_printf(bio_err, "-to addr to address\n");
530 BIO_printf(bio_err, "-from ad from address\n");
531 BIO_printf(bio_err, "-subject s subject\n");
532 BIO_printf(bio_err, "-text include or delete text MIME headers\n");
533 BIO_printf(bio_err, "-CApath dir trusted certificates directory\n");
534 BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
535 BIO_printf(bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
536 BIO_printf(bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
537 BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
538 BIO_printf(bio_err, "cert.pem recipient certificate(s) for encryption\n");
542 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
543 BIO_printf(bio_err, "Error getting password\n");
548 if (!(operation & SMIME_SIGNERS))
549 flags &= ~CMS_DETACHED;
551 if (operation & SMIME_OP) {
552 if (outformat == FORMAT_ASN1)
555 if (flags & CMS_BINARY)
559 if (operation & SMIME_IP) {
560 if (informat == FORMAT_ASN1)
563 if (flags & CMS_BINARY)
567 if (operation == SMIME_ENCRYPT) {
569 #ifndef OPENSSL_NO_DES
570 cipher = EVP_des_ede3_cbc();
572 BIO_printf(bio_err, "No cipher selected\n");
576 if (secret_key && !secret_keyid) {
577 BIO_printf(bio_err, "No secret key id\n");
581 encerts = sk_X509_new_null();
583 if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
584 NULL, e, "recipient certificate file")))
586 sk_X509_push(encerts, cert);
592 if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
593 e, "certificate file"))) {
594 ERR_print_errors(bio_err);
598 if (recipfile && (operation == SMIME_DECRYPT)) {
599 if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
600 e, "recipient certificate file"))) {
601 ERR_print_errors(bio_err);
605 if (operation == SMIME_SIGN_RECEIPT) {
606 if (!(signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
607 e, "receipt signer certificate file"))) {
608 ERR_print_errors(bio_err);
612 if (operation == SMIME_DECRYPT) {
615 } else if ((operation == SMIME_SIGN) ||
616 (operation == SMIME_SIGN_RECEIPT)) {
618 keyfile = signerfile;
623 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
629 if (!(in = BIO_new_file(infile, inmode))) {
631 "Can't open input file %s\n", infile);
635 in = BIO_new_fp(stdin, BIO_NOCLOSE);
637 if (operation & SMIME_IP) {
638 if (informat == FORMAT_SMIME)
639 cms = SMIME_read_CMS(in, &indata);
640 else if (informat == FORMAT_PEM)
641 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
642 else if (informat == FORMAT_ASN1)
643 cms = d2i_CMS_bio(in, NULL);
645 BIO_printf(bio_err, "Bad input format for CMS file\n");
650 BIO_printf(bio_err, "Error reading S/MIME message\n");
655 if (!(indata = BIO_new_file(contfile, "rb"))) {
657 "Can't read content file %s\n", contfile);
662 STACK_OF(X509) * allcerts;
663 allcerts = CMS_get1_certs(cms);
664 if (!save_certs(certsoutfile, allcerts)) {
666 "Error writing certs to %s\n",
671 sk_X509_pop_free(allcerts, X509_free);
675 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
676 if (!(rctin = BIO_new_file(rctfile, rctmode))) {
678 "Can't open receipt file %s\n", rctfile);
681 if (rctformat == FORMAT_SMIME)
682 rcms = SMIME_read_CMS(rctin, NULL);
683 else if (rctformat == FORMAT_PEM)
684 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
685 else if (rctformat == FORMAT_ASN1)
686 rcms = d2i_CMS_bio(rctin, NULL);
688 BIO_printf(bio_err, "Bad input format for receipt\n");
693 BIO_printf(bio_err, "Error reading receipt\n");
698 if (!(out = BIO_new_file(outfile, outmode))) {
700 "Can't open output file %s\n", outfile);
704 out = BIO_new_fp(stdout, BIO_NOCLOSE);
707 if ((operation == SMIME_VERIFY) ||
708 (operation == SMIME_VERIFY_RECEIPT)) {
709 if (!(store = setup_verify(bio_err, CAfile, CApath)))
711 X509_STORE_set_verify_cb(store, cms_cb);
713 X509_STORE_set1_param(store, vpm);
717 if (operation == SMIME_DATA_CREATE) {
718 cms = CMS_data_create(in, flags);
719 } else if (operation == SMIME_DIGEST_CREATE) {
720 cms = CMS_digest_create(in, sign_md, flags);
721 } else if (operation == SMIME_COMPRESS) {
722 cms = CMS_compress(in, -1, flags);
723 } else if (operation == SMIME_ENCRYPT) {
724 flags |= CMS_PARTIAL;
725 cms = CMS_encrypt(encerts, in, cipher, flags);
729 if (!CMS_add0_recipient_key(cms, NID_undef, secret_key,
730 secret_keylen, secret_keyid, secret_keyidlen,
733 /* NULL these because call absorbs them */
738 pwri_tmp = strdup(pwri_pass);
741 if (!CMS_add0_recipient_password(cms, -1, NID_undef,
742 NID_undef, pwri_tmp, -1, NULL))
746 if (!(flags & CMS_STREAM)) {
747 if (!CMS_final(cms, in, NULL, flags))
750 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
751 cms = CMS_EncryptedData_encrypt(in, cipher, secret_key,
752 secret_keylen, flags);
754 } else if (operation == SMIME_SIGN_RECEIPT) {
755 CMS_ContentInfo *srcms = NULL;
756 STACK_OF(CMS_SignerInfo) * sis;
758 sis = CMS_get0_SignerInfos(cms);
761 si = sk_CMS_SignerInfo_value(sis, 0);
762 srcms = CMS_sign_receipt(si, signer, key, other, flags);
765 CMS_ContentInfo_free(cms);
767 } else if (operation & SMIME_SIGNERS) {
770 * If detached data content we enable streaming if S/MIME
773 if (operation == SMIME_SIGN) {
775 if (flags & CMS_DETACHED) {
776 if (outformat == FORMAT_SMIME)
779 flags |= CMS_PARTIAL;
780 cms = CMS_sign(NULL, NULL, other, in, flags);
784 CMS_set1_eContentType(cms, econtent_type);
787 rr = make_receipt_request(rr_to, rr_allorfirst,
791 "Signed Receipt Request Creation Error\n");
796 flags |= CMS_REUSE_DIGEST;
797 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
799 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
800 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
801 signer = load_cert(bio_err, signerfile, FORMAT_PEM,
802 NULL, e, "signer certificate");
805 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
809 si = CMS_add1_signer(cms, signer, key, sign_md, flags);
812 if (rr && !CMS_add1_ReceiptRequest(si, rr))
819 /* If not streaming or resigning finalize structure */
820 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
821 if (!CMS_final(cms, in, NULL, flags))
826 BIO_printf(bio_err, "Error creating CMS structure\n");
830 if (operation == SMIME_DECRYPT) {
831 if (flags & CMS_DEBUG_DECRYPT)
832 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
835 if (!CMS_decrypt_set1_key(cms, secret_key,
836 secret_keylen, secret_keyid, secret_keyidlen)) {
838 "Error decrypting CMS using secret key\n");
843 if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
845 "Error decrypting CMS using private key\n");
850 if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
852 "Error decrypting CMS using password\n");
856 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
857 BIO_printf(bio_err, "Error decrypting CMS structure\n");
860 } else if (operation == SMIME_DATAOUT) {
861 if (!CMS_data(cms, out, flags))
863 } else if (operation == SMIME_UNCOMPRESS) {
864 if (!CMS_uncompress(cms, indata, out, flags))
866 } else if (operation == SMIME_DIGEST_VERIFY) {
867 if (CMS_digest_verify(cms, indata, out, flags) > 0)
868 BIO_printf(bio_err, "Verification successful\n");
870 BIO_printf(bio_err, "Verification failure\n");
873 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
874 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
877 } else if (operation == SMIME_VERIFY) {
878 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
879 BIO_printf(bio_err, "Verification successful\n");
881 BIO_printf(bio_err, "Verification failure\n");
883 ret = verify_err + 32;
887 STACK_OF(X509) * signers;
888 signers = CMS_get0_signers(cms);
889 if (!save_certs(signerfile, signers)) {
891 "Error writing signers to %s\n",
896 sk_X509_free(signers);
899 receipt_request_print(bio_err, cms);
901 } else if (operation == SMIME_VERIFY_RECEIPT) {
902 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
903 BIO_printf(bio_err, "Verification successful\n");
905 BIO_printf(bio_err, "Verification failure\n");
911 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
912 } else if (outformat == FORMAT_SMIME) {
914 BIO_printf(out, "To: %s\n", to);
916 BIO_printf(out, "From: %s\n", from);
918 BIO_printf(out, "Subject: %s\n", subject);
919 if (operation == SMIME_RESIGN)
920 ret = SMIME_write_CMS(out, cms, indata, flags);
922 ret = SMIME_write_CMS(out, cms, in, flags);
923 } else if (outformat == FORMAT_PEM)
924 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
925 else if (outformat == FORMAT_ASN1)
926 ret = i2d_CMS_bio_stream(out, cms, in, flags);
928 BIO_printf(bio_err, "Bad output format for CMS file\n");
940 ERR_print_errors(bio_err);
941 sk_X509_pop_free(encerts, X509_free);
942 sk_X509_pop_free(other, X509_free);
944 X509_VERIFY_PARAM_free(vpm);
946 sk_OPENSSL_STRING_free(sksigners);
948 sk_OPENSSL_STRING_free(skkeys);
953 ASN1_OBJECT_free(econtent_type);
955 CMS_ReceiptRequest_free(rr);
957 sk_OPENSSL_STRING_free(rr_to);
959 sk_OPENSSL_STRING_free(rr_from);
960 X509_STORE_free(store);
965 CMS_ContentInfo_free(cms);
966 CMS_ContentInfo_free(rcms);
976 save_certs(char *signerfile, STACK_OF(X509) * signers)
983 tmp = BIO_new_file(signerfile, "w");
986 for (i = 0; i < sk_X509_num(signers); i++)
987 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
992 /* Minimal callback just to output policy info (if any) */
995 cms_cb(int ok, X509_STORE_CTX * ctx)
999 error = X509_STORE_CTX_get_error(ctx);
1003 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) &&
1004 ((error != X509_V_OK) || (ok != 2)))
1007 policies_print(NULL, ctx);
1013 gnames_stack_print(BIO * out, STACK_OF(GENERAL_NAMES) * gns)
1015 STACK_OF(GENERAL_NAME) * gens;
1019 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1020 gens = sk_GENERAL_NAMES_value(gns, i);
1021 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1022 gen = sk_GENERAL_NAME_value(gens, j);
1024 GENERAL_NAME_print(out, gen);
1025 BIO_puts(out, "\n");
1032 receipt_request_print(BIO * out, CMS_ContentInfo * cms)
1034 STACK_OF(CMS_SignerInfo) * sis;
1036 CMS_ReceiptRequest *rr;
1038 STACK_OF(GENERAL_NAMES) * rto, *rlist;
1042 sis = CMS_get0_SignerInfos(cms);
1043 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1044 si = sk_CMS_SignerInfo_value(sis, i);
1045 rv = CMS_get1_ReceiptRequest(si, &rr);
1046 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1048 BIO_puts(bio_err, " No Receipt Request\n");
1050 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1051 ERR_print_errors(bio_err);
1055 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1057 BIO_puts(out, " Signed Content ID:\n");
1058 idlen = ASN1_STRING_length(scid);
1059 id = (char *) ASN1_STRING_data(scid);
1060 BIO_dump_indent(out, id, idlen, 4);
1061 BIO_puts(out, " Receipts From");
1063 BIO_puts(out, " List:\n");
1064 gnames_stack_print(out, rlist);
1065 } else if (allorfirst == 1)
1066 BIO_puts(out, ": First Tier\n");
1067 else if (allorfirst == 0)
1068 BIO_puts(out, ": All\n");
1070 BIO_printf(out, " Unknown (%d)\n", allorfirst);
1071 BIO_puts(out, " Receipts To:\n");
1072 gnames_stack_print(out, rto);
1075 CMS_ReceiptRequest_free(rr);
1079 static STACK_OF(GENERAL_NAMES) *
1080 make_names_stack(STACK_OF(OPENSSL_STRING) * ns)
1083 STACK_OF(GENERAL_NAMES) * ret;
1084 GENERAL_NAMES *gens = NULL;
1085 GENERAL_NAME *gen = NULL;
1086 ret = sk_GENERAL_NAMES_new_null();
1089 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1090 char *str = sk_OPENSSL_STRING_value(ns, i);
1091 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1094 gens = GENERAL_NAMES_new();
1097 if (!sk_GENERAL_NAME_push(gens, gen))
1100 if (!sk_GENERAL_NAMES_push(ret, gens))
1109 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1111 GENERAL_NAMES_free(gens);
1113 GENERAL_NAME_free(gen);
1118 static CMS_ReceiptRequest *
1119 make_receipt_request(STACK_OF(OPENSSL_STRING) * rr_to, int rr_allorfirst,
1120 STACK_OF(OPENSSL_STRING) * rr_from)
1122 STACK_OF(GENERAL_NAMES) * rct_to, *rct_from;
1123 CMS_ReceiptRequest *rr;
1125 rct_to = make_names_stack(rr_to);
1129 rct_from = make_names_stack(rr_from);
1134 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,