mbed client on ethernet with LWIP
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbed-client/source/m2msecurity.cpp@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 | #include <cstdio> |
mbedAustin | 11:cada08fc8a70 | 17 | #include "mbed-client/m2msecurity.h" |
mbedAustin | 11:cada08fc8a70 | 18 | #include "mbed-client/m2mconstants.h" |
mbedAustin | 11:cada08fc8a70 | 19 | #include "mbed-client/m2mobject.h" |
mbedAustin | 11:cada08fc8a70 | 20 | #include "mbed-client/m2mobjectinstance.h" |
mbedAustin | 11:cada08fc8a70 | 21 | #include "mbed-client/m2mresource.h" |
mbedAustin | 11:cada08fc8a70 | 22 | |
mbedAustin | 11:cada08fc8a70 | 23 | |
mbedAustin | 11:cada08fc8a70 | 24 | M2MSecurity::M2MSecurity(ServerType ser_type) |
mbedAustin | 11:cada08fc8a70 | 25 | : M2MObject(M2M_SECURITY_ID), |
mbedAustin | 11:cada08fc8a70 | 26 | _server_type(ser_type), |
mbedAustin | 11:cada08fc8a70 | 27 | _server_instance(NULL) |
mbedAustin | 11:cada08fc8a70 | 28 | { |
mbedAustin | 11:cada08fc8a70 | 29 | _server_instance = M2MObject::create_object_instance(); |
mbedAustin | 11:cada08fc8a70 | 30 | |
mbedAustin | 11:cada08fc8a70 | 31 | if(_server_instance) { |
mbedAustin | 11:cada08fc8a70 | 32 | |
mbedAustin | 11:cada08fc8a70 | 33 | M2MResource* res = _server_instance->create_dynamic_resource(SECURITY_M2M_SERVER_URI, |
mbedAustin | 11:cada08fc8a70 | 34 | OMA_RESOURCE_TYPE, |
mbedAustin | 11:cada08fc8a70 | 35 | M2MResourceInstance::STRING, |
mbedAustin | 11:cada08fc8a70 | 36 | false); |
mbedAustin | 11:cada08fc8a70 | 37 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 38 | res->set_operation(M2MBase::NOT_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 39 | } |
mbedAustin | 11:cada08fc8a70 | 40 | res = _server_instance->create_dynamic_resource(SECURITY_BOOTSTRAP_SERVER, |
mbedAustin | 11:cada08fc8a70 | 41 | OMA_RESOURCE_TYPE, |
mbedAustin | 11:cada08fc8a70 | 42 | M2MResourceInstance::BOOLEAN, |
mbedAustin | 11:cada08fc8a70 | 43 | false); |
mbedAustin | 11:cada08fc8a70 | 44 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 45 | res->set_operation(M2MBase::NOT_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 46 | } |
mbedAustin | 11:cada08fc8a70 | 47 | res = _server_instance->create_dynamic_resource(SECURITY_SECURITY_MODE, |
mbedAustin | 11:cada08fc8a70 | 48 | OMA_RESOURCE_TYPE, |
mbedAustin | 11:cada08fc8a70 | 49 | M2MResourceInstance::INTEGER, |
mbedAustin | 11:cada08fc8a70 | 50 | false); |
mbedAustin | 11:cada08fc8a70 | 51 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 52 | res->set_operation(M2MBase::NOT_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 53 | } |
mbedAustin | 11:cada08fc8a70 | 54 | res = _server_instance->create_dynamic_resource(SECURITY_PUBLIC_KEY, |
mbedAustin | 11:cada08fc8a70 | 55 | OMA_RESOURCE_TYPE, |
mbedAustin | 11:cada08fc8a70 | 56 | M2MResourceInstance::OPAQUE, |
mbedAustin | 11:cada08fc8a70 | 57 | false); |
mbedAustin | 11:cada08fc8a70 | 58 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 59 | res->set_operation(M2MBase::NOT_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 60 | } |
mbedAustin | 11:cada08fc8a70 | 61 | res = _server_instance->create_dynamic_resource(SECURITY_SERVER_PUBLIC_KEY, |
mbedAustin | 11:cada08fc8a70 | 62 | OMA_RESOURCE_TYPE, |
mbedAustin | 11:cada08fc8a70 | 63 | M2MResourceInstance::OPAQUE, |
mbedAustin | 11:cada08fc8a70 | 64 | false); |
mbedAustin | 11:cada08fc8a70 | 65 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 66 | res->set_operation(M2MBase::NOT_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 67 | } |
mbedAustin | 11:cada08fc8a70 | 68 | res = _server_instance->create_dynamic_resource(SECURITY_SECRET_KEY, |
mbedAustin | 11:cada08fc8a70 | 69 | OMA_RESOURCE_TYPE, |
mbedAustin | 11:cada08fc8a70 | 70 | M2MResourceInstance::OPAQUE, |
mbedAustin | 11:cada08fc8a70 | 71 | false); |
mbedAustin | 11:cada08fc8a70 | 72 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 73 | res->set_operation(M2MBase::NOT_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 74 | } |
mbedAustin | 11:cada08fc8a70 | 75 | if(M2MSecurity::M2MServer == ser_type) { |
mbedAustin | 11:cada08fc8a70 | 76 | res = _server_instance->create_dynamic_resource(SECURITY_SHORT_SERVER_ID, |
mbedAustin | 11:cada08fc8a70 | 77 | OMA_RESOURCE_TYPE, |
mbedAustin | 11:cada08fc8a70 | 78 | M2MResourceInstance::INTEGER, |
mbedAustin | 11:cada08fc8a70 | 79 | false); |
mbedAustin | 11:cada08fc8a70 | 80 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 81 | res->set_operation(M2MBase::NOT_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 82 | } |
mbedAustin | 11:cada08fc8a70 | 83 | } |
mbedAustin | 11:cada08fc8a70 | 84 | } |
mbedAustin | 11:cada08fc8a70 | 85 | } |
mbedAustin | 11:cada08fc8a70 | 86 | |
mbedAustin | 11:cada08fc8a70 | 87 | M2MSecurity::~M2MSecurity() |
mbedAustin | 11:cada08fc8a70 | 88 | { |
mbedAustin | 11:cada08fc8a70 | 89 | _server_instance = NULL; |
mbedAustin | 11:cada08fc8a70 | 90 | } |
mbedAustin | 11:cada08fc8a70 | 91 | |
mbedAustin | 11:cada08fc8a70 | 92 | M2MResource* M2MSecurity::create_resource(SecurityResource resource, uint32_t value) |
mbedAustin | 11:cada08fc8a70 | 93 | { |
mbedAustin | 11:cada08fc8a70 | 94 | M2MResource* res = NULL; |
mbedAustin | 11:cada08fc8a70 | 95 | String security_id = ""; |
mbedAustin | 11:cada08fc8a70 | 96 | if(!is_resource_present(resource)) { |
mbedAustin | 11:cada08fc8a70 | 97 | switch(resource) { |
mbedAustin | 11:cada08fc8a70 | 98 | case SMSSecurityMode: |
mbedAustin | 11:cada08fc8a70 | 99 | security_id = SECURITY_SMS_SECURITY_MODE; |
mbedAustin | 11:cada08fc8a70 | 100 | break; |
mbedAustin | 11:cada08fc8a70 | 101 | case M2MServerSMSNumber: |
mbedAustin | 11:cada08fc8a70 | 102 | security_id = SECURITY_M2M_SERVER_SMS_NUMBER; |
mbedAustin | 11:cada08fc8a70 | 103 | break; |
mbedAustin | 11:cada08fc8a70 | 104 | case ShortServerID: |
mbedAustin | 11:cada08fc8a70 | 105 | security_id = SECURITY_SHORT_SERVER_ID; |
mbedAustin | 11:cada08fc8a70 | 106 | break; |
mbedAustin | 11:cada08fc8a70 | 107 | case ClientHoldOffTime: |
mbedAustin | 11:cada08fc8a70 | 108 | security_id = SECURITY_CLIENT_HOLD_OFF_TIME; |
mbedAustin | 11:cada08fc8a70 | 109 | break; |
mbedAustin | 11:cada08fc8a70 | 110 | default: |
mbedAustin | 11:cada08fc8a70 | 111 | break; |
mbedAustin | 11:cada08fc8a70 | 112 | } |
mbedAustin | 11:cada08fc8a70 | 113 | } |
mbedAustin | 11:cada08fc8a70 | 114 | if(!security_id.empty()) { |
mbedAustin | 11:cada08fc8a70 | 115 | if(_server_instance) { |
mbedAustin | 11:cada08fc8a70 | 116 | |
mbedAustin | 11:cada08fc8a70 | 117 | res = _server_instance->create_dynamic_resource(security_id,OMA_RESOURCE_TYPE, |
mbedAustin | 11:cada08fc8a70 | 118 | M2MResourceInstance::INTEGER, |
mbedAustin | 11:cada08fc8a70 | 119 | false); |
mbedAustin | 11:cada08fc8a70 | 120 | |
mbedAustin | 11:cada08fc8a70 | 121 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 122 | char *buffer = (char*)malloc(20); |
mbedAustin | 11:cada08fc8a70 | 123 | if(buffer) { |
mbedAustin | 11:cada08fc8a70 | 124 | int size = snprintf(buffer, 20,"%ld",(long int)value); |
mbedAustin | 11:cada08fc8a70 | 125 | res->set_operation(M2MBase::NOT_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 126 | res->set_value((const uint8_t*)buffer, |
mbedAustin | 11:cada08fc8a70 | 127 | (uint32_t)size); |
mbedAustin | 11:cada08fc8a70 | 128 | free(buffer); |
mbedAustin | 11:cada08fc8a70 | 129 | } |
mbedAustin | 11:cada08fc8a70 | 130 | } |
mbedAustin | 11:cada08fc8a70 | 131 | } |
mbedAustin | 11:cada08fc8a70 | 132 | } |
mbedAustin | 11:cada08fc8a70 | 133 | return res; |
mbedAustin | 11:cada08fc8a70 | 134 | } |
mbedAustin | 11:cada08fc8a70 | 135 | |
mbedAustin | 11:cada08fc8a70 | 136 | bool M2MSecurity::delete_resource(SecurityResource resource) |
mbedAustin | 11:cada08fc8a70 | 137 | { |
mbedAustin | 11:cada08fc8a70 | 138 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 139 | String security_id = ""; |
mbedAustin | 11:cada08fc8a70 | 140 | switch(resource) { |
mbedAustin | 11:cada08fc8a70 | 141 | case SMSSecurityMode: |
mbedAustin | 11:cada08fc8a70 | 142 | security_id = SECURITY_SMS_SECURITY_MODE; |
mbedAustin | 11:cada08fc8a70 | 143 | break; |
mbedAustin | 11:cada08fc8a70 | 144 | case M2MServerSMSNumber: |
mbedAustin | 11:cada08fc8a70 | 145 | security_id = SECURITY_M2M_SERVER_SMS_NUMBER; |
mbedAustin | 11:cada08fc8a70 | 146 | break; |
mbedAustin | 11:cada08fc8a70 | 147 | case ShortServerID: |
mbedAustin | 11:cada08fc8a70 | 148 | if(M2MSecurity::Bootstrap == _server_type) { |
mbedAustin | 11:cada08fc8a70 | 149 | security_id = SECURITY_SHORT_SERVER_ID; |
mbedAustin | 11:cada08fc8a70 | 150 | } |
mbedAustin | 11:cada08fc8a70 | 151 | break; |
mbedAustin | 11:cada08fc8a70 | 152 | case ClientHoldOffTime: |
mbedAustin | 11:cada08fc8a70 | 153 | security_id = SECURITY_CLIENT_HOLD_OFF_TIME; |
mbedAustin | 11:cada08fc8a70 | 154 | break; |
mbedAustin | 11:cada08fc8a70 | 155 | default: |
mbedAustin | 11:cada08fc8a70 | 156 | // Others are mandatory resources hence cannot be deleted. |
mbedAustin | 11:cada08fc8a70 | 157 | break; |
mbedAustin | 11:cada08fc8a70 | 158 | } |
mbedAustin | 11:cada08fc8a70 | 159 | if(!security_id.empty()) { |
mbedAustin | 11:cada08fc8a70 | 160 | if(_server_instance) { |
mbedAustin | 11:cada08fc8a70 | 161 | success = _server_instance->remove_resource(security_id); |
mbedAustin | 11:cada08fc8a70 | 162 | } |
mbedAustin | 11:cada08fc8a70 | 163 | } |
mbedAustin | 11:cada08fc8a70 | 164 | return success; |
mbedAustin | 11:cada08fc8a70 | 165 | } |
mbedAustin | 11:cada08fc8a70 | 166 | |
mbedAustin | 11:cada08fc8a70 | 167 | bool M2MSecurity::set_resource_value(SecurityResource resource, |
mbedAustin | 11:cada08fc8a70 | 168 | const String &value) |
mbedAustin | 11:cada08fc8a70 | 169 | { |
mbedAustin | 11:cada08fc8a70 | 170 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 171 | if(M2MSecurity::M2MServerUri == resource) { |
mbedAustin | 11:cada08fc8a70 | 172 | M2MResource* res = get_resource(resource); |
mbedAustin | 11:cada08fc8a70 | 173 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 174 | success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length()); |
mbedAustin | 11:cada08fc8a70 | 175 | } |
mbedAustin | 11:cada08fc8a70 | 176 | } |
mbedAustin | 11:cada08fc8a70 | 177 | return success; |
mbedAustin | 11:cada08fc8a70 | 178 | } |
mbedAustin | 11:cada08fc8a70 | 179 | |
mbedAustin | 11:cada08fc8a70 | 180 | bool M2MSecurity::set_resource_value(SecurityResource resource, |
mbedAustin | 11:cada08fc8a70 | 181 | uint32_t value) |
mbedAustin | 11:cada08fc8a70 | 182 | { |
mbedAustin | 11:cada08fc8a70 | 183 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 184 | M2MResource* res = get_resource(resource); |
mbedAustin | 11:cada08fc8a70 | 185 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 186 | if(M2MSecurity::SecurityMode == resource || |
mbedAustin | 11:cada08fc8a70 | 187 | M2MSecurity::SMSSecurityMode == resource || |
mbedAustin | 11:cada08fc8a70 | 188 | M2MSecurity::M2MServerSMSNumber == resource || |
mbedAustin | 11:cada08fc8a70 | 189 | M2MSecurity::ShortServerID == resource || |
mbedAustin | 11:cada08fc8a70 | 190 | M2MSecurity::ClientHoldOffTime == resource) { |
mbedAustin | 11:cada08fc8a70 | 191 | // If it is any of the above resource |
mbedAustin | 11:cada08fc8a70 | 192 | // set the value of the resource. |
mbedAustin | 11:cada08fc8a70 | 193 | char *buffer = (char*)malloc(20); |
mbedAustin | 11:cada08fc8a70 | 194 | if(buffer) { |
mbedAustin | 11:cada08fc8a70 | 195 | int size = snprintf(buffer, 20,"%ld",(long int)value); |
mbedAustin | 11:cada08fc8a70 | 196 | success = res->set_value((const uint8_t*)buffer, |
mbedAustin | 11:cada08fc8a70 | 197 | (uint32_t)size); |
mbedAustin | 11:cada08fc8a70 | 198 | free(buffer); |
mbedAustin | 11:cada08fc8a70 | 199 | } |
mbedAustin | 11:cada08fc8a70 | 200 | } |
mbedAustin | 11:cada08fc8a70 | 201 | } |
mbedAustin | 11:cada08fc8a70 | 202 | return success; |
mbedAustin | 11:cada08fc8a70 | 203 | } |
mbedAustin | 11:cada08fc8a70 | 204 | |
mbedAustin | 11:cada08fc8a70 | 205 | bool M2MSecurity::set_resource_value(SecurityResource resource, |
mbedAustin | 11:cada08fc8a70 | 206 | const uint8_t *value, |
mbedAustin | 11:cada08fc8a70 | 207 | const uint16_t length) |
mbedAustin | 11:cada08fc8a70 | 208 | { |
mbedAustin | 11:cada08fc8a70 | 209 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 210 | M2MResource* res = get_resource(resource); |
mbedAustin | 11:cada08fc8a70 | 211 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 212 | if(M2MSecurity::PublicKey == resource || |
mbedAustin | 11:cada08fc8a70 | 213 | M2MSecurity::ServerPublicKey == resource || |
mbedAustin | 11:cada08fc8a70 | 214 | M2MSecurity::Secretkey == resource) { |
mbedAustin | 11:cada08fc8a70 | 215 | success = res->set_value(value,length); |
mbedAustin | 11:cada08fc8a70 | 216 | } |
mbedAustin | 11:cada08fc8a70 | 217 | } |
mbedAustin | 11:cada08fc8a70 | 218 | return success; |
mbedAustin | 11:cada08fc8a70 | 219 | } |
mbedAustin | 11:cada08fc8a70 | 220 | |
mbedAustin | 11:cada08fc8a70 | 221 | String M2MSecurity::resource_value_string(SecurityResource resource) const |
mbedAustin | 11:cada08fc8a70 | 222 | { |
mbedAustin | 11:cada08fc8a70 | 223 | String value = ""; |
mbedAustin | 11:cada08fc8a70 | 224 | M2MResource* res = get_resource(resource); |
mbedAustin | 11:cada08fc8a70 | 225 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 226 | if(M2MSecurity::M2MServerUri == resource) { |
mbedAustin | 11:cada08fc8a70 | 227 | uint8_t* buffer = NULL; |
mbedAustin | 11:cada08fc8a70 | 228 | uint32_t length = 0; |
mbedAustin | 11:cada08fc8a70 | 229 | res->get_value(buffer,length); |
mbedAustin | 11:cada08fc8a70 | 230 | |
mbedAustin | 11:cada08fc8a70 | 231 | char *char_buffer = (char*)malloc(length+1); |
mbedAustin | 11:cada08fc8a70 | 232 | if(char_buffer) { |
mbedAustin | 11:cada08fc8a70 | 233 | memset(char_buffer,0,length+1); |
mbedAustin | 11:cada08fc8a70 | 234 | if(buffer) { |
mbedAustin | 11:cada08fc8a70 | 235 | memcpy(char_buffer,(char*)buffer,length); |
mbedAustin | 11:cada08fc8a70 | 236 | } |
mbedAustin | 11:cada08fc8a70 | 237 | String s_name(char_buffer); |
mbedAustin | 11:cada08fc8a70 | 238 | value = s_name; |
mbedAustin | 11:cada08fc8a70 | 239 | free(char_buffer); |
mbedAustin | 11:cada08fc8a70 | 240 | } |
mbedAustin | 11:cada08fc8a70 | 241 | if(buffer) { |
mbedAustin | 11:cada08fc8a70 | 242 | free(buffer); |
mbedAustin | 11:cada08fc8a70 | 243 | } |
mbedAustin | 11:cada08fc8a70 | 244 | } |
mbedAustin | 11:cada08fc8a70 | 245 | } |
mbedAustin | 11:cada08fc8a70 | 246 | return value; |
mbedAustin | 11:cada08fc8a70 | 247 | } |
mbedAustin | 11:cada08fc8a70 | 248 | |
mbedAustin | 11:cada08fc8a70 | 249 | uint32_t M2MSecurity::resource_value_buffer(SecurityResource resource, |
mbedAustin | 11:cada08fc8a70 | 250 | uint8_t *&data) const |
mbedAustin | 11:cada08fc8a70 | 251 | { |
mbedAustin | 11:cada08fc8a70 | 252 | uint32_t size = 0; |
mbedAustin | 11:cada08fc8a70 | 253 | M2MResource* res = get_resource(resource); |
mbedAustin | 11:cada08fc8a70 | 254 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 255 | if(M2MSecurity::PublicKey == resource || |
mbedAustin | 11:cada08fc8a70 | 256 | M2MSecurity::ServerPublicKey == resource || |
mbedAustin | 11:cada08fc8a70 | 257 | M2MSecurity::Secretkey == resource) { |
mbedAustin | 11:cada08fc8a70 | 258 | res->get_value(data,size); |
mbedAustin | 11:cada08fc8a70 | 259 | } |
mbedAustin | 11:cada08fc8a70 | 260 | } |
mbedAustin | 11:cada08fc8a70 | 261 | return size; |
mbedAustin | 11:cada08fc8a70 | 262 | } |
mbedAustin | 11:cada08fc8a70 | 263 | |
mbedAustin | 11:cada08fc8a70 | 264 | |
mbedAustin | 11:cada08fc8a70 | 265 | uint32_t M2MSecurity::resource_value_int(SecurityResource resource) const |
mbedAustin | 11:cada08fc8a70 | 266 | { |
mbedAustin | 11:cada08fc8a70 | 267 | uint32_t value = 0; |
mbedAustin | 11:cada08fc8a70 | 268 | M2MResource* res = get_resource(resource); |
mbedAustin | 11:cada08fc8a70 | 269 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 270 | if(M2MSecurity::SecurityMode == resource || |
mbedAustin | 11:cada08fc8a70 | 271 | M2MSecurity::SMSSecurityMode == resource || |
mbedAustin | 11:cada08fc8a70 | 272 | M2MSecurity::M2MServerSMSNumber == resource || |
mbedAustin | 11:cada08fc8a70 | 273 | M2MSecurity::ShortServerID == resource || |
mbedAustin | 11:cada08fc8a70 | 274 | M2MSecurity::ClientHoldOffTime == resource) { |
mbedAustin | 11:cada08fc8a70 | 275 | // Get the value and convert it into integer |
mbedAustin | 11:cada08fc8a70 | 276 | uint8_t* buffer = NULL; |
mbedAustin | 11:cada08fc8a70 | 277 | uint32_t length = 0; |
mbedAustin | 11:cada08fc8a70 | 278 | res->get_value(buffer,length); |
mbedAustin | 11:cada08fc8a70 | 279 | if(buffer) { |
mbedAustin | 11:cada08fc8a70 | 280 | value = atoi((const char*)buffer); |
mbedAustin | 11:cada08fc8a70 | 281 | free(buffer); |
mbedAustin | 11:cada08fc8a70 | 282 | } |
mbedAustin | 11:cada08fc8a70 | 283 | } |
mbedAustin | 11:cada08fc8a70 | 284 | } |
mbedAustin | 11:cada08fc8a70 | 285 | return value; |
mbedAustin | 11:cada08fc8a70 | 286 | } |
mbedAustin | 11:cada08fc8a70 | 287 | |
mbedAustin | 11:cada08fc8a70 | 288 | bool M2MSecurity::is_resource_present(SecurityResource resource) const |
mbedAustin | 11:cada08fc8a70 | 289 | { |
mbedAustin | 11:cada08fc8a70 | 290 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 291 | M2MResource *res = get_resource(resource); |
mbedAustin | 11:cada08fc8a70 | 292 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 293 | success = true; |
mbedAustin | 11:cada08fc8a70 | 294 | } |
mbedAustin | 11:cada08fc8a70 | 295 | return success; |
mbedAustin | 11:cada08fc8a70 | 296 | } |
mbedAustin | 11:cada08fc8a70 | 297 | |
mbedAustin | 11:cada08fc8a70 | 298 | uint16_t M2MSecurity::total_resource_count() const |
mbedAustin | 11:cada08fc8a70 | 299 | { |
mbedAustin | 11:cada08fc8a70 | 300 | uint16_t count = 0; |
mbedAustin | 11:cada08fc8a70 | 301 | if(_server_instance) { |
mbedAustin | 11:cada08fc8a70 | 302 | count = _server_instance->resources().size(); |
mbedAustin | 11:cada08fc8a70 | 303 | } |
mbedAustin | 11:cada08fc8a70 | 304 | return count; |
mbedAustin | 11:cada08fc8a70 | 305 | } |
mbedAustin | 11:cada08fc8a70 | 306 | |
mbedAustin | 11:cada08fc8a70 | 307 | M2MSecurity::ServerType M2MSecurity::server_type() const |
mbedAustin | 11:cada08fc8a70 | 308 | { |
mbedAustin | 11:cada08fc8a70 | 309 | return _server_type; |
mbedAustin | 11:cada08fc8a70 | 310 | } |
mbedAustin | 11:cada08fc8a70 | 311 | |
mbedAustin | 11:cada08fc8a70 | 312 | M2MResource* M2MSecurity::get_resource(SecurityResource res) const |
mbedAustin | 11:cada08fc8a70 | 313 | { |
mbedAustin | 11:cada08fc8a70 | 314 | M2MResource* res_object = NULL; |
mbedAustin | 11:cada08fc8a70 | 315 | if(_server_instance) { |
mbedAustin | 11:cada08fc8a70 | 316 | String res_name = ""; |
mbedAustin | 11:cada08fc8a70 | 317 | switch(res) { |
mbedAustin | 11:cada08fc8a70 | 318 | case M2MServerUri: |
mbedAustin | 11:cada08fc8a70 | 319 | res_name = SECURITY_M2M_SERVER_URI; |
mbedAustin | 11:cada08fc8a70 | 320 | break; |
mbedAustin | 11:cada08fc8a70 | 321 | case BootstrapServer: |
mbedAustin | 11:cada08fc8a70 | 322 | res_name = SECURITY_BOOTSTRAP_SERVER; |
mbedAustin | 11:cada08fc8a70 | 323 | break; |
mbedAustin | 11:cada08fc8a70 | 324 | case SecurityMode: |
mbedAustin | 11:cada08fc8a70 | 325 | res_name = SECURITY_SECURITY_MODE; |
mbedAustin | 11:cada08fc8a70 | 326 | break; |
mbedAustin | 11:cada08fc8a70 | 327 | case PublicKey: |
mbedAustin | 11:cada08fc8a70 | 328 | res_name = SECURITY_PUBLIC_KEY; |
mbedAustin | 11:cada08fc8a70 | 329 | break; |
mbedAustin | 11:cada08fc8a70 | 330 | case ServerPublicKey: |
mbedAustin | 11:cada08fc8a70 | 331 | res_name = SECURITY_SERVER_PUBLIC_KEY; |
mbedAustin | 11:cada08fc8a70 | 332 | break; |
mbedAustin | 11:cada08fc8a70 | 333 | case Secretkey: |
mbedAustin | 11:cada08fc8a70 | 334 | res_name = SECURITY_SECRET_KEY; |
mbedAustin | 11:cada08fc8a70 | 335 | break; |
mbedAustin | 11:cada08fc8a70 | 336 | case SMSSecurityMode: |
mbedAustin | 11:cada08fc8a70 | 337 | res_name = SECURITY_SMS_SECURITY_MODE; |
mbedAustin | 11:cada08fc8a70 | 338 | break; |
mbedAustin | 11:cada08fc8a70 | 339 | case SMSBindingKey: |
mbedAustin | 11:cada08fc8a70 | 340 | res_name = SECURITY_SMS_BINDING_KEY; |
mbedAustin | 11:cada08fc8a70 | 341 | break; |
mbedAustin | 11:cada08fc8a70 | 342 | case SMSBindingSecretKey: |
mbedAustin | 11:cada08fc8a70 | 343 | res_name = SECURITY_SMS_BINDING_SECRET_KEY; |
mbedAustin | 11:cada08fc8a70 | 344 | break; |
mbedAustin | 11:cada08fc8a70 | 345 | case M2MServerSMSNumber: |
mbedAustin | 11:cada08fc8a70 | 346 | res_name = SECURITY_M2M_SERVER_SMS_NUMBER; |
mbedAustin | 11:cada08fc8a70 | 347 | break; |
mbedAustin | 11:cada08fc8a70 | 348 | case ShortServerID: |
mbedAustin | 11:cada08fc8a70 | 349 | res_name = SECURITY_SHORT_SERVER_ID; |
mbedAustin | 11:cada08fc8a70 | 350 | break; |
mbedAustin | 11:cada08fc8a70 | 351 | case ClientHoldOffTime: |
mbedAustin | 11:cada08fc8a70 | 352 | res_name = SECURITY_CLIENT_HOLD_OFF_TIME; |
mbedAustin | 11:cada08fc8a70 | 353 | break; |
mbedAustin | 11:cada08fc8a70 | 354 | } |
mbedAustin | 11:cada08fc8a70 | 355 | res_object = _server_instance->resource(res_name); |
mbedAustin | 11:cada08fc8a70 | 356 | } |
mbedAustin | 11:cada08fc8a70 | 357 | return res_object; |
mbedAustin | 11:cada08fc8a70 | 358 | } |