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