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