"Fossies" - the Fresh Open Source Software Archive

Member "libksba-1.6.0/tests/t-cms-parser.c" (2 Jun 2021, 8396 Bytes) of package /linux/privat/libksba-1.6.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.5.1_vs_1.6.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   ksba_content_type_t ct;
   64   int i;
   65   const char *algoid;
   66   ksba_stop_reason_t stopreason;
   67   const char *s;
   68   size_t n;
   69   ksba_sexp_t p;
   70   char *dn;
   71   int idx;
   72 
   73   if (!quiet)
   74     printf ("*** checking `%s' ***\n", fname);
   75   fp = fopen (fname, "r");
   76   if (!fp)
   77     {
   78       fprintf (stderr, "%s:%d: can't open `%s': %s\n",
   79                __FILE__, __LINE__, fname, strerror (errno));
   80       exit (1);
   81     }
   82 
   83   err = ksba_reader_new (&r);
   84   if (err)
   85     fail_if_err (err);
   86   err = ksba_reader_set_file (r, fp);
   87   fail_if_err (err);
   88   /* Also create a writer so that cms.c won't return an error when
   89      writing processed content.  */
   90   err = ksba_writer_new (&w);
   91   if (err)
   92     fail_if_err (err);
   93   err = ksba_writer_set_cb (w, dummy_writer_cb, NULL);
   94   fail_if_err (err);
   95 
   96   switch (ksba_cms_identify (r))
   97     {
   98     case KSBA_CT_DATA:           s = "data"; break;
   99     case KSBA_CT_SIGNED_DATA:    s = "signed data"; break;
  100     case KSBA_CT_ENVELOPED_DATA: s = "enveloped data"; break;
  101     case KSBA_CT_AUTHENVELOPED_DATA: s = "auth enveloped data"; break;
  102     case KSBA_CT_DIGESTED_DATA:  s = "digested data"; break;
  103     case KSBA_CT_ENCRYPTED_DATA: s = "encrypted data"; break;
  104     case KSBA_CT_AUTH_DATA:      s = "auth data"; break;
  105     case KSBA_CT_SPC_IND_DATA_CTX:s = "spc indirect data context"; break;
  106     case KSBA_CT_OPENPGP_KEYBLOCK:s = "openpgp keyblock"; break;
  107     default:                     s = "unknown"; break;
  108     }
  109   if (!quiet)
  110     printf ("identified as: %s\n", s);
  111 
  112   err = ksba_cms_new (&cms);
  113   if (err)
  114     fail_if_err (err);
  115 
  116   err = ksba_cms_set_reader_writer (cms, r, w);
  117   fail_if_err (err);
  118 
  119   err = ksba_cms_parse (cms, &stopreason);
  120   fail_if_err2 (fname, err);
  121   if (!quiet)
  122     printf ("stop reason: %d\n", stopreason);
  123 
  124   s = ksba_cms_get_content_oid (cms, 0);
  125   if (!quiet)
  126     printf ("ContentType: %s\n", s?s:"none");
  127 
  128   err = ksba_cms_parse (cms, &stopreason);
  129   fail_if_err2 (fname, err);
  130   if (!quiet)
  131     printf ("stop reason: %d\n", stopreason);
  132 
  133   s = ksba_cms_get_content_oid (cms, 1);
  134   if (!quiet)
  135     {
  136       printf ("EncapsulatedContentType: %s\n", s?s:"none");
  137       printf ("DigestAlgorithms:");
  138     }
  139   for (i=0; (algoid = ksba_cms_get_digest_algo_list (cms, i)); i++)
  140     if (!quiet)
  141       printf (" %s", algoid);
  142   if (!quiet)
  143     putchar('\n');
  144 
  145   if (stopreason == KSBA_SR_NEED_HASH)
  146     if (!quiet)
  147       printf("Detached signature\n");
  148 
  149   ksba_cms_set_hash_function (cms, dummy_hash_fnc, NULL);
  150 
  151   do
  152     {
  153       err = ksba_cms_parse (cms, &stopreason);
  154       fail_if_err2 (fname, err);
  155       if (!quiet)
  156         printf ("stop reason: %d\n", stopreason);
  157     }
  158   while (stopreason != KSBA_SR_READY);
  159 
  160 
  161   ct = ksba_cms_get_content_type (cms, 0);
  162   if (ct == KSBA_CT_ENVELOPED_DATA || ct == KSBA_CT_AUTHENVELOPED_DATA)
  163     {
  164       for (idx=0; ; idx++)
  165         {
  166           err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p);
  167           if (err == -1)
  168             break; /* ready */
  169 
  170           if (gpg_err_code (err) == GPG_ERR_UNSUPPORTED_CMS_OBJ)
  171             {
  172               printf ("recipient %d"
  173                       " - kekri or pwri detected\n", idx);
  174               err = 0;
  175             }
  176           else
  177             {
  178               fail_if_err2 (fname, err);
  179               if (!quiet)
  180                 {
  181                   printf ("recipient %d - issuer: ", idx);
  182                   print_dn (dn);
  183                 }
  184               ksba_free (dn);
  185               if (!quiet)
  186                 {
  187                   putchar ('\n');
  188                   printf ("recipient %d - serial: ", idx);
  189                   print_sexp_hex (p);
  190                   putchar ('\n');
  191                 }
  192               ksba_free (p);
  193             }
  194 
  195           dn = ksba_cms_get_enc_val (cms, idx);
  196           if (!quiet)
  197             {
  198               printf ("recipient %d - enc_val: ", idx);
  199               print_sexp (dn);
  200               putchar ('\n');
  201             }
  202           ksba_free (dn);
  203         }
  204     }
  205   else
  206     {
  207       for (idx=0; idx < 1; idx++)
  208         {
  209           err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p);
  210           if (gpg_err_code (err) == GPG_ERR_NO_DATA && !idx)
  211             {
  212               if (!quiet)
  213                 printf ("this is a certs-only message\n");
  214               break;
  215             }
  216 
  217           fail_if_err2 (fname, err);
  218           if (!quiet)
  219             {
  220               printf ("signer %d - issuer: ", idx);
  221               print_dn (dn);
  222               putchar ('\n');
  223             }
  224           ksba_free (dn);
  225 
  226           if (!quiet)
  227             {
  228               printf ("signer %d - serial: ", idx);
  229               print_sexp_hex (p);
  230               putchar ('\n');
  231             }
  232           ksba_free (p);
  233 
  234           err = ksba_cms_get_message_digest (cms, idx, &dn, &n);
  235           fail_if_err2 (fname, err);
  236           if (!quiet)
  237             {
  238               printf ("signer %d - messageDigest: ", idx);
  239               print_hex (dn, n);
  240               putchar ('\n');
  241             }
  242           ksba_free (dn);
  243 
  244           err = ksba_cms_get_sigattr_oids (cms, idx,
  245                                            "1.2.840.113549.1.9.3",&dn);
  246           if (err && err != -1)
  247             fail_if_err2 (fname, err);
  248           if (err != -1)
  249             {
  250               char *tmp;
  251 
  252               for (tmp=dn; *tmp; tmp++)
  253                 if (*tmp == '\n')
  254                   *tmp = ' ';
  255               if (!quiet)
  256                 printf ("signer %d - content-type: %s\n", idx, dn);
  257               ksba_free (dn);
  258             }
  259 
  260           algoid = ksba_cms_get_digest_algo (cms, idx);
  261           if (!quiet)
  262             printf ("signer %d - digest algo: %s\n", idx, algoid?algoid:"?");
  263 
  264           dn = ksba_cms_get_sig_val (cms, idx);
  265           if (dn)
  266             {
  267               if (!quiet)
  268                 {
  269                   printf ("signer %d - signature: ", idx);
  270                   print_sexp (dn);
  271                   putchar ('\n');
  272                 }
  273             }
  274           else
  275             {
  276               if (!quiet)
  277                 printf ("signer %d - signature not found\n", idx);
  278             }
  279           ksba_free (dn);
  280         }
  281     }
  282 
  283   ksba_cms_release (cms);
  284   ksba_writer_release (w);
  285   ksba_reader_release (r);
  286   fclose (fp);
  287 }
  288 
  289 
  290 
  291 
  292 int
  293 main (int argc, char **argv)
  294 {
  295   if (argc)
  296     {
  297       argc--; argv++;
  298     }
  299   if (argc && !strcmp (*argv, "--verbose"))
  300     {
  301       verbose = 1;
  302       argc--; argv++;
  303     }
  304 
  305   if (argc)
  306     {
  307       for (; argc; argc--, argv++)
  308         one_file (*argv);
  309     }
  310   else
  311     {
  312       static char *testfiles[] =
  313         {
  314          "samples/detached-sig.cms",
  315          "samples/ecdh-sample1.p7m",
  316          "samples/ecdsa-sample1.p7s",
  317          "samples/rsa-sample1.p7m",
  318          "samples/rsa-sample1.p7s",
  319          NULL
  320         };
  321       char *fname;
  322       int idx;
  323 
  324       if (!verbose)
  325         quiet = 1;
  326 
  327       for (idx=0; testfiles[idx]; idx++)
  328         {
  329           fname = prepend_srcdir (testfiles[idx]);
  330           one_file (fname);
  331           free(fname);
  332         }
  333     }
  334 
  335   if (!quiet)
  336     printf ("*** all checks done\n");
  337 
  338   return 0;
  339 }