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