aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/sa/authenticator.c
blob: 6ab1b00eea6cbf265f2c7786279ddfa19063a139 (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
/**
 * @file authenticator.c
 *
 * @brief Implementation of authenticator.
 *
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * 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 "authenticator.h"

#include <utils/allocator.h>

typedef struct private_authenticator_t private_authenticator_t;

/**
 * Private data of an authenticator_t object.
 */
struct private_authenticator_t {

	/**
	 * Public interface.
	 */
	authenticator_t public;

	/**
	 * IKE_SA.
	 */
	protected_ike_sa_t *ike_sa;

	/**
	 * A logger for.
	 * 
	 * Using logger of IKE_SA.
	 */
	logger_t *logger;
	
	/**
	 * TODO
	 */
	chunk_t (*allocate_octets) (private_authenticator_t *this,chunk_t last_message, chunk_t other_nonce,identification_t *my_id);
	
	chunk_t (*allocate_auth_data_with_preshared_secret) (private_authenticator_t *this,chunk_t octets,chunk_t preshared_secret);
};

/**
 * Implementation of authenticator_t.private_authenticator_t.
 */
static chunk_t allocate_octets(private_authenticator_t *this,chunk_t last_message, chunk_t other_nonce,identification_t *my_id)
{
	chunk_t id_chunk = my_id->get_encoding(my_id);
	u_int8_t id_with_header[4 + id_chunk.len];
	chunk_t id_with_header_chunk;
	chunk_t octets;
	u_int8_t *current_pos;
	prf_t *prf;
	
	id_with_header[0] = my_id->get_type(my_id);
	id_with_header[1] = 0x00;
	id_with_header[2] = 0x00;
	id_with_header[3] = 0x00;
	memcpy(id_with_header + 4,id_chunk.ptr,id_chunk.len);
	id_with_header_chunk.ptr = id_with_header;
	id_with_header_chunk.len = sizeof(id_with_header);
	
	prf = this->ike_sa->get_prf(this->ike_sa);
	
	prf->set_key(prf,this->ike_sa->get_key_pr(this->ike_sa));
	
	
	/* 4 bytes are id type and reserved fields of id payload */
	octets.len = last_message.len + other_nonce.len + prf->get_block_size(prf);
	octets.ptr = allocator_alloc(octets.len);
	current_pos = octets.ptr;
	memcpy(current_pos,last_message.ptr,last_message.len);
	current_pos += last_message.len;
	memcpy(current_pos,other_nonce.ptr,other_nonce.len);
	current_pos += other_nonce.len;
	prf->get_bytes(prf,id_with_header_chunk,current_pos);
	
	this->logger->log_chunk(this->logger,RAW | MORE, "Octets (Mesage + Nonce + prf(Sk_px,Idx)",&octets);
	return octets;
}

/**
 * Implementation of authenticator_t.allocate_auth_data_with_preshared_secret.
 */
static chunk_t allocate_auth_data_with_preshared_secret (private_authenticator_t *this,chunk_t octets,chunk_t preshared_secret)
{
	prf_t *prf = this->ike_sa->get_prf(this->ike_sa);
	chunk_t auth_data;	
	chunk_t key_pad;
	chunk_t key;
	
	key_pad.ptr = "Key Pad for IKEv2";
	key_pad.len = strlen(key_pad.ptr);

	prf->set_key(prf,preshared_secret);
	prf->allocate_bytes(prf,key_pad,&key);
	prf->set_key(prf,key);
	allocator_free_chunk(&key);
	prf->allocate_bytes(prf,octets,&auth_data);
	this->logger->log_chunk(this->logger,RAW | MORE, "Authenticated data",&auth_data);
			
	return auth_data;
}


/**
 * Implementation of authenticator_t.verify_authentication.
 */
static status_t verify_authentication (private_authenticator_t *this,auth_method_t auth_method, chunk_t auth_data, chunk_t last_message, chunk_t other_nonce,identification_t *my_id,bool *verified)
{
	switch(auth_method)
	{
		case SHARED_KEY_MESSAGE_INTEGRITY_CODE:
		{
			
			chunk_t preshared_secret;
			
			preshared_secret.ptr = "secret";
			preshared_secret.len = strlen(preshared_secret.ptr);
			
			chunk_t octets = this->allocate_octets(this,last_message, other_nonce,my_id);
			chunk_t my_auth_data = this->allocate_auth_data_with_preshared_secret(this,octets,preshared_secret);

			allocator_free_chunk(&octets);				

			if (auth_data.len != my_auth_data.len)
			{
				*verified = FALSE;
				allocator_free_chunk(&my_auth_data);
				return SUCCESS;
			}
			if (memcmp(auth_data.ptr,my_auth_data.ptr,my_auth_data.len) == 0)
			{
				*verified = TRUE;
			}
			else
			{
				*verified = FALSE;
			}
			allocator_free_chunk(&my_auth_data);		
			return SUCCESS;
		}
		default:
		{
			return NOT_SUPPORTED;
		}
	}
}

/**
 * Implementation of authenticator_t.allocate_auth_data.
 */
static status_t allocate_auth_data (private_authenticator_t *this,auth_method_t auth_method,chunk_t last_message, chunk_t other_nonce,identification_t *my_id,chunk_t *auth_data)
{
	switch(auth_method)
	{
		case SHARED_KEY_MESSAGE_INTEGRITY_CODE:
		{
			chunk_t preshared_secret;
			
			preshared_secret.ptr = "secret";
			preshared_secret.len = strlen(preshared_secret.ptr);
			
			chunk_t octets = this->allocate_octets(this,last_message, other_nonce,my_id);
			chunk_t my_auth_data = allocate_auth_data_with_preshared_secret(this,octets,preshared_secret);
			
			allocator_free_chunk(&octets);
			
			*auth_data = my_auth_data;
			
			return SUCCESS;
		}
		default:
		{
			return NOT_SUPPORTED;
		}
	}
}

/**
 * Implementation of authenticator_t.destroy.
 */
static void destroy (private_authenticator_t *this)
{
	allocator_free(this);
}

/*
 * Described in header.
 */
authenticator_t *authenticator_create(protected_ike_sa_t *ike_sa)
{
	private_authenticator_t *this = allocator_alloc_thing(private_authenticator_t);

	/* Public functions */
	this->public.destroy = (void(*)(authenticator_t*))destroy;
	this->public.verify_authentication = (status_t (*) (authenticator_t *,auth_method_t , chunk_t , chunk_t , chunk_t ,identification_t *,bool *) )verify_authentication;
	this->public.allocate_auth_data = (status_t (*)  (authenticator_t *,auth_method_t ,chunk_t , chunk_t ,identification_t *,chunk_t *)) allocate_auth_data;
	
	/* private functions */
	this->allocate_octets = allocate_octets;
	this->allocate_auth_data_with_preshared_secret = allocate_auth_data_with_preshared_secret;
	
	/* private data */
	this->ike_sa = ike_sa;
	this->logger = this->ike_sa->get_logger(this->ike_sa);
	
	return 	&(this->public);
}