summaryrefslogtreecommitdiffstats
path: root/sm/t-minip12.c
blob: c9adcede6fe9d69317aabac410ddb430914b556e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/* t-minip12.c - Test driver for minip12.c
 * Copyright (C) 2020, 2023 g10 Code GmbH
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>

#include "../common/util.h"
#include "minip12.h"


#define PGM "t-minip12"

static int verbose;
static int debug;
static int any_error;

static void die (const char *format, ...) GPGRT_ATTR_NR_PRINTF(1,2);
static void err (const char *format, ...) GPGRT_ATTR_PRINTF(1,2);
static void inf (const char *format, ...) GPGRT_ATTR_PRINTF(1,2);
/* static void dbg (const char *format, ...) GPGRT_ATTR_PRINTF(1,2); */
static void printresult (const char *format, ...) GPGRT_ATTR_PRINTF(1,2);
static char *my_xstrconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);

#define xstrconcat my_xstrconcat
#define trim_spaces(a) my_trim_spaces ((a))
#define my_isascii(c) (!((c) & 0x80))





/* Print diagnostic message and exit with failure. */
static void
die (const char *format, ...)
{
  va_list arg_ptr;

  fflush (stdout);
  fprintf (stderr, "%s: ", PGM);

  va_start (arg_ptr, format);
  vfprintf (stderr, format, arg_ptr);
  va_end (arg_ptr);
  if (!*format || format[strlen(format)-1] != '\n')
    putc ('\n', stderr);

  exit (1);
}


/* Print diagnostic message. */
static void
err (const char *format, ...)
{
  va_list arg_ptr;

  any_error = 1;

  fflush (stdout);
  fprintf (stderr, "%s: ", PGM);

  va_start (arg_ptr, format);
  vfprintf (stderr, format, arg_ptr);
  va_end (arg_ptr);
  if (!*format || format[strlen(format)-1] != '\n')
    putc ('\n', stderr);
}


/* Print an info message. */
static void
inf (const char *format, ...)
{
  va_list arg_ptr;

  if (verbose)
    {
      fprintf (stderr, "%s: ", PGM);

      va_start (arg_ptr, format);
      vfprintf (stderr, format, arg_ptr);
      va_end (arg_ptr);
      if (!*format || format[strlen(format)-1] != '\n')
        putc ('\n', stderr);
    }
}


/* Print a debug message. */
/* static void */
/* dbg (const char *format, ...) */
/* { */
/*   va_list arg_ptr; */

/*   if (debug) */
/*     { */
/*       fprintf (stderr, "%s: DBG: ", PGM); */

/*       va_start (arg_ptr, format); */
/*       vfprintf (stderr, format, arg_ptr); */
/*       va_end (arg_ptr); */
/*       if (!*format || format[strlen(format)-1] != '\n') */
/*         putc ('\n', stderr); */
/*     } */
/* } */


/* Print a result line to stdout.  */
static void
printresult (const char *format, ...)
{
  va_list arg_ptr;

  fflush (stdout);
#ifdef HAVE_FLOCKFILE
  flockfile (stdout);
#endif
  va_start (arg_ptr, format);
  vfprintf (stdout, format, arg_ptr);
  if (*format && format[strlen(format)-1] != '\n')
    putc ('\n', stdout);
  va_end (arg_ptr);
  fflush (stdout);
#ifdef HAVE_FLOCKFILE
  funlockfile (stdout);
#endif
}


/* Helper for xstrconcat and strconcat.  */
static char *
do_strconcat (int xmode, const char *s1, va_list arg_ptr)
{
  const char *argv[48];
  size_t argc;
  size_t needed;
  char *buffer, *p;

  argc = 0;
  argv[argc++] = s1;
  needed = strlen (s1);
  while (((argv[argc] = va_arg (arg_ptr, const char *))))
    {
      needed += strlen (argv[argc]);
      if (argc >= DIM (argv)-1)
        die ("too may args for strconcat\n");
      argc++;
    }
  needed++;
  buffer = xmode? xmalloc (needed) : malloc (needed);
  for (p = buffer, argc=0; argv[argc]; argc++)
    p = stpcpy (p, argv[argc]);

  return buffer;
}


