Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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-2019, 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 #if defined(PANA) || defined(HAVE_WS)
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         return 5;
00068     }
00069     return 4;
00070 }
00071 
00072 uint8_t *eap_header_build(uint8_t *ptr, uint16_t data_length, uint8_t eap_code, uint8_t id_seq, uint8_t type)
00073 {
00074     *ptr++ = eap_code;
00075     *ptr++ = id_seq;
00076     ptr = common_write_16_bit(data_length, ptr);
00077     if (eap_code == EAP_REQ || eap_code == EAP_RESPONSE) {
00078         *ptr++ = type;
00079     }
00080     return ptr;
00081 
00082 }
00083 
00084 bool eap_tls_header_parse(uint8_t *eap_data_ptr, uint16_t eap_datalength, eap_tls_header_t *header)
00085 {
00086     header->tls_length = 0;
00087     if (eap_datalength ==  0) {
00088         return false;
00089     }
00090     header->eap_tls_flags = *eap_data_ptr++;
00091     eap_datalength--;
00092 
00093     if (header->eap_tls_flags & EAP_TLS_FRAGMENT_LENGTH) {
00094         if (eap_datalength < 4) {
00095             return false;
00096         }
00097         uint32_t temp32 = common_read_32_bit(eap_data_ptr);
00098         eap_datalength -= 4;
00099         if (temp32 > 0x0000ffff) {
00100             return false;
00101         }
00102         eap_data_ptr += 4;
00103         header->tls_length = (uint16_t)temp32;
00104     }
00105     header->tls_frame_length = eap_datalength;
00106     header->data_ptr = eap_data_ptr;
00107     return true;
00108 }
00109 
00110 uint8_t eap_tls_header_size(uint16_t eap_tls_flags)
00111 {
00112     if (eap_tls_flags & EAP_TLS_FRAGMENT_LENGTH) {
00113         return 5;
00114     }
00115     return 1;
00116 }
00117 
00118 uint8_t *eap_tls_header_build(uint8_t *ptr, uint16_t eap_tls_flags, uint16_t frame_length)
00119 {
00120     *ptr++ = eap_tls_flags;
00121     //Test EAP Length Field
00122     if (eap_tls_flags & EAP_TLS_FRAGMENT_LENGTH) {
00123         ptr = common_write_32_bit(frame_length, ptr);
00124     }
00125 
00126 
00127     return ptr;
00128 }
00129 #endif