Imported Upstream version 2.0.19
[platform/upstream/gpg2.git] / kbx / keybox-defs.h
1 /* keybox-defs.h - interal Keybox defintions
2  *      Copyright (C) 2001, 2004 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef KEYBOX_DEFS_H
21 #define KEYBOX_DEFS_H 1
22
23 #ifdef GPG_ERR_SOURCE_DEFAULT
24 #error GPG_ERR_SOURCE_DEFAULT already defined
25 #endif
26 #define GPG_ERR_SOURCE_DEFAULT  GPG_ERR_SOURCE_KEYBOX
27 #include <gpg-error.h>
28 #define map_assuan_err(a) \
29         map_assuan_err_with_source (GPG_ERR_SOURCE_DEFAULT, (a))
30
31 #include <sys/types.h> /* off_t */
32
33 /* We include the type defintions from jnlib instead of defining our
34    owns here.  This will not allow us build KBX in a standalone way
35    but there is currently no need for it anyway.  Same goes for
36    stringhelp.h which for example provides a replacement for stpcpy -
37    fixme: Better the LIBOBJ mechnism. */
38 #include "../jnlib/types.h"
39 #include "../jnlib/stringhelp.h"
40
41 #include "keybox.h"
42
43
44 enum {
45   BLOBTYPE_EMPTY = 0,
46   BLOBTYPE_HEADER = 1,
47   BLOBTYPE_PGP = 2,
48   BLOBTYPE_X509 = 3
49 };
50
51
52 typedef struct keyboxblob *KEYBOXBLOB;
53
54
55 typedef struct keybox_name *KB_NAME;
56 typedef struct keybox_name const *CONST_KB_NAME;
57 struct keybox_name 
58 {
59   /* Link to the next resources, so that we can walk all
60      resources.  */
61   KB_NAME next;
62
63   /* True if this is a keybox with secret keys.  */
64   int secret;
65
66   /*DOTLOCK lockhd;*/
67
68   /* A table with all the handles accessing this resources.
69      HANDLE_TABLE_SIZE gives the allocated length of this table unused
70      entrues are set to NULL.  HANDLE_TABLE may be NULL. */
71   KEYBOX_HANDLE *handle_table;
72   size_t handle_table_size;
73   
74   /* Not yet used.  */
75   int is_locked;
76
77   /* Not yet used.  */
78   int did_full_scan;
79
80   /* The name of the resource file. */
81   char fname[1];
82 };
83
84
85
86 struct keybox_handle {
87   CONST_KB_NAME kb;
88   int secret;             /* this is for a secret keybox */
89   FILE *fp;
90   int eof;
91   int error;
92   int ephemeral;  
93   struct {
94     KEYBOXBLOB blob;
95     off_t offset;
96     size_t pk_no;
97     size_t uid_no;
98     unsigned int n_packets; /*used for delete and update*/
99   } found;
100   struct {
101     char *name;
102     char *pattern;
103   } word_match;
104 };
105
106
107 /* Openpgp helper structures. */
108 struct _keybox_openpgp_key_info
109 {
110   struct _keybox_openpgp_key_info *next;
111   unsigned char keyid[8];
112   int fprlen;  /* Either 16 or 20 */
113   unsigned char fpr[20];
114 };
115
116 struct _keybox_openpgp_uid_info
117 {
118   struct _keybox_openpgp_uid_info *next;
119   size_t off;
120   size_t len;
121 };
122
123 struct _keybox_openpgp_info
124 {
125   int is_secret;        /* True if this is a secret key. */
126   unsigned int nsubkeys;/* Total number of subkeys.  */
127   unsigned int nuids;   /* Total number of user IDs in the keyblock. */
128   unsigned int nsigs;   /* Total number of signatures in the keyblock. */
129
130   /* Note, we use 2 structs here to better cope with the most common
131      use of having one primary and one subkey - this allows us to
132      statically allocate this structure and only malloc stuff for more
133      than one subkey. */
134   struct _keybox_openpgp_key_info primary;
135   struct _keybox_openpgp_key_info subkeys;
136   struct _keybox_openpgp_uid_info uids;
137 };
138 typedef struct _keybox_openpgp_info *keybox_openpgp_info_t;
139
140
141 /* Don't know whether this is needed: */
142 /*  static struct { */
143 /*    const char *homedir; */
144 /*    int dry_run; */
145 /*    int quiet; */
146 /*    int verbose; */
147 /*    int preserve_permissions; */
148 /*  } keybox_opt; */
149
150 /*-- keybox-init.c --*/
151 void _keybox_close_file (KEYBOX_HANDLE hd);
152
153
154 /*-- keybox-blob.c --*/
155 #ifdef KEYBOX_WITH_OPENPGP
156   /* fixme */
157 #endif /*KEYBOX_WITH_OPENPGP*/
158 #ifdef KEYBOX_WITH_X509
159 int _keybox_create_x509_blob (KEYBOXBLOB *r_blob, ksba_cert_t cert,
160                               unsigned char *sha1_digest, int as_ephemeral);
161 #endif /*KEYBOX_WITH_X509*/
162
163 int  _keybox_new_blob (KEYBOXBLOB *r_blob,
164                        unsigned char *image, size_t imagelen,
165                        off_t off);
166 void _keybox_release_blob (KEYBOXBLOB blob);
167 const unsigned char *_keybox_get_blob_image (KEYBOXBLOB blob, size_t *n);
168 off_t _keybox_get_blob_fileoffset (KEYBOXBLOB blob);
169 void _keybox_update_header_blob (KEYBOXBLOB blob);
170
171 /*-- keybox-openpgp.c --*/
172 gpg_error_t _keybox_parse_openpgp (const unsigned char *image, size_t imagelen,
173                                    size_t *nparsed,
174                                    keybox_openpgp_info_t info);
175 void _keybox_destroy_openpgp_info (keybox_openpgp_info_t info);
176
177
178 /*-- keybox-file.c --*/
179 int _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp);
180 int _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted);
181 int _keybox_write_blob (KEYBOXBLOB blob, FILE *fp);
182 int _keybox_write_header_blob (FILE *fp);
183
184 /*-- keybox-search.c --*/
185 gpg_err_code_t _keybox_get_flag_location (const unsigned char *buffer,
186                                           size_t length,
187                                           int what,
188                                           size_t *flag_off, size_t *flag_size);
189
190 /*-- keybox-dump.c --*/
191 int _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp);
192 int _keybox_dump_file (const char *filename, int stats_only, FILE *outfp);
193 int _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp);
194 int _keybox_dump_cut_records (const char *filename, unsigned long from,
195                               unsigned long to, FILE *outfp);
196
197
198 /*-- keybox-util.c --*/
199 void *_keybox_malloc (size_t n);
200 void *_keybox_calloc (size_t n, size_t m);
201 void *_keybox_realloc (void *p, size_t n);
202 void  _keybox_free (void *p);
203
204 #define xtrymalloc(a)    _keybox_malloc ((a))
205 #define xtrycalloc(a,b)  _keybox_calloc ((a),(b))
206 #define xtryrealloc(a,b) _keybox_realloc((a),(b))
207 #define xfree(a)         _keybox_free ((a))
208
209
210 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
211 #define DIMof(type,member)   DIM(((type *)0)->member)
212 #ifndef STR
213 #  define STR(v) #v
214 #endif
215 #define STR2(v) STR(v)
216
217 /*
218   a couple of handy macros 
219 */
220
221 #define return_if_fail(expr) do {                        \
222     if (!(expr)) {                                       \
223         fprintf (stderr, "%s:%d: assertion `%s' failed\n", \
224                  __FILE__, __LINE__, #expr );            \
225         return;                                          \
226     } } while (0)
227 #define return_null_if_fail(expr) do {                   \
228     if (!(expr)) {                                       \
229         fprintf (stderr, "%s:%d: assertion `%s' failed\n", \
230                  __FILE__, __LINE__, #expr );            \
231         return NULL;                                     \
232     } } while (0)
233 #define return_val_if_fail(expr,val) do {                \
234     if (!(expr)) {                                       \
235         fprintf (stderr, "%s:%d: assertion `%s' failed\n", \
236                  __FILE__, __LINE__, #expr );            \
237         return (val);                                    \
238     } } while (0)
239 #define never_reached() do {                                   \
240         fprintf (stderr, "%s:%d: oops; should never get here\n", \
241                  __FILE__, __LINE__ );                         \
242     } while (0)
243
244
245 /* some macros to replace ctype ones and avoid locale problems */
246 #define digitp(p)   (*(p) >= '0' && *(p) <= '9')
247 #define hexdigitp(a) (digitp (a)                     \
248                       || (*(a) >= 'A' && *(a) <= 'F')  \
249                       || (*(a) >= 'a' && *(a) <= 'f'))
250 /* the atoi macros assume that the buffer has only valid digits */
251 #define atoi_1(p)   (*(p) - '0' )
252 #define atoi_2(p)   ((atoi_1(p) * 10) + atoi_1((p)+1))
253 #define atoi_4(p)   ((atoi_2(p) * 100) + atoi_2((p)+2))
254 #define xtoi_1(p)   (*(p) <= '9'? (*(p)- '0'): \
255                      *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
256 #define xtoi_2(p)   ((xtoi_1(p) * 16) + xtoi_1((p)+1))
257
258
259 #endif /*KEYBOX_DEFS_H*/
260
261