/* Concatenate the string S1 with all the following strings up to a
   NULL.  Returns a malloced buffer with the new string or dies on error. */
static char *
my_xstrconcat (const char *s1, ...)
{
  va_list arg_ptr;
  char *result;

  if (!s1)
    result = xstrdup ("");
  else
    {
      va_start (arg_ptr, s1);
      result = do_strconcat (1, s1, arg_ptr);
      va_end (arg_ptr);
    }
  return result;
}


static char *
my_trim_spaces (char *str )
{
  char *string, *p, *mark;

  string = str;
  for (p=string; *p && isspace (*(unsigned char *)p) ; p++)
    ;
  for (mark=NULL; (*string = *p); string++, p++ )
    if (isspace (*(unsigned char *)p))
      {
        if (!mark)
          mark = string;
      }
    else
      mark = NULL;
  if (mark)
    *mark = '\0';

  return str ;
}


/* Prepend FNAME with the srcdir environment variable's value and
 * return an allocated filename.  */
static char *
prepend_srcdir (const char *fname)
{
  static const char *srcdir;

  if (!srcdir && !(srcdir = getenv ("srcdir")))
    return xstrdup (fname);
  else
    return xstrconcat (srcdir, "/", fname, NULL);
}


/* (BUFFER,BUFLEN) and return a malloced hexstring.  */
static char *
hash_buffer (const void *buffer, size_t buflen)
{
  unsigned char hash[20];
  char *result;
  int i;

  gcry_md_hash_buffer (GCRY_MD_SHA1, hash, buffer, buflen);
  result = xmalloc (41);
  for (i=0; i < 20; i++)
    snprintf (result + 2*i, 3, "%02x", hash[i]);
  return result;
}


/* Read next line but skip over empty and comment lines.  Caller must
   xfree the result.  */
static char *
read_textline (FILE *fp, int *lineno)
{
  char line[4096];
  char *p;

  do
    {
      if (!fgets (line, sizeof line, fp))
        {
          if (feof (fp))
            return NULL;
          die ("error reading input line: %s\n", strerror (errno));
        }
      ++*lineno;
      p = strchr (line, '\n');
      if (!p)
        die ("input line %d not terminated or too long\n", *lineno);
      *p = 0;
      for (p--;p > line && my_isascii (*p) && isspace (*p); p--)
        *p = 0;
    }
  while (!*line || *line == '#');
  return xstrdup (line);
}


/* Copy the data after the tag to BUFFER.  BUFFER will be allocated as
   needed.  */
static void
copy_data (char **buffer, const char *line, int lineno)
{
  const char *s;

  xfree (*buffer);
  *buffer = NULL;

  s = strchr (line, ':');
  if (!s)
    {
      err ("syntax error at input line %d", lineno);
      return;
    }
  for (s++; my_isascii (*s) && isspace (*s); s++)
    ;
  *buffer = xstrdup (s);
}


static void
hexdowncase (char *string)
{
  char *p;

  if (string)
    for (p=string; *p; p++)
      if (my_isascii (*p))
        *p = tolower (*p);
}


/* Return the value of the variable VARNAME from ~/.gnupg-autogen.rc
 * or NULL if it does not exists or is empty.  */
static char *
value_from_gnupg_autogen_rc (const char *varname)
{
  const char *home;
  char *fname;
  FILE *fp;
  char *line = NULL;
  char *p;
  int lineno = 0;

  if (!(home = getenv ("HOME")))
    home = "";
  fname = xstrconcat (home, "/.gnupg-autogen.rc", NULL);
  fp = fopen (fname, "r");
  if (!fp)
    goto leave;

  while ((line = read_textline (fp, &lineno)))
    {
      p = strchr (line, '=');
      if (p)
        {
          *p++ = 0;
          trim_spaces (line);
          if (!strcmp (line, varname))
            {
              trim_spaces (p);
              if (*p)
                {
                  memmove (line, p, strlen (p)+1);
                  if (*line == '~' && line[1] == '/')
                    {
                      p = xstrconcat (home, line+1, NULL);
                      xfree (line);
                      line = p;
                    }
                  break; /* found.  */
                }
            }
        }
      xfree (line);
    }

 leave:
  if (fp)
    fclose (fp);
  xfree (fname);
  return line;
}


