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