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