]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/cms.c
PROV: Add type specific MSBLOB and PVK decoding for the MS->key decoders
[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 #ifndef OPENSSL_NO_CMS
18
19 # include <openssl/crypto.h>
20 # include <openssl/pem.h>
21 # include <openssl/err.h>
22 # include <openssl/x509_vfy.h>
23 # include <openssl/x509v3.h>
24 # include <openssl/cms.h>
25
26 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
27 static int cms_cb(int ok, X509_STORE_CTX *ctx);
28 static void receipt_request_print(CMS_ContentInfo *cms);
29 static CMS_ReceiptRequest *make_receipt_request(
30 STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
31 STACK_OF(OPENSSL_STRING) *rr_from, OSSL_LIB_CTX *libctx);
32 static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
33 STACK_OF(OPENSSL_STRING) *param);
34
35 # define SMIME_OP 0x10
36 # define SMIME_IP 0x20
37 # define SMIME_SIGNERS 0x40
38 # define SMIME_ENCRYPT (1 | SMIME_OP)
39 # define SMIME_DECRYPT (2 | SMIME_IP)
40 # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
41 # define SMIME_VERIFY (4 | SMIME_IP)
42 # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
43 # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
44 # define SMIME_DATAOUT (7 | SMIME_IP)
45 # define SMIME_DATA_CREATE (8 | SMIME_OP)
46 # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
47 # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
48 # define SMIME_UNCOMPRESS (11 | SMIME_IP)
49 # define SMIME_COMPRESS (12 | SMIME_OP)
50 # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
51 # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
52 # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
53 # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
54
55 static int verify_err = 0;
56
57 typedef struct cms_key_param_st cms_key_param;
58
59 struct cms_key_param_st {
60 int idx;
61 STACK_OF(OPENSSL_STRING) *param;
62 cms_key_param *next;
63 };
64
65 typedef enum OPTION_choice {
66 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
67 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENCRYPT,
68 OPT_DECRYPT, OPT_SIGN, OPT_CADES, OPT_SIGN_RECEIPT, OPT_RESIGN,
69 OPT_VERIFY, OPT_VERIFY_RETCODE, OPT_VERIFY_RECEIPT,
70 OPT_CMSOUT, OPT_DATA_OUT, OPT_DATA_CREATE, OPT_DIGEST_VERIFY,
71 OPT_DIGEST_CREATE, OPT_COMPRESS, OPT_UNCOMPRESS,
72 OPT_ED_DECRYPT, OPT_ED_ENCRYPT, OPT_DEBUG_DECRYPT, OPT_TEXT,
73 OPT_ASCIICRLF, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCERTS,
74 OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP, OPT_BINARY, OPT_KEYID,
75 OPT_NOSIGS, OPT_NO_CONTENT_VERIFY, OPT_NO_ATTR_VERIFY, OPT_INDEF,
76 OPT_NOINDEF, OPT_CRLFEOL, OPT_NOOUT, OPT_RR_PRINT,
77 OPT_RR_ALL, OPT_RR_FIRST, OPT_RCTFORM, OPT_CERTFILE, OPT_CAFILE,
78 OPT_CAPATH, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
79 OPT_CONTENT, OPT_PRINT, OPT_NAMEOPT,
80 OPT_SECRETKEY, OPT_SECRETKEYID, OPT_PWRI_PASSWORD, OPT_ECONTENT_TYPE,
81 OPT_PASSIN, OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP,
82 OPT_CERTSOUT, OPT_MD, OPT_INKEY, OPT_KEYFORM, OPT_KEYOPT, OPT_RR_FROM,
83 OPT_RR_TO, OPT_AES128_WRAP, OPT_AES192_WRAP, OPT_AES256_WRAP,
84 OPT_3DES_WRAP, OPT_WRAP, OPT_ENGINE,
85 OPT_R_ENUM,
86 OPT_PROV_ENUM, OPT_CONFIG,
87 OPT_V_ENUM,
88 OPT_CIPHER,
89 OPT_ORIGINATOR
90 } OPTION_CHOICE;
91
92 const OPTIONS cms_options[] = {
93 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
94
95 OPT_SECTION("General"),
96 {"help", OPT_HELP, '-', "Display this summary"},
97 {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"},
98 {"outform", OPT_OUTFORM, 'c',
99 "Output format SMIME (default), PEM or DER"},
100 {"in", OPT_IN, '<', "Input file"},
101 {"out", OPT_OUT, '>', "Output file"},
102 {"debug_decrypt", OPT_DEBUG_DECRYPT, '-',
103 "Disable MMA protection and return an error if no recipient found"
104 " (see documentation)"},
105 {"stream", OPT_INDEF, '-', "Enable CMS streaming"},
106 {"indef", OPT_INDEF, '-', "Same as -stream"},
107 {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"},
108 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only" },
109 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
110 {"CApath", OPT_CAPATH, '/', "trusted certificates directory"},
111 {"CAstore", OPT_CASTORE, ':', "trusted certificates store URI"},
112 {"no-CAfile", OPT_NOCAFILE, '-',
113 "Do not load the default certificates file"},
114 {"no-CApath", OPT_NOCAPATH, '-',
115 "Do not load certificates from the default certificates directory"},
116 {"no-CAstore", OPT_NOCASTORE, '-',
117 "Do not load certificates from the default certificates store"},
118 # ifndef OPENSSL_NO_ENGINE
119 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
120 # endif
121 OPT_CONFIG_OPTION,
122
123 OPT_SECTION("Action"),
124 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
125 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
126 {"sign", OPT_SIGN, '-', "Sign message"},
127 {"sign_receipt", OPT_SIGN_RECEIPT, '-', "Generate a signed receipt for the message"},
128 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
129 {"cades", OPT_CADES, '-', "Include or check signingCertificate (CAdES-BES)"},
130 {"verify", OPT_VERIFY, '-', "Verify signed message"},
131 {"verify_retcode", OPT_VERIFY_RETCODE, '-',
132 "Exit non-zero on verification failure"},
133 {"verify_receipt", OPT_VERIFY_RECEIPT, '<',
134 "Verify receipts; exit if receipt signatures do not verify"},
135 {"digest_verify", OPT_DIGEST_VERIFY, '-',
136 "Verify a CMS \"DigestedData\" object and output it"},
137 {"digest_create", OPT_DIGEST_CREATE, '-',
138 "Create a CMS \"DigestedData\" object"},
139 {"compress", OPT_COMPRESS, '-', "Create a CMS \"CompressedData\" object"},
140 {"uncompress", OPT_UNCOMPRESS, '-',
141 "Uncompress a CMS \"CompressedData\" object"},
142 {"EncryptedData_decrypt", OPT_ED_DECRYPT, '-',
143 "Decrypt CMS \"EncryptedData\" object using symmetric key"},
144 {"EncryptedData_encrypt", OPT_ED_ENCRYPT, '-',
145 "Create CMS \"EncryptedData\" object using symmetric key"},
146 {"data_out", OPT_DATA_OUT, '-', "Copy CMS \"Data\" object to output"},
147 {"data_create", OPT_DATA_CREATE, '-', "Create a CMS \"Data\" object"},
148 {"cmsout", OPT_CMSOUT, '-', "Output CMS structure"},
149 {"no_content_verify", OPT_NO_CONTENT_VERIFY, '-',
150 "Do not verify signed content signatures"},
151 {"no_attr_verify", OPT_NO_ATTR_VERIFY, '-',
152 "Do not verify signed attribute signatures"},
153 {"nointern", OPT_NOINTERN, '-',
154 "Don't search certificates in message for signer"},
155 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
156
157 OPT_SECTION("Formatting"),
158 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
159 {"asciicrlf", OPT_ASCIICRLF, '-',
160 "Perform CRLF canonicalisation when signing"},
161 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
162 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
163 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
164 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
165 {"keyid", OPT_KEYID, '-', "Use subject key identifier"},
166 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
167 {"nocerts", OPT_NOCERTS, '-',
168 "Don't include signers certificate when signing"},
169 {"noout", OPT_NOOUT, '-',
170 "For the -cmsout operation do not output the parsed CMS structure"},
171 {"receipt_request_print", OPT_RR_PRINT, '-', "Print CMS Receipt Request" },
172 {"receipt_request_all", OPT_RR_ALL, '-',
173 "When signing, create a receipt request for all recipients"},
174 {"receipt_request_first", OPT_RR_FIRST, '-',
175 "When signing, create a receipt request for first recipient"},
176 {"rctform", OPT_RCTFORM, 'F', "Receipt file format"},
177 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
178 {"content", OPT_CONTENT, '<',
179 "Supply or override content for detached signature"},
180 {"print", OPT_PRINT, '-',
181 "For the -cmsout operation print out all fields of the CMS structure"},
182 {"nameopt", OPT_NAMEOPT, 's',
183 "For the -print option specifies various strings printing options"},
184 {"certsout", OPT_CERTSOUT, '>', "Certificate output file"},
185
186 OPT_SECTION("Keying"),
187 {"secretkey", OPT_SECRETKEY, 's',
188 "Use specified hex-encoded key to decrypt/encrypt recipients or content"},
189 {"secretkeyid", OPT_SECRETKEYID, 's',
190 "Identity of the -secretkey for CMS \"KEKRecipientInfo\" object"},
191 {"pwri_password", OPT_PWRI_PASSWORD, 's',
192 "Specific password for recipient"},
193 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
194 {"inkey", OPT_INKEY, 's',
195 "Input private key (if not signer or recipient)"},
196 {"keyform", OPT_KEYFORM, 'f', "Input private key format (ENGINE, other values ignored)"},
197 {"keyopt", OPT_KEYOPT, 's', "Set public key parameters as n:v pairs"},
198
199 OPT_SECTION("Mail header"),
200 {"econtent_type", OPT_ECONTENT_TYPE, 's', "OID for external content"},
201 {"to", OPT_TO, 's', "To address"},
202 {"from", OPT_FROM, 's', "From address"},
203 {"subject", OPT_SUBJECT, 's', "Subject"},
204 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
205 {"originator", OPT_ORIGINATOR, 's', "Originator certificate file"},
206 {"recip", OPT_RECIP, '<', "Recipient cert file for decryption"},
207 {"receipt_request_from", OPT_RR_FROM, 's',
208 "Create signed receipt request with specified email address"},
209 {"receipt_request_to", OPT_RR_TO, 's',
210 "Create signed receipt targeted to specified address"},
211
212 OPT_SECTION("Encryption"),
213 {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"},
214 {"", OPT_CIPHER, '-', "Any supported cipher"},
215
216 OPT_SECTION("Key-wrapping"),
217 {"aes128-wrap", OPT_AES128_WRAP, '-', "Use AES128 to wrap key"},
218 {"aes192-wrap", OPT_AES192_WRAP, '-', "Use AES192 to wrap key"},
219 {"aes256-wrap", OPT_AES256_WRAP, '-', "Use AES256 to wrap key"},
220 # ifndef OPENSSL_NO_DES
221 {"des3-wrap", OPT_3DES_WRAP, '-', "Use 3DES-EDE to wrap key"},
222 # endif
223 {"wrap", OPT_WRAP, 's', "Any wrap cipher to wrap key"},
224
225 OPT_R_OPTIONS,
226 OPT_V_OPTIONS,
227 OPT_PROV_OPTIONS,
228
229 OPT_PARAMETERS(),
230 {"cert", 0, 0, "Recipient certs (optional; used only when encrypting)"},
231 {NULL}
232 };
233
234 static CMS_ContentInfo *load_content_info(int informat, BIO *in, BIO **indata,
235 const char *name,
236 OSSL_LIB_CTX *libctx,
237 const char *propq)
238 {
239 CMS_ContentInfo *ret, *ci;
240
241 ret = CMS_ContentInfo_new_ex(libctx, propq);
242 if (ret == NULL) {
243 BIO_printf(bio_err, "Error allocating CMS_contentinfo\n");
244 return NULL;
245 }
246 switch (informat) {
247 case FORMAT_SMIME:
248 ci = SMIME_read_CMS_ex(in, indata, &ret);
249 break;
250 case FORMAT_PEM:
251 ci = PEM_read_bio_CMS(in, &ret, NULL, NULL);
252 break;
253 case FORMAT_ASN1:
254 ci = d2i_CMS_bio(in, &ret);
255 break;
256 default:
257 BIO_printf(bio_err, "Bad input format for %s\n", name);
258 goto err;
259 }
260 if (ci == NULL) {
261 BIO_printf(bio_err, "Error reading %s Content Info\n", name);
262 goto err;
263 }
264 return ret;
265 err:
266 CMS_ContentInfo_free(ret);
267 return NULL;
268 }
269
270 int cms_main(int argc, char **argv)
271 {
272 CONF *conf = NULL;
273 ASN1_OBJECT *econtent_type = NULL;
274 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
275 CMS_ContentInfo *cms = NULL, *rcms = NULL;
276 CMS_ReceiptRequest *rr = NULL;
277 ENGINE *e = NULL;
278 EVP_PKEY *key = NULL;
279 const EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;
280 const EVP_MD *sign_md = NULL;
281 STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
282 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
283 STACK_OF(X509) *encerts = NULL, *other = NULL;
284 X509 *cert = NULL, *recip = NULL, *signer = NULL, *originator = NULL;
285 X509_STORE *store = NULL;
286 X509_VERIFY_PARAM *vpm = NULL;
287 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
288 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
289 char *certsoutfile = NULL, *digestname = NULL;
290 int noCAfile = 0, noCApath = 0, noCAstore = 0;
291 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
292 char *passinarg = NULL, *passin = NULL, *signerfile = NULL;
293 char *originatorfile = NULL, *recipfile = NULL, *ciphername = NULL;
294 char *to = NULL, *from = NULL, *subject = NULL, *prog;
295 cms_key_param *key_first = NULL, *key_param = NULL;
296 int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched = 0;
297 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
298 int operation = 0, ret = 1, rr_print = 0, rr_allorfirst = -1;
299 int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
300 size_t secret_keylen = 0, secret_keyidlen = 0;
301 unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
302 unsigned char *secret_key = NULL, *secret_keyid = NULL;
303 long ltmp;
304 const char *mime_eol = "\n";
305 OPTION_CHOICE o;
306 OSSL_LIB_CTX *libctx = app_get0_libctx();
307
308 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
309 return 1;
310
311 prog = opt_init(argc, argv, cms_options);
312 while ((o = opt_next()) != OPT_EOF) {
313 switch (o) {
314 case OPT_EOF:
315 case OPT_ERR:
316 opthelp:
317 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
318 goto end;
319 case OPT_HELP:
320 opt_help(cms_options);
321 ret = 0;
322 goto end;
323 case OPT_INFORM:
324 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
325 goto opthelp;
326 break;
327 case OPT_OUTFORM:
328 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
329 goto opthelp;
330 break;
331 case OPT_OUT:
332 outfile = opt_arg();
333 break;
334 case OPT_ENCRYPT:
335 operation = SMIME_ENCRYPT;
336 break;
337 case OPT_DECRYPT:
338 operation = SMIME_DECRYPT;
339 break;
340 case OPT_SIGN:
341 operation = SMIME_SIGN;
342 break;
343 case OPT_SIGN_RECEIPT:
344 operation = SMIME_SIGN_RECEIPT;
345 break;
346 case OPT_RESIGN:
347 operation = SMIME_RESIGN;
348 break;
349 case OPT_VERIFY:
350 operation = SMIME_VERIFY;
351 break;
352 case OPT_VERIFY_RETCODE:
353 verify_retcode = 1;
354 break;
355 case OPT_VERIFY_RECEIPT:
356 operation = SMIME_VERIFY_RECEIPT;
357 rctfile = opt_arg();
358 break;
359 case OPT_CMSOUT:
360 operation = SMIME_CMSOUT;
361 break;
362 case OPT_DATA_OUT:
363 operation = SMIME_DATAOUT;
364 break;
365 case OPT_DATA_CREATE:
366 operation = SMIME_DATA_CREATE;
367 break;
368 case OPT_DIGEST_VERIFY:
369 operation = SMIME_DIGEST_VERIFY;
370 break;
371 case OPT_DIGEST_CREATE:
372 operation = SMIME_DIGEST_CREATE;
373 break;
374 case OPT_COMPRESS:
375 operation = SMIME_COMPRESS;
376 break;
377 case OPT_UNCOMPRESS:
378 operation = SMIME_UNCOMPRESS;
379 break;
380 case OPT_ED_DECRYPT:
381 operation = SMIME_ENCRYPTED_DECRYPT;
382 break;
383 case OPT_ED_ENCRYPT:
384 operation = SMIME_ENCRYPTED_ENCRYPT;
385 break;
386 case OPT_DEBUG_DECRYPT:
387 flags |= CMS_DEBUG_DECRYPT;
388 break;
389 case OPT_TEXT:
390 flags |= CMS_TEXT;
391 break;
392 case OPT_ASCIICRLF:
393 flags |= CMS_ASCIICRLF;
394 break;
395 case OPT_NOINTERN:
396 flags |= CMS_NOINTERN;
397 break;
398 case OPT_NOVERIFY:
399 flags |= CMS_NO_SIGNER_CERT_VERIFY;
400 break;
401 case OPT_NOCERTS:
402 flags |= CMS_NOCERTS;
403 break;
404 case OPT_NOATTR:
405 flags |= CMS_NOATTR;
406 break;
407 case OPT_NODETACH:
408 flags &= ~CMS_DETACHED;
409 break;
410 case OPT_NOSMIMECAP:
411 flags |= CMS_NOSMIMECAP;
412 break;
413 case OPT_BINARY:
414 flags |= CMS_BINARY;
415 break;
416 case OPT_CADES:
417 flags |= CMS_CADES;
418 break;
419 case OPT_KEYID:
420 flags |= CMS_USE_KEYID;
421 break;
422 case OPT_NOSIGS:
423 flags |= CMS_NOSIGS;
424 break;
425 case OPT_NO_CONTENT_VERIFY:
426 flags |= CMS_NO_CONTENT_VERIFY;
427 break;
428 case OPT_NO_ATTR_VERIFY:
429 flags |= CMS_NO_ATTR_VERIFY;
430 break;
431 case OPT_INDEF:
432 flags |= CMS_STREAM;
433 break;
434 case OPT_NOINDEF:
435 flags &= ~CMS_STREAM;
436 break;
437 case OPT_CRLFEOL:
438 mime_eol = "\r\n";
439 flags |= CMS_CRLFEOL;
440 break;
441 case OPT_NOOUT:
442 noout = 1;
443 break;
444 case OPT_RR_PRINT:
445 rr_print = 1;
446 break;
447 case OPT_RR_ALL:
448 rr_allorfirst = 0;
449 break;
450 case OPT_RR_FIRST:
451 rr_allorfirst = 1;
452 break;
453 case OPT_RCTFORM:
454 if (rctformat == FORMAT_ASN1) {
455 if (!opt_format(opt_arg(),
456 OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))
457 goto opthelp;
458 } else {
459 rcms = load_content_info(rctformat, rctin, NULL, "recipient",
460 libctx, app_get0_propq());
461 }
462 break;
463 case OPT_CERTFILE:
464 certfile = opt_arg();
465 break;
466 case OPT_CAFILE:
467 CAfile = opt_arg();
468 break;
469 case OPT_CAPATH:
470 CApath = opt_arg();
471 break;
472 case OPT_CASTORE:
473 CAstore = opt_arg();
474 break;
475 case OPT_NOCAFILE:
476 noCAfile = 1;
477 break;
478 case OPT_NOCAPATH:
479 noCApath = 1;
480 break;
481 case OPT_NOCASTORE:
482 noCAstore = 1;
483 break;
484 case OPT_IN:
485 infile = opt_arg();
486 break;
487 case OPT_CONTENT:
488 contfile = opt_arg();
489 break;
490 case OPT_RR_FROM:
491 if (rr_from == NULL
492 && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)
493 goto end;
494 sk_OPENSSL_STRING_push(rr_from, opt_arg());
495 break;
496 case OPT_RR_TO:
497 if (rr_to == NULL
498 && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)
499 goto end;
500 sk_OPENSSL_STRING_push(rr_to, opt_arg());
501 break;
502 case OPT_PRINT:
503 noout = print = 1;
504 break;
505 case OPT_NAMEOPT:
506 if (!set_nameopt(opt_arg()))
507 goto opthelp;
508 break;
509 case OPT_SECRETKEY:
510 if (secret_key != NULL) {
511 BIO_printf(bio_err, "Invalid key (supplied twice) %s\n",
512 opt_arg());
513 goto opthelp;
514 }
515 secret_key = OPENSSL_hexstr2buf(opt_arg(), &ltmp);
516 if (secret_key == NULL) {
517 BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
518 goto end;
519 }
520 secret_keylen = (size_t)ltmp;
521 break;
522 case OPT_SECRETKEYID:
523 if (secret_keyid != NULL) {
524 BIO_printf(bio_err, "Invalid id (supplied twice) %s\n",
525 opt_arg());
526 goto opthelp;
527 }
528 secret_keyid = OPENSSL_hexstr2buf(opt_arg(), &ltmp);
529 if (secret_keyid == NULL) {
530 BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
531 goto opthelp;
532 }
533 secret_keyidlen = (size_t)ltmp;
534 break;
535 case OPT_PWRI_PASSWORD:
536 pwri_pass = (unsigned char *)opt_arg();
537 break;
538 case OPT_ECONTENT_TYPE:
539 if (econtent_type != NULL) {
540 BIO_printf(bio_err, "Invalid OID (supplied twice) %s\n",
541 opt_arg());
542 goto opthelp;
543 }
544 econtent_type = OBJ_txt2obj(opt_arg(), 0);
545 if (econtent_type == NULL) {
546 BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
547 goto opthelp;
548 }
549 break;
550 case OPT_ENGINE:
551 e = setup_engine(opt_arg(), 0);
552 break;
553 case OPT_PASSIN:
554 passinarg = opt_arg();
555 break;
556 case OPT_TO:
557 to = opt_arg();
558 break;
559 case OPT_FROM:
560 from = opt_arg();
561 break;
562 case OPT_SUBJECT:
563 subject = opt_arg();
564 break;
565 case OPT_CERTSOUT:
566 certsoutfile = opt_arg();
567 break;
568 case OPT_MD:
569 digestname = opt_arg();
570 break;
571 case OPT_SIGNER:
572 /* If previous -signer argument add signer to list */
573 if (signerfile != NULL) {
574 if (sksigners == NULL
575 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
576 goto end;
577 sk_OPENSSL_STRING_push(sksigners, signerfile);
578 if (keyfile == NULL)
579 keyfile = signerfile;
580 if (skkeys == NULL
581 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
582 goto end;
583 sk_OPENSSL_STRING_push(skkeys, keyfile);
584 keyfile = NULL;
585 }
586 signerfile = opt_arg();
587 break;
588 case OPT_ORIGINATOR:
589 originatorfile = opt_arg();
590 break;
591 case OPT_INKEY:
592 /* If previous -inkey argument add signer to list */
593 if (keyfile != NULL) {
594 if (signerfile == NULL) {
595 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
596 goto end;
597 }
598 if (sksigners == NULL
599 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
600 goto end;
601 sk_OPENSSL_STRING_push(sksigners, signerfile);
602 signerfile = NULL;
603 if (skkeys == NULL
604 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
605 goto end;
606 sk_OPENSSL_STRING_push(skkeys, keyfile);
607 }
608 keyfile = opt_arg();
609 break;
610 case OPT_KEYFORM:
611 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
612 goto opthelp;
613 break;
614 case OPT_RECIP:
615 if (operation == SMIME_ENCRYPT) {
616 if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL)
617 goto end;
618 cert = load_cert(opt_arg(), "recipient certificate file");
619 if (cert == NULL)
620 goto end;
621 sk_X509_push(encerts, cert);
622 cert = NULL;
623 } else {
624 recipfile = opt_arg();
625 }
626 break;
627 case OPT_CIPHER:
628 ciphername = opt_unknown();
629 break;
630 case OPT_KEYOPT:
631 keyidx = -1;
632 if (operation == SMIME_ENCRYPT) {
633 if (encerts != NULL)
634 keyidx += sk_X509_num(encerts);
635 } else {
636 if (keyfile != NULL || signerfile != NULL)
637 keyidx++;
638 if (skkeys != NULL)
639 keyidx += sk_OPENSSL_STRING_num(skkeys);
640 }
641 if (keyidx < 0) {
642 BIO_printf(bio_err, "No key specified\n");
643 goto opthelp;
644 }
645 if (key_param == NULL || key_param->idx != keyidx) {
646 cms_key_param *nparam;
647 nparam = app_malloc(sizeof(*nparam), "key param buffer");
648 if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL) {
649 OPENSSL_free(nparam);
650 goto end;
651 }
652 nparam->idx = keyidx;
653 nparam->next = NULL;
654 if (key_first == NULL)
655 key_first = nparam;
656 else
657 key_param->next = nparam;
658 key_param = nparam;
659 }
660 sk_OPENSSL_STRING_push(key_param->param, opt_arg());
661 break;
662 case OPT_V_CASES:
663 if (!opt_verify(o, vpm))
664 goto end;
665 vpmtouched++;
666 break;
667 case OPT_R_CASES:
668 if (!opt_rand(o))
669 goto end;
670 break;
671 case OPT_PROV_CASES:
672 if (!opt_provider(o))
673 goto end;
674 break;
675 case OPT_CONFIG:
676 conf = app_load_config_modules(opt_arg());
677 if (conf == NULL)
678 goto end;
679 break;
680 case OPT_3DES_WRAP:
681 # ifndef OPENSSL_NO_DES
682 wrap_cipher = EVP_des_ede3_wrap();
683 # endif
684 break;
685 case OPT_AES128_WRAP:
686 wrap_cipher = EVP_aes_128_wrap();
687 break;
688 case OPT_AES192_WRAP:
689 wrap_cipher = EVP_aes_192_wrap();
690 break;
691 case OPT_AES256_WRAP:
692 wrap_cipher = EVP_aes_256_wrap();
693 break;
694 case OPT_WRAP:
695 if (!opt_cipher(opt_unknown(), &wrap_cipher))
696 goto end;
697 break;
698 }
699 }
700 app_RAND_load();
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_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) {
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 CMS_ContentInfo_free(cms);
1251 CMS_ContentInfo_free(rcms);
1252 release_engine(e);
1253 BIO_free(rctin);
1254 BIO_free(in);
1255 BIO_free(indata);
1256 BIO_free_all(out);
1257 OPENSSL_free(passin);
1258 NCONF_free(conf);
1259 return ret;
1260 }
1261
1262 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1263 {
1264 int i;
1265 BIO *tmp;
1266 if (signerfile == NULL)
1267 return 1;
1268 tmp = BIO_new_file(signerfile, "w");
1269 if (tmp == NULL)
1270 return 0;
1271 for (i = 0; i < sk_X509_num(signers); i++)
1272 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1273 BIO_free(tmp);
1274 return 1;
1275 }
1276
1277 /* Minimal callback just to output policy info (if any) */
1278
1279 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1280 {
1281 int error;
1282
1283 error = X509_STORE_CTX_get_error(ctx);
1284
1285 verify_err = error;
1286
1287 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1288 && ((error != X509_V_OK) || (ok != 2)))
1289 return ok;
1290
1291 policies_print(ctx);
1292
1293 return ok;
1294
1295 }
1296
1297 static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
1298 {
1299 STACK_OF(GENERAL_NAME) *gens;
1300 GENERAL_NAME *gen;
1301 int i, j;
1302
1303 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1304 gens = sk_GENERAL_NAMES_value(gns, i);
1305 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1306 gen = sk_GENERAL_NAME_value(gens, j);
1307 BIO_puts(bio_err, " ");
1308 GENERAL_NAME_print(bio_err, gen);
1309 BIO_puts(bio_err, "\n");
1310 }
1311 }
1312 return;
1313 }
1314
1315 static void receipt_request_print(CMS_ContentInfo *cms)
1316 {
1317 STACK_OF(CMS_SignerInfo) *sis;
1318 CMS_SignerInfo *si;
1319 CMS_ReceiptRequest *rr;
1320 int allorfirst;
1321 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1322 ASN1_STRING *scid;
1323 int i, rv;
1324 sis = CMS_get0_SignerInfos(cms);
1325 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1326 si = sk_CMS_SignerInfo_value(sis, i);
1327 rv = CMS_get1_ReceiptRequest(si, &rr);
1328 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1329 if (rv == 0) {
1330 BIO_puts(bio_err, " No Receipt Request\n");
1331 } else if (rv < 0) {
1332 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1333 ERR_print_errors(bio_err);
1334 } else {
1335 const char *id;
1336 int idlen;
1337 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1338 &rlist, &rto);
1339 BIO_puts(bio_err, " Signed Content ID:\n");
1340 idlen = ASN1_STRING_length(scid);
1341 id = (const char *)ASN1_STRING_get0_data(scid);
1342 BIO_dump_indent(bio_err, id, idlen, 4);
1343 BIO_puts(bio_err, " Receipts From");
1344 if (rlist != NULL) {
1345 BIO_puts(bio_err, " List:\n");
1346 gnames_stack_print(rlist);
1347 } else if (allorfirst == 1) {
1348 BIO_puts(bio_err, ": First Tier\n");
1349 } else if (allorfirst == 0) {
1350 BIO_puts(bio_err, ": All\n");
1351 } else {
1352 BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
1353 }
1354 BIO_puts(bio_err, " Receipts To:\n");
1355 gnames_stack_print(rto);
1356 }
1357 CMS_ReceiptRequest_free(rr);
1358 }
1359 }
1360
1361 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1362 {
1363 int i;
1364 STACK_OF(GENERAL_NAMES) *ret;
1365 GENERAL_NAMES *gens = NULL;
1366 GENERAL_NAME *gen = NULL;
1367 ret = sk_GENERAL_NAMES_new_null();
1368 if (ret == NULL)
1369 goto err;
1370 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1371 char *str = sk_OPENSSL_STRING_value(ns, i);
1372 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1373 if (gen == NULL)
1374 goto err;
1375 gens = GENERAL_NAMES_new();
1376 if (gens == NULL)
1377 goto err;
1378 if (!sk_GENERAL_NAME_push(gens, gen))
1379 goto err;
1380 gen = NULL;
1381 if (!sk_GENERAL_NAMES_push(ret, gens))
1382 goto err;
1383 gens = NULL;
1384 }
1385
1386 return ret;
1387
1388 err:
1389 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1390 GENERAL_NAMES_free(gens);
1391 GENERAL_NAME_free(gen);
1392 return NULL;
1393 }
1394
1395 static CMS_ReceiptRequest *make_receipt_request(
1396 STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
1397 STACK_OF(OPENSSL_STRING) *rr_from,
1398 OSSL_LIB_CTX *libctx)
1399 {
1400 STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
1401 CMS_ReceiptRequest *rr;
1402 rct_to = make_names_stack(rr_to);
1403 if (rct_to == NULL)
1404 goto err;
1405 if (rr_from != NULL) {
1406 rct_from = make_names_stack(rr_from);
1407 if (rct_from == NULL)
1408 goto err;
1409 } else {
1410 rct_from = NULL;
1411 }
1412 rr = CMS_ReceiptRequest_create0_ex(NULL, -1, rr_allorfirst, rct_from,
1413 rct_to, libctx, app_get0_propq());
1414 return rr;
1415 err:
1416 sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
1417 return NULL;
1418 }
1419
1420 static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
1421 STACK_OF(OPENSSL_STRING) *param)
1422 {
1423 char *keyopt;
1424 int i;
1425 if (sk_OPENSSL_STRING_num(param) <= 0)
1426 return 1;
1427 for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
1428 keyopt = sk_OPENSSL_STRING_value(param, i);
1429 if (pkey_ctrl_string(pctx, keyopt) <= 0) {
1430 BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
1431 ERR_print_errors(bio_err);
1432 return 0;
1433 }
1434 }
1435 return 1;
1436 }
1437
1438 #endif