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