0cc4b46d8e2825591925a8a17c6cd8c83d2cc272
[openssl.git] / apps / cms.c
1 /* apps/cms.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4  * project.
5  */
6 /* ====================================================================
7  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  */
54
55 /* CMS utility function */
56
57 #include <stdio.h>
58 #include <string.h>
59 #include "apps.h"
60
61 #ifndef OPENSSL_NO_CMS
62
63 # include <openssl/crypto.h>
64 # include <openssl/pem.h>
65 # include <openssl/err.h>
66 # include <openssl/x509_vfy.h>
67 # include <openssl/x509v3.h>
68 # include <openssl/cms.h>
69
70 # undef PROG
71 # define PROG cms_main
72 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
73 static int cms_cb(int ok, X509_STORE_CTX *ctx);
74 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms);
75 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
76                                                 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
77                                                 *rr_from);
78
79 # define SMIME_OP        0x10
80 # define SMIME_IP        0x20
81 # define SMIME_SIGNERS   0x40
82 # define SMIME_ENCRYPT           (1 | SMIME_OP)
83 # define SMIME_DECRYPT           (2 | SMIME_IP)
84 # define SMIME_SIGN              (3 | SMIME_OP | SMIME_SIGNERS)
85 # define SMIME_VERIFY            (4 | SMIME_IP)
86 # define SMIME_CMSOUT            (5 | SMIME_IP | SMIME_OP)
87 # define SMIME_RESIGN            (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
88 # define SMIME_DATAOUT           (7 | SMIME_IP)
89 # define SMIME_DATA_CREATE       (8 | SMIME_OP)
90 # define SMIME_DIGEST_VERIFY     (9 | SMIME_IP)
91 # define SMIME_DIGEST_CREATE     (10 | SMIME_OP)
92 # define SMIME_UNCOMPRESS        (11 | SMIME_IP)
93 # define SMIME_COMPRESS          (12 | SMIME_OP)
94 # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
95 # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
96 # define SMIME_SIGN_RECEIPT      (15 | SMIME_IP | SMIME_OP)
97 # define SMIME_VERIFY_RECEIPT    (16 | SMIME_IP)
98
99 int verify_err = 0;
100
101 int MAIN(int, char **);
102
103 int MAIN(int argc, char **argv)
104 {
105     ENGINE *e = NULL;
106     int operation = 0;
107     int ret = 0;
108     char **args;
109     const char *inmode = "r", *outmode = "w";
110     char *infile = NULL, *outfile = NULL, *rctfile = NULL;
111     char *signerfile = NULL, *recipfile = NULL;
112     STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
113     char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
114     char *certsoutfile = NULL;
115     const EVP_CIPHER *cipher = NULL;
116     CMS_ContentInfo *cms = NULL, *rcms = NULL;
117     X509_STORE *store = NULL;
118     X509 *cert = NULL, *recip = NULL, *signer = NULL;
119     EVP_PKEY *key = NULL;
120     STACK_OF(X509) *encerts = NULL, *other = NULL;
121     BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
122     int badarg = 0;
123     int flags = CMS_DETACHED, noout = 0, print = 0;
124     int verify_retcode = 0;
125     int rr_print = 0, rr_allorfirst = -1;
126     STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
127     CMS_ReceiptRequest *rr = NULL;
128     char *to = NULL, *from = NULL, *subject = NULL;
129     char *CAfile = NULL, *CApath = NULL;
130     char *passargin = NULL, *passin = NULL;
131     char *inrand = NULL;
132     int need_rand = 0;
133     const EVP_MD *sign_md = NULL;
134     int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
135     int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
136 # ifndef OPENSSL_NO_ENGINE
137     char *engine = NULL;
138 # endif
139     unsigned char *secret_key = NULL, *secret_keyid = NULL;
140     unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
141     size_t secret_keylen = 0, secret_keyidlen = 0;
142
143     ASN1_OBJECT *econtent_type = NULL;
144
145     X509_VERIFY_PARAM *vpm = NULL;
146
147     args = argv + 1;
148     ret = 1;
149
150     apps_startup();
151
152     if (bio_err == NULL) {
153         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
154             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
155     }
156
157     if (!load_config(bio_err, NULL))
158         goto end;
159
160     while (!badarg && *args && *args[0] == '-') {
161         if (!strcmp(*args, "-encrypt"))
162             operation = SMIME_ENCRYPT;
163         else if (!strcmp(*args, "-decrypt"))
164             operation = SMIME_DECRYPT;
165         else if (!strcmp(*args, "-sign"))
166             operation = SMIME_SIGN;
167         else if (!strcmp(*args, "-sign_receipt"))
168             operation = SMIME_SIGN_RECEIPT;
169         else if (!strcmp(*args, "-resign"))
170             operation = SMIME_RESIGN;
171         else if (!strcmp(*args, "-verify"))
172             operation = SMIME_VERIFY;
173         else if (!strcmp(*args, "-verify_retcode"))
174             verify_retcode = 1;
175         else if (!strcmp(*args, "-verify_receipt")) {
176             operation = SMIME_VERIFY_RECEIPT;
177             if (!args[1])
178                 goto argerr;
179             args++;
180             rctfile = *args;
181         } else if (!strcmp(*args, "-cmsout"))
182             operation = SMIME_CMSOUT;
183         else if (!strcmp(*args, "-data_out"))
184             operation = SMIME_DATAOUT;
185         else if (!strcmp(*args, "-data_create"))
186             operation = SMIME_DATA_CREATE;
187         else if (!strcmp(*args, "-digest_verify"))
188             operation = SMIME_DIGEST_VERIFY;
189         else if (!strcmp(*args, "-digest_create"))
190             operation = SMIME_DIGEST_CREATE;
191         else if (!strcmp(*args, "-compress"))
192             operation = SMIME_COMPRESS;
193         else if (!strcmp(*args, "-uncompress"))
194             operation = SMIME_UNCOMPRESS;
195         else if (!strcmp(*args, "-EncryptedData_decrypt"))
196             operation = SMIME_ENCRYPTED_DECRYPT;
197         else if (!strcmp(*args, "-EncryptedData_encrypt"))
198             operation = SMIME_ENCRYPTED_ENCRYPT;
199 # ifndef OPENSSL_NO_DES
200         else if (!strcmp(*args, "-des3"))
201             cipher = EVP_des_ede3_cbc();
202         else if (!strcmp(*args, "-des"))
203             cipher = EVP_des_cbc();
204 # endif
205 # ifndef OPENSSL_NO_SEED
206         else if (!strcmp(*args, "-seed"))
207             cipher = EVP_seed_cbc();
208 # endif
209 # ifndef OPENSSL_NO_RC2
210         else if (!strcmp(*args, "-rc2-40"))
211             cipher = EVP_rc2_40_cbc();
212         else if (!strcmp(*args, "-rc2-128"))
213             cipher = EVP_rc2_cbc();
214         else if (!strcmp(*args, "-rc2-64"))
215             cipher = EVP_rc2_64_cbc();
216 # endif
217 # ifndef OPENSSL_NO_AES
218         else if (!strcmp(*args, "-aes128"))
219             cipher = EVP_aes_128_cbc();
220         else if (!strcmp(*args, "-aes192"))
221             cipher = EVP_aes_192_cbc();
222         else if (!strcmp(*args, "-aes256"))
223             cipher = EVP_aes_256_cbc();
224 # endif
225 # ifndef OPENSSL_NO_CAMELLIA
226         else if (!strcmp(*args, "-camellia128"))
227             cipher = EVP_camellia_128_cbc();
228         else if (!strcmp(*args, "-camellia192"))
229             cipher = EVP_camellia_192_cbc();
230         else if (!strcmp(*args, "-camellia256"))
231             cipher = EVP_camellia_256_cbc();
232 # endif
233         else if (!strcmp(*args, "-debug_decrypt"))
234             flags |= CMS_DEBUG_DECRYPT;
235         else if (!strcmp(*args, "-text"))
236             flags |= CMS_TEXT;
237         else if (!strcmp(*args, "-nointern"))
238             flags |= CMS_NOINTERN;
239         else if (!strcmp(*args, "-noverify")
240                  || !strcmp(*args, "-no_signer_cert_verify"))
241             flags |= CMS_NO_SIGNER_CERT_VERIFY;
242         else if (!strcmp(*args, "-nocerts"))
243             flags |= CMS_NOCERTS;
244         else if (!strcmp(*args, "-noattr"))
245             flags |= CMS_NOATTR;
246         else if (!strcmp(*args, "-nodetach"))
247             flags &= ~CMS_DETACHED;
248         else if (!strcmp(*args, "-nosmimecap"))
249             flags |= CMS_NOSMIMECAP;
250         else if (!strcmp(*args, "-binary"))
251             flags |= CMS_BINARY;
252         else if (!strcmp(*args, "-keyid"))
253             flags |= CMS_USE_KEYID;
254         else if (!strcmp(*args, "-nosigs"))
255             flags |= CMS_NOSIGS;
256         else if (!strcmp(*args, "-no_content_verify"))
257             flags |= CMS_NO_CONTENT_VERIFY;
258         else if (!strcmp(*args, "-no_attr_verify"))
259             flags |= CMS_NO_ATTR_VERIFY;
260         else if (!strcmp(*args, "-stream"))
261             flags |= CMS_STREAM;
262         else if (!strcmp(*args, "-indef"))
263             flags |= CMS_STREAM;
264         else if (!strcmp(*args, "-noindef"))
265             flags &= ~CMS_STREAM;
266         else if (!strcmp(*args, "-nooldmime"))
267             flags |= CMS_NOOLDMIMETYPE;
268         else if (!strcmp(*args, "-crlfeol"))
269             flags |= CMS_CRLFEOL;
270         else if (!strcmp(*args, "-noout"))
271             noout = 1;
272         else if (!strcmp(*args, "-receipt_request_print"))
273             rr_print = 1;
274         else if (!strcmp(*args, "-receipt_request_all"))
275             rr_allorfirst = 0;
276         else if (!strcmp(*args, "-receipt_request_first"))
277             rr_allorfirst = 1;
278         else if (!strcmp(*args, "-receipt_request_from")) {
279             if (!args[1])
280                 goto argerr;
281             args++;
282             if (!rr_from)
283                 rr_from = sk_OPENSSL_STRING_new_null();
284             sk_OPENSSL_STRING_push(rr_from, *args);
285         } else if (!strcmp(*args, "-receipt_request_to")) {
286             if (!args[1])
287                 goto argerr;
288             args++;
289             if (!rr_to)
290                 rr_to = sk_OPENSSL_STRING_new_null();
291             sk_OPENSSL_STRING_push(rr_to, *args);
292         } else if (!strcmp(*args, "-print")) {
293             noout = 1;
294             print = 1;
295         } else if (!strcmp(*args, "-secretkey")) {
296             long ltmp;
297             if (!args[1])
298                 goto argerr;
299             args++;
300             secret_key = string_to_hex(*args, &ltmp);
301             if (!secret_key) {
302                 BIO_printf(bio_err, "Invalid key %s\n", *args);
303                 goto argerr;
304             }
305             secret_keylen = (size_t)ltmp;
306         } else if (!strcmp(*args, "-secretkeyid")) {
307             long ltmp;
308             if (!args[1])
309                 goto argerr;
310             args++;
311             secret_keyid = string_to_hex(*args, &ltmp);
312             if (!secret_keyid) {
313                 BIO_printf(bio_err, "Invalid id %s\n", *args);
314                 goto argerr;
315             }
316             secret_keyidlen = (size_t)ltmp;
317         } else if (!strcmp(*args, "-pwri_password")) {
318             if (!args[1])
319                 goto argerr;
320             args++;
321             pwri_pass = (unsigned char *)*args;
322         } else if (!strcmp(*args, "-econtent_type")) {
323             if (!args[1])
324                 goto argerr;
325             args++;
326             econtent_type = OBJ_txt2obj(*args, 0);
327             if (!econtent_type) {
328                 BIO_printf(bio_err, "Invalid OID %s\n", *args);
329                 goto argerr;
330             }
331         } else if (!strcmp(*args, "-rand")) {
332             if (!args[1])
333                 goto argerr;
334             args++;
335             inrand = *args;
336             need_rand = 1;
337         }
338 # ifndef OPENSSL_NO_ENGINE
339         else if (!strcmp(*args, "-engine")) {
340             if (!args[1])
341                 goto argerr;
342             engine = *++args;
343         }
344 # endif
345         else if (!strcmp(*args, "-passin")) {
346             if (!args[1])
347                 goto argerr;
348             passargin = *++args;
349         } else if (!strcmp(*args, "-to")) {
350             if (!args[1])
351                 goto argerr;
352             to = *++args;
353         } else if (!strcmp(*args, "-from")) {
354             if (!args[1])
355                 goto argerr;
356             from = *++args;
357         } else if (!strcmp(*args, "-subject")) {
358             if (!args[1])
359                 goto argerr;
360             subject = *++args;
361         } else if (!strcmp(*args, "-signer")) {
362             if (!args[1])
363                 goto argerr;
364             /* If previous -signer argument add signer to list */
365
366             if (signerfile) {
367                 if (!sksigners)
368                     sksigners = sk_OPENSSL_STRING_new_null();
369                 sk_OPENSSL_STRING_push(sksigners, signerfile);
370                 if (!keyfile)
371                     keyfile = signerfile;
372                 if (!skkeys)
373                     skkeys = sk_OPENSSL_STRING_new_null();
374                 sk_OPENSSL_STRING_push(skkeys, keyfile);
375                 keyfile = NULL;
376             }
377             signerfile = *++args;
378         } else if (!strcmp(*args, "-recip")) {
379             if (!args[1])
380                 goto argerr;
381             recipfile = *++args;
382         } else if (!strcmp(*args, "-certsout")) {
383             if (!args[1])
384                 goto argerr;
385             certsoutfile = *++args;
386         } else if (!strcmp(*args, "-md")) {
387             if (!args[1])
388                 goto argerr;
389             sign_md = EVP_get_digestbyname(*++args);
390             if (sign_md == NULL) {
391                 BIO_printf(bio_err, "Unknown digest %s\n", *args);
392                 goto argerr;
393             }
394         } else if (!strcmp(*args, "-inkey")) {
395             if (!args[1])
396                 goto argerr;
397             /* If previous -inkey arument add signer to list */
398             if (keyfile) {
399                 if (!signerfile) {
400                     BIO_puts(bio_err, "Illegal -inkey without -signer\n");
401                     goto argerr;
402                 }
403                 if (!sksigners)
404                     sksigners = sk_OPENSSL_STRING_new_null();
405                 sk_OPENSSL_STRING_push(sksigners, signerfile);
406                 signerfile = NULL;
407                 if (!skkeys)
408                     skkeys = sk_OPENSSL_STRING_new_null();
409                 sk_OPENSSL_STRING_push(skkeys, keyfile);
410             }
411             keyfile = *++args;
412         } else if (!strcmp(*args, "-keyform")) {
413             if (!args[1])
414                 goto argerr;
415             keyform = str2fmt(*++args);
416         } else if (!strcmp(*args, "-rctform")) {
417             if (!args[1])
418                 goto argerr;
419             rctformat = str2fmt(*++args);
420         } else if (!strcmp(*args, "-certfile")) {
421             if (!args[1])
422                 goto argerr;
423             certfile = *++args;
424         } else if (!strcmp(*args, "-CAfile")) {
425             if (!args[1])
426                 goto argerr;
427             CAfile = *++args;
428         } else if (!strcmp(*args, "-CApath")) {
429             if (!args[1])
430                 goto argerr;
431             CApath = *++args;
432         } else if (!strcmp(*args, "-in")) {
433             if (!args[1])
434                 goto argerr;
435             infile = *++args;
436         } else if (!strcmp(*args, "-inform")) {
437             if (!args[1])
438                 goto argerr;
439             informat = str2fmt(*++args);
440         } else if (!strcmp(*args, "-outform")) {
441             if (!args[1])
442                 goto argerr;
443             outformat = str2fmt(*++args);
444         } else if (!strcmp(*args, "-out")) {
445             if (!args[1])
446                 goto argerr;
447             outfile = *++args;
448         } else if (!strcmp(*args, "-content")) {
449             if (!args[1])
450                 goto argerr;
451             contfile = *++args;
452         } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
453             continue;
454         else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
455             badarg = 1;
456         args++;
457     }
458
459     if (((rr_allorfirst != -1) || rr_from) && !rr_to) {
460         BIO_puts(bio_err, "No Signed Receipts Recipients\n");
461         goto argerr;
462     }
463
464     if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
465         BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
466         goto argerr;
467     }
468     if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
469         BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
470         goto argerr;
471     }
472
473     if (operation & SMIME_SIGNERS) {
474         if (keyfile && !signerfile) {
475             BIO_puts(bio_err, "Illegal -inkey without -signer\n");
476             goto argerr;
477         }
478         /* Check to see if any final signer needs to be appended */
479         if (signerfile) {
480             if (!sksigners)
481                 sksigners = sk_OPENSSL_STRING_new_null();
482             sk_OPENSSL_STRING_push(sksigners, signerfile);
483             if (!skkeys)
484                 skkeys = sk_OPENSSL_STRING_new_null();
485             if (!keyfile)
486                 keyfile = signerfile;
487             sk_OPENSSL_STRING_push(skkeys, keyfile);
488         }
489         if (!sksigners) {
490             BIO_printf(bio_err, "No signer certificate specified\n");
491             badarg = 1;
492         }
493         signerfile = NULL;
494         keyfile = NULL;
495         need_rand = 1;
496     }
497
498     else if (operation == SMIME_DECRYPT) {
499         if (!recipfile && !keyfile && !secret_key && !pwri_pass) {
500             BIO_printf(bio_err,
501                        "No recipient certificate or key specified\n");
502             badarg = 1;
503         }
504     } else if (operation == SMIME_ENCRYPT) {
505         if (!*args && !secret_key && !pwri_pass) {
506             BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
507             badarg = 1;
508         }
509         need_rand = 1;
510     } else if (!operation)
511         badarg = 1;
512
513     if (badarg) {
514  argerr:
515         BIO_printf(bio_err, "Usage cms [options] cert.pem ...\n");
516         BIO_printf(bio_err, "where options are\n");
517         BIO_printf(bio_err, "-encrypt       encrypt message\n");
518         BIO_printf(bio_err, "-decrypt       decrypt encrypted message\n");
519         BIO_printf(bio_err, "-sign          sign message\n");
520         BIO_printf(bio_err, "-verify        verify signed message\n");
521         BIO_printf(bio_err, "-cmsout        output CMS structure\n");
522 # ifndef OPENSSL_NO_DES
523         BIO_printf(bio_err, "-des3          encrypt with triple DES\n");
524         BIO_printf(bio_err, "-des           encrypt with DES\n");
525 # endif
526 # ifndef OPENSSL_NO_SEED
527         BIO_printf(bio_err, "-seed          encrypt with SEED\n");
528 # endif
529 # ifndef OPENSSL_NO_RC2
530         BIO_printf(bio_err, "-rc2-40        encrypt with RC2-40 (default)\n");
531         BIO_printf(bio_err, "-rc2-64        encrypt with RC2-64\n");
532         BIO_printf(bio_err, "-rc2-128       encrypt with RC2-128\n");
533 # endif
534 # ifndef OPENSSL_NO_AES
535         BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
536         BIO_printf(bio_err,
537                    "               encrypt PEM output with cbc aes\n");
538 # endif
539 # ifndef OPENSSL_NO_CAMELLIA
540         BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
541         BIO_printf(bio_err,
542                    "               encrypt PEM output with cbc camellia\n");
543 # endif
544         BIO_printf(bio_err,
545                    "-nointern      don't search certificates in message for signer\n");
546         BIO_printf(bio_err,
547                    "-nosigs        don't verify message signature\n");
548         BIO_printf(bio_err,
549                    "-noverify      don't verify signers certificate\n");
550         BIO_printf(bio_err,
551                    "-nocerts       don't include signers certificate when signing\n");
552         BIO_printf(bio_err, "-nodetach      use opaque signing\n");
553         BIO_printf(bio_err,
554                    "-noattr        don't include any signed attributes\n");
555         BIO_printf(bio_err,
556                    "-binary        don't translate message to text\n");
557         BIO_printf(bio_err, "-certfile file other certificates file\n");
558         BIO_printf(bio_err, "-certsout file certificate output file\n");
559         BIO_printf(bio_err, "-signer file   signer certificate file\n");
560         BIO_printf(bio_err,
561                    "-recip  file   recipient certificate file for decryption\n");
562         BIO_printf(bio_err, "-keyid         use subject key identifier\n");
563         BIO_printf(bio_err, "-in file       input file\n");
564         BIO_printf(bio_err,
565                    "-inform arg    input format SMIME (default), PEM or DER\n");
566         BIO_printf(bio_err,
567                    "-inkey file    input private key (if not signer or recipient)\n");
568         BIO_printf(bio_err,
569                    "-keyform arg   input private key format (PEM or ENGINE)\n");
570         BIO_printf(bio_err, "-out file      output file\n");
571         BIO_printf(bio_err,
572                    "-outform arg   output format SMIME (default), PEM or DER\n");
573         BIO_printf(bio_err,
574                    "-content file  supply or override content for detached signature\n");
575         BIO_printf(bio_err, "-to addr       to address\n");
576         BIO_printf(bio_err, "-from ad       from address\n");
577         BIO_printf(bio_err, "-subject s     subject\n");
578         BIO_printf(bio_err,
579                    "-text          include or delete text MIME headers\n");
580         BIO_printf(bio_err,
581                    "-CApath dir    trusted certificates directory\n");
582         BIO_printf(bio_err, "-CAfile file   trusted certificates file\n");
583         BIO_printf(bio_err,
584                    "-crl_check     check revocation status of signer's certificate using CRLs\n");
585         BIO_printf(bio_err,
586                    "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
587 # ifndef OPENSSL_NO_ENGINE
588         BIO_printf(bio_err,
589                    "-engine e      use engine e, possibly a hardware device.\n");
590 # endif
591         BIO_printf(bio_err, "-passin arg    input file pass phrase source\n");
592         BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
593                    LIST_SEPARATOR_CHAR);
594         BIO_printf(bio_err,
595                    "               load the file (or the files in the directory) into\n");
596         BIO_printf(bio_err, "               the random number generator\n");
597         BIO_printf(bio_err,
598                    "cert.pem       recipient certificate(s) for encryption\n");
599         goto end;
600     }
601 # ifndef OPENSSL_NO_ENGINE
602     e = setup_engine(bio_err, engine, 0);
603 # endif
604
605     if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
606         BIO_printf(bio_err, "Error getting password\n");
607         goto end;
608     }
609
610     if (need_rand) {
611         app_RAND_load_file(NULL, bio_err, (inrand != NULL));
612         if (inrand != NULL)
613             BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
614                        app_RAND_load_files(inrand));
615     }
616
617     ret = 2;
618
619     if (!(operation & SMIME_SIGNERS))
620         flags &= ~CMS_DETACHED;
621
622     if (operation & SMIME_OP) {
623         if (outformat == FORMAT_ASN1)
624             outmode = "wb";
625     } else {
626         if (flags & CMS_BINARY)
627             outmode = "wb";
628     }
629
630     if (operation & SMIME_IP) {
631         if (informat == FORMAT_ASN1)
632             inmode = "rb";
633     } else {
634         if (flags & CMS_BINARY)
635             inmode = "rb";
636     }
637
638     if (operation == SMIME_ENCRYPT) {
639         if (!cipher) {
640 # ifndef OPENSSL_NO_DES
641             cipher = EVP_des_ede3_cbc();
642 # else
643             BIO_printf(bio_err, "No cipher selected\n");
644             goto end;
645 # endif
646         }
647
648         if (secret_key && !secret_keyid) {
649             BIO_printf(bio_err, "No secret key id\n");
650             goto end;
651         }
652
653         if (*args)
654             encerts = sk_X509_new_null();
655         while (*args) {
656             if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
657                                    NULL, e, "recipient certificate file")))
658                 goto end;
659             sk_X509_push(encerts, cert);
660             cert = NULL;
661             args++;
662         }
663     }
664
665     if (certfile) {
666         if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
667                                  e, "certificate file"))) {
668             ERR_print_errors(bio_err);
669             goto end;
670         }
671     }
672
673     if (recipfile && (operation == SMIME_DECRYPT)) {
674         if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
675                                 e, "recipient certificate file"))) {
676             ERR_print_errors(bio_err);
677             goto end;
678         }
679     }
680
681     if (operation == SMIME_SIGN_RECEIPT) {
682         if (!(signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
683                                  e, "receipt signer certificate file"))) {
684             ERR_print_errors(bio_err);
685             goto end;
686         }
687     }
688
689     if (operation == SMIME_DECRYPT) {
690         if (!keyfile)
691             keyfile = recipfile;
692     } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
693         if (!keyfile)
694             keyfile = signerfile;
695     } else
696         keyfile = NULL;
697
698     if (keyfile) {
699         key = load_key(bio_err, keyfile, keyform, 0, passin, e,
700                        "signing key file");
701         if (!key)
702             goto end;
703     }
704
705     if (infile) {
706         if (!(in = BIO_new_file(infile, inmode))) {
707             BIO_printf(bio_err, "Can't open input file %s\n", infile);
708             goto end;
709         }
710     } else
711         in = BIO_new_fp(stdin, BIO_NOCLOSE);
712
713     if (operation & SMIME_IP) {
714         if (informat == FORMAT_SMIME)
715             cms = SMIME_read_CMS(in, &indata);
716         else if (informat == FORMAT_PEM)
717             cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
718         else if (informat == FORMAT_ASN1)
719             cms = d2i_CMS_bio(in, NULL);
720         else {
721             BIO_printf(bio_err, "Bad input format for CMS file\n");
722             goto end;
723         }
724
725         if (!cms) {
726             BIO_printf(bio_err, "Error reading S/MIME message\n");
727             goto end;
728         }
729         if (contfile) {
730             BIO_free(indata);
731             if (!(indata = BIO_new_file(contfile, "rb"))) {
732                 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
733                 goto end;
734             }
735         }
736         if (certsoutfile) {
737             STACK_OF(X509) *allcerts;
738             allcerts = CMS_get1_certs(cms);
739             if (!save_certs(certsoutfile, allcerts)) {
740                 BIO_printf(bio_err,
741                            "Error writing certs to %s\n", certsoutfile);
742                 ret = 5;
743                 goto end;
744             }
745             sk_X509_pop_free(allcerts, X509_free);
746         }
747     }
748
749     if (rctfile) {
750         char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
751         if (!(rctin = BIO_new_file(rctfile, rctmode))) {
752             BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
753             goto end;
754         }
755
756         if (rctformat == FORMAT_SMIME)
757             rcms = SMIME_read_CMS(rctin, NULL);
758         else if (rctformat == FORMAT_PEM)
759             rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
760         else if (rctformat == FORMAT_ASN1)
761             rcms = d2i_CMS_bio(rctin, NULL);
762         else {
763             BIO_printf(bio_err, "Bad input format for receipt\n");
764             goto end;
765         }
766
767         if (!rcms) {
768             BIO_printf(bio_err, "Error reading receipt\n");
769             goto end;
770         }
771     }
772
773     if (outfile) {
774         if (!(out = BIO_new_file(outfile, outmode))) {
775             BIO_printf(bio_err, "Can't open output file %s\n", outfile);
776             goto end;
777         }
778     } else {
779         out = BIO_new_fp(stdout, BIO_NOCLOSE);
780 # ifdef OPENSSL_SYS_VMS
781         {
782             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
783             out = BIO_push(tmpbio, out);
784         }
785 # endif
786     }
787
788     if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
789         if (!(store = setup_verify(bio_err, CAfile, CApath)))
790             goto end;
791         X509_STORE_set_verify_cb(store, cms_cb);
792         if (vpm)
793             X509_STORE_set1_param(store, vpm);
794     }
795
796     ret = 3;
797
798     if (operation == SMIME_DATA_CREATE) {
799         cms = CMS_data_create(in, flags);
800     } else if (operation == SMIME_DIGEST_CREATE) {
801         cms = CMS_digest_create(in, sign_md, flags);
802     } else if (operation == SMIME_COMPRESS) {
803         cms = CMS_compress(in, -1, flags);
804     } else if (operation == SMIME_ENCRYPT) {
805         flags |= CMS_PARTIAL;
806         cms = CMS_encrypt(encerts, in, cipher, flags);
807         if (!cms)
808             goto end;
809         if (secret_key) {
810             if (!CMS_add0_recipient_key(cms, NID_undef,
811                                         secret_key, secret_keylen,
812                                         secret_keyid, secret_keyidlen,
813                                         NULL, NULL, NULL))
814                 goto end;
815             /* NULL these because call absorbs them */
816             secret_key = NULL;
817             secret_keyid = NULL;
818         }
819         if (pwri_pass) {
820             pwri_tmp = (unsigned char *)BUF_strdup((char *)pwri_pass);
821             if (!pwri_tmp)
822                 goto end;
823             if (!CMS_add0_recipient_password(cms,
824                                              -1, NID_undef, NID_undef,
825                                              pwri_tmp, -1, NULL))
826                 goto end;
827             pwri_tmp = NULL;
828         }
829         if (!(flags & CMS_STREAM)) {
830             if (!CMS_final(cms, in, NULL, flags))
831                 goto end;
832         }
833     } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
834         cms = CMS_EncryptedData_encrypt(in, cipher,
835                                         secret_key, secret_keylen, flags);
836
837     } else if (operation == SMIME_SIGN_RECEIPT) {
838         CMS_ContentInfo *srcms = NULL;
839         STACK_OF(CMS_SignerInfo) *sis;
840         CMS_SignerInfo *si;
841         sis = CMS_get0_SignerInfos(cms);
842         if (!sis)
843             goto end;
844         si = sk_CMS_SignerInfo_value(sis, 0);
845         srcms = CMS_sign_receipt(si, signer, key, other, flags);
846         if (!srcms)
847             goto end;
848         CMS_ContentInfo_free(cms);
849         cms = srcms;
850     } else if (operation & SMIME_SIGNERS) {
851         int i;
852         /*
853          * If detached data content we enable streaming if S/MIME output
854          * format.
855          */
856         if (operation == SMIME_SIGN) {
857
858             if (flags & CMS_DETACHED) {
859                 if (outformat == FORMAT_SMIME)
860                     flags |= CMS_STREAM;
861             }
862             flags |= CMS_PARTIAL;
863             cms = CMS_sign(NULL, NULL, other, in, flags);
864             if (!cms)
865                 goto end;
866             if (econtent_type)
867                 CMS_set1_eContentType(cms, econtent_type);
868
869             if (rr_to) {
870                 rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
871                 if (!rr) {
872                     BIO_puts(bio_err,
873                              "Signed Receipt Request Creation Error\n");
874                     goto end;
875                 }
876             }
877         } else
878             flags |= CMS_REUSE_DIGEST;
879         for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
880             CMS_SignerInfo *si;
881             signerfile = sk_OPENSSL_STRING_value(sksigners, i);
882             keyfile = sk_OPENSSL_STRING_value(skkeys, i);
883             signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
884                                e, "signer certificate");
885             if (!signer)
886                 goto end;
887             key = load_key(bio_err, keyfile, keyform, 0, passin, e,
888                            "signing key file");
889             if (!key)
890                 goto end;
891             si = CMS_add1_signer(cms, signer, key, sign_md, flags);
892             if (!si)
893                 goto end;
894             if (rr && !CMS_add1_ReceiptRequest(si, rr))
895                 goto end;
896             X509_free(signer);
897             signer = NULL;
898             EVP_PKEY_free(key);
899             key = NULL;
900         }
901         /* If not streaming or resigning finalize structure */
902         if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
903             if (!CMS_final(cms, in, NULL, flags))
904                 goto end;
905         }
906     }
907
908     if (!cms) {
909         BIO_printf(bio_err, "Error creating CMS structure\n");
910         goto end;
911     }
912
913     ret = 4;
914     if (operation == SMIME_DECRYPT) {
915         if (flags & CMS_DEBUG_DECRYPT)
916             CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
917
918         if (secret_key) {
919             if (!CMS_decrypt_set1_key(cms,
920                                       secret_key, secret_keylen,
921                                       secret_keyid, secret_keyidlen)) {
922                 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
923                 goto end;
924             }
925         }
926
927         if (key) {
928             if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
929                 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
930                 goto end;
931             }
932         }
933
934         if (pwri_pass) {
935             if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
936                 BIO_puts(bio_err, "Error decrypting CMS using password\n");
937                 goto end;
938             }
939         }
940
941         if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
942             BIO_printf(bio_err, "Error decrypting CMS structure\n");
943             goto end;
944         }
945     } else if (operation == SMIME_DATAOUT) {
946         if (!CMS_data(cms, out, flags))
947             goto end;
948     } else if (operation == SMIME_UNCOMPRESS) {
949         if (!CMS_uncompress(cms, indata, out, flags))
950             goto end;
951     } else if (operation == SMIME_DIGEST_VERIFY) {
952         if (CMS_digest_verify(cms, indata, out, flags) > 0)
953             BIO_printf(bio_err, "Verification successful\n");
954         else {
955             BIO_printf(bio_err, "Verification failure\n");
956             goto end;
957         }
958     } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
959         if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
960                                        indata, out, flags))
961             goto end;
962     } else if (operation == SMIME_VERIFY) {
963         if (CMS_verify(cms, other, store, indata, out, flags) > 0)
964             BIO_printf(bio_err, "Verification successful\n");
965         else {
966             BIO_printf(bio_err, "Verification failure\n");
967             if (verify_retcode)
968                 ret = verify_err + 32;
969             goto end;
970         }
971         if (signerfile) {
972             STACK_OF(X509) *signers;
973             signers = CMS_get0_signers(cms);
974             if (!save_certs(signerfile, signers)) {
975                 BIO_printf(bio_err,
976                            "Error writing signers to %s\n", signerfile);
977                 ret = 5;
978                 goto end;
979             }
980             sk_X509_free(signers);
981         }
982         if (rr_print)
983             receipt_request_print(bio_err, cms);
984
985     } else if (operation == SMIME_VERIFY_RECEIPT) {
986         if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
987             BIO_printf(bio_err, "Verification successful\n");
988         else {
989             BIO_printf(bio_err, "Verification failure\n");
990             goto end;
991         }
992     } else {
993         if (noout) {
994             if (print)
995                 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
996         } else if (outformat == FORMAT_SMIME) {
997             if (to)
998                 BIO_printf(out, "To: %s\n", to);
999             if (from)
1000                 BIO_printf(out, "From: %s\n", from);
1001             if (subject)
1002                 BIO_printf(out, "Subject: %s\n", subject);
1003             if (operation == SMIME_RESIGN)
1004                 ret = SMIME_write_CMS(out, cms, indata, flags);
1005             else
1006                 ret = SMIME_write_CMS(out, cms, in, flags);
1007         } else if (outformat == FORMAT_PEM)
1008             ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1009         else if (outformat == FORMAT_ASN1)
1010             ret = i2d_CMS_bio_stream(out, cms, in, flags);
1011         else {
1012             BIO_printf(bio_err, "Bad output format for CMS file\n");
1013             goto end;
1014         }
1015         if (ret <= 0) {
1016             ret = 6;
1017             goto end;
1018         }
1019     }
1020     ret = 0;
1021  end:
1022     if (ret)
1023         ERR_print_errors(bio_err);
1024     if (need_rand)
1025         app_RAND_write_file(NULL, bio_err);
1026     sk_X509_pop_free(encerts, X509_free);
1027     sk_X509_pop_free(other, X509_free);
1028     if (vpm)
1029         X509_VERIFY_PARAM_free(vpm);
1030     if (sksigners)
1031         sk_OPENSSL_STRING_free(sksigners);
1032     if (skkeys)
1033         sk_OPENSSL_STRING_free(skkeys);
1034     if (secret_key)
1035         OPENSSL_free(secret_key);
1036     if (secret_keyid)
1037         OPENSSL_free(secret_keyid);
1038     if (pwri_tmp)
1039         OPENSSL_free(pwri_tmp);
1040     if (econtent_type)
1041         ASN1_OBJECT_free(econtent_type);
1042     if (rr)
1043         CMS_ReceiptRequest_free(rr);
1044     if (rr_to)
1045         sk_OPENSSL_STRING_free(rr_to);
1046     if (rr_from)
1047         sk_OPENSSL_STRING_free(rr_from);
1048     X509_STORE_free(store);
1049     X509_free(cert);
1050     X509_free(recip);
1051     X509_free(signer);
1052     EVP_PKEY_free(key);
1053     CMS_ContentInfo_free(cms);
1054     CMS_ContentInfo_free(rcms);
1055     BIO_free(rctin);
1056     BIO_free(in);
1057     BIO_free(indata);
1058     BIO_free_all(out);
1059     if (passin)
1060         OPENSSL_free(passin);
1061     return (ret);
1062 }
1063
1064 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1065 {
1066     int i;
1067     BIO *tmp;
1068     if (!signerfile)
1069         return 1;
1070     tmp = BIO_new_file(signerfile, "w");
1071     if (!tmp)
1072         return 0;
1073     for (i = 0; i < sk_X509_num(signers); i++)
1074         PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1075     BIO_free(tmp);
1076     return 1;
1077 }
1078
1079 /* Minimal callback just to output policy info (if any) */
1080
1081 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1082 {
1083     int error;
1084
1085     error = X509_STORE_CTX_get_error(ctx);
1086
1087     verify_err = error;
1088
1089     if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1090         && ((error != X509_V_OK) || (ok != 2)))
1091         return ok;
1092
1093     policies_print(NULL, ctx);
1094
1095     return ok;
1096
1097 }
1098
1099 static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
1100 {
1101     STACK_OF(GENERAL_NAME) *gens;
1102     GENERAL_NAME *gen;
1103     int i, j;
1104     for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1105         gens = sk_GENERAL_NAMES_value(gns, i);
1106         for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1107             gen = sk_GENERAL_NAME_value(gens, j);
1108             BIO_puts(out, "    ");
1109             GENERAL_NAME_print(out, gen);
1110             BIO_puts(out, "\n");
1111         }
1112     }
1113     return;
1114 }
1115
1116 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
1117 {
1118     STACK_OF(CMS_SignerInfo) *sis;
1119     CMS_SignerInfo *si;
1120     CMS_ReceiptRequest *rr;
1121     int allorfirst;
1122     STACK_OF(GENERAL_NAMES) *rto, *rlist;
1123     ASN1_STRING *scid;
1124     int i, rv;
1125     sis = CMS_get0_SignerInfos(cms);
1126     for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1127         si = sk_CMS_SignerInfo_value(sis, i);
1128         rv = CMS_get1_ReceiptRequest(si, &rr);
1129         BIO_printf(bio_err, "Signer %d:\n", i + 1);
1130         if (rv == 0)
1131             BIO_puts(bio_err, "  No Receipt Request\n");
1132         else if (rv < 0) {
1133             BIO_puts(bio_err, "  Receipt Request Parse Error\n");
1134             ERR_print_errors(bio_err);
1135         } else {
1136             char *id;
1137             int idlen;
1138             CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1139                                            &rlist, &rto);
1140             BIO_puts(out, "  Signed Content ID:\n");
1141             idlen = ASN1_STRING_length(scid);
1142             id = (char *)ASN1_STRING_data(scid);
1143             BIO_dump_indent(out, id, idlen, 4);
1144             BIO_puts(out, "  Receipts From");
1145             if (rlist) {
1146                 BIO_puts(out, " List:\n");
1147                 gnames_stack_print(out, rlist);
1148             } else if (allorfirst == 1)
1149                 BIO_puts(out, ": First Tier\n");
1150             else if (allorfirst == 0)
1151                 BIO_puts(out, ": All\n");
1152             else
1153                 BIO_printf(out, " Unknown (%d)\n", allorfirst);
1154             BIO_puts(out, "  Receipts To:\n");
1155             gnames_stack_print(out, rto);
1156         }
1157         if (rr)
1158             CMS_ReceiptRequest_free(rr);
1159     }
1160 }
1161
1162 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1163 {
1164     int i;
1165     STACK_OF(GENERAL_NAMES) *ret;
1166     GENERAL_NAMES *gens = NULL;
1167     GENERAL_NAME *gen = NULL;
1168     ret = sk_GENERAL_NAMES_new_null();
1169     if (!ret)
1170         goto err;
1171     for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1172         char *str = sk_OPENSSL_STRING_value(ns, i);
1173         gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1174         if (!gen)
1175             goto err;
1176         gens = GENERAL_NAMES_new();
1177         if (!gens)
1178             goto err;
1179         if (!sk_GENERAL_NAME_push(gens, gen))
1180             goto err;
1181         gen = NULL;
1182         if (!sk_GENERAL_NAMES_push(ret, gens))
1183             goto err;
1184         gens = NULL;
1185     }
1186
1187     return ret;
1188
1189  err:
1190     if (ret)
1191         sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1192     if (gens)
1193         GENERAL_NAMES_free(gens);
1194     if (gen)
1195         GENERAL_NAME_free(gen);
1196     return NULL;
1197 }
1198
1199 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
1200                                                 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
1201                                                 *rr_from)
1202 {
1203     STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1204     CMS_ReceiptRequest *rr;
1205     rct_to = make_names_stack(rr_to);
1206     if (!rct_to)
1207         goto err;
1208     if (rr_from) {
1209         rct_from = make_names_stack(rr_from);
1210         if (!rct_from)
1211             goto err;
1212     } else
1213         rct_from = NULL;
1214     rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
1215                                     rct_to);
1216     return rr;
1217  err:
1218     return NULL;
1219 }
1220
1221 #endif