Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pana_eap_header.c Source File

pana_eap_header.c

00001 /*
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "nsconfig.h"
00018 #include "ns_types.h"
00019 #include "string.h"
00020 #include "common_functions.h"
00021 #include "Security/PANA/pana_eap_header.h"
00022 #ifdef PANA
00023 
00024 bool eap_header_parse(uint8_t *data_ptr, uint16_t length, eap_header_t *header)
00025 {
00026     if (length < 4) {
00027         return false;
00028     }
00029 
00030     header->eap_code = *data_ptr++;
00031     header->id_seq = *data_ptr++;
00032     header->length = common_read_16_bit(data_ptr);
00033     header->type = 0;
00034     data_ptr += 2;
00035     if (header->length < length || header->length > length) {
00036         return false;
00037     }
00038 
00039 
00040     switch (header->eap_code) {
00041         case EAP_REQ:
00042         case EAP_RESPONSE:
00043             if (header->length < 5) {
00044                 return false;
00045             }
00046             header->type = *data_ptr++;
00047             break;
00048 
00049         case EAP_SUCCESS:
00050         case EAP_FAILURE:
00051             if (header->length != 4) {
00052                 return false;
00053             }
00054             break;
00055 
00056         default:
00057             return false;
00058     }
00059     header->data_ptr = data_ptr;
00060     return true;
00061 
00062 }
00063 
00064 uint8_t eap_header_size(uint8_t eap_code)
00065 {
00066     if (eap_code == EAP_REQ || eap_code ==EAP_RESPONSE)
00067     {
00068         return 5;
00069     }
00070     return 4;
00071 }
00072 
00073 uint8_t *eap_header_build(uint8_t *ptr, uint16_t data_length, uint8_t eap_code, uint8_t id_seq, uint8_t type)
00074 {
00075     *ptr++ = eap_code;
00076     *ptr++ = id_seq;
00077     ptr = common_write_16_bit(data_length, ptr);
00078     if (eap_code == EAP_REQ || eap_code ==EAP_RESPONSE) {
00079         *ptr++ = type;
00080     }
00081     return ptr;
00082 
00083 }
00084 
00085 bool eap_tls_header_parse(uint8_t *eap_data_ptr, uint16_t eap_datalength, eap_tls_header_t *header)
00086 {
00087     header->tls_length = 0;
00088     if (eap_datalength ==  0) {
00089         return false;
00090     }
00091     header->eap_tls_flags = *eap_data_ptr++;
00092     eap_datalength--;
00093 
00094     if (header->eap_tls_flags & EAP_TLS_FRAGMENT_LENGTH) {
00095         if (eap_datalength < 4) {
00096             return false;
00097         }
00098         uint32_t temp32 = common_read_32_bit(eap_data_ptr);
00099         eap_datalength -= 4;
00100         if (temp32 > 0x0000ffff) {
00101             return false;
00102         }
00103         eap_data_ptr += 4;
00104         header->tls_length = (uint16_t)temp32;
00105     }
00106     header->tls_frame_length = eap_datalength;
00107     header->data_ptr = eap_data_ptr;
00108     return true;
00109 }
00110 
00111 uint8_t eap_tls_header_size(uint16_t eap_tls_flags)
00112 {
00113     if (eap_tls_flags & EAP_TLS_FRAGMENT_LENGTH) {
00114         return 5;
00115     }
00116     return 1;
00117 }
00118 
00119 uint8_t *eap_tls_header_build(uint8_t *ptr, uint16_t eap_tls_flags, uint16_t frame_length)
00120 {
00121     *ptr++ = eap_tls_flags;
00122     //Test EAP Length Field
00123     if (eap_tls_flags & EAP_TLS_FRAGMENT_LENGTH) {
00124         ptr = common_write_32_bit(frame_length, ptr);
00125     }
00126 
00127 
00128     return ptr;
00129 }
00130 #endif