VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/apps/cms.c

Last change on this file was 108206, checked in by vboxsync, 3 months ago

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette