Line data Source code
1 : /* keybox-defs.h - internal Keybox definitions
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 : # if GPG_ERR_SOURCE_DEFAULT != GPG_ERR_SOURCE_KEYBOX
25 : # error GPG_ERR_SOURCE_DEFAULT already defined
26 : # endif
27 : #else
28 : # define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_KEYBOX
29 : #endif
30 : #include <gpg-error.h>
31 : #define map_assuan_err(a) \
32 : map_assuan_err_with_source (GPG_ERR_SOURCE_DEFAULT, (a))
33 :
34 : #include <sys/types.h> /* off_t */
35 :
36 : /* We include the type defintions from jnlib instead of defining our
37 : owns here. This will not allow us build KBX in a standalone way
38 : but there is currently no need for it anyway. Same goes for
39 : stringhelp.h which for example provides a replacement for stpcpy -
40 : fixme: Better use the LIBOBJ mechnism. */
41 : #include "../common/types.h"
42 : #include "../common/stringhelp.h"
43 : #include "../common/dotlock.h"
44 : #include "../common/logging.h"
45 :
46 : #include "keybox.h"
47 :
48 :
49 : typedef struct keyboxblob *KEYBOXBLOB;
50 :
51 :
52 : typedef struct keybox_name *KB_NAME;
53 : struct keybox_name
54 : {
55 : /* Link to the next resources, so that we can walk all
56 : resources. */
57 : KB_NAME next;
58 :
59 : /* True if this is a keybox with secret keys. */
60 : int secret;
61 :
62 : /* A table with all the handles accessing this resources.
63 : HANDLE_TABLE_SIZE gives the allocated length of this table unused
64 : entrues are set to NULL. HANDLE_TABLE may be NULL. */
65 : KEYBOX_HANDLE *handle_table;
66 : size_t handle_table_size;
67 :
68 : /* The lock handle or NULL it not yet initialized. */
69 : dotlock_t lockhd;
70 :
71 : /* Not yet used. */
72 : int is_locked;
73 :
74 : /* Not yet used. */
75 : int did_full_scan;
76 :
77 : /* The name of the resource file. */
78 : char fname[1];
79 : };
80 :
81 :
82 : struct keybox_found_s
83 : {
84 : KEYBOXBLOB blob;
85 : size_t pk_no;
86 : size_t uid_no;
87 : };
88 :
89 : struct keybox_handle {
90 : KB_NAME kb;
91 : int secret; /* this is for a secret keybox */
92 : FILE *fp;
93 : int eof;
94 : int error;
95 : int ephemeral;
96 : int for_openpgp; /* Used by gpg. */
97 : struct keybox_found_s found;
98 : struct keybox_found_s saved_found;
99 : struct {
100 : char *name;
101 : char *pattern;
102 : } word_match;
103 : };
104 :
105 :
106 : /* Openpgp helper structures. */
107 : struct _keybox_openpgp_key_info
108 : {
109 : struct _keybox_openpgp_key_info *next;
110 : int algo;
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 : /* int dry_run; */
144 : /* int quiet; */
145 : /* int verbose; */
146 : /* int preserve_permissions; */
147 : /* } keybox_opt; */
148 :
149 : /*-- keybox-init.c --*/
150 : void _keybox_close_file (KEYBOX_HANDLE hd);
151 :
152 :
153 : /*-- keybox-blob.c --*/
154 : gpg_error_t _keybox_create_openpgp_blob (KEYBOXBLOB *r_blob,
155 : keybox_openpgp_info_t info,
156 : const unsigned char *image,
157 : size_t imagelen,
158 : u32 *sigstatus,
159 : int as_ephemeral);
160 : #ifdef KEYBOX_WITH_X509
161 : int _keybox_create_x509_blob (KEYBOXBLOB *r_blob, ksba_cert_t cert,
162 : unsigned char *sha1_digest, int as_ephemeral);
163 : #endif /*KEYBOX_WITH_X509*/
164 :
165 : int _keybox_new_blob (KEYBOXBLOB *r_blob,
166 : unsigned char *image, size_t imagelen,
167 : off_t off);
168 : void _keybox_release_blob (KEYBOXBLOB blob);
169 : const unsigned char *_keybox_get_blob_image (KEYBOXBLOB blob, size_t *n);
170 : off_t _keybox_get_blob_fileoffset (KEYBOXBLOB blob);
171 : void _keybox_update_header_blob (KEYBOXBLOB blob, int for_openpgp);
172 :
173 : /*-- keybox-openpgp.c --*/
174 : gpg_error_t _keybox_parse_openpgp (const unsigned char *image, size_t imagelen,
175 : size_t *nparsed,
176 : keybox_openpgp_info_t info);
177 : void _keybox_destroy_openpgp_info (keybox_openpgp_info_t info);
178 :
179 :
180 : /*-- keybox-file.c --*/
181 : int _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp);
182 : int _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted);
183 : int _keybox_write_blob (KEYBOXBLOB blob, FILE *fp);
184 :
185 : /*-- keybox-search.c --*/
186 : gpg_err_code_t _keybox_get_flag_location (const unsigned char *buffer,
187 : size_t length,
188 : int what,
189 : size_t *flag_off, size_t *flag_size);
190 :
191 : static inline int
192 87975 : blob_get_type (KEYBOXBLOB blob)
193 : {
194 : const unsigned char *buffer;
195 : size_t length;
196 :
197 87975 : buffer = _keybox_get_blob_image (blob, &length);
198 87975 : if (length < 32)
199 0 : return -1; /* blob too short */
200 :
201 87975 : return buffer[4];
202 : }
203 :
204 :
205 : /*-- keybox-dump.c --*/
206 : int _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp);
207 : int _keybox_dump_file (const char *filename, int stats_only, FILE *outfp);
208 : int _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp);
209 : int _keybox_dump_cut_records (const char *filename, unsigned long from,
210 : unsigned long to, FILE *outfp);
211 :
212 :
213 : /*-- keybox-util.c --*/
214 : void *_keybox_malloc (size_t n);
215 : void *_keybox_calloc (size_t n, size_t m);
216 : void *_keybox_realloc (void *p, size_t n);
217 : void _keybox_free (void *p);
218 :
219 : #define xtrymalloc(a) _keybox_malloc ((a))
220 : #define xtrycalloc(a,b) _keybox_calloc ((a),(b))
221 : #define xtryrealloc(a,b) _keybox_realloc((a),(b))
222 : #define xfree(a) _keybox_free ((a))
223 :
224 :
225 : #define DIM(v) (sizeof(v)/sizeof((v)[0]))
226 : #define DIMof(type,member) DIM(((type *)0)->member)
227 : #ifndef STR
228 : # define STR(v) #v
229 : #endif
230 : #define STR2(v) STR(v)
231 :
232 : /*
233 : a couple of handy macros
234 : */
235 :
236 : #define return_if_fail(expr) do { \
237 : if (!(expr)) { \
238 : fprintf (stderr, "%s:%d: assertion '%s' failed\n", \
239 : __FILE__, __LINE__, #expr ); \
240 : return; \
241 : } } while (0)
242 : #define return_null_if_fail(expr) do { \
243 : if (!(expr)) { \
244 : fprintf (stderr, "%s:%d: assertion '%s' failed\n", \
245 : __FILE__, __LINE__, #expr ); \
246 : return NULL; \
247 : } } while (0)
248 : #define return_val_if_fail(expr,val) do { \
249 : if (!(expr)) { \
250 : fprintf (stderr, "%s:%d: assertion '%s' failed\n", \
251 : __FILE__, __LINE__, #expr ); \
252 : return (val); \
253 : } } while (0)
254 : #define never_reached() do { \
255 : fprintf (stderr, "%s:%d: oops; should never get here\n", \
256 : __FILE__, __LINE__ ); \
257 : } while (0)
258 :
259 :
260 : /* some macros to replace ctype ones and avoid locale problems */
261 : #define digitp(p) (*(p) >= '0' && *(p) <= '9')
262 : #define hexdigitp(a) (digitp (a) \
263 : || (*(a) >= 'A' && *(a) <= 'F') \
264 : || (*(a) >= 'a' && *(a) <= 'f'))
265 : /* the atoi macros assume that the buffer has only valid digits */
266 : #define atoi_1(p) (*(p) - '0' )
267 : #define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1))
268 : #define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2))
269 : #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
270 : *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
271 : #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
272 :
273 :
274 : #endif /*KEYBOX_DEFS_H*/
|