Ram Gandikota
/
metronome
FRDM K64F Metronome
mbed-client/source/m2msecurity.cpp@0:a2cb7295a1f7, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:35:07 2017 +0000
- Revision:
- 0:a2cb7295a1f7
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:a2cb7295a1f7 | 1 | /* |
ram54288 | 0:a2cb7295a1f7 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
ram54288 | 0:a2cb7295a1f7 | 3 | * SPDX-License-Identifier: Apache-2.0 |
ram54288 | 0:a2cb7295a1f7 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
ram54288 | 0:a2cb7295a1f7 | 5 | * not use this file except in compliance with the License. |
ram54288 | 0:a2cb7295a1f7 | 6 | * You may obtain a copy of the License at |
ram54288 | 0:a2cb7295a1f7 | 7 | * |
ram54288 | 0:a2cb7295a1f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:a2cb7295a1f7 | 9 | * |
ram54288 | 0:a2cb7295a1f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:a2cb7295a1f7 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
ram54288 | 0:a2cb7295a1f7 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:a2cb7295a1f7 | 13 | * See the License for the specific language governing permissions and |
ram54288 | 0:a2cb7295a1f7 | 14 | * limitations under the License. |
ram54288 | 0:a2cb7295a1f7 | 15 | */ |
ram54288 | 0:a2cb7295a1f7 | 16 | #include "mbed-client/m2msecurity.h" |
ram54288 | 0:a2cb7295a1f7 | 17 | #include "mbed-client/m2mconstants.h" |
ram54288 | 0:a2cb7295a1f7 | 18 | #include "mbed-client/m2mobject.h" |
ram54288 | 0:a2cb7295a1f7 | 19 | #include "mbed-client/m2mobjectinstance.h" |
ram54288 | 0:a2cb7295a1f7 | 20 | #include "mbed-client/m2mresource.h" |
ram54288 | 0:a2cb7295a1f7 | 21 | #include "mbed-client/m2mstring.h" |
ram54288 | 0:a2cb7295a1f7 | 22 | #include "mbed-trace/mbed_trace.h" |
ram54288 | 0:a2cb7295a1f7 | 23 | |
ram54288 | 0:a2cb7295a1f7 | 24 | #include <stdlib.h> |
ram54288 | 0:a2cb7295a1f7 | 25 | |
ram54288 | 0:a2cb7295a1f7 | 26 | #define TRACE_GROUP "mClt" |
ram54288 | 0:a2cb7295a1f7 | 27 | |
ram54288 | 0:a2cb7295a1f7 | 28 | #define BUFFER_SIZE 21 |
ram54288 | 0:a2cb7295a1f7 | 29 | |
ram54288 | 0:a2cb7295a1f7 | 30 | M2MSecurity::M2MSecurity(ServerType ser_type) |
ram54288 | 0:a2cb7295a1f7 | 31 | : M2MObject(M2M_SECURITY_ID, stringdup(M2M_SECURITY_ID)), |
ram54288 | 0:a2cb7295a1f7 | 32 | _server_instance(NULL), |
ram54288 | 0:a2cb7295a1f7 | 33 | _server_type(ser_type) |
ram54288 | 0:a2cb7295a1f7 | 34 | { |
ram54288 | 0:a2cb7295a1f7 | 35 | _server_instance = M2MObject::create_object_instance(); |
ram54288 | 0:a2cb7295a1f7 | 36 | |
ram54288 | 0:a2cb7295a1f7 | 37 | if(_server_instance) { |
ram54288 | 0:a2cb7295a1f7 | 38 | M2MResource* res = _server_instance->create_dynamic_resource(SECURITY_M2M_SERVER_URI, |
ram54288 | 0:a2cb7295a1f7 | 39 | OMA_RESOURCE_TYPE, |
ram54288 | 0:a2cb7295a1f7 | 40 | M2MResourceInstance::STRING, |
ram54288 | 0:a2cb7295a1f7 | 41 | false); |
ram54288 | 0:a2cb7295a1f7 | 42 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 43 | res->set_operation(M2MBase::NOT_ALLOWED); |
ram54288 | 0:a2cb7295a1f7 | 44 | } |
ram54288 | 0:a2cb7295a1f7 | 45 | res = _server_instance->create_dynamic_resource(SECURITY_BOOTSTRAP_SERVER, |
ram54288 | 0:a2cb7295a1f7 | 46 | OMA_RESOURCE_TYPE, |
ram54288 | 0:a2cb7295a1f7 | 47 | M2MResourceInstance::BOOLEAN, |
ram54288 | 0:a2cb7295a1f7 | 48 | false); |
ram54288 | 0:a2cb7295a1f7 | 49 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 50 | res->set_operation(M2MBase::NOT_ALLOWED); |
ram54288 | 0:a2cb7295a1f7 | 51 | } |
ram54288 | 0:a2cb7295a1f7 | 52 | res = _server_instance->create_dynamic_resource(SECURITY_SECURITY_MODE, |
ram54288 | 0:a2cb7295a1f7 | 53 | OMA_RESOURCE_TYPE, |
ram54288 | 0:a2cb7295a1f7 | 54 | M2MResourceInstance::INTEGER, |
ram54288 | 0:a2cb7295a1f7 | 55 | false); |
ram54288 | 0:a2cb7295a1f7 | 56 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 57 | res->set_operation(M2MBase::NOT_ALLOWED); |
ram54288 | 0:a2cb7295a1f7 | 58 | } |
ram54288 | 0:a2cb7295a1f7 | 59 | res = _server_instance->create_dynamic_resource(SECURITY_PUBLIC_KEY, |
ram54288 | 0:a2cb7295a1f7 | 60 | OMA_RESOURCE_TYPE, |
ram54288 | 0:a2cb7295a1f7 | 61 | M2MResourceInstance::OPAQUE, |
ram54288 | 0:a2cb7295a1f7 | 62 | false); |
ram54288 | 0:a2cb7295a1f7 | 63 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 64 | res->set_operation(M2MBase::NOT_ALLOWED); |
ram54288 | 0:a2cb7295a1f7 | 65 | } |
ram54288 | 0:a2cb7295a1f7 | 66 | res = _server_instance->create_dynamic_resource(SECURITY_SERVER_PUBLIC_KEY, |
ram54288 | 0:a2cb7295a1f7 | 67 | OMA_RESOURCE_TYPE, |
ram54288 | 0:a2cb7295a1f7 | 68 | M2MResourceInstance::OPAQUE, |
ram54288 | 0:a2cb7295a1f7 | 69 | false); |
ram54288 | 0:a2cb7295a1f7 | 70 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 71 | res->set_operation(M2MBase::NOT_ALLOWED); |
ram54288 | 0:a2cb7295a1f7 | 72 | } |
ram54288 | 0:a2cb7295a1f7 | 73 | res = _server_instance->create_dynamic_resource(SECURITY_SECRET_KEY, |
ram54288 | 0:a2cb7295a1f7 | 74 | OMA_RESOURCE_TYPE, |
ram54288 | 0:a2cb7295a1f7 | 75 | M2MResourceInstance::OPAQUE, |
ram54288 | 0:a2cb7295a1f7 | 76 | false); |
ram54288 | 0:a2cb7295a1f7 | 77 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 78 | res->set_operation(M2MBase::NOT_ALLOWED); |
ram54288 | 0:a2cb7295a1f7 | 79 | } |
ram54288 | 0:a2cb7295a1f7 | 80 | if(M2MSecurity::M2MServer == ser_type) { |
ram54288 | 0:a2cb7295a1f7 | 81 | res = _server_instance->create_dynamic_resource(SECURITY_SHORT_SERVER_ID, |
ram54288 | 0:a2cb7295a1f7 | 82 | OMA_RESOURCE_TYPE, |
ram54288 | 0:a2cb7295a1f7 | 83 | M2MResourceInstance::INTEGER, |
ram54288 | 0:a2cb7295a1f7 | 84 | false); |
ram54288 | 0:a2cb7295a1f7 | 85 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 86 | res->set_operation(M2MBase::NOT_ALLOWED); |
ram54288 | 0:a2cb7295a1f7 | 87 | } |
ram54288 | 0:a2cb7295a1f7 | 88 | } |
ram54288 | 0:a2cb7295a1f7 | 89 | } |
ram54288 | 0:a2cb7295a1f7 | 90 | } |
ram54288 | 0:a2cb7295a1f7 | 91 | |
ram54288 | 0:a2cb7295a1f7 | 92 | M2MSecurity::~M2MSecurity() |
ram54288 | 0:a2cb7295a1f7 | 93 | { |
ram54288 | 0:a2cb7295a1f7 | 94 | } |
ram54288 | 0:a2cb7295a1f7 | 95 | |
ram54288 | 0:a2cb7295a1f7 | 96 | M2MResource* M2MSecurity::create_resource(SecurityResource resource, uint32_t value) |
ram54288 | 0:a2cb7295a1f7 | 97 | { |
ram54288 | 0:a2cb7295a1f7 | 98 | M2MResource* res = NULL; |
ram54288 | 0:a2cb7295a1f7 | 99 | const char* security_id_ptr = ""; |
ram54288 | 0:a2cb7295a1f7 | 100 | if(!is_resource_present(resource)) { |
ram54288 | 0:a2cb7295a1f7 | 101 | switch(resource) { |
ram54288 | 0:a2cb7295a1f7 | 102 | case SMSSecurityMode: |
ram54288 | 0:a2cb7295a1f7 | 103 | security_id_ptr = SECURITY_SMS_SECURITY_MODE; |
ram54288 | 0:a2cb7295a1f7 | 104 | break; |
ram54288 | 0:a2cb7295a1f7 | 105 | case M2MServerSMSNumber: |
ram54288 | 0:a2cb7295a1f7 | 106 | security_id_ptr = SECURITY_M2M_SERVER_SMS_NUMBER; |
ram54288 | 0:a2cb7295a1f7 | 107 | break; |
ram54288 | 0:a2cb7295a1f7 | 108 | case ShortServerID: |
ram54288 | 0:a2cb7295a1f7 | 109 | security_id_ptr = SECURITY_SHORT_SERVER_ID; |
ram54288 | 0:a2cb7295a1f7 | 110 | break; |
ram54288 | 0:a2cb7295a1f7 | 111 | case ClientHoldOffTime: |
ram54288 | 0:a2cb7295a1f7 | 112 | security_id_ptr = SECURITY_CLIENT_HOLD_OFF_TIME; |
ram54288 | 0:a2cb7295a1f7 | 113 | break; |
ram54288 | 0:a2cb7295a1f7 | 114 | default: |
ram54288 | 0:a2cb7295a1f7 | 115 | break; |
ram54288 | 0:a2cb7295a1f7 | 116 | } |
ram54288 | 0:a2cb7295a1f7 | 117 | } |
ram54288 | 0:a2cb7295a1f7 | 118 | |
ram54288 | 0:a2cb7295a1f7 | 119 | const String security_id(security_id_ptr); |
ram54288 | 0:a2cb7295a1f7 | 120 | |
ram54288 | 0:a2cb7295a1f7 | 121 | if(!security_id.empty()) { |
ram54288 | 0:a2cb7295a1f7 | 122 | if(_server_instance) { |
ram54288 | 0:a2cb7295a1f7 | 123 | res = _server_instance->create_dynamic_resource(security_id,OMA_RESOURCE_TYPE, |
ram54288 | 0:a2cb7295a1f7 | 124 | M2MResourceInstance::INTEGER, |
ram54288 | 0:a2cb7295a1f7 | 125 | false); |
ram54288 | 0:a2cb7295a1f7 | 126 | |
ram54288 | 0:a2cb7295a1f7 | 127 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 128 | res->set_operation(M2MBase::NOT_ALLOWED); |
ram54288 | 0:a2cb7295a1f7 | 129 | res->set_value(value); |
ram54288 | 0:a2cb7295a1f7 | 130 | } |
ram54288 | 0:a2cb7295a1f7 | 131 | } |
ram54288 | 0:a2cb7295a1f7 | 132 | } |
ram54288 | 0:a2cb7295a1f7 | 133 | return res; |
ram54288 | 0:a2cb7295a1f7 | 134 | } |
ram54288 | 0:a2cb7295a1f7 | 135 | |
ram54288 | 0:a2cb7295a1f7 | 136 | bool M2MSecurity::delete_resource(SecurityResource resource) |
ram54288 | 0:a2cb7295a1f7 | 137 | { |
ram54288 | 0:a2cb7295a1f7 | 138 | bool success = false; |
ram54288 | 0:a2cb7295a1f7 | 139 | const char* security_id_ptr; |
ram54288 | 0:a2cb7295a1f7 | 140 | switch(resource) { |
ram54288 | 0:a2cb7295a1f7 | 141 | case SMSSecurityMode: |
ram54288 | 0:a2cb7295a1f7 | 142 | security_id_ptr = SECURITY_SMS_SECURITY_MODE; |
ram54288 | 0:a2cb7295a1f7 | 143 | break; |
ram54288 | 0:a2cb7295a1f7 | 144 | case M2MServerSMSNumber: |
ram54288 | 0:a2cb7295a1f7 | 145 | security_id_ptr = SECURITY_M2M_SERVER_SMS_NUMBER; |
ram54288 | 0:a2cb7295a1f7 | 146 | break; |
ram54288 | 0:a2cb7295a1f7 | 147 | case ShortServerID: |
ram54288 | 0:a2cb7295a1f7 | 148 | if(M2MSecurity::Bootstrap == _server_type) { |
ram54288 | 0:a2cb7295a1f7 | 149 | security_id_ptr = SECURITY_SHORT_SERVER_ID; |
ram54288 | 0:a2cb7295a1f7 | 150 | } |
ram54288 | 0:a2cb7295a1f7 | 151 | break; |
ram54288 | 0:a2cb7295a1f7 | 152 | case ClientHoldOffTime: |
ram54288 | 0:a2cb7295a1f7 | 153 | security_id_ptr = SECURITY_CLIENT_HOLD_OFF_TIME; |
ram54288 | 0:a2cb7295a1f7 | 154 | break; |
ram54288 | 0:a2cb7295a1f7 | 155 | default: |
ram54288 | 0:a2cb7295a1f7 | 156 | // Others are mandatory resources hence cannot be deleted. |
ram54288 | 0:a2cb7295a1f7 | 157 | security_id_ptr = NULL; |
ram54288 | 0:a2cb7295a1f7 | 158 | break; |
ram54288 | 0:a2cb7295a1f7 | 159 | } |
ram54288 | 0:a2cb7295a1f7 | 160 | |
ram54288 | 0:a2cb7295a1f7 | 161 | if(security_id_ptr) { |
ram54288 | 0:a2cb7295a1f7 | 162 | if(_server_instance) { |
ram54288 | 0:a2cb7295a1f7 | 163 | success = _server_instance->remove_resource(security_id_ptr); |
ram54288 | 0:a2cb7295a1f7 | 164 | } |
ram54288 | 0:a2cb7295a1f7 | 165 | } |
ram54288 | 0:a2cb7295a1f7 | 166 | return success; |
ram54288 | 0:a2cb7295a1f7 | 167 | } |
ram54288 | 0:a2cb7295a1f7 | 168 | |
ram54288 | 0:a2cb7295a1f7 | 169 | bool M2MSecurity::set_resource_value(SecurityResource resource, |
ram54288 | 0:a2cb7295a1f7 | 170 | const String &value) |
ram54288 | 0:a2cb7295a1f7 | 171 | { |
ram54288 | 0:a2cb7295a1f7 | 172 | bool success = false; |
ram54288 | 0:a2cb7295a1f7 | 173 | if(M2MSecurity::M2MServerUri == resource) { |
ram54288 | 0:a2cb7295a1f7 | 174 | M2MResource* res = get_resource(resource); |
ram54288 | 0:a2cb7295a1f7 | 175 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 176 | success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length()); |
ram54288 | 0:a2cb7295a1f7 | 177 | } |
ram54288 | 0:a2cb7295a1f7 | 178 | } |
ram54288 | 0:a2cb7295a1f7 | 179 | return success; |
ram54288 | 0:a2cb7295a1f7 | 180 | } |
ram54288 | 0:a2cb7295a1f7 | 181 | |
ram54288 | 0:a2cb7295a1f7 | 182 | bool M2MSecurity::set_resource_value(SecurityResource resource, |
ram54288 | 0:a2cb7295a1f7 | 183 | uint32_t value) |
ram54288 | 0:a2cb7295a1f7 | 184 | { |
ram54288 | 0:a2cb7295a1f7 | 185 | bool success = false; |
ram54288 | 0:a2cb7295a1f7 | 186 | M2MResource* res = get_resource(resource); |
ram54288 | 0:a2cb7295a1f7 | 187 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 188 | if(M2MSecurity::SecurityMode == resource || |
ram54288 | 0:a2cb7295a1f7 | 189 | M2MSecurity::SMSSecurityMode == resource || |
ram54288 | 0:a2cb7295a1f7 | 190 | M2MSecurity::M2MServerSMSNumber == resource || |
ram54288 | 0:a2cb7295a1f7 | 191 | M2MSecurity::ShortServerID == resource || |
ram54288 | 0:a2cb7295a1f7 | 192 | M2MSecurity::ClientHoldOffTime == resource) { |
ram54288 | 0:a2cb7295a1f7 | 193 | // If it is any of the above resource |
ram54288 | 0:a2cb7295a1f7 | 194 | // set the value of the resource. |
ram54288 | 0:a2cb7295a1f7 | 195 | uint8_t size = 0; |
ram54288 | 0:a2cb7295a1f7 | 196 | uint8_t *buffer = String::convert_integer_to_array(value, size); |
ram54288 | 0:a2cb7295a1f7 | 197 | success = res->set_value(buffer,size); |
ram54288 | 0:a2cb7295a1f7 | 198 | free(buffer); |
ram54288 | 0:a2cb7295a1f7 | 199 | } |
ram54288 | 0:a2cb7295a1f7 | 200 | } |
ram54288 | 0:a2cb7295a1f7 | 201 | return success; |
ram54288 | 0:a2cb7295a1f7 | 202 | } |
ram54288 | 0:a2cb7295a1f7 | 203 | |
ram54288 | 0:a2cb7295a1f7 | 204 | bool M2MSecurity::set_resource_value(SecurityResource resource, |
ram54288 | 0:a2cb7295a1f7 | 205 | const uint8_t *value, |
ram54288 | 0:a2cb7295a1f7 | 206 | const uint16_t length) |
ram54288 | 0:a2cb7295a1f7 | 207 | { |
ram54288 | 0:a2cb7295a1f7 | 208 | bool success = false; |
ram54288 | 0:a2cb7295a1f7 | 209 | M2MResource* res = get_resource(resource); |
ram54288 | 0:a2cb7295a1f7 | 210 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 211 | if(M2MSecurity::PublicKey == resource || |
ram54288 | 0:a2cb7295a1f7 | 212 | M2MSecurity::ServerPublicKey == resource || |
ram54288 | 0:a2cb7295a1f7 | 213 | M2MSecurity::Secretkey == resource) { |
ram54288 | 0:a2cb7295a1f7 | 214 | success = res->set_value(value,length); |
ram54288 | 0:a2cb7295a1f7 | 215 | } |
ram54288 | 0:a2cb7295a1f7 | 216 | } |
ram54288 | 0:a2cb7295a1f7 | 217 | return success; |
ram54288 | 0:a2cb7295a1f7 | 218 | } |
ram54288 | 0:a2cb7295a1f7 | 219 | |
ram54288 | 0:a2cb7295a1f7 | 220 | String M2MSecurity::resource_value_string(SecurityResource resource) const |
ram54288 | 0:a2cb7295a1f7 | 221 | { |
ram54288 | 0:a2cb7295a1f7 | 222 | String value = ""; |
ram54288 | 0:a2cb7295a1f7 | 223 | M2MResource* res = get_resource(resource); |
ram54288 | 0:a2cb7295a1f7 | 224 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 225 | if(M2MSecurity::M2MServerUri == resource) { |
ram54288 | 0:a2cb7295a1f7 | 226 | value = res->get_value_string(); |
ram54288 | 0:a2cb7295a1f7 | 227 | } |
ram54288 | 0:a2cb7295a1f7 | 228 | } |
ram54288 | 0:a2cb7295a1f7 | 229 | return value; |
ram54288 | 0:a2cb7295a1f7 | 230 | } |
ram54288 | 0:a2cb7295a1f7 | 231 | |
ram54288 | 0:a2cb7295a1f7 | 232 | uint32_t M2MSecurity::resource_value_buffer(SecurityResource resource, |
ram54288 | 0:a2cb7295a1f7 | 233 | uint8_t *&data) const |
ram54288 | 0:a2cb7295a1f7 | 234 | { |
ram54288 | 0:a2cb7295a1f7 | 235 | uint32_t size = 0; |
ram54288 | 0:a2cb7295a1f7 | 236 | M2MResource* res = get_resource(resource); |
ram54288 | 0:a2cb7295a1f7 | 237 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 238 | if(M2MSecurity::PublicKey == resource || |
ram54288 | 0:a2cb7295a1f7 | 239 | M2MSecurity::ServerPublicKey == resource || |
ram54288 | 0:a2cb7295a1f7 | 240 | M2MSecurity::Secretkey == resource) { |
ram54288 | 0:a2cb7295a1f7 | 241 | res->get_value(data,size); |
ram54288 | 0:a2cb7295a1f7 | 242 | } |
ram54288 | 0:a2cb7295a1f7 | 243 | } |
ram54288 | 0:a2cb7295a1f7 | 244 | return size; |
ram54288 | 0:a2cb7295a1f7 | 245 | } |
ram54288 | 0:a2cb7295a1f7 | 246 | |
ram54288 | 0:a2cb7295a1f7 | 247 | uint32_t M2MSecurity::resource_value_buffer(SecurityResource resource, |
ram54288 | 0:a2cb7295a1f7 | 248 | const uint8_t *&data) const |
ram54288 | 0:a2cb7295a1f7 | 249 | { |
ram54288 | 0:a2cb7295a1f7 | 250 | uint32_t size = 0; |
ram54288 | 0:a2cb7295a1f7 | 251 | M2MResource* res = get_resource(resource); |
ram54288 | 0:a2cb7295a1f7 | 252 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 253 | if(M2MSecurity::PublicKey == resource || |
ram54288 | 0:a2cb7295a1f7 | 254 | M2MSecurity::ServerPublicKey == resource || |
ram54288 | 0:a2cb7295a1f7 | 255 | M2MSecurity::Secretkey == resource) { |
ram54288 | 0:a2cb7295a1f7 | 256 | data = res->value(); |
ram54288 | 0:a2cb7295a1f7 | 257 | size = res->value_length(); |
ram54288 | 0:a2cb7295a1f7 | 258 | } |
ram54288 | 0:a2cb7295a1f7 | 259 | } |
ram54288 | 0:a2cb7295a1f7 | 260 | return size; |
ram54288 | 0:a2cb7295a1f7 | 261 | } |
ram54288 | 0:a2cb7295a1f7 | 262 | |
ram54288 | 0:a2cb7295a1f7 | 263 | |
ram54288 | 0:a2cb7295a1f7 | 264 | uint32_t M2MSecurity::resource_value_int(SecurityResource resource) const |
ram54288 | 0:a2cb7295a1f7 | 265 | { |
ram54288 | 0:a2cb7295a1f7 | 266 | uint32_t value = 0; |
ram54288 | 0:a2cb7295a1f7 | 267 | M2MResource* res = get_resource(resource); |
ram54288 | 0:a2cb7295a1f7 | 268 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 269 | if(M2MSecurity::SecurityMode == resource || |
ram54288 | 0:a2cb7295a1f7 | 270 | M2MSecurity::SMSSecurityMode == resource || |
ram54288 | 0:a2cb7295a1f7 | 271 | M2MSecurity::M2MServerSMSNumber == resource || |
ram54288 | 0:a2cb7295a1f7 | 272 | M2MSecurity::ShortServerID == resource || |
ram54288 | 0:a2cb7295a1f7 | 273 | M2MSecurity::ClientHoldOffTime == resource) { |
ram54288 | 0:a2cb7295a1f7 | 274 | // Get the value and convert it into integer. This is not the most |
ram54288 | 0:a2cb7295a1f7 | 275 | // efficient way, as it takes pointless heap copy to get the zero termination. |
ram54288 | 0:a2cb7295a1f7 | 276 | uint8_t* buffer = NULL; |
ram54288 | 0:a2cb7295a1f7 | 277 | uint32_t length = 0; |
ram54288 | 0:a2cb7295a1f7 | 278 | res->get_value(buffer,length); |
ram54288 | 0:a2cb7295a1f7 | 279 | if(buffer) { |
ram54288 | 0:a2cb7295a1f7 | 280 | value = String::convert_array_to_integer(buffer,length); |
ram54288 | 0:a2cb7295a1f7 | 281 | free(buffer); |
ram54288 | 0:a2cb7295a1f7 | 282 | } |
ram54288 | 0:a2cb7295a1f7 | 283 | } |
ram54288 | 0:a2cb7295a1f7 | 284 | } |
ram54288 | 0:a2cb7295a1f7 | 285 | return value; |
ram54288 | 0:a2cb7295a1f7 | 286 | } |
ram54288 | 0:a2cb7295a1f7 | 287 | |
ram54288 | 0:a2cb7295a1f7 | 288 | bool M2MSecurity::is_resource_present(SecurityResource resource) const |
ram54288 | 0:a2cb7295a1f7 | 289 | { |
ram54288 | 0:a2cb7295a1f7 | 290 | bool success = false; |
ram54288 | 0:a2cb7295a1f7 | 291 | M2MResource *res = get_resource(resource); |
ram54288 | 0:a2cb7295a1f7 | 292 | if(res) { |
ram54288 | 0:a2cb7295a1f7 | 293 | success = true; |
ram54288 | 0:a2cb7295a1f7 | 294 | } |
ram54288 | 0:a2cb7295a1f7 | 295 | return success; |
ram54288 | 0:a2cb7295a1f7 | 296 | } |
ram54288 | 0:a2cb7295a1f7 | 297 | |
ram54288 | 0:a2cb7295a1f7 | 298 | uint16_t M2MSecurity::total_resource_count() const |
ram54288 | 0:a2cb7295a1f7 | 299 | { |
ram54288 | 0:a2cb7295a1f7 | 300 | uint16_t count = 0; |
ram54288 | 0:a2cb7295a1f7 | 301 | if(_server_instance) { |
ram54288 | 0:a2cb7295a1f7 | 302 | count = _server_instance->resources().size(); |
ram54288 | 0:a2cb7295a1f7 | 303 | } |
ram54288 | 0:a2cb7295a1f7 | 304 | return count; |
ram54288 | 0:a2cb7295a1f7 | 305 | } |
ram54288 | 0:a2cb7295a1f7 | 306 | |
ram54288 | 0:a2cb7295a1f7 | 307 | M2MSecurity::ServerType M2MSecurity::server_type() const |
ram54288 | 0:a2cb7295a1f7 | 308 | { |
ram54288 | 0:a2cb7295a1f7 | 309 | return _server_type; |
ram54288 | 0:a2cb7295a1f7 | 310 | } |
ram54288 | 0:a2cb7295a1f7 | 311 | |
ram54288 | 0:a2cb7295a1f7 | 312 | M2MResource* M2MSecurity::get_resource(SecurityResource res) const |
ram54288 | 0:a2cb7295a1f7 | 313 | { |
ram54288 | 0:a2cb7295a1f7 | 314 | M2MResource* res_object = NULL; |
ram54288 | 0:a2cb7295a1f7 | 315 | if(_server_instance) { |
ram54288 | 0:a2cb7295a1f7 | 316 | const char* res_name_ptr = NULL; |
ram54288 | 0:a2cb7295a1f7 | 317 | switch(res) { |
ram54288 | 0:a2cb7295a1f7 | 318 | case M2MServerUri: |
ram54288 | 0:a2cb7295a1f7 | 319 | res_name_ptr = SECURITY_M2M_SERVER_URI; |
ram54288 | 0:a2cb7295a1f7 | 320 | break; |
ram54288 | 0:a2cb7295a1f7 | 321 | case BootstrapServer: |
ram54288 | 0:a2cb7295a1f7 | 322 | res_name_ptr = SECURITY_BOOTSTRAP_SERVER; |
ram54288 | 0:a2cb7295a1f7 | 323 | break; |
ram54288 | 0:a2cb7295a1f7 | 324 | case SecurityMode: |
ram54288 | 0:a2cb7295a1f7 | 325 | res_name_ptr = SECURITY_SECURITY_MODE; |
ram54288 | 0:a2cb7295a1f7 | 326 | break; |
ram54288 | 0:a2cb7295a1f7 | 327 | case PublicKey: |
ram54288 | 0:a2cb7295a1f7 | 328 | res_name_ptr = SECURITY_PUBLIC_KEY; |
ram54288 | 0:a2cb7295a1f7 | 329 | break; |
ram54288 | 0:a2cb7295a1f7 | 330 | case ServerPublicKey: |
ram54288 | 0:a2cb7295a1f7 | 331 | res_name_ptr = SECURITY_SERVER_PUBLIC_KEY; |
ram54288 | 0:a2cb7295a1f7 | 332 | break; |
ram54288 | 0:a2cb7295a1f7 | 333 | case Secretkey: |
ram54288 | 0:a2cb7295a1f7 | 334 | res_name_ptr = SECURITY_SECRET_KEY; |
ram54288 | 0:a2cb7295a1f7 | 335 | break; |
ram54288 | 0:a2cb7295a1f7 | 336 | case SMSSecurityMode: |
ram54288 | 0:a2cb7295a1f7 | 337 | res_name_ptr = SECURITY_SMS_SECURITY_MODE; |
ram54288 | 0:a2cb7295a1f7 | 338 | break; |
ram54288 | 0:a2cb7295a1f7 | 339 | case SMSBindingKey: |
ram54288 | 0:a2cb7295a1f7 | 340 | res_name_ptr = SECURITY_SMS_BINDING_KEY; |
ram54288 | 0:a2cb7295a1f7 | 341 | break; |
ram54288 | 0:a2cb7295a1f7 | 342 | case SMSBindingSecretKey: |
ram54288 | 0:a2cb7295a1f7 | 343 | res_name_ptr = SECURITY_SMS_BINDING_SECRET_KEY; |
ram54288 | 0:a2cb7295a1f7 | 344 | break; |
ram54288 | 0:a2cb7295a1f7 | 345 | case M2MServerSMSNumber: |
ram54288 | 0:a2cb7295a1f7 | 346 | res_name_ptr = SECURITY_M2M_SERVER_SMS_NUMBER; |
ram54288 | 0:a2cb7295a1f7 | 347 | break; |
ram54288 | 0:a2cb7295a1f7 | 348 | case ShortServerID: |
ram54288 | 0:a2cb7295a1f7 | 349 | res_name_ptr = SECURITY_SHORT_SERVER_ID; |
ram54288 | 0:a2cb7295a1f7 | 350 | break; |
ram54288 | 0:a2cb7295a1f7 | 351 | case ClientHoldOffTime: |
ram54288 | 0:a2cb7295a1f7 | 352 | res_name_ptr = SECURITY_CLIENT_HOLD_OFF_TIME; |
ram54288 | 0:a2cb7295a1f7 | 353 | break; |
ram54288 | 0:a2cb7295a1f7 | 354 | } |
ram54288 | 0:a2cb7295a1f7 | 355 | |
ram54288 | 0:a2cb7295a1f7 | 356 | if (res_name_ptr) { |
ram54288 | 0:a2cb7295a1f7 | 357 | res_object = _server_instance->resource(res_name_ptr); |
ram54288 | 0:a2cb7295a1f7 | 358 | } |
ram54288 | 0:a2cb7295a1f7 | 359 | } |
ram54288 | 0:a2cb7295a1f7 | 360 | return res_object; |
ram54288 | 0:a2cb7295a1f7 | 361 | } |
ram54288 | 0:a2cb7295a1f7 | 362 | |
ram54288 | 0:a2cb7295a1f7 | 363 | void M2MSecurity::clear_resources() |
ram54288 | 0:a2cb7295a1f7 | 364 | { |
ram54288 | 0:a2cb7295a1f7 | 365 | for(int i = 0; i <= M2MSecurity::ClientHoldOffTime; i++) { |
ram54288 | 0:a2cb7295a1f7 | 366 | M2MResource *res = get_resource((SecurityResource) i); |
ram54288 | 0:a2cb7295a1f7 | 367 | if (res) { |
ram54288 | 0:a2cb7295a1f7 | 368 | res->clear_value(); |
ram54288 | 0:a2cb7295a1f7 | 369 | } |
ram54288 | 0:a2cb7295a1f7 | 370 | } |
ram54288 | 0:a2cb7295a1f7 | 371 | } |