| 1 |
/***************************************************************************************************** |
| 2 |
* Software License Agreement (BSD License) |
| 3 |
* Author : Souheil Ben Ayed <[email protected]> |
| 4 |
* |
| 5 |
* Copyright (c) 2009-2010, Souheil Ben Ayed, Teraoka Laboratory of Keio University, and the WIDE Project |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use of this software in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions are met: |
| 10 |
* |
| 11 |
* 1. Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* |
| 14 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
* notice, this list of conditions and the following disclaimer in the |
| 16 |
* documentation and/or other materials provided with the distribution. |
| 17 |
* |
| 18 |
* 3. All advertising materials mentioning features or use of this software |
| 19 |
* must display the following acknowledgement: |
| 20 |
* This product includes software developed by Souheil Ben Ayed <[email protected]>. |
| 21 |
* |
| 22 |
* 4. Neither the name of Souheil Ben Ayed, Teraoka Laboratory of Keio University or the WIDE Project nor the |
| 23 |
* names of its contributors may be used to endorse or promote products |
| 24 |
* derived from this software without specific prior written permission. |
| 25 |
* |
| 26 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY |
| 27 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 30 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 33 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 |
*****************************************************************************************************/ |
| 37 |
|
| 38 |
|
| 39 |
#include "eap_tls.h" |
| 40 |
|
| 41 |
struct tls_config tls_global_conf; |
| 42 |
|
| 43 |
__attribute__((visibility("default"))) |
| 44 |
int eap_tls_configure(char * configfile); |
| 45 |
|
| 46 |
__attribute__((visibility("default"))) |
| 47 |
int eap_tls_init(struct eap_state_machine *smd); |
| 48 |
|
| 49 |
__attribute__((visibility("default"))) |
| 50 |
int eap_tls_initPickUp(struct eap_state_machine *smd); |
| 51 |
|
| 52 |
__attribute__((visibility("default"))) |
| 53 |
int eap_tls_buildReq(struct eap_state_machine *smd, u8 eap_md5, |
| 54 |
struct eap_packet * eapPacket); |
| 55 |
|
| 56 |
__attribute__((visibility("default"))) |
| 57 |
int eap_tls_getTimeout(struct eap_state_machine *smd, int * timeout); |
| 58 |
|
| 59 |
__attribute__((visibility("default"))) |
| 60 |
boolean eap_tls_check(struct eap_state_machine *smd, |
| 61 |
struct eap_packet *eapRespData); |
| 62 |
|
| 63 |
__attribute__((visibility("default"))) |
| 64 |
int eap_tls_process(struct eap_state_machine *smd, |
| 65 |
struct eap_packet *eapRespData); |
| 66 |
|
| 67 |
__attribute__((visibility("default"))) |
| 68 |
boolean eap_tls_isDone(struct eap_state_machine *smd); |
| 69 |
|
| 70 |
__attribute__((visibility("default"))) |
| 71 |
int eap_tls_getKey(struct eap_state_machine *smd, u8** msk, int * msklen, u8** emsk, int * emsklen); |
| 72 |
|
| 73 |
__attribute__((visibility("default"))) |
| 74 |
void eap_tls_unregister(void); |
| 75 |
|
| 76 |
__attribute__((visibility("default"))) |
| 77 |
void eap_tls_free(void * data); |
| 78 |
|
| 79 |
REGISTER_METHOD("eap_tls", "eap_tls_configure", "eap_tls_init", "eap_tls_initPickUp", "eap_tls_buildReq", "eap_tls_getTimeout", "eap_tls_check", "eap_tls_process", "eap_tls_isDone", "eap_tls_getKey", "eap_tls_unregister", "eap_tls_free") |
| 80 |
; |
| 81 |
|
| 82 |
int eap_tls_configure(char * configfile) |
| 83 |
{ |
| 84 |
int ret; |
| 85 |
extern FILE * eaptlsin; |
| 86 |
|
| 87 |
if (configfile) |
| 88 |
{ |
| 89 |
tls_global_conf.conffile = configfile; |
| 90 |
} |
| 91 |
tls_global_conf.certfile = NULL; |
| 92 |
tls_global_conf.keyfile = NULL; |
| 93 |
tls_global_conf.cafile = NULL; |
| 94 |
tls_global_conf.crlfile = NULL; |
| 95 |
tls_global_conf.check_cert_cn_username = FALSE; |
| 96 |
|
| 97 |
/*Parse EAP TLS configuration file */ |
| 98 |
eaptlsin = fopen(tls_global_conf.conffile, "r"); |
| 99 |
if (!eaptlsin) |
| 100 |
{ |
| 101 |
int serr = errno; |
| 102 |
TRACE_DEBUG(INFO,"%s[EAP-TLS] Unable to open configuration file %s for reading: %s",DIAMEAP_EXTENSION, tls_global_conf.conffile, strerror(errno)); |
| 103 |
return serr; |
| 104 |
} |
| 105 |
|
| 106 |
/* call yacc parser */ |
| 107 |
CHECK_FCT(eaptlsparse(&tls_global_conf)); |
| 108 |
|
| 109 |
|
| 110 |
tls_global_conf.max_size = 64*1024 /* As per RFC 5216 recommendation */; |
| 111 |
|
| 112 |
/* Initializing GnuTLS library */ |
| 113 |
ret = diameap_tls_init(&tls_global_conf); |
| 114 |
|
| 115 |
return ret; |
| 116 |
} |
| 117 |
|
| 118 |
int eap_tls_init(struct eap_state_machine *smd) |
| 119 |
{ |
| 120 |
int ret; |
| 121 |
struct tls_data *data = NULL; |
| 122 |
CHECK_MALLOC(data = malloc(sizeof(struct tls_data))); |
| 123 |
memset(data, 0, sizeof(struct tls_data)); |
| 124 |
CHECK_FCT(diameap_tls_initialize(data)); |
| 125 |
ret = diameap_tls_init_session(&tls_global_conf, data); |
| 126 |
|
| 127 |
smd->methodData = (struct tls_data*) data; |
| 128 |
if (ret < 0) |
| 129 |
{ |
| 130 |
return ret; |
| 131 |
} |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
int eap_tls_initPickUp(struct eap_state_machine *smd) |
| 136 |
{ |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
int eap_tls_buildReq(struct eap_state_machine *smd, u8 id, |
| 141 |
struct eap_packet * eapPacket) |
| 142 |
{ |
| 143 |
struct tls_data * data; |
| 144 |
data = (struct tls_data *) smd->methodData; |
| 145 |
|
| 146 |
if (data->more_toreceive == TRUE) |
| 147 |
{ |
| 148 |
CHECK_FCT(diameap_eap_tls_buildReq_ack(id,eapPacket)); |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
if (data->state == START) |
| 153 |
{ |
| 154 |
CHECK_FCT(diameap_eap_tls_buildReq_start(id,eapPacket)); |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
if (data->state == CONTINUE) |
| 159 |
{ |
| 160 |
diameap_eap_tls_buildReq_data(data, id, eapPacket); |
| 161 |
|
| 162 |
smd->methodData = (struct tls_data*) data; |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
int eap_tls_getTimeout(struct eap_state_machine *smd, int * timeout) |
| 170 |
{ |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
boolean eap_tls_check(struct eap_state_machine *smd, |
| 175 |
struct eap_packet *eapRespData) |
| 176 |
{ |
| 177 |
eap_type type; |
| 178 |
if(diameap_eap_get_type(eapRespData,&type)!=0){ |
| 179 |
goto cf; |
| 180 |
} |
| 181 |
if (type == TYPE_EAP_TLS) |
| 182 |
{ |
| 183 |
return TRUE; |
| 184 |
} |
| 185 |
cf: |
| 186 |
TRACE_DEBUG(INFO,"%s[EAP-TLS] EAP-TLS check failed: Received EAP packet with different EAP-Type (Type = %d)",DIAMEAP_EXTENSION, type); |
| 187 |
return FALSE; |
| 188 |
} |
| 189 |
|
| 190 |
int eap_tls_process(struct eap_state_machine *smd, |
| 191 |
struct eap_packet *eapRespData) |
| 192 |
{ |
| 193 |
struct tls_data * data; |
| 194 |
data = (struct tls_data *) smd->methodData; |
| 195 |
struct tls_msg tlsmsg; |
| 196 |
CHECK_FCT(diameap_eap_tls_parse(&tlsmsg,eapRespData)); |
| 197 |
|
| 198 |
if ((tlsmsg.datalength == 0)) |
| 199 |
{ |
| 200 |
if (data->more_tosend_length > 0) |
| 201 |
{ |
| 202 |
//ACK and more to send |
| 203 |
return 0; |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
//Success |
| 208 |
if (data->handshake == TRUE) |
| 209 |
{ |
| 210 |
data->state = SUCCESS; |
| 211 |
smd->user.success = TRUE; |
| 212 |
|
| 213 |
if(tls_global_conf.check_cert_cn_username == TRUE){ |
| 214 |
unsigned int list_size; |
| 215 |
const gnutls_datum_t * list = gnutls_certificate_get_peers (data->session, &list_size); |
| 216 |
if(list_size<1){ |
| 217 |
goto failure; |
| 218 |
} |
| 219 |
|
| 220 |
gnutls_x509_crt_t cert; |
| 221 |
|
| 222 |
CHECK_GNUTLS_DO(gnutls_x509_crt_init(&cert),{ |
| 223 |
TRACE_DEBUG(NONE,"%s[EAP-TLS] [GnuTLS] error in initialization crt init",DIAMEAP_EXTENSION); |
| 224 |
goto failure;}); |
| 225 |
|
| 226 |
CHECK_GNUTLS_DO(gnutls_x509_crt_import(cert, &list[0], GNUTLS_X509_FMT_DER), { |
| 227 |
TRACE_DEBUG(NONE,"%s[EAP-TLS] [GnuTLS] error parsing certificate",DIAMEAP_EXTENSION); |
| 228 |
goto failure;}); |
| 229 |
|
| 230 |
void * buff; |
| 231 |
size_t size_buffer; |
| 232 |
int ret; |
| 233 |
ret = gnutls_x509_crt_get_dn_by_oid(cert,GNUTLS_OID_X520_COMMON_NAME,0,0,NULL,&size_buffer); |
| 234 |
if( ret != GNUTLS_E_SHORT_MEMORY_BUFFER){ |
| 235 |
CHECK_GNUTLS_DO(ret,{ |
| 236 |
TRACE_DEBUG(NONE,"%s[EAP-TLS] [GnuTLS] error get dn by oid",DIAMEAP_EXTENSION); |
| 237 |
goto failure;}); |
| 238 |
} |
| 239 |
|
| 240 |
CHECK_MALLOC_DO(buff=malloc(size_buffer), goto failure); |
| 241 |
|
| 242 |
CHECK_GNUTLS_DO(gnutls_x509_crt_get_dn_by_oid(cert,GNUTLS_OID_X520_COMMON_NAME,0,0,buff,&size_buffer),{ |
| 243 |
TRACE_DEBUG(NONE,"%s[EAP-TLS] [GnuTLS] error get dn by oid",DIAMEAP_EXTENSION); |
| 244 |
goto failure;}); |
| 245 |
|
| 246 |
if(strncmp((char *)smd->user.userid,buff,smd->user.useridLength)!=0){ |
| 247 |
goto failure; |
| 248 |
} |
| 249 |
|
| 250 |
gnutls_x509_crt_deinit(cert); |
| 251 |
goto next; |
| 252 |
|
| 253 |
failure: |
| 254 |
TRACE_DEBUG(NONE,"%s[EAP-TLS] Checking failed. certificate's CN does not match User_Name AVP value.",DIAMEAP_EXTENSION); |
| 255 |
data->state = FAILURE; |
| 256 |
smd->user.success = FALSE; |
| 257 |
gnutls_x509_crt_deinit(cert); |
| 258 |
} |
| 259 |
|
| 260 |
next: |
| 261 |
smd->methodData = (struct tls_data*) data; |
| 262 |
return 0; |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
if (data->more_toreceive == TRUE) |
| 272 |
{ |
| 273 |
//reassemble received fragment to TLS Response |
| 274 |
CHECK_FCT(diameap_tls_reassemble(&data->tlsResp,tlsmsg)); |
| 275 |
} |
| 276 |
else |
| 277 |
{ |
| 278 |
//receive the first fragment or a complete TLS message |
| 279 |
CHECK_FCT(diameap_tls_copy(&data->tlsResp,tlsmsg)); |
| 280 |
} |
| 281 |
|
| 282 |
if (tlsmsg.flags & TLS_FLAG_MORE) |
| 283 |
{ |
| 284 |
data->more_toreceive = TRUE; |
| 285 |
smd->methodData = (struct tls_data*) data; |
| 286 |
return 0; |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
//last fragment received |
| 291 |
data->more_toreceive = FALSE; |
| 292 |
} |
| 293 |
data->state = CONTINUE; |
| 294 |
diameap_tls_process_receive(data); |
| 295 |
|
| 296 |
if (data->state == SUCCESS) |
| 297 |
{ |
| 298 |
smd->user.success = TRUE; |
| 299 |
} |
| 300 |
smd->methodData = (struct tls_data*) data; |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
boolean eap_tls_isDone(struct eap_state_machine *smd) |
| 305 |
{ |
| 306 |
struct tls_data * data; |
| 307 |
data = (struct tls_data *) smd->methodData; |
| 308 |
if (data->state == CONTINUE || data->state == START) |
| 309 |
{ |
| 310 |
return FALSE; |
| 311 |
} |
| 312 |
return TRUE; |
| 313 |
} |
| 314 |
|
| 315 |
int eap_tls_getKey(struct eap_state_machine *smd, u8 ** msk, int *msklen, u8 ** emsk, int *emsklen) |
| 316 |
{ |
| 317 |
struct tls_data * data; |
| 318 |
int len = emsk ? 128 : 64; |
| 319 |
data = (struct tls_data *) smd->methodData; |
| 320 |
*msk = malloc(len); |
| 321 |
if (gnutls_prf(data->session, strlen("client EAP encryption"), |
| 322 |
"client EAP encryption", 0, 0, NULL, len, (char *) *msk) |
| 323 |
!= GNUTLS_E_SUCCESS) |
| 324 |
{ |
| 325 |
free(*msk); |
| 326 |
*msk = NULL; |
| 327 |
*msklen = 0; |
| 328 |
return 1; |
| 329 |
} |
| 330 |
else |
| 331 |
{ |
| 332 |
*msklen = 64; |
| 333 |
} |
| 334 |
if (emsk) { |
| 335 |
*emsk = malloc(64); |
| 336 |
memcpy(*emsk, (*msk)+64, 64); |
| 337 |
memset((*msk)+64, 0, 64); |
| 338 |
*emsklen = 64; |
| 339 |
} |
| 340 |
|
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
void eap_tls_unregister(void) |
| 345 |
{ |
| 346 |
// |
| 347 |
} |
| 348 |
|
| 349 |
void eap_tls_free(void * mdata) |
| 350 |
{ |
| 351 |
struct tls_data *data; |
| 352 |
data = (struct tls_data*) mdata; |
| 353 |
gnutls_deinit(data->session); |
| 354 |
if(data->tlsReq.data){ |
| 355 |
free(data->tlsReq.data); |
| 356 |
data->tlsReq.data=NULL; |
| 357 |
} |
| 358 |
if(data->tlsResp.data){ |
| 359 |
free(data->tlsResp.data); |
| 360 |
data->tlsResp.data=NULL; |
| 361 |
} |
| 362 |
free(data); |
| 363 |
data=NULL; |
| 364 |
} |
| 365 |
|
| 366 |
//send TLS ACK Request (empty TLS msg) |
| 367 |
int diameap_eap_tls_buildReq_ack(u8 id, struct eap_packet * eapPacket) |
| 368 |
{ |
| 369 |
u8* payload; |
| 370 |
struct tls_msg tlsmsg; |
| 371 |
int len; |
| 372 |
CHECK_FCT(diameap_tls_new(&tlsmsg)); |
| 373 |
CHECK_FCT(diameap_tls_new_tls_packet(&payload,&len,tlsmsg)); |
| 374 |
CHECK_FCT(diameap_eap_new(EAP_REQUEST,id,TYPE_EAP_TLS,payload,len,eapPacket)); |
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
// parse EAP TLS msg |
| 379 |
int diameap_eap_tls_parse(struct tls_msg * tlsmsg, struct eap_packet *eapPacket) |
| 380 |
{ |
| 381 |
u8 *datatls; |
| 382 |
int len; |
| 383 |
|
| 384 |
//initialize a new empty EAP TLS msg |
| 385 |
diameap_tls_new(tlsmsg); |
| 386 |
//retrieve the data field from EAP Packet |
| 387 |
diameap_eap_get_data(eapPacket, &datatls, &len); |
| 388 |
//parse EAP TLS msg |
| 389 |
diameap_tls_parse(datatls, len, tlsmsg); |
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
int diameap_eap_tls_buildReq_start(u8 id, struct eap_packet * eapPacket) |
| 394 |
{ |
| 395 |
u8* payload; |
| 396 |
struct tls_msg tlsmsg; |
| 397 |
int len; |
| 398 |
CHECK_FCT(diameap_tls_new(&tlsmsg)); |
| 399 |
CHECK_FCT(diameap_tls_set_flags(&tlsmsg,TLS_FLAG_START)); |
| 400 |
CHECK_FCT(diameap_tls_new_tls_packet(&payload,&len,tlsmsg)); |
| 401 |
CHECK_FCT(diameap_eap_new(EAP_REQUEST,id,TYPE_EAP_TLS,payload,len,eapPacket)); |
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
int diameap_eap_tls_buildReq_data(struct tls_data * data, int id, |
| 406 |
struct eap_packet * eapPacket) |
| 407 |
{ |
| 408 |
struct tls_msg tlsmsg; |
| 409 |
u8* datatosend; |
| 410 |
u8 * eaptls_data; |
| 411 |
int length = 0; |
| 412 |
|
| 413 |
diameap_tls_new(&tlsmsg); |
| 414 |
|
| 415 |
if (data->more_tosend_length == 0) |
| 416 |
{ |
| 417 |
//First fragment of message or the only fragment of message |
| 418 |
data->more_tosend_length = data->tlsReq.datalength; |
| 419 |
} |
| 420 |
if (data->more_tosend_length > tls_global_conf.max_size) |
| 421 |
{ |
| 422 |
//New fragment of message. Is not the last fragment. |
| 423 |
length = tls_global_conf.max_size; |
| 424 |
CHECK_FCT(diameap_tls_set_flags(&tlsmsg,TLS_FLAG_MORE)); |
| 425 |
if (data->more_tosend_length == data->tlsReq.datalength) |
| 426 |
{ |
| 427 |
//The first fragment of message |
| 428 |
CHECK_FCT(diameap_tls_set_message_length(&tlsmsg,data->tlsReq.datalength));//set L flag and length value |
| 429 |
} |
| 430 |
} |
| 431 |
else |
| 432 |
{ |
| 433 |
//The last fragment or the only fragment. |
| 434 |
length = data->more_tosend_length; |
| 435 |
} |
| 436 |
|
| 437 |
datatosend = malloc(sizeof(u8) * length); |
| 438 |
U8COPY(datatosend,0,length,data->tlsReq.data+(data->tlsReq.datalength-data->more_tosend_length)); |
| 439 |
data->more_tosend_length -= length; |
| 440 |
CHECK_FCT(diameap_tls_set_data(&tlsmsg,datatosend,length)); |
| 441 |
|
| 442 |
CHECK_FCT(diameap_tls_new_tls_packet(&eaptls_data,&length,tlsmsg)); |
| 443 |
CHECK_FCT(diameap_eap_new(EAP_REQUEST,id,TYPE_EAP_TLS,eaptls_data,length,eapPacket)); |
| 444 |
|
| 445 |
if (data->more_tosend_length == 0) |
| 446 |
{ |
| 447 |
diameap_tls_new(&data->tlsReq); |
| 448 |
} |
| 449 |
return 0; |
| 450 |
} |