aboutsummaryrefslogtreecommitdiffstats
path: root/kbx/keybox-search.c
blob: d2c61ff2119dd078d58fb96aa34b0d10cf4f5e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* keybox-search.c - Search operations
 *	Copyright (C) 2001 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "keybox-defs.h"


/****************
 * Check whether the given fingerprint (20 bytes) is in the
 * given keyblob.  fpr is always 20 bytes.
 * Return: 0 = found
 *	   -1 = not found
	  other = error  (fixme: do not always reurn gpgerr_general)
 */
int
keybox_blob_has_fpr ( KEYBOXBLOB blob, const byte *fpr )
{
    ulong n, nkeys, keyinfolen;
    const byte *p, *pend;
    byte *buffer = blob->blob;
    size_t buflen = blob->bloblen;

    if ( buflen < 40 )
	return GPGERR_GENERAL; /* blob too short */
    n = get32( buffer );
    if ( n > buflen )
	return GPGERR_GENERAL; /* blob larger than announced length */
    buflen = n;  /* ignore trailing stuff */
    pend = buffer + n - 1;

    if ( buffer[4] != 2 )
	return GPGERR_GENERAL; /* invalid blob type */
    if ( buffer[5] != 1 )
	return GPGERR_GENERAL; /* invalid blob format version */

    nkeys = get16( buffer + 16 );
    keyinfolen = get16( buffer + 18 );
    p = buffer + 20;
    for(n=0; n < nkeys; n++, p += keyinfolen ) {
	if ( p+20 > pend )
	    return GPGERR_GENERAL; /* blob shorter than required */
	if (!memcmp ( p, fpr, 20 ) )
	    return 0; /* found */
    }
    return -1;
}

/****************
 * Check whether the given keyID (20 bytes) is in the
 * given keyblob.
 * Return: 0 = found
 *	   -1 = not found
	  other = error  (fixme: do not always return gpgerr_general)
 */
int
keybox_blob_has_kid ( KEYBOXBLOB blob, const byte *keyidbuf, size_t keyidlen )
{
    ulong n, nkeys, keyinfolen, off;
    const byte *p, *pend;
    byte *buffer = blob->blob;
    size_t buflen = blob->bloblen;

    if ( buflen < 40 )
	return GPGERR_GENERAL; /* blob too short */
    n = get32( buffer );
    if ( n > buflen )
	return GPGERR_GENERAL; /* blob larger than announced length */
    buflen = n;  /* ignore trailing stuff */
    pend = buffer + n - 1;

    if ( buffer[4] != 2 )
	return GPGERR_GENERAL; /* invalid blob type */
    if ( buffer[5] != 1 )
	return GPGERR_GENERAL; /* invalid blob format version */

    nkeys = get16( buffer + 16 );
    keyinfolen = get16( buffer + 18 );
    p = buffer + 20;
    for(n=0; n < nkeys; n++, p += keyinfolen ) {
	if ( p+24 > pend )
	    return GPGERR_GENERAL; /* blob shorter than required */
	off = get32 ( p + 20 );
	if (keyidlen < 8 ) /* actually keyidlen may either be 4 or 8 */
	    off +=4;
	if ( off+keyidlen > buflen )
	    return GPGERR_GENERAL; /* offset out of bounds */
	if ( !memcmp ( buffer+off, keyidbuf, keyidlen ) )
	    return 0; /* found */
    }
    return -1;
}



int
keybox_blob_has_uid ( KEYBOXBLOB blob,
		   int (*cmp)(const byte *, size_t, void *), void *opaque )
{
    ulong n, nuids, uidinfolen, off, len;
    const byte *p, *pend;
    byte *buffer = blob->blob;
    size_t buflen = blob->bloblen;

    if ( buflen < 40 )
	return GPGERR_GENERAL; /* blob too short */
    n = get32( buffer );
    if ( n > buflen )
	return GPGERR_GENERAL; /* blob larger than announced length */
    buflen = n;  /* ignore trailing stuff */
    pend = buffer + n - 1;

    if ( buffer[4] != 2 )
	return GPGERR_GENERAL; /* invalid blob type */
    if ( buffer[5] != 1 )
	return GPGERR_GENERAL; /* invalid blob format version */

    p = buffer + 20 + get16( buffer + 16 ) * get16( buffer + 18 );
    if ( p+4 > pend )
	return GPGERR_GENERAL; /* blob shorter than required */

    nuids = get16( p ); p+= 2;
    uidinfolen = get16( p ); p+=2;
    for(n=0; n < nuids; n++, p += uidinfolen ) {
	if ( p+8 > pend )
	    return GPGERR_GENERAL; /* blob shorter than required */
	off = get32 ( p );
	len = get32 ( p + 4 );
	if ( off+len > buflen )
	    return GPGERR_GENERAL; /* offset out of bounds */
	if ( (*cmp) ( buffer+off, len, opaque ) )
	    return 0; /* found */
    }

    return -1;
}