summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/eap_ttls/eap_ttls_avp.c
blob: f75e3e0a617bf2be2f8486203cb1c7bd5abad4c1 (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
/*
 * Copyright (C) 2010 Andreas Steffen
 * Copyright (C) 2010 HSR 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 "eap_ttls_avp.h"

#include <utils/debug.h>

#define AVP_EAP_MESSAGE		79
#define AVP_HEADER_LEN		 8

typedef struct private_eap_ttls_avp_t private_eap_ttls_avp_t;

/**
 * Private data of an eap_ttls_avp_t object.
 */
struct private_eap_ttls_avp_t {

	/**
	 * Public eap_ttls_avp_t interface.
	 */
	eap_ttls_avp_t public;

	/**
	 * AVP input buffer
	 */
	chunk_t input;

	/**
	 * Position in input buffer
	 */
	size_t inpos;

	/**
	 * process header (TRUE) or body (FALSE)
	 */
	bool process_header;

	/**
	 * Size of AVP data
	 */
	size_t data_len;
};

METHOD(eap_ttls_avp_t, build, void,
	private_eap_ttls_avp_t *this, bio_writer_t *writer, chunk_t data)
{
	char zero_padding[] = { 0x00, 0x00, 0x00 };
	chunk_t   avp_padding;
	uint8_t  avp_flags;
	uint32_t avp_len;

	avp_flags = 0x40;
	avp_len = 8 + data.len;
	avp_padding = chunk_create(zero_padding, (4 - data.len) % 4);

	writer->write_uint32(writer, AVP_EAP_MESSAGE);
	writer->write_uint8(writer, avp_flags);
	writer->write_uint24(writer, avp_len);
	writer->write_data(writer, data);
	writer->write_data(writer, avp_padding);
}

METHOD(eap_ttls_avp_t, process, status_t,
	private_eap_ttls_avp_t* this, bio_reader_t *reader, chunk_t *data)
{
	size_t len;
	chunk_t buf;

	if (this->process_header)
	{
		bio_reader_t *header;
		uint32_t avp_code;
		uint8_t  avp_flags;
		uint32_t avp_len;
		bool success;

		len = min(reader->remaining(reader), AVP_HEADER_LEN - this->inpos);
		if (!reader->read_data(reader, len, &buf))
		{
			return FAILED;
		}
		if (this->input.len == 0)
		{
			/* start of a new AVP header */
			this->input = chunk_alloc(AVP_HEADER_LEN);
			memcpy(this->input.ptr, buf.ptr, len);
			this->inpos = len;
		}
		else
		{
			memcpy(this->input.ptr + this->inpos, buf.ptr, len);
			this->inpos += len;
		}

		if (this->inpos < AVP_HEADER_LEN)
		{
			return NEED_MORE;
		}

		/* parse AVP header */
		header = bio_reader_create(this->input);
		success = header->read_uint32(header, &avp_code) &&
				  header->read_uint8(header, &avp_flags) &&
				  header->read_uint24(header, &avp_len);
		header->destroy(header);
		chunk_free(&this->input);
		this->inpos = 0;

		if (!success)
		{
			DBG1(DBG_IKE, "received invalid AVP header");
			return FAILED;
		}
 		if (avp_code != AVP_EAP_MESSAGE)
		{
			DBG1(DBG_IKE, "expected AVP_EAP_MESSAGE but received %u", avp_code);
			return FAILED;
		}
		this->process_header = FALSE;
		this->data_len = avp_len - 8;
		this->input = chunk_alloc(this->data_len + (4 - avp_len) % 4);
	}

	/* process AVP data */
	len = min(reader->remaining(reader), this->input.len - this->inpos);
	if (!reader->read_data(reader, len, &buf))
	{
		return FAILED;
	}
	memcpy(this->input.ptr + this->inpos, buf.ptr, len);
	this->inpos += len;
	if (this->inpos < this->input.len)
	{
		return NEED_MORE;
	}

	*data = this->input;
	data->len = this->data_len;

	/* preparing for next AVP */
	this->input = chunk_empty;
	this->inpos = 0;
	this->process_header = TRUE;

	return SUCCESS;
}

METHOD(eap_ttls_avp_t, destroy, void,
	private_eap_ttls_avp_t *this)
{
	chunk_free(&this->input);
	free(this);
}

/**
 * See header
 */
eap_ttls_avp_t *eap_ttls_avp_create(void)
{
	private_eap_ttls_avp_t *this;

	INIT(this,
		.public= {
			.process = _process,
			.build = _build,
			.destroy = _destroy,
		},
		.input = chunk_empty,
		.inpos = 0,
		.process_header = TRUE,
		.data_len = 0,
	);

	return &this->public;
}