A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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