Imported Upstream version 2.0.19
[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 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 "packet.h"
28 #include "cipher.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
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 /****************
71  * A data structre to hold information about the external position
72  * of a keyblock.
73  */
74 struct keyblock_pos_struct {
75     int   resno;     /* resource number */
76     enum resource_type rt;
77     off_t offset;    /* position information */
78     unsigned count;  /* length of the keyblock in packets */
79     iobuf_t  fp;     /* Used by enum_keyblocks. */
80     int secret;      /* working on a secret keyring */
81     PACKET *pkt;     /* ditto */
82     int valid;
83 };
84 typedef struct keyblock_pos_struct KBPOS;
85
86 /* structure to hold a couple of public key certificates */
87 typedef struct pk_list *PK_LIST;
88 struct pk_list {
89     PK_LIST next;
90     PKT_public_key *pk;
91     int flags; /* flag bit 1==throw_keyid */
92 };
93
94 /* structure to hold a couple of secret key certificates */
95 typedef struct sk_list *SK_LIST;
96 struct sk_list {
97     SK_LIST next;
98     PKT_secret_key *sk;
99     int mark; /* not used */
100 };
101
102 /* structure to collect all information which can be used to
103  * identify a public key */
104 typedef struct pubkey_find_info *PUBKEY_FIND_INFO;
105 struct pubkey_find_info {
106     u32  keyid[2];
107     unsigned nbits;
108     byte pubkey_algo;
109     byte fingerprint[MAX_FINGERPRINT_LEN];
110     char userid[1];
111 };
112
113
114 typedef struct keydb_handle *KEYDB_HANDLE;
115
116 typedef enum {
117     KEYDB_SEARCH_MODE_NONE,
118     KEYDB_SEARCH_MODE_EXACT,
119     KEYDB_SEARCH_MODE_SUBSTR,
120     KEYDB_SEARCH_MODE_MAIL,
121     KEYDB_SEARCH_MODE_MAILSUB,
122     KEYDB_SEARCH_MODE_MAILEND,
123     KEYDB_SEARCH_MODE_WORDS,
124     KEYDB_SEARCH_MODE_SHORT_KID,
125     KEYDB_SEARCH_MODE_LONG_KID,
126     KEYDB_SEARCH_MODE_FPR16,
127     KEYDB_SEARCH_MODE_FPR20,
128     KEYDB_SEARCH_MODE_FPR,
129     KEYDB_SEARCH_MODE_FIRST,
130     KEYDB_SEARCH_MODE_NEXT
131 } KeydbSearchMode;
132
133 struct keydb_search_desc {
134     KeydbSearchMode mode;
135     int (*skipfnc)(void *,u32*,PKT_user_id*);
136     void *skipfncvalue;
137     union {
138         const char *name;
139         byte fpr[MAX_FINGERPRINT_LEN];
140         u32  kid[2];
141     } u;
142     int exact;
143 };
144
145
146 /* Helper type for preference fucntions. */
147 union pref_hint
148 {
149   int digest_length;
150 };
151
152
153 /*-- keydb.c --*/
154
155 /*
156   Flag 1 == force
157   Flag 2 == default
158 */
159 int keydb_add_resource (const char *url, int flags, int secret);
160 KEYDB_HANDLE keydb_new (int secret);
161 void keydb_release (KEYDB_HANDLE hd);
162 const char *keydb_get_resource_name (KEYDB_HANDLE hd);
163 int keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb);
164 int keydb_update_keyblock (KEYDB_HANDLE hd, KBNODE kb);
165 int keydb_insert_keyblock (KEYDB_HANDLE hd, KBNODE kb);
166 int keydb_delete_keyblock (KEYDB_HANDLE hd);
167 int keydb_locate_writable (KEYDB_HANDLE hd, const char *reserved);
168 void keydb_rebuild_caches (int noisy);
169 int keydb_search_reset (KEYDB_HANDLE hd);
170 #define keydb_search(a,b,c) keydb_search2((a),(b),(c),NULL)
171 int keydb_search2 (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc,
172                    size_t ndesc, size_t *descindex);
173 int keydb_search_first (KEYDB_HANDLE hd);
174 int keydb_search_next (KEYDB_HANDLE hd);
175 int keydb_search_kid (KEYDB_HANDLE hd, u32 *kid);
176 int keydb_search_fpr (KEYDB_HANDLE hd, const byte *fpr);
177
178
179 /*-- pkclist.c --*/
180 void show_revocation_reason( PKT_public_key *pk, int mode );
181 int  check_signatures_trust( PKT_signature *sig );
182 void release_pk_list( PK_LIST pk_list );
183 int  build_pk_list( strlist_t rcpts, PK_LIST *ret_pk_list, unsigned use );
184 int  algo_available( preftype_t preftype, int algo,
185                      const union pref_hint *hint );
186 int  select_algo_from_prefs( PK_LIST pk_list, int preftype,
187                              int request, const union pref_hint *hint);
188 int  select_mdc_from_pklist (PK_LIST pk_list);
189 void warn_missing_mdc_from_pklist (PK_LIST pk_list);
190 void warn_missing_aes_from_pklist (PK_LIST pk_list);
191
192 /*-- skclist.c --*/
193 int  random_is_faked (void);
194 void release_sk_list( SK_LIST sk_list );
195 int  build_sk_list( strlist_t locusr, SK_LIST *ret_sk_list,
196                                             int unlock, unsigned use );
197
198 /*-- passphrase.h --*/
199 unsigned char encode_s2k_iterations (int iterations);
200 assuan_context_t agent_open (int try, const char *orig_codeset);
201 void agent_close (assuan_context_t ctx);
202 int  have_static_passphrase(void);
203 void set_passphrase_from_string(const char *pass);
204 void read_passphrase_from_fd( int fd );
205 void passphrase_clear_cache ( u32 *keyid, const char *cacheid, int algo );
206 DEK *passphrase_to_dek_ext(u32 *keyid, int pubkey_algo,
207                            int cipher_algo, STRING2KEY *s2k, int mode,
208                            const char *tryagain_text,
209                            const char *custdesc, const char *custprompt,
210                            int *canceled);
211 DEK *passphrase_to_dek( u32 *keyid, int pubkey_algo,
212                         int cipher_algo, STRING2KEY *s2k, int mode,
213                         const char *tryagain_text, int *canceled);
214 void set_next_passphrase( const char *s );
215 char *get_last_passphrase(void);
216 void next_to_last_passphrase(void);
217
218 /*-- getkey.c --*/
219 int classify_user_id( const char *name, KEYDB_SEARCH_DESC *desc);
220 void cache_public_key( PKT_public_key *pk );
221 void getkey_disable_caches(void);
222 int get_pubkey( PKT_public_key *pk, u32 *keyid );
223 int get_pubkey_fast ( PKT_public_key *pk, u32 *keyid );
224 KBNODE get_pubkeyblock( u32 *keyid );
225 int get_pubkey_byname (GETKEY_CTX *rx, PKT_public_key *pk,  const char *name,
226                        KBNODE *ret_keyblock, KEYDB_HANDLE *ret_kdbhd,
227                        int include_unusable, int no_akl );
228 int get_pubkey_bynames( GETKEY_CTX *rx, PKT_public_key *pk,
229                         strlist_t names, KBNODE *ret_keyblock );
230 int get_pubkey_next( GETKEY_CTX ctx, PKT_public_key *pk, KBNODE *ret_keyblock );
231 void get_pubkey_end( GETKEY_CTX ctx );
232 int get_seckey( PKT_secret_key *sk, u32 *keyid );
233 int get_primary_seckey( PKT_secret_key *sk, u32 *keyid );
234 int get_pubkey_byfprint( PKT_public_key *pk, const byte *fprint,
235                                                  size_t fprint_len );
236 int get_pubkey_byfprint_fast (PKT_public_key *pk,
237                               const byte *fprint, size_t fprint_len);
238 int get_keyblock_byfprint( KBNODE *ret_keyblock, const byte *fprint,
239                                                  size_t fprint_len );
240 int get_keyblock_bylid( KBNODE *ret_keyblock, ulong lid );
241 int seckey_available( u32 *keyid );
242 int get_seckey_byname( PKT_secret_key *sk, const char *name, int unlock );
243 int get_seckey_bynames( GETKEY_CTX *rx, PKT_secret_key *sk,
244                         strlist_t names, KBNODE *ret_keyblock );
245 int get_seckey_next (GETKEY_CTX ctx, PKT_secret_key *sk, KBNODE *ret_keyblock);
246 void get_seckey_end( GETKEY_CTX ctx );
247
248 int get_seckey_byfprint( PKT_secret_key *sk,
249                          const byte *fprint, size_t fprint_len);
250 int get_seckeyblock_byfprint (KBNODE *ret_keyblock, const byte *fprint,
251                               size_t fprint_len );
252
253
254 int enum_secret_keys( void **context, PKT_secret_key *sk,
255                       int with_subkeys, int with_spm );
256 void merge_keys_and_selfsig( KBNODE keyblock );
257 char*get_user_id_string( u32 *keyid );
258 char*get_user_id_string_native( u32 *keyid );
259 char*get_long_user_id_string( u32 *keyid );
260 char*get_user_id( u32 *keyid, size_t *rn );
261 char*get_user_id_native( u32 *keyid );
262 KEYDB_HANDLE get_ctx_handle(GETKEY_CTX ctx);
263 void release_akl(void);
264 int parse_auto_key_locate(char *options);
265
266 /*-- keyid.c --*/
267 int pubkey_letter( int algo );
268 u32 v3_keyid (gcry_mpi_t a, u32 *ki);
269 void hash_public_key( gcry_md_hd_t 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 char *serialno_and_fpr_from_sk (const unsigned char *sn, size_t snlen,
298                                 PKT_secret_key *sk);
299
300 /*-- kbnode.c --*/
301 KBNODE new_kbnode( PACKET *pkt );
302 KBNODE clone_kbnode( KBNODE node );
303 void release_kbnode( KBNODE n );
304 void delete_kbnode( KBNODE node );
305 void add_kbnode( KBNODE root, KBNODE node );
306 void insert_kbnode( KBNODE root, KBNODE node, int pkttype );
307 void move_kbnode( KBNODE *root, KBNODE node, KBNODE where );
308 void remove_kbnode( KBNODE *root, KBNODE node );
309 KBNODE find_prev_kbnode( KBNODE root, KBNODE node, int pkttype );
310 KBNODE find_next_kbnode( KBNODE node, int pkttype );
311 KBNODE find_kbnode( KBNODE node, int pkttype );
312 KBNODE walk_kbnode( KBNODE root, KBNODE *context, int all );
313 void clear_kbnode_flags( KBNODE n );
314 int  commit_kbnode( KBNODE *root );
315 void dump_kbnode( KBNODE node );
316
317 #endif /*G10_KEYDB_H*/