Coverage Report

Created: 2026-01-17 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/trust.c
Line
Count
Source
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
0
{
43
#ifdef NO_TRUST_MODELS
44
  (void)pk;
45
  return 0;
46
#else
47
0
  return tdb_cache_disabled_value (ctrl, pk);
48
0
#endif
49
0
}
50
51
52
void
53
register_trusted_key (const char *string)
54
0
{
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 was ignored.  We detect the above use case
63
   * here and  cut off the junk-looking-like-a comment.  */
64
0
  if (strchr (string, '#'))
65
0
    {
66
0
      char *buf;
67
68
0
      buf = xtrystrdup (string);
69
0
      if (buf)
70
0
        {
71
0
          *strchr (buf, '#') = 0;
72
0
          tdb_register_trusted_key (buf);
73
0
          xfree (buf);
74
0
          return;
75
0
        }
76
0
    }
77
78
0
  tdb_register_trusted_key (string);
79
0
#endif
80
0
}
81
82
83

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
0
{
91
0
  switch( (value & TRUST_MASK) )
92
0
    {
93
0
    case TRUST_UNKNOWN:   return '-';
94
0
    case TRUST_EXPIRED:   return 'e';
95
0
    case TRUST_UNDEFINED: return 'q';
96
0
    case TRUST_NEVER:     return 'n';
97
0
    case TRUST_MARGINAL:  return 'm';
98
0
    case TRUST_FULLY:     return 'f';
99
0
    case TRUST_ULTIMATE:  return 'u';
100
0
    default:              return '?';
101
0
    }
102
0
}
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
0
{
110
0
  switch ((value & TRUST_MASK))
111
0
    {
112
0
    case TRUST_UNKNOWN:   return _("unknown");
113
0
    case TRUST_EXPIRED:   return _("expired");
114
0
    case TRUST_UNDEFINED: return _("undefined");
115
0
    case TRUST_NEVER:     return _("never");
116
0
    case TRUST_MARGINAL:  return _("marginal");
117
0
    case TRUST_FULLY:     return _("full");
118
0
    case TRUST_ULTIMATE:  return _("ultimate");
119
0
    default:              return "err";
120
0
    }
121
0
}
122
123
124
int
125
string_to_trust_value (const char *str)
126
0
{
127
0
  if (!ascii_strcasecmp (str, "undefined"))
128
0
    return TRUST_UNDEFINED;
129
0
  else if (!ascii_strcasecmp (str, "never"))
130
0
    return TRUST_NEVER;
131
0
  else if (!ascii_strcasecmp (str, "marginal"))
132
0
    return TRUST_MARGINAL;
133
0
  else if (!ascii_strcasecmp (str, "full"))
134
0
    return TRUST_FULLY;
135
0
  else if (!ascii_strcasecmp(str, "ultimate"))
136
0
    return TRUST_ULTIMATE;
137
0
  else
138
0
    return -1;
139
0
}
140
141
142
const char *
143
uid_trust_string_fixed (ctrl_t ctrl, PKT_public_key *key, PKT_user_id *uid)
144
0
{
145
0
  if (!key && !uid)
146
0
    {
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
0
      return _("10 translator see trust.c:uid_trust_string_fixed");
156
0
    }
157
0
  else if(uid->flags.revoked || (key && key->flags.revoked))
158
0
    return                         _("[ revoked]");
159
0
  else if(uid->flags.expired)
160
0
    return                         _("[ expired]");
161
0
  else if(key)
162
0
    {
163
0
      switch (get_validity (ctrl, NULL, key, uid, NULL, 0) & TRUST_MASK)
164
0
        {
165
0
        case TRUST_UNKNOWN:   return _("[ unknown]");
166
0
        case TRUST_EXPIRED:   return _("[ expired]");
167
0
        case TRUST_UNDEFINED: return _("[  undef ]");
168
0
        case TRUST_NEVER:     return _("[  never ]");
169
0
        case TRUST_MARGINAL:  return _("[marginal]");
170
0
        case TRUST_FULLY:     return _("[  full  ]");
171
0
        case TRUST_ULTIMATE:  return _("[ultimate]");
172
0
        }
173
0
    }
174
175
0
  return "err";
176
0
}
177
178
179

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
8
{
187
#ifdef NO_TRUST_MODELS
188
  (void)pk;
189
  return TRUST_UNKNOWN;
190
#else
191
8
  return tdb_get_ownertrust (ctrl, pk, 0);
192
8
#endif
193
8
}
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
0
{
204
#ifdef NO_TRUST_MODELS
205
  (void)pk;
206
  return TRUST_UNKNOWN;
207
#else
208
0
  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
0
  if (no_create && init_trustdb (ctrl, 1))
215
0
    return TRUST_UNKNOWN;
216
217
0
  otrust = (tdb_get_ownertrust (ctrl, pk, no_create) & TRUST_MASK);
218
0
  otrust_min = tdb_get_min_ownertrust (ctrl, pk, no_create);
219
0
  if (otrust < otrust_min)
220
0
    {
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
0
      otrust = otrust_min;
228
0
    }
229
230
0
  return otrust;
231
0
#endif
232
0
}
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
0
{
243
0
  return trust_letter (get_ownertrust_with_min (ctrl, pk, no_create));
244
0
}
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
0
{
255
0
  return trust_value_to_string (get_ownertrust_with_min (ctrl, pk, no_create));
256
0
}
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
0
{
266
#ifdef NO_TRUST_MODELS
267
  (void)pk;
268
  (void)new_trust;
269
#else
270
0
  u32 keyid[2];
271
272
0
  tdb_update_ownertrust (ctrl, pk, new_trust, 0);
273
0
  keyid_from_pk (pk, keyid);
274
0
  tdb_update_utk (keyid, (new_trust & TRUST_ULTIMATE));
275
0
#endif
276
0
}
277
278
279
int
280
clear_ownertrusts (ctrl_t ctrl, PKT_public_key *pk)
281
37
{
282
#ifdef NO_TRUST_MODELS
283
  (void)pk;
284
  return 0;
285
#else
286
37
  return tdb_clear_ownertrusts (ctrl, pk);
287
37
#endif
288
37
}
289
290
291
void
292
revalidation_mark (ctrl_t ctrl)
293
1.72k
{
294
1.72k
#ifndef NO_TRUST_MODELS
295
1.72k
  tdb_revalidation_mark (ctrl);
296
1.72k
#endif
297
1.72k
}
298
299
300
void
301
check_trustdb_stale (ctrl_t ctrl)
302
1
{
303
1
#ifndef NO_TRUST_MODELS
304
1
  tdb_check_trustdb_stale (ctrl);
305
#else
306
  (void)ctrl;
307
#endif
308
1
}
309
310
311
void
312
check_or_update_trustdb (ctrl_t ctrl)
313
7.32k
{
314
7.32k
#ifndef NO_TRUST_MODELS
315
7.32k
  tdb_check_or_update (ctrl);
316
#else
317
  (void)ctrl;
318
#endif
319
7.32k
}
320
321
322
/*
323
 * Return the validity information for KB/PK (at least one must be
324
 * non-NULL).  If the namehash is not NULL, the validity of the
325
 * corresponding user ID is returned, otherwise, a reasonable value
326
 * for the entire key is returned.
327
 */
