1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-14 21:47:19 +02:00

Use inline functions to convert buffer data to scalars.

* include/host2net.h (buf16_to_ulong, buf16_to_uint): New.
(buf16_to_ushort, buf16_to_u16): New.
(buf32_to_size_t, buf32_to_ulong, buf32_to_uint, buf32_to_u32): New.
--

This fixes sign extension on shift problems.  Hanno Böck found a case
with an invalid read due to this problem.  To fix that almost all uses
of "<< 24" and "<< 8" are changed by this patch to use an inline
function from host2net.h.

(back ported from commit 2183683bd6)

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2015-02-12 20:34:44 +01:00
parent b62395cf59
commit 3627123dc8
20 changed files with 310 additions and 275 deletions

View file

@ -25,6 +25,7 @@
#include "keybox-defs.h"
#include <gcrypt.h>
#include "../include/host2net.h"
/* Argg, we can't include ../common/util.h */
char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf);
@ -33,21 +34,13 @@ char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf);
static ulong
get32 (const byte *buffer)
{
ulong a;
a = *buffer << 24;
a |= buffer[1] << 16;
a |= buffer[2] << 8;
a |= buffer[3];
return a;
return buf32_to_ulong (buffer);
}
static ulong
get16 (const byte *buffer)
{
ulong a;
a = *buffer << 8;
a |= buffer[1];
return a;
return buf16_to_ulong (buffer);
}
void
@ -93,9 +86,9 @@ dump_header_blob (const byte *buffer, size_t length, FILE *fp)
if ( memcmp (buffer+8, "KBXf", 4))
fprintf (fp, "[Error: invalid magic number]\n");
n = get32 (buffer+16);
n = get32 (buffer+16);
fprintf( fp, "created-at: %lu\n", n );
n = get32 (buffer+20);
n = get32 (buffer+20);
fprintf( fp, "last-maint: %lu\n", n );
return 0;
@ -117,7 +110,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
const byte *p;
buffer = _keybox_get_blob_image (blob, &length);
if (length < 32)
{
fprintf (fp, "[blob too short]\n");
@ -125,7 +118,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
}
n = get32( buffer );
if (n > length)
if (n > length)
fprintf (fp, "[blob larger than length - output truncated]\n");
else
length = n; /* ignore the rest */
@ -159,7 +152,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
fprintf (fp, "[blob too short]\n");
return -1;
}
n = get16 (buffer + 6);
fprintf( fp, "Blob-Flags: %04lX", n);
if (n)
@ -188,7 +181,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
fprintf( fp, "Data-Offset: %lu\n", rawdata_off );
fprintf( fp, "Data-Length: %lu\n", rawdata_len );
if (rawdata_off > length || rawdata_len > length
if (rawdata_off > length || rawdata_len > length
|| rawdata_off+rawdata_off > length)
fprintf (fp, "[Error: raw data larger than blob]\n");
@ -207,7 +200,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
{
int i;
ulong kidoff, kflags;
fprintf (fp, "Key-Fpr[%lu]: ", n );
for (i=0; i < 20; i++ )
fprintf (fp, "%02X", p[i]);
@ -220,7 +213,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
kflags = get16 (p + 24 );
fprintf( fp, "\nKey-Flags[%lu]: %04lX\n", n, kflags);
}
/* serial number */
fputs ("Serial-No: ", fp);
nserial = get16 (p);
@ -244,7 +237,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
for (n=0; n < nuids; n++, p += uidinfolen)
{
ulong uidoff, uidlen, uflags;
uidoff = get32( p );
uidlen = get32( p+4 );
if (type == BLOBTYPE_X509 && !n)
@ -284,7 +277,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
fprintf (fp, "Uid-Validity[%lu]: %d\n", n, p[10] );
}
}
nsigs = get16 (p);
fprintf (fp, "Sig-Count: %lu\n", nsigs );
siginfolen = get16 (p + 2);
@ -294,7 +287,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
for (n=0; n < nsigs; n++, p += siginfolen)
{
ulong sflags;
sflags = get32 (p);
fprintf (fp, "Sig-Expire[%lu]: ", n );
if (!sflags)
@ -341,11 +334,11 @@ hash_blob_rawdata (KEYBOXBLOB blob, unsigned char *digest)
ulong rawdata_off, rawdata_len;
buffer = _keybox_get_blob_image (blob, &length);
if (length < 32)
return -1;
n = get32 (buffer);
if (n < length)
if (n < length)
length = n; /* Blob larger than length in header - ignore the rest. */
type = buffer[4];
@ -364,11 +357,11 @@ hash_blob_rawdata (KEYBOXBLOB blob, unsigned char *digest)
if (length < 40)
return -1;
rawdata_off = get32 (buffer + 8);
rawdata_len = get32 (buffer + 12);
if (rawdata_off > length || rawdata_len > length
if (rawdata_off > length || rawdata_len > length
|| rawdata_off+rawdata_off > length)
return -1; /* Out of bounds. */
@ -408,7 +401,7 @@ update_stats (KEYBOXBLOB blob, struct file_stats_s *s)
}
n = get32( buffer );
if (n > length)
if (n > length)
s->too_large_blobs++;
else
length = n; /* ignore the rest */
@ -439,7 +432,7 @@ update_stats (KEYBOXBLOB blob, struct file_stats_s *s)
s->too_short_blobs++;
return -1;
}
n = get16 (buffer + 6);
if (n)
{
@ -512,13 +505,13 @@ _keybox_dump_file (const char *filename, int stats_only, FILE *outfp)
rc = 0;
if (rc)
fprintf (outfp, "error reading `%s': %s\n", filename, gpg_strerror (rc));
if (fp != stdin)
fclose (fp);
if (stats_only)
{
fprintf (outfp,
fprintf (outfp,
"Total number of blobs: %8lu\n"
" header: %8lu\n"
" empty: %8lu\n"
@ -551,9 +544,9 @@ _keybox_dump_file (const char *filename, int stats_only, FILE *outfp)
struct dupitem_s
struct dupitem_s
{
unsigned long recno;
unsigned long recno;
unsigned char digest[20];
};
@ -563,7 +556,7 @@ cmp_dupitems (const void *arg_a, const void *arg_b)
{
struct dupitem_s *a = (struct dupitem_s *)arg_a;
struct dupitem_s *b = (struct dupitem_s *)arg_b;
return memcmp (a->digest, b->digest, 20);
}
@ -581,7 +574,7 @@ _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp)
char fprbuf[3*20+1];
(void)print_them;
memset (zerodigest, 0, sizeof zerodigest);
if (!(fp = open_file (&filename, outfp)))
@ -601,7 +594,7 @@ _keybox_dump_find_dups (const char *filename, int print_them, FILE *outfp)
while ( !(rc = _keybox_read_blob (&blob, fp)) )
{
unsigned char digest[20];
if (hash_blob_rawdata (blob, digest))
fprintf (outfp, "error in blob %ld of `%s'\n", recno, filename);
else if (memcmp (digest, zerodigest, 20))
@ -668,7 +661,7 @@ _keybox_dump_cut_records (const char *filename, unsigned long from,
KEYBOXBLOB blob;
int rc;
unsigned long recno = 0;
if (!(fp = open_file (&filename, stderr)))
return gpg_error_from_syserror ();

View file

@ -34,6 +34,7 @@
#include "keybox-defs.h"
#include <gcrypt.h>
#include "../include/host2net.h"
enum packet_types
@ -119,10 +120,8 @@ next_packet (unsigned char const **bufptr, size_t *buflen,
{
if (len <4 )
return gpg_error (GPG_ERR_INV_PACKET); /* No length bytes. */
pktlen = (*buf++) << 24;
pktlen |= (*buf++) << 16;
pktlen |= (*buf++) << 8;
pktlen |= (*buf++);
pktlen = buf32_to_ulong (buf);
buf += 4;
len -= 4;
}
else /* Partial length encoding is not allowed for key packets. */

View file

@ -25,6 +25,7 @@
#include <errno.h>
#include "../jnlib/stringhelp.h" /* ascii_xxxx() */
#include "../include/host2net.h"
#include "keybox-defs.h"
#include <gcrypt.h>
@ -45,21 +46,13 @@ struct sn_array_s {
static inline ulong
get32 (const byte *buffer)
{
ulong a;
a = *buffer << 24;
a |= buffer[1] << 16;
a |= buffer[2] << 8;
a |= buffer[3];
return a;
return buf32_to_ulong (buffer);
}
static inline ulong
get16 (const byte *buffer)
{
ulong a;
a = *buffer << 8;
a |= buffer[1];
return a;
return buf16_to_ulong (buffer);
}
@ -112,7 +105,7 @@ _keybox_get_flag_location (const unsigned char *buffer, size_t length,
*flag_off = 6;
*flag_size = 2;
break;
case KEYBOX_FLAG_OWNERTRUST:
case KEYBOX_FLAG_VALIDITY:
case KEYBOX_FLAG_CREATED_AT:
@ -127,7 +120,7 @@ _keybox_get_flag_location (const unsigned char *buffer, size_t length,
if (pos+2 > length)
return GPG_ERR_INV_OBJ; /* Out of bounds. */
/* Serial number. */
nserial = get16 (buffer+pos);
nserial = get16 (buffer+pos);
pos += 2 + nserial;
if (pos+4 > length)
return GPG_ERR_INV_OBJ; /* Out of bounds. */
@ -135,7 +128,7 @@ _keybox_get_flag_location (const unsigned char *buffer, size_t length,
nuids = get16 (buffer + pos); pos += 2;
uidinfolen = get16 (buffer + pos); pos += 2;
if (uidinfolen < 12 )
return GPG_ERR_INV_OBJ;
return GPG_ERR_INV_OBJ;
pos += uidinfolen*nuids;
if (pos+4 > length)
return GPG_ERR_INV_OBJ ; /* Out of bounds. */
@ -143,7 +136,7 @@ _keybox_get_flag_location (const unsigned char *buffer, size_t length,
nsigs = get16 (buffer + pos); pos += 2;
siginfolen = get16 (buffer + pos); pos += 2;
if (siginfolen < 4 )
return GPG_ERR_INV_OBJ;
return GPG_ERR_INV_OBJ;
pos += siginfolen*nsigs;
if (pos+1+1+2+4+4+4+4 > length)
return GPG_ERR_INV_OBJ ; /* Out of bounds. */
@ -190,7 +183,7 @@ get_flag_from_image (const unsigned char *buffer, size_t length,
case 4: *value = get32 (buffer + pos); break;
default: ec = GPG_ERR_BUG; break;
}
return ec;
}
@ -218,7 +211,7 @@ blob_cmp_sn (KEYBOXBLOB blob, const unsigned char *sn, int snlen)
return 0; /* out of bounds */
/*serial*/
nserial = get16 (buffer+pos);
nserial = get16 (buffer+pos);
off = pos + 2;
if (off+nserial > length)
return 0; /* out of bounds */
@ -316,7 +309,7 @@ blob_cmp_name (KEYBOXBLOB blob, int idx,
return 0; /* out of bounds */
/*serial*/
nserial = get16 (buffer+pos);
nserial = get16 (buffer+pos);
pos += 2 + nserial;
if (pos+4 > length)
return 0; /* out of bounds */
@ -332,7 +325,7 @@ blob_cmp_name (KEYBOXBLOB blob, int idx,
if (idx < 0)
{ /* compare all names starting with that (negated) index */
idx = -idx;
for ( ;idx < nuids; idx++)
{
size_t mypos = pos;
@ -409,7 +402,7 @@ blob_cmp_mail (KEYBOXBLOB blob, const char *name, size_t namelen, int substr)
return 0; /* out of bounds */
/*serial*/
nserial = get16 (buffer+pos);
nserial = get16 (buffer+pos);
pos += 2 + nserial;
if (pos+4 > length)
return 0; /* out of bounds */
@ -428,7 +421,7 @@ blob_cmp_mail (KEYBOXBLOB blob, const char *name, size_t namelen, int substr)
for (idx=1 ;idx < nuids; idx++)
{
size_t mypos = pos;
mypos += idx*uidinfolen;
off = get32 (buffer+mypos);
len = get32 (buffer+mypos+4);
@ -439,7 +432,7 @@ blob_cmp_mail (KEYBOXBLOB blob, const char *name, size_t namelen, int substr)
len--; /* one back */
if ( len < 3 || buffer[off+len] != '>')
continue; /* not a proper email address */
len--;
len--;
if (substr)
{
if (ascii_memcasemem (buffer+off+1, len, name, namelen))
@ -474,7 +467,7 @@ blob_x509_has_grip (KEYBOXBLOB blob, const unsigned char *grip)
unsigned char array[20];
unsigned char *rcp;
size_t n;
buffer = _keybox_get_blob_image (blob, &length);
if (length < 40)
return 0; /* Too short. */
@ -527,7 +520,7 @@ blob_x509_has_grip (KEYBOXBLOB blob, const unsigned char *grip)
/*
The has_foo functions are used as helpers for search
The has_foo functions are used as helpers for search
*/
static inline int
has_short_kid (KEYBOXBLOB blob, const unsigned char *kid)
@ -585,7 +578,7 @@ has_issuer_sn (KEYBOXBLOB blob, const char *name,
return 0;
namelen = strlen (name);
return (blob_cmp_sn (blob, sn, snlen)
&& blob_cmp_name (blob, 0 /* issuer */, name, namelen, 0));
}
@ -664,7 +657,7 @@ release_sn_array (struct sn_array_s *array, size_t size)
*/
int
int
keybox_search_reset (KEYBOX_HANDLE hd)
{
if (!hd)
@ -683,13 +676,13 @@ keybox_search_reset (KEYBOX_HANDLE hd)
}
hd->error = 0;
hd->eof = 0;
return 0;
return 0;
}
/* Note: When in ephemeral mode the search function does visit all
blobs but in standard mode, blobs flagged as ephemeral are ignored. */
int
int
keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
{
int rc;
@ -708,18 +701,18 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
hd->found.blob = NULL;
}
if (hd->error)
if (hd->error)
return hd->error; /* still in error state */
if (hd->eof)
if (hd->eof)
return -1; /* still EOF */
/* figure out what information we need */
need_words = any_skip = 0;
for (n=0; n < ndesc; n++)
for (n=0; n < ndesc; n++)
{
switch (desc[n].mode)
switch (desc[n].mode)
{
case KEYDB_SEARCH_MODE_WORDS:
case KEYDB_SEARCH_MODE_WORDS:
need_words = 1;
break;
case KEYDB_SEARCH_MODE_FIRST:
@ -729,7 +722,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
default:
break;
}
if (desc[n].skipfnc)
if (desc[n].skipfnc)
any_skip = 1;
if (desc[n].snlen == -1 && !sn_array)
{
@ -762,7 +755,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
int i, odd;
size_t snlen;
for (n=0; n < ndesc; n++)
for (n=0; n < ndesc; n++)
{
if (!desc[n].sn)
;
@ -830,14 +823,14 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
if (!hd->ephemeral && (blobflags & 2))
continue; /* Not in ephemeral mode but blob is flagged ephemeral. */
for (n=0; n < ndesc; n++)
for (n=0; n < ndesc; n++)
{
switch (desc[n].mode)
{
case KEYDB_SEARCH_MODE_NONE:
case KEYDB_SEARCH_MODE_NONE:
never_reached ();
break;
case KEYDB_SEARCH_MODE_EXACT:
case KEYDB_SEARCH_MODE_EXACT:
if (has_subject_or_alt (blob, desc[n].u.name, 0))
goto found;
break;
@ -854,7 +847,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
goto found;
break;
case KEYDB_SEARCH_MODE_MAILEND:
case KEYDB_SEARCH_MODE_WORDS:
case KEYDB_SEARCH_MODE_WORDS:
never_reached (); /* not yet implemented */
break;
case KEYDB_SEARCH_MODE_ISSUER:
@ -876,7 +869,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
if (has_subject (blob, desc[n].u.name))
goto found;
break;
case KEYDB_SEARCH_MODE_SHORT_KID:
case KEYDB_SEARCH_MODE_SHORT_KID:
if (has_short_kid (blob, desc[n].u.kid))
goto found;
break;
@ -893,20 +886,20 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
if (has_keygrip (blob, desc[n].u.grip))
goto found;
break;
case KEYDB_SEARCH_MODE_FIRST:
case KEYDB_SEARCH_MODE_FIRST:
goto found;
break;
case KEYDB_SEARCH_MODE_NEXT:
case KEYDB_SEARCH_MODE_NEXT:
goto found;
break;
default:
default:
rc = gpg_error (GPG_ERR_INV_VALUE);
goto found;
}
}
continue;
found:
for (n=any_skip?0:ndesc; n < ndesc; n++)
found:
for (n=any_skip?0:ndesc; n < ndesc; n++)
{
/* if (desc[n].skipfnc */
/* && desc[n].skipfnc (desc[n].skipfncvalue, aki)) */
@ -915,7 +908,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
if (n == ndesc)
break; /* got it */
}
if (!rc)
{
hd->found.blob = blob;
@ -925,7 +918,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
_keybox_release_blob (blob);
hd->eof = 1;
}
else
else
{
_keybox_release_blob (blob);
hd->error = rc;

View file

@ -26,6 +26,7 @@
#include <unistd.h>
#include "keybox-defs.h"
#include "../include/host2net.h"
#define EXTSEP_S "."
@ -65,12 +66,12 @@ fseeko (FILE * stream, off_t newpos, int whence)
static int
create_tmp_file (const char *template,
char **r_bakfname, char **r_tmpfname, FILE **r_fp)
{
{
char *bakfname, *tmpfname;
*r_bakfname = NULL;
*r_tmpfname = NULL;
# ifdef USE_ONLY_8DOT3
/* Here is another Windoze bug?:
* you cant rename("pubring.kbx.tmp", "pubring.kbx");
@ -87,7 +88,7 @@ create_tmp_file (const char *template,
return gpg_error_from_syserror ();
strcpy (bakfname, template);
strcpy (bakfname+strlen(template)-4, EXTSEP_S "kb_");
tmpfname = xtrymalloc (strlen (template) + 1);
if (!tmpfname)
{
@ -98,14 +99,14 @@ create_tmp_file (const char *template,
strcpy (tmpfname,template);
strcpy (tmpfname + strlen (template)-4, EXTSEP_S "k__");
}
else
else
{ /* File does not end with kbx, thus we hope we are working on a
modern file system and appending a suffix works. */
bakfname = xtrymalloc ( strlen (template) + 5);
if (!bakfname)
return gpg_error_from_syserror ();
strcpy (stpcpy (bakfname, template), EXTSEP_S "kb_");
tmpfname = xtrymalloc ( strlen (template) + 5);
if (!tmpfname)
{
@ -120,7 +121,7 @@ create_tmp_file (const char *template,
if (!bakfname)
return gpg_error_from_syserror ();
strcpy (stpcpy (bakfname,template),"~");
tmpfname = xtrymalloc ( strlen (template) + 5);
if (!tmpfname)
{
@ -172,7 +173,7 @@ rename_tmp_file (const char *bakfname, const char *tmpfname,
/* First make a backup file except for secret keyboxes. */
if (!secret)
{
{
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
remove (bakfname);
#endif
@ -181,7 +182,7 @@ rename_tmp_file (const char *bakfname, const char *tmpfname,
return gpg_error_from_syserror ();
}
}
/* Then rename the file. */
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
remove (fname);
@ -199,7 +200,7 @@ rename_tmp_file (const char *bakfname, const char *tmpfname,
}
return rc;
}
return 0;
}
@ -211,7 +212,7 @@ rename_tmp_file (const char *bakfname, const char *tmpfname,
3 = update
*/
static int
blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
int secret, off_t start_offset)
{
FILE *fp, *newfp;
@ -221,14 +222,14 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
char buffer[4096];
int nread, nbytes;
/* Open the source file. Because we do a rename, we have to check the
/* Open the source file. Because we do a rename, we have to check the
permissions of the file */
if (access (fname, W_OK))
return gpg_error_from_syserror ();
fp = fopen (fname, "rb");
if (mode == 1 && !fp && errno == ENOENT)
{
{
/* Insert mode but file does not exist:
Create a new keybox file. */
newfp = fopen (fname, "wb");
@ -274,10 +275,10 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
fclose (newfp);
goto leave;
}
/* prepare for insert */
if (mode == 1)
{
{
/* Copy everything to the new file. */
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 )
{
@ -297,12 +298,12 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
goto leave;
}
}
/* Prepare for delete or update. */
if ( mode == 2 || mode == 3 )
{
if ( mode == 2 || mode == 3 )
{
off_t current = 0;
/* Copy first part to the new file. */
while ( current < start_offset )
{
@ -313,7 +314,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
if (!nread)
break;
current += nread;
if (fwrite (buffer, nread, 1, newfp) != 1)
{
rc = gpg_error_from_syserror ();
@ -329,7 +330,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
fclose (newfp);
goto leave;
}
/* Skip this blob. */
rc = _keybox_read_blob (NULL, fp);
if (rc)
@ -339,10 +340,10 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
return rc;
}
}
/* Do an insert or update. */
if ( mode == 1 || mode == 3 )
{
{
rc = _keybox_write_blob (blob, newfp);
if (rc)
{
@ -351,10 +352,10 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
return rc;
}
}
/* Copy the rest of the packet for an delete or update. */
if (mode == 2 || mode == 3)
{
{
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 )
{
if (fwrite (buffer, nread, 1, newfp) != 1)
@ -373,7 +374,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
goto leave;
}
}
/* Close both files. */
if (fclose(fp))
{
@ -397,7 +398,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
#ifdef KEYBOX_WITH_X509
#ifdef KEYBOX_WITH_X509
int
keybox_insert_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
unsigned char *sha1_digest)
@ -407,12 +408,12 @@ keybox_insert_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
KEYBOXBLOB blob;
if (!hd)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
if (!hd->kb)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
fname = hd->kb->fname;
if (!fname)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
/* Close this one otherwise we will mess up the position for a next
search. Fixme: it would be better to adjust the position after
@ -466,12 +467,12 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
if (!hd->found.blob)
return gpg_error (GPG_ERR_NOTHING_FOUND);
if (!hd->kb)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
if (!hd->found.blob)
return gpg_error (GPG_ERR_NOTHING_FOUND);
fname = hd->kb->fname;
if (!fname)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
off = _keybox_get_blob_fileoffset (hd->found.blob);
if (off == (off_t)-1)
@ -481,7 +482,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
ec = _keybox_get_flag_location (buffer, length, what, &flag_pos, &flag_size);
if (ec)
return gpg_error (ec);
off += flag_pos;
_keybox_close_file (hd);
@ -503,7 +504,7 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
switch (flag_size)
{
case 1:
case 1:
case 2:
case 4:
if (fwrite (tmp+4-flag_size, flag_size, 1, fp) != 1)
@ -539,10 +540,10 @@ keybox_delete (KEYBOX_HANDLE hd)
if (!hd->found.blob)
return gpg_error (GPG_ERR_NOTHING_FOUND);
if (!hd->kb)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
fname = hd->kb->fname;
if (!fname)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
off = _keybox_get_blob_fileoffset (hd->found.blob);
if (off == (off_t)-1)
@ -588,18 +589,18 @@ keybox_compress (KEYBOX_HANDLE hd)
int skipped_deleted;
if (!hd)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
if (!hd->kb)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
if (hd->secret)
return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
fname = hd->kb->fname;
if (!fname)
return gpg_error (GPG_ERR_INV_HANDLE);
return gpg_error (GPG_ERR_INV_HANDLE);
_keybox_close_file (hd);
/* Open the source file. Because we do a rename, we have to check the
/* Open the source file. Because we do a rename, we have to check the
permissions of the file */
if (access (fname, W_OK))
return gpg_error_from_syserror ();
@ -623,9 +624,8 @@ keybox_compress (KEYBOX_HANDLE hd)
buffer = _keybox_get_blob_image (blob, &length);
if (length > 4 && buffer[4] == BLOBTYPE_HEADER)
{
u32 last_maint = ((buffer[20] << 24) | (buffer[20+1] << 16)
| (buffer[20+2] << 8) | (buffer[20+3]));
u32 last_maint = buf32_to_u32 (buffer+20);
if ( (last_maint + 3*3600) > time (NULL) )
{
fclose (fp);
@ -645,7 +645,7 @@ keybox_compress (KEYBOX_HANDLE hd)
return rc;;
}
/* Processing loop. By reading using _keybox_read_blob we
automagically skip any blobs flagged as deleted. Thus what we
only have to do is to check all ephemeral flagged blocks whether
@ -690,24 +690,23 @@ keybox_compress (KEYBOX_HANDLE hd)
continue;
}
if (_keybox_get_flag_location (buffer, length,
if (_keybox_get_flag_location (buffer, length,
KEYBOX_FLAG_BLOB, &pos, &size)
|| size != 2)
{
rc = gpg_error (GPG_ERR_BUG);
break;
}
blobflags = ((buffer[pos] << 8) | (buffer[pos+1]));
blobflags = buf16_to_uint (buffer+pos);
if ((blobflags & KEYBOX_FLAG_BLOB_EPHEMERAL))
{
/* This is an ephemeral blob. */
if (_keybox_get_flag_location (buffer, length,
if (_keybox_get_flag_location (buffer, length,
KEYBOX_FLAG_CREATED_AT, &pos, &size)
|| size != 4)
created_at = 0; /* oops. */
else
created_at = ((buffer[pos] << 24) | (buffer[pos+1] << 16)
| (buffer[pos+2] << 8) | (buffer[pos+3]));
created_at = buf32_to_u32 (buffer+pos);
if (created_at && created_at < cut_time)
{