1
/* kbxfile.c - KBX file handling
2
* Copyright (C) 2000 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
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 2 of the License, or
9
* (at your option) any later version.
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.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22
* We will change the whole system to use only KBX. This file here
23
* will implement the methods needed to operate on plain KBXfiles.
24
* Most stuff from getkey and ringedit will be replaced by stuff here.
25
* To make things even more easier we will only allow one updateable kbxfile
26
* and optionally some read-only files.
44
* Read the blob at the current fileposition and return an allocated
45
* pointer nto the blob if it was found.
46
* Fixme: return a blob object.
49
do_search_by_fpr ( const char *filename, FILE *a, const char *fpr,
56
rc = kbx_read_blob ( &blob, a );
57
if ( rc && rc != -1 ) {
58
log_error (_("file `%s': error reading blob\n"), filename );
61
rc = kbx_blob_has_fpr ( blob, fpr );
70
kbx_release_blob ( blob );
76
kbxfile_search_by_fpr( const char *filename, const byte *fpr )
82
fp = fopen ( filename, "rb" );
84
log_error(_("can't open `%s': %s\n"), filename, strerror(errno) );
88
while ( (rc=do_search_by_fpr ( filename, fp, fpr, &blob )) == -1 )
91
fputs ("FOUND\n", stderr );
92
kbx_dump_blob ( stderr, blob );
93
kbx_release_blob ( blob );
102
* Read the blob at the current fileposition and return an allocated
103
* pointer nto the blob if it was found.
104
* Fixme: return a blob object.
107
do_search_by_keyid ( const char *filename, FILE *a,
108
const byte *keyidbuf, size_t keyidlen, KBXBLOB *r_blob )
114
rc = kbx_read_blob ( &blob, a );
115
if ( rc && rc != -1 ) {
116
log_error (_("file `%s': error reading blob\n"), filename );
119
rc = kbx_blob_has_kid ( blob, keyidbuf, keyidlen );
122
rc = GPGERR_GENERAL; /* eof */
128
kbx_release_blob ( blob );
134
* Look for a KBX described by an keyid. This function will in
135
* turn return each matching keyid because there may me duplicates
136
* (which can't happen for fingerprints)
137
* mode 10 = short keyid
141
kbxfile_search_by_kid ( const char *filename, u32 *kid, int mode )
146
byte kbuf[8], *kbufptr;
149
fp = fopen ( filename, "rb" );
151
log_error(_("can't open `%s': %s\n"), filename, strerror(errno) );
155
kbuf[0] = kid[0] >> 24;
156
kbuf[1] = kid[0] >> 16;
157
kbuf[2] = kid[0] >> 8;
159
kbuf[4] = kid[1] >> 24;
160
kbuf[5] = kid[1] >> 16;
161
kbuf[6] = kid[1] >> 8;
167
else if (mode == 11 ) {
176
while ( (rc=do_search_by_keyid ( filename, fp,
177
kbufptr, kbuflen, &blob )) == -1 )
180
fputs ("FOUND:\n", stderr );
181
kbx_dump_blob ( stderr, blob );
182
kbx_release_blob ( blob );
192
do_search_by_uid ( const char *filename, FILE *a,
193
int (*cmpfnc)(const byte*,size_t,void*), void *cmpdata,
200
rc = kbx_read_blob ( &blob, a );
201
if ( rc && rc != -1 ) {
202
log_error (_("file `%s': error reading blob\n"), filename );
205
rc = kbx_blob_has_uid ( blob, cmpfnc, cmpdata );
208
rc = GPGERR_GENERAL; /* eof */
214
kbx_release_blob ( blob );
221
substr_compare ( const byte *buf, size_t buflen, void *opaque )
223
return !!memistr ( buf, buflen, opaque );
228
kbxfile_search_by_uid ( const char *filename, const char *name )
233
byte kbuf[8], *kbufptr;
236
fp = fopen ( filename, "rb" );
238
log_error(_("can't open `%s': %s\n"), filename, strerror(errno) );
244
while ( (rc=do_search_by_uid ( filename, fp,
245
substr_compare, name, &blob )) == -1 )
248
fputs ("FOUND:\n", stderr );
249
kbx_dump_blob ( stderr, blob );
250
kbx_release_blob ( blob );
261
export_as_kbxfile(void)
265
KBNODE keyblock = NULL;
268
rc = enum_keyblocks_begin( &kbpos, 0 );
271
log_error("enum_keyblocks(open) failed: %s\n", gpg_errstr(rc) );
275
while( !(rc = enum_keyblocks_next( kbpos, 1, &keyblock )) ) {
280
merge_keys_and_selfsig( keyblock );
281
rc = kbx_create_blob ( &blob, keyblock );
283
log_error("kbx_create_blob failed: %s\n", gpg_errstr(rc) );
286
p = kbx_get_blob_image ( blob, &n );
287
fwrite( p, n, 1, stdout );
288
kbx_release_blob ( blob );
292
log_error("enum_keyblocks(read) failed: %s\n", gpg_errstr(rc));
295
enum_keyblocks_end( kbpos );
296
release_kbnode( keyblock );
301
do_print_kbxfile( const char *filename, FILE *a )
306
rc = kbx_read_blob ( &blob, a );
307
if ( rc && rc != -1 ) {
308
log_error (_("file `%s': error reading blob\n"), filename );
311
kbx_dump_blob ( stdout, blob );
312
kbx_release_blob ( blob );
317
print_kbxfile( const char *filename )
321
fp = fopen ( filename, "rb" );
323
log_error(_("can't open `%s': %s\n"), filename, strerror(errno) );
327
while ( !do_print_kbxfile( filename, fp ) )