Imported Upstream version 2.1.7
[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  *
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 <assuan.h>
25
26 #include "types.h"
27 #include "util.h"
28 #include "packet.h"
29
30 /* What qualifies as a certification (rather than a signature?) */
31 #define IS_CERT(s)       (IS_KEY_SIG(s) || IS_UID_SIG(s) || IS_SUBKEY_SIG(s) \
32                          || IS_KEY_REV(s) || IS_UID_REV(s) || IS_SUBKEY_REV(s))
33 #define IS_SIG(s)        (!IS_CERT(s))
34 #define IS_KEY_SIG(s)    ((s)->sig_class == 0x1f)
35 #define IS_UID_SIG(s)    (((s)->sig_class & ~3) == 0x10)
36 #define IS_SUBKEY_SIG(s) ((s)->sig_class == 0x18)
37 #define IS_KEY_REV(s)    ((s)->sig_class == 0x20)
38 #define IS_UID_REV(s)    ((s)->sig_class == 0x30)
39 #define IS_SUBKEY_REV(s) ((s)->sig_class == 0x28)
40
41 struct getkey_ctx_s;
42 typedef struct getkey_ctx_s *GETKEY_CTX;
43 typedef struct getkey_ctx_s *getkey_ctx_t;
44
45 /****************
46  * A Keyblock is all packets which form an entire certificate;
47  * i.e. the public key, certificate, trust packets, user ids,
48  * signatures, and subkey.
49  *
50  * This structure is also used to bind arbitrary packets together.
51  */
52
53 struct kbnode_struct {
54     KBNODE next;
55     PACKET *pkt;
56     int flag;
57     int private_flag;
58     ulong recno;  /* used while updating the trustdb */
59 };
60
61 #define is_deleted_kbnode(a)  ((a)->private_flag & 1)
62 #define is_cloned_kbnode(a)   ((a)->private_flag & 2)
63
64
65 enum resource_type {
66     rt_UNKNOWN = 0,
67     rt_RING = 1
68 };
69
70
71 /****************
72  * A data structre to hold information about the external position
73  * of a keyblock.
74  */
75 struct keyblock_pos_struct {
76     int   resno;     /* resource number */
77     enum resource_type rt;
78     off_t offset;    /* position information */
79     unsigned count;  /* length of the keyblock in packets */
80     iobuf_t  fp;     /* Used by enum_keyblocks. */
81     int secret;      /* working on a secret keyring */
82     PACKET *pkt;     /* ditto */
83     int valid;
84 };
85 typedef struct keyblock_pos_struct KBPOS;
86
87 /* Structure to hold a couple of public key certificates. */
88 typedef struct pk_list *PK_LIST;  /* Deprecated. */
89 typedef struct pk_list *pk_list_t;
90 struct pk_list
91 {
92   PK_LIST next;
93   PKT_public_key *pk;
94   int flags; /* flag bit 1==throw_keyid */
95 };
96
97 /* Structure to hold a list of secret key certificates.  */
98 typedef struct sk_list *SK_LIST;
99 struct sk_list
100 {
101   SK_LIST next;
102   PKT_public_key *pk;
103   int mark; /* not used */
104 };
105
106 /* structure to collect all information which can be used to
107  * identify a public key */
108 typedef struct pubkey_find_info *PUBKEY_FIND_INFO;
109 struct pubkey_find_info {
110     u32  keyid[2];
111     unsigned nbits;
112     byte pubkey_algo;
113     byte fingerprint[MAX_FINGERPRINT_LEN];
114     char userid[1];
115 };
116
117
118 typedef struct keydb_handle *KEYDB_HANDLE;
119
120
121 /* Helper type for preference fucntions. */
122 union pref_hint
123 {
124   int digest_length;
125 };
126
127
128 /*-- keydb.c --*/
129
130 #define KEYDB_RESOURCE_FLAG_PRIMARY  2  /* The primary resource.  */
131 #define KEYDB_RESOURCE_FLAG_DEFAULT  4  /* The default one.  */
132 #define KEYDB_RESOURCE_FLAG_READONLY 8  /* Open in read only mode.  */
133 #define KEYDB_RESOURCE_FLAG_GPGVDEF 16  /* Default file for gpgv.  */
134
135 gpg_error_t keydb_add_resource (const char *url, unsigned int flags);
136 void        keydb_dump_stats (void);
137
138 KEYDB_HANDLE keydb_new (void);
139 void keydb_release (KEYDB_HANDLE hd);
140 void keydb_disable_caching (KEYDB_HANDLE hd);
141 void keydb_push_found_state (KEYDB_HANDLE hd);
142 void keydb_pop_found_state (KEYDB_HANDLE hd);
143 const char *keydb_get_resource_name (KEYDB_HANDLE hd);
144 gpg_error_t keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb);
145 gpg_error_t keydb_update_keyblock (KEYDB_HANDLE hd, kbnode_t kb);
146 gpg_error_t keydb_insert_keyblock (KEYDB_HANDLE hd, kbnode_t kb);
147 gpg_error_t keydb_delete_keyblock (KEYDB_HANDLE hd);
148 gpg_error_t keydb_locate_writable (KEYDB_HANDLE hd, const char *reserved);
149 void keydb_rebuild_caches (int noisy);
150 unsigned long keydb_get_skipped_counter (KEYDB_HANDLE hd);
151 gpg_error_t keydb_search_reset (KEYDB_HANDLE hd);
152 gpg_error_t keydb_search (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc,
153                           size_t ndesc, size_t *descindex);
154 gpg_error_t keydb_search_first (KEYDB_HANDLE hd);
155 gpg_error_t keydb_search_next (KEYDB_HANDLE hd);
156 gpg_error_t keydb_search_kid (KEYDB_HANDLE hd, u32 *kid);
157 gpg_error_t keydb_search_fpr (KEYDB_HANDLE hd, const byte *fpr);
158
159
160 /*-- pkclist.c --*/
161 void show_revocation_reason( PKT_public_key *pk, int mode );
162 int  check_signatures_trust( PKT_signature *sig );
163
164 void release_pk_list (PK_LIST pk_list);
165 int  build_pk_list (ctrl_t ctrl,
166                     strlist_t rcpts, PK_LIST *ret_pk_list, unsigned use);
167 gpg_error_t find_and_check_key (ctrl_t ctrl,
168                                 const char *name, unsigned int use,
169                                 int mark_hidden, pk_list_t *pk_list_addr);
170
171 int  algo_available( preftype_t preftype, int algo,
172                      const union pref_hint *hint );
173 int  select_algo_from_prefs( PK_LIST pk_list, int preftype,
174                              int request, const union pref_hint *hint);
175 int  select_mdc_from_pklist (PK_LIST pk_list);
176 void warn_missing_mdc_from_pklist (PK_LIST pk_list);
177 void warn_missing_aes_from_pklist (PK_LIST pk_list);
178
179 /*-- skclist.c --*/
180 int  random_is_faked (void);
181 void release_sk_list( SK_LIST sk_list );
182 gpg_error_t  build_sk_list (strlist_t locusr, SK_LIST *ret_sk_list,
183                             unsigned use);
184
185 /*-- passphrase.h --*/
186 unsigned char encode_s2k_iterations (int iterations);
187 assuan_context_t agent_open (int try, const char *orig_codeset);
188 void agent_close (assuan_context_t ctx);
189 int  have_static_passphrase(void);
190 const char *get_static_passphrase (void);
191 void set_passphrase_from_string(const char *pass);
192 void read_passphrase_from_fd( int fd );
193 void passphrase_clear_cache ( u32 *keyid, const char *cacheid, int algo );
194 DEK *passphrase_to_dek_ext(u32 *keyid, int pubkey_algo,
195                            int cipher_algo, STRING2KEY *s2k, int mode,
196                            const char *tryagain_text,
197                            const char *custdesc, const char *custprompt,
198                            int *canceled);
199 DEK *passphrase_to_dek( u32 *keyid, int pubkey_algo,
200                         int cipher_algo, STRING2KEY *s2k, int mode,
201                         const char *tryagain_text, int *canceled);
202 void set_next_passphrase( const char *s );
203 char *get_last_passphrase(void);
204 void next_to_last_passphrase(void);
205
206 void emit_status_need_passphrase (u32 *keyid, u32 *mainkeyid, int pubkey_algo);
207
208 #define FORMAT_KEYDESC_NORMAL  0
209 #define FORMAT_KEYDESC_IMPORT  1
210 #define FORMAT_KEYDESC_EXPORT  2
211 #define FORMAT_KEYDESC_DELKEY  3
212 char *gpg_format_keydesc (PKT_public_key *pk, int mode, int escaped);
213
214
215 /*-- getkey.c --*/
216 void cache_public_key( PKT_public_key *pk );
217 void getkey_disable_caches(void);
218 int get_pubkey( PKT_public_key *pk, u32 *keyid );
219 int get_pubkey_fast ( PKT_public_key *pk, u32 *keyid );
220 KBNODE get_pubkeyblock( u32 *keyid );
221 int get_pubkey_byname (ctrl_t ctrl,
222                        GETKEY_CTX *rx, PKT_public_key *pk,  const char *name,
223                        KBNODE *ret_keyblock, KEYDB_HANDLE *ret_kdbhd,
224                        int include_unusable, int no_akl );
225 int get_pubkey_bynames( GETKEY_CTX *rx, PKT_public_key *pk,
226                         strlist_t names, KBNODE *ret_keyblock );
227 int get_pubkey_next( GETKEY_CTX ctx, PKT_public_key *pk, KBNODE *ret_keyblock );
228 void get_pubkey_end( GETKEY_CTX ctx );
229 gpg_error_t get_seckey (PKT_public_key *pk, u32 *keyid);
230 gpg_error_t get_pubkey_byfpr (PKT_public_key *pk, const byte *fpr);
231 int get_pubkey_byfprint (PKT_public_key *pk,  kbnode_t *r_keyblock,
232                          const byte *fprint, size_t fprint_len);
233 int get_pubkey_byfprint_fast (PKT_public_key *pk,
234                               const byte *fprint, size_t fprint_len);
235 int get_keyblock_byfprint( KBNODE *ret_keyblock, const byte *fprint,
236                                                  size_t fprint_len );
237
238 int have_secret_key_with_kid (u32 *keyid);
239
240 gpg_error_t get_seckey_byname (PKT_public_key *pk, const char *name);
241
242 gpg_error_t get_seckey_byfprint (PKT_public_key *pk,
243                                  const byte *fprint, size_t fprint_len);
244 gpg_error_t get_seckeyblock_byfprint (kbnode_t *ret_keyblock,
245                                       const byte *fprint, size_t fprint_len);
246
247 gpg_error_t getkey_bynames (getkey_ctx_t *retctx, PKT_public_key *pk,
248                             strlist_t names, int want_secret,
249                             kbnode_t *ret_keyblock);
250 gpg_error_t getkey_byname (getkey_ctx_t *retctx, PKT_public_key *pk,
251                            const char *name, int want_secret,
252                            kbnode_t *ret_keyblock);
253 gpg_error_t getkey_next (getkey_ctx_t ctx, PKT_public_key *pk,
254                          kbnode_t *ret_keyblock);
255 void getkey_end (getkey_ctx_t ctx);
256
257 gpg_error_t enum_secret_keys (void **context, PKT_public_key *pk);
258
259 void setup_main_keyids (kbnode_t keyblock);
260 void merge_keys_and_selfsig( KBNODE keyblock );
261 char*get_user_id_string_native( u32 *keyid );
262 char*get_long_user_id_string( u32 *keyid );
263 char*get_user_id( u32 *keyid, size_t *rn );
264 char*get_user_id_native( u32 *keyid );
265 char *get_user_id_byfpr (const byte *fpr, size_t *rn);
266 char *get_user_id_byfpr_native (const byte *fpr);
267 KEYDB_HANDLE get_ctx_handle(GETKEY_CTX ctx);
268 void release_akl(void);
269 int parse_auto_key_locate(char *options);
270
271 /*-- keyid.c --*/
272 int pubkey_letter( int algo );
273 char *pubkey_string (PKT_public_key *pk, char *buffer, size_t bufsize);
274 #define PUBKEY_STRING_SIZE 32
275 u32 v3_keyid (gcry_mpi_t a, u32 *ki);
276 void hash_public_key( gcry_md_hd_t md, PKT_public_key *pk );
277 size_t keystrlen(void);
278 const char *keystr(u32 *keyid);
279 const char *keystr_with_sub (u32 *main_kid, u32 *sub_kid);
280 const char *keystr_from_pk(PKT_public_key *pk);
281 const char *keystr_from_pk_with_sub (PKT_public_key *main_pk,
282                                      PKT_public_key *sub_pk);
283 const char *keystr_from_desc(KEYDB_SEARCH_DESC *desc);
284 u32 keyid_from_pk( PKT_public_key *pk, u32 *keyid );
285 u32 keyid_from_sig( PKT_signature *sig, u32 *keyid );
286 u32 keyid_from_fingerprint(const byte *fprint, size_t fprint_len, u32 *keyid);
287 byte *namehash_from_uid(PKT_user_id *uid);
288 unsigned nbits_from_pk( PKT_public_key *pk );
289 const char *datestr_from_pk( PKT_public_key *pk );
290 const char *datestr_from_sig( PKT_signature *sig );
291 const char *expirestr_from_pk( PKT_public_key *pk );
292 const char *expirestr_from_sig( PKT_signature *sig );
293 const char *revokestr_from_pk( PKT_public_key *pk );
294 const char *usagestr_from_pk (PKT_public_key *pk, int fill);
295 const char *colon_strtime (u32 t);
296 const char *colon_datestr_from_pk (PKT_public_key *pk);
297 const char *colon_datestr_from_sig (PKT_signature *sig);
298 const char *colon_expirestr_from_sig (PKT_signature *sig);
299 byte *fingerprint_from_pk( PKT_public_key *pk, byte *buf, size_t *ret_len );
300 char *hexfingerprint (PKT_public_key *pk);
301 gpg_error_t keygrip_from_pk (PKT_public_key *pk, unsigned char *array);
302 gpg_error_t hexkeygrip_from_pk (PKT_public_key *pk, char **r_grip);
303
304
305 /*-- kbnode.c --*/
306 KBNODE new_kbnode( PACKET *pkt );
307 KBNODE clone_kbnode( KBNODE node );
308 void release_kbnode( KBNODE n );
309 void delete_kbnode( KBNODE node );
310 void add_kbnode( KBNODE root, KBNODE node );
311 void insert_kbnode( KBNODE root, KBNODE node, int pkttype );
312 void move_kbnode( KBNODE *root, KBNODE node, KBNODE where );
313 void remove_kbnode( KBNODE *root, KBNODE node );
314 KBNODE find_prev_kbnode( KBNODE root, KBNODE node, int pkttype );
315 KBNODE find_next_kbnode( KBNODE node, int pkttype );
316 KBNODE find_kbnode( KBNODE node, int pkttype );
317 KBNODE walk_kbnode( KBNODE root, KBNODE *context, int all );
318 void clear_kbnode_flags( KBNODE n );
319 int  commit_kbnode( KBNODE *root );
320 void dump_kbnode( KBNODE node );
321
322 #endif /*G10_KEYDB_H*/