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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/*
* Copyright 1997,1998 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.
*/
/*
* compute_secrets.c:
* shared secret with diffie-hellman key exchange
* cryptographic hashes for session keys
*/
#ifndef lint
static char rcsid[] = "$Id: compute_secrets.c,v 1.6 1998/06/30 16:58:45 provos Exp $";
#endif
#define _SECRETS_C_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <gmp.h>
#include <md5.h>
#include "state.h"
#include <sha1.h>
#include "config.h"
#include "identity.h"
#include "attributes.h"
#include "modulus.h"
#include "secrets.h"
#include "buffer.h"
#include "spi.h"
#include "exchange.h"
#include "scheme.h"
#include "errlog.h"
int privacykey(struct stateob *st, struct idxform *hash, u_int8_t *key,
u_int8_t *packet, u_int16_t bytes, u_int16_t *order, int owner);
int
compute_shared_secret(struct stateob *st,
u_int8_t **shared, u_int16_t *sharedsize)
{
struct moduli_cache *mod;
int header;
mpz_t tmp, bits, tex;
mpz_init(tmp);
mpz_init(bits);
if((mod=mod_find_modgen(st->modulus, st->generator)) == NULL) {
log_error(0, "Can't find exchange information in cache in compute_shared_secret()");
return -1;
}
/* Compute Diffie-Hellmann a^(xy) (mod n) */
mpz_init_set_varpre(tex, st->texchange);
mpz_powm(tmp, tex, mod->private_value, mod->modulus);
mpz_clear(tex);
varpre_get_number_bits(bits, scheme_get_mod(st->scheme));
*sharedsize = BUFFER_SIZE;
if(mpz_to_varpre(buffer, sharedsize, tmp, bits) == -1)
return -1;
mpz_clear(bits);
mpz_clear(tmp);
/* The shared secret is not used with the size part */
if (buffer[0] == 255 && buffer[1] == 255)
header = 8;
else if (buffer[0] == 255)
header = 4;
else
header = 2;
*sharedsize -= header;
if((*shared = calloc(*sharedsize,sizeof(u_int8_t))) == NULL) {
log_error(0, "Not enough memory for shared secret in compute_shared_secret()");
return -1;
}
bcopy(buffer+header, *shared, *sharedsize);
return 0;
}
/*
* Generate session keys for all attributes in given SPI.
*/
int
make_session_keys(struct stateob *st, struct spiob *spi)
{
u_int8_t *p, *attributes, **secret;
u_int16_t attribsize, *secretsize;
u_int16_t i, count = 0;
int bits;
attributes = spi->attributes;
attribsize = spi->attribsize;
secret = &(spi->sessionkey);
secretsize = &(spi->sessionkeysize);
if (*secret != NULL)
return 0; /* Already calculated */
p = attributes;
for (i = 0; i<attribsize; i += p[i+1] + 2) {
if (p[i] != AT_AH_ATTRIB && p[i] != AT_ESP_ATTRIB) {
bits = get_session_key_length(p+i);
if (bits == -1) {
log_error(0, "Invalid attribute choice for SPI in make_session_keys()");
return -1;
}
count += bits & 7 ? (bits >> 3) + 1 : bits >> 3;
}
}
if ((*secret = calloc(count, sizeof(u_int8_t))) == NULL) {
log_error(1, "calloc() in make_session_keys()");
return -1;
}
*secretsize = count;
count = 0;
p = *secret;
for (i = 0; i<attribsize; i += attributes[i+1] + 2) {
if (attributes[i] != AT_AH_ATTRIB &&
attributes[i] != AT_ESP_ATTRIB) {
bits = compute_session_key(st, p, attributes+i,
spi->flags & SPI_OWNER,
&count);
if (bits == -1)
return -1;
if (bits > 0) {
#ifdef DEBUG
{
int d = BUFFER_SIZE;
printf("%s session key for AT %d: ",
spi->flags & SPI_OWNER ?
"Owner" : "User", (int)attributes[i]);
bin2hex(buffer, &d, p,
bits & 7 ? (bits >> 3) + 1 : bits >> 3);
printf("0x%s\n", buffer);
}
#endif /* DEBUG */
p += bits & 7 ? (bits >> 3) + 1 : bits >> 3;
}
}
}
return 0;
}
/*
* Return length of requried session key in bits.
* DES would be 64 bits.
*/
int
get_session_key_length(u_int8_t *attribute)
{
attrib_t *ob;
if ((ob = getattrib(*attribute)) == NULL) {
log_error(0, "Unknown attribute %d in get_session_key_length()",
*attribute);
return -1;
}
return ob->klen << 3;
}
/*
* Compute session keys for the attributes in the security association.
* owner determines the direction of the spi session key.
* order is the amount of bits we already used for other session keys.
*/
int
compute_session_key(struct stateob *st, u_int8_t *key,
u_int8_t *attribute, int owner,
u_int16_t *order)
{
struct idxform *hash;
u_int16_t size, i, n;
u_int8_t digest[HASH_MAX];
int bits;
switch(ntohs(*((u_int16_t *)st->scheme))) {
case DH_G_2_MD5:
case DH_G_3_MD5:
case DH_G_2_DES_MD5:
case DH_G_5_MD5:
case DH_G_3_DES_MD5:
case DH_G_5_DES_MD5:
case DH_G_VAR_MD5:
case DH_G_VAR_DES_MD5:
hash = get_hash(HASH_MD5);
break;
case DH_G_2_3DES_SHA1:
case DH_G_3_3DES_SHA1:
case DH_G_5_3DES_SHA1:
case DH_G_VAR_3DES_SHA1:
hash = get_hash(HASH_SHA1);
break;
default:
log_error(0, "Unkown scheme %d in compute_session_key()",
ntohs(*((u_int16_t *)st->scheme)));
return -1;
}
if ((bits = get_session_key_length(attribute)) == -1)
return -1;
if (bits == 0)
return 0;
size = bits >> 3;
if(bits & 0x7)
size++;
/* As many shared secrets we used already */
n = *order;
hash->Init(hash->ctx);
hash->Update(hash->ctx, st->icookie, COOKIE_SIZE);
hash->Update(hash->ctx, st->rcookie, COOKIE_SIZE);
if(owner) { /* Session key for Owner SPI */
hash->Update(hash->ctx,st->oSPIsecret,st->oSPIsecretsize);
hash->Update(hash->ctx,st->uSPIsecret,st->uSPIsecretsize);
} else { /* Session key for User SPI */
hash->Update(hash->ctx,st->uSPIsecret,st->uSPIsecretsize);
hash->Update(hash->ctx,st->oSPIsecret,st->oSPIsecretsize);
}
/* Message Verification field */
hash->Update(hash->ctx, st->verification, st->versize);
for (i=0; i<n; i++)
hash->Update(hash->ctx, st->shared, st->sharedsize);
do {
bcopy(hash->ctx, hash->ctx2, hash->ctxsize);
hash->Update(hash->ctx2,st->shared, st->sharedsize);
bcopy(hash->ctx2, hash->ctx, hash->ctxsize);
hash->Final(digest, hash->ctx2);
/* One iteration more */
n++;
bcopy(digest, key, size>hash->hashsize ? hash->hashsize : size);
key += size>hash->hashsize ? hash->hashsize : size;
/* Unsigned integer arithmetic */
size -= size>hash->hashsize ? hash->hashsize : size;
} while(size > 0);
*order = n;
return bits;
}
/*
* Initializes the hash contexts for privacy key computation.
*/
int
init_privacy_key(struct stateob *st, int owner)
{
void **ctx;
struct idxform *hash;
u_int8_t *first, *second;
u_int16_t firstsize, secondsize;
if (owner) {
ctx = &st->oSPIprivacyctx;
first = st->exchangevalue;
firstsize = st->exchangesize;
second = st->texchange;
secondsize = st->texchangesize;
} else {
ctx = &st->uSPIprivacyctx;
first = st->texchange;
firstsize = st->texchangesize;
second = st->exchangevalue;
secondsize = st->exchangesize;
}
switch(ntohs(*((u_int16_t *)st->scheme))) {
case DH_G_2_MD5:
case DH_G_3_MD5:
case DH_G_5_MD5:
case DH_G_2_DES_MD5:
case DH_G_3_DES_MD5:
case DH_G_5_DES_MD5:
hash = get_hash(HASH_MD5);
break;
case DH_G_2_3DES_SHA1:
case DH_G_3_3DES_SHA1:
case DH_G_5_3DES_SHA1:
hash = get_hash(HASH_SHA1);
break;
default:
log_error(0, "Unknown exchange scheme in init_privacy_key()");
return -1;
}
if (hash == NULL)
return -1;
if (*ctx != NULL)
free(*ctx);
if ((*ctx = calloc(hash->ctxsize, sizeof(char))) == NULL) {
log_error(1, "calloc() in init_privacy_key()");
return -1;
}
hash->Init(*ctx);
hash->Update(*ctx, first, firstsize);
hash->Update(*ctx, second, secondsize);
return 1;
}
/*
* order gives the number of iterations already done for keys
*/
int
compute_privacy_key(struct stateob *st, u_int8_t *key, u_int8_t *packet,
u_int16_t bits, u_int16_t *order, int owner)
{
u_int16_t size;
struct idxform *hash;
size = bits >> 3;
if(bits & 0x7)
size++;
switch(ntohs(*((u_int16_t *)st->scheme))) {
case DH_G_2_MD5:
case DH_G_3_MD5:
case DH_G_5_MD5:
case DH_G_2_DES_MD5:
case DH_G_3_DES_MD5:
case DH_G_5_DES_MD5:
hash = get_hash(HASH_MD5);
break;
case DH_G_2_3DES_SHA1:
case DH_G_3_3DES_SHA1:
case DH_G_5_3DES_SHA1:
hash = get_hash(HASH_SHA1);
break;
default:
log_error(0, "Unknown exchange scheme in compute_privacy_key()");
return -1;
}
if (hash == NULL)
return -1;
return privacykey(st, hash, key, packet, size, order, owner);
}
int
privacykey(struct stateob *st, struct idxform *hash,
u_int8_t *key, u_int8_t *packet,
u_int16_t bytes, u_int16_t *order, int owner)
{
u_int16_t i, n;
u_int8_t digest[HASH_MAX];
/* SPIprivacyctx contains the hashed exchangevalues */
bcopy(owner ? st->oSPIprivacyctx : st->uSPIprivacyctx,
hash->ctx2, hash->ctxsize);
hash->Update(hash->ctx2, packet, 2*COOKIE_SIZE + 4 + SPI_SIZE);
/* As many shared secrets we used already */
n = *order;
for(i=0; i<n; i++)
hash->Update(hash->ctx2, st->shared, st->sharedsize);
do {
bcopy(hash->ctx2, hash->ctx, hash->ctxsize);
hash->Update(hash->ctx, st->shared, st->sharedsize);
bcopy(hash->ctx, hash->ctx2, hash->ctxsize);
hash->Final(digest, hash->ctx);
bcopy(digest, key, bytes>hash->hashsize ? hash->hashsize : bytes);
key += bytes>hash->hashsize ? hash->hashsize : bytes;
/* Unsigned integer arithmetic */
bytes -= bytes>hash->hashsize ? hash->hashsize : bytes;
/* Increment the times we called Final */
i++;
} while(bytes > 0);
*order = i;
return 0;
}
|