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