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