static void
cert_cb (void *opaque, const unsigned char *cert, size_t certlen)
{
  (void)opaque;
  (void)cert;

  if (verbose)
    log_info ("got a certificate of %zu bytes length\n", certlen);
}


/* Parse one PKCS#12 file.   Returns zero on success.  */
static int
one_file (const char *name, const char *pass)
{
  FILE *fp;
  struct stat st;
  unsigned char *buf;
  size_t buflen;
  gcry_mpi_t *result;
  int badpass;
  char *curve = NULL;

  fp = fopen (name, "rb");
  if (!fp)
    {
      fprintf (stderr, PGM": can't open '%s': %s\n", name, strerror (errno));
      return 1;
    }

  if (fstat (fileno(fp), &st))
    {
      fprintf (stderr, PGM": can't stat '%s': %s\n", name, strerror (errno));
      return 1;
    }

  buflen = st.st_size;
  buf = xmalloc (buflen+1);
  if (fread (buf, buflen, 1, fp) != 1)
    {
      fprintf (stderr, "error reading '%s': %s\n", name, strerror (errno));
      return 1;
    }
  fclose (fp);

  result = p12_parse (buf, buflen, pass, cert_cb, NULL, &badpass, &curve);
  if (result)
    {
      int i, rc;
      unsigned char *tmpbuf;

      if (curve)
        log_info ("curve: %s\n", curve);
      for (i=0; result[i]; i++)
        {
          rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &tmpbuf, NULL, result[i]);
          if (rc)
            log_error ("%d: [error printing number: %s]\n",
                       i, gpg_strerror (rc));
          else
            {
              log_info ("%d: %s\n", i, tmpbuf);
              gcry_free (tmpbuf);
            }
        }
    }
  if (badpass)
    log_error ("Bad password given?\n");

  xfree (buf);
  return 0;
}


static void
cert_collect_cb (void *opaque, const unsigned char *cert, size_t certlen)
{
  char **certstr = opaque;
  char *hash, *save;

  hash = hash_buffer (cert, certlen);
  if (*certstr)
    {
      save = *certstr;
      *certstr = xstrconcat (save, ",", hash, NULL);
      xfree (save);
      xfree (hash);
    }
  else
    *certstr = hash;
}


