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