mbed client lightswitch demo
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbed-client/mbed-client/m2msecurity.h@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbedAustin | 11:cada08fc8a70 | 1 | /* |
| mbedAustin | 11:cada08fc8a70 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
| mbedAustin | 11:cada08fc8a70 | 3 | * SPDX-License-Identifier: Apache-2.0 |
| mbedAustin | 11:cada08fc8a70 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| mbedAustin | 11:cada08fc8a70 | 5 | * not use this file except in compliance with the License. |
| mbedAustin | 11:cada08fc8a70 | 6 | * You may obtain a copy of the License at |
| mbedAustin | 11:cada08fc8a70 | 7 | * |
| mbedAustin | 11:cada08fc8a70 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| mbedAustin | 11:cada08fc8a70 | 9 | * |
| mbedAustin | 11:cada08fc8a70 | 10 | * Unless required by applicable law or agreed to in writing, software |
| mbedAustin | 11:cada08fc8a70 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| mbedAustin | 11:cada08fc8a70 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| mbedAustin | 11:cada08fc8a70 | 13 | * See the License for the specific language governing permissions and |
| mbedAustin | 11:cada08fc8a70 | 14 | * limitations under the License. |
| mbedAustin | 11:cada08fc8a70 | 15 | */ |
| mbedAustin | 11:cada08fc8a70 | 16 | #ifndef M2M_SECURITY_H |
| mbedAustin | 11:cada08fc8a70 | 17 | #define M2M_SECURITY_H |
| mbedAustin | 11:cada08fc8a70 | 18 | |
| mbedAustin | 11:cada08fc8a70 | 19 | #include "mbed-client/m2mobject.h" |
| mbedAustin | 11:cada08fc8a70 | 20 | |
| mbedAustin | 11:cada08fc8a70 | 21 | // FORWARD DECLARATION |
| mbedAustin | 11:cada08fc8a70 | 22 | class M2MResource; |
| mbedAustin | 11:cada08fc8a70 | 23 | |
| mbedAustin | 11:cada08fc8a70 | 24 | /** |
| mbedAustin | 11:cada08fc8a70 | 25 | * @brief M2MSecurity. |
| mbedAustin | 11:cada08fc8a70 | 26 | * This class represents an interface for the Security Object model of the LWM2M framework. |
| mbedAustin | 11:cada08fc8a70 | 27 | * It handles the security object instances and all corresponding |
| mbedAustin | 11:cada08fc8a70 | 28 | * resources. |
| mbedAustin | 11:cada08fc8a70 | 29 | */ |
| mbedAustin | 11:cada08fc8a70 | 30 | |
| mbedAustin | 11:cada08fc8a70 | 31 | class M2MSecurity : public M2MObject { |
| mbedAustin | 11:cada08fc8a70 | 32 | |
| mbedAustin | 11:cada08fc8a70 | 33 | friend class M2MInterfaceFactory; |
| mbedAustin | 11:cada08fc8a70 | 34 | friend class M2MNsdlInterface; |
| mbedAustin | 11:cada08fc8a70 | 35 | |
| mbedAustin | 11:cada08fc8a70 | 36 | public: |
| mbedAustin | 11:cada08fc8a70 | 37 | |
| mbedAustin | 11:cada08fc8a70 | 38 | /** |
| mbedAustin | 11:cada08fc8a70 | 39 | * @brief Enum defining all resources associated with a |
| mbedAustin | 11:cada08fc8a70 | 40 | * Security Object in the LWM2M framework. |
| mbedAustin | 11:cada08fc8a70 | 41 | */ |
| mbedAustin | 11:cada08fc8a70 | 42 | typedef enum { |
| mbedAustin | 11:cada08fc8a70 | 43 | M2MServerUri, |
| mbedAustin | 11:cada08fc8a70 | 44 | BootstrapServer, |
| mbedAustin | 11:cada08fc8a70 | 45 | SecurityMode, |
| mbedAustin | 11:cada08fc8a70 | 46 | PublicKey, |
| mbedAustin | 11:cada08fc8a70 | 47 | ServerPublicKey, |
| mbedAustin | 11:cada08fc8a70 | 48 | Secretkey, |
| mbedAustin | 11:cada08fc8a70 | 49 | SMSSecurityMode, |
| mbedAustin | 11:cada08fc8a70 | 50 | SMSBindingKey, |
| mbedAustin | 11:cada08fc8a70 | 51 | SMSBindingSecretKey, |
| mbedAustin | 11:cada08fc8a70 | 52 | M2MServerSMSNumber, |
| mbedAustin | 11:cada08fc8a70 | 53 | ShortServerID, |
| mbedAustin | 11:cada08fc8a70 | 54 | ClientHoldOffTime |
| mbedAustin | 11:cada08fc8a70 | 55 | }SecurityResource; |
| mbedAustin | 11:cada08fc8a70 | 56 | |
| mbedAustin | 11:cada08fc8a70 | 57 | /** |
| mbedAustin | 11:cada08fc8a70 | 58 | * @brief Enum defining the type of a security attribute |
| mbedAustin | 11:cada08fc8a70 | 59 | * used by the Security Object. |
| mbedAustin | 11:cada08fc8a70 | 60 | */ |
| mbedAustin | 11:cada08fc8a70 | 61 | typedef enum { |
| mbedAustin | 11:cada08fc8a70 | 62 | SecurityNotSet = -1, |
| mbedAustin | 11:cada08fc8a70 | 63 | Psk = 0, |
| mbedAustin | 11:cada08fc8a70 | 64 | Certificate = 2, |
| mbedAustin | 11:cada08fc8a70 | 65 | NoSecurity = 3 |
| mbedAustin | 11:cada08fc8a70 | 66 | } SecurityModeType; |
| mbedAustin | 11:cada08fc8a70 | 67 | |
| mbedAustin | 11:cada08fc8a70 | 68 | /** |
| mbedAustin | 11:cada08fc8a70 | 69 | * @brief Enum defining an interface operation that can be |
| mbedAustin | 11:cada08fc8a70 | 70 | * handled by the Security Object. |
| mbedAustin | 11:cada08fc8a70 | 71 | */ |
| mbedAustin | 11:cada08fc8a70 | 72 | typedef enum { |
| mbedAustin | 11:cada08fc8a70 | 73 | Bootstrap = 0x0, |
| mbedAustin | 11:cada08fc8a70 | 74 | M2MServer = 0x1 |
| mbedAustin | 11:cada08fc8a70 | 75 | } ServerType; |
| mbedAustin | 11:cada08fc8a70 | 76 | |
| mbedAustin | 11:cada08fc8a70 | 77 | private: |
| mbedAustin | 11:cada08fc8a70 | 78 | |
| mbedAustin | 11:cada08fc8a70 | 79 | /** |
| mbedAustin | 11:cada08fc8a70 | 80 | * @brief Constructor |
| mbedAustin | 11:cada08fc8a70 | 81 | * @param server_type, Type of the security object created. Either bootstrap or LWM2M server. |
| mbedAustin | 11:cada08fc8a70 | 82 | */ |
| mbedAustin | 11:cada08fc8a70 | 83 | M2MSecurity(ServerType server_type); |
| mbedAustin | 11:cada08fc8a70 | 84 | |
| mbedAustin | 11:cada08fc8a70 | 85 | // Prevents the use of default constructor. |
| mbedAustin | 11:cada08fc8a70 | 86 | M2MSecurity(); |
| mbedAustin | 11:cada08fc8a70 | 87 | |
| mbedAustin | 11:cada08fc8a70 | 88 | // Prevents the use of assignment operator. |
| mbedAustin | 11:cada08fc8a70 | 89 | M2MSecurity& operator=( const M2MSecurity& /*other*/ ); |
| mbedAustin | 11:cada08fc8a70 | 90 | |
| mbedAustin | 11:cada08fc8a70 | 91 | // Prevents the use of copy constructor |
| mbedAustin | 11:cada08fc8a70 | 92 | M2MSecurity( const M2MSecurity& /*other*/ ); |
| mbedAustin | 11:cada08fc8a70 | 93 | |
| mbedAustin | 11:cada08fc8a70 | 94 | public: |
| mbedAustin | 11:cada08fc8a70 | 95 | |
| mbedAustin | 11:cada08fc8a70 | 96 | /** |
| mbedAustin | 11:cada08fc8a70 | 97 | * @brief Destructor |
| mbedAustin | 11:cada08fc8a70 | 98 | */ |
| mbedAustin | 11:cada08fc8a70 | 99 | virtual ~M2MSecurity(); |
| mbedAustin | 11:cada08fc8a70 | 100 | |
| mbedAustin | 11:cada08fc8a70 | 101 | /** |
| mbedAustin | 11:cada08fc8a70 | 102 | * @brief Creates a new resource for given resource enum. |
| mbedAustin | 11:cada08fc8a70 | 103 | * @param rescource, With this function, the following resources can be created: |
| mbedAustin | 11:cada08fc8a70 | 104 | * ' BootstrapServer', 'SecurityMode', 'SMSSecurityMode', |
| mbedAustin | 11:cada08fc8a70 | 105 | * 'M2MServerSMSNumber', 'ShortServerID', 'ClientHoldOffTime'. |
| mbedAustin | 11:cada08fc8a70 | 106 | * @param value, Value to be set on the resource, in Integer format. |
| mbedAustin | 11:cada08fc8a70 | 107 | * @return M2MResource if created successfully, else NULL. |
| mbedAustin | 11:cada08fc8a70 | 108 | */ |
| mbedAustin | 11:cada08fc8a70 | 109 | M2MResource* create_resource(SecurityResource rescource, uint32_t value); |
| mbedAustin | 11:cada08fc8a70 | 110 | |
| mbedAustin | 11:cada08fc8a70 | 111 | /** |
| mbedAustin | 11:cada08fc8a70 | 112 | * @brief Deletes the resource with the given resource enum. |
| mbedAustin | 11:cada08fc8a70 | 113 | * Mandatory resources cannot be deleted. |
| mbedAustin | 11:cada08fc8a70 | 114 | * @param resource, Resource to be deleted. |
| mbedAustin | 11:cada08fc8a70 | 115 | * @return True if deleted, else false. |
| mbedAustin | 11:cada08fc8a70 | 116 | */ |
| mbedAustin | 11:cada08fc8a70 | 117 | bool delete_resource(SecurityResource rescource); |
| mbedAustin | 11:cada08fc8a70 | 118 | |
| mbedAustin | 11:cada08fc8a70 | 119 | /** |
| mbedAustin | 11:cada08fc8a70 | 120 | * @brief Sets the value of the given resource enum. |
| mbedAustin | 11:cada08fc8a70 | 121 | * @param resource, With this function, a value can be set for the following resources: |
| mbedAustin | 11:cada08fc8a70 | 122 | * 'M2MServerUri', 'SMSBindingKey', 'SMSBindingSecretKey'. |
| mbedAustin | 11:cada08fc8a70 | 123 | * @param value, Value to be set on the resource, in String format. |
| mbedAustin | 11:cada08fc8a70 | 124 | * @return True if successfully set, else false. |
| mbedAustin | 11:cada08fc8a70 | 125 | */ |
| mbedAustin | 11:cada08fc8a70 | 126 | bool set_resource_value(SecurityResource resource, |
| mbedAustin | 11:cada08fc8a70 | 127 | const String &value); |
| mbedAustin | 11:cada08fc8a70 | 128 | |
| mbedAustin | 11:cada08fc8a70 | 129 | /** |
| mbedAustin | 11:cada08fc8a70 | 130 | * @brief Sets the value of the given resource enum. |
| mbedAustin | 11:cada08fc8a70 | 131 | * @param resource, With this function, a value can be set for the following resourecs: |
| mbedAustin | 11:cada08fc8a70 | 132 | * 'BootstrapServer', 'SecurityMode', 'SMSSecurityMode', |
| mbedAustin | 11:cada08fc8a70 | 133 | * 'M2MServerSMSNumber', 'ShortServerID', 'ClientHoldOffTime'. |
| mbedAustin | 11:cada08fc8a70 | 134 | * @param value, Value to be set on the resource, in Integer format. |
| mbedAustin | 11:cada08fc8a70 | 135 | * @return True if successfully set, else false. |
| mbedAustin | 11:cada08fc8a70 | 136 | */ |
| mbedAustin | 11:cada08fc8a70 | 137 | bool set_resource_value(SecurityResource resource, |
| mbedAustin | 11:cada08fc8a70 | 138 | uint32_t value); |
| mbedAustin | 11:cada08fc8a70 | 139 | |
| mbedAustin | 11:cada08fc8a70 | 140 | /** |
| mbedAustin | 11:cada08fc8a70 | 141 | * @brief Sets the value of the given resource enum. |
| mbedAustin | 11:cada08fc8a70 | 142 | * @param resource, With this function, a value can be set for the follwing resources: |
| mbedAustin | 11:cada08fc8a70 | 143 | * 'PublicKey', 'ServerPublicKey', 'Secretkey'. |
| mbedAustin | 11:cada08fc8a70 | 144 | * @param value, Value to be set on the resource, in uint8_t format. |
| mbedAustin | 11:cada08fc8a70 | 145 | * @param size, Size of the buffer value to be set on the resource. |
| mbedAustin | 11:cada08fc8a70 | 146 | * @return True if successfully set, else false. |
| mbedAustin | 11:cada08fc8a70 | 147 | */ |
| mbedAustin | 11:cada08fc8a70 | 148 | bool set_resource_value(SecurityResource resource, |
| mbedAustin | 11:cada08fc8a70 | 149 | const uint8_t *value, |
| mbedAustin | 11:cada08fc8a70 | 150 | const uint16_t length); |
| mbedAustin | 11:cada08fc8a70 | 151 | |
| mbedAustin | 11:cada08fc8a70 | 152 | /** |
| mbedAustin | 11:cada08fc8a70 | 153 | * @brief Returns the value of the given resource enum, in String. |
| mbedAustin | 11:cada08fc8a70 | 154 | * @param resource, With this function, the following resources can return a value: |
| mbedAustin | 11:cada08fc8a70 | 155 | * 'M2MServerUri','SMSBindingKey', 'SMSBindingSecretKey'. |
| mbedAustin | 11:cada08fc8a70 | 156 | * @return Value associated with that resource. If the resource is not valid an empty string is returned. |
| mbedAustin | 11:cada08fc8a70 | 157 | */ |
| mbedAustin | 11:cada08fc8a70 | 158 | String resource_value_string(SecurityResource resource) const; |
| mbedAustin | 11:cada08fc8a70 | 159 | |
| mbedAustin | 11:cada08fc8a70 | 160 | /** |
| mbedAustin | 11:cada08fc8a70 | 161 | * @brief Populates the data buffer and returns the size of the buffer. |
| mbedAustin | 11:cada08fc8a70 | 162 | * @param resource, With this fucntion, the following resources can return a value: |
| mbedAustin | 11:cada08fc8a70 | 163 | * 'PublicKey', 'ServerPublicKey', 'Secretkey'. |
| mbedAustin | 11:cada08fc8a70 | 164 | * @param [OUT] data, Data buffer that contains the value. |
| mbedAustin | 11:cada08fc8a70 | 165 | * @return Size of the populated buffer. |
| mbedAustin | 11:cada08fc8a70 | 166 | */ |
| mbedAustin | 11:cada08fc8a70 | 167 | uint32_t resource_value_buffer(SecurityResource resource, |
| mbedAustin | 11:cada08fc8a70 | 168 | uint8_t *&data) const; |
| mbedAustin | 11:cada08fc8a70 | 169 | |
| mbedAustin | 11:cada08fc8a70 | 170 | /** |
| mbedAustin | 11:cada08fc8a70 | 171 | * @brief Returns the value of the given resource name, in Integer. |
| mbedAustin | 11:cada08fc8a70 | 172 | * @param resource, With this function, the following resources can return a value: |
| mbedAustin | 11:cada08fc8a70 | 173 | * 'BootstrapServer', 'SecurityMode', 'SMSSecurityMode', |
| mbedAustin | 11:cada08fc8a70 | 174 | * 'M2MServerSMSNumber', 'ShortServerID', 'ClientHoldOffTime'. |
| mbedAustin | 11:cada08fc8a70 | 175 | * @return Value associated with the resource. If the resource is not valid 0 is returned. |
| mbedAustin | 11:cada08fc8a70 | 176 | */ |
| mbedAustin | 11:cada08fc8a70 | 177 | uint32_t resource_value_int(SecurityResource resource) const; |
| mbedAustin | 11:cada08fc8a70 | 178 | |
| mbedAustin | 11:cada08fc8a70 | 179 | |
| mbedAustin | 11:cada08fc8a70 | 180 | /** |
| mbedAustin | 11:cada08fc8a70 | 181 | * @brief Returns whether the resource instance with given resource enum exists or not |
| mbedAustin | 11:cada08fc8a70 | 182 | * @param resource, Resource enum. |
| mbedAustin | 11:cada08fc8a70 | 183 | * @return True if at least one instance exists, else false. |
| mbedAustin | 11:cada08fc8a70 | 184 | */ |
| mbedAustin | 11:cada08fc8a70 | 185 | bool is_resource_present(SecurityResource resource)const; |
| mbedAustin | 11:cada08fc8a70 | 186 | |
| mbedAustin | 11:cada08fc8a70 | 187 | /** |
| mbedAustin | 11:cada08fc8a70 | 188 | * @brief Returns the total number of resources for a security object. |
| mbedAustin | 11:cada08fc8a70 | 189 | * @return Total number of resources. |
| mbedAustin | 11:cada08fc8a70 | 190 | */ |
| mbedAustin | 11:cada08fc8a70 | 191 | uint16_t total_resource_count()const; |
| mbedAustin | 11:cada08fc8a70 | 192 | |
| mbedAustin | 11:cada08fc8a70 | 193 | /** |
| mbedAustin | 11:cada08fc8a70 | 194 | * @brief Returns the type of the Security Object. It can be either |
| mbedAustin | 11:cada08fc8a70 | 195 | * Bootstrap or M2MServer. |
| mbedAustin | 11:cada08fc8a70 | 196 | * @return ServerType, Type of the Security Object. |
| mbedAustin | 11:cada08fc8a70 | 197 | */ |
| mbedAustin | 11:cada08fc8a70 | 198 | ServerType server_type() const; |
| mbedAustin | 11:cada08fc8a70 | 199 | |
| mbedAustin | 11:cada08fc8a70 | 200 | private: |
| mbedAustin | 11:cada08fc8a70 | 201 | |
| mbedAustin | 11:cada08fc8a70 | 202 | M2MResource* get_resource(SecurityResource resource) const; |
| mbedAustin | 11:cada08fc8a70 | 203 | |
| mbedAustin | 11:cada08fc8a70 | 204 | private: |
| mbedAustin | 11:cada08fc8a70 | 205 | |
| mbedAustin | 11:cada08fc8a70 | 206 | ServerType _server_type; |
| mbedAustin | 11:cada08fc8a70 | 207 | M2MObjectInstance* _server_instance; |
| mbedAustin | 11:cada08fc8a70 | 208 | |
| mbedAustin | 11:cada08fc8a70 | 209 | friend class Test_M2MSecurity; |
| mbedAustin | 11:cada08fc8a70 | 210 | friend class Test_M2MInterfaceImpl; |
| mbedAustin | 11:cada08fc8a70 | 211 | friend class Test_M2MConnectionSecurityImpl; |
| mbedAustin | 11:cada08fc8a70 | 212 | friend class Test_M2MConnectionHandlerPimpl_linux; |
| mbedAustin | 11:cada08fc8a70 | 213 | friend class Test_M2MConnectionHandlerPimpl_mbed; |
| mbedAustin | 11:cada08fc8a70 | 214 | friend class Test_M2MConnectionSecurityPimpl; |
| mbedAustin | 11:cada08fc8a70 | 215 | }; |
| mbedAustin | 11:cada08fc8a70 | 216 | |
| mbedAustin | 11:cada08fc8a70 | 217 | #endif // M2M_SECURITY_H |
| mbedAustin | 11:cada08fc8a70 | 218 | |
| mbedAustin | 11:cada08fc8a70 | 219 |
Austin Blackstone
