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