1 /* keydb.h - Key database 2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 3 * 2006 Free Software Foundation, Inc. 4 * 5 * This file is part of GnuPG. 6 * 7 * GnuPG is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * GnuPG is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #ifndef G10_KEYDB_H 22 #define G10_KEYDB_H 23 24 #include "types.h" 25 #include "global.h" 26 #include "packet.h" 27 #include "cipher.h" 28 #ifdef ENABLE_AGENT_SUPPORT 29 #include "assuan.h" 30 #endif 31 32 /* What qualifies as a certification (rather than a signature?) */ 33 #define IS_CERT(s) (IS_KEY_SIG(s) || IS_UID_SIG(s) || IS_SUBKEY_SIG(s) \ 34 || IS_KEY_REV(s) || IS_UID_REV(s) || IS_SUBKEY_REV(s)) 35 #define IS_SIG(s) (!IS_CERT(s)) 36 #define IS_KEY_SIG(s) ((s)->sig_class == 0x1f) 37 #define IS_UID_SIG(s) (((s)->sig_class & ~3) == 0x10) 38 #define IS_SUBKEY_SIG(s) ((s)->sig_class == 0x18) 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 46 /**************** 47 * A Keyblock is all packets which form an entire certificate; 48 * i.e. the public key, certificate, trust packets, user ids, 49 * signatures, and subkey. 50 * 51 * This structure is also used to bind arbitrary packets together. 52 */ 53 54 struct kbnode_struct { 55 KBNODE next; 56 PACKET *pkt; 57 int flag; 58 int private_flag; 59 ulong recno; /* used while updating the trustdb */ 60 }; 61 62 #define is_deleted_kbnode(a) ((a)->private_flag & 1) 63 #define is_cloned_kbnode(a) ((a)->private_flag & 2) 64 65 66 enum resource_type { 67 rt_UNKNOWN = 0, 68 rt_RING = 1 69 }; 70 71 72 /**************** 73 * A data structre to hold information about the external position 74 * of a keyblock. 75 */ 76 struct keyblock_pos_struct { 77 int resno; /* resource number */ 78 enum resource_type rt; 79 off_t offset; /* position information */ 80 unsigned count; /* length of the keyblock in packets */ 81 IOBUF fp; /* used by enum_keyblocks */ 82 int secret; /* working on a secret keyring */ 83 PACKET *pkt; /* ditto */ 84 int valid; 85 }; 86 typedef struct keyblock_pos_struct KBPOS; 87 88 /* structure to hold a couple of public key certificates */ 89 typedef struct pk_list *PK_LIST; 90 struct pk_list { 91 PK_LIST next; 92 PKT_public_key *pk; 93 int flags; /* flag bit 1==throw_keyid */ 94 }; 95 96 /* structure to hold a couple of secret key certificates */ 97 typedef struct sk_list *SK_LIST; 98 struct sk_list { 99 SK_LIST next; 100 PKT_secret_key *sk; 101 int mark; /* not used */ 102 }; 103 104 /* structure to collect all information which can be used to 105 * identify a public key */ 106 typedef struct pubkey_find_info *PUBKEY_FIND_INFO; 107 struct pubkey_find_info { 108 u32 keyid[2]; 109 unsigned nbits; 110 byte pubkey_algo; 111 byte fingerprint[MAX_FINGERPRINT_LEN]; 112 char userid[1]; 113 }; 114 115 116 typedef struct keydb_handle *KEYDB_HANDLE; 117 118 typedef enum { 119 KEYDB_SEARCH_MODE_NONE, 120 KEYDB_SEARCH_MODE_EXACT, 121 KEYDB_SEARCH_MODE_SUBSTR, 122 KEYDB_SEARCH_MODE_MAIL, 123 KEYDB_SEARCH_MODE_MAILSUB, 124 KEYDB_SEARCH_MODE_MAILEND, 125 KEYDB_SEARCH_MODE_WORDS, 126 KEYDB_SEARCH_MODE_SHORT_KID, 127 KEYDB_SEARCH_MODE_LONG_KID, 128 KEYDB_SEARCH_MODE_FPR16, 129 KEYDB_SEARCH_MODE_FPR20, 130 KEYDB_SEARCH_MODE_FPR, 131 KEYDB_SEARCH_MODE_FIRST, 132 KEYDB_SEARCH_MODE_NEXT 133 } KeydbSearchMode; 134 135 struct keydb_search_desc { 136 KeydbSearchMode mode; 137 int (*skipfnc)(void *,u32*,PKT_user_id*); 138 void *skipfncvalue; 139 union { 140 const char *name; 141 byte fpr[MAX_FINGERPRINT_LEN]; 142 u32 kid[2]; 143 } u; 144 int exact; 145 }; 146 147 /*-- keydb.c --*/ 148 149 /* 150 Flag 1 == force 151 Flag 2 == default 152 */ 153 int keydb_add_resource (const char *url, int flags, int secret); 154 KEYDB_HANDLE keydb_new (int secret); 155 void keydb_release (KEYDB_HANDLE hd); 156 const char *keydb_get_resource_name (KEYDB_HANDLE hd); 157 int keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb); 158 int keydb_update_keyblock (KEYDB_HANDLE hd, KBNODE kb); 159 int keydb_insert_keyblock (KEYDB_HANDLE hd, KBNODE kb); 160 int keydb_delete_keyblock (KEYDB_HANDLE hd); 161 int keydb_locate_writable (KEYDB_HANDLE hd, const char *reserved); 162 void keydb_rebuild_caches (int noisy); 163 int keydb_search_reset (KEYDB_HANDLE hd); 164 #define keydb_search(a,b,c) keydb_search2((a),(b),(c),NULL) 165 int keydb_search2 (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc, 166 size_t ndesc, size_t *descindex); 167 int keydb_search_first (KEYDB_HANDLE hd); 168 int keydb_search_next (KEYDB_HANDLE hd); 169 int keydb_search_kid (KEYDB_HANDLE hd, u32 *kid); 170 int keydb_search_fpr (KEYDB_HANDLE hd, const byte *fpr); 171 172 173 /*-- pkclist.c --*/ 174 void show_revocation_reason( PKT_public_key *pk, int mode ); 175 int check_signatures_trust( PKT_signature *sig ); 176 void release_pk_list( PK_LIST pk_list ); 177 int build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use ); 178 union pref_hint 179 { 180 int digest_length; 181 }; 182 int algo_available( preftype_t preftype, int algo, 183 const union pref_hint *hint ); 184 int select_algo_from_prefs( PK_LIST pk_list, int preftype, 185 int request, const union pref_hint *hint ); 186 int select_mdc_from_pklist (PK_LIST pk_list); 187 188 /*-- skclist.c --*/ 189 void release_sk_list( SK_LIST sk_list ); 190 int build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list, 191 int unlock, unsigned use ); 192 193 /*-- passphrase.h --*/ 194 #ifdef ENABLE_AGENT_SUPPORT 195 assuan_context_t agent_open (int try, const char *orig_codeset); 196 void agent_close (assuan_context_t ctx); 197 #else 198 /* If we build w/o agent support, assuan.h won't get included and thus 199 we need to define a replacement for some Assuan types. */ 200 typedef int assuan_error_t; 201 typedef void *assuan_context_t; 202 #endif 203 int have_static_passphrase(void); 204 void set_passphrase_from_string(const char *pass); 205 void read_passphrase_from_fd( int fd ); 206 void passphrase_clear_cache ( u32 *keyid, const char *cacheid, int algo ); 207 char *ask_passphrase (const char *description, 208 const char *tryagain_text, 209 const char *promptid, 210 const char *prompt, 211 const char *cacheid, int *canceled); 212 DEK *passphrase_to_dek( u32 *keyid, int pubkey_algo, 213 int cipher_algo, STRING2KEY *s2k, int mode, 214 const char *tryagain_text, int *canceled); 215 void set_next_passphrase( const char *s ); 216 char *get_last_passphrase(void); 217 void next_to_last_passphrase(void); 218 219 /*-- getkey.c --*/ 220 int classify_user_id( const char *name, KEYDB_SEARCH_DESC *desc); 221 void cache_public_key( PKT_public_key *pk ); 222 void getkey_disable_caches(void); 223 int get_pubkey( PKT_public_key *pk, u32 *keyid ); 224 int get_pubkey_fast ( PKT_public_key *pk, u32 *keyid ); 225 KBNODE get_pubkeyblock( u32 *keyid ); 226 int get_pubkey_byname( PKT_public_key *pk, const char *name, 227 KBNODE *ret_keyblock, KEYDB_HANDLE *ret_kdbhd, 228 int include_unusable ); 229 int get_pubkey_bynames( GETKEY_CTX *rx, PKT_public_key *pk, 230 STRLIST names, KBNODE *ret_keyblock ); 231 int get_pubkey_next( GETKEY_CTX ctx, PKT_public_key *pk, KBNODE *ret_keyblock ); 232 void get_pubkey_end( GETKEY_CTX ctx ); 233 int get_seckey( PKT_secret_key *sk, u32 *keyid ); 234 int get_primary_seckey( PKT_secret_key *sk, u32 *keyid ); 235 int get_pubkey_byfprint( PKT_public_key *pk, const byte *fprint, 236 size_t fprint_len ); 237 int get_pubkey_byfprint_fast (PKT_public_key *pk, 238 const byte *fprint, size_t fprint_len); 239 int get_keyblock_byfprint( KBNODE *ret_keyblock, const byte *fprint, 240 size_t fprint_len ); 241 int get_keyblock_bylid( KBNODE *ret_keyblock, ulong lid ); 242 int seckey_available( u32 *keyid ); 243 int get_seckey_byname( PKT_secret_key *sk, const char *name, int unlock ); 244 int get_seckey_bynames( GETKEY_CTX *rx, PKT_secret_key *sk, 245 STRLIST names, KBNODE *ret_keyblock ); 246 int get_seckey_next (GETKEY_CTX ctx, PKT_secret_key *sk, KBNODE *ret_keyblock); 247 void get_seckey_end( GETKEY_CTX ctx ); 248 249 int get_seckey_byfprint( PKT_secret_key *sk, 250 const byte *fprint, size_t fprint_len); 251 int get_seckeyblock_byfprint (KBNODE *ret_keyblock, const byte *fprint, 252 size_t fprint_len ); 253 254 255 int enum_secret_keys( void **context, PKT_secret_key *sk, 256 int with_subkeys, int with_spm ); 257 void merge_keys_and_selfsig( KBNODE keyblock ); 258 char*get_user_id_string( u32 *keyid ); 259 char*get_user_id_string_native( u32 *keyid ); 260 char*get_long_user_id_string( u32 *keyid ); 261 char*get_user_id( u32 *keyid, size_t *rn ); 262 char*get_user_id_native( u32 *keyid ); 263 KEYDB_HANDLE get_ctx_handle(GETKEY_CTX ctx); 264 void release_akl(void); 265 int parse_auto_key_locate(char *options); 266 267 /*-- keyid.c --*/ 268 int pubkey_letter( int algo ); 269 void hash_public_key( MD_HANDLE md, PKT_public_key *pk ); 270 size_t keystrlen(void); 271 const char *keystr(u32 *keyid); 272 const char *keystr_from_pk(PKT_public_key *pk); 273 const char *keystr_from_sk(PKT_secret_key *sk); 274 const char *keystr_from_desc(KEYDB_SEARCH_DESC *desc); 275 u32 keyid_from_sk( PKT_secret_key *sk, u32 *keyid ); 276 u32 keyid_from_pk( PKT_public_key *pk, u32 *keyid ); 277 u32 keyid_from_sig( PKT_signature *sig, u32 *keyid ); 278 u32 keyid_from_fingerprint(const byte *fprint, size_t fprint_len, u32 *keyid); 279 byte *namehash_from_uid(PKT_user_id *uid); 280 unsigned nbits_from_pk( PKT_public_key *pk ); 281 unsigned nbits_from_sk( PKT_secret_key *sk ); 282 const char *datestr_from_pk( PKT_public_key *pk ); 283 const char *datestr_from_sk( PKT_secret_key *sk ); 284 const char *datestr_from_sig( PKT_signature *sig ); 285 const char *expirestr_from_pk( PKT_public_key *pk ); 286 const char *expirestr_from_sk( PKT_secret_key *sk ); 287 const char *expirestr_from_sig( PKT_signature *sig ); 288 const char *revokestr_from_pk( PKT_public_key *pk ); 289 const char *usagestr_from_pk( PKT_public_key *pk ); 290 const char *colon_strtime (u32 t); 291 const char *colon_datestr_from_pk (PKT_public_key *pk); 292 const char *colon_datestr_from_sk (PKT_secret_key *sk); 293 const char *colon_datestr_from_sig (PKT_signature *sig); 294 const char *colon_expirestr_from_sig (PKT_signature *sig); 295 byte *fingerprint_from_sk( PKT_secret_key *sk, byte *buf, size_t *ret_len ); 296 byte *fingerprint_from_pk( PKT_public_key *pk, byte *buf, size_t *ret_len ); 297 298 /*-- kbnode.c --*/ 299 KBNODE new_kbnode( PACKET *pkt ); 300 KBNODE clone_kbnode( KBNODE node ); 301 void release_kbnode( KBNODE n ); 302 void delete_kbnode( KBNODE node ); 303 void add_kbnode( KBNODE root, KBNODE node ); 304 void insert_kbnode( KBNODE root, KBNODE node, int pkttype ); 305 void move_kbnode( KBNODE *root, KBNODE node, KBNODE where ); 306 void remove_kbnode( KBNODE *root, KBNODE node ); 307 KBNODE find_prev_kbnode( KBNODE root, KBNODE node, int pkttype ); 308 KBNODE find_next_kbnode( KBNODE node, int pkttype ); 309 KBNODE find_kbnode( KBNODE node, int pkttype ); 310 KBNODE walk_kbnode( KBNODE root, KBNODE *context, int all ); 311 void clear_kbnode_flags( KBNODE n ); 312 int commit_kbnode( KBNODE *root ); 313 void dump_kbnode( KBNODE node ); 314 315 #endif /*G10_KEYDB_H*/ 316