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