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