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