Imported Upstream version 2.2.5
[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   FILE *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   unsigned char keyid[8];
103   int fprlen;  /* Either 16 or 20 */
104   unsigned char fpr[20];
105 };
106
107 struct _keybox_openpgp_uid_info
108 {
109   struct _keybox_openpgp_uid_info *next;
110   size_t off;
111   size_t len;
112 };
113
114 struct _keybox_openpgp_info
115 {
116   int is_secret;        /* True if this is a secret key. */
117   unsigned int nsubkeys;/* Total number of subkeys.  */
118   unsigned int nuids;   /* Total number of user IDs in the keyblock. */
119   unsigned int nsigs;   /* Total number of signatures in the keyblock. */
120
121   /* Note, we use 2 structs here to better cope with the most common
122      use of having one primary and one subkey - this allows us to
123      statically allocate this structure and only malloc stuff for more
124      than one subkey. */
125   struct _keybox_openpgp_key_info primary;
126   struct _keybox_openpgp_key_info subkeys;
127   struct _keybox_openpgp_uid_info uids;
128 };
129 typedef struct _keybox_openpgp_info *keybox_openpgp_info_t;
130
131
132 /* Don't know whether this is needed: */
133 /*  static struct { */
134 /*    int dry_run; */
135 /*    int quiet; */
136 /*    int verbose; */
137 /*    int preserve_permissions; */
138 /*  } keybox_opt; */
139
140 /*-- keybox-init.c --*/
141 void _keybox_close_file (KEYBOX_HANDLE hd);
142
143
144 /*-- keybox-blob.c --*/
145 gpg_error_t _keybox_create_openpgp_blob (KEYBOXBLOB *r_blob,
146                                          keybox_openpgp_info_t info,
147                                          const unsigned char *image,
148                                          size_t imagelen,
149                                          int as_ephemeral);
150 #ifdef KEYBOX_WITH_X509
151 int _keybox_create_x509_blob (KEYBOXBLOB *r_blob, ksba_cert_t cert,
152                               unsigned char *sha1_digest, int as_ephemeral);
153 #endif /*KEYBOX_WITH_X509*/
154
155 int  _keybox_new_blob (KEYBOXBLOB *r_blob,
156                        unsigned char *image, size_t imagelen,
157                        off_t off);
158 void _keybox_release_blob (KEYBOXBLOB blob);
159 const unsigned char *_keybox_get_blob_image (KEYBOXBLOB blob, size_t *n);
160 off_t _keybox_get_blob_fileoffset (KEYBOXBLOB blob);
161 void _keybox_update_header_blob (KEYBOXBLOB blob, int for_openpgp);
162
163 /*-- keybox-openpgp.c --*/
164 gpg_error_t _keybox_parse_openpgp (const unsigned char *image, size_t imagelen,
165                                    size_t *nparsed,
166                                    keybox_openpgp_info_t info);
167 void _keybox_destroy_openpgp_info (keybox_openpgp_info_t info);
168
169
170 /*-- keybox-file.c --*/
171 int _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted);
172 int _keybox_write_blob (KEYBOXBLOB blob, FILE *fp);
173
174 /*-- keybox-search.c --*/
175 gpg_err_code_t _keybox_get_flag_location (const unsigned char *buffer,
176                                           size_t length,
177                                           int what,
178                                           size_t *flag_off, size_t *flag_size);
179
180 static inline int
181 blob_get_type (KEYBOXBLOB blob)
182 {
183   const unsigned char *buffer;
184   size_t length;
185
186   buffer = _keybox_get_blob_image (blob, &length);
187   if (length < 32)
188     return -1; /* blob too short */
189
190   return buffer[4];
191 }
192
193
194 /*-- keybox-dump.c --*/
195 int _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp);
196 int _keybox_dump_file (const char *filename, int stats_only, FILE *outfp);
197 int _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp);
198 int _keybox_dump_cut_records (const char *filename, unsigned long from,
199                               unsigned long to, FILE *outfp);
200
201
202 /*-- keybox-util.c --*/
203
204 /*
205  * A couple of handy macros
206  */
207
208
209 #endif /*KEYBOX_DEFS_H*/