LCOV - code coverage report
Current view: top level - tests - t-cms-parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 121 0.0 %
Date: 2015-11-05 17:05:02 Functions: 0 4 0.0 %

          Line data    Source code
       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             : void
      32           0 : dummy_hash_fnc (void *arg, const void *buffer, size_t length)
      33             : {
      34             :   (void)arg;
      35             :   (void)buffer;
      36             :   (void)length;
      37           0 : }
      38             : 
      39             : static int
      40           0 : dummy_writer_cb (void *cb_value, const void *buffer, size_t count)
      41             : {
      42             :   (void)cb_value;
      43             :   (void)buffer;
      44             :   (void)count;
      45           0 :   return 0;
      46             : }
      47             : 
      48             : 
      49             : 
      50             : static void
      51           0 : one_file (const char *fname)
      52             : {
      53             :   gpg_error_t err;
      54             :   FILE *fp;
      55             :   ksba_reader_t r;
      56             :   ksba_writer_t w;
      57             :   ksba_cms_t cms;
      58             :   int i;
      59             :   const char *algoid;
      60             :   ksba_stop_reason_t stopreason;
      61             :   const char *s;
      62             :   size_t n;
      63             :   ksba_sexp_t p;
      64             :   char *dn;
      65             :   int idx;
      66             : 
      67           0 :   printf ("*** checking `%s' ***\n", fname);
      68           0 :   fp = fopen (fname, "r");
      69           0 :   if (!fp)
      70             :     {
      71           0 :       fprintf (stderr, "%s:%d: can't open `%s': %s\n",
      72           0 :                __FILE__, __LINE__, fname, strerror (errno));
      73           0 :       exit (1);
      74             :     }
      75             : 
      76           0 :   err = ksba_reader_new (&r);
      77           0 :   if (err)
      78           0 :     fail_if_err (err);
      79           0 :   err = ksba_reader_set_file (r, fp);
      80           0 :   fail_if_err (err);
      81             :   /* Also create a writer so that cms.c won't return an error when
      82             :      writing processed content.  */
      83           0 :   err = ksba_writer_new (&w);
      84           0 :   if (err)
      85           0 :     fail_if_err (err);
      86           0 :   err = ksba_writer_set_cb (w, dummy_writer_cb, NULL);
      87           0 :   fail_if_err (err);
      88             : 
      89           0 :   switch (ksba_cms_identify (r))
      90             :     {
      91           0 :     case KSBA_CT_DATA:           s = "data"; break;
      92           0 :     case KSBA_CT_SIGNED_DATA:    s = "signed data"; break;
      93           0 :     case KSBA_CT_ENVELOPED_DATA: s = "enveloped data"; break;
      94           0 :     case KSBA_CT_DIGESTED_DATA:  s = "digested data"; break;
      95           0 :     case KSBA_CT_ENCRYPTED_DATA: s = "encrypted data"; break;
      96           0 :     case KSBA_CT_AUTH_DATA:      s = "auth data"; break;
      97           0 :     default:                     s = "unknown"; break;
      98             :     }
      99           0 :   printf ("identified as: %s\n", s);
     100             : 
     101           0 :   err = ksba_cms_new (&cms);
     102           0 :   if (err)
     103           0 :     fail_if_err (err);
     104             : 
     105           0 :   err = ksba_cms_set_reader_writer (cms, r, w);
     106           0 :   fail_if_err (err);
     107             : 
     108           0 :   err = ksba_cms_parse (cms, &stopreason);
     109           0 :   fail_if_err2 (fname, err);
     110           0 :   printf ("stop reason: %d\n", stopreason);
     111             : 
     112           0 :   s = ksba_cms_get_content_oid (cms, 0);
     113           0 :   printf ("ContentType: %s\n", s?s:"none");
     114             : 
     115           0 :   err = ksba_cms_parse (cms, &stopreason);
     116           0 :   fail_if_err2 (fname, err);
     117           0 :   printf ("stop reason: %d\n", stopreason);
     118             : 
     119           0 :   s = ksba_cms_get_content_oid (cms, 1);
     120           0 :   printf ("EncapsulatedContentType: %s\n", s?s:"none");
     121           0 :   printf ("DigestAlgorithms:");
     122           0 :   for (i=0; (algoid = ksba_cms_get_digest_algo_list (cms, i)); i++)
     123           0 :     printf (" %s", algoid);
     124           0 :   putchar('\n');
     125             : 
     126           0 :   if (stopreason == KSBA_SR_NEED_HASH)
     127           0 :     printf("Detached signature\n");
     128             : 
     129           0 :   ksba_cms_set_hash_function (cms, dummy_hash_fnc, NULL);
     130             : 
     131             :   do
     132             :     {
     133           0 :       err = ksba_cms_parse (cms, &stopreason);
     134           0 :       fail_if_err2 (fname, err);
     135           0 :       printf ("stop reason: %d\n", stopreason);
     136             :     }
     137           0 :   while (stopreason != KSBA_SR_READY);
     138             : 
     139             : 
     140           0 :   if (ksba_cms_get_content_type (cms, 0) == KSBA_CT_ENVELOPED_DATA)
     141             :     {
     142           0 :       for (idx=0; ; idx++)
     143             :         {
     144           0 :           err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p);
     145           0 :           if (err == -1)
     146           0 :             break; /* ready */
     147             : 
     148           0 :           fail_if_err2 (fname, err);
     149           0 :           printf ("recipient %d - issuer: ", idx);
     150           0 :           print_dn (dn);
     151           0 :           ksba_free (dn);
     152           0 :           putchar ('\n');
     153           0 :           printf ("recipient %d - serial: ", idx);
     154           0 :           print_sexp_hex (p);
     155           0 :           ksba_free (p);
     156           0 :           putchar ('\n');
     157             : 
     158           0 :           dn = ksba_cms_get_enc_val (cms, idx);
     159           0 :           printf ("recipient %d - enc_val %s\n", idx, dn? "found": "missing");
     160           0 :           ksba_free (dn);
     161           0 :         }
     162             :     }
     163             :   else
     164             :     {
     165           0 :       for (idx=0; idx < 1; idx++)
     166             :         {
     167           0 :           err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p);
     168           0 :           if (gpg_err_code (err) == GPG_ERR_NO_DATA && !idx)
     169             :             {
     170           0 :               printf ("this is a certs-only message\n");
     171           0 :               break;
     172             :             }
     173             : 
     174           0 :           fail_if_err2 (fname, err);
     175           0 :           printf ("signer %d - issuer: ", idx);
     176           0 :           print_dn (dn);
     177           0 :           ksba_free (dn);
     178           0 :           putchar ('\n');
     179           0 :           printf ("signer %d - serial: ", idx);
     180           0 :           print_sexp_hex (p);
     181           0 :           ksba_free (p);
     182           0 :           putchar ('\n');
     183             : 
     184           0 :           err = ksba_cms_get_message_digest (cms, idx, &dn, &n);
     185           0 :           fail_if_err2 (fname, err);
     186           0 :           printf ("signer %d - messageDigest: ", idx);
     187           0 :           print_hex (dn, n);
     188           0 :           ksba_free (dn);
     189           0 :           putchar ('\n');
     190             : 
     191           0 :           err = ksba_cms_get_sigattr_oids (cms, idx,
     192             :                                            "1.2.840.113549.1.9.3",&dn);
     193           0 :           if (err && err != -1)
     194           0 :             fail_if_err2 (fname, err);
     195           0 :           if (err != -1)
     196             :             {
     197             :               char *tmp;
     198             : 
     199           0 :               for (tmp=dn; *tmp; tmp++)
     200           0 :                 if (*tmp == '\n')
     201           0 :                   *tmp = ' ';
     202           0 :               printf ("signer %d - content-type: %s\n", idx, dn);
     203           0 :               ksba_free (dn);
     204             :             }
     205             : 
     206           0 :           algoid = ksba_cms_get_digest_algo (cms, idx);
     207           0 :           printf ("signer %d - digest algo: %s\n", idx, algoid?algoid:"?");
     208             : 
     209           0 :           dn = ksba_cms_get_sig_val (cms, idx);
     210           0 :           if (dn)
     211             :             {
     212           0 :               printf ("signer %d - signature: ", idx);
     213           0 :               print_sexp (dn);
     214           0 :               putchar ('\n');
     215             :             }
     216             :           else
     217           0 :             printf ("signer %d - signature not found\n", idx);
     218           0 :           ksba_free (dn);
     219             :         }
     220             :     }
     221             : 
     222           0 :   ksba_cms_release (cms);
     223           0 :   ksba_reader_release (r);
     224           0 :   fclose (fp);
     225           0 : }
     226             : 
     227             : 
     228             : 
     229             : 
     230             : int
     231           0 : main (int argc, char **argv)
     232             : {
     233           0 :   if (argc > 1)
     234           0 :     one_file (argv[1]);
     235             :   else
     236           0 :     one_file ("x.ber");
     237             :   /*one_file ("pkcs7-1.ber");*/
     238             :   /*one_file ("root-cert-2.der");  should fail */
     239             : 
     240           0 :   return 0;
     241             : }

Generated by: LCOV version 1.11