Bump to 2.4.3
[platform/upstream/gpg2.git] / common / name-value.c
1 /* name-value.c - Parser and writer for a name-value format.
2  *      Copyright (C) 2016 g10 Code GmbH
3  *
4  * This file is part of GnuPG.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * GnuPG is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, see <https://www.gnu.org/licenses/>.
28  */
29
30 /*
31  * This module aso provides features for the extended private key
32  * format of gpg-agent.
33  */
34
35 #include <config.h>
36 #include <assert.h>
37 #include <gcrypt.h>
38 #include <gpg-error.h>
39 #include <string.h>
40
41 #include "mischelp.h"
42 #include "strlist.h"
43 #include "util.h"
44 #include "name-value.h"
45
46 struct name_value_container
47 {
48   struct name_value_entry *first;
49   struct name_value_entry *last;
50   unsigned int private_key_mode:1;
51 };
52
53
54 struct name_value_entry
55 {
56   struct name_value_entry *prev;
57   struct name_value_entry *next;
58
59   /* The name.  Comments and blank lines have NAME set to NULL.  */
60   char *name;
61
62   /* The value as stored in the file.  We store it when we parse
63      a file so that we can reproduce it.  */
64   strlist_t raw_value;
65
66   /* The decoded value.  */
67   char *value;
68 };
69
70
71 /* Helper */
72 static inline gpg_error_t
73 my_error_from_syserror (void)
74 {
75   return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
76 }
77
78
79 static inline gpg_error_t
80 my_error (gpg_err_code_t ec)
81 {
82   return gpg_err_make (default_errsource, ec);
83 }
84
85
86 \f
87
88 /* Allocation and deallocation.  */
89
90 /* Allocate a private key container structure.  */
91 nvc_t
92 nvc_new (void)
93 {
94   return xtrycalloc (1, sizeof (struct name_value_container));
95 }
96
97
98 /* Allocate a private key container structure for use with private keys.  */
99 nvc_t
100 nvc_new_private_key (void)
101 {
102   nvc_t nvc = nvc_new ();
103   if (nvc)
104     nvc->private_key_mode = 1;
105   return nvc;
106 }
107
108
109 static void
110 nve_release (nve_t entry, int private_key_mode)
111 {
112   if (entry == NULL)
113     return;
114
115   xfree (entry->name);
116   if (entry->value && private_key_mode)
117     wipememory (entry->value, strlen (entry->value));
118   xfree (entry->value);
119   if (private_key_mode)
120     free_strlist_wipe (entry->raw_value);
121   else
122     free_strlist (entry->raw_value);
123   xfree (entry);
124 }
125
126
127 /* Release a private key container structure.  */
128 void
129 nvc_release (nvc_t pk)
130 {
131   nve_t e, next;
132
133   if (pk == NULL)
134     return;
135
136   for (e = pk->first; e; e = next)
137     {
138       next = e->next;
139       nve_release (e, pk->private_key_mode);
140     }
141
142   xfree (pk);
143 }
144
145 \f
146
147 /* Dealing with names and values.  */
148
149 /* Check whether the given name is valid.  Valid names start with a
150    letter, end with a colon, and contain only alphanumeric characters
151    and the hyphen.  */
152 static int
153 valid_name (const char *name)
154 {
155   size_t i, len = strlen (name);
156
157   if (! alphap (name) || len == 0 || name[len - 1] != ':')
158     return 0;
159
160   for (i = 1; i < len - 1; i++)
161     if (! alnump (&name[i]) && name[i] != '-')
162       return 0;
163
164   return 1;
165 }
166
167
168 /* Makes sure that ENTRY has a RAW_VALUE.  */
169 static gpg_error_t
170 assert_raw_value (nve_t entry)
171 {
172   gpg_error_t err = 0;
173   size_t len, offset;
174 #define LINELEN 70
175   char buf[LINELEN+3];
176
177   if (entry->raw_value)
178     return 0;
179
180   len = strlen (entry->value);
181   offset = 0;
182   while (len)
183     {
184       size_t amount, linelen = LINELEN;
185
186       /* On the first line we need to subtract space for the name.  */
187       if (entry->raw_value == NULL && strlen (entry->name) < linelen)
188         linelen -= strlen (entry->name);
189
190       /* See if the rest of the value fits in this line.  */
191       if (len <= linelen)
192         amount = len;
193       else
194         {
195           size_t i;
196
197           /* Find a suitable space to break on.  */
198           for (i = linelen - 1; linelen - i < 30; i--)
199             if (ascii_isspace (entry->value[offset+i]))
200               break;
201
202           if (ascii_isspace (entry->value[offset+i]))
203             {
204               /* Found one.  */
205               amount = i;
206             }
207           else
208             {
209               /* Just induce a hard break.  */
210               amount = linelen;
211             }
212         }
213
214       snprintf (buf, sizeof buf, " %.*s\n", (int) amount,
215                 &entry->value[offset]);
216       if (append_to_strlist_try (&entry->raw_value, buf) == NULL)
217         {
218           err = my_error_from_syserror ();
219           goto leave;
220         }
221
222       offset += amount;
223       len -= amount;
224     }
225
226  leave:
227   if (err)
228     {
229       free_strlist_wipe (entry->raw_value);
230       entry->raw_value = NULL;
231     }
232
233   return err;
234 #undef LINELEN
235 }
236
237
238 /* Computes the length of the value encoded as continuation.  If
239    *SWALLOW_WS is set, all whitespace at the beginning of S is
240    swallowed.  If START is given, a pointer to the beginning of the
241    value is stored there.  */
242 static size_t
243 continuation_length (const char *s, int *swallow_ws, const char **start)
244 {
245   size_t len;
246
247   if (*swallow_ws)
248     {
249       /* The previous line was a blank line and we inserted a newline.
250          Swallow all whitespace at the beginning of this line.  */
251       while (ascii_isspace (*s))
252         s++;
253     }
254   else
255     {
256       /* Iff a continuation starts with more than one space, it
257          encodes a space.  */
258       if (ascii_isspace (*s))
259         s++;
260     }
261
262   /* Strip whitespace at the end.  */
263   len = strlen (s);
264   while (len > 0 && ascii_isspace (s[len-1]))
265     len--;
266
267   if (len == 0)
268     {
269       /* Blank lines encode newlines.  */
270       len = 1;
271       s = "\n";
272       *swallow_ws = 1;
273     }
274   else
275     *swallow_ws = 0;
276
277   if (start)
278     *start = s;
279
280   return len;
281 }
282
283
284 /* Makes sure that ENTRY has a VALUE.  */
285 static gpg_error_t
286 assert_value (nve_t entry)
287 {
288   size_t len;
289   int swallow_ws;
290   strlist_t s;
291   char *p;
292
293   if (entry->value)
294     return 0;
295
296   len = 0;
297   swallow_ws = 0;
298   for (s = entry->raw_value; s; s = s->next)
299     len += continuation_length (s->d, &swallow_ws, NULL);
300
301   /* Add one for the terminating zero.  */
302   len += 1;
303
304   entry->value = p = xtrymalloc (len);
305   if (entry->value == NULL)
306     return my_error_from_syserror ();
307
308   swallow_ws = 0;
309   for (s = entry->raw_value; s; s = s->next)
310     {
311       const char *start;
312       size_t l = continuation_length (s->d, &swallow_ws, &start);
313
314       memcpy (p, start, l);
315       p += l;
316     }
317
318   *p++ = 0;
319   assert (p - entry->value == len);
320
321   return 0;
322 }
323
324
325 /* Get the name.  */
326 char *
327 nve_name (nve_t pke)
328 {
329   return pke->name;
330 }
331
332
333 /* Get the value.  */
334 char *
335 nve_value (nve_t pke)
336 {
337   if (assert_value (pke))
338     return NULL;
339   return pke->value;
340 }
341
342 \f
343
344 /* Adding and modifying values.  */
345
346 /* Add (NAME, VALUE, RAW_VALUE) to PK.  NAME may be NULL for comments
347    and blank lines.  At least one of VALUE and RAW_VALUE must be
348    given.  If PRESERVE_ORDER is not given, entries with the same name
349    are grouped.  NAME, VALUE and RAW_VALUE is consumed.  */
350 static gpg_error_t
351 _nvc_add (nvc_t pk, char *name, char *value, strlist_t raw_value,
352           int preserve_order)
353 {
354   gpg_error_t err = 0;
355   nve_t e;
356
357   assert (value || raw_value);
358
359   if (name && ! valid_name (name))
360     {
361       err = my_error (GPG_ERR_INV_NAME);
362       goto leave;
363     }
364
365   if (name
366       && pk->private_key_mode
367       && !ascii_strcasecmp (name, "Key:")
368       && nvc_lookup (pk, "Key:"))
369     {
370       err = my_error (GPG_ERR_INV_NAME);
371       goto leave;
372     }
373
374   e = xtrycalloc (1, sizeof *e);
375   if (e == NULL)
376     {
377       err = my_error_from_syserror ();
378       goto leave;
379     }
380
381   e->name = name;
382   e->value = value;
383   e->raw_value = raw_value;
384
385   if (pk->first)
386     {
387       nve_t last;
388
389       if (preserve_order || name == NULL)
390         last = pk->last;
391       else
392         {
393           /* See if there is already an entry with NAME.  */
394           last = nvc_lookup (pk, name);
395
396           /* If so, find the last in that block.  */
397           if (last)
398             {
399               while (last->next)
400                 {
401                   nve_t next = last->next;
402
403                   if (next->name && ascii_strcasecmp (next->name, name) == 0)
404                     last = next;
405                   else
406                     break;
407                 }
408             }
409           else /* Otherwise, just find the last entry.  */
410             last = pk->last;
411         }
412
413       if (last->next)
414         {
415           e->prev = last;
416           e->next = last->next;
417           last->next = e;
418           e->next->prev = e;
419         }
420       else
421         {
422           e->prev = last;
423           last->next = e;
424           pk->last = e;
425         }
426     }
427   else
428     pk->first = pk->last = e;
429
430  leave:
431   if (err)
432     {
433       xfree (name);
434       if (value)
435         wipememory (value, strlen (value));
436       xfree (value);
437       free_strlist_wipe (raw_value);
438     }
439
440   return err;
441 }
442
443
444 /* Add (NAME, VALUE) to PK.  If an entry with NAME already exists, it
445    is not updated but the new entry is appended.  */
446 gpg_error_t
447 nvc_add (nvc_t pk, const char *name, const char *value)
448 {
449   char *k, *v;
450
451   k = xtrystrdup (name);
452   if (k == NULL)
453     return my_error_from_syserror ();
454
455   v = xtrystrdup (value);
456   if (v == NULL)
457     {
458       xfree (k);
459       return my_error_from_syserror ();
460     }
461
462   return _nvc_add (pk, k, v, NULL, 0);
463 }
464
465
466 /* Add (NAME, VALUE) to PK.  If an entry with NAME already exists, it
467    is updated with VALUE.  If multiple entries with NAME exist, the
468    first entry is updated.  */
469 gpg_error_t
470 nvc_set (nvc_t pk, const char *name, const char *value)
471 {
472   nve_t e;
473
474   if (! valid_name (name))
475     return GPG_ERR_INV_NAME;
476
477   e = nvc_lookup (pk, name);
478   if (e)
479     {
480       char *v;
481
482       v = xtrystrdup (value);
483       if (v == NULL)
484         return my_error_from_syserror ();
485
486       free_strlist_wipe (e->raw_value);
487       e->raw_value = NULL;
488       if (e->value)
489         wipememory (e->value, strlen (e->value));
490       xfree (e->value);
491       e->value = v;
492
493       return 0;
494     }
495   else
496     return nvc_add (pk, name, value);
497 }
498
499
500 /* Update entry E to VALUE.  */
501 gpg_error_t
502 nve_set (nve_t e, const char *value)
503 {
504   char *v;
505
506   if (!e)
507     return GPG_ERR_INV_ARG;
508
509   v = xtrystrdup (value? value:"");
510   if (!v)
511     return my_error_from_syserror ();
512
513   free_strlist_wipe (e->raw_value);
514   e->raw_value = NULL;
515   if (e->value)
516     wipememory (e->value, strlen (e->value));
517   xfree (e->value);
518   e->value = v;
519
520   return 0;
521 }
522
523
524 /* Delete the given entry from PK.  */
525 void
526 nvc_delete (nvc_t pk, nve_t entry)
527 {
528   if (entry->prev)
529     entry->prev->next = entry->next;
530   else
531     pk->first = entry->next;
532
533   if (entry->next)
534     entry->next->prev = entry->prev;
535   else
536     pk->last = entry->prev;
537
538   nve_release (entry, pk->private_key_mode);
539 }
540
541
542 /* Delete the entries with NAME from PK.  */
543 void
544 nvc_delete_named (nvc_t pk, const char *name)
545 {
546   nve_t e;
547
548   if (!valid_name (name))
549     return;
550
551   while ((e = nvc_lookup (pk, name)))
552     nvc_delete (pk, e);
553 }
554
555
556 \f
557
558 /* Lookup and iteration.  */
559
560 /* Get the first non-comment entry.  */
561 nve_t
562 nvc_first (nvc_t pk)
563 {
564   nve_t entry;
565
566   if (!pk)
567     return NULL;
568
569   for (entry = pk->first; entry; entry = entry->next)
570     if (entry->name)
571       return entry;
572
573   return NULL;
574 }
575
576
577 /* Get the first entry with the given name.  Return NULL if it does
578  * not exist.  */
579 nve_t
580 nvc_lookup (nvc_t pk, const char *name)
581 {
582   nve_t entry;
583
584   if (!pk)
585     return NULL;
586
587   for (entry = pk->first; entry; entry = entry->next)
588     if (entry->name && ascii_strcasecmp (entry->name, name) == 0)
589       return entry;
590
591   return NULL;
592 }
593
594
595 /* Get the next non-comment entry.  */
596 nve_t
597 nve_next (nve_t entry)
598 {
599   for (entry = entry->next; entry; entry = entry->next)
600     if (entry->name)
601       return entry;
602   return NULL;
603 }
604
605
606 /* Get the next entry with the given name.  */
607 nve_t
608 nve_next_value (nve_t entry, const char *name)
609 {
610   for (entry = entry->next; entry; entry = entry->next)
611     if (entry->name && ascii_strcasecmp (entry->name, name) == 0)
612       return entry;
613   return NULL;
614 }
615
616
617 /* Return the string for the first entry in NVC with NAME.  If an
618  * entry with NAME is missing in NVC or its value is the empty string
619  * NULL is returned.  Note that the the returned string is a pointer
620  * into NVC.  */
621 const char *
622 nvc_get_string (nvc_t nvc, const char *name)
623 {
624   nve_t item;
625
626   if (!nvc)
627     return NULL;
628   item = nvc_lookup (nvc, name);
629   if (!item)
630     return NULL;
631   return nve_value (item);
632 }
633
634
635 /* Return true (ie. a non-zero value) if NAME exists and its value is
636  * true; that is either "yes", "true", or a decimal value unequal to 0.  */
637 int
638 nvc_get_boolean (nvc_t nvc, const char *name)
639 {
640   nve_t item;
641   const char *s;
642   int n;
643
644   if (!nvc)
645     return 0;
646   item = nvc_lookup (nvc, name);
647   if (!item)
648     return 0;
649   s = nve_value (item);
650   if (!s)
651     return 0;
652   n = atoi (s);
653   if (n)
654     return n;
655   if (!ascii_strcasecmp (s, "yes") || !ascii_strcasecmp (s, "true"))
656     return 1;
657   return 0;
658 }
659
660
661 \f
662
663 /* Private key handling.  */
664
665 /* Get the private key.  */
666 gpg_error_t
667 nvc_get_private_key (nvc_t pk, gcry_sexp_t *retsexp)
668 {
669   gpg_error_t err;
670   nve_t e;
671
672   e = pk->private_key_mode? nvc_lookup (pk, "Key:") : NULL;
673   if (e == NULL)
674     return my_error (GPG_ERR_MISSING_KEY);
675
676   err = assert_value (e);
677   if (err)
678     return err;
679
680   return gcry_sexp_sscan (retsexp, NULL, e->value, strlen (e->value));
681 }
682
683
684 /* Set the private key.  */
685 gpg_error_t
686 nvc_set_private_key (nvc_t pk, gcry_sexp_t sexp)
687 {
688   gpg_error_t err;
689   char *raw, *clean, *p;
690   size_t len, i;
691
692   if (!pk->private_key_mode)
693     return my_error (GPG_ERR_MISSING_KEY);
694
695   len = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0);
696
697   raw = xtrymalloc (len);
698   if (raw == NULL)
699     return my_error_from_syserror ();
700
701   clean = xtrymalloc (len);
702   if (clean == NULL)
703     {
704       xfree (raw);
705       return my_error_from_syserror ();
706     }
707
708   gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, raw, len);
709
710   /* Strip any whitespace at the end.  */
711   i = strlen (raw) - 1;
712   while (i && ascii_isspace (raw[i]))
713     {
714       raw[i] = 0;
715       i--;
716     }
717
718   /* Replace any newlines with spaces, remove superfluous whitespace.  */
719   len = strlen (raw);
720   for (p = clean, i = 0; i < len; i++)
721     {
722       char c = raw[i];
723
724       /* Collapse contiguous and superfluous spaces.  */
725       if (ascii_isspace (c) && i > 0
726           && (ascii_isspace (raw[i-1]) || raw[i-1] == '(' || raw[i-1] == ')'))
727         continue;
728
729       if (c == '\n')
730         c = ' ';
731
732       *p++ = c;
733     }
734   *p = 0;
735
736   err = nvc_set (pk, "Key:", clean);
737   xfree (raw);
738   xfree (clean);
739   return err;
740 }
741
742 \f
743
744 /* Parsing and serialization.  */
745
746 static gpg_error_t
747 do_nvc_parse (nvc_t *result, int *errlinep, estream_t stream,
748               int for_private_key)
749 {
750   gpg_error_t err = 0;
751   gpgrt_ssize_t len;
752   char *buf = NULL;
753   size_t buf_len = 0;
754   char *name = NULL;
755   strlist_t raw_value = NULL;
756
757   *result = for_private_key? nvc_new_private_key () : nvc_new ();
758   if (*result == NULL)
759     return my_error_from_syserror ();
760
761   if (errlinep)
762     *errlinep = 0;
763   while ((len = es_read_line (stream, &buf, &buf_len, NULL)) > 0)
764     {
765       char *p;
766       if (errlinep)
767         *errlinep += 1;
768
769       /* Skip any whitespace.  */
770       for (p = buf; *p && ascii_isspace (*p); p++)
771         /* Do nothing.  */;
772
773       if (name && (spacep (buf) || *p == 0))
774         {
775           /* A continuation.  */
776           if (append_to_strlist_try (&raw_value, buf) == NULL)
777             {
778               err = my_error_from_syserror ();
779               goto leave;
780             }
781           continue;
782         }
783
784       /* No continuation.  Add the current entry if any.  */
785       if (raw_value)
786         {
787           err = _nvc_add (*result, name, NULL, raw_value, 1);
788           name = NULL;
789           if (err)
790             goto leave;
791         }
792
793       /* And prepare for the next one.  */
794       name = NULL;
795       raw_value = NULL;
796
797       if (*p != 0 && *p != '#')
798         {
799           char *colon, *value, tmp;
800
801           colon = strchr (buf, ':');
802           if (colon == NULL)
803             {
804               err = my_error (GPG_ERR_INV_VALUE);
805               goto leave;
806             }
807
808           value = colon + 1;
809           tmp = *value;
810           *value = 0;
811           name = xtrystrdup (p);
812           *value = tmp;
813
814           if (name == NULL)
815             {
816               err = my_error_from_syserror ();
817               goto leave;
818             }
819
820           if (append_to_strlist_try (&raw_value, value) == NULL)
821             {
822               err = my_error_from_syserror ();
823               goto leave;
824             }
825           continue;
826         }
827
828       if (append_to_strlist_try (&raw_value, buf) == NULL)
829         {
830           err = my_error_from_syserror ();
831           goto leave;
832         }
833     }
834   if (len < 0)
835     {
836       err = gpg_error_from_syserror ();
837       goto leave;
838     }
839
840   /* Add the final entry.  */
841   if (raw_value)
842     {
843       err = _nvc_add (*result, name, NULL, raw_value, 1);
844       name = NULL;
845     }
846
847  leave:
848   xfree (name);
849   gpgrt_free (buf);
850   if (err)
851     {
852       nvc_release (*result);
853       *result = NULL;
854     }
855
856   return err;
857 }
858
859
860 /* Parse STREAM and return a newly allocated name value container
861    structure in RESULT.  If ERRLINEP is given, the line number the
862    parser was last considering is stored there.  */
863 gpg_error_t
864 nvc_parse (nvc_t *result, int *errlinep, estream_t stream)
865 {
866   return do_nvc_parse (result, errlinep, stream, 0);
867 }
868
869
870 /* Parse STREAM and return a newly allocated name value container
871    structure in RESULT - assuming the extended private key format.  If
872    ERRLINEP is given, the line number the parser was last considering
873    is stored there.  */
874 gpg_error_t
875 nvc_parse_private_key (nvc_t *result, int *errlinep, estream_t stream)
876 {
877   return do_nvc_parse (result, errlinep, stream, 1);
878 }
879
880
881 /* Helper fpr nvc_write.  */
882 static gpg_error_t
883 write_one_entry (nve_t entry, estream_t stream)
884 {
885   gpg_error_t err;
886   strlist_t sl;
887
888   if (entry->name)
889     es_fputs (entry->name, stream);
890
891   err = assert_raw_value (entry);
892   if (err)
893     return err;
894
895   for (sl = entry->raw_value; sl; sl = sl->next)
896     es_fputs (sl->d, stream);
897
898   if (es_ferror (stream))
899     return my_error_from_syserror ();
900
901   return 0;
902 }
903
904
905 /* Write a representation of PK to STREAM.  */
906 gpg_error_t
907 nvc_write (nvc_t pk, estream_t stream)
908 {
909   gpg_error_t err = 0;
910   nve_t entry;
911   nve_t keyentry = NULL;
912
913   for (entry = pk->first; entry; entry = entry->next)
914     {
915       if (pk->private_key_mode
916           && entry->name && !ascii_strcasecmp (entry->name, "Key:"))
917         {
918           if (!keyentry)
919             keyentry = entry;
920           continue;
921         }
922
923       err = write_one_entry (entry, stream);
924       if (err)
925         return err;
926     }
927
928   /* In private key mode we write the Key always last.  */
929   if (keyentry)
930     err = write_one_entry (keyentry, stream);
931
932   return err;
933 }