forked from droe/fiked
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer_ctx.c
146 lines (126 loc) · 3.12 KB
/
peer_ctx.c
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
/*
* fiked - a fake IKE PSK+XAUTH daemon based on vpnc
* Copyright (C) 2005, Daniel Roethlisberger <daniel@roe.ch>
*
* This program 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.
*
* This program 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, see http://www.gnu.org/copyleft/
*/
#include "peer_ctx.h"
#include "mem.h"
#include "vpnc/math_group.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* message_iv */
message_iv *
message_iv_get(uint32_t id, message_iv **head)
{
message_iv *found = NULL;
for (message_iv *p = *head; p && !found; p = p->next) {
if (p->id == id)
found = p;
}
if (!found) {
mem_allocate(&found, sizeof(message_iv));
memset(found, 0, sizeof(message_iv));
found->id = id;
found->next = *head;
*head = found;
}
return found;
}
void
message_iv_free(message_iv *msg_iv)
{
if (msg_iv->next) {
message_iv_free(msg_iv->next);
msg_iv->next = NULL;
}
mem_free(&msg_iv->iv);
free(msg_iv);
}
/* peer_ctx */
peer_ctx *
peer_ctx_get(datagram *dgm, config *cfg, peer_ctx **head)
{
peer_ctx *found = NULL;
for (peer_ctx *p = *head; p && !found; p = p->next) {
if (p->peer_addr.sin_addr.s_addr == dgm->peer_addr.sin_addr.s_addr &&
p->peer_addr.sin_port == dgm->peer_addr.sin_port)
found = p;
}
if (!found) {
mem_allocate(&found, sizeof(peer_ctx));
memset(found, 0, sizeof(peer_ctx));
found->peer_addr = dgm->peer_addr;
found->next = *head;
found->state = STATE_NEW;
found->cfg = cfg;
*head = found;
}
return found;
}
void
peer_ctx_clear(peer_ctx *ctx)
{
mem_free(&ctx->ipsec_id);
mem_free(&ctx->ipsec_secret);
mem_free(&ctx->xauth_username);
mem_free(&ctx->xauth_password);
mem_free(&ctx->key);
mem_free(&ctx->iv0);
if (ctx->msg_iv) {
message_iv_free(ctx->msg_iv);
ctx->msg_iv = NULL;
}
if (ctx->dh_group) {
group_free(ctx->dh_group);
ctx->dh_group = NULL;
}
mem_free(&ctx->dh_i_public);
mem_free(&ctx->dh_r_public);
mem_free(&ctx->dh_secret);
mem_free(&ctx->skeyid);
mem_free(&ctx->skeyid_e);
mem_free(&ctx->skeyid_a);
mem_free(&ctx->skeyid_d);
mem_free(&ctx->i_sa);
mem_free(&ctx->i_id);
mem_free(&ctx->r_id);
mem_free(&ctx->i_nonce);
mem_free(&ctx->r_nonce);
mem_free(&ctx->i_hash);
mem_free(&ctx->r_hash);
}
void
peer_ctx_reset(peer_ctx *ctx)
{
peer_ctx_clear(ctx);
memset((uint8_t *)&ctx->state, 0,
(uint8_t *)ctx - (uint8_t *)&ctx->state + sizeof(peer_ctx));
ctx->state = STATE_NEW;
}
void
peer_ctx_free(peer_ctx *ctx)
{
if (ctx->next) {
peer_ctx_free(ctx->next);
ctx->next = NULL;
}
mem_free(&ctx->last_dgm_hash); /* only free on free_peer_ctx */
peer_ctx_clear(ctx);
free(ctx);
}