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