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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
* Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Niels Provos.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* handle_value_request:
* receive a VALUE_REQUEST packet; return -1 on failure, 0 on success
*
*/
#ifndef lint
static char rcsid[] = "$Id: handle_value_request.c,v 1.4 2000/12/11 21:21:17 provos Exp $";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <ssl/bn.h>
#include "config.h"
#include "photuris.h"
#include "packets.h"
#include "state.h"
#include "cookie.h"
#include "config.h"
#include "buffer.h"
#include "scheme.h"
#include "packet.h"
#include "exchange.h"
#include "secrets.h"
#include "server.h"
#include "log.h"
int
handle_value_request(u_char *packet, int size,
char *address, u_short port,
u_int8_t *schemes, u_int16_t ssize)
{
struct packet_sub parts[] = {
{ "Exchange Value", FLD_VARPRE, 0, 0, },
{ "Offered Attributes", FLD_ATTRIB, FMD_ATT_FILL, 0, },
{ NULL }
};
struct packet vr_msg = {
"Value Request",
VALUE_REQUEST_MIN, 0, parts
};
struct value_request *header;
struct stateob *st;
BIGNUM *test, *gen, *mod;
u_int8_t *p, *modp, *refp, *genp = NULL;
size_t sstart, vsize, modsize, modpsize, refpsize;
int modflag;
u_int8_t scheme_ref[2];
u_int8_t rcookie[COOKIE_SIZE];
if (size < VALUE_REQUEST_MIN)
return -1; /* packet too small */
if (packet_check(packet, size, &vr_msg) == -1) {
log_print("bad packet structure in handle_value_request()");
return -1;
}
header = (struct value_request *) packet;
st = state_find_cookies(address, header->icookie, header->rcookie);
if (st == NULL) {
struct stateob tempst;
bzero((char *)&tempst, sizeof(tempst)); /* Set up temp. state */
tempst.initiator = 0; /* We are the Responder */
bcopy(header->icookie, tempst.icookie, COOKIE_SIZE);
strncpy(tempst.address, address, 15);
tempst.port = global_port;
tempst.counter = header->counter;
cookie_generate(&tempst, rcookie, COOKIE_SIZE, schemes, ssize);
/* Check for invalid cookie */
if (bcmp(rcookie, header->rcookie, COOKIE_SIZE)) {
packet_size = PACKET_BUFFER_SIZE;
photuris_error_message(&tempst, packet_buffer, &packet_size,
header->icookie, header->rcookie,
header->counter, BAD_COOKIE);
send_packet();
return 0;
}
/* Check exchange value - XXX doesn't check long form */
vsize = parts[0].size;
/* Check schemes - selected length is in exchange value*/
sstart = 0;
modflag = 0;
refp = modp = NULL;
*(u_int16_t *)scheme_ref = htons(scheme_get_ref(header->scheme));
while (sstart < ssize) {
p = scheme_get_mod(schemes + sstart);
modsize = varpre2octets(p);
if (!bcmp(header->scheme, schemes + sstart, 2)) {
modflag = 1;
if (modsize == vsize) {
genp = scheme_get_gen(schemes+sstart);
modp = p;
modpsize = modsize;
break; /* On right scheme + right size */
} else if (modsize <= 2 && refp != NULL) {
modp = refp;
modpsize = refpsize;
break;
}
} else if (!bcmp(scheme_ref, schemes + sstart, 2) &&
modsize == vsize) {
genp = scheme_get_gen(schemes + sstart);
if (modflag) {
modp = p;
modpsize = modsize;
break;
}
refp = p;
refpsize = modsize;
}
sstart += scheme_get_len(schemes+sstart);
}
if (sstart >= ssize)
return -1; /* Did not find a scheme - XXX log */
/* now check the exchange value */
test = BN_new();
if (BN_varpre2bn(parts[0].where, parts[0].size, test) == NULL) {
BN_free(test);
return (-1);
}
mod = BN_new();
if (BN_varpre2bn(modp, modpsize, mod) == NULL) {
BN_free(test);
BN_free(mod);
return (-1);
}
gen = BN_new();
if (exchange_set_generator(gen, header->scheme, genp) == -1 ||
!exchange_check_value(test, gen, mod)) {
BN_free(test);
BN_free(gen);
BN_free(mod);
return 0;
}
BN_free(test);
BN_free(gen);
BN_free(mod);
if ((st = state_new()) == NULL)
return -1;
/* Default options */
st->flags = IPSEC_OPT_ENC|IPSEC_OPT_AUTH;
/* Fill the state object */
st->uSPIoattrib = calloc(parts[1].size, sizeof(u_int8_t));
if (st->uSPIoattrib == NULL) {
state_value_reset(st);
return -1;
}
bcopy(parts[1].where, st->uSPIoattrib, parts[1].size);
st->uSPIoattribsize = parts[1].size;
/* Save scheme, which will be used by both parties */
vsize = 2 + varpre2octets(modp);
/* XXX - VPN - only support two octets */
if (genp != NULL)
vsize += 2 + varpre2octets(genp);
st->scheme = calloc(vsize, sizeof(u_int8_t));
if (st->scheme == NULL) {
state_value_reset(st);
return -1;
}
bcopy(header->scheme, st->scheme, 2);
if (genp != NULL) {
st->scheme[2] = (vsize-4) >> 8;
st->scheme[3] = (vsize-4) & 0xFF;
bcopy(genp, st->scheme+2+2, varpre2octets(genp));
}
bcopy(modp, st->scheme + 2 + (genp == NULL ? 0 : 2 + varpre2octets(genp)),
varpre2octets(modp));;
st->schemesize = vsize;
#ifdef DEBUG
{
int i = BUFFER_SIZE;
bin2hex(buffer, &i, parts[0].where, varpre2octets(VALUE_REQUEST_VALUE(header)));
printf("Got exchange value 0x%s\n", buffer);
}
#endif
/* Set exchange value */
st->texchangesize = parts[0].size;
st->texchange = calloc(st->texchangesize, sizeof(u_int8_t));
if (st->texchange == NULL) {
log_error("calloc() in handle_value_request()");
return -1;
}
bcopy(parts[0].where, st->texchange, st->texchangesize);
/* Fill in the state object with generic data */
strncpy(st->address, address, 15);
st->port = port;
st->counter = header->counter;
bcopy(header->icookie, st->icookie, COOKIE_SIZE);
bcopy(header->rcookie, st->rcookie, COOKIE_SIZE);
bcopy(&header->counter, st->uSPITBV, 3);
if ((st->roschemes = calloc(ssize, sizeof(u_int8_t))) == NULL) {
state_value_reset(st);
return -1;
}
bcopy(schemes, st->roschemes, ssize);
st->roschemesize = ssize;
if (pick_attrib(st, &(st->oSPIoattrib),
&(st->oSPIoattribsize)) == -1) {
state_value_reset(st);
return -1;
}
st->lifetime = exchange_timeout + time(NULL);
/* Now put the filled state object in the chain */
state_insert(st);
}
packet_size = PACKET_BUFFER_SIZE;
if (photuris_value_response(st, packet_buffer, &packet_size) == -1)
return -1;
send_packet();
/* Compute the shared secret now */
compute_shared_secret(st, &(st->shared), &(st->sharedsize));
#ifdef DEBUG
{
int i = BUFFER_SIZE;
bin2hex(buffer, &i, st->shared, st->sharedsize);
printf("Shared secret is: 0x%s\n", buffer);
}
#endif
if (st->oSPIprivacyctx == NULL) {
/* Initialize Privacy Keys from Exchange Values */
init_privacy_key(st, 0); /* User -> Owner direction */
init_privacy_key(st, 1); /* Owner -> User direction */
}
st->retries = 0;
st->phase = VALUE_RESPONSE;
return 0;
}
|