Imported Upstream version 2.2.14
[platform/upstream/gpg2.git] / g10 / keydb.h
1 /* keydb.h - Key database
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3  *               2006, 2010 Free Software Foundation, Inc.
4  * Copyright (C) 2015, 2016 g10 Code GmbH
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 #ifndef G10_KEYDB_H
23 #define G10_KEYDB_H
24
25 #include "../common/types.h"
26 #include "../common/util.h"
27 #include "packet.h"
28
29 /* What qualifies as a certification (key-signature in contrast to a
30  * data signature)?  Note that a back signature is special and can be
31  * made by key and data signatures capable subkeys.) */
32 #define IS_CERT(s)       (IS_KEY_SIG(s) || IS_UID_SIG(s) || IS_SUBKEY_SIG(s) \
33                          || IS_KEY_REV(s) || IS_UID_REV(s) || IS_SUBKEY_REV(s))
34 #define IS_SIG(s)        (!IS_CERT(s))
35 #define IS_KEY_SIG(s)    ((s)->sig_class == 0x1f)
36 #define IS_UID_SIG(s)    (((s)->sig_class & ~3) == 0x10)
37 #define IS_SUBKEY_SIG(s) ((s)->sig_class == 0x18)
38 #define IS_BACK_SIG(s)   ((s)->sig_class == 0x19)
39 #define IS_KEY_REV(s)    ((s)->sig_class == 0x20)
40 #define IS_UID_REV(s)    ((s)->sig_class == 0x30)
41 #define IS_SUBKEY_REV(s) ((s)->sig_class == 0x28)
42
43 struct getkey_ctx_s;
44 typedef struct getkey_ctx_s *GETKEY_CTX;
45 typedef struct getkey_ctx_s *getkey_ctx_t;
46
47 /****************
48  * A Keyblock is all packets which form an entire certificate;
49  * i.e. the public key, certificate, trust packets, user ids,
50  * signatures, and subkey.
51  *
52  * This structure is also used to bind arbitrary packets together.
53  */
54
55 struct kbnode_struct
56 {
57   kbnode_t next;
58   PACKET *pkt;
59   int flag;          /* Local use during keyblock processing (not cloned).*/
60   unsigned int tag;  /* Ditto. */
61   int private_flag;
62 };
63
64 #define is_deleted_kbnode(a)  ((a)->private_flag & 1)
65 #define is_cloned_kbnode(a)   ((a)->private_flag & 2)
66
67
68 /*
69  * A structure to store key identification as well as some stuff
70  * needed for key validation.
71  */
72 struct key_item {
73   struct key_item *next;
74   unsigned int ownertrust,min_ownertrust;
75   byte trust_depth;
76   byte trust_value;
77   char *trust_regexp;
78   u32 kid[2];
79 };
80
81
82 /* Bit flags used with build_pk_list.  */
83 enum
84   {
85     PK_LIST_ENCRYPT_TO = 1, /* This is an encrypt-to recipient.    */
86     PK_LIST_HIDDEN     = 2, /* This is a hidden recipient.         */
87     PK_LIST_CONFIG     = 4, /* Specified via config file.          */
88     PK_LIST_FROM_FILE  = 8  /* Take key from file with that name.  */
89   };
90
91 /* To store private data in the flags the private data must be left
92  * shifted by this value.  */
93 enum
94   {
95     PK_LIST_SHIFT = 4
96   };
97
98
99 /* Structure to hold a couple of public key certificates. */
100 typedef struct pk_list *PK_LIST;  /* Deprecated. */
101 typedef struct pk_list *pk_list_t;
102 struct pk_list
103 {
104   PK_LIST next;
105   PKT_public_key *pk;
106   int flags;           /* See PK_LIST_ constants. */
107 };
108
109 /* Structure to hold a list of secret key certificates.  */
110 typedef struct sk_list *SK_LIST;
111 struct sk_list
112 {
113   SK_LIST next;
114   PKT_public_key *pk;
115   int mark; /* not used */
116 };
117
118 /* structure to collect all information which can be used to
119  * identify a public key */
120 typedef struct pubkey_find_info *PUBKEY_FIND_INFO;
121 struct pubkey_find_info {
122     u32  keyid[2];
123     unsigned nbits;
124     byte pubkey_algo;
125     byte fingerprint[MAX_FINGERPRINT_LEN];
126     char userid[1];
127 };
128
129
130 /* Helper type for preference functions. */
131 union pref_hint
132 {
133   int digest_length;
134 };
135
136
137 /* Constants to describe from where a key was fetched or updated.  */
138 enum
139   {
140     KEYORG_UNKNOWN = 0,
141     KEYORG_KS      = 1, /* Public keyserver.    */
142     KEYORG_KS_PREF = 2, /* Preferred keysrver.  */
143     KEYORG_DANE    = 3, /* OpenPGP DANE.        */
144     KEYORG_WKD     = 4, /* Web Key Directory.   */
145     KEYORG_URL     = 5, /* Trusted URL.         */
146     KEYORG_FILE    = 6, /* Trusted file.        */
147     KEYORG_SELF    = 7  /* We generated it.     */
148   };
149
150
151 /*
152  * Check whether the signature SIG is in the klist K.
153  */
154 static inline struct key_item *
155 is_in_klist (struct key_item *k, PKT_signature *sig)
156 {
157   for (; k; k = k->next)
158     {
159       if (k->kid[0] == sig->keyid[0] && k->kid[1] == sig->keyid[1])
160         return k;
161     }
162   return NULL;
163 }
164
165
166
167 /*-- keydb.c --*/
168
169 #define KEYDB_RESOURCE_FLAG_PRIMARY  2  /* The primary resource.  */
170 #define KEYDB_RESOURCE_FLAG_DEFAULT  4  /* The default one.  */
171 #define KEYDB_RESOURCE_FLAG_READONLY 8  /* Open in read only mode.  */
172 #define KEYDB_RESOURCE_FLAG_GPGVDEF 16  /* Default file for gpgv.  */
173
174 /* Format a search term for debugging output.  The caller must free
175    the result.  */
176 char *keydb_search_desc_dump (struct keydb_search_desc *desc);
177
178 /* Register a resource (keyring or keybox).  */
179 gpg_error_t keydb_add_resource (const char *url, unsigned int flags);
180
181 /* Dump some statistics to the log.  */
182 void keydb_dump_stats (void);
183
184 /* Create a new database handle.  Returns NULL on error, sets ERRNO,
185    and prints an error diagnostic. */
186 KEYDB_HANDLE keydb_new (void);
187
188 /* Free all resources owned by the database handle.  */
189 void keydb_release (KEYDB_HANDLE hd);
190
191 /* Take a lock on the files immediately and not only during insert or
192  * update.  This lock is released with keydb_release.  */
193 gpg_error_t keydb_lock (KEYDB_HANDLE hd);
194
195 /* Set a flag on the handle to suppress use of cached results.  This
196    is required for updating a keyring and for key listings.  Fixme:
197    Using a new parameter for keydb_new might be a better solution.  */
198 void keydb_disable_caching (KEYDB_HANDLE hd);
199
200 /* Save the last found state and invalidate the current selection.  */
201 void keydb_push_found_state (KEYDB_HANDLE hd);
202
203 /* Restore the previous save state.  */
204 void keydb_pop_found_state (KEYDB_HANDLE hd);
205
206 /* Return the file name of the resource.  */
207 const char *keydb_get_resource_name (KEYDB_HANDLE hd);
208
209 /* Return the keyblock last found by keydb_search.  */
210 gpg_error_t keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb);
211
212 /* Update the keyblock KB.  */
213 gpg_error_t keydb_update_keyblock (ctrl_t ctrl, KEYDB_HANDLE hd, kbnode_t kb);
214
215 /* Insert a keyblock into one of the underlying keyrings or keyboxes.  */
216 gpg_error_t keydb_insert_keyblock (KEYDB_HANDLE hd, kbnode_t kb);
217
218 /* Delete the currently selected keyblock.  */
219 gpg_error_t keydb_delete_keyblock (KEYDB_HANDLE hd);
220
221 /* Find the first writable resource.  */
222 gpg_error_t keydb_locate_writable (KEYDB_HANDLE hd);
223
224 /* Rebuild the on-disk caches of all key resources.  */
225 void keydb_rebuild_caches (ctrl_t ctrl, int noisy);
226
227 /* Return the number of skipped blocks (because they were to large to
228    read from a keybox) since the last search reset.  */
229 unsigned long keydb_get_skipped_counter (KEYDB_HANDLE hd);
230
231 /* Clears the current search result and resets the handle's position.  */
232 gpg_error_t keydb_search_reset (KEYDB_HANDLE hd);
233
234 /* Search the database for keys matching the search description.  */
235 gpg_error_t keydb_search (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc,
236                           size_t ndesc, size_t *descindex);
237
238 /* Return the first non-legacy key in the database.  */
239 gpg_error_t keydb_search_first (KEYDB_HANDLE hd);
240
241 /* Return the next key (not the next matching key!).  */
242 gpg_error_t keydb_search_next (KEYDB_HANDLE hd);
243
244 /* This is a convenience function for searching for keys with a long
245    key id.  */
246 gpg_error_t keydb_search_kid (KEYDB_HANDLE hd, u32 *kid);
247
248 /* This is a convenience function for searching for keys with a long
249    (20 byte) fingerprint.  */
250 gpg_error_t keydb_search_fpr (KEYDB_HANDLE hd, const byte *fpr);
251
252
253 /*-- pkclist.c --*/
254 void show_revocation_reason (ctrl_t ctrl, PKT_public_key *pk, int mode );
255 int  check_signatures_trust (ctrl_t ctrl, PKT_signature *sig);
256
257 void release_pk_list (PK_LIST pk_list);
258 int  build_pk_list (ctrl_t ctrl, strlist_t rcpts, PK_LIST *ret_pk_list);
259 gpg_error_t find_and_check_key (ctrl_t ctrl,
260                                 const char *name, unsigned int use,
261                                 int mark_hidden, int from_file,
262                                 pk_list_t *pk_list_addr);
263
264 int  algo_available( preftype_t preftype, int algo,
265                      const union pref_hint *hint );
266 int  select_algo_from_prefs( PK_LIST pk_list, int preftype,
267                              int request, const union pref_hint *hint);
268 int  select_mdc_from_pklist (PK_LIST pk_list);
269 void warn_missing_mdc_from_pklist (PK_LIST pk_list);
270 void warn_missing_aes_from_pklist (PK_LIST pk_list);
271
272 /*-- skclist.c --*/
273 int  random_is_faked (void);
274 void release_sk_list( SK_LIST sk_list );
275 gpg_error_t build_sk_list (ctrl_t ctrl, strlist_t locusr,
276                            SK_LIST *ret_sk_list, unsigned use);
277
278 /*-- passphrase.h --*/
279 unsigned char encode_s2k_iterations (int iterations);
280 int  have_static_passphrase(void);
281 const char *get_static_passphrase (void);
282 void set_passphrase_from_string(const char *pass);
283 void read_passphrase_from_fd( int fd );
284 void passphrase_clear_cache (const char *cacheid);
285 DEK *passphrase_to_dek_ext(u32 *keyid, int pubkey_algo,
286                            int cipher_algo, STRING2KEY *s2k, int mode,
287                            const char *tryagain_text,
288                            const char *custdesc, const char *custprompt,
289                            int *canceled);
290 DEK *passphrase_to_dek (int cipher_algo, STRING2KEY *s2k,
291                         int create, int nocache,
292                         const char *tryagain_text, int *canceled);
293 void set_next_passphrase( const char *s );
294 char *get_last_passphrase(void);
295 void next_to_last_passphrase(void);
296
297 void emit_status_need_passphrase (ctrl_t ctrl, u32 *keyid,
298                                   u32 *mainkeyid, int pubkey_algo);
299
300 #define FORMAT_KEYDESC_NORMAL  0
301 #define FORMAT_KEYDESC_IMPORT  1
302 #define FORMAT_KEYDESC_EXPORT  2
303 #define FORMAT_KEYDESC_DELKEY  3
304 char *gpg_format_keydesc (ctrl_t ctrl,
305                           PKT_public_key *pk, int mode, int escaped);
306
307
308 /*-- getkey.c --*/
309
310 /* Cache a copy of a public key in the public key cache.  */
311 void cache_public_key( PKT_public_key *pk );
312
313 /* Disable and drop the public key cache.  */
314 void getkey_disable_caches(void);
315
316 /* Return the public key used for signature SIG and store it at PK.  */
317 gpg_error_t get_pubkey_for_sig (ctrl_t ctrl,
318                                 PKT_public_key *pk, PKT_signature *sig);
319
320 /* Return the public key with the key id KEYID and store it at PK.  */
321 int get_pubkey (ctrl_t ctrl, PKT_public_key *pk, u32 *keyid);
322
323 /* Similar to get_pubkey, but it does not take PK->REQ_USAGE into
324    account nor does it merge in the self-signed data.  This function
325    also only considers primary keys.  */
326 int get_pubkey_fast (PKT_public_key *pk, u32 *keyid);
327
328 /* Return the entire keyblock used to create SIG.  This is a
329  * specialized version of get_pubkeyblock.  */
330 kbnode_t get_pubkeyblock_for_sig (ctrl_t ctrl, PKT_signature *sig);
331
332 /* Return the key block for the key with KEYID.  */
333 kbnode_t get_pubkeyblock (ctrl_t ctrl, u32 *keyid);
334
335 /* A list used by get_pubkeys to gather all of the matches.  */
336 struct pubkey_s
337 {
338   struct pubkey_s *next;
339   /* The key to use (either the public key or the subkey).  */
340   PKT_public_key *pk;
341   kbnode_t keyblock;
342 };
343 typedef struct pubkey_s *pubkey_t;
344
345 /* Free a list of public keys.  */
346 void pubkeys_free (pubkey_t keys);
347
348 /* Find a public key identified by NAME.  */
349 int get_pubkey_byname (ctrl_t ctrl,
350                        GETKEY_CTX *retctx, PKT_public_key *pk,
351                        const char *name,
352                        KBNODE *ret_keyblock, KEYDB_HANDLE *ret_kdbhd,
353                        int include_unusable, int no_akl );
354
355 /* Likewise, but only return the best match if NAME resembles a mail
356  * address.  */
357 gpg_error_t get_best_pubkey_byname (ctrl_t ctrl,
358                                     GETKEY_CTX *retctx, PKT_public_key *pk,
359                                     const char *name, KBNODE *ret_keyblock,
360                                     int include_unusable);
361
362 /* Get a public key directly from file FNAME.  */
363 gpg_error_t get_pubkey_fromfile (ctrl_t ctrl,
364                                  PKT_public_key *pk, const char *fname);
365
366 /* Return the public key with the key id KEYID iff the secret key is
367  * available and store it at PK.  */
368 gpg_error_t get_seckey (ctrl_t ctrl, PKT_public_key *pk, u32 *keyid);
369
370 /* Lookup a key with the specified fingerprint.  */
371 int get_pubkey_byfprint (ctrl_t ctrl, PKT_public_key *pk, kbnode_t *r_keyblock,
372                          const byte *fprint, size_t fprint_len);
373
374 /* This function is similar to get_pubkey_byfprint, but it doesn't
375    merge the self-signed data into the public key and subkeys or into
376    the user ids.  */
377 gpg_error_t get_pubkey_byfprint_fast (PKT_public_key *pk,
378                                       const byte *fprint, size_t fprint_len);
379
380 /* This function is similar to get_pubkey_byfprint, but it doesn't
381    merge the self-signed data into the public key and subkeys or into
382    the user ids.  */
383 gpg_error_t get_keyblock_byfprint_fast (kbnode_t *r_keyblock,
384                                         KEYDB_HANDLE *r_hd,
385                                         const byte *fprint, size_t fprint_len,
386                                         int lock);
387
388
389 /* Returns true if a secret key is available for the public key with
390    key id KEYID.  */
391 int have_secret_key_with_kid (u32 *keyid);
392
393 /* Parse the --default-key parameter.  Returns the last key (in terms
394    of when the option is given) that is available.  */
395 const char *parse_def_secret_key (ctrl_t ctrl);
396
397 /* Look up a secret key.  */
398 gpg_error_t get_seckey_default (ctrl_t ctrl, PKT_public_key *pk);
399 gpg_error_t get_seckey_default_or_card (ctrl_t ctrl, PKT_public_key *pk,
400                                         const byte *fpr, size_t fpr_len);
401
402 /* Search for keys matching some criteria.  */
403 gpg_error_t getkey_bynames (ctrl_t ctrl,
404                             getkey_ctx_t *retctx, PKT_public_key *pk,
405                             strlist_t names, int want_secret,
406                             kbnode_t *ret_keyblock);
407
408 /* Search for one key matching some criteria.  */
409 gpg_error_t getkey_byname (ctrl_t ctrl,
410                            getkey_ctx_t *retctx, PKT_public_key *pk,
411                            const char *name, int want_secret,
412                            kbnode_t *ret_keyblock);
413
414 /* Return the next search result.  */
415 gpg_error_t getkey_next (ctrl_t ctrl, getkey_ctx_t ctx,
416                          PKT_public_key *pk, kbnode_t *ret_keyblock);
417
418 /* Release any resources used by a key listing context.  */
419 void getkey_end (ctrl_t ctrl, getkey_ctx_t ctx);
420
421 /* Return the database handle used by this context.  The context still
422    owns the handle.  */
423 KEYDB_HANDLE get_ctx_handle(GETKEY_CTX ctx);
424
425 /* Enumerate some secret keys.  */
426 gpg_error_t enum_secret_keys (ctrl_t ctrl, void **context, PKT_public_key *pk);
427
428 /* Set the mainkey_id fields for all keys in KEYBLOCK.  */
429 void setup_main_keyids (kbnode_t keyblock);
430
431 /* This function merges information from the self-signed data into the
432    data structures.  */
433 void merge_keys_and_selfsig (ctrl_t ctrl, kbnode_t keyblock);
434
435 char *get_user_id_string_native (ctrl_t ctrl, u32 *keyid);
436 char *get_long_user_id_string (ctrl_t ctrl, u32 *keyid);
437 char *get_user_id (ctrl_t ctrl, u32 *keyid, size_t *rn, int *r_nouid);
438 char *get_user_id_native (ctrl_t ctrl, u32 *keyid);
439 char *get_user_id_byfpr (ctrl_t ctrl, const byte *fpr, size_t *rn);
440 char *get_user_id_byfpr_native (ctrl_t ctrl, const byte *fpr);
441
442 void release_akl(void);
443 int parse_auto_key_locate(const char *options);
444 int parse_key_origin (char *string);
445 const char *key_origin_string (int origin);
446
447 /*-- keyid.c --*/
448 int pubkey_letter( int algo );
449 char *pubkey_string (PKT_public_key *pk, char *buffer, size_t bufsize);
450 #define PUBKEY_STRING_SIZE 32
451 u32 v3_keyid (gcry_mpi_t a, u32 *ki);
452 void hash_public_key( gcry_md_hd_t md, PKT_public_key *pk );
453 char *format_keyid (u32 *keyid, int format, char *buffer, int len);
454
455 /* Return PK's keyid.  The memory is owned by PK.  */
456 u32 *pk_keyid (PKT_public_key *pk);
457
458 /* Return the keyid of the primary key associated with PK.  The memory
459    is owned by PK.  */
460 u32 *pk_main_keyid (PKT_public_key *pk);
461
462 /* Order A and B.  If A < B then return -1, if A == B then return 0,
463    and if A > B then return 1.  */
464 static int GPGRT_ATTR_UNUSED
465 keyid_cmp (const u32 *a, const u32 *b)
466 {
467   if (a[0] < b[0])
468     return -1;
469   if (a[0] > b[0])
470     return 1;
471   if (a[1] < b[1])
472     return -1;
473   if (a[1] > b[1])
474     return 1;
475   return 0;
476 }
477
478 /* Return whether PK is a primary key.  */
479 static int GPGRT_ATTR_UNUSED
480 pk_is_primary (PKT_public_key *pk)
481 {
482   return keyid_cmp (pk_keyid (pk), pk_main_keyid (pk)) == 0;
483 }
484
485 /* Copy the keyid in SRC to DEST and return DEST.  */
486 u32 *keyid_copy (u32 *dest, const u32 *src);
487
488 size_t keystrlen(void);
489 const char *keystr(u32 *keyid);
490 const char *keystr_with_sub (u32 *main_kid, u32 *sub_kid);
491 const char *keystr_from_pk(PKT_public_key *pk);
492 const char *keystr_from_pk_with_sub (PKT_public_key *main_pk,
493                                      PKT_public_key *sub_pk);
494
495 /* Return PK's key id as a string using the default format.  PK owns
496    the storage.  */
497 const char *pk_keyid_str (PKT_public_key *pk);
498
499 const char *keystr_from_desc(KEYDB_SEARCH_DESC *desc);
500 u32 keyid_from_pk( PKT_public_key *pk, u32 *keyid );
501 u32 keyid_from_sig (PKT_signature *sig, u32 *keyid );
502 u32 keyid_from_fingerprint (ctrl_t ctrl, const byte *fprint, size_t fprint_len,
503                             u32 *keyid);
504 byte *namehash_from_uid(PKT_user_id *uid);
505 unsigned nbits_from_pk( PKT_public_key *pk );
506
507 /* Convert an UTC TIMESTAMP into an UTC yyyy-mm-dd string.  Return
508  * that string.  The caller should pass a buffer with at least a size
509  * of MK_DATESTR_SIZE.  */
510 char *mk_datestr (char *buffer, size_t bufsize, u32 timestamp);
511 #define MK_DATESTR_SIZE 11
512
513 const char *datestr_from_pk( PKT_public_key *pk );
514 const char *datestr_from_sig( PKT_signature *sig );
515 const char *expirestr_from_pk( PKT_public_key *pk );
516 const char *expirestr_from_sig( PKT_signature *sig );
517 const char *revokestr_from_pk( PKT_public_key *pk );
518 const char *usagestr_from_pk (PKT_public_key *pk, int fill);
519 const char *colon_strtime (u32 t);
520 const char *colon_datestr_from_pk (PKT_public_key *pk);
521 const char *colon_datestr_from_sig (PKT_signature *sig);
522 const char *colon_expirestr_from_sig (PKT_signature *sig);
523 byte *fingerprint_from_pk( PKT_public_key *pk, byte *buf, size_t *ret_len );
524 char *hexfingerprint (PKT_public_key *pk, char *buffer, size_t buflen);
525 char *format_hexfingerprint (const char *fingerprint,
526                              char *buffer, size_t buflen);
527 gpg_error_t keygrip_from_pk (PKT_public_key *pk, unsigned char *array);
528 gpg_error_t hexkeygrip_from_pk (PKT_public_key *pk, char **r_grip);
529
530
531 /*-- kbnode.c --*/
532 KBNODE new_kbnode( PACKET *pkt );
533 KBNODE clone_kbnode( KBNODE node );
534 void release_kbnode( KBNODE n );
535 void delete_kbnode( KBNODE node );
536 void add_kbnode( KBNODE root, KBNODE node );
537 void insert_kbnode( KBNODE root, KBNODE node, int pkttype );
538 void move_kbnode( KBNODE *root, KBNODE node, KBNODE where );
539 void remove_kbnode( KBNODE *root, KBNODE node );
540 KBNODE find_prev_kbnode( KBNODE root, KBNODE node, int pkttype );
541 KBNODE find_next_kbnode( KBNODE node, int pkttype );
542 KBNODE find_kbnode( KBNODE node, int pkttype );
543 KBNODE walk_kbnode( KBNODE root, KBNODE *context, int all );
544 void clear_kbnode_flags( KBNODE n );
545 int  commit_kbnode( KBNODE *root );
546 void dump_kbnode( KBNODE node );
547
548 #endif /*G10_KEYDB_H*/