FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:dbad57390bd1 1 /*
ram54288 0:dbad57390bd1 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
ram54288 0:dbad57390bd1 3 * SPDX-License-Identifier: Apache-2.0
ram54288 0:dbad57390bd1 4 * Licensed under the Apache License, Version 2.0 (the License); you may
ram54288 0:dbad57390bd1 5 * not use this file except in compliance with the License.
ram54288 0:dbad57390bd1 6 * You may obtain a copy of the License at
ram54288 0:dbad57390bd1 7 *
ram54288 0:dbad57390bd1 8 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:dbad57390bd1 9 *
ram54288 0:dbad57390bd1 10 * Unless required by applicable law or agreed to in writing, software
ram54288 0:dbad57390bd1 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
ram54288 0:dbad57390bd1 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:dbad57390bd1 13 * See the License for the specific language governing permissions and
ram54288 0:dbad57390bd1 14 * limitations under the License.
ram54288 0:dbad57390bd1 15 */
ram54288 0:dbad57390bd1 16
ram54288 0:dbad57390bd1 17 #ifndef __M2M_CONNECTION_SECURITY_PIMPL_H__
ram54288 0:dbad57390bd1 18 #define __M2M_CONNECTION_SECURITY_PIMPL_H__
ram54288 0:dbad57390bd1 19
ram54288 0:dbad57390bd1 20 #include "mbed-client/m2mconnectionsecurity.h"
ram54288 0:dbad57390bd1 21 #include "mbed-client/m2mtimerobserver.h"
ram54288 0:dbad57390bd1 22 #include "mbed-client/m2mconstants.h"
ram54288 0:dbad57390bd1 23 #include "mbed-client/m2msecurity.h"
ram54288 0:dbad57390bd1 24
ram54288 0:dbad57390bd1 25 #include "mbedtls/config.h"
ram54288 0:dbad57390bd1 26 #include "mbedtls/platform.h"
ram54288 0:dbad57390bd1 27 #include "mbedtls/debug.h"
ram54288 0:dbad57390bd1 28 #include "mbedtls/ssl.h"
ram54288 0:dbad57390bd1 29 #include "mbedtls/entropy.h"
ram54288 0:dbad57390bd1 30 #include "mbedtls/ctr_drbg.h"
ram54288 0:dbad57390bd1 31 #include "mbedtls/error.h"
ram54288 0:dbad57390bd1 32 #include "mbedtls/certs.h"
ram54288 0:dbad57390bd1 33 #include "mbedtls/entropy_poll.h"
ram54288 0:dbad57390bd1 34
ram54288 0:dbad57390bd1 35 class M2MTimer;
ram54288 0:dbad57390bd1 36
ram54288 0:dbad57390bd1 37 //TODO: Should we let application to select these or not??
ram54288 0:dbad57390bd1 38 const static int PSK_SUITES[] = {
ram54288 0:dbad57390bd1 39 MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256,
ram54288 0:dbad57390bd1 40 MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8,
ram54288 0:dbad57390bd1 41 MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8,
ram54288 0:dbad57390bd1 42 0
ram54288 0:dbad57390bd1 43 };
ram54288 0:dbad57390bd1 44
ram54288 0:dbad57390bd1 45
ram54288 0:dbad57390bd1 46 /**
ram54288 0:dbad57390bd1 47 * @brief The M2MConnectionSecurityPimpl class
ram54288 0:dbad57390bd1 48 */
ram54288 0:dbad57390bd1 49 class M2MConnectionSecurityPimpl : public M2MTimerObserver {
ram54288 0:dbad57390bd1 50
ram54288 0:dbad57390bd1 51 private:
ram54288 0:dbad57390bd1 52
ram54288 0:dbad57390bd1 53 // Prevents the use of assignment operator by accident.
ram54288 0:dbad57390bd1 54 M2MConnectionSecurityPimpl& operator=( const M2MConnectionSecurityPimpl& /*other*/ );
ram54288 0:dbad57390bd1 55 // Prevents the use of copy constructor by accident
ram54288 0:dbad57390bd1 56 M2MConnectionSecurityPimpl( const M2MConnectionSecurityPimpl& /*other*/ );
ram54288 0:dbad57390bd1 57
ram54288 0:dbad57390bd1 58 public:
ram54288 0:dbad57390bd1 59
ram54288 0:dbad57390bd1 60 /**
ram54288 0:dbad57390bd1 61 * @brief Constructor
ram54288 0:dbad57390bd1 62 */
ram54288 0:dbad57390bd1 63 M2MConnectionSecurityPimpl(M2MConnectionSecurity::SecurityMode mode);
ram54288 0:dbad57390bd1 64
ram54288 0:dbad57390bd1 65 /**
ram54288 0:dbad57390bd1 66 * @brief Destructor
ram54288 0:dbad57390bd1 67 */
ram54288 0:dbad57390bd1 68 virtual ~M2MConnectionSecurityPimpl();
ram54288 0:dbad57390bd1 69
ram54288 0:dbad57390bd1 70 /**
ram54288 0:dbad57390bd1 71 * \brief Resets the socket connection states.
ram54288 0:dbad57390bd1 72 */
ram54288 0:dbad57390bd1 73 void reset();
ram54288 0:dbad57390bd1 74
ram54288 0:dbad57390bd1 75 /**
ram54288 0:dbad57390bd1 76 * \brief Initiatlizes the socket connection states.
ram54288 0:dbad57390bd1 77 */
ram54288 0:dbad57390bd1 78 int init(const M2MSecurity *security);
ram54288 0:dbad57390bd1 79
ram54288 0:dbad57390bd1 80 /**
ram54288 0:dbad57390bd1 81 * \brief Starts the connection in non-blocking mode.
ram54288 0:dbad57390bd1 82 * \param connHandler The ConnectionHandler object that maintains the socket.
ram54288 0:dbad57390bd1 83 * \return Returns the state of the connection. Successful or not.
ram54288 0:dbad57390bd1 84 */
ram54288 0:dbad57390bd1 85 int start_connecting_non_blocking(M2MConnectionHandler* connHandler);
ram54288 0:dbad57390bd1 86
ram54288 0:dbad57390bd1 87 /**
ram54288 0:dbad57390bd1 88 * \brief Continues connectivity logic for secure connection.
ram54288 0:dbad57390bd1 89 * \return Returns an error code if any while continuing the connection sequence.
ram54288 0:dbad57390bd1 90 */
ram54288 0:dbad57390bd1 91 int continue_connecting();
ram54288 0:dbad57390bd1 92
ram54288 0:dbad57390bd1 93 /**
ram54288 0:dbad57390bd1 94 * \brief Connects the client to the server.
ram54288 0:dbad57390bd1 95 * \param connHandler The ConnectionHandler object that maintains the socket.
ram54288 0:dbad57390bd1 96 * \return Returns the state of the connection. Successful or not.
ram54288 0:dbad57390bd1 97 */
ram54288 0:dbad57390bd1 98 int connect(M2MConnectionHandler* connHandler);
ram54288 0:dbad57390bd1 99
ram54288 0:dbad57390bd1 100 /**
ram54288 0:dbad57390bd1 101 * \brief Sends data to the server.
ram54288 0:dbad57390bd1 102 * \param message The data to be sent.
ram54288 0:dbad57390bd1 103 * \param len The length of the data.
ram54288 0:dbad57390bd1 104 * @return Indicates whether the data is sent successfully or not.
ram54288 0:dbad57390bd1 105 */
ram54288 0:dbad57390bd1 106 int send_message(unsigned char *message, int len);
ram54288 0:dbad57390bd1 107
ram54288 0:dbad57390bd1 108 /**
ram54288 0:dbad57390bd1 109 * \brief Reads the data received from the server.
ram54288 0:dbad57390bd1 110 * \param message The data to be read.
ram54288 0:dbad57390bd1 111 * \param len The length of the data.
ram54288 0:dbad57390bd1 112 * \return Indicates whether the data is read successfully or not.
ram54288 0:dbad57390bd1 113 */
ram54288 0:dbad57390bd1 114 int read(unsigned char* buffer, uint16_t len);
ram54288 0:dbad57390bd1 115
ram54288 0:dbad57390bd1 116 /**
ram54288 0:dbad57390bd1 117 * \brief Sets the function callback that will be called by mbed-client for
ram54288 0:dbad57390bd1 118 * fetching random number from application for ensuring strong entropy.
ram54288 0:dbad57390bd1 119 * \param random_callback A function pointer that will be called by mbed-client
ram54288 0:dbad57390bd1 120 * while performing secure handshake.
ram54288 0:dbad57390bd1 121 * Function signature should be uint32_t (*random_number_callback)(void);
ram54288 0:dbad57390bd1 122 */
ram54288 0:dbad57390bd1 123 void set_random_number_callback(random_number_cb callback);
ram54288 0:dbad57390bd1 124
ram54288 0:dbad57390bd1 125 /**
ram54288 0:dbad57390bd1 126 * \brief Sets the function callback that will be called by mbed-client for
ram54288 0:dbad57390bd1 127 * providing entropy source from application for ensuring strong entropy.
ram54288 0:dbad57390bd1 128 * \param entropy_callback A function pointer that will be called by mbed-client
ram54288 0:dbad57390bd1 129 * while performing secure handshake.
ram54288 0:dbad57390bd1 130 * Function signature , if using mbed-client-mbedtls should be
ram54288 0:dbad57390bd1 131 * int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output,
ram54288 0:dbad57390bd1 132 * size_t len, size_t *olen);
ram54288 0:dbad57390bd1 133 */
ram54288 0:dbad57390bd1 134 void set_entropy_callback(entropy_cb callback);
ram54288 0:dbad57390bd1 135
ram54288 0:dbad57390bd1 136 protected: //From M2MTimerObserver
ram54288 0:dbad57390bd1 137
ram54288 0:dbad57390bd1 138 virtual void timer_expired(M2MTimerObserver::Type type);
ram54288 0:dbad57390bd1 139
ram54288 0:dbad57390bd1 140 private:
ram54288 0:dbad57390bd1 141
ram54288 0:dbad57390bd1 142 int start_handshake();
ram54288 0:dbad57390bd1 143
ram54288 0:dbad57390bd1 144 private:
ram54288 0:dbad57390bd1 145
ram54288 0:dbad57390bd1 146 bool _init_done;
ram54288 0:dbad57390bd1 147 mbedtls_ssl_config _conf;
ram54288 0:dbad57390bd1 148 mbedtls_ssl_context _ssl;
ram54288 0:dbad57390bd1 149 mbedtls_x509_crt _cacert;
ram54288 0:dbad57390bd1 150 mbedtls_x509_crt _owncert;
ram54288 0:dbad57390bd1 151 mbedtls_pk_context _pkey;
ram54288 0:dbad57390bd1 152 mbedtls_ctr_drbg_context _ctr_drbg;
ram54288 0:dbad57390bd1 153 mbedtls_entropy_context _entropy;
ram54288 0:dbad57390bd1 154 uint32_t _flags;
ram54288 0:dbad57390bd1 155 M2MTimer *_timer;
ram54288 0:dbad57390bd1 156 M2MConnectionSecurity::SecurityMode _sec_mode;
ram54288 0:dbad57390bd1 157
ram54288 0:dbad57390bd1 158 friend class Test_M2MConnectionSecurityPimpl;
ram54288 0:dbad57390bd1 159 };
ram54288 0:dbad57390bd1 160
ram54288 0:dbad57390bd1 161 #endif //__M2M_CONNECTION_SECURITY_PIMPL_H__