Annotation of embedaddon/strongswan/src/libcharon/plugins/eap_peap/eap_peap_avp.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2011 Andreas Steffen
                      3:  * HSR 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_peap_avp.h"
                     17: 
                     18: #include <eap/eap.h>
                     19: #include <utils/debug.h>
                     20: 
                     21: /**
                     22:  * Microsoft Success and Failure Result AVPs
                     23:  */
                     24: static const chunk_t MS_AVP_Success = chunk_from_chars(
                     25:                                                                                        0x80, 0x03, 0x00, 0x02, 0x00, 0x01);
                     26: static const chunk_t MS_AVP_Failure = chunk_from_chars(
                     27:                                                                                        0x80, 0x03, 0x00, 0x02, 0x00, 0x02);
                     28: 
                     29: typedef struct private_eap_peap_avp_t private_eap_peap_avp_t;
                     30: 
                     31: /**
                     32:  * Private data of an eap_peap_avp_t object.
                     33:  */
                     34: struct private_eap_peap_avp_t {
                     35: 
                     36:        /**
                     37:         * Public eap_peap_avp_t interface.
                     38:         */
                     39:        eap_peap_avp_t public;
                     40: 
                     41:        /**
                     42:         * EAP server or peer
                     43:         */
                     44:        bool is_server;
                     45: };
                     46: 
                     47: METHOD(eap_peap_avp_t, build, void,
                     48:        private_eap_peap_avp_t *this, bio_writer_t *writer, chunk_t data)
                     49: {
                     50:        uint8_t code;
                     51:        eap_packet_t *pkt;
                     52:        chunk_t avp_data;
                     53: 
                     54:        pkt = (eap_packet_t*)data.ptr;
                     55: 
                     56:        if (pkt->code == EAP_SUCCESS || pkt->code == EAP_FAILURE)
                     57:        {
                     58:                code = (this->is_server) ? EAP_REQUEST : EAP_RESPONSE;
                     59:                writer->write_uint8(writer, code);
                     60:                writer->write_uint8(writer, pkt->identifier);
                     61:                writer->write_uint16(writer, 11);
                     62:                writer->write_uint8(writer, EAP_MSTLV);
                     63:                avp_data = (pkt->code == EAP_SUCCESS) ? MS_AVP_Success : MS_AVP_Failure;
                     64:        }
                     65:        else
                     66:        {
                     67:                avp_data = chunk_skip(data, 4);
                     68:        }
                     69:        writer->write_data(writer, avp_data);
                     70: }
                     71: 
                     72: METHOD(eap_peap_avp_t, process, status_t,
                     73:        private_eap_peap_avp_t* this, bio_reader_t *reader, chunk_t *data,
                     74:        uint8_t identifier)
                     75: {
                     76:        uint8_t code;
                     77:        uint16_t len;
                     78:        eap_packet_t *pkt;
                     79:        chunk_t avp_data;
                     80: 
                     81:        code = (this->is_server) ? EAP_RESPONSE : EAP_REQUEST;
                     82:        len = reader->remaining(reader);
                     83:        if (!reader->read_data(reader, len, &avp_data))
                     84:        {
                     85:                return FAILED;
                     86:        }
                     87:        pkt = (eap_packet_t*)avp_data.ptr;
                     88: 
                     89:        if (len > 4 && pkt->code == code && untoh16(&pkt->length) == len)
                     90:        {
                     91:                if (len == 5 && pkt->type == EAP_IDENTITY)
                     92:                {
                     93:                        DBG2(DBG_IKE, "uncompressed EAP Identity request");
                     94:                        *data = chunk_clone(avp_data);
                     95:                        return SUCCESS;
                     96:                }
                     97:                else if (len == 11 && pkt->type == EAP_MSTLV)
                     98:                {
                     99:                        if (memeq(&pkt->data, MS_AVP_Success.ptr, MS_AVP_Success.len))
                    100:                        {
                    101:                                DBG2(DBG_IKE, "MS Success Result AVP");
                    102:                                code = EAP_SUCCESS;
                    103:                        }
                    104:                        else if (memeq(&pkt->data, MS_AVP_Failure.ptr, MS_AVP_Failure.len))
                    105:                        {
                    106:                                DBG2(DBG_IKE, "MS Failure Result AVP");
                    107:                                code = EAP_FAILURE;
                    108:                        }
                    109:                        else
                    110:                        {
                    111:                                DBG1(DBG_IKE, "unknown MS AVP message");
                    112:                                return FAILED;
                    113:                        }
                    114:                        identifier = pkt->identifier;
                    115:                        len = 0;
                    116:                }
                    117:        }
                    118: 
                    119:        *data = chunk_alloc(4 + len);
                    120:        pkt = (eap_packet_t*)data->ptr;
                    121:        pkt->code = code;
                    122:        pkt->identifier = identifier;
                    123:        htoun16(&pkt->length, data->len);
                    124:        memcpy(data->ptr + 4, avp_data.ptr, len);
                    125: 
                    126:        return SUCCESS;
                    127: }
                    128: 
                    129: METHOD(eap_peap_avp_t, destroy, void,
                    130:        private_eap_peap_avp_t *this)
                    131: {
                    132:        free(this);
                    133: }
                    134: 
                    135: /**
                    136:  * See header
                    137:  */
                    138: eap_peap_avp_t *eap_peap_avp_create(bool is_server)
                    139: {
                    140:        private_eap_peap_avp_t *this;
                    141: 
                    142:        INIT(this,
                    143:                .public= {
                    144:                        .process = _process,
                    145:                        .build = _build,
                    146:                        .destroy = _destroy,
                    147:                },
                    148:                .is_server = is_server,
                    149:        );
                    150: 
                    151:        return &this->public;
                    152: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>