Imported Upstream version 2.3.0
[platform/upstream/gpg2.git] / kbx / keybox-defs.h
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 <https://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 #include "../common/util.h"
37 #include "keybox.h"
38
39
40 typedef struct keyboxblob *KEYBOXBLOB;
41
42
43 typedef struct keybox_name *KB_NAME;
44 struct keybox_name
45 {
46   /* Link to the next resources, so that we can walk all
47      resources.  */
48   KB_NAME next;
49
50   /* True if this is a keybox with secret keys.  */
51   int secret;
52
53   /* A table with all the handles accessing this resources.
54      HANDLE_TABLE_SIZE gives the allocated length of this table unused
55      entrues are set to NULL.  HANDLE_TABLE may be NULL. */
56   KEYBOX_HANDLE *handle_table;
57   size_t handle_table_size;
58
59   /* The lock handle or NULL it not yet initialized.  */
60   dotlock_t lockhd;
61
62   /* Not yet used.  */
63   int is_locked;
64
65   /* Not yet used.  */
66   int did_full_scan;
67
68   /* The name of the resource file. */
69   char fname[1];
70 };
71
72
73 struct keybox_found_s
74 {
75   KEYBOXBLOB blob;
76   size_t pk_no;
77   size_t uid_no;
78 };
79
80 struct keybox_handle {
81   KB_NAME kb;
82   int secret;             /* this is for a secret keybox */
83   estream_t fp;
84   int eof;
85   int error;
86   int ephemeral;
87   int for_openpgp;        /* Used by gpg.  */
88   struct keybox_found_s found;
89   struct keybox_found_s saved_found;
90   struct {
91     char *name;
92     char *pattern;
93   } word_match;
94 };
95
96
97 /* OpenPGP helper structures.  */
98 struct _keybox_openpgp_key_info
99 {
100   struct _keybox_openpgp_key_info *next;
101   int algo;
102   int version;
103   unsigned char grip[20];
104   unsigned char keyid[8];
105   int fprlen;  /* Either 16, 20 or 32 */
106   unsigned char fpr[32];
107 };
108
109 struct _keybox_openpgp_uid_info
110 {
111   struct _keybox_openpgp_uid_info *next;
112   size_t off;
113   size_t len;
114 };
115
116 struct _keybox_openpgp_info
117 {
118   int is_secret;        /* True if this is a secret key. */
119   unsigned int nsubkeys;/* Total number of subkeys.  */
120   unsigned int nuids;   /* Total number of user IDs in the keyblock. */
121   unsigned int nsigs;   /* Total number of signatures in the keyblock. */
122
123   /* Note, we use 2 structs here to better cope with the most common
124      use of having one primary and one subkey - this allows us to
125      statically allocate this structure and only malloc stuff for more
126      than one subkey. */
127   struct _keybox_openpgp_key_info primary;
128   struct _keybox_openpgp_key_info subkeys;
129   struct _keybox_openpgp_uid_info uids;
130 };
131 typedef struct _keybox_openpgp_info *keybox_openpgp_info_t;
132
133
134 /* Don't know whether this is needed: */
135 /*  static struct { */
136 /*    int dry_run; */
137 /*    int quiet; */
138 /*    int verbose; */
139 /*    int preserve_permissions; */
140 /*  } keybox_opt; */
141
142 /*-- keybox-init.c --*/
143 void _keybox_close_file (KEYBOX_HANDLE hd);
144
145
146 /*-- keybox-blob.c --*/
147 gpg_error_t _keybox_create_openpgp_blob (KEYBOXBLOB *r_blob,
148                                          keybox_openpgp_info_t info,
149                                          const unsigned char *image,
150                                          size_t imagelen,
151                                          int as_ephemeral);
152 char *_keybox_x509_email_kludge (const char *name);
153
154 #ifdef KEYBOX_WITH_X509
155 int _keybox_create_x509_blob (KEYBOXBLOB *r_blob, ksba_cert_t cert,
156                               unsigned char *sha1_digest, int as_ephemeral);
157 #endif /*KEYBOX_WITH_X509*/
158
159 int  _keybox_new_blob (KEYBOXBLOB *r_blob,
160                        unsigned char *image, size_t imagelen,
161                        off_t off);
162 void _keybox_release_blob (KEYBOXBLOB blob);
163 const unsigned char *_keybox_get_blob_image (KEYBOXBLOB blob, size_t *n);
164 off_t _keybox_get_blob_fileoffset (KEYBOXBLOB blob);
165 void _keybox_update_header_blob (KEYBOXBLOB blob, int for_openpgp);
166
167 /*-- keybox-openpgp.c --*/
168 gpg_error_t _keybox_parse_openpgp (const unsigned char *image, size_t imagelen,
169                                    size_t *nparsed,
170                                    keybox_openpgp_info_t info);
171 void _keybox_destroy_openpgp_info (keybox_openpgp_info_t info);
172
173
174 /*-- keybox-file.c --*/
175 int _keybox_read_blob (KEYBOXBLOB *r_blob, estream_t fp, int *skipped_deleted);
176 int _keybox_write_blob (KEYBOXBLOB blob, estream_t fp, FILE *outfp);
177
178 /*-- keybox-search.c --*/
179 gpg_err_code_t _keybox_get_flag_location (const unsigned char *buffer,
180                                           size_t length,
181                                           int what,
182                                           size_t *flag_off, size_t *flag_size);
183
184 static inline int
185 blob_get_type (KEYBOXBLOB blob)
186 {
187   const unsigned char *buffer;
188   size_t length;
189
190   buffer = _keybox_get_blob_image (blob, &length);
191   if (length < 32)
192     return -1; /* blob too short */
193
194   return buffer[4];
195 }
196
197
198 /*-- keybox-dump.c --*/
199 int _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp);
200 int _keybox_dump_file (const char *filename, int stats_only, FILE *outfp);
201 int _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp);
202 int _keybox_dump_cut_records (const char *filename, unsigned long from,
203                               unsigned long to, FILE *outfp);
204
205
206 /*-- keybox-util.c --*/
207
208 /*
209  * A couple of handy macros
210  */
211
212
213 #endif /*KEYBOX_DEFS_H*/