Ram Gandikota
/
IOTMetronome
FRDM K64F Metronome
mbed-client/mbed-client/m2mconnectionsecurity.h@0:dbad57390bd1, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:37:05 2017 +0000
- Revision:
- 0:dbad57390bd1
Initial commit
Who changed what in which revision?
User | Revision | Line number | New 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 | #ifndef __M2M_CONNECTION_SECURITY_H__ |
ram54288 | 0:dbad57390bd1 | 17 | #define __M2M_CONNECTION_SECURITY_H__ |
ram54288 | 0:dbad57390bd1 | 18 | |
ram54288 | 0:dbad57390bd1 | 19 | #include "mbed-client/m2mconfig.h" |
ram54288 | 0:dbad57390bd1 | 20 | |
ram54288 | 0:dbad57390bd1 | 21 | #include <stdint.h> |
ram54288 | 0:dbad57390bd1 | 22 | |
ram54288 | 0:dbad57390bd1 | 23 | class M2MConnectionHandler; |
ram54288 | 0:dbad57390bd1 | 24 | class M2MSecurity; |
ram54288 | 0:dbad57390bd1 | 25 | class M2MConnectionSecurityPimpl; |
ram54288 | 0:dbad57390bd1 | 26 | class M2MConnectionHandler; |
ram54288 | 0:dbad57390bd1 | 27 | |
ram54288 | 0:dbad57390bd1 | 28 | /*! \file m2mconnectionsecurity.h |
ram54288 | 0:dbad57390bd1 | 29 | * \brief M2MConnectionSecurity. |
ram54288 | 0:dbad57390bd1 | 30 | * This class provides a method to create a secure socket connection |
ram54288 | 0:dbad57390bd1 | 31 | * to handle connectivity for the mbed Client. It handles sending, receiving |
ram54288 | 0:dbad57390bd1 | 32 | * and establishing a secure connection for mbed Client on top of the |
ram54288 | 0:dbad57390bd1 | 33 | * normal socket connection. |
ram54288 | 0:dbad57390bd1 | 34 | */ |
ram54288 | 0:dbad57390bd1 | 35 | |
ram54288 | 0:dbad57390bd1 | 36 | class M2MConnectionSecurity { |
ram54288 | 0:dbad57390bd1 | 37 | public: |
ram54288 | 0:dbad57390bd1 | 38 | typedef enum { |
ram54288 | 0:dbad57390bd1 | 39 | NO_SECURITY = 0, |
ram54288 | 0:dbad57390bd1 | 40 | TLS, |
ram54288 | 0:dbad57390bd1 | 41 | DTLS |
ram54288 | 0:dbad57390bd1 | 42 | } SecurityMode; |
ram54288 | 0:dbad57390bd1 | 43 | |
ram54288 | 0:dbad57390bd1 | 44 | private: |
ram54288 | 0:dbad57390bd1 | 45 | // Prevents the use of assignment operator by accident. |
ram54288 | 0:dbad57390bd1 | 46 | M2MConnectionSecurity& operator=( const M2MConnectionSecurity& /*other*/ ); |
ram54288 | 0:dbad57390bd1 | 47 | // Prevents the use of copy constructor by accident. |
ram54288 | 0:dbad57390bd1 | 48 | M2MConnectionSecurity( const M2MConnectionSecurity& /*other*/ ); |
ram54288 | 0:dbad57390bd1 | 49 | |
ram54288 | 0:dbad57390bd1 | 50 | public: |
ram54288 | 0:dbad57390bd1 | 51 | /** |
ram54288 | 0:dbad57390bd1 | 52 | * \brief Default Constructor. |
ram54288 | 0:dbad57390bd1 | 53 | */ |
ram54288 | 0:dbad57390bd1 | 54 | M2MConnectionSecurity(SecurityMode mode); |
ram54288 | 0:dbad57390bd1 | 55 | |
ram54288 | 0:dbad57390bd1 | 56 | /** |
ram54288 | 0:dbad57390bd1 | 57 | * \brief Default Destructor. |
ram54288 | 0:dbad57390bd1 | 58 | */ |
ram54288 | 0:dbad57390bd1 | 59 | ~M2MConnectionSecurity(); |
ram54288 | 0:dbad57390bd1 | 60 | |
ram54288 | 0:dbad57390bd1 | 61 | /** |
ram54288 | 0:dbad57390bd1 | 62 | * \brief Resets the socket connection states. |
ram54288 | 0:dbad57390bd1 | 63 | */ |
ram54288 | 0:dbad57390bd1 | 64 | void reset(); |
ram54288 | 0:dbad57390bd1 | 65 | |
ram54288 | 0:dbad57390bd1 | 66 | /** |
ram54288 | 0:dbad57390bd1 | 67 | * \brief Initiatlizes the socket connection states. |
ram54288 | 0:dbad57390bd1 | 68 | */ |
ram54288 | 0:dbad57390bd1 | 69 | int init(const M2MSecurity *security); |
ram54288 | 0:dbad57390bd1 | 70 | |
ram54288 | 0:dbad57390bd1 | 71 | /** |
ram54288 | 0:dbad57390bd1 | 72 | * \brief Starts the connection in non-blocking mode. |
ram54288 | 0:dbad57390bd1 | 73 | * \param connHandler The ConnectionHandler object that maintains the socket. |
ram54288 | 0:dbad57390bd1 | 74 | * \return Returns the state of the connection. Successful or not. |
ram54288 | 0:dbad57390bd1 | 75 | */ |
ram54288 | 0:dbad57390bd1 | 76 | int start_connecting_non_blocking(M2MConnectionHandler* connHandler); |
ram54288 | 0:dbad57390bd1 | 77 | |
ram54288 | 0:dbad57390bd1 | 78 | /** |
ram54288 | 0:dbad57390bd1 | 79 | * \brief Continues connectivity logic for a secure connection. |
ram54288 | 0:dbad57390bd1 | 80 | * \return Returns an error code if any while continuing the connection sequence. |
ram54288 | 0:dbad57390bd1 | 81 | */ |
ram54288 | 0:dbad57390bd1 | 82 | int continue_connecting(); |
ram54288 | 0:dbad57390bd1 | 83 | |
ram54288 | 0:dbad57390bd1 | 84 | /** |
ram54288 | 0:dbad57390bd1 | 85 | * \brief Connects the client to the server. |
ram54288 | 0:dbad57390bd1 | 86 | * \param connHandler The ConnectionHandler object that maintains the socket. |
ram54288 | 0:dbad57390bd1 | 87 | * \return Returns the state of the connection. Successful or not. |
ram54288 | 0:dbad57390bd1 | 88 | */ |
ram54288 | 0:dbad57390bd1 | 89 | int connect(M2MConnectionHandler* connHandler); |
ram54288 | 0:dbad57390bd1 | 90 | |
ram54288 | 0:dbad57390bd1 | 91 | /** |
ram54288 | 0:dbad57390bd1 | 92 | * \brief Sends data to the server. |
ram54288 | 0:dbad57390bd1 | 93 | * \param message The data to be sent. |
ram54288 | 0:dbad57390bd1 | 94 | * \param len The length of the data. |
ram54288 | 0:dbad57390bd1 | 95 | * \return Indicates whether the data is sent successfully or not. |
ram54288 | 0:dbad57390bd1 | 96 | */ |
ram54288 | 0:dbad57390bd1 | 97 | int send_message(unsigned char *message, int len); |
ram54288 | 0:dbad57390bd1 | 98 | |
ram54288 | 0:dbad57390bd1 | 99 | /** |
ram54288 | 0:dbad57390bd1 | 100 | * \brief Reads the data received from the server. |
ram54288 | 0:dbad57390bd1 | 101 | * \param message The data to be read. |
ram54288 | 0:dbad57390bd1 | 102 | * \param len The length of the data. |
ram54288 | 0:dbad57390bd1 | 103 | * \return Indicates whether the data is read successfully or not. |
ram54288 | 0:dbad57390bd1 | 104 | */ |
ram54288 | 0:dbad57390bd1 | 105 | int read(unsigned char* buffer, uint16_t len); |
ram54288 | 0:dbad57390bd1 | 106 | |
ram54288 | 0:dbad57390bd1 | 107 | /** |
ram54288 | 0:dbad57390bd1 | 108 | * \brief Sets the function callback that is called by mbed Client to |
ram54288 | 0:dbad57390bd1 | 109 | * fetch a random number from an application to ensure strong entropy. |
ram54288 | 0:dbad57390bd1 | 110 | * \param random_callback A function pointer that is called by mbed Client |
ram54288 | 0:dbad57390bd1 | 111 | * while performing a secure handshake. |
ram54288 | 0:dbad57390bd1 | 112 | * The function signature should be uint32_t (*random_number_callback)(void); |
ram54288 | 0:dbad57390bd1 | 113 | */ |
ram54288 | 0:dbad57390bd1 | 114 | void set_random_number_callback(random_number_cb callback); |
ram54288 | 0:dbad57390bd1 | 115 | |
ram54288 | 0:dbad57390bd1 | 116 | /** |
ram54288 | 0:dbad57390bd1 | 117 | * \brief Sets the function callback that is called by mbed Client to |
ram54288 | 0:dbad57390bd1 | 118 | * provide an entropy source from an application to ensure strong entropy. |
ram54288 | 0:dbad57390bd1 | 119 | * \param entropy_callback A function pointer that is called by mbed-client |
ram54288 | 0:dbad57390bd1 | 120 | * while performing a secure handshake. |
ram54288 | 0:dbad57390bd1 | 121 | * Function signature, if using mbed-client-mbedtls, should be |
ram54288 | 0:dbad57390bd1 | 122 | * int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, |
ram54288 | 0:dbad57390bd1 | 123 | * size_t len, size_t *olen); |
ram54288 | 0:dbad57390bd1 | 124 | */ |
ram54288 | 0:dbad57390bd1 | 125 | void set_entropy_callback(entropy_cb callback); |
ram54288 | 0:dbad57390bd1 | 126 | |
ram54288 | 0:dbad57390bd1 | 127 | private: |
ram54288 | 0:dbad57390bd1 | 128 | |
ram54288 | 0:dbad57390bd1 | 129 | M2MConnectionSecurityPimpl* _private_impl; |
ram54288 | 0:dbad57390bd1 | 130 | |
ram54288 | 0:dbad57390bd1 | 131 | friend class Test_M2MConnectionSecurity; |
ram54288 | 0:dbad57390bd1 | 132 | //friend class Test_M2MConnectionSecurityImpl; |
ram54288 | 0:dbad57390bd1 | 133 | }; |
ram54288 | 0:dbad57390bd1 | 134 | |
ram54288 | 0:dbad57390bd1 | 135 | #endif //__M2M_CONNECTION_SECURITY_H__ |