328
unsigned int
329
get_validity (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk, PKT_user_id *uid,
330
              PKT_signature *sig, int may_ask)
331
0
{
332
0
  int rc;
333
0
  unsigned int validity;
334
0
  u32 kid[2];
335
0
  PKT_public_key *main_pk;
336
337
0
  if (kb && pk)
338
0
    log_assert (keyid_cmp (pk_main_keyid (pk),
339
0
                           pk_main_keyid (kb->pkt->pkt.public_key)) == 0);
340
341
0
  if (! pk)
342
0
    {
343
0
      log_assert (kb);
344
0
      pk = kb->pkt->pkt.public_key;
345
0
    }
346
347
0
  if (uid)
348
0
    namehash_from_uid (uid);
349
350
0
  keyid_from_pk (pk, kid);
351
0
  if (pk->main_keyid[0] != kid[0] || pk->main_keyid[1] != kid[1])
352
0
    {
353
      /* This is a subkey - get the mainkey. */
354
0
      if (kb)
355
0
        main_pk = kb->pkt->pkt.public_key;
356
0
      else
357
0
        {
358
0
          main_pk = xmalloc_clear (sizeof *main_pk);
359
0
          rc = get_pubkey (ctrl, main_pk, pk->main_keyid);
360
0
          if (rc)
361
0
            {
362
0
              char *tempkeystr = xstrdup (keystr (pk->main_keyid));
363
0
              log_error ("error getting main key %s of subkey %s: %s\n",
364
0
                         tempkeystr, keystr (kid), gpg_strerror (rc));
365
0
              xfree (tempkeystr);
366
0
              validity = TRUST_UNKNOWN;
367
0
              goto leave;
368
0
            }
369
0
        }
370
0
    }
371
0
  else
372
0
    main_pk = pk;
373
374
#ifdef NO_TRUST_MODELS
375
  validity = TRUST_UNKNOWN;
376
#else
377
0
  validity = tdb_get_validity_core (ctrl, kb, pk, uid, main_pk, sig, may_ask);
378
0
#endif
379
380
0
 leave:
381
  /* Set some flags direct from the key */
382
0
  if (main_pk->flags.revoked)
383
0
    validity |= TRUST_FLAG_REVOKED;
384
0
  if (main_pk != pk && pk->flags.revoked)
385
0
    validity |= TRUST_FLAG_SUB_REVOKED;
386
  /* Note: expiration is a trust value and not a flag - don't know why
387
   * I initially designed it that way.  */
388
0
  if (main_pk->has_expired || pk->has_expired)
389
0
    validity = ((validity & (~TRUST_MASK | TRUST_FLAG_PENDING_CHECK))
390
0
                | TRUST_EXPIRED);
391
392
0
  if (main_pk != pk && !kb)
393
0
    free_public_key (main_pk);
394
0
  return validity;
395
0
}
396
397
398
int
399
get_validity_info (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk,
400
                   PKT_user_id *uid)
401
0
{
402
0
  int trustlevel;
403
404
0
  if (kb && pk)
405
0
    log_assert (keyid_cmp (pk_main_keyid (pk),
406
0
                           pk_main_keyid (kb->pkt->pkt.public_key)) == 0);
407
408
0
  if (! pk && kb)
409
0
    pk = kb->pkt->pkt.public_key;
410
0
  if (!pk)
411
0
    return '?';  /* Just in case a NULL PK is passed.  */
412
413
0
  trustlevel = get_validity (ctrl, kb, pk, uid, NULL, 0);
414
0
  if ((trustlevel & TRUST_FLAG_REVOKED))
415
0
    return 'r';
416
0
  return trust_letter (trustlevel);
417
0
}
418
419
420
const char *
421
get_validity_string (ctrl_t ctrl, PKT_public_key *pk, PKT_user_id *uid)
422
0
{
423
0
  int trustlevel;
424
425
0
  if (!pk)
426
0
    return "err";  /* Just in case a NULL PK is passed.  */
427
428
0
  trustlevel = get_validity (ctrl, NULL, pk, uid, NULL, 0);
429
0
  if ((trustlevel & TRUST_FLAG_REVOKED))
430
0
    return _("revoked");
431
0
  return trust_value_to_string (trustlevel);
432
0
}