diff options
Diffstat (limited to 'kbx/keybox-search.c')
-rw-r--r-- | kbx/keybox-search.c | 85 |
1 files changed, 39 insertions, 46 deletions
diff --git a/kbx/keybox-search.c b/kbx/keybox-search.c index 1680dd732..4b013ce7b 100644 --- a/kbx/keybox-search.c +++ b/kbx/keybox-search.c @@ -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; |