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