summaryrefslogtreecommitdiff
path: root/src/charon-tkm/src/tkm/tkm_diffie_hellman.c
blob: 67db5e6d87d66f88ea9fd3f9baed383386d168dc (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
/*
 * Copyrigth (C) 2012 Reto Buerki
 * Copyright (C) 2012 Adrian-Ken Rueegsegger
 * Hochschule fuer Technik Rapperswil
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include <tkm/client.h>
#include <tkm/constants.h>

#include "tkm.h"
#include "tkm_utils.h"
#include "tkm_diffie_hellman.h"

#include <daemon.h>
#include <collections/hashtable.h>

typedef struct private_tkm_diffie_hellman_t private_tkm_diffie_hellman_t;

static hashtable_t *group_map = NULL;

/**
 * Private data of a tkm_diffie_hellman_t object.
 */
struct private_tkm_diffie_hellman_t {

	/**
	 * Public tkm_diffie_hellman_t interface.
	 */
	tkm_diffie_hellman_t public;

	/**
	 * Diffie Hellman group number.
	 */
	u_int16_t group;

	/**
	 * Diffie Hellman public value.
	 */
	dh_pubvalue_type pubvalue;

	/**
	 * Context id.
	 */
	dh_id_type context_id;

};

METHOD(diffie_hellman_t, get_my_public_value, void,
	private_tkm_diffie_hellman_t *this, chunk_t *value)
{
	sequence_to_chunk(this->pubvalue.data, this->pubvalue.size, value);
}

METHOD(diffie_hellman_t, get_shared_secret, status_t,
	private_tkm_diffie_hellman_t *this, chunk_t *secret)
{
	*secret = chunk_empty;
	return SUCCESS;
}


METHOD(diffie_hellman_t, set_other_public_value, void,
	private_tkm_diffie_hellman_t *this, chunk_t value)
{
	// TODO: unvoid this function

	dh_pubvalue_type othervalue;
	othervalue.size = value.len;
	memcpy(&othervalue.data, value.ptr, value.len);

	ike_dh_generate_key(this->context_id, othervalue);
}

METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
	private_tkm_diffie_hellman_t *this)
{
	return this->group;
}

METHOD(diffie_hellman_t, destroy, void,
	private_tkm_diffie_hellman_t *this)
{
	if (ike_dh_reset(this->context_id) != TKM_OK)
	{
		DBG1(DBG_LIB, "failed to reset DH context %d", this->context_id);
	}

	tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_DH, this->context_id);
	free(this);
}

METHOD(tkm_diffie_hellman_t, get_id, dh_id_type,
	private_tkm_diffie_hellman_t *this)
{
	return this->context_id;
}

static u_int hash(void *key)
{
	diffie_hellman_group_t k = *(diffie_hellman_group_t*)key;
	return chunk_hash(chunk_from_thing(k));
}

static bool equals(void *key, void *other_key)
{
	return *(diffie_hellman_group_t*)key == *(diffie_hellman_group_t*)other_key;
}

/*
 * Described in header.
 */
int register_dh_mapping()
{
	int count, i;
	char *iana_id_str, *tkm_id_str;
	diffie_hellman_group_t *iana_id;
	u_int64_t *tkm_id;
	hashtable_t *map;
	enumerator_t *enumerator;

	map = hashtable_create((hashtable_hash_t)hash,
						   (hashtable_equals_t)equals, 16);

	enumerator = lib->settings->create_key_value_enumerator(lib->settings,
															"%s.dh_mapping",
															lib->ns);

	while (enumerator->enumerate(enumerator, &iana_id_str, &tkm_id_str))
	{
		iana_id = malloc_thing(diffie_hellman_group_t);
		*iana_id = settings_value_as_int(iana_id_str, 0);
		tkm_id = malloc_thing(u_int64_t);
		*tkm_id = settings_value_as_int(tkm_id_str, 0);

		map->put(map, iana_id, tkm_id);
	}
	enumerator->destroy(enumerator);

	count = map->get_count(map);
	plugin_feature_t f[count + 1];
	f[0] = PLUGIN_REGISTER(DH, tkm_diffie_hellman_create);

	i = 1;
	enumerator = map->create_enumerator(map);
	while (enumerator->enumerate(enumerator, &iana_id, &tkm_id))
	{
		f[i] = PLUGIN_PROVIDE(DH, *iana_id);
		i++;
	}
	enumerator->destroy(enumerator);

	lib->plugins->add_static_features(lib->plugins, "tkm-dh", f, countof(f),
									  TRUE, NULL, NULL);

	if (count > 0)
	{
		group_map = map;
	}
	else
	{
		map->destroy(map);
	}

	return count;
}

/*
 * Described in header.
 */
void destroy_dh_mapping()
{
	enumerator_t *enumerator;
	char *key, *value;

	if (group_map)
	{
		enumerator = group_map->create_enumerator(group_map);
		while (enumerator->enumerate(enumerator, &key, &value))
		{
			free(key);
			free(value);
		}
		enumerator->destroy(enumerator);
		group_map->destroy(group_map);
	}
}

/*
 * Described in header.
 */
tkm_diffie_hellman_t *tkm_diffie_hellman_create(diffie_hellman_group_t group)
{
	private_tkm_diffie_hellman_t *this;

	if (!group_map)
	{
		return NULL;
	}

	INIT(this,
		.public = {
			.dh = {
				.get_shared_secret = _get_shared_secret,
				.set_other_public_value = _set_other_public_value,
				.get_my_public_value = _get_my_public_value,
				.get_dh_group = _get_dh_group,
				.destroy = _destroy,
			},
			.get_id = _get_id,
		},
		.group = group,
		.context_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_DH),
	);

	if (!this->context_id)
	{
		free(this);
		return NULL;
	}

	u_int64_t *dha_id = group_map->get(group_map, &group);
	if (!dha_id)
	{
		free(this);
		return NULL;
	}

	if (ike_dh_create(this->context_id, *dha_id, &this->pubvalue) != TKM_OK)
	{
		free(this);
		return NULL;
	}

	return &this->public;
}