1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci/* S/MIME utility function */ 11e1051a39Sopenharmony_ci 12e1051a39Sopenharmony_ci#include <stdio.h> 13e1051a39Sopenharmony_ci#include <string.h> 14e1051a39Sopenharmony_ci#include "apps.h" 15e1051a39Sopenharmony_ci#include "progs.h" 16e1051a39Sopenharmony_ci#include <openssl/crypto.h> 17e1051a39Sopenharmony_ci#include <openssl/pem.h> 18e1051a39Sopenharmony_ci#include <openssl/err.h> 19e1051a39Sopenharmony_ci#include <openssl/x509_vfy.h> 20e1051a39Sopenharmony_ci#include <openssl/x509v3.h> 21e1051a39Sopenharmony_ci 22e1051a39Sopenharmony_cistatic int save_certs(char *signerfile, STACK_OF(X509) *signers); 23e1051a39Sopenharmony_cistatic int smime_cb(int ok, X509_STORE_CTX *ctx); 24e1051a39Sopenharmony_ci 25e1051a39Sopenharmony_ci#define SMIME_OP 0x10 26e1051a39Sopenharmony_ci#define SMIME_IP 0x20 27e1051a39Sopenharmony_ci#define SMIME_SIGNERS 0x40 28e1051a39Sopenharmony_ci#define SMIME_ENCRYPT (1 | SMIME_OP) 29e1051a39Sopenharmony_ci#define SMIME_DECRYPT (2 | SMIME_IP) 30e1051a39Sopenharmony_ci#define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS) 31e1051a39Sopenharmony_ci#define SMIME_VERIFY (4 | SMIME_IP) 32e1051a39Sopenharmony_ci#define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP) 33e1051a39Sopenharmony_ci#define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS) 34e1051a39Sopenharmony_ci 35e1051a39Sopenharmony_citypedef enum OPTION_choice { 36e1051a39Sopenharmony_ci OPT_COMMON, 37e1051a39Sopenharmony_ci OPT_ENCRYPT, OPT_DECRYPT, OPT_SIGN, OPT_RESIGN, OPT_VERIFY, 38e1051a39Sopenharmony_ci OPT_PK7OUT, OPT_TEXT, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCHAIN, 39e1051a39Sopenharmony_ci OPT_NOCERTS, OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP, 40e1051a39Sopenharmony_ci OPT_BINARY, OPT_NOSIGS, OPT_STREAM, OPT_INDEF, OPT_NOINDEF, 41e1051a39Sopenharmony_ci OPT_CRLFEOL, OPT_ENGINE, OPT_PASSIN, 42e1051a39Sopenharmony_ci OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, OPT_MD, 43e1051a39Sopenharmony_ci OPT_CIPHER, OPT_INKEY, OPT_KEYFORM, OPT_CERTFILE, OPT_CAFILE, 44e1051a39Sopenharmony_ci OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE, OPT_NOCAPATH, OPT_NOCASTORE, 45e1051a39Sopenharmony_ci OPT_R_ENUM, OPT_PROV_ENUM, OPT_CONFIG, 46e1051a39Sopenharmony_ci OPT_V_ENUM, 47e1051a39Sopenharmony_ci OPT_IN, OPT_INFORM, OPT_OUT, 48e1051a39Sopenharmony_ci OPT_OUTFORM, OPT_CONTENT 49e1051a39Sopenharmony_ci} OPTION_CHOICE; 50e1051a39Sopenharmony_ci 51e1051a39Sopenharmony_ciconst OPTIONS smime_options[] = { 52e1051a39Sopenharmony_ci {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"}, 53e1051a39Sopenharmony_ci 54e1051a39Sopenharmony_ci OPT_SECTION("General"), 55e1051a39Sopenharmony_ci {"help", OPT_HELP, '-', "Display this summary"}, 56e1051a39Sopenharmony_ci {"in", OPT_IN, '<', "Input file"}, 57e1051a39Sopenharmony_ci {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"}, 58e1051a39Sopenharmony_ci {"out", OPT_OUT, '>', "Output file"}, 59e1051a39Sopenharmony_ci {"outform", OPT_OUTFORM, 'c', 60e1051a39Sopenharmony_ci "Output format SMIME (default), PEM or DER"}, 61e1051a39Sopenharmony_ci {"inkey", OPT_INKEY, 's', 62e1051a39Sopenharmony_ci "Input private key (if not signer or recipient)"}, 63e1051a39Sopenharmony_ci {"keyform", OPT_KEYFORM, 'f', "Input private key format (ENGINE, other values ignored)"}, 64e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE 65e1051a39Sopenharmony_ci {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, 66e1051a39Sopenharmony_ci#endif 67e1051a39Sopenharmony_ci {"stream", OPT_STREAM, '-', "Enable CMS streaming" }, 68e1051a39Sopenharmony_ci {"indef", OPT_INDEF, '-', "Same as -stream" }, 69e1051a39Sopenharmony_ci {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"}, 70e1051a39Sopenharmony_ci OPT_CONFIG_OPTION, 71e1051a39Sopenharmony_ci 72e1051a39Sopenharmony_ci OPT_SECTION("Action"), 73e1051a39Sopenharmony_ci {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"}, 74e1051a39Sopenharmony_ci {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"}, 75e1051a39Sopenharmony_ci {"sign", OPT_SIGN, '-', "Sign message"}, 76e1051a39Sopenharmony_ci {"resign", OPT_RESIGN, '-', "Resign a signed message"}, 77e1051a39Sopenharmony_ci {"verify", OPT_VERIFY, '-', "Verify signed message"}, 78e1051a39Sopenharmony_ci 79e1051a39Sopenharmony_ci OPT_SECTION("Signing/Encryption"), 80e1051a39Sopenharmony_ci {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, 81e1051a39Sopenharmony_ci {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"}, 82e1051a39Sopenharmony_ci {"", OPT_CIPHER, '-', "Any supported cipher"}, 83e1051a39Sopenharmony_ci {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"}, 84e1051a39Sopenharmony_ci {"nointern", OPT_NOINTERN, '-', 85e1051a39Sopenharmony_ci "Don't search certificates in message for signer"}, 86e1051a39Sopenharmony_ci {"nodetach", OPT_NODETACH, '-', "Use opaque signing"}, 87e1051a39Sopenharmony_ci {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"}, 88e1051a39Sopenharmony_ci {"binary", OPT_BINARY, '-', "Don't translate message to text"}, 89e1051a39Sopenharmony_ci {"signer", OPT_SIGNER, 's', "Signer certificate file"}, 90e1051a39Sopenharmony_ci {"content", OPT_CONTENT, '<', 91e1051a39Sopenharmony_ci "Supply or override content for detached signature"}, 92e1051a39Sopenharmony_ci {"nocerts", OPT_NOCERTS, '-', 93e1051a39Sopenharmony_ci "Don't include signers certificate when signing"}, 94e1051a39Sopenharmony_ci 95e1051a39Sopenharmony_ci OPT_SECTION("Verification/Decryption"), 96e1051a39Sopenharmony_ci {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"}, 97e1051a39Sopenharmony_ci {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"}, 98e1051a39Sopenharmony_ci 99e1051a39Sopenharmony_ci {"certfile", OPT_CERTFILE, '<', "Other certificates file"}, 100e1051a39Sopenharmony_ci {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"}, 101e1051a39Sopenharmony_ci 102e1051a39Sopenharmony_ci OPT_SECTION("Email"), 103e1051a39Sopenharmony_ci {"to", OPT_TO, 's', "To address"}, 104e1051a39Sopenharmony_ci {"from", OPT_FROM, 's', "From address"}, 105e1051a39Sopenharmony_ci {"subject", OPT_SUBJECT, 's', "Subject"}, 106e1051a39Sopenharmony_ci {"text", OPT_TEXT, '-', "Include or delete text MIME headers"}, 107e1051a39Sopenharmony_ci {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"}, 108e1051a39Sopenharmony_ci 109e1051a39Sopenharmony_ci OPT_SECTION("Certificate chain"), 110e1051a39Sopenharmony_ci {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"}, 111e1051a39Sopenharmony_ci {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"}, 112e1051a39Sopenharmony_ci {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"}, 113e1051a39Sopenharmony_ci {"no-CAfile", OPT_NOCAFILE, '-', 114e1051a39Sopenharmony_ci "Do not load the default certificates file"}, 115e1051a39Sopenharmony_ci {"no-CApath", OPT_NOCAPATH, '-', 116e1051a39Sopenharmony_ci "Do not load certificates from the default certificates directory"}, 117e1051a39Sopenharmony_ci {"no-CAstore", OPT_NOCASTORE, '-', 118e1051a39Sopenharmony_ci "Do not load certificates from the default certificates store"}, 119e1051a39Sopenharmony_ci {"nochain", OPT_NOCHAIN, '-', 120e1051a39Sopenharmony_ci "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" }, 121e1051a39Sopenharmony_ci {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only"}, 122e1051a39Sopenharmony_ci 123e1051a39Sopenharmony_ci OPT_R_OPTIONS, 124e1051a39Sopenharmony_ci OPT_V_OPTIONS, 125e1051a39Sopenharmony_ci OPT_PROV_OPTIONS, 126e1051a39Sopenharmony_ci 127e1051a39Sopenharmony_ci OPT_PARAMETERS(), 128e1051a39Sopenharmony_ci {"cert", 0, 0, "Recipient certs, used when encrypting"}, 129e1051a39Sopenharmony_ci {NULL} 130e1051a39Sopenharmony_ci}; 131e1051a39Sopenharmony_ci 132e1051a39Sopenharmony_ciint smime_main(int argc, char **argv) 133e1051a39Sopenharmony_ci{ 134e1051a39Sopenharmony_ci CONF *conf = NULL; 135e1051a39Sopenharmony_ci BIO *in = NULL, *out = NULL, *indata = NULL; 136e1051a39Sopenharmony_ci EVP_PKEY *key = NULL; 137e1051a39Sopenharmony_ci PKCS7 *p7 = NULL; 138e1051a39Sopenharmony_ci STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL; 139e1051a39Sopenharmony_ci STACK_OF(X509) *encerts = NULL, *other = NULL; 140e1051a39Sopenharmony_ci X509 *cert = NULL, *recip = NULL, *signer = NULL; 141e1051a39Sopenharmony_ci X509_STORE *store = NULL; 142e1051a39Sopenharmony_ci X509_VERIFY_PARAM *vpm = NULL; 143e1051a39Sopenharmony_ci EVP_CIPHER *cipher = NULL; 144e1051a39Sopenharmony_ci EVP_MD *sign_md = NULL; 145e1051a39Sopenharmony_ci const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog = NULL; 146e1051a39Sopenharmony_ci char *certfile = NULL, *keyfile = NULL, *contfile = NULL; 147e1051a39Sopenharmony_ci char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL; 148e1051a39Sopenharmony_ci char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL; 149e1051a39Sopenharmony_ci char *subject = NULL, *digestname = NULL, *ciphername = NULL; 150e1051a39Sopenharmony_ci OPTION_CHOICE o; 151e1051a39Sopenharmony_ci int noCApath = 0, noCAfile = 0, noCAstore = 0; 152e1051a39Sopenharmony_ci int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0; 153e1051a39Sopenharmony_ci int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform = 154e1051a39Sopenharmony_ci FORMAT_UNDEF; 155e1051a39Sopenharmony_ci int vpmtouched = 0, rv = 0; 156e1051a39Sopenharmony_ci ENGINE *e = NULL; 157e1051a39Sopenharmony_ci const char *mime_eol = "\n"; 158e1051a39Sopenharmony_ci OSSL_LIB_CTX *libctx = app_get0_libctx(); 159e1051a39Sopenharmony_ci 160e1051a39Sopenharmony_ci if ((vpm = X509_VERIFY_PARAM_new()) == NULL) 161e1051a39Sopenharmony_ci return 1; 162e1051a39Sopenharmony_ci 163e1051a39Sopenharmony_ci prog = opt_init(argc, argv, smime_options); 164e1051a39Sopenharmony_ci while ((o = opt_next()) != OPT_EOF) { 165e1051a39Sopenharmony_ci switch (o) { 166e1051a39Sopenharmony_ci case OPT_EOF: 167e1051a39Sopenharmony_ci case OPT_ERR: 168e1051a39Sopenharmony_ci opthelp: 169e1051a39Sopenharmony_ci BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 170e1051a39Sopenharmony_ci goto end; 171e1051a39Sopenharmony_ci case OPT_HELP: 172e1051a39Sopenharmony_ci opt_help(smime_options); 173e1051a39Sopenharmony_ci ret = 0; 174e1051a39Sopenharmony_ci goto end; 175e1051a39Sopenharmony_ci case OPT_INFORM: 176e1051a39Sopenharmony_ci if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat)) 177e1051a39Sopenharmony_ci goto opthelp; 178e1051a39Sopenharmony_ci break; 179e1051a39Sopenharmony_ci case OPT_IN: 180e1051a39Sopenharmony_ci infile = opt_arg(); 181e1051a39Sopenharmony_ci break; 182e1051a39Sopenharmony_ci case OPT_OUTFORM: 183e1051a39Sopenharmony_ci if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat)) 184e1051a39Sopenharmony_ci goto opthelp; 185e1051a39Sopenharmony_ci break; 186e1051a39Sopenharmony_ci case OPT_OUT: 187e1051a39Sopenharmony_ci outfile = opt_arg(); 188e1051a39Sopenharmony_ci break; 189e1051a39Sopenharmony_ci case OPT_ENCRYPT: 190e1051a39Sopenharmony_ci operation = SMIME_ENCRYPT; 191e1051a39Sopenharmony_ci break; 192e1051a39Sopenharmony_ci case OPT_DECRYPT: 193e1051a39Sopenharmony_ci operation = SMIME_DECRYPT; 194e1051a39Sopenharmony_ci break; 195e1051a39Sopenharmony_ci case OPT_SIGN: 196e1051a39Sopenharmony_ci operation = SMIME_SIGN; 197e1051a39Sopenharmony_ci break; 198e1051a39Sopenharmony_ci case OPT_RESIGN: 199e1051a39Sopenharmony_ci operation = SMIME_RESIGN; 200e1051a39Sopenharmony_ci break; 201e1051a39Sopenharmony_ci case OPT_VERIFY: 202e1051a39Sopenharmony_ci operation = SMIME_VERIFY; 203e1051a39Sopenharmony_ci break; 204e1051a39Sopenharmony_ci case OPT_PK7OUT: 205e1051a39Sopenharmony_ci operation = SMIME_PK7OUT; 206e1051a39Sopenharmony_ci break; 207e1051a39Sopenharmony_ci case OPT_TEXT: 208e1051a39Sopenharmony_ci flags |= PKCS7_TEXT; 209e1051a39Sopenharmony_ci break; 210e1051a39Sopenharmony_ci case OPT_NOINTERN: 211e1051a39Sopenharmony_ci flags |= PKCS7_NOINTERN; 212e1051a39Sopenharmony_ci break; 213e1051a39Sopenharmony_ci case OPT_NOVERIFY: 214e1051a39Sopenharmony_ci flags |= PKCS7_NOVERIFY; 215e1051a39Sopenharmony_ci break; 216e1051a39Sopenharmony_ci case OPT_NOCHAIN: 217e1051a39Sopenharmony_ci flags |= PKCS7_NOCHAIN; 218e1051a39Sopenharmony_ci break; 219e1051a39Sopenharmony_ci case OPT_NOCERTS: 220e1051a39Sopenharmony_ci flags |= PKCS7_NOCERTS; 221e1051a39Sopenharmony_ci break; 222e1051a39Sopenharmony_ci case OPT_NOATTR: 223e1051a39Sopenharmony_ci flags |= PKCS7_NOATTR; 224e1051a39Sopenharmony_ci break; 225e1051a39Sopenharmony_ci case OPT_NODETACH: 226e1051a39Sopenharmony_ci flags &= ~PKCS7_DETACHED; 227e1051a39Sopenharmony_ci break; 228e1051a39Sopenharmony_ci case OPT_NOSMIMECAP: 229e1051a39Sopenharmony_ci flags |= PKCS7_NOSMIMECAP; 230e1051a39Sopenharmony_ci break; 231e1051a39Sopenharmony_ci case OPT_BINARY: 232e1051a39Sopenharmony_ci flags |= PKCS7_BINARY; 233e1051a39Sopenharmony_ci break; 234e1051a39Sopenharmony_ci case OPT_NOSIGS: 235e1051a39Sopenharmony_ci flags |= PKCS7_NOSIGS; 236e1051a39Sopenharmony_ci break; 237e1051a39Sopenharmony_ci case OPT_STREAM: 238e1051a39Sopenharmony_ci case OPT_INDEF: 239e1051a39Sopenharmony_ci indef = 1; 240e1051a39Sopenharmony_ci break; 241e1051a39Sopenharmony_ci case OPT_NOINDEF: 242e1051a39Sopenharmony_ci indef = 0; 243e1051a39Sopenharmony_ci break; 244e1051a39Sopenharmony_ci case OPT_CRLFEOL: 245e1051a39Sopenharmony_ci flags |= PKCS7_CRLFEOL; 246e1051a39Sopenharmony_ci mime_eol = "\r\n"; 247e1051a39Sopenharmony_ci break; 248e1051a39Sopenharmony_ci case OPT_R_CASES: 249e1051a39Sopenharmony_ci if (!opt_rand(o)) 250e1051a39Sopenharmony_ci goto end; 251e1051a39Sopenharmony_ci break; 252e1051a39Sopenharmony_ci case OPT_PROV_CASES: 253e1051a39Sopenharmony_ci if (!opt_provider(o)) 254e1051a39Sopenharmony_ci goto end; 255e1051a39Sopenharmony_ci break; 256e1051a39Sopenharmony_ci case OPT_CONFIG: 257e1051a39Sopenharmony_ci conf = app_load_config_modules(opt_arg()); 258e1051a39Sopenharmony_ci if (conf == NULL) 259e1051a39Sopenharmony_ci goto end; 260e1051a39Sopenharmony_ci break; 261e1051a39Sopenharmony_ci case OPT_ENGINE: 262e1051a39Sopenharmony_ci e = setup_engine(opt_arg(), 0); 263e1051a39Sopenharmony_ci break; 264e1051a39Sopenharmony_ci case OPT_PASSIN: 265e1051a39Sopenharmony_ci passinarg = opt_arg(); 266e1051a39Sopenharmony_ci break; 267e1051a39Sopenharmony_ci case OPT_TO: 268e1051a39Sopenharmony_ci to = opt_arg(); 269e1051a39Sopenharmony_ci break; 270e1051a39Sopenharmony_ci case OPT_FROM: 271e1051a39Sopenharmony_ci from = opt_arg(); 272e1051a39Sopenharmony_ci break; 273e1051a39Sopenharmony_ci case OPT_SUBJECT: 274e1051a39Sopenharmony_ci subject = opt_arg(); 275e1051a39Sopenharmony_ci break; 276e1051a39Sopenharmony_ci case OPT_SIGNER: 277e1051a39Sopenharmony_ci /* If previous -signer argument add signer to list */ 278e1051a39Sopenharmony_ci if (signerfile != NULL) { 279e1051a39Sopenharmony_ci if (sksigners == NULL 280e1051a39Sopenharmony_ci && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) 281e1051a39Sopenharmony_ci goto end; 282e1051a39Sopenharmony_ci sk_OPENSSL_STRING_push(sksigners, signerfile); 283e1051a39Sopenharmony_ci if (keyfile == NULL) 284e1051a39Sopenharmony_ci keyfile = signerfile; 285e1051a39Sopenharmony_ci if (skkeys == NULL 286e1051a39Sopenharmony_ci && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) 287e1051a39Sopenharmony_ci goto end; 288e1051a39Sopenharmony_ci sk_OPENSSL_STRING_push(skkeys, keyfile); 289e1051a39Sopenharmony_ci keyfile = NULL; 290e1051a39Sopenharmony_ci } 291e1051a39Sopenharmony_ci signerfile = opt_arg(); 292e1051a39Sopenharmony_ci break; 293e1051a39Sopenharmony_ci case OPT_RECIP: 294e1051a39Sopenharmony_ci recipfile = opt_arg(); 295e1051a39Sopenharmony_ci break; 296e1051a39Sopenharmony_ci case OPT_MD: 297e1051a39Sopenharmony_ci digestname = opt_arg(); 298e1051a39Sopenharmony_ci break; 299e1051a39Sopenharmony_ci case OPT_CIPHER: 300e1051a39Sopenharmony_ci ciphername = opt_unknown(); 301e1051a39Sopenharmony_ci break; 302e1051a39Sopenharmony_ci case OPT_INKEY: 303e1051a39Sopenharmony_ci /* If previous -inkey argument add signer to list */ 304e1051a39Sopenharmony_ci if (keyfile != NULL) { 305e1051a39Sopenharmony_ci if (signerfile == NULL) { 306e1051a39Sopenharmony_ci BIO_printf(bio_err, 307e1051a39Sopenharmony_ci "%s: Must have -signer before -inkey\n", prog); 308e1051a39Sopenharmony_ci goto opthelp; 309e1051a39Sopenharmony_ci } 310e1051a39Sopenharmony_ci if (sksigners == NULL 311e1051a39Sopenharmony_ci && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) 312e1051a39Sopenharmony_ci goto end; 313e1051a39Sopenharmony_ci sk_OPENSSL_STRING_push(sksigners, signerfile); 314e1051a39Sopenharmony_ci signerfile = NULL; 315e1051a39Sopenharmony_ci if (skkeys == NULL 316e1051a39Sopenharmony_ci && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) 317e1051a39Sopenharmony_ci goto end; 318e1051a39Sopenharmony_ci sk_OPENSSL_STRING_push(skkeys, keyfile); 319e1051a39Sopenharmony_ci } 320e1051a39Sopenharmony_ci keyfile = opt_arg(); 321e1051a39Sopenharmony_ci break; 322e1051a39Sopenharmony_ci case OPT_KEYFORM: 323e1051a39Sopenharmony_ci if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform)) 324e1051a39Sopenharmony_ci goto opthelp; 325e1051a39Sopenharmony_ci break; 326e1051a39Sopenharmony_ci case OPT_CERTFILE: 327e1051a39Sopenharmony_ci certfile = opt_arg(); 328e1051a39Sopenharmony_ci break; 329e1051a39Sopenharmony_ci case OPT_CAFILE: 330e1051a39Sopenharmony_ci CAfile = opt_arg(); 331e1051a39Sopenharmony_ci break; 332e1051a39Sopenharmony_ci case OPT_CAPATH: 333e1051a39Sopenharmony_ci CApath = opt_arg(); 334e1051a39Sopenharmony_ci break; 335e1051a39Sopenharmony_ci case OPT_CASTORE: 336e1051a39Sopenharmony_ci CAstore = opt_arg(); 337e1051a39Sopenharmony_ci break; 338e1051a39Sopenharmony_ci case OPT_NOCAFILE: 339e1051a39Sopenharmony_ci noCAfile = 1; 340e1051a39Sopenharmony_ci break; 341e1051a39Sopenharmony_ci case OPT_NOCAPATH: 342e1051a39Sopenharmony_ci noCApath = 1; 343e1051a39Sopenharmony_ci break; 344e1051a39Sopenharmony_ci case OPT_NOCASTORE: 345e1051a39Sopenharmony_ci noCAstore = 1; 346e1051a39Sopenharmony_ci break; 347e1051a39Sopenharmony_ci case OPT_CONTENT: 348e1051a39Sopenharmony_ci contfile = opt_arg(); 349e1051a39Sopenharmony_ci break; 350e1051a39Sopenharmony_ci case OPT_V_CASES: 351e1051a39Sopenharmony_ci if (!opt_verify(o, vpm)) 352e1051a39Sopenharmony_ci goto opthelp; 353e1051a39Sopenharmony_ci vpmtouched++; 354e1051a39Sopenharmony_ci break; 355e1051a39Sopenharmony_ci } 356e1051a39Sopenharmony_ci } 357e1051a39Sopenharmony_ci 358e1051a39Sopenharmony_ci /* Extra arguments are files with recipient keys. */ 359e1051a39Sopenharmony_ci argc = opt_num_rest(); 360e1051a39Sopenharmony_ci argv = opt_rest(); 361e1051a39Sopenharmony_ci 362e1051a39Sopenharmony_ci if (!app_RAND_load()) 363e1051a39Sopenharmony_ci goto end; 364e1051a39Sopenharmony_ci 365e1051a39Sopenharmony_ci if (digestname != NULL) { 366e1051a39Sopenharmony_ci if (!opt_md(digestname, &sign_md)) 367e1051a39Sopenharmony_ci goto opthelp; 368e1051a39Sopenharmony_ci } 369e1051a39Sopenharmony_ci if (ciphername != NULL) { 370e1051a39Sopenharmony_ci if (!opt_cipher_any(ciphername, &cipher)) 371e1051a39Sopenharmony_ci goto opthelp; 372e1051a39Sopenharmony_ci } 373e1051a39Sopenharmony_ci if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) { 374e1051a39Sopenharmony_ci BIO_puts(bio_err, "Multiple signers or keys not allowed\n"); 375e1051a39Sopenharmony_ci goto opthelp; 376e1051a39Sopenharmony_ci } 377e1051a39Sopenharmony_ci if (!operation) { 378e1051a39Sopenharmony_ci BIO_puts(bio_err, 379e1051a39Sopenharmony_ci "No operation (-encrypt|-sign|...) specified\n"); 380e1051a39Sopenharmony_ci goto opthelp; 381e1051a39Sopenharmony_ci } 382e1051a39Sopenharmony_ci 383e1051a39Sopenharmony_ci if (operation & SMIME_SIGNERS) { 384e1051a39Sopenharmony_ci /* Check to see if any final signer needs to be appended */ 385e1051a39Sopenharmony_ci if (keyfile && !signerfile) { 386e1051a39Sopenharmony_ci BIO_puts(bio_err, "Illegal -inkey without -signer\n"); 387e1051a39Sopenharmony_ci goto opthelp; 388e1051a39Sopenharmony_ci } 389e1051a39Sopenharmony_ci if (signerfile != NULL) { 390e1051a39Sopenharmony_ci if (sksigners == NULL 391e1051a39Sopenharmony_ci && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) 392e1051a39Sopenharmony_ci goto end; 393e1051a39Sopenharmony_ci sk_OPENSSL_STRING_push(sksigners, signerfile); 394e1051a39Sopenharmony_ci if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) 395e1051a39Sopenharmony_ci goto end; 396e1051a39Sopenharmony_ci if (!keyfile) 397e1051a39Sopenharmony_ci keyfile = signerfile; 398e1051a39Sopenharmony_ci sk_OPENSSL_STRING_push(skkeys, keyfile); 399e1051a39Sopenharmony_ci } 400e1051a39Sopenharmony_ci if (sksigners == NULL) { 401e1051a39Sopenharmony_ci BIO_printf(bio_err, "No signer certificate specified\n"); 402e1051a39Sopenharmony_ci goto opthelp; 403e1051a39Sopenharmony_ci } 404e1051a39Sopenharmony_ci signerfile = NULL; 405e1051a39Sopenharmony_ci keyfile = NULL; 406e1051a39Sopenharmony_ci } else if (operation == SMIME_DECRYPT) { 407e1051a39Sopenharmony_ci if (recipfile == NULL && keyfile == NULL) { 408e1051a39Sopenharmony_ci BIO_printf(bio_err, 409e1051a39Sopenharmony_ci "No recipient certificate or key specified\n"); 410e1051a39Sopenharmony_ci goto opthelp; 411e1051a39Sopenharmony_ci } 412e1051a39Sopenharmony_ci } else if (operation == SMIME_ENCRYPT) { 413e1051a39Sopenharmony_ci if (argc == 0) { 414e1051a39Sopenharmony_ci BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n"); 415e1051a39Sopenharmony_ci goto opthelp; 416e1051a39Sopenharmony_ci } 417e1051a39Sopenharmony_ci } 418e1051a39Sopenharmony_ci 419e1051a39Sopenharmony_ci if (!app_passwd(passinarg, NULL, &passin, NULL)) { 420e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error getting password\n"); 421e1051a39Sopenharmony_ci goto end; 422e1051a39Sopenharmony_ci } 423e1051a39Sopenharmony_ci 424e1051a39Sopenharmony_ci ret = 2; 425e1051a39Sopenharmony_ci 426e1051a39Sopenharmony_ci if (!(operation & SMIME_SIGNERS)) 427e1051a39Sopenharmony_ci flags &= ~PKCS7_DETACHED; 428e1051a39Sopenharmony_ci 429e1051a39Sopenharmony_ci if (!(operation & SMIME_OP)) { 430e1051a39Sopenharmony_ci if (flags & PKCS7_BINARY) 431e1051a39Sopenharmony_ci outformat = FORMAT_BINARY; 432e1051a39Sopenharmony_ci } 433e1051a39Sopenharmony_ci 434e1051a39Sopenharmony_ci if (!(operation & SMIME_IP)) { 435e1051a39Sopenharmony_ci if (flags & PKCS7_BINARY) 436e1051a39Sopenharmony_ci informat = FORMAT_BINARY; 437e1051a39Sopenharmony_ci } 438e1051a39Sopenharmony_ci 439e1051a39Sopenharmony_ci if (operation == SMIME_ENCRYPT) { 440e1051a39Sopenharmony_ci if (cipher == NULL) { 441e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DES 442e1051a39Sopenharmony_ci cipher = (EVP_CIPHER *)EVP_des_ede3_cbc(); 443e1051a39Sopenharmony_ci#else 444e1051a39Sopenharmony_ci BIO_printf(bio_err, "No cipher selected\n"); 445e1051a39Sopenharmony_ci goto end; 446e1051a39Sopenharmony_ci#endif 447e1051a39Sopenharmony_ci } 448e1051a39Sopenharmony_ci encerts = sk_X509_new_null(); 449e1051a39Sopenharmony_ci if (encerts == NULL) 450e1051a39Sopenharmony_ci goto end; 451e1051a39Sopenharmony_ci while (*argv != NULL) { 452e1051a39Sopenharmony_ci cert = load_cert(*argv, FORMAT_UNDEF, 453e1051a39Sopenharmony_ci "recipient certificate file"); 454e1051a39Sopenharmony_ci if (cert == NULL) 455e1051a39Sopenharmony_ci goto end; 456e1051a39Sopenharmony_ci sk_X509_push(encerts, cert); 457e1051a39Sopenharmony_ci cert = NULL; 458e1051a39Sopenharmony_ci argv++; 459e1051a39Sopenharmony_ci } 460e1051a39Sopenharmony_ci } 461e1051a39Sopenharmony_ci 462e1051a39Sopenharmony_ci if (certfile != NULL) { 463e1051a39Sopenharmony_ci if (!load_certs(certfile, 0, &other, NULL, "certificates")) { 464e1051a39Sopenharmony_ci ERR_print_errors(bio_err); 465e1051a39Sopenharmony_ci goto end; 466e1051a39Sopenharmony_ci } 467e1051a39Sopenharmony_ci } 468e1051a39Sopenharmony_ci 469e1051a39Sopenharmony_ci if (recipfile != NULL && (operation == SMIME_DECRYPT)) { 470e1051a39Sopenharmony_ci if ((recip = load_cert(recipfile, FORMAT_UNDEF, 471e1051a39Sopenharmony_ci "recipient certificate file")) == NULL) { 472e1051a39Sopenharmony_ci ERR_print_errors(bio_err); 473e1051a39Sopenharmony_ci goto end; 474e1051a39Sopenharmony_ci } 475e1051a39Sopenharmony_ci } 476e1051a39Sopenharmony_ci 477e1051a39Sopenharmony_ci if (operation == SMIME_DECRYPT) { 478e1051a39Sopenharmony_ci if (keyfile == NULL) 479e1051a39Sopenharmony_ci keyfile = recipfile; 480e1051a39Sopenharmony_ci } else if (operation == SMIME_SIGN) { 481e1051a39Sopenharmony_ci if (keyfile == NULL) 482e1051a39Sopenharmony_ci keyfile = signerfile; 483e1051a39Sopenharmony_ci } else { 484e1051a39Sopenharmony_ci keyfile = NULL; 485e1051a39Sopenharmony_ci } 486e1051a39Sopenharmony_ci 487e1051a39Sopenharmony_ci if (keyfile != NULL) { 488e1051a39Sopenharmony_ci key = load_key(keyfile, keyform, 0, passin, e, "signing key"); 489e1051a39Sopenharmony_ci if (key == NULL) 490e1051a39Sopenharmony_ci goto end; 491e1051a39Sopenharmony_ci } 492e1051a39Sopenharmony_ci 493e1051a39Sopenharmony_ci in = bio_open_default(infile, 'r', informat); 494e1051a39Sopenharmony_ci if (in == NULL) 495e1051a39Sopenharmony_ci goto end; 496e1051a39Sopenharmony_ci 497e1051a39Sopenharmony_ci if (operation & SMIME_IP) { 498e1051a39Sopenharmony_ci PKCS7 *p7_in = NULL; 499e1051a39Sopenharmony_ci 500e1051a39Sopenharmony_ci p7 = PKCS7_new_ex(libctx, app_get0_propq()); 501e1051a39Sopenharmony_ci if (p7 == NULL) { 502e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error allocating PKCS7 object\n"); 503e1051a39Sopenharmony_ci goto end; 504e1051a39Sopenharmony_ci } 505e1051a39Sopenharmony_ci if (informat == FORMAT_SMIME) { 506e1051a39Sopenharmony_ci p7_in = SMIME_read_PKCS7_ex(in, &indata, &p7); 507e1051a39Sopenharmony_ci } else if (informat == FORMAT_PEM) { 508e1051a39Sopenharmony_ci p7_in = PEM_read_bio_PKCS7(in, &p7, NULL, NULL); 509e1051a39Sopenharmony_ci } else if (informat == FORMAT_ASN1) { 510e1051a39Sopenharmony_ci p7_in = d2i_PKCS7_bio(in, &p7); 511e1051a39Sopenharmony_ci } else { 512e1051a39Sopenharmony_ci BIO_printf(bio_err, "Bad input format for PKCS#7 file\n"); 513e1051a39Sopenharmony_ci goto end; 514e1051a39Sopenharmony_ci } 515e1051a39Sopenharmony_ci 516e1051a39Sopenharmony_ci if (p7_in == NULL) { 517e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error reading S/MIME message\n"); 518e1051a39Sopenharmony_ci goto end; 519e1051a39Sopenharmony_ci } 520e1051a39Sopenharmony_ci if (contfile != NULL) { 521e1051a39Sopenharmony_ci BIO_free(indata); 522e1051a39Sopenharmony_ci if ((indata = BIO_new_file(contfile, "rb")) == NULL) { 523e1051a39Sopenharmony_ci BIO_printf(bio_err, "Can't read content file %s\n", contfile); 524e1051a39Sopenharmony_ci goto end; 525e1051a39Sopenharmony_ci } 526e1051a39Sopenharmony_ci } 527e1051a39Sopenharmony_ci } 528e1051a39Sopenharmony_ci 529e1051a39Sopenharmony_ci out = bio_open_default(outfile, 'w', outformat); 530e1051a39Sopenharmony_ci if (out == NULL) 531e1051a39Sopenharmony_ci goto end; 532e1051a39Sopenharmony_ci 533e1051a39Sopenharmony_ci if (operation == SMIME_VERIFY) { 534e1051a39Sopenharmony_ci if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath, 535e1051a39Sopenharmony_ci CAstore, noCAstore)) == NULL) 536e1051a39Sopenharmony_ci goto end; 537e1051a39Sopenharmony_ci X509_STORE_set_verify_cb(store, smime_cb); 538e1051a39Sopenharmony_ci if (vpmtouched) 539e1051a39Sopenharmony_ci X509_STORE_set1_param(store, vpm); 540e1051a39Sopenharmony_ci } 541e1051a39Sopenharmony_ci 542e1051a39Sopenharmony_ci ret = 3; 543e1051a39Sopenharmony_ci 544e1051a39Sopenharmony_ci if (operation == SMIME_ENCRYPT) { 545e1051a39Sopenharmony_ci if (indef) 546e1051a39Sopenharmony_ci flags |= PKCS7_STREAM; 547e1051a39Sopenharmony_ci p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq()); 548e1051a39Sopenharmony_ci } else if (operation & SMIME_SIGNERS) { 549e1051a39Sopenharmony_ci int i; 550e1051a39Sopenharmony_ci /* 551e1051a39Sopenharmony_ci * If detached data content we only enable streaming if S/MIME output 552e1051a39Sopenharmony_ci * format. 553e1051a39Sopenharmony_ci */ 554e1051a39Sopenharmony_ci if (operation == SMIME_SIGN) { 555e1051a39Sopenharmony_ci if (flags & PKCS7_DETACHED) { 556e1051a39Sopenharmony_ci if (outformat == FORMAT_SMIME) 557e1051a39Sopenharmony_ci flags |= PKCS7_STREAM; 558e1051a39Sopenharmony_ci } else if (indef) { 559e1051a39Sopenharmony_ci flags |= PKCS7_STREAM; 560e1051a39Sopenharmony_ci } 561e1051a39Sopenharmony_ci flags |= PKCS7_PARTIAL; 562e1051a39Sopenharmony_ci p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq()); 563e1051a39Sopenharmony_ci if (p7 == NULL) 564e1051a39Sopenharmony_ci goto end; 565e1051a39Sopenharmony_ci if (flags & PKCS7_NOCERTS) { 566e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_num(other); i++) { 567e1051a39Sopenharmony_ci X509 *x = sk_X509_value(other, i); 568e1051a39Sopenharmony_ci PKCS7_add_certificate(p7, x); 569e1051a39Sopenharmony_ci } 570e1051a39Sopenharmony_ci } 571e1051a39Sopenharmony_ci } else { 572e1051a39Sopenharmony_ci flags |= PKCS7_REUSE_DIGEST; 573e1051a39Sopenharmony_ci } 574e1051a39Sopenharmony_ci for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) { 575e1051a39Sopenharmony_ci signerfile = sk_OPENSSL_STRING_value(sksigners, i); 576e1051a39Sopenharmony_ci keyfile = sk_OPENSSL_STRING_value(skkeys, i); 577e1051a39Sopenharmony_ci signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate"); 578e1051a39Sopenharmony_ci if (signer == NULL) 579e1051a39Sopenharmony_ci goto end; 580e1051a39Sopenharmony_ci key = load_key(keyfile, keyform, 0, passin, e, "signing key"); 581e1051a39Sopenharmony_ci if (key == NULL) 582e1051a39Sopenharmony_ci goto end; 583e1051a39Sopenharmony_ci 584e1051a39Sopenharmony_ci if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags)) 585e1051a39Sopenharmony_ci goto end; 586e1051a39Sopenharmony_ci X509_free(signer); 587e1051a39Sopenharmony_ci signer = NULL; 588e1051a39Sopenharmony_ci EVP_PKEY_free(key); 589e1051a39Sopenharmony_ci key = NULL; 590e1051a39Sopenharmony_ci } 591e1051a39Sopenharmony_ci /* If not streaming or resigning finalize structure */ 592e1051a39Sopenharmony_ci if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) { 593e1051a39Sopenharmony_ci if (!PKCS7_final(p7, in, flags)) 594e1051a39Sopenharmony_ci goto end; 595e1051a39Sopenharmony_ci } 596e1051a39Sopenharmony_ci } 597e1051a39Sopenharmony_ci 598e1051a39Sopenharmony_ci if (p7 == NULL) { 599e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error creating PKCS#7 structure\n"); 600e1051a39Sopenharmony_ci goto end; 601e1051a39Sopenharmony_ci } 602e1051a39Sopenharmony_ci 603e1051a39Sopenharmony_ci ret = 4; 604e1051a39Sopenharmony_ci if (operation == SMIME_DECRYPT) { 605e1051a39Sopenharmony_ci if (!PKCS7_decrypt(p7, key, recip, out, flags)) { 606e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n"); 607e1051a39Sopenharmony_ci goto end; 608e1051a39Sopenharmony_ci } 609e1051a39Sopenharmony_ci } else if (operation == SMIME_VERIFY) { 610e1051a39Sopenharmony_ci STACK_OF(X509) *signers; 611e1051a39Sopenharmony_ci if (PKCS7_verify(p7, other, store, indata, out, flags)) 612e1051a39Sopenharmony_ci BIO_printf(bio_err, "Verification successful\n"); 613e1051a39Sopenharmony_ci else { 614e1051a39Sopenharmony_ci BIO_printf(bio_err, "Verification failure\n"); 615e1051a39Sopenharmony_ci goto end; 616e1051a39Sopenharmony_ci } 617e1051a39Sopenharmony_ci signers = PKCS7_get0_signers(p7, other, flags); 618e1051a39Sopenharmony_ci if (!save_certs(signerfile, signers)) { 619e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error writing signers to %s\n", signerfile); 620e1051a39Sopenharmony_ci ret = 5; 621e1051a39Sopenharmony_ci goto end; 622e1051a39Sopenharmony_ci } 623e1051a39Sopenharmony_ci sk_X509_free(signers); 624e1051a39Sopenharmony_ci } else if (operation == SMIME_PK7OUT) { 625e1051a39Sopenharmony_ci PEM_write_bio_PKCS7(out, p7); 626e1051a39Sopenharmony_ci } else { 627e1051a39Sopenharmony_ci if (to) 628e1051a39Sopenharmony_ci BIO_printf(out, "To: %s%s", to, mime_eol); 629e1051a39Sopenharmony_ci if (from) 630e1051a39Sopenharmony_ci BIO_printf(out, "From: %s%s", from, mime_eol); 631e1051a39Sopenharmony_ci if (subject) 632e1051a39Sopenharmony_ci BIO_printf(out, "Subject: %s%s", subject, mime_eol); 633e1051a39Sopenharmony_ci if (outformat == FORMAT_SMIME) { 634e1051a39Sopenharmony_ci if (operation == SMIME_RESIGN) 635e1051a39Sopenharmony_ci rv = SMIME_write_PKCS7(out, p7, indata, flags); 636e1051a39Sopenharmony_ci else 637e1051a39Sopenharmony_ci rv = SMIME_write_PKCS7(out, p7, in, flags); 638e1051a39Sopenharmony_ci } else if (outformat == FORMAT_PEM) { 639e1051a39Sopenharmony_ci rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags); 640e1051a39Sopenharmony_ci } else if (outformat == FORMAT_ASN1) { 641e1051a39Sopenharmony_ci rv = i2d_PKCS7_bio_stream(out, p7, in, flags); 642e1051a39Sopenharmony_ci } else { 643e1051a39Sopenharmony_ci BIO_printf(bio_err, "Bad output format for PKCS#7 file\n"); 644e1051a39Sopenharmony_ci goto end; 645e1051a39Sopenharmony_ci } 646e1051a39Sopenharmony_ci if (rv == 0) { 647e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error writing output\n"); 648e1051a39Sopenharmony_ci ret = 3; 649e1051a39Sopenharmony_ci goto end; 650e1051a39Sopenharmony_ci } 651e1051a39Sopenharmony_ci } 652e1051a39Sopenharmony_ci ret = 0; 653e1051a39Sopenharmony_ci end: 654e1051a39Sopenharmony_ci if (ret) 655e1051a39Sopenharmony_ci ERR_print_errors(bio_err); 656e1051a39Sopenharmony_ci sk_X509_pop_free(encerts, X509_free); 657e1051a39Sopenharmony_ci sk_X509_pop_free(other, X509_free); 658e1051a39Sopenharmony_ci X509_VERIFY_PARAM_free(vpm); 659e1051a39Sopenharmony_ci sk_OPENSSL_STRING_free(sksigners); 660e1051a39Sopenharmony_ci sk_OPENSSL_STRING_free(skkeys); 661e1051a39Sopenharmony_ci X509_STORE_free(store); 662e1051a39Sopenharmony_ci X509_free(cert); 663e1051a39Sopenharmony_ci X509_free(recip); 664e1051a39Sopenharmony_ci X509_free(signer); 665e1051a39Sopenharmony_ci EVP_PKEY_free(key); 666e1051a39Sopenharmony_ci EVP_MD_free(sign_md); 667e1051a39Sopenharmony_ci EVP_CIPHER_free(cipher); 668e1051a39Sopenharmony_ci PKCS7_free(p7); 669e1051a39Sopenharmony_ci release_engine(e); 670e1051a39Sopenharmony_ci BIO_free(in); 671e1051a39Sopenharmony_ci BIO_free(indata); 672e1051a39Sopenharmony_ci BIO_free_all(out); 673e1051a39Sopenharmony_ci OPENSSL_free(passin); 674e1051a39Sopenharmony_ci NCONF_free(conf); 675e1051a39Sopenharmony_ci return ret; 676e1051a39Sopenharmony_ci} 677e1051a39Sopenharmony_ci 678e1051a39Sopenharmony_cistatic int save_certs(char *signerfile, STACK_OF(X509) *signers) 679e1051a39Sopenharmony_ci{ 680e1051a39Sopenharmony_ci int i; 681e1051a39Sopenharmony_ci BIO *tmp; 682e1051a39Sopenharmony_ci 683e1051a39Sopenharmony_ci if (signerfile == NULL) 684e1051a39Sopenharmony_ci return 1; 685e1051a39Sopenharmony_ci tmp = BIO_new_file(signerfile, "w"); 686e1051a39Sopenharmony_ci if (tmp == NULL) 687e1051a39Sopenharmony_ci return 0; 688e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_num(signers); i++) 689e1051a39Sopenharmony_ci PEM_write_bio_X509(tmp, sk_X509_value(signers, i)); 690e1051a39Sopenharmony_ci BIO_free(tmp); 691e1051a39Sopenharmony_ci return 1; 692e1051a39Sopenharmony_ci} 693e1051a39Sopenharmony_ci 694e1051a39Sopenharmony_ci/* Minimal callback just to output policy info (if any) */ 695e1051a39Sopenharmony_ci 696e1051a39Sopenharmony_cistatic int smime_cb(int ok, X509_STORE_CTX *ctx) 697e1051a39Sopenharmony_ci{ 698e1051a39Sopenharmony_ci int error; 699e1051a39Sopenharmony_ci 700e1051a39Sopenharmony_ci error = X509_STORE_CTX_get_error(ctx); 701e1051a39Sopenharmony_ci 702e1051a39Sopenharmony_ci if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) 703e1051a39Sopenharmony_ci && ((error != X509_V_OK) || (ok != 2))) 704e1051a39Sopenharmony_ci return ok; 705e1051a39Sopenharmony_ci 706e1051a39Sopenharmony_ci policies_print(ctx); 707e1051a39Sopenharmony_ci 708e1051a39Sopenharmony_ci return ok; 709e1051a39Sopenharmony_ci} 710