]> git.ipfire.org Git - people/ms/strongswan.git/blob - programs/pluto/cookie.c
- import of strongswan-2.7.0
[people/ms/strongswan.git] / programs / pluto / cookie.c
1 /* cookie generation/verification routines.
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2002 D. Hugh Redelmeier.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * RCSID $Id: cookie.c,v 1.2 2005/08/17 16:38:20 as Exp $
16 */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23
24 #include <freeswan.h>
25
26 #include "constants.h"
27 #include "defs.h"
28 #include "sha1.h"
29 #include "rnd.h"
30 #include "cookie.h"
31
32 const u_char zero_cookie[COOKIE_SIZE]; /* guaranteed 0 */
33
34 /* Generate a cookie.
35 * First argument is true if we're to create an Initiator cookie.
36 * Length SHOULD be a multiple of sizeof(u_int32_t).
37 */
38 void
39 get_cookie(bool initiator, u_int8_t *cookie, int length, const ip_address *addr)
40 {
41 u_char buffer[SHA1_DIGEST_SIZE];
42 SHA1_CTX ctx;
43
44 do {
45 if (initiator)
46 {
47 get_rnd_bytes(cookie, length);
48 }
49 else /* Responder cookie */
50 {
51 /* This looks as good as any way */
52 size_t addr_length;
53 static u_int32_t counter = 0;
54 unsigned char addr_buff[
55 sizeof(union {struct in_addr A; struct in6_addr B;})];
56
57 addr_length = addrbytesof(addr, addr_buff, sizeof(addr_buff));
58 SHA1Init(&ctx);
59 SHA1Update(&ctx, addr_buff, addr_length);
60 SHA1Update(&ctx, secret_of_the_day, sizeof(secret_of_the_day));
61 counter++;
62 SHA1Update(&ctx, (const void *) &counter, sizeof(counter));
63 SHA1Final(buffer, &ctx);
64 memcpy(cookie, buffer, length);
65 }
66 } while (is_zero_cookie(cookie)); /* probably never loops */
67 }