summaryrefslogtreecommitdiffstats
path: root/kbx
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2001-12-11 13:28:17 +0100
committerWerner Koch <wk@gnupg.org>2001-12-11 13:28:17 +0100
commitf463880562fd22d9900465b8c208cb73fc197669 (patch)
tree760295f98968ab5038480f8be4c47072bbad78b9 /kbx
parentnew error codes (diff)
downloadgnupg2-f463880562fd22d9900465b8c208cb73fc197669.tar.xz
gnupg2-f463880562fd22d9900465b8c208cb73fc197669.zip
New searchmode and hack to allow a hex encoded serial number
Diffstat (limited to 'kbx')
-rw-r--r--kbx/keybox-search-desc.h8
-rw-r--r--kbx/keybox-search.c108
2 files changed, 112 insertions, 4 deletions
diff --git a/kbx/keybox-search-desc.h b/kbx/keybox-search-desc.h
index 88ffde8d0..84be20f18 100644
--- a/kbx/keybox-search-desc.h
+++ b/kbx/keybox-search-desc.h
@@ -42,6 +42,7 @@ typedef enum {
KEYDB_SEARCH_MODE_FPR,
KEYDB_SEARCH_MODE_ISSUER,
KEYDB_SEARCH_MODE_ISSUER_SN,
+ KEYDB_SEARCH_MODE_SN,
KEYDB_SEARCH_MODE_SUBJECT,
KEYDB_SEARCH_MODE_FIRST,
KEYDB_SEARCH_MODE_NEXT
@@ -51,11 +52,12 @@ struct keydb_search_desc {
KeydbSearchMode mode;
int (*skipfnc)(void *,void*); /* used to be: void*, u32* */
void *skipfncvalue;
- const unsigned char *sn; /* used only with _MODE_ISSUER_SN */
+ const unsigned char *sn;
+ int sn_is_string; /* very ugly */
union {
const char *name;
- char fpr[24];
- /*fixme: u32 kid[2];*/
+ unsigned char fpr[24];
+ unsigned char kid[8];
} u;
};
diff --git a/kbx/keybox-search.c b/kbx/keybox-search.c
index 2a4ceca94..23543ec66 100644
--- a/kbx/keybox-search.c
+++ b/kbx/keybox-search.c
@@ -22,9 +22,15 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <assert.h>
#include "keybox-defs.h"
+#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
+ *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
+#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
+
+
static ulong
get32 (const byte *buffer)
{
@@ -206,6 +212,16 @@ has_issuer_sn (KEYBOXBLOB blob, const char *name, const unsigned char *sn)
}
static int
+has_sn (KEYBOXBLOB blob, const unsigned char *sn)
+{
+ return_val_if_fail (sn, 0);
+
+ if (blob_get_type (blob) != BLOBTYPE_X509)
+ return 0;
+ return blob_cmp_sn (blob, sn);
+}
+
+static int
has_subject (KEYBOXBLOB blob, const char *name)
{
size_t namelen;
@@ -220,6 +236,16 @@ has_subject (KEYBOXBLOB blob, const char *name)
}
+static void
+release_sn_array (unsigned char **array, size_t size)
+{
+ size_t n;
+
+ for (n=0; n < size; n++)
+ xfree (array[n]);
+ xfree (array);
+}
+
/*
@@ -256,6 +282,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
size_t n;
int need_words, any_skip;
KEYBOXBLOB blob = NULL;
+ unsigned char **sn_array = NULL;
if (!hd)
return KEYBOX_Invalid_Value;
@@ -290,13 +317,84 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
}
if (desc[n].skipfnc)
any_skip = 1;
+ if (desc[n].sn_is_string && !sn_array)
+ {
+ sn_array = xtrycalloc (ndesc, sizeof *sn_array);
+ if (!sn_array)
+ return (hd->error = KEYBOX_Out_Of_Core);
+ }
}
if (!hd->fp)
{
hd->fp = fopen (hd->kb->fname, "rb");
if (!hd->fp)
+ {
+ xfree (sn_array);
return (hd->error = KEYBOX_File_Open_Error);
+ }
+ }
+
+ /* kludge: we need to convert an SN given as hexstring to it's
+ binary representation - in some cases we are not able to store it
+ in the search descriptor, because due to its usgae it is not
+ possible to free allocated memory */
+ if (sn_array)
+ {
+ const unsigned char *s;
+ int i, odd;
+ size_t snlen;
+
+ for (n=0; n < ndesc; n++)
+ {
+ if (!desc[n].sn)
+ ;
+ else if (desc[n].sn_is_string)
+ {
+ unsigned char *sn;
+
+ s = desc[n].sn;
+ for (i=0; *s && *s != '/'; s++, i++)
+ ;
+ odd = (i & 1);
+ snlen = (i+1)/2;
+ sn_array[n] = xtrymalloc (4+snlen);
+ if (!sn_array[n])
+ {
+ release_sn_array (sn_array, n);
+ return (hd->error = KEYBOX_Out_Of_Core);
+ }
+ sn = sn_array[n] + 4;
+ s = desc[n].sn;
+ if (odd)
+ {
+ *sn++ = xtoi_1 (s);
+ s++;
+ }
+ for (; *s && *s != '/'; s += 2)
+ *sn++ = xtoi_2 (s);
+ assert (sn - sn_array[n] == 4+snlen);
+ sn = sn_array[n];
+ sn[0] = snlen >> 24;
+ sn[1] = snlen >> 16;
+ sn[2] = snlen >> 8;
+ sn[3] = snlen;
+ }
+ else
+ {
+ const unsigned char *sn;
+
+ sn = desc[n].sn;
+ snlen = (sn[0] << 24) | (sn[1] << 16) | (sn[2] << 8) | sn[3];
+ sn_array[n] = xtrymalloc (4+snlen);
+ if (!sn_array[n])
+ {
+ release_sn_array (sn_array, n);
+ return (hd->error = KEYBOX_Out_Of_Core);
+ }
+ memcpy (sn_array[n], sn, 4+snlen);
+ }
+ }
}
@@ -327,7 +425,12 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
goto found;
break;
case KEYDB_SEARCH_MODE_ISSUER_SN:
- if (has_issuer_sn (blob, desc[n].u.name, desc[n].sn))
+ if (has_issuer_sn (blob, desc[n].u.name,
+ sn_array? sn_array[n] : desc[n].sn))
+ goto found;
+ break;
+ case KEYDB_SEARCH_MODE_SN:
+ if (has_sn (blob, sn_array? sn_array[n] : desc[n].sn))
goto found;
break;
case KEYDB_SEARCH_MODE_SUBJECT:
@@ -384,6 +487,9 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc)
hd->error = rc;
}
+ if (sn_array)
+ release_sn_array (sn_array, ndesc);
+
return rc;
}