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