"Fossies" - the Fresh Open Source Software Archive

Member "libksba-1.5.0/tests/t-cms-parser.c" (18 Nov 2020, 7937 Bytes) of package /linux/privat/libksba-1.5.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "t-cms-parser.c": 1.4.0_vs_1.5.0.

    1 /* t-cms-parser.c - basic test for the CMS parser.
    2  *      Copyright (C) 2001 g10 Code GmbH
    3  *
    4  * This file is part of KSBA.
    5  *
    6  * KSBA is free software; you can redistribute it and/or modify
    7  * it under the terms of the GNU General Public License as published by
    8  * the Free Software Foundation; either version 3 of the License, or
    9  * (at your option) any later version.
   10  *
   11  * KSBA is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
   18  */
   19 
   20 #include <stdio.h>
   21 #include <stdlib.h>
   22 #include <string.h>
   23 #include <assert.h>
   24 #include <time.h>
   25 #include <errno.h>
   26 
   27 #include "../src/ksba.h"
   28 
   29 #include "t-common.h"
   30 
   31 
   32 static int quiet;
   33 static int verbose;
   34 
   35 
   36 void
   37 dummy_hash_fnc (void *arg, const void *buffer, size_t length)
   38 {
   39   (void)arg;
   40   (void)buffer;
   41   (void)length;
   42 }
   43 
   44 static int
   45 dummy_writer_cb (void *cb_value, const void *buffer, size_t count)
   46 {
   47   (void)cb_value;
   48   (void)buffer;
   49   (void)count;
   50   return 0;
   51 }
   52 
   53 
   54 
   55 static void
   56 one_file (const char *fname)
   57 {
   58   gpg_error_t err;
   59   FILE *fp;
   60   ksba_reader_t r;
   61   ksba_writer_t w;
   62   ksba_cms_t cms;
   63   int i;
   64   const char *algoid;
   65   ksba_stop_reason_t stopreason;
   66   const char *s;
   67   size_t n;
   68   ksba_sexp_t p;
   69   char *dn;
   70   int idx;
   71 
   72   if (!quiet)
   73     printf ("*** checking `%s' ***\n", fname);
   74   fp = fopen (fname, "r");
   75   if (!fp)
   76     {
   77       fprintf (stderr, "%s:%d: can't open `%s': %s\n",
   78                __FILE__, __LINE__, fname, strerror (errno));
   79       exit (1);
   80     }
   81 
   82   err = ksba_reader_new (&r);
   83   if (err)
   84     fail_if_err (err);
   85   err = ksba_reader_set_file (r, fp);
   86   fail_if_err (err);
   87   /* Also create a writer so that cms.c won't return an error when
   88      writing processed content.  */
   89   err = ksba_writer_new (&w);
   90   if (err)
   91     fail_if_err (err);
   92   err = ksba_writer_set_cb (w, dummy_writer_cb, NULL);
   93   fail_if_err (err);
   94 
   95   switch (ksba_cms_identify (r))
   96     {
   97     case KSBA_CT_DATA:           s = "data"; break;
   98     case KSBA_CT_SIGNED_DATA:    s = "signed data"; break;
   99     case KSBA_CT_ENVELOPED_DATA: s = "enveloped data"; break;
  100     case KSBA_CT_DIGESTED_DATA:  s = "digested data"; break;
  101     case KSBA_CT_ENCRYPTED_DATA: s = "encrypted data"; break;
  102     case KSBA_CT_AUTH_DATA:      s = "auth data"; break;
  103     case KSBA_CT_SPC_IND_DATA_CTX:s = "spc indirect data context"; break;
  104     case KSBA_CT_OPENPGP_KEYBLOCK:s = "openpgp keyblock"; break;
  105     default:                     s = "unknown"; break;
  106     }
  107   if (!quiet)
  108     printf ("identified as: %s\n", s);
  109 
  110   err = ksba_cms_new (&cms);
  111   if (err)
  112     fail_if_err (err);
  113 
  114   err = ksba_cms_set_reader_writer (cms, r, w);
  115   fail_if_err (err);
  116 
  117   err = ksba_cms_parse (cms, &stopreason);
  118   fail_if_err2 (fname, err);
  119   if (!quiet)
  120     printf ("stop reason: %d\n", stopreason);
  121 
  122   s = ksba_cms_get_content_oid (cms, 0);
  123   if (!quiet)
  124     printf ("ContentType: %s\n", s?s:"none");
  125 
  126   err = ksba_cms_parse (cms, &stopreason);
  127   fail_if_err2 (fname, err);
  128   if (!quiet)
  129     printf ("stop reason: %d\n", stopreason);
  130 
  131   s = ksba_cms_get_content_oid (cms, 1);
  132   if (!quiet)
  133     {
  134       printf ("EncapsulatedContentType: %s\n", s?s:"none");
  135       printf ("DigestAlgorithms:");
  136     }
  137   for (i=0; (algoid = ksba_cms_get_digest_algo_list (cms, i)); i++)
  138     if (!quiet)
  139       printf (" %s", algoid);
  140   if (!quiet)
  141     putchar('\n');
  142 
  143   if (stopreason == KSBA_SR_NEED_HASH)
  144     if (!quiet)
  145       printf("Detached signature\n");
  146 
  147   ksba_cms_set_hash_function (cms, dummy_hash_fnc, NULL);
  148 
  149   do
  150     {
  151       err = ksba_cms_parse (cms, &stopreason);
  152       fail_if_err2 (fname, err);
  153       if (!quiet)
  154         printf ("stop reason: %d\n", stopreason);
  155     }
  156   while (stopreason != KSBA_SR_READY);
  157 
  158 
  159   if (ksba_cms_get_content_type (cms, 0) == KSBA_CT_ENVELOPED_DATA)
  160     {
  161       for (idx=0; ; idx++)
  162         {
  163           err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p);
  164           if (err == -1)
  165             break; /* ready */
  166 
  167           fail_if_err2 (fname, err);
  168           if (!quiet)
  169             {
  170               printf ("recipient %d - issuer: ", idx);
  171               print_dn (dn);
  172             }
  173           ksba_free (dn);
  174           if (!quiet)
  175             {
  176               putchar ('\n');
  177               printf ("recipient %d - serial: ", idx);
  178               print_sexp_hex (p);
  179               putchar ('\n');
  180             }
  181           ksba_free (p);
  182 
  183           dn = ksba_cms_get_enc_val (cms, idx);
  184           if (!quiet)
  185             {
  186               printf ("recipient %d - enc_val: ", idx);
  187               print_sexp (dn);
  188               putchar ('\n');
  189             }
  190           ksba_free (dn);
  191         }
  192     }
  193   else
  194     {
  195       for (idx=0; idx < 1; idx++)
  196         {
  197           err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p);
  198           if (gpg_err_code (err) == GPG_ERR_NO_DATA && !idx)
  199             {
  200               if (!quiet)
  201                 printf ("this is a certs-only message\n");
  202               break;
  203             }
  204 
  205           fail_if_err2 (fname, err);
  206           if (!quiet)
  207             {
  208               printf ("signer %d - issuer: ", idx);
  209               print_dn (dn);
  210               putchar ('\n');
  211             }
  212           ksba_free (dn);
  213 
  214           if (!quiet)
  215             {
  216               printf ("signer %d - serial: ", idx);
  217               print_sexp_hex (p);
  218               putchar ('\n');
  219             }
  220           ksba_free (p);
  221 
  222           err = ksba_cms_get_message_digest (cms, idx, &dn, &n);
  223           fail_if_err2 (fname, err);
  224           if (!quiet)
  225             {
  226               printf ("signer %d - messageDigest: ", idx);
  227               print_hex (dn, n);
  228               putchar ('\n');
  229             }
  230           ksba_free (dn);
  231 
  232           err = ksba_cms_get_sigattr_oids (cms, idx,
  233                                            "1.2.840.113549.1.9.3",&dn);
  234           if (err && err != -1)
  235             fail_if_err2 (fname, err);
  236           if (err != -1)
  237             {
  238               char *tmp;
  239 
  240               for (tmp=dn; *tmp; tmp++)
  241                 if (*tmp == '\n')
  242                   *tmp = ' ';
  243               if (!quiet)
  244                 printf ("signer %d - content-type: %s\n", idx, dn);
  245               ksba_free (dn);
  246             }
  247 
  248           algoid = ksba_cms_get_digest_algo (cms, idx);
  249           if (!quiet)
  250             printf ("signer %d - digest algo: %s\n", idx, algoid?algoid:"?");
  251 
  252           dn = ksba_cms_get_sig_val (cms, idx);
  253           if (dn)
  254             {
  255               if (!quiet)
  256                 {
  257                   printf ("signer %d - signature: ", idx);
  258                   print_sexp (dn);
  259                   putchar ('\n');
  260                 }
  261             }
  262           else
  263             {
  264               if (!quiet)
  265                 printf ("signer %d - signature not found\n", idx);
  266             }
  267           ksba_free (dn);
  268         }
  269     }
  270 
  271   ksba_cms_release (cms);
  272   ksba_writer_release (w);
  273   ksba_reader_release (r);
  274   fclose (fp);
  275 }
  276 
  277 
  278 
  279 
  280 int
  281 main (int argc, char **argv)
  282 {
  283   if (argc)
  284     {
  285       argc--; argv++;
  286     }
  287   if (argc && !strcmp (*argv, "--verbose"))
  288     {
  289       verbose = 1;
  290       argc--; argv++;
  291     }
  292 
  293   if (argc)
  294     {
  295       for (; argc; argc--, argv++)
  296         one_file (*argv);
  297     }
  298   else
  299     {
  300       static char *testfiles[] =
  301         {
  302          "samples/detached-sig.cms",
  303          "samples/ecdh-sample1.p7m",
  304          "samples/ecdsa-sample1.p7s",
  305          "samples/rsa-sample1.p7m",
  306          "samples/rsa-sample1.p7s",
  307          NULL
  308         };
  309       char *fname;
  310       int idx;
  311 
  312       if (!verbose)
  313         quiet = 1;
  314 
  315       for (idx=0; testfiles[idx]; idx++)
  316         {
  317           fname = prepend_srcdir (testfiles[idx]);
  318           one_file (fname);
  319           free(fname);
  320         }
  321     }
  322 
  323   if (!quiet)
  324     printf ("*** all checks done\n");
  325 
  326   return 0;
  327 }