static int
run_one_test (const char *name, const char *desc, const char *pass,
              const char *certexpected, const char *keyexpected)
{
  FILE *fp;
  struct stat st;
  unsigned char *buf;
  size_t buflen;
  gcry_mpi_t *result;
  int badpass;
  char *curve = NULL;
  char *resulthash = NULL;
  char *p;
  char *certstr = NULL;
  int ret;

  inf ("testing '%s' (%s)", name , desc? desc:"");
  fp = fopen (name, "rb");
  if (!fp)
    {
      err ("can't open '%s': %s\n", name, strerror (errno));
      printresult ("FAIL: %s - test file not found\n", name);
      return 1;
    }

  if (fstat (fileno (fp), &st))
    {
      err ("can't stat '%s': %s\n", name, strerror (errno));
      printresult ("FAIL: %s - error stating test file\n", name);
      fclose (fp);
      return 1;
    }

  buflen = st.st_size;
  buf = xmalloc (buflen+1);
  if (fread (buf, buflen, 1, fp) != 1)
    {
      err ("error reading '%s': %s\n", name, strerror (errno));
      printresult ("FAIL: %s - error reading test file\n", name);
      fclose (fp);
      xfree (buf);
      return 1;
    }
  fclose (fp);

  result = p12_parse (buf, buflen, pass? pass:"", cert_collect_cb, &certstr,
                      &badpass, &curve);
  if (result)
    {
      int i, rc;
      char *tmpstring;
      unsigned char *tmpbuf;
      char numbuf[20];

      if (curve)
        {
          if (verbose > 1)
            inf ("curve: %s\n", curve);
          tmpstring = xstrconcat ("curve:", curve, "\n", NULL);
        }
      else
        tmpstring = xstrdup ("\n");
      for (i=0; result[i]; i++)
        {
          rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &tmpbuf, NULL, result[i]);
          if (rc)
            die ("result %d: [error printing number: %s]\n",
                 i, gpg_strerror (rc));
          else
            {
              if (verbose > 1)
                inf ("result %d: %s\n", i, tmpbuf);
              snprintf (numbuf, sizeof numbuf, "%d:", i);
              p = xstrconcat (tmpstring, numbuf, tmpbuf, "\n", NULL);
              xfree (tmpstring);
              tmpstring = p;
              gcry_free (tmpbuf);
            }
        }

      /* Hash only if we have at least one parameter; i.e. the curve
       * alone is not sufficient.  */
      if (result[0])
        resulthash = hash_buffer (tmpstring, strlen (tmpstring));
      xfree (tmpstring);
    }

  if (verbose > 1)
    {
      inf ("cert(exp)=%s", certexpected);
      inf ("cert(got)=%s", certstr? certstr:"[null]");
      inf ("key(exp)=%s", keyexpected);
      inf ("key(got)=%s", resulthash? resulthash:"[null]");
    }

  ret = 1;
  if (!result)
    printresult ("FAIL: %s - error from parser\n", name);
  else if (certexpected && !certstr)
    printresult ("FAIL: %s - expected certs but got none\n", name);
  else if (!certexpected && certstr)
    printresult ("FAIL: %s - no certs expected but got one\n", name);
  else if (certexpected && certstr && strcmp (certexpected, certstr))
    {
      printresult ("FAIL: %s - certs not as expected\n", name);
      inf ("cert(exp)=%s", certexpected);
      inf ("cert(got)=%s", certstr? certstr:"[null]");
    }
  else if (keyexpected && !resulthash)
    printresult ("FAIL: %s - expected key but got none\n", name);
  else if (!keyexpected && resulthash)
    printresult ("FAIL: %s - key not expected but got one\n", name);
  else if (keyexpected && resulthash && strcmp (keyexpected, resulthash))
    {
      printresult ("FAIL: %s - keys not as expected\n", name);
      inf ("key(exp)=%s", keyexpected);
      inf ("key(got)=%s", resulthash? resulthash:"[null]");
    }
  else
    {
      printresult ("PASS: %s\n", name);
      ret = 0;
    }

  if (result)
    {
      int i;
      for (i=0; result[i]; i++)
        gcry_mpi_release (result[i]);
      gcry_free (result);
    }
  xfree (certstr);
  xfree (resulthash);
  xfree (curve);
  xfree (buf);
  return ret;
}


