Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tls_lib.c Source File

tls_lib.c

00001 /*
00002  * Copyright (c) 2013-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 #ifdef PANA
00020 #include "string.h"
00021 #include "Core/include/address.h"
00022 #include "Core/include/ns_buffer.h"
00023 #ifdef ECC
00024 #include "libX509_V3.h"
00025 #include "ecc.h"
00026 #endif
00027 #include "randLIB.h"
00028 #include "Core/include/socket.h"
00029 //#include "6LoWPAN/Bootstraps/network_lib.h"
00030 #include "shalib.h"
00031 #include "Security/TLS/tls_lib.h"
00032 #include "Security/TLS/tls_ccm_crypt.h"
00033 #include "Security/Common/sec_lib.h"
00034 #include "nsdynmemLIB.h"
00035 #include "net_nvm_api.h"
00036 #include "Security/PANA/pana.h"
00037 #include "Security/PANA/pana_internal_api.h"
00038 #include "ns_trace.h"
00039 #include "common_functions.h"
00040 #include "net_interface.h"
00041 
00042 #define TRACE_GROUP "TLSl"
00043 
00044 typedef struct tls_psk_key_ {
00045     uint16_t key_id;
00046     uint8_t psk_key[16];
00047     ns_list_link_t link;
00048 } tls_psk_key_t;
00049 
00050 static NS_LIST_DEFINE(tls_psk_list, tls_psk_key_t, link);
00051 
00052 NS_LARGE tls_header_t tls_header;
00053 NS_LARGE tls_msg_t tls_msg;
00054 
00055 #ifdef ECC
00056 
00057 tls_ecc_heap_t *ecc_allocate_ram(void);
00058 static uint8_t tls_parse_certificate(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite);
00059 static uint8_t tls_parse_certificate_verify(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite);
00060 static uint8_t ecc_verify_calculate_hash(sec_suite_t *tls_suite);
00061 void tls_ecc_reverse_hash(uint8_t *ptr);
00062 uint8_t tls_write_signature_parameters(uint8_t *ptr, uint8_t *signature_parameter, uint8_t leadin_zeros);
00063 uint8_t tls_parse_certificate_request(uint8_t *ptr, uint16_t len);
00064 static uint8_t tls_parse_client_key_exchange(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite);
00065 static uint8_t tls_parse_server_key_exchange(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite);
00066 
00067 #endif
00068 
00069 static uint8_t *tls_set_client_key_excange(uint8_t *ptr, sec_suite_t *tls_suite);
00070 static uint8_t tls_parse_server_hello(uint8_t *ptr, sec_suite_t *tls_suite);
00071 static uint8_t tls_parse_client_hello(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite);
00072 static tls_psk_key_t *tls_get_key(uint16_t key_id);
00073 
00074 tls_session_t *amr_tls_session_allocate(void) {
00075     tls_session_t *t_session = 0;
00076     t_session = ns_dyn_mem_alloc(sizeof(tls_session_t));
00077     if (t_session) {
00078         memset(t_session, 0, sizeof(tls_session_t));
00079     }
00080     return t_session;
00081 }
00082 
00083 void arm_tls_session_clear(tls_session_t *t_session)
00084 {
00085     if (t_session->tls_heap) {
00086         tls_heap_free(t_session->tls_heap);
00087         t_session->tls_heap = NULL;
00088     }
00089 }
00090 
00091 static tls_heap_t * tls_heap_structure_allocate(void) {
00092     tls_heap_t *heap_ptr = (tls_heap_t *) ns_dyn_mem_temporary_alloc(sizeof(tls_heap_t));
00093     if (heap_ptr) {
00094         memset(heap_ptr, 0, sizeof(tls_heap_t));
00095     }
00096     return heap_ptr;
00097 }
00098 
00099 int8_t arm_tls_add_psk_key(const uint8_t *key_ptr, uint16_t key_id)
00100 {
00101     tls_psk_key_t *key_entry = tls_get_key(key_id);
00102 
00103     if (key_ptr == NULL) {
00104         return -1;
00105     }
00106 
00107     /* If key with given ID already exists, remove old */
00108     if (key_entry) {
00109         arm_tls_remove_psk_key(key_id);
00110         key_entry = NULL;
00111     }
00112 
00113     /* Make new entry */
00114     key_entry = ns_dyn_mem_alloc(sizeof(tls_psk_key_t));
00115     if (key_entry == NULL) {
00116         return -1;
00117     }
00118 
00119     memcpy(key_entry->psk_key, key_ptr, 16);
00120     key_entry->key_id = key_id;
00121 
00122     ns_list_add_to_end(&tls_psk_list, key_entry);
00123 
00124     return 0;
00125 }
00126 
00127 int8_t arm_tls_check_key(uint16_t key_id)
00128 {
00129     if (tls_get_key(key_id) == NULL) {
00130         return -1;
00131     }
00132     return 0;
00133 }
00134 
00135 int8_t arm_tls_remove_psk_key(uint16_t key_id)
00136 {
00137     tls_psk_key_t *key_to_remove = tls_get_key(key_id);
00138 
00139     if (key_to_remove == NULL) {
00140         return -1;
00141     }
00142 
00143     ns_list_remove(&tls_psk_list, key_to_remove);
00144 
00145     ns_dyn_mem_free(key_to_remove);
00146 
00147     return 0;
00148 }
00149 
00150 /* returns key entry, null if not found */
00151 static tls_psk_key_t *tls_get_key(uint16_t key_id)
00152 {
00153     ns_list_foreach(tls_psk_key_t, entry, &tls_psk_list) {
00154         if (entry->key_id == key_id) {
00155             return entry;
00156         }
00157     }
00158     return NULL;
00159 }
00160 
00161 
00162 
00163 void tls_finnish_copy(uint8_t *ptr, tls_heap_t *heap_ptr)
00164 {
00165     tls_msg_t *tmp_msg = tls_msg_ptr_get();
00166     tmp_msg->len = 12;
00167     tmp_msg->msg_ptr = ptr;
00168     tls_handshake_copy(tmp_msg, heap_ptr);
00169 
00170 }
00171 
00172 uint8_t tls_parse_client_hello(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite)
00173 {
00174     uint8_t ret_val = 0, i = 0;
00175     uint16_t tls_version;
00176     tls_version = common_read_16_bit(ptr);
00177     ptr += 2;
00178     if (tls_version == TLS_1_2_VERSION) {
00179         uint8_t id_len;
00180         tls_heap_t *thep = tls_suite->tls_session->tls_heap;
00181         memcpy((thep->tls_hello_random + CLIENT_HELLO_PTR), ptr, 32);
00182         ptr += 32;
00183         //skip sesion id
00184         id_len = *ptr++;
00185         len -= 35;
00186         if (id_len < 33 && (len > id_len + 3)) {
00187             if (id_len == 0) {
00188                 tr_debug("Fisrt Time generate ID!!");
00189                 len -= 2;
00190                 tls_session_id_genrate(tls_suite->tls_session->tls_session_id, 4);
00191                 tls_suite->tls_session->id_length = 4;
00192             } else {
00193                 if (tls_suite->tls_session->id_length == id_len) {
00194                     if (memcmp(tls_suite->tls_session->tls_session_id, ptr, id_len) == 0) {
00195                         tr_debug("Generate new Session");
00196                         tls_session_id_genrate(tls_suite->tls_session->tls_session_id, 4);
00197                         tls_suite->tls_session->id_length = 4;
00198 
00199                     }
00200                 }
00201 
00202                 if (ret_val != 0) {
00203                     tr_debug("TLS SESSION ID FAIL");
00204                     return 0;
00205                 }
00206                 ptr += id_len;
00207                 len -= id_len;
00208                 len -= 1;
00209             }
00210             ptr++;
00211 
00212             id_len = *ptr++;
00213             ret_val = 0;
00214 
00215             while (id_len) {
00216                 if (len < 2) {
00217                     tr_debug("Cor Client hello pack");
00218                     return 0;
00219                 }
00220                 uint16_t tls_ciphersuite = common_read_16_bit(ptr);
00221                 switch (tls_ciphersuite) {
00222                     case TLS_PSK_WITH_AES_128_CCM_8:
00223                         tr_debug("Client Sup PSK");
00224                         ret_val |= SEC_CIPHERSUITE_PSK;
00225                         break;
00226 #ifdef ECC
00227                     case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
00228                         thep->client_knows_standard_ecc_ciphersuite = true;
00229                         /* no break */
00230                     case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_COMPAT:
00231                         tr_debug("Client Sup ECC");
00232                         ret_val |= SEC_CIPHERSUITE_ECC;
00233                         break;
00234 #endif
00235                     default:
00236                         tr_debug("Un Sup Suite: %04" PRIx16, tls_ciphersuite);
00237                         break;
00238                 }
00239                 ptr += 2;
00240                 id_len -= 2;
00241                 len -= 2;
00242 
00243             }
00244             ret_val &= tls_suite->supported_chipher_suites;
00245             if (ret_val) {
00246                 tr_debug("Client pack TRUE");
00247 
00248                 if (ret_val & SEC_CIPHERSUITE_ECC) {
00249                     thep->tls_chipher_mode = CHIPHER_ECC;
00250                 } else {
00251                     thep->tls_chipher_mode = CHIPHER_PSK;
00252                 }
00253             } else {
00254                 tr_debug("CipherSuite Err");
00255                 i = 2;
00256             }
00257         } else {
00258             tr_debug("Session ID length Fail: %02x", *ptr);
00259             i = 4;
00260         }
00261     } else {
00262         i = 3;
00263     }
00264     if (i) {
00265         tr_debug("%02x", i);
00266     }
00267     return ret_val;
00268 }
00269 
00270 
00271 uint8_t tls_parse_server_hello(uint8_t *ptr, sec_suite_t *tls_suite)
00272 {
00273     uint8_t ret_val = 0, i = 0;
00274     uint16_t tls_version;
00275     tls_version = common_read_16_bit(ptr);
00276     ptr += 2;
00277     if (tls_version == TLS_1_2_VERSION) {
00278         uint8_t id_len;
00279         tls_heap_t *thep = tls_suite->tls_session->tls_heap;
00280         if (thep == 0) {
00281             return 0;
00282         }
00283         memcpy((thep->tls_hello_random + SERVER_HELLO_PTR), ptr, 32);
00284         ptr += 32;
00285         //skip sesion id
00286         id_len = *ptr++;
00287         if (id_len < 33) {
00288             tls_suite->tls_session->id_length = id_len;
00289             memcpy(tls_suite->tls_session->tls_session_id, ptr, id_len);
00290             ptr += id_len;
00291             uint16_t tls_cipher_suite = common_read_16_bit(ptr);
00292             switch (tls_cipher_suite) {
00293                 case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
00294                 case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_COMPAT:
00295                     tr_debug("ECC CipherSuite");
00296                     ret_val = 2;
00297                     thep->tls_chipher_mode = CHIPHER_ECC;
00298                     break;
00299                 case TLS_PSK_WITH_AES_128_CCM_8:
00300                     tr_debug("PSK CipherSuite");
00301                     ret_val = 1;
00302                     thep->tls_chipher_mode = CHIPHER_PSK;
00303                     tls_ecc_heap_free(thep);
00304                     break;
00305                 default:
00306                     tr_debug("CipherSuite Err: %04x", tls_cipher_suite);
00307                     ptr += 2;
00308                     i = 2;
00309                     break;
00310             }
00311         } else {
00312             tr_debug("Session ID length Fail: %02x", *ptr);
00313             i = 4;
00314         }
00315     } else {
00316         i = 3;
00317     }
00318     if (i) {
00319         tr_debug("%02x", i);
00320     }
00321     return ret_val;
00322 }
00323 
00324 
00325 void tls_alert_build(buffer_t *buf, uint8_t alert)
00326 {
00327     tr_debug("TTLs TX:Alert");
00328     //Build Client Hello
00329     buffer_data_clear(buf);
00330     buffer_push_uint8(buf, TLS_ALERT_TYPE);
00331     buffer_push_uint16(buf, TLS_1_2_VERSION);
00332     buffer_push_uint16(buf, 0x0002); //Length
00333 
00334     if (alert == ALERT_CLOSE) {
00335         buffer_push_uint8(buf, 1); //Mean 2=Fatal, 1=warming
00336     } else {
00337         buffer_push_uint8(buf, 2); //Mean 2=Fatal, 1=warming
00338     }
00339     buffer_push_uint8(buf, alert);
00340 }
00341 
00342 #ifdef PANA_SERVER_API
00343 void tls_server_hello_build(buffer_t *buf,  sec_suite_t *tls_suite)
00344 {
00345     uint16_t tls_len = 0;
00346     uint8_t *ptr;
00347     tls_msg_t *tmp_msg = tls_msg_ptr_get();
00348     tr_debug("TTLs TX:Server Hello");
00349     //Build Client Hello
00350     buffer_data_clear(buf);
00351     ptr = buffer_data_pointer(buf);
00352     *ptr++ = TLS_HANDSHAKE;
00353     ptr = common_write_16_bit(TLS_1_2_VERSION, ptr);
00354     ptr = common_write_16_bit(tls_len, ptr);
00355 
00356     //for Hash calculation
00357     tmp_msg->msg_ptr = ptr + 4;
00358     ptr = tls_build_server_hello_msg(ptr, tls_suite->tls_session);
00359     buffer_data_end_set(buf, ptr);
00360 
00361     tls_len = buffer_data_length(buf);
00362     tls_len -= 9;
00363     tmp_msg->len = tls_len;
00364     tls_handshake_copy(tmp_msg, tls_suite->tls_session->tls_heap);
00365 
00366     tls_len += 4;
00367     ptr = buffer_data_pointer(buf);
00368     ptr += 3;
00369     ptr = common_write_16_bit(tls_len, ptr);
00370 }
00371 #endif
00372 
00373 void tls_prepare_change_chipher_spec(sec_suite_t *tls_suite)
00374 {
00375     tls_heap_t *theap = tls_suite->tls_session->tls_heap;
00376     bool server;
00377     tls_build_client_verify_payload(theap);
00378     if ((tls_suite->setups & TLS_SERVER_MODE) == 0) {
00379         server = false;
00380         if ((tls_suite->setups & TLS_HANSHAKE_HASH) == 0) {
00381             tls_finnish_copy(theap->hash_buf + 4, theap);
00382             tls_suite->setups |= TLS_HANSHAKE_HASH;
00383         }
00384 
00385     } else {
00386         server = true;
00387     }
00388 
00389     tls_ccm_data_encrypt(theap->hash_buf, 16,tls_suite->tls_session->key_expansion, tls_suite->tls_session->tls_nonce_explit, TLS_HANDSHAKE, server);
00390 }
00391 
00392 
00393 static buffer_t *tls_down(buffer_t *buf)
00394 {
00395     uint16_t tls_len;
00396     if ((buf = buffer_headroom(buf, 5)) != 0) {
00397         uint8_t *ptr;
00398         buffer_data_reserve_header(buf, 5);
00399         ptr = buffer_data_pointer(buf);
00400         tls_len = buffer_data_length(buf);
00401         tls_len -= 5; //Cut Flags byte off
00402 
00403         *ptr++ = TLS_HANDSHAKE;
00404         ptr = common_write_16_bit(TLS_1_2_VERSION, ptr);
00405         ptr = common_write_16_bit(tls_len, ptr);
00406     }
00407     return (buf);
00408 }
00409 
00410 #ifdef ECC
00411 
00412 void tls_parse_subject_get_pub_key_from_chain(tls_heap_t *theap, uint8_t rd_ptr)
00413 {
00414     uint8_t *ptr = 0;
00415     certificate_info_t *cer_info = &(theap->rx_ceri_chain.certi_chain[rd_ptr]);
00416     uint8_t i;
00417     ptr = cer_info->pub_key_ptr;
00418     theap->ecc_heap->cert_pub_key.finite = 1;
00419     theap->ecc_heap->cert_pub_key.invalid = 0;
00420 
00421     memset(theap->ecc_heap->cert_pub_key.x.data, 0, sizeof(MPint));
00422     memset(theap->ecc_heap->cert_pub_key.y.data, 0, sizeof(MPint));
00423     tr_debug("Certificates PUB Key: %s", tr_array(ptr, 64));
00424     for (i = 0; i < 32; i++) {
00425 
00426         *((uint8_t *)theap->ecc_heap->cert_pub_key.x.data + i) = *(ptr + 31 - i) ;
00427         *((uint8_t *)theap->ecc_heap->cert_pub_key.y.data + i) = *(ptr + 63 - i) ;
00428 
00429     }
00430 }
00431 
00432 
00433 
00434 uint8_t tls_parse_certificate(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite)
00435 {
00436 
00437     uint16_t sub_len = 0;
00438     uint8_t ret_val = 0;
00439     tls_heap_t *theap = tls_suite->tls_session->tls_heap;
00440     //Check Lengths
00441     if (*ptr++) {
00442         tr_debug("Too Long len");
00443     } else {
00444         sub_len = common_read_16_bit(ptr);
00445         ptr += 2;
00446         len -= 3;
00447         tr_debug("Certi(Chain) Len: %04x", sub_len);
00448         if (sub_len == 0 || sub_len > len) {
00449             tr_debug("Cert Base len mis match");
00450         } else {
00451             if (x509_v3_certi_chain_analyze(ptr, sub_len, &(theap->rx_ceri_chain)) == 0) {
00452                 tr_debug("Certificate Chain not valid");
00453             } else {
00454                 tls_parse_subject_get_pub_key_from_chain(theap, 0);
00455                 ret_val = 1;
00456             }
00457         }
00458     }
00459     return ret_val;
00460 
00461 }
00462 
00463 
00464 uint8_t tls_parse_server_key_exchange(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite)
00465 {
00466     (void)len;
00467     uint16_t curve_type;
00468     if (*ptr++ == 3) {
00469         curve_type = common_read_16_bit(ptr);
00470         if (curve_type == TLS_NAMED_CURVE_SECP256R1) {
00471             uint8_t c_len = 0, mode;
00472             uint16_t sig_algh, signature_len;
00473             tls_heap_t *theap = tls_suite->tls_session->tls_heap;
00474             ptr += 2;
00475             //len -= 5;
00476             c_len = *ptr++;
00477             mode = *ptr++;
00478 
00479             if (mode == 4) {
00480                 c_len--;
00481                 memcpy(theap->ecc_heap->server_public_key, ptr, 64);
00482                 ptr += 64;
00483                 sig_algh = common_read_16_bit(ptr);
00484                 if (sig_algh != TLS_SIG_HASH_ALG_SHA256_ECDSA) {
00485                     tr_debug("tls ser key ex. er");
00486                     return 0;
00487                 }
00488                 ptr += 2;
00489                 signature_len = common_read_16_bit(ptr);
00490                 if (signature_len < 11) {
00491                     return 0;
00492                 }
00493                 ptr += 2;
00494                 if (*ptr++ != 0x30) {
00495                     return 0;
00496                 }
00497                 if (*ptr++ < 9) {
00498                     return 0;
00499                 }
00500                 if (*ptr++ != 2) {
00501                     return 0;
00502                 } else {
00503                     uint8_t rslen;
00504                     //uint8_t i;
00505                     theap->key_signature_ptr = ptr;
00506                     rslen = *ptr++;
00507 
00508                     if (rslen > 33) {
00509                         return 0;
00510                     } else if (rslen == 0) {
00511                         return 0;
00512                     } else if (rslen == 33) {
00513 
00514                         ptr++;
00515                         rslen = 32;
00516 
00517                     }
00518                     if (theap->ecc_heap->sgnt == 0) {
00519                         theap->ecc_heap->sgnt = ECDSA_get_signature();
00520                     }
00521                     if (!theap->ecc_heap->sgnt) {
00522                         return 0;
00523                     }
00524 
00525                     ptr += rslen;
00526                     if (*ptr++ != 2) {
00527                         return 0;
00528                     }
00529 
00530                     rslen = *ptr++;
00531 
00532                     if (rslen > 33) {
00533                         return 0;
00534                     } else if (rslen == 0) {
00535                         return 0;
00536                     }
00537                     return 1;
00538                 }
00539 
00540             } else {
00541                 tr_debug("Mode!: %02x", mode);
00542             }
00543         }
00544     }
00545     return 0;
00546 }
00547 
00548 static uint8_t tls_parse_client_key_exchange(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite)
00549 {
00550     (void)len;
00551     uint8_t d_len = 0;
00552     uint8_t mode = 0;
00553     d_len = *ptr++;
00554     mode = *ptr++;
00555     if (d_len == 65 && mode == 4) {
00556         tls_heap_t *theap = tls_suite->tls_session->tls_heap;
00557         tr_debug("Valid Client ECC curve:");
00558         memcpy(theap->ecc_heap->client_public_key, ptr, 64);
00559         tr_debug("%s", tr_array(ptr, 64));
00560         return 1;
00561     } else {
00562         tr_debug("Len: %02x, mode fail: %02x", d_len, mode);
00563     }
00564 
00565     return 0;
00566 }
00567 
00568 
00569 void tls_read_certi_signature(tls_heap_t *theap, uint8_t certificate)
00570 {
00571     uint8_t rslen = 0;
00572     uint8_t *ptr = 0;
00573     if (certificate) {
00574         //
00575         certificate_info_t *cer_info = &(theap->rx_ceri_chain.certi_chain[theap->rx_ceri_chain.rd_ptr]);
00576         ptr = cer_info->signature_ptr;
00577     } else {
00578         ptr = theap->key_signature_ptr;
00579     }
00580     memset(theap->ecc_heap->sgnt->m_R.data, 0, sizeof(MPint));
00581     memset(theap->ecc_heap->sgnt->m_s.data, 0, sizeof(MPint));
00582     rslen  = x509_v3_parse_signature_parameter(ptr, ((uint8_t *)theap->ecc_heap->sgnt->m_R.data));
00583     ptr += rslen;
00584     ptr++;
00585     x509_v3_parse_signature_parameter(ptr, ((uint8_t *)theap->ecc_heap->sgnt->m_s.data));
00586 
00587 }
00588 
00589 
00590 uint8_t tls_parse_certificate_verify(uint8_t *ptr, uint16_t len, sec_suite_t *tls_suite)
00591 {
00592     (void)len;
00593     uint16_t sig_algh, sig_len;
00594     tls_heap_t *theap = tls_suite->tls_session->tls_heap;
00595 
00596     sig_algh = common_read_16_bit(ptr);
00597     if (sig_algh != TLS_SIG_HASH_ALG_SHA256_ECDSA) {
00598         tr_debug("tls ser key ex. er2");
00599         return 0;
00600     }
00601     ptr += 2;
00602     sig_len = common_read_16_bit(ptr);
00603     if (sig_len < 11) {
00604         return 0;
00605     }
00606     ptr += 2;
00607     if (*ptr++ != 0x30) {
00608         return 0;
00609     }
00610     if (*ptr++ < 9) {
00611         return 0;
00612     }
00613     if (theap->ecc_heap) {
00614         if (theap->ecc_heap->sgnt == 0) {
00615             tr_debug("Allocate SIG");
00616             theap->ecc_heap->sgnt = ECDSA_get_signature();
00617 
00618             if (!theap->ecc_heap->sgnt) {
00619                 tr_debug("Signature Allocate Fail");
00620                 return 0;
00621             }
00622         }
00623     } else {
00624         return 0;
00625     }
00626     memset(theap->ecc_heap->sgnt->m_R.data, 0, sizeof(MPint));
00627     memset(theap->ecc_heap->sgnt->m_s.data, 0, sizeof(MPint));
00628 
00629 
00630     if (*ptr++ != 2) {
00631         return 0;
00632     } else {
00633         uint8_t rslen = 0;
00634         theap->key_signature_ptr = ptr;
00635         rslen  = x509_v3_parse_signature_parameter(ptr, ((uint8_t *)theap->ecc_heap->sgnt->m_R.data));
00636         if (rslen == 0) {
00637             return 0;
00638         }
00639         ptr += rslen;
00640     }
00641     if (*ptr++ != 2) {
00642         return 0;
00643     } else {
00644         uint8_t rslen = 0;
00645         rslen  = x509_v3_parse_signature_parameter(ptr, ((uint8_t *)theap->ecc_heap->sgnt->m_s.data));
00646         if (rslen == 0) {
00647             return 0;
00648         }
00649         //ptr += rslen;
00650     }
00651     return 1;
00652 }
00653 
00654 
00655 tls_ecc_heap_t *ecc_allocate_ram(void)
00656 {
00657     tls_ecc_heap_t *heap_ptr = (tls_ecc_heap_t *) ns_dyn_mem_temporary_alloc(sizeof(tls_ecc_heap_t));
00658 
00659     if (heap_ptr == 0) {
00660         tr_debug("ECC variable malloc fail");
00661     } else {
00662         //heap_scan();
00663         heap_ptr->sgnt = ecc_get_ecdsa_signature();
00664         if (!heap_ptr->sgnt) {
00665             tr_debug("Signature Fail");
00666             ns_dyn_mem_free(heap_ptr);
00667             heap_ptr = 0;
00668         }
00669     }
00670     return heap_ptr;
00671 
00672 }
00673 
00674 
00675 uint8_t *tls_client_key_exchange_msg_set(uint8_t *ptr , tls_heap_t *heap_ptr)
00676 {
00677     /* Client Key Exchange */
00678     *ptr++ = TLS_CLIENT_KEY_EXCHANGE;
00679     ptr = common_write_24_bit(66, ptr);
00680     ptr = common_write_16_bit(0x4104, ptr);
00681     //There shuold be calculated case now only test purpose
00682     memcpy(ptr, heap_ptr->ecc_heap->client_public_key, 64);
00683     ptr += 64;
00684     return ptr;
00685 }
00686 #endif
00687 
00688 
00689 uint8_t *tls_set_client_key_excange(uint8_t *ptr, sec_suite_t *tls_suite)
00690 {
00691     uint8_t key_id_len = (tls_suite->psk_key_id > 0xFF) ? 2 : 1;
00692 
00693     /* TLS plaintext header = [type][version][length] */
00694     *ptr++ = TLS_HANDSHAKE;
00695     ptr = common_write_16_bit(TLS_1_2_VERSION, ptr);
00696     ptr = common_write_16_bit(6 + key_id_len, ptr);
00697 
00698     /* Handshake message [msg_type][length] */
00699     *ptr++ = TLS_CLIENT_KEY_EXCHANGE;
00700     ptr = common_write_24_bit(2 + key_id_len, ptr);
00701 
00702     /* ClientKeyExchange [length][psk_id] */
00703     ptr = common_write_16_bit(key_id_len, ptr); //len
00704     if (tls_suite->psk_key_id > 0xFF) {
00705         *ptr++ = tls_suite->psk_key_id >> 8;
00706     }
00707     *ptr++ = tls_suite->psk_key_id;
00708 
00709     return ptr;
00710 }
00711 
00712 uint8_t *tls_build_change_chipher_suite_finnish_msg(uint8_t *ptr, tls_session_t *tls_session)
00713 {
00714     *ptr++ = TLS_CHANGE_CIPHER_SPEC;
00715     ptr = common_write_16_bit(TLS_1_2_VERSION, ptr);
00716     ptr = common_write_16_bit(1, ptr); //len
00717     *ptr++ = 1;
00718     *ptr++ = TLS_HANDSHAKE;
00719     ptr = common_write_16_bit(TLS_1_2_VERSION, ptr);
00720     ptr = common_write_16_bit(32, ptr); //len
00721     //Calculate Verify and MAC (MIC)
00722     //CCMNONCEEXPLICT //SHuold be 0x0000000000000000 at client side
00723 
00724     memcpy(ptr, tls_session->tls_nonce_explit, 8); // Dynamic
00725     ptr += 8;
00726     //MSG Finished
00727     memcpy(ptr, tls_session->tls_heap->hash_buf, 24);
00728     ptr += 24;
00729     return ptr;
00730 }
00731 
00732 void tls_build_client_change_chipher_suite_finnish(buffer_t *buf, sec_suite_t *tls_suite)
00733 {
00734     uint8_t *ptr;
00735     buffer_data_clear(buf);
00736     ptr = buffer_data_pointer(buf);
00737     if ((tls_suite->setups & TLS_SERVER_MODE) == 0) {
00738         ptr = tls_set_client_key_excange(ptr, tls_suite);
00739     }
00740     ptr = tls_build_change_chipher_suite_finnish_msg(ptr, tls_suite->tls_session);
00741     buffer_data_end_set(buf, ptr);
00742     tr_debug("TLS TX: KEY Exchange");
00743 }
00744 #ifdef ECC
00745 void tls_ecc_verfify_start(sec_suite_t *tls_suite)
00746 {
00747     if (ecc_state_idle_check() == ECC_STATUS_OK) {
00748         tls_heap_t *tls_heap = tls_suite->tls_session->tls_heap;
00749         uint8_t hash_response = ecc_verify_calculate_hash(tls_suite);
00750         if (hash_response) {
00751             tr_debug("HASH calc Fail: %02x", hash_response);
00752             sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
00753         } else {
00754             int8_t ecc_response = ecc_calculate_verify(tls_heap->ecc_heap->sgnt, &(tls_heap->ecc_heap->cert_pub_key), &ecc_operation_done_callback);
00755             if (ecc_response == ECC_STATUS_OK) {
00756                 tls_heap->ecc_heap->sgnt = 0;
00757                 sec_lib_state_machine_lock(tls_suite, TLS_ECC_MESSAGE_VERIFY);
00758                 sec_ecc_state_save(tls_suite);
00759             } else {
00760                 tr_debug("calcVerify Fail: %d", ecc_response);
00761             }
00762         }
00763     } else {
00764         sec_lib_state_machine_trig(tls_suite, TLS_ECC_MESSAGE_VERIFY_START2);
00765     }
00766 }
00767 
00768 
00769 void tls_certificate_signature_verify(sec_suite_t *tls_suite)
00770 {
00771     tls_heap_t *tls_heap = tls_suite->tls_session->tls_heap;
00772     if (ecc_state_idle_check() == ECC_STATUS_OK) {
00773         uint8_t alert_tx = 0;
00774         uint8_t chain_analyze_ok = 0;
00775         if (tls_heap->rx_ceri_chain.rd_ptr + 1 <= tls_heap->rx_ceri_chain.wr_ptr) {
00776             uint8_t certi_verify = 0;
00777             certificate_info_t *cer_info = &(tls_heap->rx_ceri_chain.certi_chain[tls_heap->rx_ceri_chain.rd_ptr]);
00778 
00779             //Verify Cur Certi Signature bye Next Certi PK
00780             tr_debug("index,Check Signature: rd: %02x, wr: %02x", tls_heap->rx_ceri_chain.rd_ptr, tls_heap->rx_ceri_chain.wr_ptr);
00781 
00782             if ((tls_heap->rx_ceri_chain.rd_ptr + 1) == tls_heap->rx_ceri_chain.wr_ptr) {
00783                 uint8_t self_signed = 0;
00784                 if (cer_info->issue_len == cer_info->subj_len) {
00785                     if (memcmp(cer_info->issue_ptr, cer_info->subj_ptr, cer_info->issue_len) == 0) {
00786                         self_signed =  1;
00787                     }
00788                 }
00789 
00790                 if (self_signed) {
00791                     certificate_chain_internal_t   *temp;
00792 
00793                     temp = sec_cetificate_chain_get(SEC_NWK_AUTHENTICATION_CERTI_CHAIN);
00794 
00795                     tr_debug("SELF Signed. Compare Root");
00796                     if (temp) {
00797                         uint16_t root_len = 0;
00798                         uint8_t root_type = 0;
00799                         const uint8_t *ptr = temp->certi_chain[0];
00800                         uint8_t *root_ptr = cer_info->certi_ptr;
00801                         root_ptr -= 4;
00802                         asn1_parse(root_ptr, &root_len, &root_type);
00803                         alert_tx = 1;
00804                         if ((root_len + 4) == temp->certi_len[0]) {
00805                             if (memcmp(root_ptr, ptr, temp->certi_len[0]) == 0) {
00806                                 tr_debug("Trusted Root certificate");
00807                                 alert_tx = 0;
00808                                 chain_analyze_ok = 1;
00809                             } else {
00810                                 tr_debug("Not Trusted Root");
00811                             }
00812                         } else {
00813                             alert_tx = 1;
00814                         }
00815                     } else {
00816                         alert_tx = 1;
00817                     }
00818                 } else {
00819 
00820                     //Compare all ways to Root Certi
00821                     certificate_chain_internal_t   *temp;
00822 
00823                     temp = sec_cetificate_chain_get(SEC_NWK_AUTHENTICATION_CERTI_CHAIN);
00824                     if (temp) {
00825                         if (temp->sub_len[0] == cer_info->issue_len) {
00826                             if (memcmp(cer_info->issue_ptr, temp->sub_chain[0], cer_info->issue_len) == 0) {
00827                                 tr_debug("Get Root PK");
00828                                 tls_heap->rx_ceri_chain.wr_ptr--;
00829                                 if (x509_v3_certi_pk_get((uint8_t *)temp->certi_chain[0], &(tls_heap->rx_ceri_chain)) == 0) {
00830                                     tr_debug("..fail");
00831                                 } else {
00832                                     tr_debug("..OK");
00833                                     tls_parse_subject_get_pub_key_from_chain(tls_heap, (tls_heap->rx_ceri_chain.rd_ptr));
00834                                     certi_verify = 2;
00835                                 }
00836                             }
00837                         }
00838                         if (certi_verify == 0) {
00839                             alert_tx = 1;
00840                             tr_debug("Unknow CA");
00841                         }
00842                     } else {
00843                         alert_tx = 1;
00844                     }
00845                 }
00846             } else {
00847 
00848                 certificate_info_t *cer_sig = &(tls_heap->rx_ceri_chain.certi_chain[tls_heap->rx_ceri_chain.rd_ptr + 1]);
00849 
00850                 if (cer_info->issue_len == cer_sig->subj_len) {
00851                     if (memcmp(cer_info->issue_ptr, cer_sig->subj_ptr, cer_info->issue_len) == 0) {
00852                         certi_verify =  1;
00853                     } else {
00854                         alert_tx = 1;
00855                         tr_debug("Not Valid chain signature subject entry");
00856                     }
00857                 } else {
00858                     alert_tx = 1;
00859                     tr_debug("Not Valid chain signature subject len");
00860                 }
00861 
00862 
00863             }
00864             if (certi_verify) {
00865                 //Calc HASH
00866                 memset((uint8_t *)tls_heap->ecc_heap->sgnt->m_m.data, 0, sizeof(MPint));
00867                 ns_sha256(cer_info->certi_ptr, cer_info->certi_len, tls_heap->ecc_heap->sgnt->m_m.data);
00868                 tls_ecc_reverse_hash((uint8_t *)tls_heap->ecc_heap->sgnt->m_m.data);
00869 
00870                 //GET Signature From Cur Certi
00871                 tls_read_certi_signature(tls_heap, 1);
00872 
00873                 //GET PUB KEY from next HOP
00874                 if (certi_verify == 1) {
00875                     tls_parse_subject_get_pub_key_from_chain(tls_heap, (tls_heap->rx_ceri_chain.rd_ptr + 1));
00876 
00877                 }
00878 
00879                 if (ecc_calculate_verify(tls_heap->ecc_heap->sgnt, &tls_heap->ecc_heap->cert_pub_key, &ecc_operation_done_callback) == ECC_STATUS_OK) {
00880                     tls_heap->ecc_heap->sgnt = 0;
00881                     sec_lib_state_machine_lock(tls_suite, TLS_ECC_CERTIFICATE_SIGNATURE_CHECK);
00882                     sec_ecc_state_save(tls_suite);
00883                     tls_heap->rx_ceri_chain.rd_ptr++;
00884                 } else {
00885                     if (tls_heap->ecc_heap->sgnt) {
00886                         ns_dyn_mem_free(tls_heap->ecc_heap->sgnt);
00887                         tls_heap->ecc_heap->sgnt = 0;
00888                     }
00889                     alert_tx = 1;
00890 
00891                 }
00892             }
00893         } else {
00894             chain_analyze_ok = 1;
00895         }
00896 
00897         if (chain_analyze_ok) {
00898             if (tls_heap->signature_temp_buf) {
00899                 //Take Signature
00900                 tls_read_certi_signature(tls_heap, 0);
00901                 if (tls_heap->signature_temp_buf == tls_heap->cert_temp_buf) {
00902                     tls_heap->signature_temp_buf = 0;
00903                 } else {
00904                     if (tls_heap->pointer_types & 2) {
00905                         buffer_free(tls_heap->signature_temp_buf);
00906                     } else {
00907                         ns_dyn_mem_free(tls_heap->signature_temp_buf);
00908                     }
00909                     tls_heap->signature_temp_buf = 0;
00910                 }
00911             }
00912 
00913             tr_debug("Certi signature check OK");
00914             tls_parse_subject_get_pub_key_from_chain(tls_heap, 0);
00915 
00916             if (tls_heap->rx_ceri_chain.wr_ptr == 0) {
00917                 certificate_info_t *c_ptr = &(tls_heap->rx_ceri_chain.certi_chain[0]);
00918                 if (x509_v3_certi_pk_get((uint8_t *)c_ptr->certi_ptr, &tls_heap->rx_ceri_chain) == 0) {
00919                     tr_debug("Fail read certificate PUB Key");
00920                 } else {
00921                     tls_parse_subject_get_pub_key_from_chain(tls_heap, (tls_heap->rx_ceri_chain.wr_ptr));
00922                 }
00923             }
00924 
00925             if (tls_heap->pointer_types & 1) {
00926                 buffer_free(tls_heap->cert_temp_buf);
00927             } else {
00928                 ns_dyn_mem_free(tls_heap->cert_temp_buf);
00929             }
00930             tls_heap->cert_temp_buf = 0;
00931 
00932             if (tls_suite->setups & TLS_SERVER_MODE) {
00933                 sec_lib_state_machine_trig(tls_suite, TLS_ECC_MESSAGE_SERVER_VERIFY_START);
00934             } else {
00935                 tr_debug("Start Do Certi Verify calc");
00936                 sec_lib_state_machine_trig(tls_suite, TLS_ECC_MESSAGE_VERIFY_START2);
00937             }
00938         }
00939         if (alert_tx) {
00940             tr_debug("PSK cal fail");
00941             sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
00942         }
00943     } else {
00944         tls_suite->timer = 1;
00945     }
00946 }
00947 
00948 void tls_server_finnish_handle_start(sec_suite_t *tls_suite)
00949 {
00950     if (ecc_state_idle_check() == ECC_STATUS_OK) {
00951         tls_heap_t *tls_heap = tls_suite->tls_session->tls_heap;
00952         uint8_t alert_tx = 0;
00953         if (tls_suite->setups & TLS_ECC_CERTIFICATE_VERIFY) {
00954             memcpy(tls_heap->ecc_heap->sgnt->m_m.data, tls_heap->hash_buf, 32);
00955             // initialize the rest of MPint as the hash is covering only part of it
00956             memset(((uint8_t*)tls_heap->ecc_heap->sgnt->m_m.data) + 32, 0, sizeof(MPint) - 32);
00957             tls_ecc_reverse_hash((uint8_t *)tls_heap->ecc_heap->sgnt->m_m.data);
00958 
00959             if (ecc_calculate_verify(tls_heap->ecc_heap->sgnt, &tls_heap->ecc_heap->cert_pub_key, &ecc_operation_done_callback) == ECC_STATUS_OK) {
00960                 tls_heap->ecc_heap->sgnt = 0;
00961                 sec_lib_state_machine_lock(tls_suite, TLS_ECC_MESSAGE_VERIFY);
00962                 sec_ecc_state_save(tls_suite);
00963             } else {
00964                 if (tls_heap->ecc_heap->sgnt) {
00965                     ecc_library_free_pointer(tls_heap->ecc_heap->sgnt);
00966                     //ns_dyn_mem_free(tls_heap->ecc_heap->sgnt);
00967                     tls_heap->ecc_heap->sgnt = 0;
00968                 }
00969                 alert_tx = 1;
00970 
00971             }
00972         } else {
00973             if (tls_ecc_start_premaster_secret(0, tls_suite) == 0) {
00974                 alert_tx = 1;
00975             }
00976         }
00977         if (alert_tx) {
00978             tr_debug("PSK cal fail");
00979             sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
00980         }
00981     } else {
00982         sec_lib_state_machine_trig(tls_suite, TLS_ECC_MESSAGE_SERVER_VERIFY_START);
00983     }
00984 }
00985 #endif
00986 
00987 #ifdef PANA_SERVER_API
00988 static buffer_t * tls_verify_handler(uint8_t certi_rx, tls_header_t *tls_header_ptr, buffer_t *buf, sec_suite_t *tls_suite)
00989 {
00990     tls_heap_t *tls_heap = tls_suite->tls_session->tls_heap;
00991     tls_heap->client_verify_buf_len = tls_header_ptr->length;
00992     if (tls_heap->client_verify_buf) {
00993         tr_debug("Free Server Client 1r");
00994         ns_dyn_mem_free(tls_heap->client_verify_buf);
00995         tls_heap->client_verify_buf = 0;
00996     }
00997     tls_heap->client_verify_buf = ns_dyn_mem_alloc(tls_header_ptr->length);
00998     if (tls_heap->client_verify_buf) {
00999         memcpy(tls_heap->client_verify_buf, tls_header_ptr->ptr, tls_header_ptr->length);
01000 
01001 #ifdef ECC
01002         if (tls_heap->tls_chipher_mode == CHIPHER_ECC) {
01003 
01004             if (certi_rx) {
01005                 sec_lib_state_machine_trig(tls_suite, TLS_ECC_MESSAGE_SERVER_VERIFY_START);
01006                 tr_debug("Certi RX > 0");
01007                 if (tls_suite->tls_session->tls_heap) {
01008                     uint8_t free_buf = 1;
01009                     if (certi_rx & 1) {
01010                         if (tls_heap->cert_temp_buf == 0) {
01011                             tls_heap->pointer_types |= 1;
01012                             tls_heap->cert_temp_buf = buf;
01013                             free_buf = 0;
01014                         }
01015                     }
01016                     if (certi_rx & 2) {
01017                         if (tls_heap->signature_temp_buf == 0) {
01018                             tls_heap->pointer_types |= 2;
01019                             tls_heap->signature_temp_buf = buf;
01020                             free_buf = 0;
01021                         }
01022                     }
01023 
01024                     if (free_buf == 0) {
01025                         buf = NULL;
01026                     }
01027                 }
01028 
01029             }
01030             if (buf) {
01031                 buf = buffer_free(buf);
01032             }
01033 
01034         } else
01035 #endif
01036         {
01037             tr_debug("Start Server PRF_CALC state");
01038             sec_prf_state_set(tls_suite);
01039         }
01040     } else {
01041         tr_debug("Heap_error: client verify chiphersuite!!");
01042     }
01043 
01044     return (buf);
01045 }
01046 #endif
01047 
01048 static int8_t tls_client_verify_handler(tls_header_t *tls_header_ptr, sec_suite_t *tls_suite)
01049 {
01050     if (tls_ccm_data_decrypt(tls_header_ptr->ptr, tls_header_ptr->length, tls_suite->tls_session->key_expansion, TLS_HANDSHAKE, true) == 0) {
01051         uint8_t *ptr = tls_header_ptr->ptr + 8;
01052         tls_heap_t *tls_heap = tls_suite->tls_session->tls_heap;
01053 
01054         if (ptr[0] == TLS_FINISHED && common_read_24_bit(ptr + 1) == 12) {
01055             ptr += 4;
01056             //
01057             tr_debug("Finish RX: %s", tr_array(ptr, 12));
01058 
01059             memcpy(tls_heap->verify, ptr, 12);
01060             return 0;
01061         } else {
01062             //RETURN error
01063             tr_debug("No Chiphertext");
01064             return -1;
01065         }
01066     }
01067 
01068     tr_debug("Encode error");
01069     return -2;
01070 
01071 }
01072 
01073 static buffer_t * tls_certificate_buffer_store(buffer_t *buf, uint8_t certi_rx, sec_suite_t *tls_suite)
01074 {
01075     if (certi_rx) {
01076         if (tls_suite->tls_session->tls_heap) {
01077             uint8_t free_buf = 1;
01078             tls_heap_t *theap = tls_suite->tls_session->tls_heap;
01079             if (certi_rx & 1) {
01080                 if (theap->cert_temp_buf == 0) {
01081                     theap->pointer_types |= 1;
01082                     theap->cert_temp_buf = buf;
01083                     free_buf = 0;
01084 
01085                 }
01086             }
01087             if (certi_rx & 2) {
01088                 if (theap->signature_temp_buf == 0) {
01089                     theap->pointer_types |= 2;
01090                     theap->signature_temp_buf = buf;
01091                     free_buf = 0;
01092                 }
01093             }
01094             if (free_buf == 0) {
01095                 buf = NULL;
01096             }
01097         }
01098     }
01099 
01100     if (buf) {
01101         buf = buffer_free(buf);
01102     }
01103 
01104     return buf;
01105 }
01106 
01107 buffer_t *tls_client_up(buffer_t *buf, sec_suite_t *tls_suite)
01108 {
01109     uint8_t i = 0, certi_rx = 0;
01110     uint16_t data_len = 0;
01111     uint8_t alert_case = 0;
01112     uint8_t *ptr;
01113     tls_header_t *tls_header_ptr;
01114     tls_heap_t *tls_heap = tls_suite->tls_session->tls_heap;
01115     uint8_t algo_ok = 0, j = 0;
01116     ptr = buffer_data_pointer(buf);
01117 
01118     i = 1;
01119     while (buf->buf_ptr  < buf->buf_end ) {
01120         data_len = buffer_data_length(buf);
01121         ptr = buffer_data_pointer(buf);
01122         tls_header_ptr = NULL;
01123 
01124         if (data_len >= 5) {
01125             uint16_t tls_version;
01126             tls_header.type = *ptr++;
01127             tls_version = common_read_16_bit(ptr);
01128             ptr += 2;
01129             if (tls_version != TLS_1_2_VERSION) {
01130                 tr_debug("Len: %04x", data_len);
01131                 tr_debug("%s", tr_array(ptr, 4));
01132                 alert_case = 4;
01133             } else {
01134                 tls_header.length = common_read_16_bit(ptr);
01135                 ptr += 2;
01136                 data_len -= 5;
01137                 if (tls_header.length > data_len) {
01138                     alert_case = 5;
01139                 } else {
01140                     tr_debug("Full TLS Record");
01141                     tls_header.ptr = ptr;
01142                     buf->buf_ptr  += tls_header.length;
01143                     buf->buf_ptr  += 5;
01144                     tls_header_ptr = &tls_header;
01145                 }
01146             }
01147         } else {
01148             alert_case = 5;
01149         }
01150 
01151         if (alert_case) {
01152             tr_debug("TLS Segmentation or datagram error: %02x", alert_case);
01153             buf->buf_ptr  = buf->buf_end ;
01154             //SET Alert Case
01155             sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
01156             return buffer_free(buf);
01157         }
01158 
01159         if (tls_header_ptr) {
01160             if (tls_header_ptr->type == TLS_HANDSHAKE && (tls_heap != 0)) {
01161                 tr_debug("Type:Handshake");
01162                 if ((tls_suite->state == TLS_CHANGE_CHIPHER)) {
01163                     if (tls_header_ptr->length < 32) {
01164                         tr_debug("Too short Chiher Text");
01165                     } else if ((algo_ok & 0x20) && (tls_suite->state == PRF_CALC)) {
01166                         tr_debug("Drop Client RE TX");
01167                         /*tls_text_cnt = */j = 1;
01168                     } else {
01169                         int8_t returnCode = tls_client_verify_handler(tls_header_ptr, tls_suite);
01170                         if (returnCode == 0)
01171                         {
01172                             algo_ok |= 0x10;
01173                             sec_lib_state_machine_trig(tls_suite, TLS_FINISH);
01174                             j = 1;
01175                         } else if (returnCode == -1) {
01176                             sec_lib_state_machine_trig(tls_suite, PANA_ERROR);
01177                             return buffer_free(buf);
01178                         } else {
01179                             i = 5;
01180                             sec_lib_state_machine_trig(tls_suite, PANA_ERROR);
01181                             break;
01182                         }
01183 
01184                         //Set Back Originals
01185                         tls_header_ptr->ptr -= 8;
01186                         tls_header_ptr->length += 16;
01187                     }
01188                 } else {
01189                     uint8_t tls_msg_cnt = 0;
01190                     tls_msg_t *tls_msg_ptr = 0;
01191                     if (algo_ok != 0x18) {
01192                         tls_msg_cnt = tls_msg_analyzy(tls_header_ptr->ptr, tls_header_ptr->length);
01193                         j = 0;
01194                     }
01195                     ptr = tls_header_ptr->ptr;
01196                     while (j < tls_msg_cnt) {
01197                         tls_msg_ptr = tls_msg_get(ptr);
01198 
01199                         switch (tls_msg_ptr->type) {
01200                             case TLS_SERVER_HELLO:
01201                                 if (tls_msg_ptr->len >= 38) {
01202                                     if (tls_suite->state == TLS_INIT) {
01203                                         if (tls_parse_server_hello(tls_msg_ptr->msg_ptr, tls_suite)) {
01204 
01205                                             uint8_t switch_state = 0;
01206                                             tr_debug("TLS:S_Hello OK");
01207                                             if (tls_heap->tls_chipher_mode != CHIPHER_ECC) {
01208                                                 algo_ok |= 1;
01209                                                 switch_state = 1;
01210                                                 tls_ecc_heap_free(tls_heap);
01211                                             } else {
01212 
01213 #ifdef ECC
01214                                                 tls_suite->retry_counter = 0;
01215                                                 switch_state = 1;
01216 #else
01217                                                 tr_debug("NO ECC Sup");
01218                                                 sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
01219                                                 return buffer_free(buf);
01220 #endif
01221 
01222                                             }
01223                                             if (switch_state) {
01224                                                 tls_suite->setups &= ~TLS_HANSHAKE_HASH;
01225                                                 sec_set_auth_timeout(tls_suite, TLS_HELLO_RX);
01226                                                 tls_suite->retry_counter = 0;
01227                                                 tls_handshake_copy(tls_msg_ptr, tls_heap);
01228                                             }
01229                                         } else {
01230                                             tr_debug("TLS:S_Hello Fail: %02x", tls_suite->state);
01231                                         }
01232                                     } else {
01233                                         tr_debug("SERver RE Tx  drop Ser Hello");
01234                                     }
01235                                 }
01236                                 break;
01237 
01238                             case TLS_SERVER_HELLO_DONE:
01239                                 if (tls_msg_ptr->len == 0) {
01240                                     if (tls_suite->state >= TLS_CERTIFICATE_RX && tls_suite->state < PANA_ERROR) {
01241                                         tr_debug("TLS:S_Hello DONE");
01242                                         algo_ok |= 2;
01243                                         tls_handshake_copy(tls_msg_ptr, tls_heap);
01244                                         sec_lib_state_machine_trig(tls_suite, TLS_HELLO_DONE);
01245                                     } else {
01246                                         tr_debug("Drop Ser Hello Done: %02x", tls_suite->state);
01247                                     }
01248                                 }
01249                                 break;
01250 
01251                             case TLS_CERTIFICATE:
01252 #ifdef ECC
01253                                 if (tls_suite->state == TLS_HELLO_RX) {
01254                                     tr_debug("TLS Certi RX");
01255 
01256                                     if (tls_parse_certificate(tls_msg_ptr->msg_ptr, tls_msg_ptr->len, tls_suite)) {
01257 
01258                                         tls_suite->setups |= TLS_ECC_CERTIFICATE_RECEIVED;
01259                                         algo_ok = 0;
01260                                         tls_handshake_copy(tls_msg_ptr, tls_heap);
01261                                         tls_suite->timer = pana_retry_req_max_get();
01262 
01263                                         tls_suite->state = TLS_CERTIFICATE_RX;
01264                                         certi_rx |= 1;
01265 
01266                                     } else {
01267                                         tr_debug("TLS Malformed Certificate");
01268 
01269                                         sec_lib_state_machine_trig(tls_suite, TLS_ALERT_BAD_CERTIFICATE);
01270                                         return buffer_free(buf);
01271                                     }
01272 
01273                                 } else {
01274                                     tr_debug("Drop Cert: %02x", tls_suite->state);
01275                                     return buffer_free(buf);
01276                                 }
01277 #else
01278                                 sec_lib_state_machine_trig(tls_suite, PANA_ERROR);
01279 #endif
01280                             break;
01281 
01282                             case TLS_SERVER_KEY_EXCHANGE:
01283                                 tr_debug(" TLS Serv KEY Exchange RX");
01284 #ifdef ECC
01285                                 if (!tls_parse_server_key_exchange(tls_msg_ptr->msg_ptr, tls_msg_ptr->len, tls_suite)) {
01286                                     tr_debug("Drop Key");
01287                                     sec_lib_state_machine_trig(tls_suite, PANA_ERROR);
01288                                 } else {
01289                                     algo_ok = 0;
01290                                     tls_handshake_copy(tls_msg_ptr, tls_heap);
01291                                     certi_rx |= 2;
01292                                     sec_set_auth_timeout(tls_suite, TLS_SERVER_KEY_EXCHANGE_RX);
01293                                 }
01294 #else
01295                                 tr_debug("Drop Key");
01296                                 sec_lib_state_machine_trig(tls_suite, PANA_ERROR);
01297 #endif
01298 
01299                                 break;
01300 
01301                             case TLS_CERTIFICATE_REQUEST:
01302 #ifdef ECC
01303                                 if (tls_parse_certificate_request(tls_msg_ptr->msg_ptr, tls_msg_ptr->len)) {
01304                                     tr_debug(" TLS Cert request- ");
01305                                     if ((tls_suite->setups & TLS_ECC_CERTIFICATE_REQUESTED) == 0) {
01306                                         tls_suite->setups |= TLS_ECC_CERTIFICATE_REQUESTED;
01307                                         tls_handshake_copy(tls_msg_ptr, tls_heap);
01308 
01309                                     }
01310                                 } else
01311 #endif
01312                                 {
01313                                     tr_debug("Client drop Cert Request");
01314                                     sec_lib_state_machine_trig(tls_suite, TLS_ALERT_DECRYPT);
01315                                     return buffer_free(buf);
01316                                 }
01317                                 break;
01318 
01319                             default:
01320                                 break;
01321                         }
01322                         ptr = tls_msg_ptr->msg_ptr;
01323                         ptr += tls_msg_ptr->len;
01324                         j++;
01325                     }
01326                 }
01327 
01328             } else if (tls_header_ptr->type == TLS_CHANGE_CIPHER_SPEC && (tls_heap != 0) /*&& (tls_header[i].length == 1)*/) {
01329                 uint8_t tx_alert = 1;
01330                 tr_debug("TLS:Change ChipherS");
01331                 tr_debug("%02x", tls_suite->state);
01332                 if (tls_suite->state == TLS_KEY_CHANGE) {
01333                     tx_alert = 0;
01334                 }
01335 
01336                 if (tx_alert) {
01337                     tr_debug("Wrong state TX Alert");
01338                     sec_lib_state_machine_trig(tls_suite, TLS_ALERT_CLOSE_FATAL);
01339                     return buffer_free(buf);
01340                 } else {
01341                     sec_set_auth_timeout(tls_suite, TLS_CHANGE_CHIPHER);
01342                     algo_ok |= 8;
01343                 }
01344 
01345             } else if (tls_header_ptr->type == TLS_ALERT_TYPE) {
01346                 tr_debug("Alert!!");
01347                 if (tls_header_ptr->length == 2) {
01348                     uint8_t *dptr = tls_header_ptr->ptr;
01349                     tr_debug("%s", tr_array(tls_header_ptr->ptr, 2));
01350                     //Skip Alert Type and descriptions
01351                     dptr += 2;
01352                     algo_ok = 0xf0;
01353                     sec_lib_state_machine_trig(tls_suite, PANA_FAILURE);
01354                 }
01355                 i = 0xfe;
01356             }
01357         }
01358     }
01359 
01360     if (algo_ok) {
01361         if (tls_suite->state == TLS_HELLO_RX) {
01362             sec_set_auth_timeout(tls_suite, TLS_HELLO_RX);
01363             tls_suite->retry_counter = 0;
01364             i = 0xff;
01365         } else if (tls_suite->state == TLS_HELLO_DONE) {
01366             //ADD already Now Client key change handshake part
01367 #ifdef ECC
01368             if (tls_heap->tls_chipher_mode == CHIPHER_ECC) {
01369                 if (tls_suite->setups & TLS_ECC_CERTIFICATE_REQUESTED) {
01370                     tr_debug("Message verify start");
01371                     sec_lib_state_machine_trig(tls_suite, TLS_ECC_MESSAGE_VERIFY_START);
01372                 } else {
01373                     tr_debug("Cert REQ diabled");
01374                 }
01375             } else
01376 #endif
01377             {
01378 
01379                 tls_msg_t *tmp_msg = tls_msg_ptr_get();
01380 
01381                 tmp_msg->len = 3;
01382                 if (tls_suite->psk_key_id > 0xff) {
01383                     tmp_msg->len++;
01384                 }
01385                 ptr = buf->buf ;
01386                 *ptr++ = TLS_CLIENT_KEY_EXCHANGE;
01387                 ptr = common_write_24_bit(tmp_msg->len, ptr);
01388                 tmp_msg->msg_ptr = ptr;
01389                 ptr = common_write_16_bit(tmp_msg->len - 2, ptr);
01390                 if (tls_suite->psk_key_id > 0xff) {
01391                     *ptr++ = tls_suite->psk_key_id >> 8;
01392                 }
01393 
01394                 *ptr = tls_suite->psk_key_id;;
01395                 tls_handshake_copy(tmp_msg, tls_heap);
01396                 sec_prf_state_set(tls_suite);
01397             }
01398             i = 0xff;
01399         } else if (tls_suite->state == TLS_CHANGE_CHIPHER) {
01400             tr_debug("No Chipher Set Close state");
01401             sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
01402             i = 0xff;
01403         } else if (tls_suite->state == TLS_FINISH) {
01404             tr_debug("Finish RX");
01405             sec_lib_state_machine_trig(tls_suite, TLS_FINISH);
01406             i = 0xff;
01407         }else if (algo_ok == 0x18) {
01408 
01409         }
01410         else {
01411             i = 11;
01412             tr_debug("%02x", algo_ok);
01413         }
01414     }
01415 
01416     if (i) {
01417         buf = tls_certificate_buffer_store(buf, certi_rx, tls_suite);
01418     }
01419     return (buf);
01420 }
01421 
01422 
01423 
01424 buffer_t *tls_server_up(buffer_t *buf, sec_suite_t *tls_suite)
01425 {
01426 #ifdef PANA_SERVER_API
01427     uint8_t i = 0, certi_rx = 0;
01428     uint16_t data_len = 0;
01429     uint8_t alert_case = 0;
01430     uint8_t *ptr;
01431     tls_header_t *tls_header_ptr;
01432     tls_heap_t *tls_heap = tls_suite->tls_session->tls_heap;
01433     uint8_t algo_ok = 0, j = 0;
01434     ptr = buffer_data_pointer(buf);
01435 
01436     i = 1;
01437     while (buf->buf_ptr  < buf->buf_end ) {
01438         data_len = buffer_data_length(buf);
01439         ptr = buffer_data_pointer(buf);
01440         tls_header_ptr = NULL;
01441 
01442         if (data_len >= 5) {
01443             uint16_t tls_version;
01444             tls_header.type = *ptr++;
01445             tls_version = common_read_16_bit(ptr);
01446             ptr += 2;
01447             if (tls_version != TLS_1_2_VERSION) {
01448                 tr_debug("Len: %04x", data_len);
01449                 tr_debug("%s", tr_array(ptr, 4));
01450                 alert_case = 4;
01451             } else {
01452                 tls_header.length = common_read_16_bit(ptr);
01453                 ptr += 2;
01454                 data_len -= 5;
01455                 if (tls_header.length > data_len) {
01456                     alert_case = 5;
01457                 } else {
01458                     tr_debug("Full TLS Record");
01459                     tls_header.ptr = ptr;
01460                     buf->buf_ptr  += tls_header.length;
01461                     buf->buf_ptr  += 5;
01462                     tls_header_ptr = &tls_header;
01463                 }
01464             }
01465         } else {
01466             alert_case = 5;
01467         }
01468 
01469         if (alert_case) {
01470             tr_debug("TLS Segmentation or datagram error: %02x", alert_case);
01471             buf->buf_ptr  = buf->buf_end ;
01472             //SET Alert Case
01473             sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
01474             return buffer_free(buf);
01475         }
01476 
01477         if (tls_header_ptr) {
01478             if (tls_header_ptr->type == TLS_HANDSHAKE && (tls_heap != 0)) {
01479                 tr_debug("Type:Handshake");
01480                 if ((tls_suite->state == TLS_CHANGE_CHIPHER)) {
01481                     if (tls_header_ptr->length < 32) {
01482                         tr_debug("Too short Chiher Text");
01483                     } else if ((algo_ok & 0x20) && (tls_suite->state == PRF_CALC)) {
01484                         tr_debug("Drop Client RE TX");
01485                         /*tls_text_cnt = */j = 1;
01486                     } else {
01487                         buf = tls_verify_handler(certi_rx,tls_header_ptr, buf, tls_suite);
01488                         if (!buf) {
01489                             return buf;
01490                         }
01491                     }
01492                 } else {
01493                     uint8_t tls_msg_cnt = 0;
01494                     tls_msg_t *tls_msg_ptr = 0;
01495                     if (algo_ok != 0x18) {
01496                         tls_msg_cnt = tls_msg_analyzy(tls_header_ptr->ptr, tls_header_ptr->length);
01497                         j = 0;
01498                     }
01499                     ptr = tls_header_ptr->ptr;
01500                     while (j < tls_msg_cnt) {
01501                         tls_msg_ptr = tls_msg_get(ptr);
01502 
01503                         switch (tls_msg_ptr->type) {
01504                             case TLS_CLIENT_HELLO:
01505                                 //Parse
01506                                 if (tls_parse_client_hello(tls_msg_ptr->msg_ptr, tls_msg_ptr->len, tls_suite)) {
01507                                     tr_debug("TLS:C_Hello OK");
01508                                     algo_ok = 0;
01509                                     tls_heap->tls_handshake_h_len = 0;
01510                                     tls_handshake_copy(tls_msg_ptr, tls_heap);
01511                                     if (tls_heap->tls_chipher_mode == CHIPHER_ECC) {
01512 #ifdef ECC
01513                                         sec_lib_state_machine_trig(tls_suite, TLS_SERVER_ECC_PUB_KEY_GEN);
01514 #else
01515                                         tr_debug("NO ECC Sup");
01516                                         sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
01517                                         return buffer_free(buf);
01518 #endif
01519 
01520                                     } else {
01521                                         tls_ecc_heap_free(tls_heap);
01522                                         sec_lib_state_machine_trig(tls_suite, TLS_SERVER_TX_SERVER_HELLO);
01523                                     }
01524 
01525                                 } else {
01526                                     tr_debug("TLS:C_Hello Fail");
01527                                     sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
01528                                     return buffer_free(buf);
01529                                 }
01530                                 break;
01531 
01532                             case TLS_CERTIFICATE:
01533 #ifdef ECC
01534 
01535                                 if (tls_suite->state == TLS_TX_SERVER_KEY_EXCHANGE || tls_suite->state == TLS_SERVER_WAIT_CHANGE_CHIPHERSUITE) {
01536                                     tr_debug("TLS Certi RX");
01537 
01538                                     if (tls_parse_certificate(tls_msg_ptr->msg_ptr, tls_msg_ptr->len, tls_suite)) {
01539 
01540                                         tls_suite->setups |= TLS_ECC_CERTIFICATE_RECEIVED;
01541                                         algo_ok = 0;
01542                                         tls_handshake_copy(tls_msg_ptr, tls_heap);
01543                                         tls_suite->timer = pana_retry_req_max_get();
01544                                         tls_suite->state = TLS_CERTIFICATE_RX;
01545                                         certi_rx |= 1;
01546 
01547                                     } else {
01548                                         tr_debug("TLS Malformed Certificate");
01549 
01550                                         sec_lib_state_machine_trig(tls_suite, TLS_ALERT_BAD_CERTIFICATE);
01551                                         return buffer_free(buf);
01552                                     }
01553 
01554                                 } else {
01555                                     tr_debug("Drop Cert: %02x", tls_suite->state);
01556                                     return buffer_free(buf);
01557                                 }
01558 #else
01559                                 sec_lib_state_machine_trig(tls_suite, PANA_ERROR);
01560 #endif
01561                             break;
01562 
01563                             case TLS_CLIENT_KEY_EXCHANGE:
01564                                 tr_debug(" TLS Client KEY Exchange RX");
01565                                 if (tls_heap->tls_chipher_mode == CHIPHER_PSK) {
01566                                     uint8_t *d_ptr = tls_msg_ptr->msg_ptr;
01567                                     uint16_t key_len;
01568                                     uint16_t received_key_id = 0;
01569 
01570                                     key_len = common_read_16_bit(d_ptr);
01571                                     d_ptr += 2;
01572 
01573                                     if (key_len) {
01574                                         if (key_len == 2) {
01575                                             received_key_id = (*d_ptr++) << 8;
01576                                         }
01577                                         received_key_id += *d_ptr;
01578                                     }
01579 
01580                                     if (tls_get_key(received_key_id) != NULL) {
01581                                         tls_suite->psk_key_id = received_key_id;
01582 
01583                                         if (tls_suite->state != PRF_CALC) {
01584                                             tr_debug("Client KEY Exchange ");
01585                                             tls_handshake_copy(tls_msg_ptr, tls_heap);
01586                                             algo_ok = 0;
01587                                         } else {
01588                                             tr_debug("Client Re TX");
01589                                         }
01590                                     }
01591 
01592                                     /* Not valid Key ID */
01593                                     else {
01594                                         tr_debug("Server drop Client Key Exchange");
01595                                         sec_lib_state_machine_trig(tls_suite, TLS_ALERT_DECRYPT);
01596                                         return buffer_free(buf);
01597                                     }
01598                                 } else {
01599 #ifdef ECC
01600                                     if (tls_parse_client_key_exchange(tls_msg_ptr->msg_ptr, tls_msg_ptr->len, tls_suite)) {
01601                                         algo_ok = 0;
01602                                         tls_handshake_copy(tls_msg_ptr, tls_heap);
01603                                         sec_set_auth_timeout(tls_suite, TLS_CLIENT_KEY_EXCHANGE_RX);
01604                                     } else
01605 #endif
01606                                     {
01607                                         tr_debug("Server drop Client Key EX");
01608                                         sec_lib_state_machine_trig(tls_suite, TLS_ALERT_DECRYPT);
01609                                         return buffer_free(buf);
01610                                     }
01611                                 }
01612                                 break;
01613 
01614                             case TLS_CERTIFICATE_VERIFY:
01615                                 tr_debug(" TLS Cert Verify ");
01616 #ifdef ECC
01617                                 if (tls_parse_certificate_verify(tls_msg_ptr->msg_ptr, tls_msg_ptr->len, tls_suite)) {
01618 
01619                                     if ((tls_suite->setups & TLS_ECC_CERTIFICATE_VERIFY) == 0) {
01620                                         tr_debug("Calc Hash for Cert Verify");
01621                                         tls_suite->setups |= TLS_ECC_CERTIFICATE_VERIFY;
01622                                         tls_hanshake_hash_cal(tls_heap);
01623                                         tls_handshake_copy(tls_msg_ptr, tls_heap);
01624                                         certi_rx |= 2;
01625                                     }
01626                                 } else
01627 #endif
01628                                 {
01629                                     tr_debug("Client Cert Verfify fail");
01630                                     sec_lib_state_machine_trig(tls_suite, TLS_ALERT_DECRYPT);
01631                                     return buffer_free(buf);
01632                                 }
01633                                 break;
01634                             default:
01635                                 break;
01636                         }
01637                         ptr = tls_msg_ptr->msg_ptr;
01638                         ptr += tls_msg_ptr->len;
01639                         j++;
01640                     }
01641                 }
01642 
01643             } else if (tls_header_ptr->type == TLS_CHANGE_CIPHER_SPEC && (tls_heap != 0) /*&& (tls_header[i].length == 1)*/) {
01644                 uint8_t tx_alert = 1;
01645                 tr_debug("TLS:Change ChipherS");
01646                 tr_debug("%02x", tls_suite->state);
01647                 if (tls_suite->state == TLS_CLIENT_KEY_EXCHANGE_RX || tls_suite->state == TLS_SERVER_WAIT_CHANGE_CHIPHERSUITE) {
01648                     tx_alert = 0;
01649                 }
01650 
01651                 if (tx_alert) {
01652                     tr_debug("Wrong state TX Alert");
01653                     sec_lib_state_machine_trig(tls_suite, TLS_ALERT_CLOSE_FATAL);
01654                     return buffer_free(buf);
01655                 } else {
01656                     sec_set_auth_timeout(tls_suite, TLS_CHANGE_CHIPHER);
01657                     algo_ok |= 8;
01658                 }
01659 
01660             } else if (tls_header_ptr->type == TLS_ALERT_TYPE) {
01661                 tr_debug("Alert!!");
01662                 if (tls_header_ptr->length == 2) {
01663                     uint8_t *dptr = tls_header_ptr->ptr;
01664                     tr_debug("%s", tr_array(tls_header_ptr->ptr, 2));
01665                     //Skip Alert Type and descriptions
01666                     dptr += 2;
01667                     algo_ok = 0xf0;
01668                     sec_lib_state_machine_trig(tls_suite, PANA_FAILURE);
01669                 }
01670                 i = 0xfe;
01671             }
01672         }
01673     }
01674     if (algo_ok) {
01675         if (tls_suite->state == TLS_CHANGE_CHIPHER) {
01676             tr_debug("No Chipher RX--TXalert");
01677             sec_lib_state_machine_trig(tls_suite, TLS_ALERT_INTERNAL);
01678             i = 0xff;
01679         } else if (algo_ok == 0x18) {
01680 
01681         }
01682         else {
01683             i = 11;
01684             tr_debug("%02x", algo_ok);
01685         }
01686     }
01687 
01688     if (i) {
01689         buf = tls_certificate_buffer_store(buf, certi_rx, tls_suite);
01690     }
01691 #else
01692     if (buf) {
01693         buf = buffer_free(buf);
01694     }
01695 #endif
01696     return (buf);
01697 }
01698 
01699 
01700 buffer_t * tls_client_hello_build(buffer_t *buf, sec_suite_t *tls_suite)
01701 {
01702     uint8_t *ptr;
01703     tls_msg_t *tmp_msg = tls_msg_ptr_get();
01704     tls_heap_t *tls_heap = tls_suite->tls_session->tls_heap;
01705     tr_debug("TTLs TX:Client Hello");
01706     //Build Client Hello
01707     ptr = buffer_data_clear(buf);
01708     //for Hash calculation
01709 
01710     *ptr++ = TLS_CLIENT_HELLO;
01711     ptr += 3; // skip length, fill in at the end
01712     tmp_msg->msg_ptr = ptr;
01713     ptr = common_write_16_bit(TLS_1_2_VERSION, ptr);
01714     memcpy(ptr, (tls_heap->tls_hello_random + CLIENT_HELLO_PTR), 32);
01715     ptr += 32;
01716     *ptr++ = 0; //Session ID length
01717     ptr += 2; // Skip cipher suite length; fill in a moment
01718     uint8_t *ciphers_ptr = ptr;
01719 #ifdef ECC
01720     if (tls_suite->supported_chipher_suites & SEC_CIPHERSUITE_ECC) {
01721         ptr = common_write_16_bit(TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, ptr);
01722         ptr = common_write_16_bit(TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_COMPAT, ptr);
01723     }
01724 #endif
01725     if (tls_suite->supported_chipher_suites & SEC_CIPHERSUITE_PSK) {
01726         ptr = common_write_16_bit(TLS_PSK_WITH_AES_128_CCM_8, ptr);
01727     }
01728     // Go back and fill in length
01729     common_write_16_bit(ptr - ciphers_ptr, ciphers_ptr - 2);
01730 
01731     *ptr++ = 1; // 1 Compression method
01732     *ptr++ = TLS_COMPRESSION_METHOD_NULL;
01733 #ifdef ECC
01734 
01735     //Set ECC Extensions
01736     if (tls_suite->supported_chipher_suites & SEC_CIPHERSUITE_ECC) {
01737         ptr = common_write_16_bit(8 + 8 + 6, ptr); //Extensions length
01738 
01739         ptr = common_write_16_bit(TLS_EXTENSION_SIGNATURE_ALGORITHMS, ptr);
01740         ptr = common_write_16_bit(4, ptr); //extension data len
01741         ptr = common_write_16_bit(2, ptr); //algorithm list len
01742         ptr = common_write_16_bit(TLS_SIG_HASH_ALG_SHA256_ECDSA, ptr);
01743 
01744         ptr = common_write_16_bit(TLS_EXTENSION_ELLIPTIC_CURVES, ptr);
01745         ptr = common_write_16_bit(4, ptr); //extension data len
01746         ptr = common_write_16_bit(2, ptr); //curve list len
01747         ptr = common_write_16_bit(TLS_NAMED_CURVE_SECP256R1, ptr);
01748 
01749         ptr = common_write_16_bit(TLS_EXTENSION_EC_POINT_FORMATS, ptr);
01750         ptr = common_write_16_bit(2, ptr); //extension data len
01751         *ptr++ = 1; // format list len
01752         *ptr++ = 0; //Uncompressed mode
01753     }
01754 #endif
01755     buffer_data_end_set(buf, ptr);
01756 
01757     // Go back and write the length
01758     uint16_t tls_len = buffer_data_length(buf) - 4;
01759     tmp_msg->len = tls_len;
01760     common_write_24_bit(tls_len, buffer_data_pointer(buf) + 1);
01761 
01762     tls_heap->tls_handshake_h_len = 0;
01763     tls_handshake_copy(tmp_msg, tls_heap);
01764     tls_suite->setups |= TLS_HANSHAKE_HASH;
01765     return tls_down(buf);
01766 
01767 }
01768 
01769 #ifdef ECC
01770 uint8_t *tls_server_key_excahnge_msg_build(uint8_t *ptr , tls_heap_t *heap_ptr)
01771 {
01772     uint8_t r_zeros;
01773     uint8_t s_zeros;
01774 
01775     r_zeros = tls_get_leading_zeros(heap_ptr->ecc_heap->sgnt->m_R.data);
01776     s_zeros = tls_get_leading_zeros(heap_ptr->ecc_heap->sgnt->m_s.data);
01777     *ptr++ = TLS_SERVER_KEY_EXCHANGE;
01778     ptr = common_write_24_bit((145 - r_zeros - s_zeros), ptr);
01779     *ptr++ = 3; //type
01780     ptr = common_write_16_bit(TLS_NAMED_CURVE_SECP256R1, ptr);
01781     //There shuold be calculated case now only test purpose
01782     *ptr++ = 65; //len
01783     *ptr++ = 4; //type
01784     memcpy(ptr, heap_ptr->ecc_heap->server_public_key, 64);
01785     ptr += 64;
01786     ptr = common_write_16_bit(TLS_SIG_HASH_ALG_SHA256_ECDSA, ptr);
01787     ptr = common_write_16_bit((72 - r_zeros - s_zeros), ptr);
01788     *ptr++ = 0x30;
01789     *ptr++ = 70 - r_zeros - s_zeros;
01790     ptr += tls_write_signature_parameters(ptr, ((uint8_t *)heap_ptr->ecc_heap->sgnt->m_R.data), r_zeros);
01791     ptr += tls_write_signature_parameters(ptr, ((uint8_t *)heap_ptr->ecc_heap->sgnt->m_s.data), s_zeros);
01792     return ptr;
01793 }
01794 
01795 
01796 uint8_t *tls_certificate_verify_msg_set(uint8_t *ptr, tls_heap_t *heap_ptr)
01797 {
01798     uint8_t r_zeros;
01799     uint8_t s_zeros;
01800     r_zeros = tls_get_leading_zeros(heap_ptr->ecc_heap->sgnt->m_R.data);
01801     s_zeros = tls_get_leading_zeros(heap_ptr->ecc_heap->sgnt->m_s.data);
01802 
01803     *ptr++ = TLS_CERTIFICATE_VERIFY;
01804     ptr = common_write_24_bit((76 - r_zeros - s_zeros), ptr);
01805     *ptr++ = 4;
01806     *ptr++ = 3;
01807     *ptr++ = 0;
01808     *ptr++ = 72 - r_zeros - s_zeros;
01809     *ptr++ = 0x30;
01810     *ptr++ = 70 - r_zeros - s_zeros;
01811     ptr += tls_write_signature_parameters(ptr, ((uint8_t *)heap_ptr->ecc_heap->sgnt->m_R.data), r_zeros);
01812     ptr += tls_write_signature_parameters(ptr, ((uint8_t *)heap_ptr->ecc_heap->sgnt->m_s.data), s_zeros);
01813     return ptr;
01814 }
01815 #define CERTI_CHAIN_3
01816 
01817 uint16_t tls_certificate_len(certificate_chain_internal_t   *temp)
01818 {
01819     uint8_t i;
01820     uint16_t len = 0;
01821 
01822     if (temp) {
01823         len += 10;
01824         if (temp->chain_length > 1) {
01825             for (i = 0; i < (temp->chain_length - 1); i++) {
01826                 if (i) {
01827                     len += 3;
01828                 }
01829                 len += temp->certi_len[i + 1];
01830             }
01831         } else if (temp->chain_length == 1) {
01832             len += temp->certi_len[0];
01833         }
01834     }
01835     return len;
01836 }
01837 
01838 uint8_t *tls_certificate_msg_set(uint8_t *ptr, certificate_chain_internal_t *temp)
01839 {
01840     uint8_t i;
01841     uint16_t len = 0;
01842     len = tls_certificate_len(temp);
01843     if (temp) {
01844         len -= 4; // Cut TLS haeder space off
01845         /* Set Certificate */
01846         *ptr++ = TLS_CERTIFICATE;
01847         ptr = common_write_24_bit(len, ptr);
01848         len -= 3;
01849         ptr = common_write_24_bit(len, ptr);
01850 
01851         i = temp->chain_length;
01852         if (i > 1) {
01853             while (i) {
01854                 i--;
01855                 if (i) {
01856                     ptr = common_write_24_bit(temp->certi_len[i], ptr);
01857                     memcpy(ptr, temp->certi_chain[i], temp->certi_len[i]);
01858                     ptr += temp->certi_len[i];
01859                 }
01860             }
01861         } else if (i == 1) {
01862             ptr = common_write_24_bit(temp->certi_len[0], ptr);
01863             memcpy(ptr, temp->certi_chain[0], temp->certi_len[0]);
01864             ptr += temp->certi_len[0];
01865         }
01866     }
01867     return ptr;
01868 }
01869 
01870 uint8_t tls_write_signature_parameters(uint8_t *ptr, uint8_t *signature_parameter, uint8_t leadin_zeros)
01871 {
01872     uint8_t len = 2;
01873     uint8_t i;
01874     *ptr++ = 2;
01875     *ptr++ = 33 - leadin_zeros;
01876     if (leadin_zeros == 0) {
01877         *ptr++ = 0;
01878         len += 33;
01879     } else {
01880         leadin_zeros -= 1;
01881         len += (32 - leadin_zeros);
01882     }
01883     for (i = leadin_zeros; i < 32; i++) {
01884         *ptr++ = signature_parameter[31 - i];
01885     }
01886     return len;
01887 }
01888 
01889 void tls_ecc_point_reverse_order(uint8_t *dst, uint8_t *src)
01890 {
01891     uint8_t i;
01892     for (i = 0; i < 32; i++) {
01893         dst[i] = src[31 - i];
01894     }
01895 }
01896 #endif
01897 
01898 uint8_t *tls_build_server_hello_msg(uint8_t *ptr, tls_session_t *tls_session)
01899 {
01900     tls_heap_t *heap_ptr = tls_session->tls_heap;
01901     *ptr++ = TLS_SERVER_HELLO;
01902     ptr += 3; // fill length in at the end
01903     uint8_t *startptr = ptr;
01904     ptr = common_write_16_bit(TLS_1_2_VERSION, ptr);
01905 
01906     memcpy(ptr, (heap_ptr->tls_hello_random + SERVER_HELLO_PTR), 32);
01907     ptr += 32;
01908 
01909     *ptr++ = tls_session->id_length;
01910     memcpy(ptr, tls_session->tls_session_id, tls_session->id_length);
01911     ptr += tls_session->id_length;
01912 
01913     uint16_t ciphersuite;
01914 #ifdef ECC
01915     if (heap_ptr->tls_chipher_mode == CHIPHER_ECC) {
01916         ciphersuite = heap_ptr->client_knows_standard_ecc_ciphersuite ?
01917                              TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 :
01918                              TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_COMPAT;
01919     } else
01920 #endif
01921     {
01922         ciphersuite = TLS_PSK_WITH_AES_128_CCM_8;
01923     }
01924 
01925     ptr = common_write_16_bit(ciphersuite, ptr);
01926     *ptr++ = TLS_COMPRESSION_METHOD_NULL;
01927 
01928     if (heap_ptr->tls_chipher_mode == CHIPHER_ECC) {
01929         ptr = common_write_16_bit(6, ptr); // 6 bytes of Extensions
01930         ptr = common_write_16_bit(TLS_EXTENSION_EC_POINT_FORMATS, ptr);
01931         ptr = common_write_16_bit(2, ptr); // 2 bytes of extension data
01932         *ptr++ = 1; // 1 byte of ec_point_format_list
01933         *ptr++ = 0; // ECPointFormat::uncompressed
01934     }
01935 
01936     // Go back and fill in length
01937     common_write_24_bit(ptr - startptr, startptr - 3);
01938 
01939     if (heap_ptr->tls_chipher_mode != CHIPHER_ECC) {
01940         *ptr++ = TLS_SERVER_HELLO_DONE;
01941         ptr = common_write_24_bit(0, ptr); // length
01942     }
01943     return ptr;
01944 }
01945 
01946 void tls_header_set(buffer_t *buf)
01947 {
01948     uint8_t *ptr;
01949     uint16_t len;
01950     ptr = buffer_data_pointer(buf);
01951     len = buffer_data_length(buf);
01952     len -= 5; //Cut Flags byte off
01953     *ptr++ = TLS_HANDSHAKE;
01954     ptr = common_write_16_bit(TLS_1_2_VERSION, ptr);
01955     ptr = common_write_16_bit(len, ptr);
01956 }
01957 
01958 void tls_session_id_genrate(uint8_t *suite, uint8_t length)
01959 {
01960     if (length > 32) {
01961         length = 32;
01962     }
01963     randLIB_get_n_bytes_random(suite, length);
01964 }
01965 
01966 tls_heap_t *tls_heap_allocate(void)
01967 {
01968     tls_heap_t *heap_ptr = tls_heap_structure_allocate();
01969     if (heap_ptr) {
01970 #ifdef ECC
01971         tls_ecc_heap_t *ecc_ptr = ecc_allocate_ram();
01972         if (ecc_ptr == 0) {
01973             tr_debug("ECC Alloc Fail");
01974             ns_dyn_mem_free(heap_ptr);
01975             heap_ptr = 0;
01976             return heap_ptr;
01977         } else {
01978             //uint8_t *ptr, i;
01979             heap_ptr->ecc_heap = ecc_ptr;
01980         }
01981         heap_ptr->client_knows_standard_ecc_ciphersuite = false;
01982 #endif
01983         randLIB_get_n_bytes_random(heap_ptr->tls_hello_random , 64);
01984         ns_sha256_init(&heap_ptr->sha256_ctx);
01985         heap_ptr->tls_chipher_mode = CHIPHER_NONE;
01986         heap_ptr->tls_handshake_h_len = 0;
01987         heap_ptr->client_verify_buf = 0;
01988         heap_ptr->client_verify_buf_len = 0;
01989     }
01990     return heap_ptr;
01991 }
01992 
01993 void tls_heap_free(tls_heap_t *heap_ptr)
01994 {
01995     if (heap_ptr) {
01996         if (heap_ptr->cert_temp_buf) {
01997             if (heap_ptr->signature_temp_buf) {
01998                 if (heap_ptr->signature_temp_buf == heap_ptr->cert_temp_buf) {
01999                     heap_ptr->signature_temp_buf = 0;
02000                 }
02001 
02002 
02003             }
02004 
02005             if (heap_ptr->pointer_types & 1) {
02006                 buffer_free(heap_ptr->cert_temp_buf);
02007             } else {
02008                 ns_dyn_mem_free(heap_ptr->cert_temp_buf);
02009             }
02010             heap_ptr->cert_temp_buf = 0;
02011         }
02012         if (heap_ptr->signature_temp_buf) {
02013             if (heap_ptr->pointer_types & 2) {
02014                 buffer_free(heap_ptr->signature_temp_buf);
02015             } else {
02016                 ns_dyn_mem_free(heap_ptr->signature_temp_buf);
02017             }
02018             heap_ptr->signature_temp_buf = 0;
02019         }
02020         if (heap_ptr->client_verify_buf) {
02021             ns_dyn_mem_free(heap_ptr->client_verify_buf);
02022             heap_ptr->client_verify_buf = 0;
02023         }
02024         tls_ecc_heap_free(heap_ptr);
02025         ns_sha256_free(&heap_ptr->sha256_ctx);
02026         ns_dyn_mem_free(heap_ptr);
02027     }
02028 }
02029 #ifdef ECC
02030 void tls_ecc_heap_free(tls_heap_t *heap_ptr)
02031 {
02032     if (heap_ptr) {
02033         if (heap_ptr->ecc_heap) {
02034             if (heap_ptr->ecc_heap->sgnt) {
02035                 ecc_library_free_pointer(heap_ptr->ecc_heap->sgnt);
02036                 //ns_dyn_mem_free(heap_ptr->ecc_heap->sgnt);
02037             }
02038             ns_dyn_mem_free(heap_ptr->ecc_heap);
02039             heap_ptr->ecc_heap = 0;
02040         }
02041     }
02042 }
02043 #endif
02044 
02045 #ifdef ECC
02046 uint8_t tls_get_leading_zeros(void *data)
02047 {
02048 
02049     uint8_t *ptr = (uint8_t *)data + 31;
02050     uint8_t i = 0;
02051 
02052     for (;; ptr--) {
02053         if (*ptr & 0x80) {
02054             break;
02055         }
02056         i++;
02057         if (i == 33) {
02058             break;
02059         }
02060 
02061         if (*ptr) {
02062             break;
02063         }
02064     }
02065 
02066     return i;
02067 
02068 }
02069 #endif
02070 
02071 
02072 void tls_key_expansion_cal(tls_heap_t *heap_ptr, uint8_t *key_save_ptr, uint8_t *master_secret)
02073 {
02074     uint8_t *ptr;
02075     prf_sec_param_t *prf_ptr = shalib_prf_param_get();
02076     prf_ptr->secret = master_secret;
02077     prf_ptr->sec_len = 48;
02078     prf_ptr->label = "key expansion";
02079     prf_ptr->seed = heap_ptr->temp_buf;
02080     ptr = heap_ptr->temp_buf;
02081     memcpy(ptr, (heap_ptr->tls_hello_random + SERVER_HELLO_PTR) , 32);
02082     ptr += 32;
02083     memcpy(ptr, (heap_ptr->tls_hello_random + CLIENT_HELLO_PTR) , 32);
02084     prf_ptr->seedlen = 64;
02085     shalib_prf_calc(key_save_ptr, 10); // Why 40 bytes?
02086 }
02087 
02088 void tls_master_key_cal(tls_heap_t *heap_ptr, sec_suite_t *tls_suite)
02089 {
02090     uint8_t *ptr;
02091     prf_sec_param_t *prf_ptr = shalib_prf_param_get();
02092     uint8_t secret_buf[2 + 16 + 2 + 16];
02093     tr_debug("CAL Master secret:");
02094     //Her have to to be set check is
02095 #ifdef ECC
02096     if (heap_ptr->tls_chipher_mode == CHIPHER_ECC) {
02097         prf_ptr->secret = heap_ptr->ecc_heap->pre_secret_mat;
02098         prf_ptr->sec_len = 32;
02099     } else
02100 #endif
02101     {
02102         tls_psk_key_t *key_temp = tls_get_key(tls_suite->psk_key_id);
02103         if (key_temp) {
02104             /* RFC 4279 - premaster secret for N-octet PSK is
02105              *  +----------+----------+----------+----------+
02106              *  | 2 octets | N octets | 2 octets | N octets |
02107              *  +----------+----------+----------+----------+
02108              *  |    N     |    0     |    N     |   PSK    |
02109              *  +----------+----------+----------+----------+
02110              */
02111             ptr = secret_buf;
02112             ptr = common_write_16_bit(16, ptr);
02113             memset(ptr, 0, 16), ptr += 16;
02114             ptr = common_write_16_bit(16, ptr);
02115             memcpy(ptr, key_temp->psk_key, 16);
02116             prf_ptr->secret = secret_buf;
02117             prf_ptr->sec_len = sizeof secret_buf;
02118         }
02119     }
02120     prf_ptr->label = "master secret";
02121     prf_ptr->seed = heap_ptr->temp_buf;
02122     ptr = heap_ptr->temp_buf;
02123     memcpy(ptr, (heap_ptr->tls_hello_random + CLIENT_HELLO_PTR) , 32);
02124     ptr += 32;
02125     memcpy(ptr, (heap_ptr->tls_hello_random + SERVER_HELLO_PTR) , 32);
02126     prf_ptr->seedlen = 64;
02127 
02128     shalib_prf_calc(tls_suite->tls_session->master_secret, 12);
02129 }
02130 
02131 void tls_verify_calc(uint8_t output[12], uint8_t server, tls_heap_t *heap_ptr, uint8_t *master_secret)
02132 {
02133     prf_sec_param_t *prf_ptr = shalib_prf_param_get();
02134     prf_ptr->secret = master_secret;
02135     prf_ptr->sec_len = 48;
02136     prf_ptr->label = server ? "server finished" : "client finished";
02137     prf_ptr->seed = heap_ptr->hash_buf;
02138     prf_ptr->seedlen = 32;
02139     shalib_prf_calc(output, 3);
02140 }
02141 
02142 
02143 void tls_handshake_copy(tls_msg_t *tls_msg_ptr, tls_heap_t *heap_ptr)
02144 {
02145     uint16_t len = tls_msg_ptr->len;
02146     uint8_t *ptr = tls_msg_ptr->msg_ptr;
02147     ptr -= 4;
02148     len += 4;
02149 
02150     if (heap_ptr->tls_handshake_h_len == 0) {
02151         ns_sha256_starts(&heap_ptr->sha256_ctx);
02152     }
02153     ns_sha256_update(&heap_ptr->sha256_ctx, ptr, len);
02154     heap_ptr->tls_handshake_h_len += len;
02155 }
02156 
02157 
02158 void tls_hanshake_hash_cal(tls_heap_t *heap_ptr)
02159 {
02160     tr_debug("HASH Calc");
02161     ns_sha256_context ctx;
02162     ns_sha256_clone(&ctx, &heap_ptr->sha256_ctx);
02163     ns_sha256_finish(&ctx, heap_ptr->hash_buf);
02164     ns_sha256_free(&ctx);
02165 }
02166 
02167 #if 0
02168 uint8_t tls_txt_analyze(buffer_t *buf)
02169 {
02170     uint8_t *dptr;
02171     uint8_t tls_text_cnt = 0;
02172     uint16_t data_len = buffer_data_length(buf);
02173     uint16_t readed = 0;
02174     dptr = buffer_data_pointer(buf);
02175 
02176     while (readed  < data_len) {
02177         uint16_t tls_version;
02178         tls_header.type = *dptr++;
02179         tls_version = common_read_16_bit(dptr);
02180         dptr += 2;
02181         if (tls_version == TLS_1_2_VERSION) {
02182             tls_text_cnt = 0;
02183             break;
02184         } else {
02185             uint16_t tls_temp_var_16;
02186             tls_temp_var_16 = common_read_16_bit(dptr);
02187             dptr += 2;
02188             if (tls_temp_var_16) {
02189                 tls_header.length = tls_temp_var_16;
02190                 tls_header.ptr = dptr;
02191 
02192                 readed += 5;
02193                 readed += tls_temp_var_16;
02194 
02195                 if (readed > data_len) {
02196                     tr_debug("Over Read1: %02x", tls_header.type);
02197                     tr_debug("LEN: %04x", data_len);
02198                     tr_debug("Read: %04x", readed);
02199                     tls_text_cnt = 0;
02200                     break;
02201                 } else {
02202                     dptr += tls_temp_var_16;
02203                     tls_text_cnt++;
02204                 }
02205             } else {
02206                 tls_text_cnt = 0;
02207                 break;;
02208             }
02209         }
02210     }
02211 
02212     if (readed != data_len) {
02213         tr_debug("TSL txt Parse Fail");
02214         tls_text_cnt = 0;
02215     }
02216     return tls_text_cnt;
02217 }
02218 #endif
02219 
02220 #if 0
02221 tls_header_t *tls_message_get(uint8_t *dptr)
02222 {
02223     uint16_t tls_temp_var_16;
02224     tls_header.type = *dptr;
02225     dptr += 3;
02226     tls_temp_var_16 = common_read_16_bit(dptr);
02227     dptr += 2;
02228     tls_header.length = tls_temp_var_16;
02229     tls_header.ptr = dptr;
02230     return &tls_header;
02231 }
02232 #endif
02233 
02234 uint8_t tls_msg_analyzy(uint8_t *ptr, uint16_t data_len)
02235 {
02236     uint8_t *dptr;
02237     uint8_t msg_cnt = 0;
02238     uint16_t readed = 0, m_len = 0;
02239     dptr = ptr;
02240     while (readed  < data_len) {
02241         dptr += 1; //Skip Type
02242         if (*dptr++) {
02243             //Too long skip packet
02244             tr_debug("Too long: %s", tr_array(ptr, data_len));
02245             msg_cnt = 0;
02246             break;
02247         } else {
02248             m_len = common_read_16_bit(dptr);
02249             dptr += 2;
02250         }
02251         readed += 4;
02252         readed += m_len;
02253         dptr += m_len;
02254 
02255         if (readed > data_len) {
02256             tr_debug("Over Read1: %02x", tls_msg.type);
02257             tr_debug("LEN: %04x", data_len);
02258             tr_debug("Read: %04x", readed);
02259             msg_cnt = 0;
02260             break;
02261         } else {
02262             msg_cnt++;
02263         }
02264     }
02265     if (readed != data_len) {
02266         tr_debug("TLS MSG Parse Fail");
02267         msg_cnt = 0;
02268     }
02269     return msg_cnt;
02270 }
02271 
02272 tls_msg_t *tls_msg_get(uint8_t *dptr)
02273 {
02274     tls_msg.type = *dptr;
02275     dptr += 2;
02276     tls_msg.len = common_read_16_bit(dptr);
02277     dptr += 2;
02278     tls_msg.msg_ptr = dptr;
02279     return &tls_msg;
02280 }
02281 
02282 tls_msg_t *tls_msg_ptr_get(void)
02283 {
02284     return &tls_msg;
02285 }
02286 
02287 void tls_build_client_verify_payload(tls_heap_t *tls_heap)
02288 {
02289     uint8_t *ptr;
02290     ptr = tls_heap->hash_buf;
02291     ptr = common_write_32_bit(0x1400000c, ptr);
02292     memcpy(ptr, tls_heap->verify, 12);
02293 }
02294 
02295 void tls_nonce_update(uint8_t *ptr)
02296 {
02297     uint8_t i = 8;
02298     ptr += 7;
02299 
02300     while (i--) {
02301         if (*ptr == 0xff) {
02302             *ptr = 0;
02303             ptr--;
02304         } else {
02305             *ptr += 1;
02306             break;
02307         }
02308     }
02309 }
02310 #ifdef ECC
02311 uint8_t tls_parse_certificate_request(uint8_t *ptr, uint16_t len)
02312 {
02313     (void)len;
02314     uint16_t hash_alg;
02315     uint16_t count;
02316     uint8_t found = 0;
02317     count = *ptr++;
02318 
02319     if (!count) {
02320         tr_debug("cert req 0 count 1");
02321         return 0;
02322     }
02323 
02324     while (count--) {
02325 
02326         if (*ptr++ == TLS_CERT_TYPE_ECDSA) {
02327 
02328             found = TLS_CERT_TYPE_ECDSA;
02329 
02330         }
02331 
02332     }
02333 
02334     if (!found) {
02335         tr_debug("cert req no supported type");
02336         return 0;
02337     }
02338     found = 0;
02339 
02340     count = (uint16_t) * ptr++ << 8;
02341     count |= *ptr++;
02342     count >>= 1;
02343 
02344     if (!count) {
02345         tr_debug("cert req 0 count 2");
02346         return 0;
02347     }
02348 
02349 
02350     while (count--) {
02351 
02352         hash_alg = common_read_16_bit(ptr);
02353         ptr += 2;
02354         if (hash_alg == TLS_SIG_HASH_ALG_SHA256_ECDSA) {
02355             found = 1;
02356         }
02357 
02358     }
02359 
02360     if (!found) {
02361 
02362         tr_debug("cert req no supported Algo");
02363         return 0;
02364     }
02365 
02366     return 1;
02367     /* There is also names field but we do not have parser for it yet */
02368 
02369 }
02370 
02371 
02372 static uint8_t ecc_verify_calculate_hash(sec_suite_t *tls_suite)
02373 {
02374     tls_heap_t *theap = tls_suite->tls_session->tls_heap;
02375     if (!(tls_suite->setups & TLS_ECC_CERTIFICATE_RECEIVED)) {
02376         return 1;
02377     } else {
02378         tls_suite->setups &= ~TLS_ECC_CERTIFICATE_RECEIVED;
02379     }
02380 
02381     if (tls_suite->setups & TLS_SERVER_MODE) {
02382         if (!(tls_suite->setups & TLS_ECC_CERTIFICATE_VERIFY)) {
02383             return 2;
02384         } else {
02385             tls_suite->setups &= ~TLS_ECC_CERTIFICATE_VERIFY;
02386         }
02387     }
02388     if (!theap->ecc_heap->sgnt) {
02389         return 3;
02390     }
02391 
02392     tls_ecc_server_key_signature_hash(theap);
02393     return 0;
02394 
02395 }
02396 
02397 
02398 void tls_ecc_reverse_hash(uint8_t *ptr)
02399 {
02400     uint8_t start = 0;
02401     uint8_t end = 31;
02402     uint8_t chr;
02403     while (start < end) {
02404         chr = ptr[start];
02405         ptr[start] = ptr[end];
02406         ptr[end] = chr;
02407 
02408         start++;
02409         end--;
02410     }
02411 }
02412 
02413 void tls_ecc_server_key_signature_hash(tls_heap_t *heap_ptr)
02414 {
02415     memset(heap_ptr->ecc_heap->sgnt->m_m.data, 0, MPINT_DATA_SIZE);
02416     ns_sha256_context ctx;
02417     ns_sha256_init(&ctx);
02418     ns_sha256_starts(&ctx);
02419     ns_sha256_update(&ctx, heap_ptr->tls_hello_random, 64);
02420     //ServerECDHParams(ECParameters+ECPoint)
02421     heap_ptr->hash_buf[0] = TLS_EC_CURVE_TYPE_NAMED_CURVE;
02422     common_write_16_bit(TLS_NAMED_CURVE_SECP256R1, heap_ptr->hash_buf + 1);
02423     heap_ptr->hash_buf[3] = 65; // ECPoint public - length indicator for following 512-bit public key?
02424     heap_ptr->hash_buf[4] = 4;
02425     ns_sha256_update(&ctx, heap_ptr->hash_buf, 5);
02426     ns_sha256_update(&ctx, heap_ptr->ecc_heap->server_public_key, 64);
02427     ns_sha256_finish(&ctx, heap_ptr->ecc_heap->sgnt->m_m.data);
02428     ns_sha256_free(&ctx);
02429     tls_ecc_reverse_hash((uint8_t *)heap_ptr->ecc_heap->sgnt->m_m.data);
02430 }
02431 
02432 #endif
02433 
02434 #else
02435 int8_t arm_tls_add_psk_key(const uint8_t *key_ptr, uint16_t key_id) {
02436     (void)key_ptr;
02437     (void)key_id;
02438     return -1;
02439 }
02440 #endif
02441 
02442 
02443