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