1 /* trustlist.c - Maintain the list of trusted keys
2 * Copyright (C) 2002, 2004, 2006, 2007 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 */
36 /* A structure to store the information from the trust file. */
41 int for_pgp
:1; /* Set by '*' or 'P' as first flag. */
42 int for_smime
:1; /* Set by '*' or 'S' as first flag. */
43 int relax
:1; /* Relax checking of root certificate
45 int cm
:1; /* Use chain model for validation. */
47 unsigned char fpr
[20]; /* The binary fingerprint. */
49 typedef struct trustitem_s trustitem_t
;
51 /* Malloced table and its allocated size with all trust items. */
52 static trustitem_t
*trusttable
;
53 static size_t trusttablesize
;
54 /* A mutex used to protect the table. */
55 static pth_mutex_t trusttable_lock
;
59 static const char headerblurb
[] =
60 "# This is the list of trusted keys. Comment lines, like this one, as\n"
61 "# well as empty lines are ignored. Lines have a length limit but this\n"
62 "# is not serious limitation as the format of the entries is fixed and\n"
63 "# checked by gpg-agent. A non-comment line starts with optional white\n"
64 "# space, followed by the SHA-1 fingerpint in hex, optionally followed\n"
65 "# by a flag character which my either be 'P', 'S' or '*'. You should\n"
66 "# give the gpg-agent a HUP after editing this file.\n"
68 "# Include the default trust list\n"
73 /* This function must be called once to initialize this module. This
74 has to be done before a second thread is spawned. We can't do the
75 static initialization because Pth emulation code might not be able
76 to do a static init; in particular, it is not possible for W32. */
78 initialize_module_trustlist (void)
80 static int initialized
;
84 if (!pth_mutex_init (&trusttable_lock
))
85 log_fatal ("error initializing mutex: %s\n", strerror (errno
));
94 lock_trusttable (void)
96 if (!pth_mutex_acquire (&trusttable_lock
, 0, NULL
))
97 log_fatal ("failed to acquire mutex in %s\n", __FILE__
);
101 unlock_trusttable (void)
103 if (!pth_mutex_release (&trusttable_lock
))
104 log_fatal ("failed to release mutex in %s\n", __FILE__
);
110 read_one_trustfile (const char *fname
, int allow_include
,
111 trustitem_t
**addr_of_table
,
112 size_t *addr_of_tablesize
,
113 int *addr_of_tableidx
)
119 trustitem_t
*table
, *ti
;
124 table
= *addr_of_table
;
125 tablesize
= *addr_of_tablesize
;
126 tableidx
= *addr_of_tableidx
;
128 fp
= fopen (fname
, "r");
131 err
= gpg_error_from_syserror ();
132 log_error (_("error opening `%s': %s\n"), fname
, gpg_strerror (err
));
136 while (fgets (line
, DIM(line
)-1, fp
))
140 if (!*line
|| line
[strlen(line
)-1] != '\n')
142 /* Eat until end of line. */
143 while ( (c
=getc (fp
)) != EOF
&& c
!= '\n')
145 err
= gpg_error (*line
? GPG_ERR_LINE_TOO_LONG
146 : GPG_ERR_INCOMPLETE_LINE
);
147 log_error (_("file `%s', line %d: %s\n"),
148 fname
, lnr
, gpg_strerror (err
));
151 line
[strlen(line
)-1] = 0; /* Chop the LF. */
153 /* Allow for empty lines and spaces */
154 for (p
=line
; spacep (p
); p
++)
156 if (!*p
|| *p
== '#')
159 if (!strncmp (p
, "include-default", 15)
160 && (!p
[15] || spacep (p
+15)))
167 log_error (_("statement \"%s\" ignored in `%s', line %d\n"),
168 "include-default", fname
, lnr
);
171 /* fixme: Should check for trailing garbage. */
173 etcname
= make_filename (gnupg_sysconfdir (), "trustlist.txt", NULL
);
174 if ( !strcmp (etcname
, fname
) ) /* Same file. */
175 log_info (_("statement \"%s\" ignored in `%s', line %d\n"),
176 "include-default", fname
, lnr
);
177 else if ( access (etcname
, F_OK
) && errno
== ENOENT
)
179 /* A non existent system trustlist is not an error.
180 Just print a note. */
181 log_info (_("system trustlist `%s' not available\n"), etcname
);
185 err2
= read_one_trustfile (etcname
, 0,
186 &table
, &tablesize
, &tableidx
);
195 if (tableidx
== tablesize
) /* Need more space. */
200 tmplen
= tablesize
+ 20;
201 tmp
= xtryrealloc (table
, tmplen
* sizeof *table
);
204 err
= gpg_error_from_syserror ();
211 ti
= table
+ tableidx
;
213 n
= hexcolon2bin (p
, ti
->fpr
, 20);
216 log_error (_("bad fingerprint in `%s', line %d\n"), fname
, lnr
);
217 err
= gpg_error (GPG_ERR_BAD_DATA
);
221 for (; spacep (p
); p
++)
224 memset (&ti
->flags
, 0, sizeof ti
->flags
);
225 /* Process the first flag which needs to be the first for
226 backward compatibility. */
227 if (!*p
|| *p
== '*' )
229 ti
->flags
.for_smime
= 1;
230 ti
->flags
.for_pgp
= 1;
232 else if ( *p
== 'P' || *p
== 'p')
234 ti
->flags
.for_pgp
= 1;
236 else if ( *p
== 'S' || *p
== 's')
238 ti
->flags
.for_smime
= 1;
242 log_error (_("invalid keyflag in `%s', line %d\n"), fname
, lnr
);
243 err
= gpg_error (GPG_ERR_BAD_DATA
);
247 if ( *p
&& !spacep (p
) )
249 log_error (_("invalid keyflag in `%s', line %d\n"), fname
, lnr
);
250 err
= gpg_error (GPG_ERR_BAD_DATA
);
254 /* Now check for more key-value pairs of the form NAME[=VALUE]. */
257 for (; spacep (p
); p
++)
261 n
= strcspn (p
, "= \t");
264 log_error ("assigning a value to a flag is not yet supported; "
265 "in `%s', line %d\n", fname
, lnr
);
266 err
= gpg_error (GPG_ERR_BAD_DATA
);
269 else if (n
== 5 && !memcmp (p
, "relax", 5))
271 else if (n
== 2 && !memcmp (p
, "cm", 2))
274 log_error ("flag `%.*s' in `%s', line %d ignored\n",
280 if ( !err
&& !feof (fp
) )
282 err
= gpg_error_from_syserror ();
283 log_error (_("error reading `%s', line %d: %s\n"),
284 fname
, lnr
, gpg_strerror (err
));
290 *addr_of_table
= table
;
291 *addr_of_tablesize
= tablesize
;
292 *addr_of_tableidx
= tableidx
;
297 /* Read the trust files and update the global table on success. */
299 read_trustfiles (void)
302 trustitem_t
*table
, *ti
;
306 int allow_include
= 1;
309 table
= xtrycalloc (tablesize
, sizeof *table
);
311 return gpg_error_from_syserror ();
314 fname
= make_filename (opt
.homedir
, "trustlist.txt", NULL
);
315 if ( access (fname
, F_OK
) )
317 if ( errno
== ENOENT
)
318 ; /* Silently ignore a non-existing trustfile. */
321 err
= gpg_error_from_syserror ();
322 log_error (_("error opening `%s': %s\n"), fname
, gpg_strerror (err
));
325 fname
= make_filename (gnupg_sysconfdir (), "trustlist.txt", NULL
);
328 err
= read_one_trustfile (fname
, allow_include
,
329 &table
, &tablesize
, &tableidx
);
335 if (gpg_err_code (err
) == GPG_ERR_ENOENT
)
337 /* Take a missing trustlist as an empty one. */
342 unlock_trusttable ();
348 /* Fixme: we should drop duplicates and sort the table. */
349 ti
= xtryrealloc (table
, (tableidx
?tableidx
:1) * sizeof *table
);
359 trusttablesize
= tableidx
;
360 unlock_trusttable ();
366 /* Check whether the given fpr is in our trustdb. We expect FPR to be
367 an all uppercase hexstring of 40 characters. */
369 agent_istrusted (ctrl_t ctrl
, const char *fpr
)
374 unsigned char fprbin
[20];
376 if ( hexcolon2bin (fpr
, fprbin
, 20) < 0 )
377 return gpg_error (GPG_ERR_INV_VALUE
);
381 err
= read_trustfiles ();
384 log_error (_("error reading list of trusted root certificates\n"));
391 for (ti
=trusttable
, len
= trusttablesize
; len
; ti
++, len
--)
392 if (!memcmp (ti
->fpr
, fprbin
, 20))
396 err
= agent_write_status (ctrl
,
397 "TRUSTLISTFLAG", "relax",
402 else if (ti
->flags
.cm
)
404 err
= agent_write_status (ctrl
,
405 "TRUSTLISTFLAG", "cm",
410 return 0; /* Trusted. */
413 return gpg_error (GPG_ERR_NOT_TRUSTED
);
417 /* Write all trust entries to FP. */
419 agent_listtrusted (void *assuan_context
)
428 err
= read_trustfiles ();
431 log_error (_("error reading list of trusted root certificates\n"));
438 /* We need to lock the table because the scheduler may interrupt
439 assuan_send_data and an other thread may then re-read the table. */
441 for (ti
=trusttable
, len
= trusttablesize
; len
; ti
++, len
--)
443 bin2hex (ti
->fpr
, 20, key
);
445 key
[41] = ((ti
->flags
.for_smime
&& ti
->flags
.for_pgp
)? '*'
446 : ti
->flags
.for_smime
? 'S': ti
->flags
.for_pgp
? 'P':' ');
448 assuan_send_data (assuan_context
, key
, 43);
449 assuan_send_data (assuan_context
, NULL
, 0); /* flush */
451 unlock_trusttable ();
458 /* Create a copy of string with colons inserted after each two bytes.
459 Caller needs to release the string. In case of a memory failure,
462 insert_colons (const char *string
)
465 size_t n
= strlen (string
);
466 size_t nnew
= n
+ (n
+1)/2;
468 p
= buffer
= xtrymalloc ( nnew
+ 1 );
482 assert (strlen (buffer
) <= nnew
);
488 /* Insert the given fpr into our trustdb. We expect FPR to be an all
489 uppercase hexstring of 40 characters. FLAG is either 'P' or 'C'.
490 This function does first check whether that key has already been put
491 into the trustdb and returns success in this case. Before a FPR
492 actually gets inserted, the user is asked by means of the Pinentry
493 whether this is actual wants he want to do. */
495 agent_marktrusted (ctrl_t ctrl
, const char *name
, const char *fpr
, int flag
)
504 /* Check whether we are at all allowed to modify the trustlist.
505 This is useful so that the trustlist may be a symlink to a global
506 trustlist with only admin priviliges to modify it. Of course
507 this is not a secure way of denying access, but it avoids the
508 usual clicking on an Okay button most users are used to. */
509 fname
= make_filename (opt
.homedir
, "trustlist.txt", NULL
);
510 if ( access (fname
, W_OK
) && errno
!= ENOENT
)
513 return gpg_error (GPG_ERR_EPERM
);
517 if (!agent_istrusted (ctrl
, fpr
))
519 return 0; /* We already got this fingerprint. Silently return
523 /* This feature must explicitly been enabled. */
524 if (!opt
.allow_mark_trusted
)
525 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
527 /* Insert a new one. */
528 fprformatted
= insert_colons (fpr
);
530 return out_of_core ();
531 desc
= xtryasprintf (
532 /* TRANSLATORS: This prompt is shown by the Pinentry
533 and has one special property: A "%%0A" is used by
534 Pinentry to insert a line break. The double
535 percent sign is actually needed because it is also
536 a printf format string. If you need to insert a
537 plain % sign, you need to encode it as "%%25". The
538 second "%s" gets replaced by a hexdecimal
539 fingerprint string whereas the first one receives
540 the name as stored in the certificate. */
541 _("Please verify that the certificate identified as:%%0A"
543 "has the fingerprint:%%0A"
544 " %s"), name
, fprformatted
);
547 xfree (fprformatted
);
548 return out_of_core ();
551 /* TRANSLATORS: "Correct" is the label of a button and intended to
552 be hit if the fingerprint matches the one of the CA. The other
553 button is "the default "Cancel" of the Pinentry. */
554 err
= agent_get_confirmation (ctrl
, desc
, _("Correct"), NULL
);
556 /* If the user did not confirmed this, we return cancel here so that
557 gpgsm may stop asking further questions. We won't do this for
558 the second question of course. */
561 xfree (fprformatted
);
562 return (gpg_err_code (err
) == GPG_ERR_NOT_CONFIRMED
?
563 gpg_err_make (gpg_err_source (err
), GPG_ERR_CANCELED
) : err
);
568 desc
= xtryasprintf (
569 /* TRANSLATORS: This prompt is shown by the Pinentry
570 and has one special property: A "%%0A" is used by
571 Pinentry to insert a line break. The double
572 percent sign is actually needed because it is also
573 a printf format string. If you need to insert a
574 plain % sign, you need to encode it as "%%25". The
575 "%s" gets replaced by the name as store in the
577 _("Do you ultimately trust%%0A"
579 "to correctly certify user certificates?"),
583 xfree (fprformatted
);
584 return out_of_core ();
587 err
= agent_get_confirmation (ctrl
, desc
, _("Yes"), _("No"));
591 xfree (fprformatted
);
595 /* Now check again to avoid duplicates. We take the lock to make
596 sure that nobody else plays with our file. Frankly we don't work
597 with the trusttable but using this lock is just fine for our
600 if (!agent_istrusted (ctrl
, fpr
))
602 unlock_trusttable ();
603 xfree (fprformatted
);
608 fname
= make_filename (opt
.homedir
, "trustlist.txt", NULL
);
609 if ( access (fname
, F_OK
) && errno
== ENOENT
)
611 fp
= fopen (fname
, "wx"); /* Warning: "x" is a GNU extension. */
614 err
= gpg_error_from_syserror ();
615 log_error ("can't create `%s': %s\n", fname
, gpg_strerror (err
));
617 unlock_trusttable ();
618 xfree (fprformatted
);
621 fputs (headerblurb
, fp
);
624 fp
= fopen (fname
, "a+");
627 err
= gpg_error_from_syserror ();
628 log_error ("can't open `%s': %s\n", fname
, gpg_strerror (err
));
630 unlock_trusttable ();
631 xfree (fprformatted
);
635 /* Append the key. */
637 print_sanitized_string (fp
, name
, 0);
638 fprintf (fp
, "\n%s %c\n", fprformatted
, flag
);
640 err
= gpg_error_from_syserror ();
643 err
= gpg_error_from_syserror ();
646 agent_reload_trustlist ();
648 unlock_trusttable ();
649 xfree (fprformatted
);
654 /* This function may be called to force reloading of the
657 agent_reload_trustlist (void)
659 /* All we need to do is to delete the trusttable. At the next
660 access it will get re-read. */
665 unlock_trusttable ();
666 bump_key_eventcounter ();