Imported Upstream version 2.3.4
[platform/upstream/gpg2.git] / g10 / trust.c
1 /* trust.c - High level trust functions
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3  *               2008, 2012 Free Software Foundation, Inc.
4  * Copyright (C) 2014 Werner Koch
5  *
6  * This file is part of GnuPG.
7  *
8  * GnuPG is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuPG is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <https://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "gpg.h"
28 #include "keydb.h"
29 #include "../common/util.h"
30 #include "options.h"
31 #include "packet.h"
32 #include "main.h"
33 #include "../common/i18n.h"
34 #include "trustdb.h"
35 #include "../common/host2net.h"
36
37
38 /* Return true if key is disabled.  Note that this is usually used via
39    the pk_is_disabled macro.  */
40 int
41 cache_disabled_value (ctrl_t ctrl, PKT_public_key *pk)
42 {
43 #ifdef NO_TRUST_MODELS
44   (void)pk;
45   return 0;
46 #else
47   return tdb_cache_disabled_value (ctrl, pk);
48 #endif
49 }
50
51
52 void
53 register_trusted_key (const char *string)
54 {
55 #ifdef NO_TRUST_MODELS
56   (void)string;
57 #else
58
59   /* Some users have conf files with entries like
60    *   trusted-key 0x1234567812345678    # foo
61    * That is obviously wrong.  Before fixing bug#1206 trailing garbage
62    * on a key specification if was ignored.  We detect the above use case
63    * here and  cut off the junk-looking-like-a comment.  */
64   if (strchr (string, '#'))
65     {
66       char *buf;
67
68       buf = xtrystrdup (string);
69       if (buf)
70         {
71           *strchr (buf, '#') = 0;
72           tdb_register_trusted_key (buf);
73           xfree (buf);
74           return;
75         }
76     }
77
78   tdb_register_trusted_key (string);
79 #endif
80 }
81
82
83 \f
84 /*
85  * This function returns a letter for a trust value.  Trust flags
86  * are ignored.
87  */
88 static int
89 trust_letter (unsigned int value)
90 {
91   switch( (value & TRUST_MASK) )
92     {
93     case TRUST_UNKNOWN:   return '-';
94     case TRUST_EXPIRED:   return 'e';
95     case TRUST_UNDEFINED: return 'q';
96     case TRUST_NEVER:     return 'n';
97     case TRUST_MARGINAL:  return 'm';
98     case TRUST_FULLY:     return 'f';
99     case TRUST_ULTIMATE:  return 'u';
100     default:              return '?';
101     }
102 }
103
104
105 /* The strings here are similar to those in
106    pkclist.c:do_edit_ownertrust() */
107 const char *
108 trust_value_to_string (unsigned int value)
109 {
110   switch ((value & TRUST_MASK))
111     {
112     case TRUST_UNKNOWN:   return _("unknown");
113     case TRUST_EXPIRED:   return _("expired");
114     case TRUST_UNDEFINED: return _("undefined");
115     case TRUST_NEVER:     return _("never");
116     case TRUST_MARGINAL:  return _("marginal");
117     case TRUST_FULLY:     return _("full");
118     case TRUST_ULTIMATE:  return _("ultimate");
119     default:              return "err";
120     }
121 }
122
123
124 int
125 string_to_trust_value (const char *str)
126 {
127   if (!ascii_strcasecmp (str, "undefined"))
128     return TRUST_UNDEFINED;
129   else if (!ascii_strcasecmp (str, "never"))
130     return TRUST_NEVER;
131   else if (!ascii_strcasecmp (str, "marginal"))
132     return TRUST_MARGINAL;
133   else if (!ascii_strcasecmp (str, "full"))
134     return TRUST_FULLY;
135   else if (!ascii_strcasecmp(str, "ultimate"))
136     return TRUST_ULTIMATE;
137   else
138     return -1;
139 }
140
141
142 const char *
143 uid_trust_string_fixed (ctrl_t ctrl, PKT_public_key *key, PKT_user_id *uid)
144 {
145   if (!key && !uid)
146     {
147       /* TRANSLATORS: these strings are similar to those in
148          trust_value_to_string(), but are a fixed length.  This is needed to
149          make attractive information listings where columns line up
150          properly.  The value "10" should be the length of the strings you
151          choose to translate to.  This is the length in printable columns.
152          It gets passed to atoi() so everything after the number is
153          essentially a comment and need not be translated.  Either key and
154          uid are both NULL, or neither are NULL. */
155       return _("10 translator see trust.c:uid_trust_string_fixed");
156     }
157   else if(uid->flags.revoked || (key && key->flags.revoked))
158     return                         _("[ revoked]");
159   else if(uid->flags.expired)
160     return                         _("[ expired]");
161   else if(key)
162     {
163       switch (get_validity (ctrl, NULL, key, uid, NULL, 0) & TRUST_MASK)
164         {
165         case TRUST_UNKNOWN:   return _("[ unknown]");
166         case TRUST_EXPIRED:   return _("[ expired]");
167         case TRUST_UNDEFINED: return _("[  undef ]");
168         case TRUST_NEVER:     return _("[  never ]");
169         case TRUST_MARGINAL:  return _("[marginal]");
170         case TRUST_FULLY:     return _("[  full  ]");
171         case TRUST_ULTIMATE:  return _("[ultimate]");
172         }
173     }
174
175   return "err";
176 }
177
178
179 \f
180 /*
181  * Return the assigned ownertrust value for the given public key.
182  * The key should be the primary key.
183  */
184 unsigned int
185 get_ownertrust (ctrl_t ctrl, PKT_public_key *pk)
186 {
187 #ifdef NO_TRUST_MODELS
188   (void)pk;
189   return TRUST_UNKNOWN;
190 #else
191   return tdb_get_ownertrust (ctrl, pk, 0);
192 #endif
193 }
194
195
196 /*
197  * Same as get_ownertrust but this takes the minimum ownertrust value
198  * into account, and will bump up the value as needed.  NO_CREATE
199  * inhibits creation of a trustdb it that does not yet exists.
200  */
201 static int
202 get_ownertrust_with_min (ctrl_t ctrl, PKT_public_key *pk, int no_create)
203 {
204 #ifdef NO_TRUST_MODELS
205   (void)pk;
206   return TRUST_UNKNOWN;
207 #else
208   unsigned int otrust, otrust_min;
209
210   /* Shortcut instead of doing the same twice in the two tdb_get
211    * functions: If the caller asked not to create a trustdb we call
212    * init_trustdb directly and allow it to fail with an error code for
213    * a non-existing trustdb.  */
214   if (no_create && init_trustdb (ctrl, 1))
215     return TRUST_UNKNOWN;
216
217   otrust = (tdb_get_ownertrust (ctrl, pk, no_create) & TRUST_MASK);
218   otrust_min = tdb_get_min_ownertrust (ctrl, pk, no_create);
219   if (otrust < otrust_min)
220     {
221       /* If the trust that the user has set is less than the trust
222          that was calculated from a trust signature chain, use the
223          higher of the two.  We do this here and not in
224          get_ownertrust since the underlying ownertrust should not
225          really be set - just the appearance of the ownertrust. */
226
227       otrust = otrust_min;
228     }
229
230   return otrust;
231 #endif
232 }
233
234
235 /*
236  * Same as get_ownertrust but return a trust letter instead of an
237  * value.  This takes the minimum ownertrust value into account.  If
238  * NO_CREATE is set, no efforts for creating a trustdb will be taken.
239  */
240 int
241 get_ownertrust_info (ctrl_t ctrl, PKT_public_key *pk, int no_create)
242 {
243   return trust_letter (get_ownertrust_with_min (ctrl, pk, no_create));
244 }
245
246
247 /*
248  * Same as get_ownertrust but return a trust string instead of an
249  * value.  This takes the minimum ownertrust value into account.  If
250  * NO_CREATE is set, no efforts for creating a trustdb will be taken.
251  */
252 const char *
253 get_ownertrust_string (ctrl_t ctrl, PKT_public_key *pk, int no_create)
254 {
255   return trust_value_to_string (get_ownertrust_with_min (ctrl, pk, no_create));
256 }
257
258
259 /*
260  * Set the trust value of the given public key to the new value.
261  * The key should be a primary one.
262  */
263 void
264 update_ownertrust (ctrl_t ctrl, PKT_public_key *pk, unsigned int new_trust)
265 {
266 #ifdef NO_TRUST_MODELS
267   (void)pk;
268   (void)new_trust;
269 #else
270   tdb_update_ownertrust (ctrl, pk, new_trust, 0);
271 #endif
272 }
273
274
275 int
276 clear_ownertrusts (ctrl_t ctrl, PKT_public_key *pk)
277 {
278 #ifdef NO_TRUST_MODELS
279   (void)pk;
280   return 0;
281 #else
282   return tdb_clear_ownertrusts (ctrl, pk);
283 #endif
284 }
285
286
287 void
288 revalidation_mark (ctrl_t ctrl)
289 {
290 #ifndef NO_TRUST_MODELS
291   tdb_revalidation_mark (ctrl);
292 #endif
293 }
294
295
296 void
297 check_trustdb_stale (ctrl_t ctrl)
298 {
299 #ifndef NO_TRUST_MODELS
300   tdb_check_trustdb_stale (ctrl);
301 #else
302   (void)ctrl;
303 #endif
304 }
305
306
307 void
308 check_or_update_trustdb (ctrl_t ctrl)
309 {
310 #ifndef NO_TRUST_MODELS
311   tdb_check_or_update (ctrl);
312 #else
313   (void)ctrl;
314 #endif
315 }
316
317
318 /*
319  * Return the validity information for KB/PK (at least one must be
320  * non-NULL).  If the namehash is not NULL, the validity of the
321  * corresponding user ID is returned, otherwise, a reasonable value
322  * for the entire key is returned.
323  */
324 unsigned int
325 get_validity (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk, PKT_user_id *uid,
326               PKT_signature *sig, int may_ask)
327 {
328   int rc;
329   unsigned int validity;
330   u32 kid[2];
331   PKT_public_key *main_pk;
332
333   if (kb && pk)
334     log_assert (keyid_cmp (pk_main_keyid (pk),
335                            pk_main_keyid (kb->pkt->pkt.public_key)) == 0);
336
337   if (! pk)
338     {
339       log_assert (kb);
340       pk = kb->pkt->pkt.public_key;
341     }
342
343   if (uid)
344     namehash_from_uid (uid);
345
346   keyid_from_pk (pk, kid);
347   if (pk->main_keyid[0] != kid[0] || pk->main_keyid[1] != kid[1])
348     {
349       /* This is a subkey - get the mainkey. */
350       if (kb)
351         main_pk = kb->pkt->pkt.public_key;
352       else
353         {
354           main_pk = xmalloc_clear (sizeof *main_pk);
355           rc = get_pubkey (ctrl, main_pk, pk->main_keyid);
356           if (rc)
357             {
358               char *tempkeystr = xstrdup (keystr (pk->main_keyid));
359               log_error ("error getting main key %s of subkey %s: %s\n",
360                          tempkeystr, keystr (kid), gpg_strerror (rc));
361               xfree (tempkeystr);
362               validity = TRUST_UNKNOWN;
363               goto leave;
364             }
365         }
366     }
367   else
368     main_pk = pk;
369
370 #ifdef NO_TRUST_MODELS
371   validity = TRUST_UNKNOWN;
372 #else
373   validity = tdb_get_validity_core (ctrl, kb, pk, uid, main_pk, sig, may_ask);
374 #endif
375
376  leave:
377   /* Set some flags direct from the key */
378   if (main_pk->flags.revoked)
379     validity |= TRUST_FLAG_REVOKED;
380   if (main_pk != pk && pk->flags.revoked)
381     validity |= TRUST_FLAG_SUB_REVOKED;
382   /* Note: expiration is a trust value and not a flag - don't know why
383    * I initially designed it that way.  */
384   if (main_pk->has_expired || pk->has_expired)
385     validity = ((validity & (~TRUST_MASK | TRUST_FLAG_PENDING_CHECK))
386                 | TRUST_EXPIRED);
387
388   if (main_pk != pk && !kb)
389     free_public_key (main_pk);
390   return validity;
391 }
392
393
394 int
395 get_validity_info (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk,
396                    PKT_user_id *uid)
397 {
398   int trustlevel;
399
400   if (kb && pk)
401     log_assert (keyid_cmp (pk_main_keyid (pk),
402                            pk_main_keyid (kb->pkt->pkt.public_key)) == 0);
403
404   if (! pk && kb)
405     pk = kb->pkt->pkt.public_key;
406   if (!pk)
407     return '?';  /* Just in case a NULL PK is passed.  */
408
409   trustlevel = get_validity (ctrl, kb, pk, uid, NULL, 0);
410   if ((trustlevel & TRUST_FLAG_REVOKED))
411     return 'r';
412   return trust_letter (trustlevel);
413 }
414
415
416 const char *
417 get_validity_string (ctrl_t ctrl, PKT_public_key *pk, PKT_user_id *uid)
418 {
419   int trustlevel;
420
421   if (!pk)
422     return "err";  /* Just in case a NULL PK is passed.  */
423
424   trustlevel = get_validity (ctrl, NULL, pk, uid, NULL, 0);
425   if ((trustlevel & TRUST_FLAG_REVOKED))
426     return _("revoked");
427   return trust_value_to_string (trustlevel);
428 }