Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
m2msecurity.cpp
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 #include <cstdio> 00017 #include "mbed-client/m2msecurity.h" 00018 #include "mbed-client/m2mconstants.h" 00019 #include "mbed-client/m2mobject.h" 00020 #include "mbed-client/m2mobjectinstance.h" 00021 #include "mbed-client/m2mresource.h" 00022 #include "mbed-client/m2mstring.h" 00023 #include "mbed-trace/mbed_trace.h" 00024 #define TRACE_GROUP "mClt" 00025 00026 #define BUFFER_SIZE 21 00027 00028 M2MSecurity::M2MSecurity(ServerType ser_type) 00029 : M2MObject(M2M_SECURITY_ID), 00030 _server_instance(NULL), 00031 _server_type(ser_type) 00032 { 00033 _server_instance = M2MObject::create_object_instance(); 00034 00035 if(_server_instance) { 00036 M2MResource* res = _server_instance->create_dynamic_resource(SECURITY_M2M_SERVER_URI, 00037 OMA_RESOURCE_TYPE, 00038 M2MResourceInstance::STRING, 00039 false); 00040 if(res) { 00041 res->set_operation(M2MBase::NOT_ALLOWED); 00042 } 00043 res = _server_instance->create_dynamic_resource(SECURITY_BOOTSTRAP_SERVER, 00044 OMA_RESOURCE_TYPE, 00045 M2MResourceInstance::BOOLEAN, 00046 false); 00047 if(res) { 00048 res->set_operation(M2MBase::NOT_ALLOWED); 00049 } 00050 res = _server_instance->create_dynamic_resource(SECURITY_SECURITY_MODE, 00051 OMA_RESOURCE_TYPE, 00052 M2MResourceInstance::INTEGER, 00053 false); 00054 if(res) { 00055 res->set_operation(M2MBase::NOT_ALLOWED); 00056 } 00057 res = _server_instance->create_dynamic_resource(SECURITY_PUBLIC_KEY, 00058 OMA_RESOURCE_TYPE, 00059 M2MResourceInstance::OPAQUE, 00060 false); 00061 if(res) { 00062 res->set_operation(M2MBase::NOT_ALLOWED); 00063 } 00064 res = _server_instance->create_dynamic_resource(SECURITY_SERVER_PUBLIC_KEY, 00065 OMA_RESOURCE_TYPE, 00066 M2MResourceInstance::OPAQUE, 00067 false); 00068 if(res) { 00069 res->set_operation(M2MBase::NOT_ALLOWED); 00070 } 00071 res = _server_instance->create_dynamic_resource(SECURITY_SECRET_KEY, 00072 OMA_RESOURCE_TYPE, 00073 M2MResourceInstance::OPAQUE, 00074 false); 00075 if(res) { 00076 res->set_operation(M2MBase::NOT_ALLOWED); 00077 } 00078 if(M2MSecurity::M2MServer == ser_type) { 00079 res = _server_instance->create_dynamic_resource(SECURITY_SHORT_SERVER_ID, 00080 OMA_RESOURCE_TYPE, 00081 M2MResourceInstance::INTEGER, 00082 false); 00083 if(res) { 00084 res->set_operation(M2MBase::NOT_ALLOWED); 00085 } 00086 } 00087 } 00088 } 00089 00090 M2MSecurity::~M2MSecurity() 00091 { 00092 } 00093 00094 M2MResource* M2MSecurity::create_resource(SecurityResource resource, uint32_t value) 00095 { 00096 M2MResource* res = NULL; 00097 const char* security_id_ptr = ""; 00098 if(!is_resource_present(resource)) { 00099 switch(resource) { 00100 case SMSSecurityMode: 00101 security_id_ptr = SECURITY_SMS_SECURITY_MODE; 00102 break; 00103 case M2MServerSMSNumber: 00104 security_id_ptr = SECURITY_M2M_SERVER_SMS_NUMBER; 00105 break; 00106 case ShortServerID: 00107 security_id_ptr = SECURITY_SHORT_SERVER_ID; 00108 break; 00109 case ClientHoldOffTime: 00110 security_id_ptr = SECURITY_CLIENT_HOLD_OFF_TIME; 00111 break; 00112 default: 00113 break; 00114 } 00115 } 00116 00117 const String security_id(security_id_ptr); 00118 00119 if(!security_id.empty()) { 00120 if(_server_instance) { 00121 res = _server_instance->create_dynamic_resource(security_id,OMA_RESOURCE_TYPE, 00122 M2MResourceInstance::INTEGER, 00123 false); 00124 00125 if(res) { 00126 res->set_operation(M2MBase::NOT_ALLOWED); 00127 res->set_value(value); 00128 } 00129 } 00130 } 00131 return res; 00132 } 00133 00134 bool M2MSecurity::delete_resource(SecurityResource resource) 00135 { 00136 bool success = false; 00137 const char* security_id_ptr = ""; 00138 switch(resource) { 00139 case SMSSecurityMode: 00140 security_id_ptr = SECURITY_SMS_SECURITY_MODE; 00141 break; 00142 case M2MServerSMSNumber: 00143 security_id_ptr = SECURITY_M2M_SERVER_SMS_NUMBER; 00144 break; 00145 case ShortServerID: 00146 if(M2MSecurity::Bootstrap == _server_type) { 00147 security_id_ptr = SECURITY_SHORT_SERVER_ID; 00148 } 00149 break; 00150 case ClientHoldOffTime: 00151 security_id_ptr = SECURITY_CLIENT_HOLD_OFF_TIME; 00152 break; 00153 default: 00154 // Others are mandatory resources hence cannot be deleted. 00155 break; 00156 } 00157 00158 const String security_id(security_id_ptr); 00159 00160 if(!security_id.empty()) { 00161 if(_server_instance) { 00162 success = _server_instance->remove_resource(security_id); 00163 } 00164 } 00165 return success; 00166 } 00167 00168 bool M2MSecurity::set_resource_value(SecurityResource resource, 00169 const String &value) 00170 { 00171 bool success = false; 00172 if(M2MSecurity::M2MServerUri == resource) { 00173 M2MResource* res = get_resource(resource); 00174 if(res) { 00175 success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length()); 00176 } 00177 } 00178 return success; 00179 } 00180 00181 bool M2MSecurity::set_resource_value(SecurityResource resource, 00182 uint32_t value) 00183 { 00184 bool success = false; 00185 M2MResource* res = get_resource(resource); 00186 if(res) { 00187 if(M2MSecurity::SecurityMode == resource || 00188 M2MSecurity::SMSSecurityMode == resource || 00189 M2MSecurity::M2MServerSMSNumber == resource || 00190 M2MSecurity::ShortServerID == resource || 00191 M2MSecurity::ClientHoldOffTime == resource) { 00192 // If it is any of the above resource 00193 // set the value of the resource. 00194 uint8_t size = 0; 00195 uint8_t *buffer = String::convert_integer_to_array(value, size); 00196 success = res->set_value(buffer,size); 00197 free(buffer); 00198 } 00199 } 00200 return success; 00201 } 00202 00203 bool M2MSecurity::set_resource_value(SecurityResource resource, 00204 const uint8_t *value, 00205 const uint16_t length) 00206 { 00207 bool success = false; 00208 M2MResource* res = get_resource(resource); 00209 if(res) { 00210 if(M2MSecurity::PublicKey == resource || 00211 M2MSecurity::ServerPublicKey == resource || 00212 M2MSecurity::Secretkey == resource) { 00213 success = res->set_value(value,length); 00214 } 00215 } 00216 return success; 00217 } 00218 00219 String M2MSecurity::resource_value_string(SecurityResource resource) const 00220 { 00221 String value = ""; 00222 M2MResource* res = get_resource(resource); 00223 if(res) { 00224 if(M2MSecurity::M2MServerUri == resource) { 00225 value = res->get_value_string(); 00226 } 00227 } 00228 return value; 00229 } 00230 00231 uint32_t M2MSecurity::resource_value_buffer(SecurityResource resource, 00232 uint8_t *&data) const 00233 { 00234 uint32_t size = 0; 00235 M2MResource* res = get_resource(resource); 00236 if(res) { 00237 if(M2MSecurity::PublicKey == resource || 00238 M2MSecurity::ServerPublicKey == resource || 00239 M2MSecurity::Secretkey == resource) { 00240 res->get_value(data,size); 00241 } 00242 } 00243 return size; 00244 } 00245 00246 uint32_t M2MSecurity::resource_value_buffer(SecurityResource resource, 00247 const uint8_t *&data) const 00248 { 00249 uint32_t size = 0; 00250 M2MResource* res = get_resource(resource); 00251 if(res) { 00252 if(M2MSecurity::PublicKey == resource || 00253 M2MSecurity::ServerPublicKey == resource || 00254 M2MSecurity::Secretkey == resource) { 00255 data = res->value(); 00256 size = res->value_length(); 00257 } 00258 } 00259 return size; 00260 } 00261 00262 00263 uint32_t M2MSecurity::resource_value_int(SecurityResource resource) const 00264 { 00265 uint32_t value = 0; 00266 M2MResource* res = get_resource(resource); 00267 if(res) { 00268 if(M2MSecurity::SecurityMode == resource || 00269 M2MSecurity::SMSSecurityMode == resource || 00270 M2MSecurity::M2MServerSMSNumber == resource || 00271 M2MSecurity::ShortServerID == resource || 00272 M2MSecurity::ClientHoldOffTime == resource) { 00273 // Get the value and convert it into integer. This is not the most 00274 // efficient way, as it takes pointless heap copy to get the zero termination. 00275 uint8_t* buffer = NULL; 00276 uint32_t length = 0; 00277 res->get_value(buffer,length); 00278 if(buffer) { 00279 value = String::convert_array_to_integer(buffer,length); 00280 free(buffer); 00281 } 00282 } 00283 } 00284 return value; 00285 } 00286 00287 bool M2MSecurity::is_resource_present(SecurityResource resource) const 00288 { 00289 bool success = false; 00290 M2MResource *res = get_resource(resource); 00291 if(res) { 00292 success = true; 00293 } 00294 return success; 00295 } 00296 00297 uint16_t M2MSecurity::total_resource_count() const 00298 { 00299 uint16_t count = 0; 00300 if(_server_instance) { 00301 count = _server_instance->resources().size(); 00302 } 00303 return count; 00304 } 00305 00306 M2MSecurity::ServerType M2MSecurity::server_type() const 00307 { 00308 return _server_type; 00309 } 00310 00311 M2MResource* M2MSecurity::get_resource(SecurityResource res) const 00312 { 00313 M2MResource* res_object = NULL; 00314 if(_server_instance) { 00315 const char* res_name_ptr = ""; 00316 switch(res) { 00317 case M2MServerUri: 00318 res_name_ptr = SECURITY_M2M_SERVER_URI; 00319 break; 00320 case BootstrapServer: 00321 res_name_ptr = SECURITY_BOOTSTRAP_SERVER; 00322 break; 00323 case SecurityMode: 00324 res_name_ptr = SECURITY_SECURITY_MODE; 00325 break; 00326 case PublicKey: 00327 res_name_ptr = SECURITY_PUBLIC_KEY; 00328 break; 00329 case ServerPublicKey: 00330 res_name_ptr = SECURITY_SERVER_PUBLIC_KEY; 00331 break; 00332 case Secretkey: 00333 res_name_ptr = SECURITY_SECRET_KEY; 00334 break; 00335 case SMSSecurityMode: 00336 res_name_ptr = SECURITY_SMS_SECURITY_MODE; 00337 break; 00338 case SMSBindingKey: 00339 res_name_ptr = SECURITY_SMS_BINDING_KEY; 00340 break; 00341 case SMSBindingSecretKey: 00342 res_name_ptr = SECURITY_SMS_BINDING_SECRET_KEY; 00343 break; 00344 case M2MServerSMSNumber: 00345 res_name_ptr = SECURITY_M2M_SERVER_SMS_NUMBER; 00346 break; 00347 case ShortServerID: 00348 res_name_ptr = SECURITY_SHORT_SERVER_ID; 00349 break; 00350 case ClientHoldOffTime: 00351 res_name_ptr = SECURITY_CLIENT_HOLD_OFF_TIME; 00352 break; 00353 } 00354 const String res_name(res_name_ptr); 00355 00356 res_object = _server_instance->resource(res_name); 00357 } 00358 return res_object; 00359 } 00360 00361 void M2MSecurity::clear_resources() 00362 { 00363 for(int i = 0; i <= M2MSecurity::ClientHoldOffTime; i++) { 00364 M2MResource *res = get_resource((SecurityResource) i); 00365 if (res) { 00366 res->clear_value(); 00367 } 00368 } 00369 }
Generated on Tue Jul 12 2022 12:28:39 by
1.7.2
