1 /* trustlist.c - Maintain the list of trusted keys
2 * Copyright (C) 2002, 2004, 2006, 2007, 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG 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.
11 * GnuPG 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.
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/>.
32 #include <assuan.h> /* fixme: need a way to avoid assuan calls here */
37 /* A structure to store the information from the trust file. */
42 int disabled
:1; /* This entry is disabled. */
43 int for_pgp
:1; /* Set by '*' or 'P' as first flag. */
44 int for_smime
:1; /* Set by '*' or 'S' as first flag. */
45 int relax
:1; /* Relax checking of root certificate
47 int cm
:1; /* Use chain model for validation. */
49 unsigned char fpr
[20]; /* The binary fingerprint. */
51 typedef struct trustitem_s trustitem_t
;
53 /* Malloced table and its allocated size with all trust items. */
54 static trustitem_t
*trusttable
;
55 static size_t trusttablesize
;
56 /* A mutex used to protect the table. */
57 static pth_mutex_t trusttable_lock
;
61 static const char headerblurb
[] =
62 "# This is the list of trusted keys. Comment lines, like this one, as\n"
63 "# well as empty lines are ignored. Lines have a length limit but this\n"
64 "# is not a serious limitation as the format of the entries is fixed and\n"
65 "# checked by gpg-agent. A non-comment line starts with optional white\n"
66 "# space, followed by the SHA-1 fingerpint in hex, followed by a flag\n"
67 "# which may be one of 'P', 'S' or '*' and optionally followed by a list of\n"
68 "# other flags. The fingerprint may be prefixed with a '!' to mark the\n"
69 "# key as not trusted. You should give the gpg-agent a HUP or run the\n"
70 "# command \"gpgconf --reload gpg-agent\" after changing this file.\n"
72 "# Include the default trust list\n"
77 /* This function must be called once to initialize this module. This
78 has to be done before a second thread is spawned. We can't do the
79 static initialization because Pth emulation code might not be able
80 to do a static init; in particular, it is not possible for W32. */
82 initialize_module_trustlist (void)
84 static int initialized
;
88 if (!pth_mutex_init (&trusttable_lock
))
89 log_fatal ("error initializing mutex: %s\n", strerror (errno
));
98 lock_trusttable (void)
100 if (!pth_mutex_acquire (&trusttable_lock
, 0, NULL
))
101 log_fatal ("failed to acquire mutex in %s\n", __FILE__
);
105 unlock_trusttable (void)
107 if (!pth_mutex_release (&trusttable_lock
))
108 log_fatal ("failed to release mutex in %s\n", __FILE__
);
114 read_one_trustfile (const char *fname
, int allow_include
,
115 trustitem_t
**addr_of_table
,
116 size_t *addr_of_tablesize
,
117 int *addr_of_tableidx
)
123 trustitem_t
*table
, *ti
;
128 table
= *addr_of_table
;
129 tablesize
= *addr_of_tablesize
;
130 tableidx
= *addr_of_tableidx
;
132 fp
= fopen (fname
, "r");
135 err
= gpg_error_from_syserror ();
136 log_error (_("error opening `%s': %s\n"), fname
, gpg_strerror (err
));
140 while (fgets (line
, DIM(line
)-1, fp
))
144 if (!*line
|| line
[strlen(line
)-1] != '\n')
146 /* Eat until end of line. */
147 while ( (c
=getc (fp
)) != EOF
&& c
!= '\n')
149 err
= gpg_error (*line
? GPG_ERR_LINE_TOO_LONG
150 : GPG_ERR_INCOMPLETE_LINE
);
151 log_error (_("file `%s', line %d: %s\n"),
152 fname
, lnr
, gpg_strerror (err
));
155 line
[strlen(line
)-1] = 0; /* Chop the LF. */
157 /* Allow for empty lines and spaces */
158 for (p
=line
; spacep (p
); p
++)
160 if (!*p
|| *p
== '#')
163 if (!strncmp (p
, "include-default", 15)
164 && (!p
[15] || spacep (p
+15)))
171 log_error (_("statement \"%s\" ignored in `%s', line %d\n"),
172 "include-default", fname
, lnr
);
175 /* fixme: Should check for trailing garbage. */
177 etcname
= make_filename (gnupg_sysconfdir (), "trustlist.txt", NULL
);
178 if ( !strcmp (etcname
, fname
) ) /* Same file. */
179 log_info (_("statement \"%s\" ignored in `%s', line %d\n"),
180 "include-default", fname
, lnr
);
181 else if ( access (etcname
, F_OK
) && errno
== ENOENT
)
183 /* A non existent system trustlist is not an error.
184 Just print a note. */
185 log_info (_("system trustlist `%s' not available\n"), etcname
);
189 err2
= read_one_trustfile (etcname
, 0,
190 &table
, &tablesize
, &tableidx
);
199 if (tableidx
== tablesize
) /* Need more space. */
204 tmplen
= tablesize
+ 20;
205 tmp
= xtryrealloc (table
, tmplen
* sizeof *table
);
208 err
= gpg_error_from_syserror ();
215 ti
= table
+ tableidx
;
217 memset (&ti
->flags
, 0, sizeof ti
->flags
);
220 ti
->flags
.disabled
= 1;
226 n
= hexcolon2bin (p
, ti
->fpr
, 20);
229 log_error (_("bad fingerprint in `%s', line %d\n"), fname
, lnr
);
230 err
= gpg_error (GPG_ERR_BAD_DATA
);
234 for (; spacep (p
); p
++)
237 /* Process the first flag which needs to be the first for
238 backward compatibility. */
239 if (!*p
|| *p
== '*' )
241 ti
->flags
.for_smime
= 1;
242 ti
->flags
.for_pgp
= 1;
244 else if ( *p
== 'P' || *p
== 'p')
246 ti
->flags
.for_pgp
= 1;
248 else if ( *p
== 'S' || *p
== 's')
250 ti
->flags
.for_smime
= 1;
254 log_error (_("invalid keyflag in `%s', line %d\n"), fname
, lnr
);
255 err
= gpg_error (GPG_ERR_BAD_DATA
);
259 if ( *p
&& !spacep (p
) )
261 log_error (_("invalid keyflag in `%s', line %d\n"), fname
, lnr
);
262 err
= gpg_error (GPG_ERR_BAD_DATA
);
266 /* Now check for more key-value pairs of the form NAME[=VALUE]. */
269 for (; spacep (p
); p
++)
273 n
= strcspn (p
, "= \t");
276 log_error ("assigning a value to a flag is not yet supported; "
277 "in `%s', line %d\n", fname
, lnr
);
278 err
= gpg_error (GPG_ERR_BAD_DATA
);
281 else if (n
== 5 && !memcmp (p
, "relax", 5))
283 else if (n
== 2 && !memcmp (p
, "cm", 2))
286 log_error ("flag `%.*s' in `%s', line %d ignored\n",
292 if ( !err
&& !feof (fp
) )
294 err
= gpg_error_from_syserror ();
295 log_error (_("error reading `%s', line %d: %s\n"),
296 fname
, lnr
, gpg_strerror (err
));
302 *addr_of_table
= table
;
303 *addr_of_tablesize
= tablesize
;
304 *addr_of_tableidx
= tableidx
;
309 /* Read the trust files and update the global table on success. */
311 read_trustfiles (void)
314 trustitem_t
*table
, *ti
;
318 int allow_include
= 1;
321 table
= xtrycalloc (tablesize
, sizeof *table
);
323 return gpg_error_from_syserror ();
326 fname
= make_filename (opt
.homedir
, "trustlist.txt", NULL
);
327 if ( access (fname
, F_OK
) )
329 if ( errno
== ENOENT
)
330 ; /* Silently ignore a non-existing trustfile. */
333 err
= gpg_error_from_syserror ();
334 log_error (_("error opening `%s': %s\n"), fname
, gpg_strerror (err
));
337 fname
= make_filename (gnupg_sysconfdir (), "trustlist.txt", NULL
);
340 err
= read_one_trustfile (fname
, allow_include
,
341 &table
, &tablesize
, &tableidx
);
347 if (gpg_err_code (err
) == GPG_ERR_ENOENT
)
349 /* Take a missing trustlist as an empty one. */
354 unlock_trusttable ();
360 /* Fixme: we should drop duplicates and sort the table. */
361 ti
= xtryrealloc (table
, (tableidx
?tableidx
:1) * sizeof *table
);
371 trusttablesize
= tableidx
;
372 unlock_trusttable ();
378 /* Check whether the given fpr is in our trustdb. We expect FPR to be
379 an all uppercase hexstring of 40 characters. */
381 agent_istrusted (ctrl_t ctrl
, const char *fpr
, int *r_disabled
)
386 unsigned char fprbin
[20];
391 if ( hexcolon2bin (fpr
, fprbin
, 20) < 0 )
392 return gpg_error (GPG_ERR_INV_VALUE
);
396 err
= read_trustfiles ();
399 log_error (_("error reading list of trusted root certificates\n"));
406 for (ti
=trusttable
, len
= trusttablesize
; len
; ti
++, len
--)
407 if (!memcmp (ti
->fpr
, fprbin
, 20))
409 if (ti
->flags
.disabled
&& r_disabled
)
414 err
= agent_write_status (ctrl
,
415 "TRUSTLISTFLAG", "relax",
420 else if (ti
->flags
.cm
)
422 err
= agent_write_status (ctrl
,
423 "TRUSTLISTFLAG", "cm",
428 return ti
->flags
.disabled
? gpg_error (GPG_ERR_NOT_TRUSTED
) : 0;
431 return gpg_error (GPG_ERR_NOT_TRUSTED
);
435 /* Write all trust entries to FP. */
437 agent_listtrusted (void *assuan_context
)
446 err
= read_trustfiles ();
449 log_error (_("error reading list of trusted root certificates\n"));
456 /* We need to lock the table because the scheduler may interrupt
457 assuan_send_data and an other thread may then re-read the table. */
459 for (ti
=trusttable
, len
= trusttablesize
; len
; ti
++, len
--)
461 if (ti
->flags
.disabled
)
463 bin2hex (ti
->fpr
, 20, key
);
465 key
[41] = ((ti
->flags
.for_smime
&& ti
->flags
.for_pgp
)? '*'
466 : ti
->flags
.for_smime
? 'S': ti
->flags
.for_pgp
? 'P':' ');
468 assuan_send_data (assuan_context
, key
, 43);
469 assuan_send_data (assuan_context
, NULL
, 0); /* flush */
471 unlock_trusttable ();
478 /* Create a copy of string with colons inserted after each two bytes.
479 Caller needs to release the string. In case of a memory failure,
482 insert_colons (const char *string
)
485 size_t n
= strlen (string
);
486 size_t nnew
= n
+ (n
+1)/2;
488 p
= buffer
= xtrymalloc ( nnew
+ 1 );
502 assert (strlen (buffer
) <= nnew
);
508 /* To pretty print DNs in the Pinentry, we replace slashes by
509 REPLSTRING. The caller needs to free the returned string. NULL is
510 returned on error with ERRNO set. */
512 reformat_name (const char *name
, const char *replstring
)
518 size_t replstringlen
= strlen (replstring
);
520 /* If the name does not start with a slash it is not a preformatted
521 DN and thus we don't bother to reformat it. */
523 return xtrystrdup (name
);
525 /* Count the names. Note that a slash contained in a DN part is
526 expected to be C style escaped and thus the slashes we see here
527 are the actual part delimiters. */
528 for (s
=name
+1, count
=0; *s
; s
++)
531 newname
= xtrymalloc (strlen (name
) + count
*replstringlen
+ 1);
534 for (s
=name
+1, d
=newname
; *s
; s
++)
536 d
= stpcpy (d
, replstring
);
544 /* Insert the given fpr into our trustdb. We expect FPR to be an all
545 uppercase hexstring of 40 characters. FLAG is either 'P' or 'C'.
546 This function does first check whether that key has already been put
547 into the trustdb and returns success in this case. Before a FPR
548 actually gets inserted, the user is asked by means of the Pinentry
549 whether this is actual want he wants to do. */
551 agent_marktrusted (ctrl_t ctrl
, const char *name
, const char *fpr
, int flag
)
562 /* Check whether we are at all allowed to modify the trustlist.
563 This is useful so that the trustlist may be a symlink to a global
564 trustlist with only admin priviliges to modify it. Of course
565 this is not a secure way of denying access, but it avoids the
566 usual clicking on an Okay button most users are used to. */
567 fname
= make_filename (opt
.homedir
, "trustlist.txt", NULL
);
568 if ( access (fname
, W_OK
) && errno
!= ENOENT
)
571 return gpg_error (GPG_ERR_EPERM
);
575 if (!agent_istrusted (ctrl
, fpr
, &is_disabled
))
577 return 0; /* We already got this fingerprint. Silently return
581 /* This feature must explicitly been enabled. */
582 if (!opt
.allow_mark_trusted
)
583 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
587 /* There is an disabled entry in the trustlist. Return an error
588 so that the user won't be asked again for that one. Changing
589 this flag with the integrated marktrusted feature is and will
590 not be made possible. */
591 return gpg_error (GPG_ERR_NOT_TRUSTED
);
595 /* Insert a new one. */
596 nameformatted
= reformat_name (name
, "%0A ");
598 return gpg_error_from_syserror ();
600 /* First a general question whether this is trusted. */
601 desc
= xtryasprintf (
602 /* TRANSLATORS: This prompt is shown by the Pinentry
603 and has one special property: A "%%0A" is used by
604 Pinentry to insert a line break. The double
605 percent sign is actually needed because it is also
606 a printf format string. If you need to insert a
607 plain % sign, you need to encode it as "%%25". The
608 "%s" gets replaced by the name as stored in the
610 _("Do you ultimately trust%%0A"
612 "to correctly certify user certificates?"),
616 xfree (nameformatted
);
617 return out_of_core ();
619 err
= agent_get_confirmation (ctrl
, desc
, _("Yes"), _("No"), 1);
623 else if (gpg_err_code (err
) == GPG_ERR_NOT_CONFIRMED
)
627 xfree (nameformatted
);
632 fprformatted
= insert_colons (fpr
);
635 xfree (nameformatted
);
636 return out_of_core ();
639 /* If the user trusts this certificate he has to verify the
640 fingerprint of course. */
645 /* TRANSLATORS: This prompt is shown by the Pinentry and has
646 one special property: A "%%0A" is used by Pinentry to
647 insert a line break. The double percent sign is actually
648 needed because it is also a printf format string. If you
649 need to insert a plain % sign, you need to encode it as
650 "%%25". The second "%s" gets replaced by a hexdecimal
651 fingerprint string whereas the first one receives the name
652 as stored in the certificate. */
653 _("Please verify that the certificate identified as:%%0A"
655 "has the fingerprint:%%0A"
656 " %s"), nameformatted
, fprformatted
);
659 xfree (fprformatted
);
660 xfree (nameformatted
);
661 return out_of_core ();
664 /* TRANSLATORS: "Correct" is the label of a button and intended
665 to be hit if the fingerprint matches the one of the CA. The
666 other button is "the default "Cancel" of the Pinentry. */
667 err
= agent_get_confirmation (ctrl
, desc
, _("Correct"), _("Wrong"), 1);
669 if (gpg_err_code (err
) == GPG_ERR_NOT_CONFIRMED
)
673 xfree (fprformatted
);
674 xfree (nameformatted
);
680 /* Now check again to avoid duplicates. We take the lock to make
681 sure that nobody else plays with our file and force a reread. */
683 agent_reload_trustlist ();
684 if (!agent_istrusted (ctrl
, fpr
, &is_disabled
) || is_disabled
)
686 unlock_trusttable ();
687 xfree (fprformatted
);
688 xfree (nameformatted
);
689 return is_disabled
? gpg_error (GPG_ERR_NOT_TRUSTED
) : 0;
692 fname
= make_filename (opt
.homedir
, "trustlist.txt", NULL
);
693 if ( access (fname
, F_OK
) && errno
== ENOENT
)
695 fp
= es_fopen (fname
, "wx");
698 err
= gpg_error_from_syserror ();
699 log_error ("can't create `%s': %s\n", fname
, gpg_strerror (err
));
701 unlock_trusttable ();
702 xfree (fprformatted
);
703 xfree (nameformatted
);
706 es_fputs (headerblurb
, fp
);
709 fp
= es_fopen (fname
, "a+");
712 err
= gpg_error_from_syserror ();
713 log_error ("can't open `%s': %s\n", fname
, gpg_strerror (err
));
715 unlock_trusttable ();
716 xfree (fprformatted
);
717 xfree (nameformatted
);
721 /* Append the key. */
722 es_fputs ("\n# ", fp
);
723 xfree (nameformatted
);
724 nameformatted
= reformat_name (name
, "\n# ");
725 if (!nameformatted
|| strchr (name
, '\n'))
727 /* Note that there should never be a LF in NAME but we better
728 play safe and print a sanitized version in this case. */
729 es_write_sanitized (fp
, name
, strlen (name
), NULL
, NULL
);
732 es_fputs (nameformatted
, fp
);
733 es_fprintf (fp
, "\n%s%s %c\n", yes_i_trust
?"":"!", fprformatted
, flag
);
735 err
= gpg_error_from_syserror ();
738 err
= gpg_error_from_syserror ();
740 agent_reload_trustlist ();
742 unlock_trusttable ();
743 xfree (fprformatted
);
744 xfree (nameformatted
);
749 /* This function may be called to force reloading of the
752 agent_reload_trustlist (void)
754 /* All we need to do is to delete the trusttable. At the next
755 access it will get re-read. */
760 unlock_trusttable ();
761 bump_key_eventcounter ();