summaryrefslogtreecommitdiffstats
path: root/kbx/keybox-defs.h
blob: c588016389b174bf33493b1cffa390e8a246570f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* keybox-defs.h - interal Keybox defintions
 *	Copyright (C) 2001, 2004 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#ifndef KEYBOX_DEFS_H
#define KEYBOX_DEFS_H 1

#ifdef GPG_ERR_SOURCE_DEFAULT
# if GPG_ERR_SOURCE_DEFAULT != GPG_ERR_SOURCE_KEYBOX
#  error GPG_ERR_SOURCE_DEFAULT already defined
# endif
#else
# define GPG_ERR_SOURCE_DEFAULT  GPG_ERR_SOURCE_KEYBOX
#endif
#include <gpg-error.h>
#define map_assuan_err(a) \
        map_assuan_err_with_source (GPG_ERR_SOURCE_DEFAULT, (a))

#include <sys/types.h> /* off_t */

/* We include the type defintions from jnlib instead of defining our
   owns here.  This will not allow us build KBX in a standalone way
   but there is currently no need for it anyway.  Same goes for
   stringhelp.h which for example provides a replacement for stpcpy -
   fixme: Better use the LIBOBJ mechnism. */
#include "../common/types.h"
#include "../common/stringhelp.h"

#include "keybox.h"


enum {
  BLOBTYPE_EMPTY = 0,
  BLOBTYPE_HEADER = 1,
  BLOBTYPE_PGP = 2,
  BLOBTYPE_X509 = 3
};


typedef struct keyboxblob *KEYBOXBLOB;


typedef struct keybox_name *KB_NAME;
typedef struct keybox_name const *CONST_KB_NAME;
struct keybox_name
{
  /* Link to the next resources, so that we can walk all
     resources.  */
  KB_NAME next;

  /* True if this is a keybox with secret keys.  */
  int secret;

  /*DOTLOCK lockhd;*/

  /* A table with all the handles accessing this resources.
     HANDLE_TABLE_SIZE gives the allocated length of this table unused
     entrues are set to NULL.  HANDLE_TABLE may be NULL. */
  KEYBOX_HANDLE *handle_table;
  size_t handle_table_size;

  /* Not yet used.  */
  int is_locked;

  /* Not yet used.  */
  int did_full_scan;

  /* The name of the resource file. */
  char fname[1];
};



struct keybox_handle {
  CONST_KB_NAME kb;
  int secret;             /* this is for a secret keybox */
  FILE *fp;
  int eof;
  int error;
  int ephemeral;
  struct {
    KEYBOXBLOB blob;
    off_t offset;
    size_t pk_no;
    size_t uid_no;
    unsigned int n_packets; /*used for delete and update*/
  } found;
  struct {
    char *name;
    char *pattern;
  } word_match;
};


/* Openpgp helper structures. */
struct _keybox_openpgp_key_info
{
  struct _keybox_openpgp_key_info *next;
  int algo;
  unsigned char keyid[8];
  int fprlen;  /* Either 16 or 20 */
  unsigned char fpr[20];
};

struct _keybox_openpgp_uid_info
{
  struct _keybox_openpgp_uid_info *next;
  size_t off;
  size_t len;
};

struct _keybox_openpgp_info
{
  int is_secret;        /* True if this is a secret key. */
  unsigned int nsubkeys;/* Total number of subkeys.  */
  unsigned int nuids;   /* Total number of user IDs in the keyblock. */
  unsigned int nsigs;   /* Total number of signatures in the keyblock. */

  /* Note, we use 2 structs here to better cope with the most common
     use of having one primary and one subkey - this allows us to
     statically allocate this structure and only malloc stuff for more
     than one subkey. */
  struct _keybox_openpgp_key_info primary;
  struct _keybox_openpgp_key_info subkeys;
  struct _keybox_openpgp_uid_info uids;
};
typedef struct _keybox_openpgp_info *keybox_openpgp_info_t;


/* Don't know whether this is needed: */
/*  static struct { */
/*    const char *homedir; */
/*    int dry_run; */
/*    int quiet; */
/*    int verbose; */
/*    int preserve_permissions; */
/*  } keybox_opt; */

/*-- keybox-init.c --*/
void _keybox_close_file (KEYBOX_HANDLE hd);


/*-- keybox-blob.c --*/
#ifdef KEYBOX_WITH_OPENPGP
  /* fixme */
