LCOV - code coverage report
Current view: top level - g10 - keydb.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 8 10 80.0 %
Date: 2016-09-12 12:29:17 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.11