summaryrefslogtreecommitdiffstats
path: root/common/name-value.c
blob: b9b13d185cb84af1ea376ce67350121828acac1e (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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/* name-value.c - Parser and writer for a name-value format.
 *	Copyright (C) 2016 g10 Code GmbH
 *
 * This file is part of GnuPG.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of either
 *
 *   - the GNU Lesser General Public License as published by the Free
 *     Software Foundation; either version 3 of the License, or (at
 *     your option) any later version.
 *
 * or
 *
 *   - the GNU General Public License as published by the Free
 *     Software Foundation; either version 2 of the License, or (at
 *     your option) any later version.
 *
 * or both in parallel, as here.
 *
 * 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/>.
 */

/*
 * This module aso provides features for the extended private key
 * format of gpg-agent.
 */

#include <config.h>
#include <assert.h>
#include <gcrypt.h>
#include <gpg-error.h>
#include <string.h>

#include "mischelp.h"
#include "strlist.h"
#include "util.h"
#include "name-value.h"

struct name_value_container
{
  struct name_value_entry *first;
  struct name_value_entry *last;
  unsigned int private_key_mode:1;
};


struct name_value_entry
{
  struct name_value_entry *prev;
  struct name_value_entry *next;

  /* The name.  Comments and blank lines have NAME set to NULL.  */
  char *name;

  /* The value as stored in the file.  We store it when we parse
     a file so that we can reproduce it.  */
  strlist_t raw_value;

  /* The decoded value.  */
  char *value;
};


/* Helper */
static inline gpg_error_t
my_error_from_syserror (void)
{
  return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
}


static inline gpg_error_t
my_error (gpg_err_code_t ec)
{
  return gpg_err_make (default_errsource, ec);
}




/* Allocation and deallocation.  */

/* Allocate a private key container structure.  */
nvc_t
nvc_new (void)
{
  return xtrycalloc (1, sizeof (struct name_value_container));
}


/* Allocate a private key container structure for use with private keys.  */
nvc_t
nvc_new_private_key (void)
{
  nvc_t nvc = nvc_new ();
  if (nvc)
    nvc->private_key_mode = 1;
  return nvc;
}


static void
nve_release (nve_t entry, int private_key_mode)
{
  if (entry == NULL)
    return;

  xfree (entry->name);
  if (entry->value && private_key_mode)
    wipememory (entry->value, strlen (entry->value));
  xfree (entry->value);
  if (private_key_mode)
    free_strlist_wipe (entry->raw_value);
  else
    free_strlist (entry->raw_value);
  xfree (entry);
}


/* Release a private key container structure.  */
void
nvc_release (nvc_t pk)
{
  nve_t e, next;

  if (pk == NULL)
    return;

  for (e = pk->first; e; e = next)
    {
      next = e->next;
      nve_release (e, pk->private_key_mode);
    }

  xfree (pk);
}



/* Dealing with names and values.  */

/* Check whether the given name is valid.  Valid names start with a
   letter, end with a colon, and contain only alphanumeric characters
   and the hyphen.  */
static int
valid_name (const char *name)
{
  size_t i, len = strlen (name);

  if (! alphap (name) || len == 0 || name[len - 1] != ':')
    return 0;

  for (i = 1; i < len - 1; i++)
    if (! alnump (&name[i]) && name[i] != '-')
      return 0;

  return 1;
}


/* Makes sure that ENTRY has a RAW_VALUE.  */
static gpg_error_t
assert_raw_value (nve_t entry)
{
  gpg_error_t err = 0;
  size_t len, offset;
#define LINELEN	70
  char buf[LINELEN+3];

  if (entry->raw_value)
    return 0;

  len = strlen (entry->value);
  offset = 0;
  while (len)
    {
      size_t amount, linelen = LINELEN;

      /* On the first line we need to subtract space for the name.  */
      if (entry->raw_value == NULL && strlen (entry->name) < linelen)
	linelen -= strlen (entry->name);

      /* See if the rest of the value fits in this line.  */
      if (len <= linelen)
	amount = len;
      else
	{
	  size_t i;

	  /* Find a suitable space to break on.  */
	  for (i = linelen - 1; linelen - i < 30; i--)
	    if (ascii_isspace (entry->value[offset+i]))
	      break;

	  if (ascii_isspace (entry->value[offset+i]))
	    {
	      /* Found one.  */
	      amount = i;
	    }
	  else
	    {
	      /* Just induce a hard break.  */
	      amount = linelen;
	    }
	}

      snprintf (buf, sizeof buf, " %.*s\n", (int) amount,
		&entry->value[offset]);
      if (append_to_strlist_try (&entry->raw_value, buf) == NULL)
	{
	  err = my_error_from_syserror ();
	  goto leave;
	}

      offset += amount;
      len -= amount;
    }

 leave:
  if (err)
    {
      free_strlist_wipe (entry->raw_value);
      entry->raw_value = NULL;
    }

  return err;
#undef LINELEN
}


/* Computes the length of the value encoded as continuation.  If
   *SWALLOW_WS is set, all whitespace at the beginning of S is
   swallowed.  If START is given, a pointer to the beginning of the
   value is stored there.  */
static size_t
continuation_length (const char *s, int *swallow_ws, const char **start)
{
  size_t len;

  if (*swallow_ws)
    {
      /* The previous line was a blank line and we inserted a newline.
	 Swallow all whitespace at the beginning of this line.  */
      while (ascii_isspace (*s))
	s++;
    }
  else
    {
      /* Iff a continuation starts with more than one space, it
	 encodes a space.  */
      if (ascii_isspace (*s))
	s++;
    }

  /* Strip whitespace at the end.  */
  len = strlen (s);
  while (len > 0 && ascii_isspace (s[len-1]))
    len--;

  if (len == 0)
    {
      /* Blank lines encode newlines.  */
      len = 1;
      s = "\n";
      *swallow_ws = 1;
    }
  else
    *swallow_ws = 0;

  if (start)
    *start = s;

  return len;
}


/* Makes sure that ENTRY has a VALUE.  */
static gpg_error_t
assert_value (nve_t entry)
{
  size_t len;
  int swallow_ws;
  strlist_t s;
  char *p;

  if (entry->value)
    return 0;

  len = 0;
  swallow_ws = 0;
  for (s = entry->raw_value; s; s = s->next)
    len += continuation_length (s->d, &swallow_ws, NULL);

  /* Add one for the terminating zero.  */
  len += 1;

  entry->value = p = xtrymalloc (len);
  if (entry->value == NULL)
    return my_error_from_syserror ();

  swallow_ws = 0;
  for (s = entry->raw_value; s; s = s->next)
    {
      const char *start;
      size_t l = continuation_length (s->d, &swallow_ws, &start);

      memcpy (p, start, l);
      p += l;
    }

  *p++ = 0;
  assert (p - entry->value == len);

  return 0;
}


/* Get the name.  */
char *
nve_name (nve_t pke)
{
  return pke->name;
}


/* Get the value.  */
char *
nve_value (nve_t pke)
{
  if (assert_value (pke))
    return NULL;
  return pke->value;
}



/* Adding and modifying values.  */

/* Add (NAME, VALUE, RAW_VALUE) to PK.  NAME may be NULL for comments
   and blank lines.  At least one of VALUE and RAW_VALUE must be
   given.  If PRESERVE_ORDER is not given, entries with the same name
   are grouped.  NAME, VALUE and RAW_VALUE is consumed.  */
static gpg_error_t
_nvc_add (nvc_t pk, char *name, char *value, strlist_t raw_value,
	  int preserve_order)
{
  gpg_error_t err = 0;
  nve_t e;

  assert (value || raw_value);

  if (name && ! valid_name (name))
    {
      err = my_error (GPG_ERR_INV_NAME);
      goto leave;
    }

  if (name
      && pk->private_key_mode
      && !ascii_strcasecmp (name, "Key:")
      && nvc_lookup (pk, "Key:"))
    {
      err = my_error (GPG_ERR_INV_NAME);
      goto leave;
    }

  e = xtrycalloc (1, sizeof *e);
  if (e == NULL)
    {
      err = my_error_from_syserror ();
      goto leave;
    }

  e->name = name;
  e->value = value;
  e->raw_value = raw_value;

  if (pk->first)
    {
      nve_t last;

      if (preserve_order || name == NULL)
	last = pk->last;
      else
	{
	  /* See if there is already an entry with NAME.  */
	  last = nvc_lookup (pk, name);

	  /* If so, find the last in that block.  */
	  if (last)
            {
              while (last->next)
                {
                  nve_t next = last->next;

                  if (next->name && ascii_strcasecmp (next->name, name) == 0)
                    last = next;
                  else
                    break;
                }
            }
	  else /* Otherwise, just find the last entry.  */
	    last = pk->last;
	}

      if (last->next)
	{
	  e->prev = last;
	  e->next = last->next;
	  last->next = e;
	  e->next->prev = e;
	}
      else
	{
	  e->prev = last;
	  last->next = e;
	  pk->last = e;
	}
    }
  else
    pk->first = pk->last = e;

 leave:
  if (err)
    {
      xfree (name);
      if (value)
	wipememory (value, strlen (value));
      xfree (value);
      free_strlist_wipe (raw_value);
    }

  return err;
}


/* Add (NAME, VALUE) to PK.  If an entry with NAME already exists, it
   is not updated but the new entry is appended.  */
gpg_error_t
nvc_add (nvc_t pk, const char *name, const char *value)
{
  char *k, *v;

  k = xtrystrdup (name);
  if (k == NULL)
    return my_error_from_syserror ();

  v = xtrystrdup (value);
  if (v == NULL)
    {
      xfree (k);
      return my_error_from_syserror ();
    }

  return _nvc_add (pk, k, v, NULL, 0);
}


/* Add (NAME, VALUE) to PK.  If an entry with NAME already exists, it
   is updated with VALUE.  If multiple entries with NAME exist, the
   first entry is updated.  */
gpg_error_t
nvc_set (nvc_t pk, const char *name, const char *value)
{
  nve_t e;

  if (! valid_name (name))
    return GPG_ERR_INV_NAME;

  e = nvc_lookup (pk, name);
  if (e)
    return nve_set (e, value);
  else
    return nvc_add (pk, name, value);
}


/* Update entry E to VALUE.  */
gpg_error_t
nve_set (nve_t e, const char *value)
{
  char *v;

  if (!e)
    return GPG_ERR_INV_ARG;

  v = xtrystrdup (value? value:"");
  if (!v)
    return my_error_from_syserror ();

  free_strlist_wipe (e->raw_value);
  e->raw_value = NULL;
  if (e->value)
    wipememory (e->value, strlen (e->value));
  xfree (e->value);
  e->value = v;

  return 0;
}


/* Delete the given entry from PK.  */
void
nvc_delete (nvc_t pk, nve_t entry)
{
  if (entry->prev)
    entry->prev->next = entry->next;
  else
    pk->first = entry->next;

  if (entry->next)
    entry->next->prev = entry->prev;
  else
    pk->last = entry->prev;

  nve_release (entry, pk->private_key_mode);
}

/* Delete the entries with NAME from PK.  */
void
nvc_delete_named (nvc_t pk, const char *name)
{
  nve_t e;

  if (!valid_name (name))
    return;

  while ((e = nvc_lookup (pk, name)))
    nvc_delete (pk, e);
}




/* Lookup and iteration.  */

/* Get the first non-comment entry.  */
nve_t
nvc_first (nvc_t pk)
{
  nve_t entry;

  if (!pk)
    return NULL;

  for (entry = pk->first; entry; entry = entry->next)
    if (entry->name)
      return entry;

  return NULL;
}


/* Get the first entry with the given name.  Return NULL if it does
 * not exist.  */
nve_t
nvc_lookup (nvc_t pk, const char *name)
{
  nve_t entry;

  if (!pk)
    return NULL;

  for (entry = pk->first; entry; entry = entry->next)
    if (entry->name && ascii_strcasecmp (entry->name, name) == 0)
      return entry;

  return NULL;
}


/* Get the next non-comment entry.  */
nve_t
nve_next (nve_t entry)
{
  for (entry = entry->next; entry; entry = entry->next)
    if (entry->name)
      return entry;
  return NULL;
}


/* Get the next entry with the given name.  */
nve_t
nve_next_value (nve_t entry, const char *name)
{
  for (entry = entry->next; entry; entry = entry->next)
    if (entry->name && ascii_strcasecmp (entry->name, name) == 0)
      return entry;
  return NULL;
}


/* Return the string for the first entry in NVC with NAME.  If an
 * entry with NAME is missing in NVC or its value is the empty string
 * NULL is returned.  Note that the returned string is a pointer
 * into NVC.  */
const char *
nvc_get_string (nvc_t nvc, const char *name)
{
  nve_t item;

  if (!nvc)
    return NULL;
  item = nvc_lookup (nvc, name);
  if (!item)
    return NULL;
  return nve_value (item);
}


/* Return true if NAME exists and its value is true; that is either
 * "yes", "true", or a decimal value unequal to 0.  */
int
nvc_get_boolean (nvc_t nvc, const char *name)
{
  nve_t item;
  const char *s;

  if (!nvc)
    return 0;
  item = nvc_lookup (nvc, name);
  if (!item)
    return 0;
  s = nve_value (item);
  if (s && (atoi (s)
            || !ascii_strcasecmp (s, "yes")
            || !ascii_strcasecmp (s, "true")))
    return 1;
  return 0;
}



/* Private key handling.  */

/* Get the private key.  */
gpg_error_t
nvc_get_private_key (nvc_t pk, gcry_sexp_t *retsexp)
{
  gpg_error_t err;
  nve_t e;

  e = pk->private_key_mode? nvc_lookup (pk, "Key:") : NULL;
  if (e == NULL)
    return my_error (GPG_ERR_MISSING_KEY);

  err = assert_value (e);
  if (err)
    return err;

  return gcry_sexp_sscan (retsexp, NULL, e->value, strlen (e->value));
}


/* Set the private key.  */
gpg_error_t
nvc_set_private_key (nvc_t pk, gcry_sexp_t sexp)
{
  gpg_error_t err;
  char *raw, *clean, *p;
  size_t len, i;

  if (!pk->private_key_mode)
    return my_error (GPG_ERR_MISSING_KEY);

  len = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0);

  raw = xtrymalloc (len);
  if (raw == NULL)
    return my_error_from_syserror ();

  clean = xtrymalloc (len);
  if (clean == NULL)
    {
      xfree (raw);
      return my_error_from_syserror ();
    }

  gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, raw, len);

  /* Strip any whitespace at the end.  */
  i = strlen (raw) - 1;
  while (i && ascii_isspace (raw[i]))
    {
      raw[i] = 0;
      i--;
    }

  /* Replace any newlines with spaces, remove superfluous whitespace.  */
  len = strlen (raw);
  for (p = clean, i = 0; i < len; i++)
    {
      char c = raw[i];

      /* Collapse contiguous and superfluous spaces.  */
      if (ascii_isspace (c) && i > 0
	  && (ascii_isspace (raw[i-1]) || raw[i-1] == '(' || raw[i-1] == ')'))
	continue;

      if (c == '\n')
	c = ' ';

      *p++ = c;
    }
  *p = 0;

  err = nvc_set (pk, "Key:", clean);
  xfree (raw);
  xfree (clean);
  return err;
}



