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