summaryrefslogtreecommitdiff
path: root/src/usr.bin/openssl/cms.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/usr.bin/openssl/cms.c')
-rw-r--r--src/usr.bin/openssl/cms.c1154
1 files changed, 1154 insertions, 0 deletions
diff --git a/src/usr.bin/openssl/cms.c b/src/usr.bin/openssl/cms.c
new file mode 100644
index 0000000000..21d27adad1
--- /dev/null
+++ b/src/usr.bin/openssl/cms.c
@@ -0,0 +1,1154 @@
1/* $OpenBSD: cms.c,v 1.1 2014/08/26 17:47:24 jsing Exp $ */
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
59#include "apps.h"
60
61#ifndef OPENSSL_NO_CMS
62
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>
69
70static int save_certs(char *signerfile, STACK_OF(X509) * signers);
71static int cms_cb(int ok, X509_STORE_CTX * ctx);
72static void receipt_request_print(BIO * out, CMS_ContentInfo * cms);
73static CMS_ReceiptRequest * make_receipt_request(
74 STACK_OF(OPENSSL_STRING) * rr_to, int rr_allorfirst,
75 STACK_OF(OPENSSL_STRING) * rr_from);
76
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)
96
97int verify_err = 0;
98
99int cms_main(int, char **);
100
101int
102cms_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";
109 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
110 char *signerfile = NULL, *recipfile = NULL;
111 STACK_OF(OPENSSL_STRING) * sksigners = NULL, *skkeys = NULL;
112 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
113 char *certsoutfile = NULL;
114 const EVP_CIPHER *cipher = NULL;
115 CMS_ContentInfo *cms = NULL, *rcms = NULL;
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;
120 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
121 int badarg = 0;
122 int flags = CMS_DETACHED, noout = 0, print = 0;
123 int verify_retcode = 0;
124 int rr_print = 0, rr_allorfirst = -1;
125 STACK_OF(OPENSSL_STRING) * rr_to = NULL, *rr_from = NULL;
126 CMS_ReceiptRequest *rr = NULL;
127 char *to = NULL, *from = NULL, *subject = NULL;
128 char *CAfile = NULL, *CApath = NULL;
129 char *passargin = NULL, *passin = NULL;
130 const EVP_MD *sign_md = NULL;
131 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
132 int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
133#ifndef OPENSSL_NO_ENGINE
134 char *engine = NULL;
135#endif
136 unsigned char *secret_key = NULL, *secret_keyid = NULL;
137 unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
138 size_t secret_keylen = 0, secret_keyidlen = 0;
139
140 ASN1_OBJECT *econtent_type = NULL;
141
142 X509_VERIFY_PARAM *vpm = NULL;
143
144 args = argv + 1;
145 ret = 1;
146
147 while (!badarg && *args && *args[0] == '-') {
148 if (!strcmp(*args, "-encrypt"))
149 operation = SMIME_ENCRYPT;
150 else if (!strcmp(*args, "-decrypt"))
151 operation = SMIME_DECRYPT;
152 else if (!strcmp(*args, "-sign"))
153 operation = SMIME_SIGN;
154 else if (!strcmp(*args, "-sign_receipt"))
155 operation = SMIME_SIGN_RECEIPT;
156 else if (!strcmp(*args, "-resign"))
157 operation = SMIME_RESIGN;
158 else if (!strcmp(*args, "-verify"))
159 operation = SMIME_VERIFY;
160 else if (!strcmp(*args, "-verify_retcode"))
161 verify_retcode = 1;
162 else if (!strcmp(*args, "-verify_receipt")) {
163 operation = SMIME_VERIFY_RECEIPT;
164 if (!args[1])
165 goto argerr;
166 args++;
167 rctfile = *args;
168 } else if (!strcmp(*args, "-cmsout"))
169 operation = SMIME_CMSOUT;
170 else if (!strcmp(*args, "-data_out"))
171 operation = SMIME_DATAOUT;
172 else if (!strcmp(*args, "-data_create"))
173 operation = SMIME_DATA_CREATE;
174 else if (!strcmp(*args, "-digest_verify"))
175 operation = SMIME_DIGEST_VERIFY;
176 else if (!strcmp(*args, "-digest_create"))
177 operation = SMIME_DIGEST_CREATE;
178 else if (!strcmp(*args, "-compress"))
179 operation = SMIME_COMPRESS;
180 else if (!strcmp(*args, "-uncompress"))
181 operation = SMIME_UNCOMPRESS;
182 else if (!strcmp(*args, "-EncryptedData_decrypt"))
183 operation = SMIME_ENCRYPTED_DECRYPT;
184 else if (!strcmp(*args, "-EncryptedData_encrypt"))
185 operation = SMIME_ENCRYPTED_ENCRYPT;
186#ifndef OPENSSL_NO_DES
187 else if (!strcmp(*args, "-des3"))
188 cipher = EVP_des_ede3_cbc();
189 else if (!strcmp(*args, "-des"))
190 cipher = EVP_des_cbc();
191#endif
192#ifndef OPENSSL_NO_RC2
193 else if (!strcmp(*args, "-rc2-40"))
194 cipher = EVP_rc2_40_cbc();
195 else if (!strcmp(*args, "-rc2-128"))
196 cipher = EVP_rc2_cbc();
197 else if (!strcmp(*args, "-rc2-64"))
198 cipher = EVP_rc2_64_cbc();
199#endif
200#ifndef OPENSSL_NO_AES
201 else if (!strcmp(*args, "-aes128"))
202 cipher = EVP_aes_128_cbc();
203 else if (!strcmp(*args, "-aes192"))
204 cipher = EVP_aes_192_cbc();
205 else if (!strcmp(*args, "-aes256"))
206 cipher = EVP_aes_256_cbc();
207#endif
208#ifndef OPENSSL_NO_CAMELLIA
209 else if (!strcmp(*args, "-camellia128"))
210 cipher = EVP_camellia_128_cbc();
211 else if (!strcmp(*args, "-camellia192"))
212 cipher = EVP_camellia_192_cbc();
213 else if (!strcmp(*args, "-camellia256"))
214 cipher = EVP_camellia_256_cbc();
215#endif
216 else if (!strcmp(*args, "-debug_decrypt"))
217 flags |= CMS_DEBUG_DECRYPT;
218 else if (!strcmp(*args, "-text"))
219 flags |= CMS_TEXT;
220 else if (!strcmp(*args, "-nointern"))
221 flags |= CMS_NOINTERN;
222 else if (!strcmp(*args, "-noverify") ||
223 !strcmp(*args, "-no_signer_cert_verify"))
224 flags |= CMS_NO_SIGNER_CERT_VERIFY;
225 else if (!strcmp(*args, "-nocerts"))
226 flags |= CMS_NOCERTS;
227 else if (!strcmp(*args, "-noattr"))
228 flags |= CMS_NOATTR;
229 else if (!strcmp(*args, "-nodetach"))
230 flags &= ~CMS_DETACHED;
231 else if (!strcmp(*args, "-nosmimecap"))
232 flags |= CMS_NOSMIMECAP;
233 else if (!strcmp(*args, "-binary"))
234 flags |= CMS_BINARY;
235 else if (!strcmp(*args, "-keyid"))
236 flags |= CMS_USE_KEYID;
237 else if (!strcmp(*args, "-nosigs"))
238 flags |= CMS_NOSIGS;
239 else if (!strcmp(*args, "-no_content_verify"))
240 flags |= CMS_NO_CONTENT_VERIFY;
241 else if (!strcmp(*args, "-no_attr_verify"))
242 flags |= CMS_NO_ATTR_VERIFY;
243 else if (!strcmp(*args, "-stream"))
244 flags |= CMS_STREAM;
245 else if (!strcmp(*args, "-indef"))
246 flags |= CMS_STREAM;
247 else if (!strcmp(*args, "-noindef"))
248 flags &= ~CMS_STREAM;
249 else if (!strcmp(*args, "-nooldmime"))
250 flags |= CMS_NOOLDMIMETYPE;
251 else if (!strcmp(*args, "-crlfeol"))
252 flags |= CMS_CRLFEOL;
253 else if (!strcmp(*args, "-noout"))
254 noout = 1;
255 else if (!strcmp(*args, "-receipt_request_print"))
256 rr_print = 1;
257 else if (!strcmp(*args, "-receipt_request_all"))
258 rr_allorfirst = 0;
259 else if (!strcmp(*args, "-receipt_request_first"))
260 rr_allorfirst = 1;
261 else if (!strcmp(*args, "-receipt_request_from")) {
262 if (!args[1])
263 goto argerr;
264 args++;
265 if (!rr_from)
266 rr_from = sk_OPENSSL_STRING_new_null();
267 sk_OPENSSL_STRING_push(rr_from, *args);
268 } else if (!strcmp(*args, "-receipt_request_to")) {
269 if (!args[1])
270 goto argerr;
271 args++;
272 if (!rr_to)
273 rr_to = sk_OPENSSL_STRING_new_null();
274 sk_OPENSSL_STRING_push(rr_to, *args);
275 } else if (!strcmp(*args, "-print")) {
276 noout = 1;
277 print = 1;
278 } else if (!strcmp(*args, "-secretkey")) {
279 long ltmp;
280 if (!args[1])
281 goto argerr;
282 args++;
283 secret_key = string_to_hex(*args, &ltmp);
284 if (!secret_key) {
285 BIO_printf(bio_err, "Invalid key %s\n", *args);
286 goto argerr;
287 }
288 secret_keylen = (size_t) ltmp;
289 } else if (!strcmp(*args, "-secretkeyid")) {
290 long ltmp;
291 if (!args[1])
292 goto argerr;
293 args++;
294 secret_keyid = string_to_hex(*args, &ltmp);
295 if (!secret_keyid) {
296 BIO_printf(bio_err, "Invalid id %s\n", *args);
297 goto argerr;
298 }
299 secret_keyidlen = (size_t) ltmp;
300 } else if (!strcmp(*args, "-pwri_password")) {
301 if (!args[1])
302 goto argerr;
303 args++;
304 pwri_pass = (unsigned char *) *args;
305 } else if (!strcmp(*args, "-econtent_type")) {
306 if (!args[1])
307 goto argerr;
308 args++;
309 econtent_type = OBJ_txt2obj(*args, 0);
310 if (!econtent_type) {
311 BIO_printf(bio_err, "Invalid OID %s\n", *args);
312 goto argerr;
313 }
314 }
315#ifndef OPENSSL_NO_ENGINE
316 else if (!strcmp(*args, "-engine")) {
317 if (!args[1])
318 goto argerr;
319 engine = *++args;
320 }
321#endif
322 else if (!strcmp(*args, "-passin")) {
323 if (!args[1])
324 goto argerr;
325 passargin = *++args;
326 } else if (!strcmp(*args, "-to")) {
327 if (!args[1])
328 goto argerr;
329 to = *++args;
330 } else if (!strcmp(*args, "-from")) {
331 if (!args[1])
332 goto argerr;
333 from = *++args;
334 } else if (!strcmp(*args, "-subject")) {
335 if (!args[1])
336 goto argerr;
337 subject = *++args;
338 } else if (!strcmp(*args, "-signer")) {
339 if (!args[1])
340 goto argerr;
341 /* If previous -signer argument add signer to list */
342
343 if (signerfile) {
344 if (!sksigners)
345 sksigners =
346 sk_OPENSSL_STRING_new_null();
347 sk_OPENSSL_STRING_push(sksigners, signerfile);
348 if (!keyfile)
349 keyfile = signerfile;
350 if (!skkeys)
351 skkeys = sk_OPENSSL_STRING_new_null();
352 sk_OPENSSL_STRING_push(skkeys, keyfile);
353 keyfile = NULL;
354 }
355 signerfile = *++args;
356 } else if (!strcmp(*args, "-recip")) {
357 if (!args[1])
358 goto argerr;
359 recipfile = *++args;
360 } else if (!strcmp(*args, "-certsout")) {
361 if (!args[1])
362 goto argerr;
363 certsoutfile = *++args;
364 } else if (!strcmp(*args, "-md")) {
365 if (!args[1])
366 goto argerr;
367 sign_md = EVP_get_digestbyname(*++args);
368 if (sign_md == NULL) {
369 BIO_printf(bio_err, "Unknown digest %s\n",
370 *args);
371 goto argerr;
372 }
373 } else if (!strcmp(*args, "-inkey")) {
374 if (!args[1])
375 goto argerr;
376 /* If previous -inkey arument add signer to list */
377 if (keyfile) {
378 if (!signerfile) {
379 BIO_puts(bio_err,
380 "Illegal -inkey without -signer\n");
381 goto argerr;
382 }
383 if (!sksigners)
384 sksigners =
385 sk_OPENSSL_STRING_new_null();
386 sk_OPENSSL_STRING_push(sksigners, signerfile);
387 signerfile = NULL;
388 if (!skkeys)
389 skkeys = sk_OPENSSL_STRING_new_null();
390 sk_OPENSSL_STRING_push(skkeys, keyfile);
391 }
392 keyfile = *++args;
393 } else if (!strcmp(*args, "-keyform")) {
394 if (!args[1])
395 goto argerr;
396 keyform = str2fmt(*++args);
397 } else if (!strcmp(*args, "-rctform")) {
398 if (!args[1])
399 goto argerr;
400 rctformat = str2fmt(*++args);
401 } else if (!strcmp(*args, "-certfile")) {
402 if (!args[1])
403 goto argerr;
404 certfile = *++args;
405 } else if (!strcmp(*args, "-CAfile")) {
406 if (!args[1])
407 goto argerr;
408 CAfile = *++args;
409 } else if (!strcmp(*args, "-CApath")) {
410 if (!args[1])
411 goto argerr;
412 CApath = *++args;
413 } else if (!strcmp(*args, "-in")) {
414 if (!args[1])
415 goto argerr;
416 infile = *++args;
417 } else if (!strcmp(*args, "-inform")) {
418 if (!args[1])
419 goto argerr;
420 informat = str2fmt(*++args);
421 } else if (!strcmp(*args, "-outform")) {
422 if (!args[1])
423 goto argerr;
424 outformat = str2fmt(*++args);
425 } else if (!strcmp(*args, "-out")) {
426 if (!args[1])
427 goto argerr;
428 outfile = *++args;
429 } else if (!strcmp(*args, "-content")) {
430 if (!args[1])
431 goto argerr;
432 contfile = *++args;
433 } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
434 continue;
435 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
436 badarg = 1;
437 args++;
438 }
439
440 if (((rr_allorfirst != -1) || rr_from) && !rr_to) {
441 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
442 goto argerr;
443 }
444 if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
445 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
446 goto argerr;
447 }
448 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
449 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
450 goto argerr;
451 }
452 if (operation & SMIME_SIGNERS) {
453 if (keyfile && !signerfile) {
454 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
455 goto argerr;
456 }
457 /* Check to see if any final signer needs to be appended */
458 if (signerfile) {
459 if (!sksigners)
460 sksigners = sk_OPENSSL_STRING_new_null();
461 sk_OPENSSL_STRING_push(sksigners, signerfile);
462 if (!skkeys)
463 skkeys = sk_OPENSSL_STRING_new_null();
464 if (!keyfile)
465 keyfile = signerfile;
466 sk_OPENSSL_STRING_push(skkeys, keyfile);
467 }
468 if (!sksigners) {
469 BIO_printf(bio_err,
470 "No signer certificate specified\n");
471 badarg = 1;
472 }
473 signerfile = NULL;
474 keyfile = NULL;
475 } else if (operation == SMIME_DECRYPT) {
476 if (!recipfile && !keyfile && !secret_key && !pwri_pass) {
477 BIO_printf(bio_err,
478 "No recipient certificate or key specified\n");
479 badarg = 1;
480 }
481 } else if (operation == SMIME_ENCRYPT) {
482 if (!*args && !secret_key && !pwri_pass) {
483 BIO_printf(bio_err,
484 "No recipient(s) certificate(s) specified\n");
485 badarg = 1;
486 }
487 } else if (!operation)
488 badarg = 1;
489
490 if (badarg) {
491argerr:
492 BIO_printf(bio_err, "Usage cms [options] cert.pem ...\n");
493 BIO_printf(bio_err, "where options are\n");
494 BIO_printf(bio_err, "-encrypt encrypt message\n");
495 BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
496 BIO_printf(bio_err, "-sign sign message\n");
497 BIO_printf(bio_err, "-verify verify signed message\n");
498 BIO_printf(bio_err, "-cmsout output CMS structure\n");
499#ifndef OPENSSL_NO_DES
500 BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
501 BIO_printf(bio_err, "-des encrypt with DES\n");
502#endif
503#ifndef OPENSSL_NO_RC2
504 BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
505 BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
506 BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
507#endif
508#ifndef OPENSSL_NO_AES
509 BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
510 BIO_printf(bio_err, " encrypt PEM output with cbc aes\n");
511#endif
512#ifndef OPENSSL_NO_CAMELLIA
513 BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
514 BIO_printf(bio_err, " encrypt PEM output with cbc camellia\n");
515#endif
516 BIO_printf(bio_err, "-nointern don't search certificates in message for signer\n");
517 BIO_printf(bio_err, "-nosigs don't verify message signature\n");
518 BIO_printf(bio_err, "-noverify don't verify signers certificate\n");
519 BIO_printf(bio_err, "-nocerts don't include signers certificate when signing\n");
520 BIO_printf(bio_err, "-nodetach use opaque signing\n");
521 BIO_printf(bio_err, "-noattr don't include any signed attributes\n");
522 BIO_printf(bio_err, "-binary don't translate message to text\n");
523 BIO_printf(bio_err, "-certfile file other certificates file\n");
524 BIO_printf(bio_err, "-certsout file certificate output file\n");
525 BIO_printf(bio_err, "-signer file signer certificate file\n");
526 BIO_printf(bio_err, "-recip file recipient certificate file for decryption\n");
527 BIO_printf(bio_err, "-keyid use subject key identifier\n");
528 BIO_printf(bio_err, "-in file input file\n");
529 BIO_printf(bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
530 BIO_printf(bio_err, "-inkey file input private key (if not signer or recipient)\n");
531 BIO_printf(bio_err, "-keyform arg input private key format (PEM or ENGINE)\n");
532 BIO_printf(bio_err, "-out file output file\n");
533 BIO_printf(bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
534 BIO_printf(bio_err, "-content file supply or override content for detached signature\n");
535 BIO_printf(bio_err, "-to addr to address\n");
536 BIO_printf(bio_err, "-from ad from address\n");
537 BIO_printf(bio_err, "-subject s subject\n");
538 BIO_printf(bio_err, "-text include or delete text MIME headers\n");
539 BIO_printf(bio_err, "-CApath dir trusted certificates directory\n");
540 BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
541 BIO_printf(bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
542 BIO_printf(bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
543#ifndef OPENSSL_NO_ENGINE
544 BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n");
545#endif
546 BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
547 BIO_printf(bio_err, "cert.pem recipient certificate(s) for encryption\n");
548 goto end;
549 }
550#ifndef OPENSSL_NO_ENGINE
551 e = setup_engine(bio_err, engine, 0);
552#endif
553
554 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
555 BIO_printf(bio_err, "Error getting password\n");
556 goto end;
557 }
558 ret = 2;
559
560 if (!(operation & SMIME_SIGNERS))
561 flags &= ~CMS_DETACHED;
562
563 if (operation & SMIME_OP) {
564 if (outformat == FORMAT_ASN1)
565 outmode = "wb";
566 } else {
567 if (flags & CMS_BINARY)
568 outmode = "wb";
569 }
570
571 if (operation & SMIME_IP) {
572 if (informat == FORMAT_ASN1)
573 inmode = "rb";
574 } else {
575 if (flags & CMS_BINARY)
576 inmode = "rb";
577 }
578
579 if (operation == SMIME_ENCRYPT) {
580 if (!cipher) {
581#ifndef OPENSSL_NO_DES
582 cipher = EVP_des_ede3_cbc();
583#else
584 BIO_printf(bio_err, "No cipher selected\n");
585 goto end;
586#endif
587 }
588 if (secret_key && !secret_keyid) {
589 BIO_printf(bio_err, "No secret key id\n");
590 goto end;
591 }
592 if (*args)
593 encerts = sk_X509_new_null();
594 while (*args) {
595 if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
596 NULL, e, "recipient certificate file")))
597 goto end;
598 sk_X509_push(encerts, cert);
599 cert = NULL;
600 args++;
601 }
602 }
603 if (certfile) {
604 if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
605 e, "certificate file"))) {
606 ERR_print_errors(bio_err);
607 goto end;
608 }
609 }
610 if (recipfile && (operation == SMIME_DECRYPT)) {
611 if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
612 e, "recipient certificate file"))) {
613 ERR_print_errors(bio_err);
614 goto end;
615 }
616 }
617 if (operation == SMIME_SIGN_RECEIPT) {
618 if (!(signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
619 e, "receipt signer certificate file"))) {
620 ERR_print_errors(bio_err);
621 goto end;
622 }
623 }
624 if (operation == SMIME_DECRYPT) {
625 if (!keyfile)
626 keyfile = recipfile;
627 } else if ((operation == SMIME_SIGN) ||
628 (operation == SMIME_SIGN_RECEIPT)) {
629 if (!keyfile)
630 keyfile = signerfile;
631 } else
632 keyfile = NULL;
633
634 if (keyfile) {
635 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
636 "signing key file");
637 if (!key)
638 goto end;
639 }
640 if (infile) {
641 if (!(in = BIO_new_file(infile, inmode))) {
642 BIO_printf(bio_err,
643 "Can't open input file %s\n", infile);
644 goto end;
645 }
646 } else
647 in = BIO_new_fp(stdin, BIO_NOCLOSE);
648
649 if (operation & SMIME_IP) {
650 if (informat == FORMAT_SMIME)
651 cms = SMIME_read_CMS(in, &indata);
652 else if (informat == FORMAT_PEM)
653 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
654 else if (informat == FORMAT_ASN1)
655 cms = d2i_CMS_bio(in, NULL);
656 else {
657 BIO_printf(bio_err, "Bad input format for CMS file\n");
658 goto end;
659 }
660
661 if (!cms) {
662 BIO_printf(bio_err, "Error reading S/MIME message\n");
663 goto end;
664 }
665 if (contfile) {
666 BIO_free(indata);
667 if (!(indata = BIO_new_file(contfile, "rb"))) {
668 BIO_printf(bio_err,
669 "Can't read content file %s\n", contfile);
670 goto end;
671 }
672 }
673 if (certsoutfile) {
674 STACK_OF(X509) * allcerts;
675 allcerts = CMS_get1_certs(cms);
676 if (!save_certs(certsoutfile, allcerts)) {
677 BIO_printf(bio_err,
678 "Error writing certs to %s\n",
679 certsoutfile);
680 ret = 5;
681 goto end;
682 }
683 sk_X509_pop_free(allcerts, X509_free);
684 }
685 }
686 if (rctfile) {
687 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
688 if (!(rctin = BIO_new_file(rctfile, rctmode))) {
689 BIO_printf(bio_err,
690 "Can't open receipt file %s\n", rctfile);
691 goto end;
692 }
693 if (rctformat == FORMAT_SMIME)
694 rcms = SMIME_read_CMS(rctin, NULL);
695 else if (rctformat == FORMAT_PEM)
696 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
697 else if (rctformat == FORMAT_ASN1)
698 rcms = d2i_CMS_bio(rctin, NULL);
699 else {
700 BIO_printf(bio_err, "Bad input format for receipt\n");
701 goto end;
702 }
703
704 if (!rcms) {
705 BIO_printf(bio_err, "Error reading receipt\n");
706 goto end;
707 }
708 }
709 if (outfile) {
710 if (!(out = BIO_new_file(outfile, outmode))) {
711 BIO_printf(bio_err,
712 "Can't open output file %s\n", outfile);
713 goto end;
714 }
715 } else {
716 out = BIO_new_fp(stdout, BIO_NOCLOSE);
717 }
718
719 if ((operation == SMIME_VERIFY) ||
720 (operation == SMIME_VERIFY_RECEIPT)) {
721 if (!(store = setup_verify(bio_err, CAfile, CApath)))
722 goto end;
723 X509_STORE_set_verify_cb(store, cms_cb);
724 if (vpm)
725 X509_STORE_set1_param(store, vpm);
726 }
727 ret = 3;
728
729 if (operation == SMIME_DATA_CREATE) {
730 cms = CMS_data_create(in, flags);
731 } else if (operation == SMIME_DIGEST_CREATE) {
732 cms = CMS_digest_create(in, sign_md, flags);
733 } else if (operation == SMIME_COMPRESS) {
734 cms = CMS_compress(in, -1, flags);
735 } else if (operation == SMIME_ENCRYPT) {
736 flags |= CMS_PARTIAL;
737 cms = CMS_encrypt(encerts, in, cipher, flags);
738 if (!cms)
739 goto end;
740 if (secret_key) {
741 if (!CMS_add0_recipient_key(cms, NID_undef, secret_key,
742 secret_keylen, secret_keyid, secret_keyidlen,
743 NULL, NULL, NULL))
744 goto end;
745 /* NULL these because call absorbs them */
746 secret_key = NULL;
747 secret_keyid = NULL;
748 }
749 if (pwri_pass) {
750 pwri_tmp = strdup(pwri_pass);
751 if (!pwri_tmp)
752 goto end;
753 if (!CMS_add0_recipient_password(cms, -1, NID_undef,
754 NID_undef, pwri_tmp, -1, NULL))
755 goto end;
756 pwri_tmp = NULL;
757 }
758 if (!(flags & CMS_STREAM)) {
759 if (!CMS_final(cms, in, NULL, flags))
760 goto end;
761 }
762 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
763 cms = CMS_EncryptedData_encrypt(in, cipher, secret_key,
764 secret_keylen, flags);
765
766 } else if (operation == SMIME_SIGN_RECEIPT) {
767 CMS_ContentInfo *srcms = NULL;
768 STACK_OF(CMS_SignerInfo) * sis;
769 CMS_SignerInfo *si;
770 sis = CMS_get0_SignerInfos(cms);
771 if (!sis)
772 goto end;
773 si = sk_CMS_SignerInfo_value(sis, 0);
774 srcms = CMS_sign_receipt(si, signer, key, other, flags);
775 if (!srcms)
776 goto end;
777 CMS_ContentInfo_free(cms);
778 cms = srcms;
779 } else if (operation & SMIME_SIGNERS) {
780 int i;
781 /*
782 * If detached data content we enable streaming if S/MIME
783 * output format.
784 */
785 if (operation == SMIME_SIGN) {
786
787 if (flags & CMS_DETACHED) {
788 if (outformat == FORMAT_SMIME)
789 flags |= CMS_STREAM;
790 }
791 flags |= CMS_PARTIAL;
792 cms = CMS_sign(NULL, NULL, other, in, flags);
793 if (!cms)
794 goto end;
795 if (econtent_type)
796 CMS_set1_eContentType(cms, econtent_type);
797
798 if (rr_to) {
799 rr = make_receipt_request(rr_to, rr_allorfirst,
800 rr_from);
801 if (!rr) {
802 BIO_puts(bio_err,
803 "Signed Receipt Request Creation Error\n");
804 goto end;
805 }
806 }
807 } else
808 flags |= CMS_REUSE_DIGEST;
809 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
810 CMS_SignerInfo *si;
811 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
812 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
813 signer = load_cert(bio_err, signerfile, FORMAT_PEM,
814 NULL, e, "signer certificate");
815 if (!signer)
816 goto end;
817 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
818 "signing key file");
819 if (!key)
820 goto end;
821 si = CMS_add1_signer(cms, signer, key, sign_md, flags);
822 if (!si)
823 goto end;
824 if (rr && !CMS_add1_ReceiptRequest(si, rr))
825 goto end;
826 X509_free(signer);
827 signer = NULL;
828 EVP_PKEY_free(key);
829 key = NULL;
830 }
831 /* If not streaming or resigning finalize structure */
832 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
833 if (!CMS_final(cms, in, NULL, flags))
834 goto end;
835 }
836 }
837 if (!cms) {
838 BIO_printf(bio_err, "Error creating CMS structure\n");
839 goto end;
840 }
841 ret = 4;
842 if (operation == SMIME_DECRYPT) {
843 if (flags & CMS_DEBUG_DECRYPT)
844 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
845
846 if (secret_key) {
847 if (!CMS_decrypt_set1_key(cms, secret_key,
848 secret_keylen, secret_keyid, secret_keyidlen)) {
849 BIO_puts(bio_err,
850 "Error decrypting CMS using secret key\n");
851 goto end;
852 }
853 }
854 if (key) {
855 if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
856 BIO_puts(bio_err,
857 "Error decrypting CMS using private key\n");
858 goto end;
859 }
860 }
861 if (pwri_pass) {
862 if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
863 BIO_puts(bio_err,
864 "Error decrypting CMS using password\n");
865 goto end;
866 }
867 }
868 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
869 BIO_printf(bio_err, "Error decrypting CMS structure\n");
870 goto end;
871 }
872 } else if (operation == SMIME_DATAOUT) {
873 if (!CMS_data(cms, out, flags))
874 goto end;
875 } else if (operation == SMIME_UNCOMPRESS) {
876 if (!CMS_uncompress(cms, indata, out, flags))
877 goto end;
878 } else if (operation == SMIME_DIGEST_VERIFY) {
879 if (CMS_digest_verify(cms, indata, out, flags) > 0)
880 BIO_printf(bio_err, "Verification successful\n");
881 else {
882 BIO_printf(bio_err, "Verification failure\n");
883 goto end;
884 }
885 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
886 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
887 indata, out, flags))
888 goto end;
889 } else if (operation == SMIME_VERIFY) {
890 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
891 BIO_printf(bio_err, "Verification successful\n");
892 else {
893 BIO_printf(bio_err, "Verification failure\n");
894 if (verify_retcode)
895 ret = verify_err + 32;
896 goto end;
897 }
898 if (signerfile) {
899 STACK_OF(X509) * signers;
900 signers = CMS_get0_signers(cms);
901 if (!save_certs(signerfile, signers)) {
902 BIO_printf(bio_err,
903 "Error writing signers to %s\n",
904 signerfile);
905 ret = 5;
906 goto end;
907 }
908 sk_X509_free(signers);
909 }
910 if (rr_print)
911 receipt_request_print(bio_err, cms);
912
913 } else if (operation == SMIME_VERIFY_RECEIPT) {
914 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
915 BIO_printf(bio_err, "Verification successful\n");
916 else {
917 BIO_printf(bio_err, "Verification failure\n");
918 goto end;
919 }
920 } else {
921 if (noout) {
922 if (print)
923 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
924 } else if (outformat == FORMAT_SMIME) {
925 if (to)
926 BIO_printf(out, "To: %s\n", to);
927 if (from)
928 BIO_printf(out, "From: %s\n", from);
929 if (subject)
930 BIO_printf(out, "Subject: %s\n", subject);
931 if (operation == SMIME_RESIGN)
932 ret = SMIME_write_CMS(out, cms, indata, flags);
933 else
934 ret = SMIME_write_CMS(out, cms, in, flags);
935 } else if (outformat == FORMAT_PEM)
936 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
937 else if (outformat == FORMAT_ASN1)
938 ret = i2d_CMS_bio_stream(out, cms, in, flags);
939 else {
940 BIO_printf(bio_err, "Bad output format for CMS file\n");
941 goto end;
942 }
943 if (ret <= 0) {
944 ret = 6;
945 goto end;
946 }
947 }
948 ret = 0;
949
950end:
951 if (ret)
952 ERR_print_errors(bio_err);
953 sk_X509_pop_free(encerts, X509_free);
954 sk_X509_pop_free(other, X509_free);
955 if (vpm)
956 X509_VERIFY_PARAM_free(vpm);
957 if (sksigners)
958 sk_OPENSSL_STRING_free(sksigners);
959 if (skkeys)
960 sk_OPENSSL_STRING_free(skkeys);
961 free(secret_key);
962 free(secret_keyid);
963 free(pwri_tmp);
964 if (econtent_type)
965 ASN1_OBJECT_free(econtent_type);
966 if (rr)
967 CMS_ReceiptRequest_free(rr);
968 if (rr_to)
969 sk_OPENSSL_STRING_free(rr_to);
970 if (rr_from)
971 sk_OPENSSL_STRING_free(rr_from);
972 X509_STORE_free(store);
973 X509_free(cert);
974 X509_free(recip);
975 X509_free(signer);
976 EVP_PKEY_free(key);
977 CMS_ContentInfo_free(cms);
978 CMS_ContentInfo_free(rcms);
979 BIO_free(rctin);
980 BIO_free(in);
981 BIO_free(indata);
982 BIO_free_all(out);
983 free(passin);
984 return (ret);
985}
986
987static int
988save_certs(char *signerfile, STACK_OF(X509) * signers)
989{
990 int i;
991 BIO *tmp;
992
993 if (!signerfile)
994 return 1;
995 tmp = BIO_new_file(signerfile, "w");
996 if (!tmp)
997 return 0;
998 for (i = 0; i < sk_X509_num(signers); i++)
999 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1000 BIO_free(tmp);
1001 return 1;
1002}
1003
1004/* Minimal callback just to output policy info (if any) */
1005
1006static int
1007cms_cb(int ok, X509_STORE_CTX * ctx)
1008{
1009 int error;
1010
1011 error = X509_STORE_CTX_get_error(ctx);
1012
1013 verify_err = error;
1014
1015 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) &&
1016 ((error != X509_V_OK) || (ok != 2)))
1017 return ok;
1018
1019 policies_print(NULL, ctx);
1020
1021 return ok;
1022}
1023
1024static void
1025gnames_stack_print(BIO * out, STACK_OF(GENERAL_NAMES) * gns)
1026{
1027 STACK_OF(GENERAL_NAME) * gens;
1028 GENERAL_NAME *gen;
1029 int i, j;
1030
1031 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1032 gens = sk_GENERAL_NAMES_value(gns, i);
1033 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1034 gen = sk_GENERAL_NAME_value(gens, j);
1035 BIO_puts(out, " ");
1036 GENERAL_NAME_print(out, gen);
1037 BIO_puts(out, "\n");
1038 }
1039 }
1040 return;
1041}
1042
1043static void
1044receipt_request_print(BIO * out, CMS_ContentInfo * cms)
1045{
1046 STACK_OF(CMS_SignerInfo) * sis;
1047 CMS_SignerInfo *si;
1048 CMS_ReceiptRequest *rr;
1049 int allorfirst;
1050 STACK_OF(GENERAL_NAMES) * rto, *rlist;
1051 ASN1_STRING *scid;
1052 int i, rv;
1053
1054 sis = CMS_get0_SignerInfos(cms);
1055 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1056 si = sk_CMS_SignerInfo_value(sis, i);
1057 rv = CMS_get1_ReceiptRequest(si, &rr);
1058 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1059 if (rv == 0)
1060 BIO_puts(bio_err, " No Receipt Request\n");
1061 else if (rv < 0) {
1062 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1063 ERR_print_errors(bio_err);
1064 } else {
1065 char *id;
1066 int idlen;
1067 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1068 &rlist, &rto);
1069 BIO_puts(out, " Signed Content ID:\n");
1070 idlen = ASN1_STRING_length(scid);
1071 id = (char *) ASN1_STRING_data(scid);
1072 BIO_dump_indent(out, id, idlen, 4);
1073 BIO_puts(out, " Receipts From");
1074 if (rlist) {
1075 BIO_puts(out, " List:\n");
1076 gnames_stack_print(out, rlist);
1077 } else if (allorfirst == 1)
1078 BIO_puts(out, ": First Tier\n");
1079 else if (allorfirst == 0)
1080 BIO_puts(out, ": All\n");
1081 else
1082 BIO_printf(out, " Unknown (%d)\n", allorfirst);
1083 BIO_puts(out, " Receipts To:\n");
1084 gnames_stack_print(out, rto);
1085 }
1086 if (rr)
1087 CMS_ReceiptRequest_free(rr);
1088 }
1089}
1090
1091static STACK_OF(GENERAL_NAMES) *
1092make_names_stack(STACK_OF(OPENSSL_STRING) * ns)
1093{
1094 int i;
1095 STACK_OF(GENERAL_NAMES) * ret;
1096 GENERAL_NAMES *gens = NULL;
1097 GENERAL_NAME *gen = NULL;
1098 ret = sk_GENERAL_NAMES_new_null();
1099 if (!ret)
1100 goto err;
1101 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1102 char *str = sk_OPENSSL_STRING_value(ns, i);
1103 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1104 if (!gen)
1105 goto err;
1106 gens = GENERAL_NAMES_new();
1107 if (!gens)
1108 goto err;
1109 if (!sk_GENERAL_NAME_push(gens, gen))
1110 goto err;
1111 gen = NULL;
1112 if (!sk_GENERAL_NAMES_push(ret, gens))
1113 goto err;
1114 gens = NULL;
1115 }
1116
1117 return ret;
1118
1119err:
1120 if (ret)
1121 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1122 if (gens)
1123 GENERAL_NAMES_free(gens);
1124 if (gen)
1125 GENERAL_NAME_free(gen);
1126 return NULL;
1127}
1128
1129
1130static CMS_ReceiptRequest *
1131make_receipt_request(STACK_OF(OPENSSL_STRING) * rr_to, int rr_allorfirst,
1132 STACK_OF(OPENSSL_STRING) * rr_from)
1133{
1134 STACK_OF(GENERAL_NAMES) * rct_to, *rct_from;
1135 CMS_ReceiptRequest *rr;
1136
1137 rct_to = make_names_stack(rr_to);
1138 if (!rct_to)
1139 goto err;
1140 if (rr_from) {
1141 rct_from = make_names_stack(rr_from);
1142 if (!rct_from)
1143 goto err;
1144 } else
1145 rct_from = NULL;
1146 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
1147 rct_to);
1148 return rr;
1149
1150err:
1151 return NULL;
1152}
1153
1154#endif