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