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