Trust RSA_check_key() to return correct values
[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             if (secret_key != NULL) {
416                 /* Cannot be supplied twice */
417                 BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
418                 goto opthelp;
419             }
420             secret_key = OPENSSL_hexstr2buf(opt_arg(), &ltmp);
421             if (secret_key == NULL) {
422                 BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
423                 goto end;
424             }
425             secret_keylen = (size_t)ltmp;
426             break;
427         case OPT_SECRETKEYID:
428             if (secret_keyid != NULL) {
429                 /* Cannot be supplied twice */
430                 BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
431                 goto opthelp;
432             }
433             secret_keyid = OPENSSL_hexstr2buf(opt_arg(), &ltmp);
434             if (secret_keyid == NULL) {
435                 BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
436                 goto opthelp;
437             }
438             secret_keyidlen = (size_t)ltmp;
439             break;
440         case OPT_PWRI_PASSWORD:
441             pwri_pass = (unsigned char *)opt_arg();
442             break;
443         case OPT_ECONTENT_TYPE:
444             if (econtent_type != NULL) {
445                 /* Cannot be supplied twice */
446                 BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
447                 goto opthelp;
448             }
449             econtent_type = OBJ_txt2obj(opt_arg(), 0);
450             if (econtent_type == NULL) {
451                 BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
452                 goto opthelp;
453             }
454             break;
455         case OPT_RAND:
456             inrand = opt_arg();
457             need_rand = 1;
458             break;
459         case OPT_ENGINE:
460             e = setup_engine(opt_arg(), 0);
461             break;
462         case OPT_PASSIN:
463             passinarg = opt_arg();
464             break;
465         case OPT_TO:
466             to = opt_arg();
467             break;
468         case OPT_FROM:
469             from = opt_arg();
470             break;
471         case OPT_SUBJECT:
472             subject = opt_arg();
473             break;
474         case OPT_CERTSOUT:
475             certsoutfile = opt_arg();
476             break;
477         case OPT_MD:
478             if (!opt_md(opt_arg(), &sign_md))
479                 goto end;
480             break;
481         case OPT_SIGNER:
482             /* If previous -signer argument add signer to list */
483             if (signerfile) {
484                 if (sksigners == NULL
485                     && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
486                     goto end;
487                 sk_OPENSSL_STRING_push(sksigners, signerfile);
488                 if (keyfile == NULL)
489                     keyfile = signerfile;
490                 if (skkeys == NULL
491                     && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
492                     goto end;
493                 sk_OPENSSL_STRING_push(skkeys, keyfile);
494                 keyfile = NULL;
495             }
496             signerfile = opt_arg();
497             break;
498         case OPT_INKEY:
499             /* If previous -inkey argument add signer to list */
500             if (keyfile) {
501                 if (signerfile == NULL) {
502                     BIO_puts(bio_err, "Illegal -inkey without -signer\n");
503                     goto end;
504                 }
505                 if (sksigners == NULL
506                     && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
507                     goto end;
508                 sk_OPENSSL_STRING_push(sksigners, signerfile);
509                 signerfile = NULL;
510                 if (skkeys == NULL
511                     && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
512                     goto end;
513                 sk_OPENSSL_STRING_push(skkeys, keyfile);
514             }
515             keyfile = opt_arg();
516             break;
517         case OPT_KEYFORM:
518             if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
519                 goto opthelp;
520             break;
521         case OPT_RECIP:
522             if (operation == SMIME_ENCRYPT) {
523                 if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL)
524                     goto end;
525                 cert = load_cert(opt_arg(), FORMAT_PEM,
526                                  "recipient certificate file");
527                 if (cert == NULL)
528                     goto end;
529                 sk_X509_push(encerts, cert);
530                 cert = NULL;
531             } else
532                 recipfile = opt_arg();
533             break;
534         case OPT_CIPHER:
535             if (!opt_cipher(opt_unknown(), &cipher))
536                 goto end;
537             break;
538         case OPT_KEYOPT:
539             keyidx = -1;
540             if (operation == SMIME_ENCRYPT) {
541                 if (encerts)
542                     keyidx += sk_X509_num(encerts);
543             } else {
544                 if (keyfile || signerfile)
545                     keyidx++;
546                 if (skkeys)
547                     keyidx += sk_OPENSSL_STRING_num(skkeys);
548             }
549             if (keyidx < 0) {
550                 BIO_printf(bio_err, "No key specified\n");
551                 goto opthelp;
552             }
553             if (key_param == NULL || key_param->idx != keyidx) {
554                 cms_key_param *nparam;
555                 nparam = app_malloc(sizeof(*nparam), "key param buffer");
556                 nparam->idx = keyidx;
557                 if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)
558                     goto end;
559                 nparam->next = NULL;
560                 if (key_first == NULL)
561                     key_first = nparam;
562                 else
563                     key_param->next = nparam;
564                 key_param = nparam;
565             }
566             sk_OPENSSL_STRING_push(key_param->param, opt_arg());
567             break;
568         case OPT_V_CASES:
569             if (!opt_verify(o, vpm))
570                 goto end;
571             vpmtouched++;
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) && !rr_to) {
593         BIO_puts(bio_err, "No Signed Receipts Recipients\n");
594         goto opthelp;
595     }
596
597     if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
598         BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
599         goto opthelp;
600     }
601     if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
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 && !signerfile) {
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) {
613             if (!sksigners
614                 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
615                 goto end;
616             sk_OPENSSL_STRING_push(sksigners, signerfile);
617             if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
618                 goto end;
619             if (!keyfile)
620                 keyfile = signerfile;
621             sk_OPENSSL_STRING_push(skkeys, keyfile);
622         }
623         if (!sksigners) {
624             BIO_printf(bio_err, "No signer certificate specified\n");
625             goto opthelp;
626         }
627         signerfile = NULL;
628         keyfile = NULL;
629         need_rand = 1;
630     }
631
632     else if (operation == SMIME_DECRYPT) {
633         if (!recipfile && !keyfile && !secret_key && !pwri_pass) {
634             BIO_printf(bio_err,
635                        "No recipient certificate or key specified\n");
636             goto opthelp;
637         }
638     } else if (operation == SMIME_ENCRYPT) {
639         if (*argv == NULL && !secret_key && !pwri_pass && !encerts) {
640             BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
641             goto opthelp;
642         }
643         need_rand = 1;
644     } else if (!operation)
645         goto opthelp;
646
647     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
648         BIO_printf(bio_err, "Error getting password\n");
649         goto end;
650     }
651
652     if (need_rand) {
653         app_RAND_load_file(NULL, (inrand != NULL));
654         if (inrand != NULL)
655             BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
656                        app_RAND_load_files(inrand));
657     }
658
659     ret = 2;
660
661     if (!(operation & SMIME_SIGNERS))
662         flags &= ~CMS_DETACHED;
663
664     if (!(operation & SMIME_OP)) {
665         if (flags & CMS_BINARY)
666             outformat = FORMAT_BINARY;
667     }
668
669     if (!(operation & SMIME_IP)) {
670         if (flags & CMS_BINARY)
671             informat = FORMAT_BINARY;
672     }
673
674     if (operation == SMIME_ENCRYPT) {
675         if (!cipher) {
676 # ifndef OPENSSL_NO_DES
677             cipher = EVP_des_ede3_cbc();
678 # else
679             BIO_printf(bio_err, "No cipher selected\n");
680             goto end;
681 # endif
682         }
683
684         if (secret_key && !secret_keyid) {
685             BIO_printf(bio_err, "No secret key id\n");
686             goto end;
687         }
688
689         if (*argv && !encerts)
690             if ((encerts = sk_X509_new_null()) == NULL)
691                 goto end;
692         while (*argv) {
693             if ((cert = load_cert(*argv, FORMAT_PEM,
694                                   "recipient certificate file")) == NULL)
695                 goto end;
696             sk_X509_push(encerts, cert);
697             cert = NULL;
698             argv++;
699         }
700     }
701
702     if (certfile) {
703         if (!load_certs(certfile, &other, FORMAT_PEM, NULL,
704                         "certificate file")) {
705             ERR_print_errors(bio_err);
706             goto end;
707         }
708     }
709
710     if (recipfile && (operation == SMIME_DECRYPT)) {
711         if ((recip = load_cert(recipfile, FORMAT_PEM,
712                                "recipient certificate file")) == NULL) {
713             ERR_print_errors(bio_err);
714             goto end;
715         }
716     }
717
718     if (operation == SMIME_SIGN_RECEIPT) {
719         if ((signer = load_cert(signerfile, FORMAT_PEM,
720                                 "receipt signer certificate file")) == NULL) {
721             ERR_print_errors(bio_err);
722             goto end;
723         }
724     }
725
726     if (operation == SMIME_DECRYPT) {
727         if (!keyfile)
728             keyfile = recipfile;
729     } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
730         if (!keyfile)
731             keyfile = signerfile;
732     } else
733         keyfile = NULL;
734
735     if (keyfile) {
736         key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
737         if (!key)
738             goto end;
739     }
740
741     in = bio_open_default(infile, 'r', informat);
742     if (in == NULL)
743         goto end;
744
745     if (operation & SMIME_IP) {
746         if (informat == FORMAT_SMIME)
747             cms = SMIME_read_CMS(in, &indata);
748         else if (informat == FORMAT_PEM)
749             cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
750         else if (informat == FORMAT_ASN1)
751             cms = d2i_CMS_bio(in, NULL);
752         else {
753             BIO_printf(bio_err, "Bad input format for CMS file\n");
754             goto end;
755         }
756
757         if (!cms) {
758             BIO_printf(bio_err, "Error reading S/MIME message\n");
759             goto end;
760         }
761         if (contfile) {
762             BIO_free(indata);
763             if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
764                 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
765                 goto end;
766             }
767         }
768         if (certsoutfile) {
769             STACK_OF(X509) *allcerts;
770             allcerts = CMS_get1_certs(cms);
771             if (!save_certs(certsoutfile, allcerts)) {
772                 BIO_printf(bio_err,
773                            "Error writing certs to %s\n", certsoutfile);
774                 ret = 5;
775                 goto end;
776             }
777             sk_X509_pop_free(allcerts, X509_free);
778         }
779     }
780
781     if (rctfile) {
782         char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
783         if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
784             BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
785             goto end;
786         }
787
788         if (rctformat == FORMAT_SMIME)
789             rcms = SMIME_read_CMS(rctin, NULL);
790         else if (rctformat == FORMAT_PEM)
791             rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
792         else if (rctformat == FORMAT_ASN1)
793             rcms = d2i_CMS_bio(rctin, NULL);
794         else {
795             BIO_printf(bio_err, "Bad input format for receipt\n");
796             goto end;
797         }
798
799         if (!rcms) {
800             BIO_printf(bio_err, "Error reading receipt\n");
801             goto end;
802         }
803     }
804
805     out = bio_open_default(outfile, 'w', outformat);
806     if (out == NULL)
807         goto end;
808
809     if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
810         if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
811             goto end;
812         X509_STORE_set_verify_cb(store, cms_cb);
813         if (vpmtouched)
814             X509_STORE_set1_param(store, vpm);
815     }
816
817     ret = 3;
818
819     if (operation == SMIME_DATA_CREATE) {
820         cms = CMS_data_create(in, flags);
821     } else if (operation == SMIME_DIGEST_CREATE) {
822         cms = CMS_digest_create(in, sign_md, flags);
823     } else if (operation == SMIME_COMPRESS) {
824         cms = CMS_compress(in, -1, flags);
825     } else if (operation == SMIME_ENCRYPT) {
826         int i;
827         flags |= CMS_PARTIAL;
828         cms = CMS_encrypt(NULL, in, cipher, flags);
829         if (!cms)
830             goto end;
831         for (i = 0; i < sk_X509_num(encerts); i++) {
832             CMS_RecipientInfo *ri;
833             cms_key_param *kparam;
834             int tflags = flags;
835             X509 *x = sk_X509_value(encerts, i);
836             for (kparam = key_first; kparam; kparam = kparam->next) {
837                 if (kparam->idx == i) {
838                     tflags |= CMS_KEY_PARAM;
839                     break;
840                 }
841             }
842             ri = CMS_add1_recipient_cert(cms, x, tflags);
843             if (!ri)
844                 goto end;
845             if (kparam) {
846                 EVP_PKEY_CTX *pctx;
847                 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
848                 if (!cms_set_pkey_param(pctx, kparam->param))
849                     goto end;
850             }
851             if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
852                 && wrap_cipher) {
853                 EVP_CIPHER_CTX *wctx;
854                 wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
855                 EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
856             }
857         }
858
859         if (secret_key) {
860             if (!CMS_add0_recipient_key(cms, NID_undef,
861                                         secret_key, secret_keylen,
862                                         secret_keyid, secret_keyidlen,
863                                         NULL, NULL, NULL))
864                 goto end;
865             /* NULL these because call absorbs them */
866             secret_key = NULL;
867             secret_keyid = NULL;
868         }
869         if (pwri_pass) {
870             pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
871             if (!pwri_tmp)
872                 goto end;
873             if (!CMS_add0_recipient_password(cms,
874                                              -1, NID_undef, NID_undef,
875                                              pwri_tmp, -1, NULL))
876                 goto end;
877             pwri_tmp = NULL;
878         }
879         if (!(flags & CMS_STREAM)) {
880             if (!CMS_final(cms, in, NULL, flags))
881                 goto end;
882         }
883     } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
884         cms = CMS_EncryptedData_encrypt(in, cipher,
885                                         secret_key, secret_keylen, flags);
886
887     } else if (operation == SMIME_SIGN_RECEIPT) {
888         CMS_ContentInfo *srcms = NULL;
889         STACK_OF(CMS_SignerInfo) *sis;
890         CMS_SignerInfo *si;
891         sis = CMS_get0_SignerInfos(cms);
892         if (!sis)
893             goto end;
894         si = sk_CMS_SignerInfo_value(sis, 0);
895         srcms = CMS_sign_receipt(si, signer, key, other, flags);
896         if (!srcms)
897             goto end;
898         CMS_ContentInfo_free(cms);
899         cms = srcms;
900     } else if (operation & SMIME_SIGNERS) {
901         int i;
902         /*
903          * If detached data content we enable streaming if S/MIME output
904          * format.
905          */
906         if (operation == SMIME_SIGN) {
907
908             if (flags & CMS_DETACHED) {
909                 if (outformat == FORMAT_SMIME)
910                     flags |= CMS_STREAM;
911             }
912             flags |= CMS_PARTIAL;
913             cms = CMS_sign(NULL, NULL, other, in, flags);
914             if (!cms)
915                 goto end;
916             if (econtent_type)
917                 CMS_set1_eContentType(cms, econtent_type);
918
919             if (rr_to) {
920                 rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
921                 if (!rr) {
922                     BIO_puts(bio_err,
923                              "Signed Receipt Request Creation Error\n");
924                     goto end;
925                 }
926             }
927         } else
928             flags |= CMS_REUSE_DIGEST;
929         for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
930             CMS_SignerInfo *si;
931             cms_key_param *kparam;
932             int tflags = flags;
933             signerfile = sk_OPENSSL_STRING_value(sksigners, i);
934             keyfile = sk_OPENSSL_STRING_value(skkeys, i);
935
936             signer = load_cert(signerfile, FORMAT_PEM, "signer certificate");
937             if (!signer)
938                 goto end;
939             key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
940             if (!key)
941                 goto end;
942             for (kparam = key_first; kparam; kparam = kparam->next) {
943                 if (kparam->idx == i) {
944                     tflags |= CMS_KEY_PARAM;
945                     break;
946                 }
947             }
948             si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
949             if (!si)
950                 goto end;
951             if (kparam) {
952                 EVP_PKEY_CTX *pctx;
953                 pctx = CMS_SignerInfo_get0_pkey_ctx(si);
954                 if (!cms_set_pkey_param(pctx, kparam->param))
955                     goto end;
956             }
957             if (rr && !CMS_add1_ReceiptRequest(si, rr))
958                 goto end;
959             X509_free(signer);
960             signer = NULL;
961             EVP_PKEY_free(key);
962             key = NULL;
963         }
964         /* If not streaming or resigning finalize structure */
965         if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
966             if (!CMS_final(cms, in, NULL, flags))
967                 goto end;
968         }
969     }
970
971     if (!cms) {
972         BIO_printf(bio_err, "Error creating CMS structure\n");
973         goto end;
974     }
975
976     ret = 4;
977     if (operation == SMIME_DECRYPT) {
978         if (flags & CMS_DEBUG_DECRYPT)
979             CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
980
981         if (secret_key) {
982             if (!CMS_decrypt_set1_key(cms,
983                                       secret_key, secret_keylen,
984                                       secret_keyid, secret_keyidlen)) {
985                 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
986                 goto end;
987             }
988         }
989
990         if (key) {
991             if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
992                 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
993                 goto end;
994             }
995         }
996
997         if (pwri_pass) {
998             if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
999                 BIO_puts(bio_err, "Error decrypting CMS using password\n");
1000                 goto end;
1001             }
1002         }
1003
1004         if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
1005             BIO_printf(bio_err, "Error decrypting CMS structure\n");
1006             goto end;
1007         }
1008     } else if (operation == SMIME_DATAOUT) {
1009         if (!CMS_data(cms, out, flags))
1010             goto end;
1011     } else if (operation == SMIME_UNCOMPRESS) {
1012         if (!CMS_uncompress(cms, indata, out, flags))
1013             goto end;
1014     } else if (operation == SMIME_DIGEST_VERIFY) {
1015         if (CMS_digest_verify(cms, indata, out, flags) > 0)
1016             BIO_printf(bio_err, "Verification successful\n");
1017         else {
1018             BIO_printf(bio_err, "Verification failure\n");
1019             goto end;
1020         }
1021     } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
1022         if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1023                                        indata, out, flags))
1024             goto end;
1025     } else if (operation == SMIME_VERIFY) {
1026         if (CMS_verify(cms, other, store, indata, out, flags) > 0)
1027             BIO_printf(bio_err, "Verification successful\n");
1028         else {
1029             BIO_printf(bio_err, "Verification failure\n");
1030             if (verify_retcode)
1031                 ret = verify_err + 32;
1032             goto end;
1033         }
1034         if (signerfile) {
1035             STACK_OF(X509) *signers;
1036             signers = CMS_get0_signers(cms);
1037             if (!save_certs(signerfile, signers)) {
1038                 BIO_printf(bio_err,
1039                            "Error writing signers to %s\n", signerfile);
1040                 ret = 5;
1041                 goto end;
1042             }
1043             sk_X509_free(signers);
1044         }
1045         if (rr_print)
1046             receipt_request_print(cms);
1047
1048     } else if (operation == SMIME_VERIFY_RECEIPT) {
1049         if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
1050             BIO_printf(bio_err, "Verification successful\n");
1051         else {
1052             BIO_printf(bio_err, "Verification failure\n");
1053             goto end;
1054         }
1055     } else {
1056         if (noout) {
1057             if (print)
1058                 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
1059         } else if (outformat == FORMAT_SMIME) {
1060             if (to)
1061                 BIO_printf(out, "To: %s%s", to, mime_eol);
1062             if (from)
1063                 BIO_printf(out, "From: %s%s", from, mime_eol);
1064             if (subject)
1065                 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
1066             if (operation == SMIME_RESIGN)
1067                 ret = SMIME_write_CMS(out, cms, indata, flags);
1068             else
1069                 ret = SMIME_write_CMS(out, cms, in, flags);
1070         } else if (outformat == FORMAT_PEM)
1071             ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1072         else if (outformat == FORMAT_ASN1)
1073             ret = i2d_CMS_bio_stream(out, cms, in, flags);
1074         else {
1075             BIO_printf(bio_err, "Bad output format for CMS file\n");
1076             goto end;
1077         }
1078         if (ret <= 0) {
1079             ret = 6;
1080             goto end;
1081         }
1082     }
1083     ret = 0;
1084  end:
1085     if (ret)
1086         ERR_print_errors(bio_err);
1087     if (need_rand)
1088         app_RAND_write_file(NULL);
1089     sk_X509_pop_free(encerts, X509_free);
1090     sk_X509_pop_free(other, X509_free);
1091     X509_VERIFY_PARAM_free(vpm);
1092     sk_OPENSSL_STRING_free(sksigners);
1093     sk_OPENSSL_STRING_free(skkeys);
1094     OPENSSL_free(secret_key);
1095     OPENSSL_free(secret_keyid);
1096     OPENSSL_free(pwri_tmp);
1097     ASN1_OBJECT_free(econtent_type);
1098     CMS_ReceiptRequest_free(rr);
1099     sk_OPENSSL_STRING_free(rr_to);
1100     sk_OPENSSL_STRING_free(rr_from);
1101     for (key_param = key_first; key_param;) {
1102         cms_key_param *tparam;
1103         sk_OPENSSL_STRING_free(key_param->param);
1104         tparam = key_param->next;
1105         OPENSSL_free(key_param);
1106         key_param = tparam;
1107     }
1108     X509_STORE_free(store);
1109     X509_free(cert);
1110     X509_free(recip);
1111     X509_free(signer);
1112     EVP_PKEY_free(key);
1113     CMS_ContentInfo_free(cms);
1114     CMS_ContentInfo_free(rcms);
1115     BIO_free(rctin);
1116     BIO_free(in);
1117     BIO_free(indata);
1118     BIO_free_all(out);
1119     OPENSSL_free(passin);
1120     return (ret);
1121 }
1122
1123 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1124 {
1125     int i;
1126     BIO *tmp;
1127     if (!signerfile)
1128         return 1;
1129     tmp = BIO_new_file(signerfile, "w");
1130     if (!tmp)
1131         return 0;
1132     for (i = 0; i < sk_X509_num(signers); i++)
1133         PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1134     BIO_free(tmp);
1135     return 1;
1136 }
1137
1138 /* Minimal callback just to output policy info (if any) */
1139
1140 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1141 {
1142     int error;
1143
1144     error = X509_STORE_CTX_get_error(ctx);
1145
1146     verify_err = error;
1147
1148     if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1149         && ((error != X509_V_OK) || (ok != 2)))
1150         return ok;
1151
1152     policies_print(ctx);
1153
1154     return ok;
1155
1156 }
1157
1158 static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
1159 {
1160     STACK_OF(GENERAL_NAME) *gens;
1161     GENERAL_NAME *gen;
1162     int i, j;
1163
1164     for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1165         gens = sk_GENERAL_NAMES_value(gns, i);
1166         for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1167             gen = sk_GENERAL_NAME_value(gens, j);
1168             BIO_puts(bio_err, "    ");
1169             GENERAL_NAME_print(bio_err, gen);
1170             BIO_puts(bio_err, "\n");
1171         }
1172     }
1173     return;
1174 }
1175
1176 static void receipt_request_print(CMS_ContentInfo *cms)
1177 {
1178     STACK_OF(CMS_SignerInfo) *sis;
1179     CMS_SignerInfo *si;
1180     CMS_ReceiptRequest *rr;
1181     int allorfirst;
1182     STACK_OF(GENERAL_NAMES) *rto, *rlist;
1183     ASN1_STRING *scid;
1184     int i, rv;
1185     sis = CMS_get0_SignerInfos(cms);
1186     for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1187         si = sk_CMS_SignerInfo_value(sis, i);
1188         rv = CMS_get1_ReceiptRequest(si, &rr);
1189         BIO_printf(bio_err, "Signer %d:\n", i + 1);
1190         if (rv == 0)
1191             BIO_puts(bio_err, "  No Receipt Request\n");
1192         else if (rv < 0) {
1193             BIO_puts(bio_err, "  Receipt Request Parse Error\n");
1194             ERR_print_errors(bio_err);
1195         } else {
1196             const char *id;
1197             int idlen;
1198             CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1199                                            &rlist, &rto);
1200             BIO_puts(bio_err, "  Signed Content ID:\n");
1201             idlen = ASN1_STRING_length(scid);
1202             id = (const char *)ASN1_STRING_get0_data(scid);
1203             BIO_dump_indent(bio_err, id, idlen, 4);
1204             BIO_puts(bio_err, "  Receipts From");
1205             if (rlist) {
1206                 BIO_puts(bio_err, " List:\n");
1207                 gnames_stack_print(rlist);
1208             } else if (allorfirst == 1)
1209                 BIO_puts(bio_err, ": First Tier\n");
1210             else if (allorfirst == 0)
1211                 BIO_puts(bio_err, ": All\n");
1212             else
1213                 BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
1214             BIO_puts(bio_err, "  Receipts To:\n");
1215             gnames_stack_print(rto);
1216         }
1217         CMS_ReceiptRequest_free(rr);
1218     }
1219 }
1220
1221 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1222 {
1223     int i;
1224     STACK_OF(GENERAL_NAMES) *ret;
1225     GENERAL_NAMES *gens = NULL;
1226     GENERAL_NAME *gen = NULL;
1227     ret = sk_GENERAL_NAMES_new_null();
1228     if (!ret)
1229         goto err;
1230     for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1231         char *str = sk_OPENSSL_STRING_value(ns, i);
1232         gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1233         if (!gen)
1234             goto err;
1235         gens = GENERAL_NAMES_new();
1236         if (gens == NULL)
1237             goto err;
1238         if (!sk_GENERAL_NAME_push(gens, gen))
1239             goto err;
1240         gen = NULL;
1241         if (!sk_GENERAL_NAMES_push(ret, gens))
1242             goto err;
1243         gens = NULL;
1244     }
1245
1246     return ret;
1247
1248  err:
1249     sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1250     GENERAL_NAMES_free(gens);
1251     GENERAL_NAME_free(gen);
1252     return NULL;
1253 }
1254
1255 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
1256                                                 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
1257                                                 *rr_from)
1258 {
1259     STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
1260     CMS_ReceiptRequest *rr;
1261     rct_to = make_names_stack(rr_to);
1262     if (!rct_to)
1263         goto err;
1264     if (rr_from) {
1265         rct_from = make_names_stack(rr_from);
1266         if (!rct_from)
1267             goto err;
1268     } else
1269         rct_from = NULL;
1270     rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
1271                                     rct_to);
1272     return rr;
1273  err:
1274     sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
1275     return NULL;
1276 }
1277
1278 static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
1279                               STACK_OF(OPENSSL_STRING) *param)
1280 {
1281     char *keyopt;
1282     int i;
1283     if (sk_OPENSSL_STRING_num(param) <= 0)
1284         return 1;
1285     for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
1286         keyopt = sk_OPENSSL_STRING_value(param, i);
1287         if (pkey_ctrl_string(pctx, keyopt) <= 0) {
1288             BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
1289             ERR_print_errors(bio_err);
1290             return 0;
1291         }
1292     }
1293     return 1;
1294 }
1295
1296 #endif