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