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