/* Parsing and serialization.  */

static gpg_error_t
do_nvc_parse (nvc_t *result, int *errlinep, estream_t stream,
              int for_private_key)
{
  gpg_error_t err = 0;
  gpgrt_ssize_t len;
  char *buf = NULL;
  size_t buf_len = 0;
  char *name = NULL;
  strlist_t raw_value = NULL;

  *result = for_private_key? nvc_new_private_key () : nvc_new ();
  if (*result == NULL)
    return my_error_from_syserror ();

  if (errlinep)
    *errlinep = 0;
  while ((len = es_read_line (stream, &buf, &buf_len, NULL)) > 0)
    {
      char *p;
      if (errlinep)
	*errlinep += 1;

      /* Skip any whitespace.  */
      for (p = buf; *p && ascii_isspace (*p); p++)
	/* Do nothing.  */;

      if (name && (spacep (buf) || *p == 0))
	{
	  /* A continuation.  */
	  if (append_to_strlist_try (&raw_value, buf) == NULL)
	    {
	      err = my_error_from_syserror ();
	      goto leave;
	    }
	  continue;
	}

      /* No continuation.  Add the current entry if any.  */
      if (raw_value)
	{
	  err = _nvc_add (*result, name, NULL, raw_value, 1);
          name = NULL;
	  if (err)
	    goto leave;
	}

      /* And prepare for the next one.  */
      name = NULL;
      raw_value = NULL;

      if (*p != 0 && *p != '#')
	{
	  char *colon, *value, tmp;

	  colon = strchr (buf, ':');
	  if (colon == NULL)
	    {
	      err = my_error (GPG_ERR_INV_VALUE);
	      goto leave;
	    }

	  value = colon + 1;
	  tmp = *value;
	  *value = 0;
	  name = xtrystrdup (p);
	  *value = tmp;

	  if (name == NULL)
	    {
	      err = my_error_from_syserror ();
	      goto leave;
	    }

	  if (append_to_strlist_try (&raw_value, value) == NULL)
	    {
	      err = my_error_from_syserror ();
	      goto leave;
	    }
	  continue;
	}

      if (append_to_strlist_try (&raw_value, buf) == NULL)
	{
	  err = my_error_from_syserror ();
	  goto leave;
	}
    }
  if (len < 0)
    {
      err = gpg_error_from_syserror ();
      goto leave;
    }

  /* Add the final entry.  */
  if (raw_value)
    err = _nvc_add (*result, name, NULL, raw_value, 1);

 leave:
  gpgrt_free (buf);
  if (err)
    {
      nvc_release (*result);
      *result = NULL;
    }

  return err;
}


