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