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