diff options
author | DongHun Kwak <dh0128.kwak@samsung.com> | 2021-02-09 15:59:56 +0900 |
---|---|---|
committer | DongHun Kwak <dh0128.kwak@samsung.com> | 2021-02-09 15:59:56 +0900 |
commit | c399f09830e89ea0ae248ed78502f6c0e6b632c1 (patch) | |
tree | 4a6c4ceb1045e6d87cfa2b6094ff2a9c661e503e /kbx | |
parent | 979221d85a7560cff764e23c0d97554ab9c3d853 (diff) | |
download | gpg2-c399f09830e89ea0ae248ed78502f6c0e6b632c1.tar.gz gpg2-c399f09830e89ea0ae248ed78502f6c0e6b632c1.tar.bz2 gpg2-c399f09830e89ea0ae248ed78502f6c0e6b632c1.zip |
Imported Upstream version 2.1.2upstream/2.1.2
Diffstat (limited to 'kbx')
-rw-r--r-- | kbx/kbxutil.c | 1 | ||||
-rw-r--r-- | kbx/keybox-dump.c | 22 | ||||
-rw-r--r-- | kbx/keybox-openpgp.c | 8 | ||||
-rw-r--r-- | kbx/keybox-search.c | 58 | ||||
-rw-r--r-- | kbx/keybox-update.c | 46 |
5 files changed, 76 insertions, 59 deletions
diff --git a/kbx/kbxutil.c b/kbx/kbxutil.c index 34cbc53..368c02d 100644 --- a/kbx/kbxutil.c +++ b/kbx/kbxutil.c @@ -452,6 +452,7 @@ main( int argc, char **argv ) unsigned long from = 0, to = ULONG_MAX; int dry_run = 0; + early_system_init (); set_strusage( my_strusage ); gcry_control (GCRYCTL_DISABLE_SECMEM); log_set_prefix ("kbxutil", 1); diff --git a/kbx/keybox-dump.c b/kbx/keybox-dump.c index 5315e84..8815a6f 100644 --- a/kbx/keybox-dump.c +++ b/kbx/keybox-dump.c @@ -25,30 +25,14 @@ #include "keybox-defs.h" #include <gcrypt.h> +#include "host2net.h" /* Argg, we can't include ../common/util.h */ char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf); +#define get32(a) buf32_to_ulong ((a)) +#define get16(a) buf16_to_ulong ((a)) -static ulong -get32 (const byte *buffer) -{ - ulong a; - a = *buffer << 24; - a |= buffer[1] << 16; - a |= buffer[2] << 8; - a |= buffer[3]; - return a; -} - -static ulong -get16 (const byte *buffer) -{ - ulong a; - a = *buffer << 8; - a |= buffer[1]; - return a; -} void print_string (FILE *fp, const byte *p, size_t n, int delim) diff --git a/kbx/keybox-openpgp.c b/kbx/keybox-openpgp.c index 6ae6c44..2cac242 100644 --- a/kbx/keybox-openpgp.c +++ b/kbx/keybox-openpgp.c @@ -36,7 +36,7 @@ #include <gcrypt.h> #include "../common/openpgpdefs.h" - +#include "host2net.h" /* Assume a valid OpenPGP packet at the address pointed to by BUFBTR which has a maximum length as stored at BUFLEN. Return the header @@ -94,10 +94,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. */ diff --git a/kbx/keybox-search.c b/kbx/keybox-search.c index 6e72d0b..d22ef19 100644 --- a/kbx/keybox-search.c +++ b/kbx/keybox-search.c @@ -29,7 +29,7 @@ #include "keybox-defs.h" #include <gcrypt.h> - +#include "host2net.h" #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \ *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10)) @@ -42,27 +42,8 @@ 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; -} - -static inline ulong -get16 (const byte *buffer) -{ - ulong a; - a = *buffer << 8; - a |= buffer[1]; - return a; -} - +#define get32(a) buf32_to_ulong ((a)) +#define get16(a) buf16_to_ulong ((a)) static inline unsigned int @@ -79,6 +60,30 @@ blob_get_blob_flags (KEYBOXBLOB blob) } +/* Return the first keyid from the blob. Returns true if + available. */ +static int +blob_get_first_keyid (KEYBOXBLOB blob, u32 *kid) +{ + const unsigned char *buffer; + size_t length, nkeys, keyinfolen; + + buffer = _keybox_get_blob_image (blob, &length); + if (length < 48) + return 0; /* blob too short */ + + nkeys = get16 (buffer + 16); + keyinfolen = get16 (buffer + 18); + if (!nkeys || keyinfolen < 28) + return 0; /* invalid blob */ + + kid[0] = get32 (buffer + 32); + kid[1] = get32 (buffer + 36); + + return 1; +} + + /* Return information on the flag WHAT within the blob BUFFER,LENGTH. Return the offset and the length (in bytes) of the flag in FLAGOFF,FLAG_SIZE. */ @@ -967,9 +972,12 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc, *r_descindex = n; for (n=any_skip?0:ndesc; n < ndesc; n++) { -/* if (desc[n].skipfnc */ -/* && desc[n].skipfnc (desc[n].skipfncvalue, aki, NULL)) */ -/* break; */ + u32 kid[2]; + + if (desc[n].skipfnc + && blob_get_first_keyid (blob, kid) + && desc[n].skipfnc (desc[n].skipfncvalue, kid, NULL)) + break; } if (n == ndesc) break; /* got it */ diff --git a/kbx/keybox-update.c b/kbx/keybox-update.c index 11861ac..4b14b2f 100644 --- a/kbx/keybox-update.c +++ b/kbx/keybox-update.c @@ -28,6 +28,7 @@ #include "keybox-defs.h" #include "../common/sysutils.h" +#include "../common/host2net.h" #define EXTSEP_S "." @@ -241,11 +242,17 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob, rc = _keybox_write_header_blob (newfp, for_openpgp); if (rc) - return rc; + { + fclose (newfp); + return rc; + } rc = _keybox_write_blob (blob, newfp); if (rc) - return rc; + { + fclose (newfp); + return rc; + } if ( fclose (newfp) ) return gpg_error_from_syserror (); @@ -268,7 +275,8 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob, rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp); if (rc) { - fclose(fp); + fclose (fp); + fclose (newfp); goto leave; } @@ -292,12 +300,16 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob, if (fwrite (buffer, nread, 1, newfp) != 1) { rc = gpg_error_from_syserror (); + fclose (fp); + fclose (newfp); goto leave; } } if (ferror (fp)) { rc = gpg_error_from_syserror (); + fclose (fp); + fclose (newfp); goto leave; } } @@ -321,19 +333,27 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob, if (fwrite (buffer, nread, 1, newfp) != 1) { rc = gpg_error_from_syserror (); + fclose (fp); + fclose (newfp); goto leave; } } if (ferror (fp)) { rc = gpg_error_from_syserror (); + fclose (fp); + fclose (newfp); goto leave; } /* Skip this blob. */ rc = _keybox_read_blob (NULL, fp); if (rc) - return rc; + { + fclose (fp); + fclose (newfp); + return rc; + } } /* Do an insert or update. */ @@ -341,7 +361,11 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob, { rc = _keybox_write_blob (blob, newfp); if (rc) + { + fclose (fp); + fclose (newfp); return rc; + } } /* Copy the rest of the packet for an delete or update. */ @@ -352,12 +376,16 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob, if (fwrite (buffer, nread, 1, newfp) != 1) { rc = gpg_error_from_syserror (); + fclose (fp); + fclose (newfp); goto leave; } } if (ferror (fp)) { rc = gpg_error_from_syserror (); + fclose (fp); + fclose (newfp); goto leave; } } @@ -707,8 +735,7 @@ keybox_compress (KEYBOX_HANDLE hd) buffer = _keybox_get_blob_image (blob, &length); if (length > 4 && buffer[4] == KEYBOX_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) ) { @@ -726,7 +753,7 @@ keybox_compress (KEYBOX_HANDLE hd) rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp); if (rc) { - fclose(fp); + fclose (fp); return rc;; } @@ -784,7 +811,7 @@ keybox_compress (KEYBOX_HANDLE hd) 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. */ @@ -793,8 +820,7 @@ keybox_compress (KEYBOX_HANDLE hd) || 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) { |