summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/eap_peap/eap_peap_avp.c
blob: d5ce5fbc1d48ac967ee2038f90c69f8cbff278fd (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
/*
 * Copyright (C) 2011 Andreas Steffen
 * Copyright (C) 2011 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_peap_avp.h"

#include <eap/eap.h>
#include <utils/debug.h>

/**
 * Microsoft Success and Failure Result AVPs
 */
static const chunk_t MS_AVP_Success = chunk_from_chars(
											0x80, 0x03, 0x00, 0x02, 0x00, 0x01);
static const chunk_t MS_AVP_Failure = chunk_from_chars(
											0x80, 0x03, 0x00, 0x02, 0x00, 0x02);

typedef struct private_eap_peap_avp_t private_eap_peap_avp_t;

/**
 * Private data of an eap_peap_avp_t object.
 */
struct private_eap_peap_avp_t {

	/**
	 * Public eap_peap_avp_t interface.
	 */
	eap_peap_avp_t public;

	/**
	 * EAP server or peer
	 */
	bool is_server;
};

METHOD(eap_peap_avp_t, build, void,
	private_eap_peap_avp_t *this, bio_writer_t *writer, chunk_t data)
{
	uint8_t code;
	eap_packet_t *pkt;
	chunk_t avp_data;

	pkt = (eap_packet_t*)data.ptr;

	if (pkt->code == EAP_SUCCESS || pkt->code == EAP_FAILURE)
	{
		code = (this->is_server) ? EAP_REQUEST : EAP_RESPONSE;
		writer->write_uint8(writer, code);
		writer->write_uint8(writer, pkt->identifier);
		writer->write_uint16(writer, 11);
		writer->write_uint8(writer, EAP_MSTLV);
		avp_data = (pkt->code == EAP_SUCCESS) ? MS_AVP_Success : MS_AVP_Failure;
	}
	else
	{
		avp_data = chunk_skip(data, 4);
	}
	writer->write_data(writer, avp_data);
}

METHOD(eap_peap_avp_t, process, status_t,
	private_eap_peap_avp_t* this, bio_reader_t *reader, chunk_t *data,
	uint8_t identifier)
{
	uint8_t code;
	uint16_t len;
	eap_packet_t *pkt;
	chunk_t avp_data;

	code = (this->is_server) ? EAP_RESPONSE : EAP_REQUEST;
	len = reader->remaining(reader);
	if (!reader->read_data(reader, len, &avp_data))
	{
		return FAILED;
	}
	pkt = (eap_packet_t*)avp_data.ptr;

	if (len > 4 && pkt->code == code && untoh16(&pkt->length) == len)
	{
		if (len == 5 && pkt->type == EAP_IDENTITY)
		{
			DBG2(DBG_IKE, "uncompressed EAP Identity request");
			*data = chunk_clone(avp_data);
			return SUCCESS;
		}
		else if (len == 11 && pkt->type == EAP_MSTLV)
		{
			if (memeq(&pkt->data, MS_AVP_Success.ptr, MS_AVP_Success.len))
			{
				DBG2(DBG_IKE, "MS Success Result AVP");
				code = EAP_SUCCESS;
			}
			else if (memeq(&pkt->data, MS_AVP_Failure.ptr, MS_AVP_Failure.len))
			{
				DBG2(DBG_IKE, "MS Failure Result AVP");
				code = EAP_FAILURE;
			}
			else
			{
				DBG1(DBG_IKE, "unknown MS AVP message");
				return FAILED;
			}
			identifier = pkt->identifier;
			len = 0;
		}
	}

	*data = chunk_alloc(4 + len);
	pkt = (eap_packet_t*)data->ptr;
	pkt->code = code;
	pkt->identifier = identifier;
	htoun16(&pkt->length, data->len);
	memcpy(data->ptr + 4, avp_data.ptr, len);

	return SUCCESS;
}

METHOD(eap_peap_avp_t, destroy, void,
	private_eap_peap_avp_t *this)
{
	free(this);
}

/**
 * See header
 */
eap_peap_avp_t *eap_peap_avp_create(bool is_server)
{
	private_eap_peap_avp_t *this;

	INIT(this,
		.public= {
			.process = _process,
			.build = _build,
			.destroy = _destroy,
		},
		.is_server = is_server,
	);

	return &this->public;
}