/* Run a regression test using the Info take from DESCFNAME.  */
static int
run_tests_from_file (const char *descfname)
{
  FILE *fp;
  char *descdir;
  int lineno, ntests;
  char *line;
  char *name = NULL;
  char *desc = NULL;
  char *pass = NULL;
  char *cert = NULL;
  char *key = NULL;
  int ret = 0;
  char *p;

  inf ("Running tests from '%s'", descfname);
  descdir = xstrdup (descfname);
  p = strrchr (descdir, '/');
  if (p)
    *p = 0;
  else
    {
      xfree (descdir);
      descdir = xstrdup (".");
    }

  fp = fopen (descfname, "r");
  if (!fp)
    die ("error opening '%s': %s\n", descfname, strerror (errno));

  lineno = ntests = 0;
  while ((line = read_textline (fp, &lineno)))
    {
      if (!strncmp (line, "Name:", 5))
        {
          if (name)
            ret |= run_one_test (name, desc, pass, cert, key);
          xfree (cert); cert = NULL;
          xfree (desc); desc = NULL;
          xfree (pass); pass = NULL;
          xfree (key);  key = NULL;
          copy_data (&name, line, lineno);
          if (name)
            {
              p = xstrconcat (descdir, "/", name, NULL);
              xfree (name);
              name = p;
            }
        }
      else if (!strncmp (line, "Desc:", 5))
        copy_data (&desc, line, lineno);
      else if (!strncmp (line, "Pass:", 5))
        copy_data (&pass, line, lineno);
      else if (!strncmp (line, "Cert:", 5))
        {
          p = NULL;
          copy_data (&p, line, lineno);
          hexdowncase (p);
          if (p && cert)
            {
              char *save = cert;
              cert = xstrconcat (save, ",", p, NULL);
              xfree (save);
              xfree (p);
            }
          else
            cert = p;
        }
      else if (!strncmp (line, "Key:", 4))
        {
          copy_data (&key, line, lineno);
          hexdowncase (key);
        }
      else
        inf ("%s:%d: unknown tag ignored", descfname, lineno);

      xfree (line);
    }
  if (name)
    ret |= run_one_test (name, desc, pass, cert, key);
  xfree (name);
  xfree (desc);
  xfree (pass);
  xfree (cert);
  xfree (key);

  fclose (fp);
  xfree (descdir);
  return ret;
}



int
main (int argc, char **argv)
{
  int last_argc = -1;
  char const *name = NULL;
  char const *pass = NULL;
  int ret;
  int no_extra = 0;

  if (argc)
    { argc--; argv++; }
  while (argc && last_argc != argc )
    {
      last_argc = argc;
      if (!strcmp (*argv, "--"))
        {
          argc--; argv++;
          break;
        }
      else if (!strcmp (*argv, "--help"))
        {
          fputs ("usage: " PGM " <pkcs12file> [<passphrase>]\n"
                 "Without <pkcs12file> a regression test is run\n"
                 "Options:\n"
                 "  --no-extra          do not run extra tests\n"
                 "  --verbose           print timings etc.\n"
                 "                      given twice shows more\n"
                 "  --debug             flyswatter\n"
                 , stdout);
          exit (0);
        }
      else if (!strcmp (*argv, "--no-extra"))
        {
          no_extra = 1;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--verbose"))
        {
          verbose++;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--debug"))
        {
          verbose += 2;
          debug++;
          argc--; argv++;
        }
      else if (!strncmp (*argv, "--", 2))
        {
          fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
          exit (1);
        }
    }

  if (!argc)
    {
      name = NULL;
      pass = NULL;
    }
  else if (argc == 1)
    {
      name = argv[0];
      pass = "";
    }
  else if (argc == 2)
    {
      name = argv[0];
      pass = argv[1];
    }
  else
    {
      fprintf (stderr, "usage: " PGM " [<file> [<passphrase>]]\n");
      exit (1);
    }

  gcry_control (GCRYCTL_DISABLE_SECMEM, NULL);
  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL);

  if (name)
    {
      p12_set_verbosity (verbose, debug);
      ret = one_file (name, pass);
    }
  else
    {
      char *descfname, *p;

      if (verbose > 1)
        p12_set_verbosity (verbose > 1? (verbose - 1):0, debug);
      descfname = prepend_srcdir ("../tests/samplekeys/Description-p12");
      ret = run_tests_from_file (descfname);
      xfree (descfname);

      /* Check whether we have non-public regression test cases. */
      p = no_extra? NULL:value_from_gnupg_autogen_rc ("GNUPG_EXTRA_TESTS_DIR");
      if (p)
        {
          descfname = xstrconcat  (p, "/pkcs12/Description", NULL);
          xfree (p);
          ret |= run_tests_from_file (descfname);
          xfree (descfname);
        }
    }

  return ret;
}