Simulated product dispenser
Fork of mbed-cloud-workshop-connect-HTS221 by
m2msecurity.h
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef M2M_SECURITY_H 00017 #define M2M_SECURITY_H 00018 00019 #include "mbed-client/m2mobject.h" 00020 00021 // FORWARD DECLARATION 00022 class M2MResource; 00023 00024 /*! \file m2msecurity.h 00025 * \brief M2MSecurity. 00026 * This class represents an interface for the Security Object model of the LWM2M framework. 00027 * It handles the security object instances and all corresponding 00028 * resources. 00029 */ 00030 00031 class M2MSecurity : public M2MObject { 00032 00033 friend class M2MInterfaceFactory; 00034 friend class M2MNsdlInterface; 00035 00036 public: 00037 00038 /** 00039 * \brief An enum defining all resources associated with a 00040 * Security Object in the LWM2M framework. 00041 */ 00042 typedef enum { 00043 M2MServerUri, 00044 BootstrapServer, 00045 SecurityMode, 00046 PublicKey, 00047 ServerPublicKey, 00048 Secretkey, 00049 SMSSecurityMode, 00050 SMSBindingKey, 00051 SMSBindingSecretKey, 00052 M2MServerSMSNumber, 00053 ShortServerID, 00054 ClientHoldOffTime, 00055 OpenCertificateChain, 00056 CloseCertificateChain, 00057 ReadDeviceCertificateChain 00058 } SecurityResource; 00059 00060 /** 00061 * \brief An enum defining the type of the security attribute 00062 * used by the Security Object. 00063 */ 00064 typedef enum { 00065 SecurityNotSet = -1, 00066 Psk = 0, 00067 Certificate = 2, 00068 NoSecurity = 3, 00069 EST = 4 00070 } SecurityModeType; 00071 00072 /** 00073 * \brief An enum defining an interface operation that can be 00074 * handled by the Security Object. 00075 */ 00076 typedef enum { 00077 M2MServer = 0x0, 00078 Bootstrap = 0x1 00079 } ServerType; 00080 00081 private: 00082 00083 /** 00084 * \brief Constructor 00085 * \param server_type The type of the security object created. Either bootstrap or LWM2M server. 00086 */ 00087 M2MSecurity(ServerType server_type); 00088 00089 00090 /** 00091 * \brief Destructor 00092 */ 00093 virtual ~M2MSecurity(); 00094 00095 // Prevents the use of default constructor. 00096 M2MSecurity(); 00097 00098 // Prevents the use of assignment operator. 00099 M2MSecurity& operator=( const M2MSecurity& /*other*/ ); 00100 00101 // Prevents the use of copy constructor 00102 M2MSecurity( const M2MSecurity& /*other*/ ); 00103 00104 public: 00105 00106 /** 00107 * \brief Get the singleton instance of M2MSecurity 00108 */ 00109 static M2MSecurity* get_instance(); 00110 00111 /** 00112 * \brief Delete the singleton instance of M2MSecurity 00113 */ 00114 static void delete_instance(); 00115 00116 /** 00117 * \brief Creates a new object instance. 00118 * \param server_type Server type for new object instance. 00119 * \return M2MObjectInstance if created successfully, else NULL. 00120 */ 00121 M2MObjectInstance* create_object_instance(ServerType server_type); 00122 00123 /** 00124 * \brief Remove all security object instances. 00125 */ 00126 void remove_security_instances(); 00127 00128 /** 00129 * \brief Creates a new resource for a given resource enum. 00130 * \param rescource With this function, the following resources can be created: 00131 * ' BootstrapServer', 'SecurityMode', 'SMSSecurityMode', 00132 * 'M2MServerSMSNumber', 'ShortServerID', 'ClientHoldOffTime'. 00133 * \param value The value to be set on the resource, in integer format. 00134 * \param instance_id Instance id of the security instance where resource should be created. 00135 * \return M2MResource if created successfully, else NULL. 00136 */ 00137 M2MResource* create_resource(SecurityResource rescource, uint32_t value, uint16_t instance_id); 00138 00139 /** 00140 * \brief Deletes a resource with a given resource enum. 00141 * Mandatory resources cannot be deleted. 00142 * \param resource The resource to be deleted. 00143 * \param instance_id Instance id of the security instance where resource should be deleted. 00144 * \return True if deleted, else false. 00145 */ 00146 bool delete_resource(SecurityResource rescource, uint16_t instance_id); 00147 00148 /** 00149 * \brief Sets the value of a given resource enum. 00150 * \param resource With this function, a value can be set for the following resources: 00151 * 'M2MServerUri', 'SMSBindingKey', 'SMSBindingSecretKey'. 00152 * \param value The value to be set on the resource, in string format. 00153 * \param instance_id Instance id of the security instance where resource value should be set. 00154 * \return True if successfully set, else false. 00155 */ 00156 bool set_resource_value(SecurityResource resource, 00157 const String &value, 00158 uint16_t instance_id); 00159 00160 /** 00161 * \brief Sets the value of a given resource enum. 00162 * \param resource With this function, a value can be set for the following resourecs: 00163 * 'BootstrapServer', 'SecurityMode', 'SMSSecurityMode', 00164 * 'M2MServerSMSNumber', 'ShortServerID', 'ClientHoldOffTime'. 00165 * \param value The value to be set on the resource, in integer format. 00166 * \param instance_id Instance id of the security instance where resource value should be set. 00167 * \return True if successfully set, else false. 00168 */ 00169 bool set_resource_value(SecurityResource resource, 00170 uint32_t value, 00171 uint16_t instance_id); 00172 00173 /** 00174 * \brief Sets the value of a given resource enum. 00175 * \param resource With this function, a value can be set for the follwing resources: 00176 * 'PublicKey', 'ServerPublicKey', 'Secretkey'. 00177 * \param value The value to be set on the resource, in uint8_t format. 00178 * \param length The size of the buffer value to be set on the resource. 00179 * \param instance_id Instance id of the security instance where resource value should be set. 00180 * \return True if successfully set, else false. 00181 */ 00182 bool set_resource_value(SecurityResource resource, 00183 const uint8_t *value, 00184 const uint16_t length, 00185 uint16_t instance_id); 00186 00187 /** 00188 * \brief Returns the value of a given resource enum, in string format. 00189 * \param resource With this function, the following resources can return a value: 00190 * 'M2MServerUri','SMSBindingKey', 'SMSBindingSecretKey'. 00191 * \param instance_id Instance id of the security instance where resource value should be retrieved. 00192 * \return The value associated with the resource. If the resource is not valid an empty string is returned. 00193 */ 00194 String resource_value_string(SecurityResource resource, uint16_t instance_id) const; 00195 00196 /** 00197 * \brief Populates the data buffer and returns the size of the buffer. 00198 * \param resource With this function, the following resources can return a value: 00199 * 'PublicKey', 'ServerPublicKey', 'Secretkey', 00200 * 'OpenCertificateChain', 'CloseCertificateChain' 'ReadDeviceCertificateChain'. 00201 * \param [OUT]data A copy of the data buffer that contains the value. The caller 00202 * is responsible for freeing this buffer. 00203 * \param instance_id Instance id of the security instance where resource value should be retrieve. 00204 * \param buffer_len[IN/OUT] Length of the buffer. 00205 * \return Error code, 0 on success otherwise < 0 00206 */ 00207 int resource_value_buffer(SecurityResource resource, 00208 uint8_t *&data, 00209 uint16_t instance_id, 00210 size_t *buffer_len) const; 00211 00212 /** 00213 * \brief Returns a pointer to the value and size of the buffer. 00214 * \param resource With this function, the following resources can return a value: 00215 * 'PublicKey', 'ServerPublicKey', 'Secretkey'. 00216 * \param [OUT]data A pointer to the data buffer that contains the value. 00217 * \param instance_id Instance id of the security instance where resource value should be retrieved. 00218 * \return The size of the populated buffer. 00219 */ 00220 uint32_t resource_value_buffer(SecurityResource resource, 00221 const uint8_t *&data, 00222 uint16_t instance_id) const; 00223 00224 /** 00225 * \brief Returns the value of a given resource name, in integer format. 00226 * \param resource With this function, the following resources can return a value: 00227 * 'BootstrapServer', 'SecurityMode', 'SMSSecurityMode', 00228 * 'M2MServerSMSNumber', 'ShortServerID', 'ClientHoldOffTime'. 00229 * \param instance_id Instance id of the security instance where resource should be created. 00230 * \return The value associated with the resource. If the resource is not valid 0 is returned. 00231 */ 00232 uint32_t resource_value_int(SecurityResource resource, 00233 uint16_t instance_id) const; 00234 00235 /** 00236 * \brief Returns whether a resource instance with a given resource enum exists or not 00237 * \param resource Resource enum. 00238 * \param instance_id Instance id of the security instance where resource should be checked. 00239 * \return True if at least one instance exists, else false. 00240 */ 00241 bool is_resource_present(SecurityResource resource, 00242 uint16_t instance_id) const; 00243 00244 /** 00245 * \brief Returns the total number of resources for a security object. 00246 * \param instance_id Instance id of the security instance where resources should be counted. 00247 * \return The total number of resources. 00248 */ 00249 uint16_t total_resource_count(uint16_t instance_id) const; 00250 00251 /** 00252 * \brief Returns the type of the Security Object. It can be either 00253 * Bootstrap or M2MServer. 00254 * \param instance_id Instance id of the security instance where resource should be created. 00255 * \return ServerType The type of the Security Object. 00256 */ 00257 ServerType server_type(uint16_t instance_id) const; 00258 00259 /** 00260 * \brief Returns first bootstrap or lwm2m server security object instance id. 00261 * \param server_type Which server type security instance to return. 00262 * \return Object instance id, or -1 if no such instance exists. 00263 */ 00264 int32_t get_security_instance_id(ServerType server_type) const; 00265 00266 M2MResource* get_resource(SecurityResource resource, uint16_t instance_id = 0) const; 00267 private: 00268 00269 00270 void clear_resources(uint16_t instance_id = 0); 00271 00272 protected: 00273 static M2MSecurity* _instance; 00274 00275 friend class Test_M2MSecurity; 00276 friend class Test_M2MInterfaceImpl; 00277 friend class Test_M2MConnectionSecurityImpl; 00278 friend class Test_M2MConnectionHandlerPimpl_linux; 00279 friend class Test_M2MConnectionHandlerPimpl_mbed; 00280 friend class Test_M2MConnectionSecurityPimpl; 00281 friend class Test_M2MNsdlInterface; 00282 friend class Test_M2MConnectionHandlerPimpl_classic; 00283 }; 00284 00285 #endif // M2M_SECURITY_H 00286 00287
Generated on Tue Jul 12 2022 19:12:13 by 1.7.2