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