Tizen 2.1 base
[framework/security/gnupg.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 2 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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  */
22
23 #ifndef G10_KEYDB_H
24 #define G10_KEYDB_H
25
26 #include "types.h"
27 #include "global.h"
28 #include "packet.h"
29 #include "cipher.h"
30 #ifdef ENABLE_AGENT_SUPPORT
31 #include "assuan.h"
32 #endif
33
34 /* What qualifies as a certification (rather than a signature?) */
35 #define IS_CERT(s)       (IS_KEY_SIG(s) || IS_UID_SIG(s) || IS_SUBKEY_SIG(s) \
36                          || IS_KEY_REV(s) || IS_UID_REV(s) || IS_SUBKEY_REV(s))
37 #define IS_SIG(s)        (!IS_CERT(s))
38 #define IS_KEY_SIG(s)    ((s)->sig_class == 0x1f)
39 #define IS_UID_SIG(s)    (((s)->sig_class & ~3) == 0x10)
40 #define IS_SUBKEY_SIG(s) ((s)->sig_class == 0x18)
41 #define IS_KEY_REV(s)    ((s)->sig_class == 0x20)
42 #define IS_UID_REV(s)    ((s)->sig_class == 0x30)
43 #define IS_SUBKEY_REV(s) ((s)->sig_class == 0x28)
44
45 struct getkey_ctx_s;
46 typedef struct getkey_ctx_s *GETKEY_CTX;
47
48 /****************
49  * A Keyblock is all packets which form an entire certificate;
50  * i.e. the public key, certificate, trust packets, user ids,
51  * signatures, and subkey.
52  *
53  * This structure is also used to bind arbitrary packets together.
54  */
55
56 struct kbnode_struct {
57     KBNODE next;
58     PACKET *pkt;
59     int flag;
60     int private_flag;
61     ulong recno;  /* used while updating the trustdb */
62 };
63
64 #define is_deleted_kbnode(a)  ((a)->private_flag & 1)
65 #define is_cloned_kbnode(a)   ((a)->private_flag & 2)
66
67
68 enum resource_type {
69     rt_UNKNOWN = 0,
70     rt_RING = 1
71 };
72
73
74 /****************
75  * A data structre to hold information about the external position
76  * of a keyblock.
77  */
78 struct keyblock_pos_struct {
79     int   resno;     /* resource number */
80     enum resource_type rt;
81     off_t offset;    /* position information */
82     unsigned count;  /* length of the keyblock in packets */
83     IOBUF  fp;       /* used by enum_keyblocks */
84     int secret;      /* working on a secret keyring */
85     PACKET *pkt;     /* ditto */
86     int valid;
87 };
88 typedef struct keyblock_pos_struct KBPOS;
89
90 /* structure to hold a couple of public key certificates */
91 typedef struct pk_list *PK_LIST;
92 struct pk_list {
93     PK_LIST next;
94     PKT_public_key *pk;
95     int flags; /* flag bit 1==throw_keyid */
96 };
97
98 /* structure to hold a couple of secret key certificates */
99 typedef struct sk_list *SK_LIST;
100 struct sk_list {
101     SK_LIST next;
102     PKT_secret_key *sk;
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 typedef enum {
121     KEYDB_SEARCH_MODE_NONE,
122     KEYDB_SEARCH_MODE_EXACT,
123     KEYDB_SEARCH_MODE_SUBSTR,
124     KEYDB_SEARCH_MODE_MAIL,
125     KEYDB_SEARCH_MODE_MAILSUB,
126     KEYDB_SEARCH_MODE_MAILEND,
127     KEYDB_SEARCH_MODE_WORDS,
128     KEYDB_SEARCH_MODE_SHORT_KID,
129     KEYDB_SEARCH_MODE_LONG_KID,
130     KEYDB_SEARCH_MODE_FPR16,
131     KEYDB_SEARCH_MODE_FPR20,
132     KEYDB_SEARCH_MODE_FPR,
133     KEYDB_SEARCH_MODE_FIRST,
134     KEYDB_SEARCH_MODE_NEXT
135 } KeydbSearchMode;
136
137 struct keydb_search_desc {
138     KeydbSearchMode mode;
139     int (*skipfnc)(void *,u32*,PKT_user_id*);
140     void *skipfncvalue;
141     union {
142         const char *name;
143         byte fpr[MAX_FINGERPRINT_LEN];
144         u32  kid[2];
145     } u;
146     int exact;
147 };
148
149 /*-- keydb.c --*/
150
151 /*
152   Flag 1 == force
153   Flag 2 == default
154 */
155 int keydb_add_resource (const char *url, int flags, int secret);
156 KEYDB_HANDLE keydb_new (int secret);
157 void keydb_release (KEYDB_HANDLE hd);
158 const char *keydb_get_resource_name (KEYDB_HANDLE hd);
159 int keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb);
160 int keydb_update_keyblock (KEYDB_HANDLE hd, KBNODE kb);
161 int keydb_insert_keyblock (KEYDB_HANDLE hd, KBNODE kb);
162 int keydb_delete_keyblock (KEYDB_HANDLE hd);
163 int keydb_locate_writable (KEYDB_HANDLE hd, const char *reserved);
164 void keydb_rebuild_caches (int noisy);
165 int keydb_search_reset (KEYDB_HANDLE hd);
166 #define keydb_search(a,b,c) keydb_search2((a),(b),(c),NULL)
167 int keydb_search2 (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc,
168                    size_t ndesc, size_t *descindex);
169 int keydb_search_first (KEYDB_HANDLE hd);
170 int keydb_search_next (KEYDB_HANDLE hd);
171 int keydb_search_kid (KEYDB_HANDLE hd, u32 *kid);
172 int keydb_search_fpr (KEYDB_HANDLE hd, const byte *fpr);
173
174
175 /*-- pkclist.c --*/
176 void show_revocation_reason( PKT_public_key *pk, int mode );
177 int  check_signatures_trust( PKT_signature *sig );
178 void release_pk_list( PK_LIST pk_list );
179 int  build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use );
180 union pref_hint
181 {
182   int digest_length;
183 };
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
190 /*-- skclist.c --*/
191 void release_sk_list( SK_LIST sk_list );
192 int  build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
193                                             int unlock, unsigned use );
194
195 /*-- passphrase.h --*/
196 #ifdef ENABLE_AGENT_SUPPORT
197 assuan_context_t agent_open (int try, const char *orig_codeset);
198 void agent_close (assuan_context_t ctx);
199 #else
200 /* If we build w/o agent support, assuan.h won't get included and thus
201    we need to define a replacement for some Assuan types. */
202 typedef int assuan_error_t;
203 typedef void *assuan_context_t;
204 #endif
205 int  have_static_passphrase(void);
206 void set_passphrase_from_string(const char *pass);
207 void read_passphrase_from_fd( int fd );
208 void passphrase_clear_cache ( u32 *keyid, const char *cacheid, int algo );
209 char *ask_passphrase (const char *description,
210                       const char *tryagain_text,
211                       const char *promptid,
212                       const char *prompt, 
213                       const char *cacheid, int *canceled);
214 DEK *passphrase_to_dek( u32 *keyid, int pubkey_algo,
215                         int cipher_algo, STRING2KEY *s2k, int mode,
216                         const char *tryagain_text, int *canceled);
217 void set_next_passphrase( const char *s );
218 char *get_last_passphrase(void);
219 void next_to_last_passphrase(void);
220
221 /*-- getkey.c --*/
222 int classify_user_id( const char *name, KEYDB_SEARCH_DESC *desc);
223 void cache_public_key( PKT_public_key *pk );
224 void getkey_disable_caches(void);
225 int get_pubkey( PKT_public_key *pk, u32 *keyid );
226 int get_pubkey_fast ( PKT_public_key *pk, u32 *keyid );
227 KBNODE get_pubkeyblock( u32 *keyid );
228 int get_pubkey_byname( PKT_public_key *pk,  const char *name,
229                        KBNODE *ret_keyblock, KEYDB_HANDLE *ret_kdbhd,
230                        int include_unusable );
231 int get_pubkey_bynames( GETKEY_CTX *rx, PKT_public_key *pk,
232                         STRLIST names, KBNODE *ret_keyblock );
233 int get_pubkey_next( GETKEY_CTX ctx, PKT_public_key *pk, KBNODE *ret_keyblock );
234 void get_pubkey_end( GETKEY_CTX ctx );
235 int get_seckey( PKT_secret_key *sk, u32 *keyid );
236 int get_primary_seckey( PKT_secret_key *sk, u32 *keyid );
237 int get_pubkey_byfprint( PKT_public_key *pk, const byte *fprint,
238                                                  size_t fprint_len );
239 int get_pubkey_byfprint_fast (PKT_public_key *pk,
240                               const byte *fprint, size_t fprint_len);
241 int get_keyblock_byfprint( KBNODE *ret_keyblock, const byte *fprint,
242                                                  size_t fprint_len );
243 int get_keyblock_bylid( KBNODE *ret_keyblock, ulong lid );
244 int seckey_available( u32 *keyid );
245 int get_seckey_byname( PKT_secret_key *sk, const char *name, int unlock );
246 int get_seckey_bynames( GETKEY_CTX *rx, PKT_secret_key *sk,
247                         STRLIST names, KBNODE *ret_keyblock );
248 int get_seckey_next (GETKEY_CTX ctx, PKT_secret_key *sk, KBNODE *ret_keyblock);
249 void get_seckey_end( GETKEY_CTX ctx );
250
251 int get_seckey_byfprint( PKT_secret_key *sk,
252                          const byte *fprint, size_t fprint_len);
253 int get_seckeyblock_byfprint (KBNODE *ret_keyblock, const byte *fprint,
254                               size_t fprint_len );
255
256
257 int enum_secret_keys( void **context, PKT_secret_key *sk,
258                       int with_subkeys, int with_spm );
259 void merge_keys_and_selfsig( KBNODE keyblock );
260 char*get_user_id_string( u32 *keyid );
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 KEYDB_HANDLE get_ctx_handle(GETKEY_CTX ctx);
266 void release_akl(void);
267 int parse_auto_key_locate(char *options);
268
269 /*-- keyid.c --*/
270 int pubkey_letter( int algo );
271 void hash_public_key( MD_HANDLE md, PKT_public_key *pk );
272 size_t keystrlen(void);
273 const char *keystr(u32 *keyid);
274 const char *keystr_from_pk(PKT_public_key *pk);
275 const char *keystr_from_sk(PKT_secret_key *sk);
276 const char *keystr_from_desc(KEYDB_SEARCH_DESC *desc);
277 u32 keyid_from_sk( PKT_secret_key *sk, u32 *keyid );
278 u32 keyid_from_pk( PKT_public_key *pk, u32 *keyid );
279 u32 keyid_from_sig( PKT_signature *sig, u32 *keyid );
280 u32 keyid_from_fingerprint(const byte *fprint, size_t fprint_len, u32 *keyid);
281 byte *namehash_from_uid(PKT_user_id *uid);
282 unsigned nbits_from_pk( PKT_public_key *pk );
283 unsigned nbits_from_sk( PKT_secret_key *sk );
284 const char *datestr_from_pk( PKT_public_key *pk );
285 const char *datestr_from_sk( PKT_secret_key *sk );
286 const char *datestr_from_sig( PKT_signature *sig );
287 const char *expirestr_from_pk( PKT_public_key *pk );
288 const char *expirestr_from_sk( PKT_secret_key *sk );
289 const char *expirestr_from_sig( PKT_signature *sig );
290 const char *revokestr_from_pk( PKT_public_key *pk );
291 const char *usagestr_from_pk( PKT_public_key *pk );
292 const char *colon_strtime (u32 t);
293 const char *colon_datestr_from_pk (PKT_public_key *pk);
294 const char *colon_datestr_from_sk (PKT_secret_key *sk);
295 const char *colon_datestr_from_sig (PKT_signature *sig);
296 const char *colon_expirestr_from_sig (PKT_signature *sig);
297 byte *fingerprint_from_sk( PKT_secret_key *sk, byte *buf, size_t *ret_len );
298 byte *fingerprint_from_pk( PKT_public_key *pk, byte *buf, size_t *ret_len );
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*/