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