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