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
6 * This file is part of GnuPG.
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.
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.
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/>.
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)
41 typedef struct getkey_ctx_s *GETKEY_CTX;
42 typedef struct getkey_ctx_s *getkey_ctx_t;
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.
49 * This structure is also used to bind arbitrary packets together.
52 struct kbnode_struct {
57 ulong recno; /* used while updating the trustdb */
60 #define is_deleted_kbnode(a) ((a)->private_flag & 1)
61 #define is_cloned_kbnode(a) ((a)->private_flag & 2)
64 /* Bit flags used with build_pk_list. */
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. */
73 /* To store private data in the flags the private data must be left
74 * shifted by this value. */
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;
88 int flags; /* See PK_LIST_ constants. */
91 /* Structure to hold a list of secret key certificates. */
92 typedef struct sk_list *SK_LIST;
97 int mark; /* not used */
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 {
107 byte fingerprint[MAX_FINGERPRINT_LEN];
112 typedef struct keydb_handle *KEYDB_HANDLE;
115 /* Helper type for preference fucntions. */
124 #define KEYDB_RESOURCE_FLAG_PRIMARY 2 /* The primary resource. */
125 #define KEYDB_RESOURCE_FLAG_DEFAULT 4 /* The default one. */
126 #define KEYDB_RESOURCE_FLAG_READONLY 8 /* Open in read only mode. */
127 #define KEYDB_RESOURCE_FLAG_GPGVDEF 16 /* Default file for gpgv. */
129 /* Format a search term for debugging output. The caller must free
131 char *keydb_search_desc_dump (struct keydb_search_desc *desc);
133 /* Register a resource (keyring or keybox). */
134 gpg_error_t keydb_add_resource (const char *url, unsigned int flags);
136 /* Dump some statistics to the log. */
137 void keydb_dump_stats (void);
139 /* Create a new database handle. Returns NULL on error, sets ERRNO,
140 and prints an error diagnostic. */
141 KEYDB_HANDLE keydb_new (void);
143 /* Free all resources owned by the database handle. */
144 void keydb_release (KEYDB_HANDLE hd);
146 /* Set a flag on the handle to suppress use of cached results. This
147 is required for updating a keyring and for key listings. Fixme:
148 Using a new parameter for keydb_new might be a better solution. */
149 void keydb_disable_caching (KEYDB_HANDLE hd);
151 /* Save the last found state and invalidate the current selection. */
152 void keydb_push_found_state (KEYDB_HANDLE hd);
154 /* Restore the previous save state. */
155 void keydb_pop_found_state (KEYDB_HANDLE hd);
157 /* Return the file name of the resource. */
158 const char *keydb_get_resource_name (KEYDB_HANDLE hd);
160 /* Return the keyblock last found by keydb_search. */
161 gpg_error_t keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb);
163 /* Update the keyblock KB. */
164 gpg_error_t keydb_update_keyblock (ctrl_t ctrl, KEYDB_HANDLE hd, kbnode_t kb);
166 /* Insert a keyblock into one of the underlying keyrings or keyboxes. */
167 gpg_error_t keydb_insert_keyblock (KEYDB_HANDLE hd, kbnode_t kb);
169 /* Delete the currently selected keyblock. */
170 gpg_error_t keydb_delete_keyblock (KEYDB_HANDLE hd);
172 /* Find the first writable resource. */
173 gpg_error_t keydb_locate_writable (KEYDB_HANDLE hd);
175 /* Rebuild the on-disk caches of all key resources. */
176 void keydb_rebuild_caches (int noisy);
178 /* Return the number of skipped blocks (because they were to large to
179 read from a keybox) since the last search reset. */
180 unsigned long keydb_get_skipped_counter (KEYDB_HANDLE hd);
182 /* Clears the current search result and resets the handle's position. */
183 gpg_error_t keydb_search_reset (KEYDB_HANDLE hd);
185 /* Search the database for keys matching the search description. */
186 gpg_error_t keydb_search (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc,
187 size_t ndesc, size_t *descindex);
189 /* Return the first non-legacy key in the database. */
190 gpg_error_t keydb_search_first (KEYDB_HANDLE hd);
192 /* Return the next key (not the next matching key!). */
193 gpg_error_t keydb_search_next (KEYDB_HANDLE hd);
195 /* This is a convenience function for searching for keys with a long
197 gpg_error_t keydb_search_kid (KEYDB_HANDLE hd, u32 *kid);
199 /* This is a convenience function for searching for keys with a long
200 (20 byte) fingerprint. */
201 gpg_error_t keydb_search_fpr (KEYDB_HANDLE hd, const byte *fpr);
205 void show_revocation_reason( PKT_public_key *pk, int mode );
206 int check_signatures_trust (ctrl_t ctrl, PKT_signature *sig);
208 void release_pk_list (PK_LIST pk_list);
209 int build_pk_list (ctrl_t ctrl, strlist_t rcpts, PK_LIST *ret_pk_list);
210 gpg_error_t find_and_check_key (ctrl_t ctrl,
211 const char *name, unsigned int use,
212 int mark_hidden, int from_file,
213 pk_list_t *pk_list_addr);
215 int algo_available( preftype_t preftype, int algo,
216 const union pref_hint *hint );
217 int select_algo_from_prefs( PK_LIST pk_list, int preftype,
218 int request, const union pref_hint *hint);
219 int select_mdc_from_pklist (PK_LIST pk_list);
220 void warn_missing_mdc_from_pklist (PK_LIST pk_list);
221 void warn_missing_aes_from_pklist (PK_LIST pk_list);
224 int random_is_faked (void);
225 void release_sk_list( SK_LIST sk_list );
226 gpg_error_t build_sk_list (ctrl_t ctrl, strlist_t locusr,
227 SK_LIST *ret_sk_list, unsigned use);
229 /*-- passphrase.h --*/
230 unsigned char encode_s2k_iterations (int iterations);
231 int have_static_passphrase(void);
232 const char *get_static_passphrase (void);
233 void set_passphrase_from_string(const char *pass);
234 void read_passphrase_from_fd( int fd );
235 void passphrase_clear_cache (const char *cacheid);
236 DEK *passphrase_to_dek_ext(u32 *keyid, int pubkey_algo,
237 int cipher_algo, STRING2KEY *s2k, int mode,
238 const char *tryagain_text,
239 const char *custdesc, const char *custprompt,
241 DEK *passphrase_to_dek (int cipher_algo, STRING2KEY *s2k,
242 int create, int nocache,
243 const char *tryagain_text, int *canceled);
244 void set_next_passphrase( const char *s );
245 char *get_last_passphrase(void);
246 void next_to_last_passphrase(void);
248 void emit_status_need_passphrase (u32 *keyid, u32 *mainkeyid, int pubkey_algo);
250 #define FORMAT_KEYDESC_NORMAL 0
251 #define FORMAT_KEYDESC_IMPORT 1
252 #define FORMAT_KEYDESC_EXPORT 2
253 #define FORMAT_KEYDESC_DELKEY 3
254 char *gpg_format_keydesc (PKT_public_key *pk, int mode, int escaped);
259 /* Cache a copy of a public key in the public key cache. */
260 void cache_public_key( PKT_public_key *pk );
262 /* Disable and drop the public key cache. */
263 void getkey_disable_caches(void);
265 /* Return the public key with the key id KEYID and store it at PK. */
266 int get_pubkey( PKT_public_key *pk, u32 *keyid );
268 /* Similar to get_pubkey, but it does not take PK->REQ_USAGE into
269 account nor does it merge in the self-signed data. This function
270 also only considers primary keys. */
271 int get_pubkey_fast (PKT_public_key *pk, u32 *keyid);
273 /* Return the key block for the key with KEYID. */
274 kbnode_t get_pubkeyblock (u32 *keyid);
276 /* A list used by get_pubkeys to gather all of the matches. */
279 struct pubkey_s *next;
280 /* The key to use (either the public key or the subkey). */
284 typedef struct pubkey_s *pubkey_t;
286 /* Free a single key. This does not remove key from any list! */
287 void pubkey_free (pubkey_t key);
289 /* Free a list of public keys. */
290 void pubkeys_free (pubkey_t keys);
292 /* Returns all keys that match the search specfication SEARCH_TERMS.
293 The returned keys should be freed using pubkeys_free. */
295 get_pubkeys (ctrl_t ctrl,
296 char *search_terms, int use, int include_unusable, char *source,
297 int warn_possibly_ambiguous,
300 /* Find a public key identified by NAME. */
301 int get_pubkey_byname (ctrl_t ctrl,
302 GETKEY_CTX *retctx, PKT_public_key *pk,
304 KBNODE *ret_keyblock, KEYDB_HANDLE *ret_kdbhd,
305 int include_unusable, int no_akl );
307 /* Likewise, but only return the best match if NAME resembles a mail
309 int get_best_pubkey_byname (ctrl_t ctrl,
310 GETKEY_CTX *retctx, PKT_public_key *pk,
311 const char *name, KBNODE *ret_keyblock,
312 int include_unusable, int no_akl);
314 /* Get a public key directly from file FNAME. */
315 gpg_error_t get_pubkey_fromfile (ctrl_t ctrl,
316 PKT_public_key *pk, const char *fname);
318 /* Return the public key with the key id KEYID iff the secret key is
319 * available and store it at PK. */
320 gpg_error_t get_seckey (PKT_public_key *pk, u32 *keyid);
322 /* Lookup a key with the specified fingerprint. */
323 int get_pubkey_byfprint (PKT_public_key *pk, kbnode_t *r_keyblock,
324 const byte *fprint, size_t fprint_len);
326 /* This function is similar to get_pubkey_byfprint, but it doesn't
327 merge the self-signed data into the public key and subkeys or into
329 int get_pubkey_byfprint_fast (PKT_public_key *pk,
330 const byte *fprint, size_t fprint_len);
332 /* Returns true if a secret key is available for the public key with
334 int have_secret_key_with_kid (u32 *keyid);
336 /* Parse the --default-key parameter. Returns the last key (in terms
337 of when the option is given) that is available. */
338 const char *parse_def_secret_key (ctrl_t ctrl);
340 /* Look up a secret key. */
341 gpg_error_t get_seckey_default (ctrl_t ctrl, PKT_public_key *pk);
343 /* Search for keys matching some criteria. */
344 gpg_error_t getkey_bynames (getkey_ctx_t *retctx, PKT_public_key *pk,
345 strlist_t names, int want_secret,
346 kbnode_t *ret_keyblock);
348 /* Search for one key matching some criteria. */
349 gpg_error_t getkey_byname (ctrl_t ctrl,
350 getkey_ctx_t *retctx, PKT_public_key *pk,
351 const char *name, int want_secret,
352 kbnode_t *ret_keyblock);
354 /* Return the next search result. */
355 gpg_error_t getkey_next (getkey_ctx_t ctx, PKT_public_key *pk,
356 kbnode_t *ret_keyblock);
358 /* Release any resources used by a key listing context. */
359 void getkey_end (getkey_ctx_t ctx);
361 /* Return the database handle used by this context. The context still
363 KEYDB_HANDLE get_ctx_handle(GETKEY_CTX ctx);
365 /* Enumerate some secret keys. */
366 gpg_error_t enum_secret_keys (ctrl_t ctrl, void **context, PKT_public_key *pk);
368 /* Set the mainkey_id fields for all keys in KEYBLOCK. */
369 void setup_main_keyids (kbnode_t keyblock);
371 /* This function merges information from the self-signed data into the
373 void merge_keys_and_selfsig (kbnode_t keyblock);
375 char*get_user_id_string_native( u32 *keyid );
376 char*get_long_user_id_string( u32 *keyid );
377 char*get_user_id( u32 *keyid, size_t *rn );
378 char*get_user_id_native( u32 *keyid );
379 char *get_user_id_byfpr (const byte *fpr, size_t *rn);
380 char *get_user_id_byfpr_native (const byte *fpr);
382 void release_akl(void);
383 int parse_auto_key_locate(char *options);
386 int pubkey_letter( int algo );
387 char *pubkey_string (PKT_public_key *pk, char *buffer, size_t bufsize);
388 #define PUBKEY_STRING_SIZE 32
389 u32 v3_keyid (gcry_mpi_t a, u32 *ki);
390 void hash_public_key( gcry_md_hd_t md, PKT_public_key *pk );
391 char *format_keyid (u32 *keyid, int format, char *buffer, int len);
393 /* Return PK's keyid. The memory is owned by PK. */
394 u32 *pk_keyid (PKT_public_key *pk);
396 /* Return the keyid of the primary key associated with PK. The memory
398 u32 *pk_main_keyid (PKT_public_key *pk);
400 /* Order A and B. If A < B then return -1, if A == B then return 0,
401 and if A > B then return 1. */
402 static int GPGRT_ATTR_UNUSED
403 keyid_cmp (const u32 *a, const u32 *b)
416 /* Return whether PK is a primary key. */
417 static int GPGRT_ATTR_UNUSED
418 pk_is_primary (PKT_public_key *pk)
420 return keyid_cmp (pk_keyid (pk), pk_main_keyid (pk)) == 0;
423 /* Copy the keyid in SRC to DEST and return DEST. */
424 u32 *keyid_copy (u32 *dest, const u32 *src);
426 size_t keystrlen(void);
427 const char *keystr(u32 *keyid);
428 const char *keystr_with_sub (u32 *main_kid, u32 *sub_kid);
429 const char *keystr_from_pk(PKT_public_key *pk);
430 const char *keystr_from_pk_with_sub (PKT_public_key *main_pk,
431 PKT_public_key *sub_pk);
433 /* Return PK's key id as a string using the default format. PK owns
435 const char *pk_keyid_str (PKT_public_key *pk);
437 const char *keystr_from_desc(KEYDB_SEARCH_DESC *desc);
438 u32 keyid_from_pk( PKT_public_key *pk, u32 *keyid );
439 u32 keyid_from_sig( PKT_signature *sig, u32 *keyid );
440 u32 keyid_from_fingerprint(const byte *fprint, size_t fprint_len, u32 *keyid);
441 byte *namehash_from_uid(PKT_user_id *uid);
442 unsigned nbits_from_pk( PKT_public_key *pk );
443 const char *datestr_from_pk( PKT_public_key *pk );
444 const char *datestr_from_sig( PKT_signature *sig );
445 const char *expirestr_from_pk( PKT_public_key *pk );
446 const char *expirestr_from_sig( PKT_signature *sig );
447 const char *revokestr_from_pk( PKT_public_key *pk );
448 const char *usagestr_from_pk (PKT_public_key *pk, int fill);
449 const char *colon_strtime (u32 t);
450 const char *colon_datestr_from_pk (PKT_public_key *pk);
451 const char *colon_datestr_from_sig (PKT_signature *sig);
452 const char *colon_expirestr_from_sig (PKT_signature *sig);
453 byte *fingerprint_from_pk( PKT_public_key *pk, byte *buf, size_t *ret_len );
454 char *hexfingerprint (PKT_public_key *pk, char *buffer, size_t buflen);
455 char *format_hexfingerprint (const char *fingerprint,
456 char *buffer, size_t buflen);
457 gpg_error_t keygrip_from_pk (PKT_public_key *pk, unsigned char *array);
458 gpg_error_t hexkeygrip_from_pk (PKT_public_key *pk, char **r_grip);
462 KBNODE new_kbnode( PACKET *pkt );
463 KBNODE clone_kbnode( KBNODE node );
464 void release_kbnode( KBNODE n );
465 void delete_kbnode( KBNODE node );
466 void add_kbnode( KBNODE root, KBNODE node );
467 void insert_kbnode( KBNODE root, KBNODE node, int pkttype );
468 void move_kbnode( KBNODE *root, KBNODE node, KBNODE where );
469 void remove_kbnode( KBNODE *root, KBNODE node );
470 KBNODE find_prev_kbnode( KBNODE root, KBNODE node, int pkttype );
471 KBNODE find_next_kbnode( KBNODE node, int pkttype );
472 KBNODE find_kbnode( KBNODE node, int pkttype );
473 KBNODE walk_kbnode( KBNODE root, KBNODE *context, int all );
474 void clear_kbnode_flags( KBNODE n );
475 int commit_kbnode( KBNODE *root );
476 void dump_kbnode( KBNODE node );
478 #endif /*G10_KEYDB_H*/