]>
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 | |
db50661f DSH |
98 | int verify_err = 0; |
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, | |
121 | OPT_CAPATH, OPT_CONTENT, OPT_PRINT, OPT_SECRETKEY, | |
122 | OPT_SECRETKEYID, OPT_PWRI_PASSWORD, OPT_ECONTENT_TYPE, OPT_RAND, | |
123 | OPT_PASSIN, OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, | |
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"}, | |
188 | {"content", OPT_CONTENT, '<', | |
189 | "Supply or override content for detached signature"}, | |
190 | {"print", OPT_PRINT, '-'}, | |
191 | {"secretkey", OPT_SECRETKEY, 's'}, | |
192 | {"secretkeyid", OPT_SECRETKEYID, 's'}, | |
193 | {"pwri_password", OPT_PWRI_PASSWORD, 's'}, | |
194 | {"econtent_type", OPT_ECONTENT_TYPE, 's'}, | |
195 | {"rand", OPT_RAND, 's', | |
196 | "Load the file(s) into the random number generator"}, | |
197 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, | |
198 | {"to", OPT_TO, 's', "To address"}, | |
199 | {"from", OPT_FROM, 's', "From address"}, | |
200 | {"subject", OPT_SUBJECT, 's', "Subject"}, | |
201 | {"signer", OPT_SIGNER, 's', "Signer certificate file"}, | |
202 | {"recip", OPT_RECIP, '<', "Recipient cert file for decryption"}, | |
203 | {"certsout", OPT_CERTSOUT, '>', "Certificate output file"}, | |
204 | {"md", OPT_MD, 's'}, | |
205 | {"inkey", OPT_INKEY, '<', | |
206 | "Input private key (if not signer or recipient)"}, | |
207 | {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"}, | |
208 | {"keyopt", OPT_KEYOPT, 's', "Set public key parameters as n:v pairs"}, | |
209 | {"receipt_request_from", OPT_RR_FROM, 's'}, | |
210 | {"receipt_request_to", OPT_RR_TO, 's'}, | |
9c3bcfa0 RS |
211 | {"", OPT_CIPHER, '-', "Any supported cipher"}, |
212 | OPT_V_OPTIONS, | |
7e1b7485 RS |
213 | # ifndef OPENSSL_NO_AES |
214 | {"aes128-wrap", OPT_AES128_WRAP, '-', "Use AES128 to wrap key"}, | |
215 | {"aes192-wrap", OPT_AES192_WRAP, '-', "Use AES192 to wrap key"}, | |
216 | {"aes256-wrap", OPT_AES256_WRAP, '-', "Use AES256 to wrap key"}, | |
217 | # endif | |
218 | # ifndef OPENSSL_NO_DES | |
219 | {"des3-wrap", OPT_3DES_WRAP, '-', "Use 3DES-EDE to wrap key"}, | |
220 | # endif | |
221 | # ifndef OPENSSL_NO_ENGINE | |
222 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, | |
223 | # endif | |
9c3bcfa0 | 224 | {NULL} |
7e1b7485 | 225 | }; |
8931b30d | 226 | |
7e1b7485 | 227 | int cms_main(int argc, char **argv) |
0f113f3e | 228 | { |
7e1b7485 | 229 | ASN1_OBJECT *econtent_type = NULL; |
0f113f3e | 230 | BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL; |
7e1b7485 | 231 | CMS_ContentInfo *cms = NULL, *rcms = NULL; |
0f113f3e | 232 | CMS_ReceiptRequest *rr = NULL; |
7e1b7485 RS |
233 | ENGINE *e = NULL; |
234 | EVP_PKEY *key = NULL; | |
235 | const EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL; | |
0f113f3e | 236 | const EVP_MD *sign_md = NULL; |
7e1b7485 RS |
237 | STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL; |
238 | STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL; | |
239 | STACK_OF(X509) *encerts = NULL, *other = NULL; | |
240 | X509 *cert = NULL, *recip = NULL, *signer = NULL; | |
241 | X509_STORE *store = NULL; | |
242 | X509_VERIFY_PARAM *vpm = NULL; | |
243 | char *certfile = NULL, *keyfile = NULL, *contfile = NULL; | |
333b070e | 244 | char *CAfile = NULL, *CApath = NULL, *certsoutfile = NULL; |
7e1b7485 RS |
245 | char *infile = NULL, *outfile = NULL, *rctfile = NULL, *inrand = NULL; |
246 | char *passinarg = NULL, *passin = NULL, *signerfile = NULL, *recipfile = | |
247 | NULL; | |
248 | char *to = NULL, *from = NULL, *subject = NULL, *prog; | |
249 | cms_key_param *key_first = NULL, *key_param = NULL; | |
250 | const char *inmode = "r", *outmode = "w"; | |
251 | int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched = | |
252 | 0; | |
0f113f3e | 253 | int informat = FORMAT_SMIME, outformat = FORMAT_SMIME; |
7e1b7485 RS |
254 | int need_rand = 0, operation = 0, ret = 1, rr_print = 0, rr_allorfirst = |
255 | -1; | |
256 | int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_PEM; | |
0f113f3e | 257 | size_t secret_keylen = 0, secret_keyidlen = 0; |
7e1b7485 RS |
258 | unsigned char *pwri_pass = NULL, *pwri_tmp = NULL; |
259 | unsigned char *secret_key = NULL, *secret_keyid = NULL; | |
260 | long ltmp; | |
261 | OPTION_CHOICE o; | |
0f113f3e | 262 | |
7e1b7485 RS |
263 | if ((vpm = X509_VERIFY_PARAM_new()) == NULL) |
264 | return 1; | |
0f113f3e | 265 | |
7e1b7485 RS |
266 | prog = opt_init(argc, argv, cms_options); |
267 | while ((o = opt_next()) != OPT_EOF) { | |
268 | switch (o) { | |
269 | case OPT_EOF: | |
270 | case OPT_ERR: | |
271 | opthelp: | |
272 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
273 | goto end; | |
274 | case OPT_HELP: | |
275 | opt_help(cms_options); | |
276 | ret = 0; | |
277 | goto end; | |
278 | case OPT_INFORM: | |
279 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
280 | goto opthelp; | |
281 | break; | |
282 | case OPT_OUTFORM: | |
283 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
284 | goto opthelp; | |
285 | break; | |
286 | case OPT_OUT: | |
287 | outfile = opt_arg(); | |
288 | break; | |
289 | case OPT_ENCRYPT: | |
0f113f3e | 290 | operation = SMIME_ENCRYPT; |
7e1b7485 RS |
291 | break; |
292 | case OPT_DECRYPT: | |
0f113f3e | 293 | operation = SMIME_DECRYPT; |
7e1b7485 RS |
294 | break; |
295 | case OPT_SIGN: | |
0f113f3e | 296 | operation = SMIME_SIGN; |
7e1b7485 RS |
297 | break; |
298 | case OPT_SIGN_RECEIPT: | |
0f113f3e | 299 | operation = SMIME_SIGN_RECEIPT; |
7e1b7485 RS |
300 | break; |
301 | case OPT_RESIGN: | |
0f113f3e | 302 | operation = SMIME_RESIGN; |
7e1b7485 RS |
303 | break; |
304 | case OPT_VERIFY: | |
0f113f3e | 305 | operation = SMIME_VERIFY; |
7e1b7485 RS |
306 | break; |
307 | case OPT_VERIFY_RETCODE: | |
0f113f3e | 308 | verify_retcode = 1; |
7e1b7485 RS |
309 | break; |
310 | case OPT_VERIFY_RECEIPT: | |
0f113f3e | 311 | operation = SMIME_VERIFY_RECEIPT; |
7e1b7485 RS |
312 | rctfile = opt_arg(); |
313 | break; | |
314 | case OPT_CMSOUT: | |
0f113f3e | 315 | operation = SMIME_CMSOUT; |
7e1b7485 RS |
316 | break; |
317 | case OPT_DATA_OUT: | |
0f113f3e | 318 | operation = SMIME_DATAOUT; |
7e1b7485 RS |
319 | break; |
320 | case OPT_DATA_CREATE: | |
0f113f3e | 321 | operation = SMIME_DATA_CREATE; |
7e1b7485 RS |
322 | break; |
323 | case OPT_DIGEST_VERIFY: | |
0f113f3e | 324 | operation = SMIME_DIGEST_VERIFY; |
7e1b7485 RS |
325 | break; |
326 | case OPT_DIGEST_CREATE: | |
0f113f3e | 327 | operation = SMIME_DIGEST_CREATE; |
7e1b7485 RS |
328 | break; |
329 | case OPT_COMPRESS: | |
0f113f3e | 330 | operation = SMIME_COMPRESS; |
7e1b7485 RS |
331 | break; |
332 | case OPT_UNCOMPRESS: | |
0f113f3e | 333 | operation = SMIME_UNCOMPRESS; |
7e1b7485 RS |
334 | break; |
335 | case OPT_ED_DECRYPT: | |
0f113f3e | 336 | operation = SMIME_ENCRYPTED_DECRYPT; |
7e1b7485 RS |
337 | break; |
338 | case OPT_ED_ENCRYPT: | |
0f113f3e | 339 | operation = SMIME_ENCRYPTED_ENCRYPT; |
7e1b7485 RS |
340 | break; |
341 | case OPT_DEBUG_DECRYPT: | |
0f113f3e | 342 | flags |= CMS_DEBUG_DECRYPT; |
7e1b7485 RS |
343 | break; |
344 | case OPT_TEXT: | |
0f113f3e | 345 | flags |= CMS_TEXT; |
7e1b7485 RS |
346 | break; |
347 | case OPT_ASCIICRLF: | |
0f113f3e | 348 | flags |= CMS_ASCIICRLF; |
7e1b7485 RS |
349 | break; |
350 | case OPT_NOINTERN: | |
0f113f3e | 351 | flags |= CMS_NOINTERN; |
7e1b7485 RS |
352 | break; |
353 | case OPT_NOVERIFY: | |
0f113f3e | 354 | flags |= CMS_NO_SIGNER_CERT_VERIFY; |
7e1b7485 RS |
355 | break; |
356 | case OPT_NOCERTS: | |
0f113f3e | 357 | flags |= CMS_NOCERTS; |
7e1b7485 RS |
358 | break; |
359 | case OPT_NOATTR: | |
0f113f3e | 360 | flags |= CMS_NOATTR; |
7e1b7485 RS |
361 | break; |
362 | case OPT_NODETACH: | |
0f113f3e | 363 | flags &= ~CMS_DETACHED; |
7e1b7485 RS |
364 | break; |
365 | case OPT_NOSMIMECAP: | |
0f113f3e | 366 | flags |= CMS_NOSMIMECAP; |
7e1b7485 RS |
367 | break; |
368 | case OPT_BINARY: | |
0f113f3e | 369 | flags |= CMS_BINARY; |
7e1b7485 RS |
370 | break; |
371 | case OPT_KEYID: | |
0f113f3e | 372 | flags |= CMS_USE_KEYID; |
7e1b7485 RS |
373 | break; |
374 | case OPT_NOSIGS: | |
0f113f3e | 375 | flags |= CMS_NOSIGS; |
7e1b7485 RS |
376 | break; |
377 | case OPT_NO_CONTENT_VERIFY: | |
0f113f3e | 378 | flags |= CMS_NO_CONTENT_VERIFY; |
7e1b7485 RS |
379 | break; |
380 | case OPT_NO_ATTR_VERIFY: | |
0f113f3e | 381 | flags |= CMS_NO_ATTR_VERIFY; |
7e1b7485 RS |
382 | break; |
383 | case OPT_INDEF: | |
0f113f3e | 384 | flags |= CMS_STREAM; |
7e1b7485 RS |
385 | break; |
386 | case OPT_NOINDEF: | |
0f113f3e | 387 | flags &= ~CMS_STREAM; |
7e1b7485 RS |
388 | break; |
389 | case OPT_NOOLDMIME: | |
0f113f3e | 390 | flags |= CMS_NOOLDMIMETYPE; |
7e1b7485 RS |
391 | break; |
392 | case OPT_CRLFEOL: | |
0f113f3e | 393 | flags |= CMS_CRLFEOL; |
7e1b7485 RS |
394 | break; |
395 | case OPT_NOOUT: | |
0f113f3e | 396 | noout = 1; |
7e1b7485 RS |
397 | break; |
398 | case OPT_RR_PRINT: | |
0f113f3e | 399 | rr_print = 1; |
7e1b7485 RS |
400 | break; |
401 | case OPT_RR_ALL: | |
0f113f3e | 402 | rr_allorfirst = 0; |
7e1b7485 RS |
403 | break; |
404 | case OPT_RR_FIRST: | |
0f113f3e | 405 | rr_allorfirst = 1; |
7e1b7485 RS |
406 | break; |
407 | case OPT_RCTFORM: | |
408 | if (rctformat == FORMAT_SMIME) | |
409 | rcms = SMIME_read_CMS(rctin, NULL); | |
410 | else if (rctformat == FORMAT_PEM) | |
411 | rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL); | |
412 | else if (rctformat == FORMAT_ASN1) | |
413 | if (!opt_format(opt_arg(), | |
414 | OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat)) | |
415 | goto opthelp; | |
416 | break; | |
417 | case OPT_CERTFILE: | |
418 | certfile = opt_arg(); | |
419 | break; | |
420 | case OPT_CAFILE: | |
421 | CAfile = opt_arg(); | |
422 | break; | |
423 | case OPT_CAPATH: | |
424 | CApath = opt_arg(); | |
425 | break; | |
426 | case OPT_IN: | |
427 | infile = opt_arg(); | |
428 | break; | |
429 | case OPT_CONTENT: | |
430 | contfile = opt_arg(); | |
431 | break; | |
432 | case OPT_RR_FROM: | |
433 | if (rr_from == NULL | |
434 | && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL) | |
435 | goto end; | |
436 | sk_OPENSSL_STRING_push(rr_from, opt_arg()); | |
437 | break; | |
438 | case OPT_RR_TO: | |
439 | if (rr_to == NULL | |
440 | && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL) | |
441 | goto end; | |
442 | sk_OPENSSL_STRING_push(rr_to, opt_arg()); | |
443 | break; | |
444 | case OPT_PRINT: | |
445 | noout = print = 1; | |
446 | break; | |
447 | case OPT_SECRETKEY: | |
448 | secret_key = string_to_hex(opt_arg(), <mp); | |
449 | if (secret_key == NULL) { | |
450 | BIO_printf(bio_err, "Invalid key %s\n", opt_arg()); | |
451 | goto end; | |
0f113f3e MC |
452 | } |
453 | secret_keylen = (size_t)ltmp; | |
7e1b7485 RS |
454 | break; |
455 | case OPT_SECRETKEYID: | |
456 | secret_keyid = string_to_hex(opt_arg(), <mp); | |
457 | if (secret_keyid == NULL) { | |
458 | BIO_printf(bio_err, "Invalid id %s\n", opt_arg()); | |
459 | goto opthelp; | |
0f113f3e MC |
460 | } |
461 | secret_keyidlen = (size_t)ltmp; | |
7e1b7485 RS |
462 | break; |
463 | case OPT_PWRI_PASSWORD: | |
464 | pwri_pass = (unsigned char *)opt_arg(); | |
465 | break; | |
466 | case OPT_ECONTENT_TYPE: | |
467 | econtent_type = OBJ_txt2obj(opt_arg(), 0); | |
468 | if (econtent_type == NULL) { | |
469 | BIO_printf(bio_err, "Invalid OID %s\n", opt_arg()); | |
470 | goto opthelp; | |
0f113f3e | 471 | } |
7e1b7485 RS |
472 | break; |
473 | case OPT_RAND: | |
474 | inrand = opt_arg(); | |
0f113f3e | 475 | need_rand = 1; |
7e1b7485 RS |
476 | break; |
477 | case OPT_ENGINE: | |
333b070e | 478 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
479 | break; |
480 | case OPT_PASSIN: | |
481 | passinarg = opt_arg(); | |
482 | break; | |
483 | case OPT_TO: | |
484 | to = opt_arg(); | |
485 | break; | |
486 | case OPT_FROM: | |
487 | from = opt_arg(); | |
488 | break; | |
489 | case OPT_SUBJECT: | |
490 | subject = opt_arg(); | |
491 | break; | |
492 | case OPT_CERTSOUT: | |
493 | certsoutfile = opt_arg(); | |
494 | break; | |
495 | case OPT_MD: | |
496 | if (!opt_md(opt_arg(), &sign_md)) | |
497 | goto end; | |
498 | break; | |
499 | case OPT_SIGNER: | |
0f113f3e | 500 | /* If previous -signer argument add signer to list */ |
0f113f3e | 501 | if (signerfile) { |
7e1b7485 RS |
502 | if (sksigners == NULL |
503 | && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) | |
504 | goto end; | |
0f113f3e | 505 | sk_OPENSSL_STRING_push(sksigners, signerfile); |
7e1b7485 | 506 | if (keyfile == NULL) |
0f113f3e | 507 | keyfile = signerfile; |
7e1b7485 RS |
508 | if (skkeys == NULL |
509 | && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) | |
510 | goto end; | |
0f113f3e MC |
511 | sk_OPENSSL_STRING_push(skkeys, keyfile); |
512 | keyfile = NULL; | |
513 | } | |
7e1b7485 RS |
514 | signerfile = opt_arg(); |
515 | break; | |
516 | case OPT_INKEY: | |
0f113f3e MC |
517 | /* If previous -inkey arument add signer to list */ |
518 | if (keyfile) { | |
7e1b7485 | 519 | if (signerfile == NULL) { |
0f113f3e | 520 | BIO_puts(bio_err, "Illegal -inkey without -signer\n"); |
7e1b7485 | 521 | goto end; |
0f113f3e | 522 | } |
7e1b7485 RS |
523 | if (sksigners == NULL |
524 | && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) | |
525 | goto end; | |
0f113f3e MC |
526 | sk_OPENSSL_STRING_push(sksigners, signerfile); |
527 | signerfile = NULL; | |
7e1b7485 RS |
528 | if (skkeys == NULL |
529 | && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) | |
530 | goto end; | |
0f113f3e MC |
531 | sk_OPENSSL_STRING_push(skkeys, keyfile); |
532 | } | |
7e1b7485 RS |
533 | keyfile = opt_arg(); |
534 | break; | |
535 | case OPT_KEYFORM: | |
536 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform)) | |
537 | goto opthelp; | |
538 | break; | |
539 | case OPT_RECIP: | |
540 | if (operation == SMIME_ENCRYPT) { | |
541 | if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL) | |
542 | goto end; | |
543 | cert = load_cert(opt_arg(), FORMAT_PEM, NULL, e, | |
544 | "recipient certificate file"); | |
545 | if (cert == NULL) | |
546 | goto end; | |
547 | sk_X509_push(encerts, cert); | |
548 | cert = NULL; | |
549 | } else | |
550 | recipfile = opt_arg(); | |
551 | break; | |
552 | case OPT_CIPHER: | |
553 | if (!opt_cipher(opt_unknown(), &cipher)) | |
554 | goto end; | |
555 | break; | |
556 | case OPT_KEYOPT: | |
557 | keyidx = -1; | |
0f113f3e MC |
558 | if (operation == SMIME_ENCRYPT) { |
559 | if (encerts) | |
560 | keyidx += sk_X509_num(encerts); | |
561 | } else { | |
562 | if (keyfile || signerfile) | |
563 | keyidx++; | |
564 | if (skkeys) | |
565 | keyidx += sk_OPENSSL_STRING_num(skkeys); | |
566 | } | |
567 | if (keyidx < 0) { | |
568 | BIO_printf(bio_err, "No key specified\n"); | |
7e1b7485 | 569 | goto opthelp; |
0f113f3e MC |
570 | } |
571 | if (key_param == NULL || key_param->idx != keyidx) { | |
572 | cms_key_param *nparam; | |
b4faea50 | 573 | nparam = app_malloc(sizeof(*nparam), "key param buffer"); |
0f113f3e | 574 | nparam->idx = keyidx; |
7e1b7485 RS |
575 | if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL) |
576 | goto end; | |
0f113f3e MC |
577 | nparam->next = NULL; |
578 | if (key_first == NULL) | |
579 | key_first = nparam; | |
580 | else | |
581 | key_param->next = nparam; | |
582 | key_param = nparam; | |
583 | } | |
7e1b7485 RS |
584 | sk_OPENSSL_STRING_push(key_param->param, opt_arg()); |
585 | break; | |
586 | case OPT_V_CASES: | |
587 | if (!opt_verify(o, vpm)) | |
588 | goto end; | |
589 | vpmtouched++; | |
590 | break; | |
7e1b7485 | 591 | case OPT_3DES_WRAP: |
9c3bcfa0 | 592 | # ifndef OPENSSL_NO_DES |
7e1b7485 | 593 | wrap_cipher = EVP_des_ede3_wrap(); |
7e1b7485 | 594 | # endif |
9c3bcfa0 | 595 | break; |
7e1b7485 RS |
596 | # ifndef OPENSSL_NO_AES |
597 | case OPT_AES128_WRAP: | |
598 | wrap_cipher = EVP_aes_128_wrap(); | |
599 | break; | |
600 | case OPT_AES192_WRAP: | |
601 | wrap_cipher = EVP_aes_192_wrap(); | |
602 | break; | |
603 | case OPT_AES256_WRAP: | |
604 | wrap_cipher = EVP_aes_256_wrap(); | |
605 | break; | |
9c3bcfa0 RS |
606 | # else |
607 | case OPT_AES128_WRAP: | |
608 | case OPT_AES192_WRAP: | |
609 | case OPT_AES256_WRAP: | |
610 | break; | |
7e1b7485 RS |
611 | # endif |
612 | } | |
0f113f3e | 613 | } |
7e1b7485 RS |
614 | argc = opt_num_rest(); |
615 | argv = opt_rest(); | |
0f113f3e MC |
616 | |
617 | if (((rr_allorfirst != -1) || rr_from) && !rr_to) { | |
618 | BIO_puts(bio_err, "No Signed Receipts Recipients\n"); | |
7e1b7485 | 619 | goto opthelp; |
0f113f3e MC |
620 | } |
621 | ||
622 | if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) { | |
623 | BIO_puts(bio_err, "Signed receipts only allowed with -sign\n"); | |
7e1b7485 | 624 | goto opthelp; |
0f113f3e MC |
625 | } |
626 | if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) { | |
627 | BIO_puts(bio_err, "Multiple signers or keys not allowed\n"); | |
7e1b7485 | 628 | goto opthelp; |
0f113f3e MC |
629 | } |
630 | ||
631 | if (operation & SMIME_SIGNERS) { | |
632 | if (keyfile && !signerfile) { | |
633 | BIO_puts(bio_err, "Illegal -inkey without -signer\n"); | |
7e1b7485 | 634 | goto opthelp; |
0f113f3e MC |
635 | } |
636 | /* Check to see if any final signer needs to be appended */ | |
637 | if (signerfile) { | |
7e1b7485 RS |
638 | if (!sksigners |
639 | && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL) | |
640 | goto end; | |
0f113f3e | 641 | sk_OPENSSL_STRING_push(sksigners, signerfile); |
7e1b7485 RS |
642 | if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL) |
643 | goto end; | |
0f113f3e MC |
644 | if (!keyfile) |
645 | keyfile = signerfile; | |
646 | sk_OPENSSL_STRING_push(skkeys, keyfile); | |
647 | } | |
648 | if (!sksigners) { | |
649 | BIO_printf(bio_err, "No signer certificate specified\n"); | |
7e1b7485 | 650 | goto opthelp; |
0f113f3e MC |
651 | } |
652 | signerfile = NULL; | |
653 | keyfile = NULL; | |
654 | need_rand = 1; | |
655 | } | |
656 | ||
657 | else if (operation == SMIME_DECRYPT) { | |
658 | if (!recipfile && !keyfile && !secret_key && !pwri_pass) { | |
659 | BIO_printf(bio_err, | |
660 | "No recipient certificate or key specified\n"); | |
7e1b7485 | 661 | goto opthelp; |
0f113f3e MC |
662 | } |
663 | } else if (operation == SMIME_ENCRYPT) { | |
7e1b7485 | 664 | if (*argv == NULL && !secret_key && !pwri_pass && !encerts) { |
0f113f3e | 665 | BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n"); |
7e1b7485 | 666 | goto opthelp; |
0f113f3e MC |
667 | } |
668 | need_rand = 1; | |
669 | } else if (!operation) | |
7e1b7485 RS |
670 | goto opthelp; |
671 | ||
7e1b7485 | 672 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
673 | BIO_printf(bio_err, "Error getting password\n"); |
674 | goto end; | |
675 | } | |
676 | ||
296f54ee RL |
677 | if (!app_load_modules(NULL)) |
678 | goto end; | |
679 | ||
0f113f3e | 680 | if (need_rand) { |
7e1b7485 | 681 | app_RAND_load_file(NULL, (inrand != NULL)); |
0f113f3e MC |
682 | if (inrand != NULL) |
683 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", | |
684 | app_RAND_load_files(inrand)); | |
685 | } | |
686 | ||
687 | ret = 2; | |
688 | ||
689 | if (!(operation & SMIME_SIGNERS)) | |
690 | flags &= ~CMS_DETACHED; | |
691 | ||
692 | if (operation & SMIME_OP) { | |
d303b9d8 | 693 | outmode = WB(outformat); |
0f113f3e MC |
694 | } else { |
695 | if (flags & CMS_BINARY) | |
696 | outmode = "wb"; | |
697 | } | |
698 | ||
699 | if (operation & SMIME_IP) { | |
d303b9d8 | 700 | inmode = RB(informat); |
0f113f3e MC |
701 | } else { |
702 | if (flags & CMS_BINARY) | |
703 | inmode = "rb"; | |
704 | } | |
705 | ||
706 | if (operation == SMIME_ENCRYPT) { | |
707 | if (!cipher) { | |
708 | # ifndef OPENSSL_NO_DES | |
709 | cipher = EVP_des_ede3_cbc(); | |
710 | # else | |
711 | BIO_printf(bio_err, "No cipher selected\n"); | |
712 | goto end; | |
713 | # endif | |
714 | } | |
715 | ||
716 | if (secret_key && !secret_keyid) { | |
717 | BIO_printf(bio_err, "No secret key id\n"); | |
718 | goto end; | |
719 | } | |
720 | ||
7e1b7485 RS |
721 | if (*argv && !encerts) |
722 | if ((encerts = sk_X509_new_null()) == NULL) | |
723 | goto end; | |
724 | while (*argv) { | |
75ebbd9a RS |
725 | if ((cert = load_cert(*argv, FORMAT_PEM, NULL, e, |
726 | "recipient certificate file")) == NULL) | |
0f113f3e MC |
727 | goto end; |
728 | sk_X509_push(encerts, cert); | |
729 | cert = NULL; | |
7e1b7485 | 730 | argv++; |
0f113f3e MC |
731 | } |
732 | } | |
733 | ||
734 | if (certfile) { | |
75ebbd9a RS |
735 | if ((other = load_certs(certfile, FORMAT_PEM, NULL, e, |
736 | "certificate file")) == NULL) { | |
0f113f3e MC |
737 | ERR_print_errors(bio_err); |
738 | goto end; | |
739 | } | |
740 | } | |
741 | ||
742 | if (recipfile && (operation == SMIME_DECRYPT)) { | |
75ebbd9a RS |
743 | if ((recip = load_cert(recipfile, FORMAT_PEM, NULL, e, |
744 | "recipient certificate file")) == NULL) { | |
0f113f3e MC |
745 | ERR_print_errors(bio_err); |
746 | goto end; | |
747 | } | |
748 | } | |
749 | ||
750 | if (operation == SMIME_SIGN_RECEIPT) { | |
75ebbd9a RS |
751 | if ((signer = load_cert(signerfile, FORMAT_PEM, NULL, e, |
752 | "receipt signer certificate file")) == NULL) { | |
0f113f3e MC |
753 | ERR_print_errors(bio_err); |
754 | goto end; | |
755 | } | |
756 | } | |
757 | ||
758 | if (operation == SMIME_DECRYPT) { | |
759 | if (!keyfile) | |
760 | keyfile = recipfile; | |
761 | } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) { | |
762 | if (!keyfile) | |
763 | keyfile = signerfile; | |
764 | } else | |
765 | keyfile = NULL; | |
766 | ||
767 | if (keyfile) { | |
7e1b7485 | 768 | key = load_key(keyfile, keyform, 0, passin, e, "signing key file"); |
0f113f3e MC |
769 | if (!key) |
770 | goto end; | |
771 | } | |
772 | ||
7e1b7485 RS |
773 | in = bio_open_default(infile, inmode); |
774 | if (in == NULL) | |
775 | goto end; | |
0f113f3e MC |
776 | |
777 | if (operation & SMIME_IP) { | |
778 | if (informat == FORMAT_SMIME) | |
779 | cms = SMIME_read_CMS(in, &indata); | |
780 | else if (informat == FORMAT_PEM) | |
781 | cms = PEM_read_bio_CMS(in, NULL, NULL, NULL); | |
782 | else if (informat == FORMAT_ASN1) | |
783 | cms = d2i_CMS_bio(in, NULL); | |
784 | else { | |
785 | BIO_printf(bio_err, "Bad input format for CMS file\n"); | |
786 | goto end; | |
787 | } | |
788 | ||
789 | if (!cms) { | |
790 | BIO_printf(bio_err, "Error reading S/MIME message\n"); | |
791 | goto end; | |
792 | } | |
793 | if (contfile) { | |
794 | BIO_free(indata); | |
75ebbd9a | 795 | if ((indata = BIO_new_file(contfile, "rb")) == NULL) { |
0f113f3e MC |
796 | BIO_printf(bio_err, "Can't read content file %s\n", contfile); |
797 | goto end; | |
798 | } | |
799 | } | |
800 | if (certsoutfile) { | |
801 | STACK_OF(X509) *allcerts; | |
802 | allcerts = CMS_get1_certs(cms); | |
803 | if (!save_certs(certsoutfile, allcerts)) { | |
804 | BIO_printf(bio_err, | |
805 | "Error writing certs to %s\n", certsoutfile); | |
806 | ret = 5; | |
807 | goto end; | |
808 | } | |
809 | sk_X509_pop_free(allcerts, X509_free); | |
810 | } | |
811 | } | |
812 | ||
813 | if (rctfile) { | |
814 | char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r"; | |
75ebbd9a | 815 | if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) { |
0f113f3e MC |
816 | BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile); |
817 | goto end; | |
818 | } | |
819 | ||
820 | if (rctformat == FORMAT_SMIME) | |
821 | rcms = SMIME_read_CMS(rctin, NULL); | |
822 | else if (rctformat == FORMAT_PEM) | |
823 | rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL); | |
824 | else if (rctformat == FORMAT_ASN1) | |
825 | rcms = d2i_CMS_bio(rctin, NULL); | |
826 | else { | |
827 | BIO_printf(bio_err, "Bad input format for receipt\n"); | |
828 | goto end; | |
829 | } | |
830 | ||
831 | if (!rcms) { | |
832 | BIO_printf(bio_err, "Error reading receipt\n"); | |
833 | goto end; | |
834 | } | |
835 | } | |
836 | ||
7e1b7485 RS |
837 | out = bio_open_default(outfile, outmode); |
838 | if (out == NULL) | |
839 | goto end; | |
0f113f3e MC |
840 | |
841 | if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) { | |
75ebbd9a | 842 | if ((store = setup_verify(CAfile, CApath)) == NULL) |
0f113f3e MC |
843 | goto end; |
844 | X509_STORE_set_verify_cb(store, cms_cb); | |
7e1b7485 | 845 | if (vpmtouched) |
0f113f3e MC |
846 | X509_STORE_set1_param(store, vpm); |
847 | } | |
848 | ||
849 | ret = 3; | |
850 | ||
851 | if (operation == SMIME_DATA_CREATE) { | |
852 | cms = CMS_data_create(in, flags); | |
853 | } else if (operation == SMIME_DIGEST_CREATE) { | |
854 | cms = CMS_digest_create(in, sign_md, flags); | |
855 | } else if (operation == SMIME_COMPRESS) { | |
856 | cms = CMS_compress(in, -1, flags); | |
857 | } else if (operation == SMIME_ENCRYPT) { | |
858 | int i; | |
859 | flags |= CMS_PARTIAL; | |
860 | cms = CMS_encrypt(NULL, in, cipher, flags); | |
861 | if (!cms) | |
862 | goto end; | |
863 | for (i = 0; i < sk_X509_num(encerts); i++) { | |
864 | CMS_RecipientInfo *ri; | |
865 | cms_key_param *kparam; | |
866 | int tflags = flags; | |
867 | X509 *x = sk_X509_value(encerts, i); | |
868 | for (kparam = key_first; kparam; kparam = kparam->next) { | |
869 | if (kparam->idx == i) { | |
870 | tflags |= CMS_KEY_PARAM; | |
871 | break; | |
872 | } | |
873 | } | |
874 | ri = CMS_add1_recipient_cert(cms, x, tflags); | |
875 | if (!ri) | |
876 | goto end; | |
877 | if (kparam) { | |
878 | EVP_PKEY_CTX *pctx; | |
879 | pctx = CMS_RecipientInfo_get0_pkey_ctx(ri); | |
880 | if (!cms_set_pkey_param(pctx, kparam->param)) | |
881 | goto end; | |
882 | } | |
883 | if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE | |
884 | && wrap_cipher) { | |
885 | EVP_CIPHER_CTX *wctx; | |
886 | wctx = CMS_RecipientInfo_kari_get0_ctx(ri); | |
887 | EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL); | |
888 | } | |
889 | } | |
890 | ||
891 | if (secret_key) { | |
892 | if (!CMS_add0_recipient_key(cms, NID_undef, | |
893 | secret_key, secret_keylen, | |
894 | secret_keyid, secret_keyidlen, | |
895 | NULL, NULL, NULL)) | |
896 | goto end; | |
897 | /* NULL these because call absorbs them */ | |
898 | secret_key = NULL; | |
899 | secret_keyid = NULL; | |
900 | } | |
901 | if (pwri_pass) { | |
902 | pwri_tmp = (unsigned char *)BUF_strdup((char *)pwri_pass); | |
903 | if (!pwri_tmp) | |
904 | goto end; | |
905 | if (!CMS_add0_recipient_password(cms, | |
906 | -1, NID_undef, NID_undef, | |
907 | pwri_tmp, -1, NULL)) | |
908 | goto end; | |
909 | pwri_tmp = NULL; | |
910 | } | |
911 | if (!(flags & CMS_STREAM)) { | |
912 | if (!CMS_final(cms, in, NULL, flags)) | |
913 | goto end; | |
914 | } | |
915 | } else if (operation == SMIME_ENCRYPTED_ENCRYPT) { | |
916 | cms = CMS_EncryptedData_encrypt(in, cipher, | |
917 | secret_key, secret_keylen, flags); | |
918 | ||
919 | } else if (operation == SMIME_SIGN_RECEIPT) { | |
920 | CMS_ContentInfo *srcms = NULL; | |
921 | STACK_OF(CMS_SignerInfo) *sis; | |
922 | CMS_SignerInfo *si; | |
923 | sis = CMS_get0_SignerInfos(cms); | |
924 | if (!sis) | |
925 | goto end; | |
926 | si = sk_CMS_SignerInfo_value(sis, 0); | |
927 | srcms = CMS_sign_receipt(si, signer, key, other, flags); | |
928 | if (!srcms) | |
929 | goto end; | |
930 | CMS_ContentInfo_free(cms); | |
931 | cms = srcms; | |
932 | } else if (operation & SMIME_SIGNERS) { | |
933 | int i; | |
934 | /* | |
935 | * If detached data content we enable streaming if S/MIME output | |
936 | * format. | |
937 | */ | |
938 | if (operation == SMIME_SIGN) { | |
939 | ||
940 | if (flags & CMS_DETACHED) { | |
941 | if (outformat == FORMAT_SMIME) | |
942 | flags |= CMS_STREAM; | |
943 | } | |
944 | flags |= CMS_PARTIAL; | |
945 | cms = CMS_sign(NULL, NULL, other, in, flags); | |
946 | if (!cms) | |
947 | goto end; | |
948 | if (econtent_type) | |
949 | CMS_set1_eContentType(cms, econtent_type); | |
950 | ||
951 | if (rr_to) { | |
952 | rr = make_receipt_request(rr_to, rr_allorfirst, rr_from); | |
953 | if (!rr) { | |
954 | BIO_puts(bio_err, | |
955 | "Signed Receipt Request Creation Error\n"); | |
956 | goto end; | |
957 | } | |
958 | } | |
959 | } else | |
960 | flags |= CMS_REUSE_DIGEST; | |
961 | for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) { | |
962 | CMS_SignerInfo *si; | |
963 | cms_key_param *kparam; | |
964 | int tflags = flags; | |
965 | signerfile = sk_OPENSSL_STRING_value(sksigners, i); | |
966 | keyfile = sk_OPENSSL_STRING_value(skkeys, i); | |
967 | ||
7e1b7485 | 968 | signer = load_cert(signerfile, FORMAT_PEM, NULL, |
0f113f3e MC |
969 | e, "signer certificate"); |
970 | if (!signer) | |
971 | goto end; | |
7e1b7485 | 972 | key = load_key(keyfile, keyform, 0, passin, e, "signing key file"); |
0f113f3e MC |
973 | if (!key) |
974 | goto end; | |
975 | for (kparam = key_first; kparam; kparam = kparam->next) { | |
976 | if (kparam->idx == i) { | |
977 | tflags |= CMS_KEY_PARAM; | |
978 | break; | |
979 | } | |
980 | } | |
981 | si = CMS_add1_signer(cms, signer, key, sign_md, tflags); | |
982 | if (!si) | |
983 | goto end; | |
984 | if (kparam) { | |
985 | EVP_PKEY_CTX *pctx; | |
986 | pctx = CMS_SignerInfo_get0_pkey_ctx(si); | |
987 | if (!cms_set_pkey_param(pctx, kparam->param)) | |
988 | goto end; | |
989 | } | |
990 | if (rr && !CMS_add1_ReceiptRequest(si, rr)) | |
991 | goto end; | |
992 | X509_free(signer); | |
993 | signer = NULL; | |
994 | EVP_PKEY_free(key); | |
995 | key = NULL; | |
996 | } | |
997 | /* If not streaming or resigning finalize structure */ | |
998 | if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) { | |
999 | if (!CMS_final(cms, in, NULL, flags)) | |
1000 | goto end; | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | if (!cms) { | |
1005 | BIO_printf(bio_err, "Error creating CMS structure\n"); | |
1006 | goto end; | |
1007 | } | |
1008 | ||
1009 | ret = 4; | |
1010 | if (operation == SMIME_DECRYPT) { | |
1011 | if (flags & CMS_DEBUG_DECRYPT) | |
1012 | CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags); | |
1013 | ||
1014 | if (secret_key) { | |
1015 | if (!CMS_decrypt_set1_key(cms, | |
1016 | secret_key, secret_keylen, | |
1017 | secret_keyid, secret_keyidlen)) { | |
1018 | BIO_puts(bio_err, "Error decrypting CMS using secret key\n"); | |
1019 | goto end; | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | if (key) { | |
1024 | if (!CMS_decrypt_set1_pkey(cms, key, recip)) { | |
1025 | BIO_puts(bio_err, "Error decrypting CMS using private key\n"); | |
1026 | goto end; | |
1027 | } | |
1028 | } | |
1029 | ||
1030 | if (pwri_pass) { | |
1031 | if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) { | |
1032 | BIO_puts(bio_err, "Error decrypting CMS using password\n"); | |
1033 | goto end; | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) { | |
1038 | BIO_printf(bio_err, "Error decrypting CMS structure\n"); | |
1039 | goto end; | |
1040 | } | |
1041 | } else if (operation == SMIME_DATAOUT) { | |
1042 | if (!CMS_data(cms, out, flags)) | |
1043 | goto end; | |
1044 | } else if (operation == SMIME_UNCOMPRESS) { | |
1045 | if (!CMS_uncompress(cms, indata, out, flags)) | |
1046 | goto end; | |
1047 | } else if (operation == SMIME_DIGEST_VERIFY) { | |
1048 | if (CMS_digest_verify(cms, indata, out, flags) > 0) | |
1049 | BIO_printf(bio_err, "Verification successful\n"); | |
1050 | else { | |
1051 | BIO_printf(bio_err, "Verification failure\n"); | |
1052 | goto end; | |
1053 | } | |
1054 | } else if (operation == SMIME_ENCRYPTED_DECRYPT) { | |
1055 | if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen, | |
1056 | indata, out, flags)) | |
1057 | goto end; | |
1058 | } else if (operation == SMIME_VERIFY) { | |
1059 | if (CMS_verify(cms, other, store, indata, out, flags) > 0) | |
1060 | BIO_printf(bio_err, "Verification successful\n"); | |
1061 | else { | |
1062 | BIO_printf(bio_err, "Verification failure\n"); | |
1063 | if (verify_retcode) | |
1064 | ret = verify_err + 32; | |
1065 | goto end; | |
1066 | } | |
1067 | if (signerfile) { | |
1068 | STACK_OF(X509) *signers; | |
1069 | signers = CMS_get0_signers(cms); | |
1070 | if (!save_certs(signerfile, signers)) { | |
1071 | BIO_printf(bio_err, | |
1072 | "Error writing signers to %s\n", signerfile); | |
1073 | ret = 5; | |
1074 | goto end; | |
1075 | } | |
1076 | sk_X509_free(signers); | |
1077 | } | |
1078 | if (rr_print) | |
ecf3a1fb | 1079 | receipt_request_print(cms); |
0f113f3e MC |
1080 | |
1081 | } else if (operation == SMIME_VERIFY_RECEIPT) { | |
1082 | if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) | |
1083 | BIO_printf(bio_err, "Verification successful\n"); | |
1084 | else { | |
1085 | BIO_printf(bio_err, "Verification failure\n"); | |
1086 | goto end; | |
1087 | } | |
1088 | } else { | |
1089 | if (noout) { | |
1090 | if (print) | |
1091 | CMS_ContentInfo_print_ctx(out, cms, 0, NULL); | |
1092 | } else if (outformat == FORMAT_SMIME) { | |
1093 | if (to) | |
1094 | BIO_printf(out, "To: %s\n", to); | |
1095 | if (from) | |
1096 | BIO_printf(out, "From: %s\n", from); | |
1097 | if (subject) | |
1098 | BIO_printf(out, "Subject: %s\n", subject); | |
1099 | if (operation == SMIME_RESIGN) | |
1100 | ret = SMIME_write_CMS(out, cms, indata, flags); | |
1101 | else | |
1102 | ret = SMIME_write_CMS(out, cms, in, flags); | |
1103 | } else if (outformat == FORMAT_PEM) | |
1104 | ret = PEM_write_bio_CMS_stream(out, cms, in, flags); | |
1105 | else if (outformat == FORMAT_ASN1) | |
1106 | ret = i2d_CMS_bio_stream(out, cms, in, flags); | |
1107 | else { | |
1108 | BIO_printf(bio_err, "Bad output format for CMS file\n"); | |
1109 | goto end; | |
1110 | } | |
1111 | if (ret <= 0) { | |
1112 | ret = 6; | |
1113 | goto end; | |
1114 | } | |
1115 | } | |
1116 | ret = 0; | |
1117 | end: | |
1118 | if (ret) | |
1119 | ERR_print_errors(bio_err); | |
1120 | if (need_rand) | |
7e1b7485 | 1121 | app_RAND_write_file(NULL); |
0f113f3e MC |
1122 | sk_X509_pop_free(encerts, X509_free); |
1123 | sk_X509_pop_free(other, X509_free); | |
7e1b7485 | 1124 | X509_VERIFY_PARAM_free(vpm); |
25aaa98a RS |
1125 | sk_OPENSSL_STRING_free(sksigners); |
1126 | sk_OPENSSL_STRING_free(skkeys); | |
b548a1f1 RS |
1127 | OPENSSL_free(secret_key); |
1128 | OPENSSL_free(secret_keyid); | |
1129 | OPENSSL_free(pwri_tmp); | |
0dfb9398 | 1130 | ASN1_OBJECT_free(econtent_type); |
25aaa98a RS |
1131 | CMS_ReceiptRequest_free(rr); |
1132 | sk_OPENSSL_STRING_free(rr_to); | |
1133 | sk_OPENSSL_STRING_free(rr_from); | |
0f113f3e MC |
1134 | for (key_param = key_first; key_param;) { |
1135 | cms_key_param *tparam; | |
1136 | sk_OPENSSL_STRING_free(key_param->param); | |
1137 | tparam = key_param->next; | |
1138 | OPENSSL_free(key_param); | |
1139 | key_param = tparam; | |
1140 | } | |
1141 | X509_STORE_free(store); | |
1142 | X509_free(cert); | |
1143 | X509_free(recip); | |
1144 | X509_free(signer); | |
1145 | EVP_PKEY_free(key); | |
1146 | CMS_ContentInfo_free(cms); | |
1147 | CMS_ContentInfo_free(rcms); | |
1148 | BIO_free(rctin); | |
1149 | BIO_free(in); | |
1150 | BIO_free(indata); | |
1151 | BIO_free_all(out); | |
b548a1f1 | 1152 | OPENSSL_free(passin); |
0f113f3e | 1153 | return (ret); |
8931b30d DSH |
1154 | } |
1155 | ||
1156 | static int save_certs(char *signerfile, STACK_OF(X509) *signers) | |
0f113f3e MC |
1157 | { |
1158 | int i; | |
1159 | BIO *tmp; | |
1160 | if (!signerfile) | |
1161 | return 1; | |
1162 | tmp = BIO_new_file(signerfile, "w"); | |
1163 | if (!tmp) | |
1164 | return 0; | |
1165 | for (i = 0; i < sk_X509_num(signers); i++) | |
1166 | PEM_write_bio_X509(tmp, sk_X509_value(signers, i)); | |
1167 | BIO_free(tmp); | |
1168 | return 1; | |
1169 | } | |
8931b30d DSH |
1170 | |
1171 | /* Minimal callback just to output policy info (if any) */ | |
1172 | ||
7f50d9a4 | 1173 | static int cms_cb(int ok, X509_STORE_CTX *ctx) |
0f113f3e MC |
1174 | { |
1175 | int error; | |
8931b30d | 1176 | |
0f113f3e | 1177 | error = X509_STORE_CTX_get_error(ctx); |
8931b30d | 1178 | |
0f113f3e | 1179 | verify_err = error; |
db50661f | 1180 | |
0f113f3e MC |
1181 | if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) |
1182 | && ((error != X509_V_OK) || (ok != 2))) | |
1183 | return ok; | |
8931b30d | 1184 | |
ecf3a1fb | 1185 | policies_print(ctx); |
8931b30d | 1186 | |
0f113f3e | 1187 | return ok; |
8931b30d | 1188 | |
0f113f3e | 1189 | } |
8931b30d | 1190 | |
ecf3a1fb | 1191 | static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns) |
0f113f3e MC |
1192 | { |
1193 | STACK_OF(GENERAL_NAME) *gens; | |
1194 | GENERAL_NAME *gen; | |
1195 | int i, j; | |
ecf3a1fb | 1196 | |
0f113f3e MC |
1197 | for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) { |
1198 | gens = sk_GENERAL_NAMES_value(gns, i); | |
1199 | for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) { | |
1200 | gen = sk_GENERAL_NAME_value(gens, j); | |
ecf3a1fb RS |
1201 | BIO_puts(bio_err, " "); |
1202 | GENERAL_NAME_print(bio_err, gen); | |
1203 | BIO_puts(bio_err, "\n"); | |
0f113f3e MC |
1204 | } |
1205 | } | |
1206 | return; | |
1207 | } | |
f4cc56f4 | 1208 | |
ecf3a1fb | 1209 | static void receipt_request_print(CMS_ContentInfo *cms) |
0f113f3e MC |
1210 | { |
1211 | STACK_OF(CMS_SignerInfo) *sis; | |
1212 | CMS_SignerInfo *si; | |
1213 | CMS_ReceiptRequest *rr; | |
1214 | int allorfirst; | |
1215 | STACK_OF(GENERAL_NAMES) *rto, *rlist; | |
1216 | ASN1_STRING *scid; | |
1217 | int i, rv; | |
1218 | sis = CMS_get0_SignerInfos(cms); | |
1219 | for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) { | |
1220 | si = sk_CMS_SignerInfo_value(sis, i); | |
1221 | rv = CMS_get1_ReceiptRequest(si, &rr); | |
1222 | BIO_printf(bio_err, "Signer %d:\n", i + 1); | |
1223 | if (rv == 0) | |
1224 | BIO_puts(bio_err, " No Receipt Request\n"); | |
1225 | else if (rv < 0) { | |
1226 | BIO_puts(bio_err, " Receipt Request Parse Error\n"); | |
1227 | ERR_print_errors(bio_err); | |
1228 | } else { | |
1229 | char *id; | |
1230 | int idlen; | |
1231 | CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst, | |
1232 | &rlist, &rto); | |
ecf3a1fb | 1233 | BIO_puts(bio_err, " Signed Content ID:\n"); |
0f113f3e MC |
1234 | idlen = ASN1_STRING_length(scid); |
1235 | id = (char *)ASN1_STRING_data(scid); | |
ecf3a1fb RS |
1236 | BIO_dump_indent(bio_err, id, idlen, 4); |
1237 | BIO_puts(bio_err, " Receipts From"); | |
0f113f3e | 1238 | if (rlist) { |
ecf3a1fb RS |
1239 | BIO_puts(bio_err, " List:\n"); |
1240 | gnames_stack_print(rlist); | |
0f113f3e | 1241 | } else if (allorfirst == 1) |
ecf3a1fb | 1242 | BIO_puts(bio_err, ": First Tier\n"); |
0f113f3e | 1243 | else if (allorfirst == 0) |
ecf3a1fb | 1244 | BIO_puts(bio_err, ": All\n"); |
0f113f3e | 1245 | else |
ecf3a1fb RS |
1246 | BIO_printf(bio_err, " Unknown (%d)\n", allorfirst); |
1247 | BIO_puts(bio_err, " Receipts To:\n"); | |
1248 | gnames_stack_print(rto); | |
0f113f3e | 1249 | } |
25aaa98a | 1250 | CMS_ReceiptRequest_free(rr); |
0f113f3e MC |
1251 | } |
1252 | } | |
f4cc56f4 | 1253 | |
c869da88 | 1254 | static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns) |
0f113f3e MC |
1255 | { |
1256 | int i; | |
1257 | STACK_OF(GENERAL_NAMES) *ret; | |
1258 | GENERAL_NAMES *gens = NULL; | |
1259 | GENERAL_NAME *gen = NULL; | |
1260 | ret = sk_GENERAL_NAMES_new_null(); | |
1261 | if (!ret) | |
1262 | goto err; | |
1263 | for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) { | |
1264 | char *str = sk_OPENSSL_STRING_value(ns, i); | |
1265 | gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0); | |
1266 | if (!gen) | |
1267 | goto err; | |
1268 | gens = GENERAL_NAMES_new(); | |
1269 | if (!gens) | |
1270 | goto err; | |
1271 | if (!sk_GENERAL_NAME_push(gens, gen)) | |
1272 | goto err; | |
1273 | gen = NULL; | |
1274 | if (!sk_GENERAL_NAMES_push(ret, gens)) | |
1275 | goto err; | |
1276 | gens = NULL; | |
1277 | } | |
1278 | ||
1279 | return ret; | |
1280 | ||
1281 | err: | |
25aaa98a RS |
1282 | sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free); |
1283 | GENERAL_NAMES_free(gens); | |
1284 | GENERAL_NAME_free(gen); | |
0f113f3e MC |
1285 | return NULL; |
1286 | } | |
1287 | ||
1288 | static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING) | |
1289 | *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING) | |
1290 | *rr_from) | |
1291 | { | |
1292 | STACK_OF(GENERAL_NAMES) *rct_to, *rct_from; | |
1293 | CMS_ReceiptRequest *rr; | |
1294 | rct_to = make_names_stack(rr_to); | |
1295 | if (!rct_to) | |
1296 | goto err; | |
1297 | if (rr_from) { | |
1298 | rct_from = make_names_stack(rr_from); | |
1299 | if (!rct_from) | |
1300 | goto err; | |
1301 | } else | |
1302 | rct_from = NULL; | |
1303 | rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from, | |
1304 | rct_to); | |
1305 | return rr; | |
1306 | err: | |
1307 | return NULL; | |
1308 | } | |
f5e2354c | 1309 | |
02498cc8 | 1310 | static int cms_set_pkey_param(EVP_PKEY_CTX *pctx, |
0f113f3e MC |
1311 | STACK_OF(OPENSSL_STRING) *param) |
1312 | { | |
1313 | char *keyopt; | |
1314 | int i; | |
1315 | if (sk_OPENSSL_STRING_num(param) <= 0) | |
1316 | return 1; | |
1317 | for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) { | |
1318 | keyopt = sk_OPENSSL_STRING_value(param, i); | |
1319 | if (pkey_ctrl_string(pctx, keyopt) <= 0) { | |
1320 | BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt); | |
1321 | ERR_print_errors(bio_err); | |
1322 | return 0; | |
1323 | } | |
1324 | } | |
1325 | return 1; | |
1326 | } | |
02498cc8 | 1327 | |
8931b30d | 1328 | #endif |