summaryrefslogtreecommitdiff
path: root/sbin/photurisd/exchange.c
blob: 40fdd5f013f4b77d206b69444f92275466cae691 (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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*	$OpenBSD: exchange.c,v 1.6 2002/06/09 08:13:08 todd Exp $	*/

/*
 * Copyright 1997-2000 Niels Provos <provos@citi.umich.edu>
 * 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.
 */
/*
 * exchange.c:
 *
 */

#ifndef lint
static char rcsid[] = "$OpenBSD: exchange.c,v 1.6 2002/06/09 08:13:08 todd Exp $";
#endif

#define _EXCHANGE_C_

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ssl/bn.h>

#include "config.h"
#include "state.h"
#include "exchange.h"
#include "modulus.h"
#include "attributes.h"
#include "buffer.h"
#include "cookie.h"
#include "schedule.h"
#include "scheme.h"
#include "log.h"

/*
 * Get the number of bits from a variable precision number
 * according to draft-simpson-photuris-11
 */

u_int8_t *
varpre_get_number_bits(size_t *nbits, u_int8_t *varpre)
{
     int blocks;
     size_t bits;

     if (varpre == NULL)
	  return (NULL);

     /* We don't support numbers, that long */
     if (*varpre == 255 && *(varpre+1) == 255)
	     return (NULL);

     bits = 0;
     if (*varpre == 255) {
	  blocks = 3;
	  bits = 65280;
          varpre++;
     } else
	  blocks = 2;

     while (blocks-- > 0) {
	     bits = (bits << 8) + *varpre;
	     varpre++;
     }

     *nbits = bits;

     return (varpre);
}

/*
 * Convert a variable precision number to a bignum
 */

u_int8_t *
BN_varpre2bn(u_int8_t *varpre, size_t size, BIGNUM *a)
{
     u_int8_t *p;
     size_t bytes;

     BN_zero(a);
     p = varpre_get_number_bits(&bytes, varpre);
     if (p == NULL)
	     return (NULL);

     bytes = (bytes + 7) / 8;

     if (p + bytes != varpre + size)
	     return (NULL);

     while (bytes > 0) {
	  BN_lshift(a, a, 8);
	  BN_add_word(a, *p);

	  bytes--;
	  p++;
     }

     return (p);
}

int
BN_bn2varpre(BIGNUM *p, u_int8_t *value, size_t *size)
{
	size_t bits, bytes;
	int header;
	BIGNUM *a;

	bits = BN_num_bits(p);
	bytes = (bits + 7) / 8;

	/* We only support 4 octets */
	if (bits > 65279) {
		bits -= 65280;
		value[0] = 255;
		value[1] = (bits >> 16) & 0xFF;
		value[2] = (bits >>  8) & 0xFF;
		value[3] =  bits        & 0xFF;
		header = 4;
	} else {
		value[0] = (bits >> 8) & 0xFF;
		value[1] =  bits       & 0xFF;
		header = 2;
	}

	/* Check if the buffer is big enough */
	if (bytes + header > (*size - header))
		return (-1);

	a = BN_new();
	BN_copy(a, p);

	*size = bytes + header;

	while (bytes > 0) {
		bytes--;
		value[bytes + header] = BN_mod_word(a, 256);
		BN_rshift(a, a, 8);
	}
	BN_clear_free(a);

	return (0);
}


int
exchange_check_value(BIGNUM *exchange, BIGNUM *gen, BIGNUM *mod)
{
     size_t bits;
     BIGNUM *test;

     bits = BN_num_bits(mod);
     if (BN_num_bits(exchange) < bits/2)
	  return (0);

     test = BN_new();
     BN_copy(test, mod);
     BN_sub_word(test, 1);
     if (!BN_cmp(exchange, test)) {
	  BN_free(test);
	  return (0);
     }

     /* XXX - more tests need to go here */

     BN_free(test);
     return (1);
}

/*
 * Finds to a given modulus and generator cached information
 * which is used to create the private value and exchange value
 */

int
exchange_make_values(struct stateob *st, BIGNUM *modulus, BIGNUM *generator)
{
     struct moduli_cache *p, *tmp;
     u_int8_t *mod;
     time_t tm;

     tm = time(NULL);

     /* See if we have this cached already */
     if ((p = mod_find_modgen(modulus,generator)) == NULL) {
	  /* Create a new modulus, generator pair */
	  if((p = mod_new_modgen(modulus,generator)) == NULL) {
	       BN_clear_free(generator);
	       BN_clear_free(modulus);
	       log_error("Not enough memory in exchange_make_values()");
	       return (-1);
	  }
	  mod_insert(p);
     }
     /* If we don't have a private value calculate a new one */
     if (p->lifetime < tm || BN_is_zero(p->private_value)) {
	  if (p->exchangevalue != NULL)
	       free(p->exchangevalue);

	  /* See if we can find a cached private value */
	  if ((tmp = mod_find_modulus(modulus)) != NULL &&
	      tmp->lifetime > tm && !BN_is_zero(tmp->private_value)) {
	       BN_copy(p->private_value, tmp->private_value);

	       /* Keep exchange value on same (gen,mod) pair */
	       if (!BN_cmp(p->generator, tmp->generator)) {
		    p->exchangevalue = calloc(tmp->exchangesize,sizeof(u_int8_t));
		    if (p->exchangevalue == NULL) {
			 log_error("calloc() in exchange_make_values()");
			 return (-1);
		    }
		    bcopy(tmp->exchangevalue, p->exchangevalue,
			  tmp->exchangesize);
		    p->exchangesize = tmp->exchangesize;
	       } else
		    p->exchangevalue = NULL;
		
	       p->iterations = tmp->iterations;
	       p->status = tmp->status;
	       p->lifetime = tmp->lifetime;
	  } else {
		  size_t bits;

	       /*
		* Make a new private value and change responder secrets
		* as required by draft.
		*/

	       schedule_remove(REKEY, NULL);
	       schedule_insert(REKEY, REKEY_TIMEOUT, NULL, 0);
	       reset_secret();

	       p->lifetime = tm + MOD_TIMEOUT;
	       p->exchangevalue = NULL;

	       /* Find pointer to the VPN containing the modulus */
	       mod = scheme_get_mod(st->scheme);
	       varpre_get_number_bits(&bits, mod);
	       BN_rand(p->private_value, bits, 0, 0);
	  }
	  /* Do we need to generate a new exchange value */
	  if (p->exchangevalue == NULL) {
	       BIGNUM *tmp;
	       BN_CTX *ctx;
	       size_t bits;

	       mod = scheme_get_mod(st->scheme);
	       varpre_get_number_bits(&bits, mod);

	       tmp = BN_new();
	       ctx = BN_CTX_new();
	       BN_mod_exp(tmp, p->generator, p->private_value, p->modulus,
			  ctx);

	       /*
		* If our exchange value is defective we need to make a new one
		* to avoid subgroup confinement.
		*/
	       while (!exchange_check_value(tmp, p->generator, p->modulus)) {
		    BN_rand(p->private_value, bits, 0, 0);
		    BN_mod_exp(tmp, p->generator, p->private_value, p->modulus,
			       ctx);
	       }

	       BN_CTX_free(ctx);

	       p->exchangesize = BUFFER_SIZE;
	       BN_bn2varpre(tmp, buffer, &(p->exchangesize));

	       p->exchangevalue = calloc(p->exchangesize, sizeof(u_int8_t));
	       if (p->exchangevalue == NULL) {
		    log_error("calloc() in exchange_make_value()");
		    BN_clear_free(tmp);
		    return (-1);
	       }
	       bcopy(buffer, p->exchangevalue, p->exchangesize);

	       BN_clear_free(tmp);
	  }
     }

     if (st->exchangevalue != NULL)
	  free(st->exchangevalue);

     st->exchangevalue = calloc(p->exchangesize, sizeof(u_int8_t));
     if (st->exchangevalue == NULL) {
	  log_error("calloc() in exchange_make_values()");
	  return (-1);
     }
     bcopy(p->exchangevalue, st->exchangevalue, p->exchangesize);

     st->exchangesize = p->exchangesize;
     BN_copy(st->modulus, p->modulus);
     BN_copy(st->generator, p->generator);

     return (0);
}

int
exchange_set_generator(BIGNUM *generator, u_int8_t *scheme, u_int8_t *gen)
{
	switch (ntohs(*((u_int16_t *)scheme))) {
	case DH_G_2_MD5:                    /* DH: Generator of 2 */
	case DH_G_2_DES_MD5:                /* DH: Generator of 2 + privacy */
	case DH_G_2_3DES_SHA1:
	     BN_set_word(generator,2);
	     break;
	case DH_G_3_MD5:
	case DH_G_3_DES_MD5:
	case DH_G_3_3DES_SHA1:
	     BN_set_word(generator,3);
	     break;
	case DH_G_5_MD5:
	case DH_G_5_DES_MD5:
	case DH_G_5_3DES_SHA1:
             BN_set_word(generator,5);
	     break;
	default:
	     log_print("Unsupported exchange scheme %d",
		       *((u_int16_t *)scheme));
	     return (-1);
	}
	return (0);
}

/*
 * Generates the exchange values needed for the value_request
 * and value_response packets.
 */

int
exchange_value_generate(struct stateob *st, u_int8_t *value, u_int16_t *size)
{
        BIGNUM *modulus, *generator;
	struct moduli_cache *p;
	u_int8_t *varpre;

	if ((varpre = scheme_get_mod(st->scheme)) == NULL)
	     return (-1);

	generator = BN_new();
	if (exchange_set_generator(generator, st->scheme,
				   scheme_get_gen(st->scheme)) == -1) {
	     BN_clear_free(generator);
	     return (-1);
	}

	modulus = BN_new();
	BN_varpre2bn(varpre, varpre2octets(varpre), modulus);

	if(exchange_make_values(st, modulus, generator) == -1) {
	     BN_clear_free(modulus);
	     BN_clear_free(generator);
	     return (-1);
	}

	p = mod_find_modgen(modulus,generator);
	if (*size < p->exchangesize)
	     return (-1);

	bcopy(p->exchangevalue, value, p->exchangesize);
	BN_clear_free(modulus);
	BN_clear_free(generator);
	
	*size = p->exchangesize;
	return (1);
}