#endif /*KEYBOX_WITH_OPENPGP*/
#ifdef KEYBOX_WITH_X509
int _keybox_create_x509_blob (KEYBOXBLOB *r_blob, ksba_cert_t cert,
                              unsigned char *sha1_digest, int as_ephemeral);
#endif /*KEYBOX_WITH_X509*/

int  _keybox_new_blob (KEYBOXBLOB *r_blob,
                       unsigned char *image, size_t imagelen,
                       off_t off);
void _keybox_release_blob (KEYBOXBLOB blob);
const unsigned char *_keybox_get_blob_image (KEYBOXBLOB blob, size_t *n);
off_t _keybox_get_blob_fileoffset (KEYBOXBLOB blob);
void _keybox_update_header_blob (KEYBOXBLOB blob);

/*-- keybox-openpgp.c --*/
gpg_error_t _keybox_parse_openpgp (const unsigned char *image, size_t imagelen,
                                   size_t *nparsed,
                                   keybox_openpgp_info_t info);
void _keybox_destroy_openpgp_info (keybox_openpgp_info_t info);


/*-- keybox-file.c --*/
int _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp);
int _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted);
int _keybox_write_blob (KEYBOXBLOB blob, FILE *fp);

/*-- keybox-search.c --*/
gpg_err_code_t _keybox_get_flag_location (const unsigned char *buffer,
                                          size_t length,
                                          int what,
                                          size_t *flag_off, size_t *flag_size);

/*-- keybox-dump.c --*/
int _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp);
int _keybox_dump_file (const char *filename, int stats_only, FILE *outfp);
int _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp);
int _keybox_dump_cut_records (const char *filename, unsigned long from,
                              unsigned long to, FILE *outfp);


/*-- keybox-util.c --*/
void *_keybox_malloc (size_t n);
void *_keybox_calloc (size_t n, size_t m);
void *_keybox_realloc (void *p, size_t n);
void  _keybox_free (void *p);

#define xtrymalloc(a)    _keybox_malloc ((a))
#define xtrycalloc(a,b)  _keybox_calloc ((a),(b))
#define xtryrealloc(a,b) _keybox_realloc((a),(b))
#define xfree(a)         _keybox_free ((a))


#define DIM(v) (sizeof(v)/sizeof((v)[0]))
#define DIMof(type,member)   DIM(((type *)0)->member)
#ifndef STR
#  define STR(v) #v
#endif
#define STR2(v) STR(v)

/*
  a couple of handy macros
*/

#define return_if_fail(expr) do {                        \
    if (!(expr)) {                                       \
        fprintf (stderr, "%s:%d: assertion '%s' failed\n", \
                 __FILE__, __LINE__, #expr );            \
        return;	                                         \
    } } while (0)
#define return_null_if_fail(expr) do {                   \
    if (!(expr)) {                                       \
        fprintf (stderr, "%s:%d: assertion '%s' failed\n", \
                 __FILE__, __LINE__, #expr );            \
        return NULL;	                                 \
    } } while (0)
#define return_val_if_fail(expr,val) do {                \
    if (!(expr)) {                                       \
        fprintf (stderr, "%s:%d: assertion '%s' failed\n", \
                 __FILE__, __LINE__, #expr );            \
        return (val);	                                 \
    } } while (0)
#define never_reached() do {                                   \
        fprintf (stderr, "%s:%d: oops; should never get here\n", \
                 __FILE__, __LINE__ );                         \
    } while (0)


/* some macros to replace ctype ones and avoid locale problems */
#define digitp(p)   (*(p) >= '0' && *(p) <= '9')
#define hexdigitp(a) (digitp (a)                     \
                      || (*(a) >= 'A' && *(a) <= 'F')  \
                      || (*(a) >= 'a' && *(a) <= 'f'))
/* the atoi macros assume that the buffer has only valid digits */
#define atoi_1(p)   (*(p) - '0' )
#define atoi_2(p)   ((atoi_1(p) * 10) + atoi_1((p)+1))
#define atoi_4(p)   ((atoi_2(p) * 100) + atoi_2((p)+2))
#define xtoi_1(p)   (*(p) <= '9'? (*(p)- '0'): \
                     *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
#define xtoi_2(p)   ((xtoi_1(p) * 16) + xtoi_1((p)+1))


#endif /*KEYBOX_DEFS_H*/