/* Parse STREAM and return a newly allocated name value container
   structure in RESULT.  If ERRLINEP is given, the line number the
   parser was last considering is stored there.  */
gpg_error_t
nvc_parse (nvc_t *result, int *errlinep, estream_t stream)
{
  return do_nvc_parse (result, errlinep, stream, 0);
}


/* Parse STREAM and return a newly allocated name value container
   structure in RESULT - assuming the extended private key format.  If
   ERRLINEP is given, the line number the parser was last considering
   is stored there.  */
gpg_error_t
nvc_parse_private_key (nvc_t *result, int *errlinep, estream_t stream)
{
  return do_nvc_parse (result, errlinep, stream, 1);
}


/* Helper fpr nvc_write.  */
static gpg_error_t
write_one_entry (nve_t entry, estream_t stream)
{
  gpg_error_t err;
  strlist_t sl;

  if (entry->name)
    es_fputs (entry->name, stream);

  err = assert_raw_value (entry);
  if (err)
    return err;

  for (sl = entry->raw_value; sl; sl = sl->next)
    es_fputs (sl->d, stream);

  if (es_ferror (stream))
    return my_error_from_syserror ();

  return 0;
}


/* Write a representation of PK to STREAM.  */
gpg_error_t
nvc_write (nvc_t pk, estream_t stream)
{
  gpg_error_t err = 0;
  nve_t entry;
  nve_t keyentry = NULL;

  for (entry = pk->first; entry; entry = entry->next)
    {
      if (pk->private_key_mode
          && entry->name && !ascii_strcasecmp (entry->name, "Key:"))
        {
          if (!keyentry)
            keyentry = entry;
          continue;
        }

      err = write_one_entry (entry, stream);
      if (err)
	return err;
    }

  /* In private key mode we write the Key always last.  */
  if (keyentry)
    err = write_one_entry (keyentry, stream);

  return err;
}