Maxim nexpaq / nexpaq_dev
Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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