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