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