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