max max_message_count configurable and move it into tls_eap_t
[strongswan.git] / src / libcharon / plugins / eap_tnc / eap_tnc.c
1 /*
2 * Copyright (C) 2010 Andreas Steffen
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "eap_tnc.h"
17 #include "tnc_if_tnccs.h"
18
19 #include <tls_eap.h>
20
21 #include <daemon.h>
22 #include <library.h>
23
24 typedef struct private_eap_tnc_t private_eap_tnc_t;
25
26 /**
27 * Private data of an eap_tnc_t object.
28 */
29 struct private_eap_tnc_t {
30
31 /**
32 * Public authenticator_t interface.
33 */
34 eap_tnc_t public;
35
36 /**
37 * TLS stack, wrapped by EAP helper
38 */
39 tls_eap_t *tls_eap;
40 };
41
42
43 /** Maximum number of EAP-TNC messages/fragments allowed */
44 #define MAX_MESSAGE_COUNT 2
45 /** Default size of a EAP-TNC fragment */
46 #define MAX_FRAGMENT_LEN 50000
47
48 METHOD(eap_method_t, initiate, status_t,
49 private_eap_tnc_t *this, eap_payload_t **out)
50 {
51 chunk_t data;
52
53 if (this->tls_eap->initiate(this->tls_eap, &data) == NEED_MORE)
54 {
55 *out = eap_payload_create_data(data);
56 free(data.ptr);
57 return NEED_MORE;
58 }
59 return FAILED;
60 }
61
62 METHOD(eap_method_t, process, status_t,
63 private_eap_tnc_t *this, eap_payload_t *in, eap_payload_t **out)
64 {
65 status_t status;
66 chunk_t data;
67
68 data = in->get_data(in);
69 status = this->tls_eap->process(this->tls_eap, data, &data);
70 if (status == NEED_MORE)
71 {
72 *out = eap_payload_create_data(data);
73 free(data.ptr);
74 }
75 return status;
76 }
77
78 METHOD(eap_method_t, get_type, eap_type_t,
79 private_eap_tnc_t *this, u_int32_t *vendor)
80 {
81 *vendor = 0;
82 return EAP_TNC;
83 }
84
85 METHOD(eap_method_t, get_msk, status_t,
86 private_eap_tnc_t *this, chunk_t *msk)
87 {
88 *msk = this->tls_eap->get_msk(this->tls_eap);
89 if (msk->len)
90 {
91 return SUCCESS;
92 }
93 return FAILED;
94 }
95
96 METHOD(eap_method_t, is_mutual, bool,
97 private_eap_tnc_t *this)
98 {
99 return FALSE;
100 }
101
102 METHOD(eap_method_t, destroy, void,
103 private_eap_tnc_t *this)
104 {
105 this->tls_eap->destroy(this->tls_eap);
106 free(this);
107 }
108
109 /**
110 * Generic private constructor
111 */
112 static eap_tnc_t *eap_tnc_create(identification_t *server,
113 identification_t *peer, bool is_server)
114 {
115 private_eap_tnc_t *this;
116 size_t frag_size;
117 int max_msg_count;
118 tls_t *tnc_if_tnccs;
119
120 INIT(this,
121 .public = {
122 .eap_method = {
123 .initiate = _initiate,
124 .process = _process,
125 .get_type = _get_type,
126 .is_mutual = _is_mutual,
127 .get_msk = _get_msk,
128 .destroy = _destroy,
129 },
130 },
131 );
132
133 frag_size = lib->settings->get_int(lib->settings,
134 "charon.plugins.eap-tnc.fragment_size", MAX_FRAGMENT_LEN);
135 max_msg_count = lib->settings->get_int(lib->settings,
136 "charon.plugins.eap-tnc.max_message_count", MAX_MESSAGE_COUNT);
137 tnc_if_tnccs = tnc_if_tnccs_create(is_server, TLS_PURPOSE_EAP_TNC);
138 this->tls_eap = tls_eap_create(EAP_TNC, tnc_if_tnccs, frag_size, max_msg_count);
139 if (!this->tls_eap)
140 {
141 free(this);
142 return NULL;
143 }
144 return &this->public;
145 }
146
147 eap_tnc_t *eap_tnc_create_server(identification_t *server,
148 identification_t *peer)
149 {
150 return eap_tnc_create(server, peer, TRUE);
151 }
152
153 eap_tnc_t *eap_tnc_create_peer(identification_t *server,
154 identification_t *peer)
155 {
156 return eap_tnc_create(server, peer, FALSE);
157 }