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