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