1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "oic_malloc.h"
24 #include "resourcemanager.h"
25 #include "securevirtualresourcetypes.h"
26 #include "srmresourcestrings.h"
29 #include "aclresource.h"
30 #include "amaclresource.h"
31 #include "srmutility.h"
33 #include "secureresourcemanager.h"
34 #include "doxmresource.h"
35 #include "policyengine.h"
36 #include "oic_string.h"
37 #include "caremotehandler.h"
39 #define TAG "SRM-AMSMGR"
41 //Callback for AMS service multicast discovery request.
42 static OCStackApplicationResult AmsMgrDiscoveryCallback(void *ctx, OCDoHandle handle,
43 OCClientResponse * clientResponse);
45 //Callback for unicast secured port discovery request.
46 static OCStackApplicationResult SecurePortDiscoveryCallback(void *ctx, OCDoHandle handle,
47 OCClientResponse * clientResponse);
49 //Callback for unicast ACL request
50 static OCStackApplicationResult AmsMgrAclReqCallback(void *ctx, OCDoHandle handle,
51 OCClientResponse * clientResponse);
53 OCStackResult DiscoverAmsService(PEContext_t *context)
55 OIC_LOG(INFO, TAG, "IN DiscoverAmsService");
57 OCStackResult ret = OC_STACK_ERROR;
58 const char DOXM_DEVICEID_QUERY_FMT[] = "%s?%s=%s";
59 char uri[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {};
60 OCCallbackData cbData = {.context=NULL};
62 VERIFY_NON_NULL(TAG, context, ERROR);
63 snprintf(uri, sizeof(uri), DOXM_DEVICEID_QUERY_FMT, OIC_RSRC_DOXM_URI,
64 OIC_JSON_DEVICE_ID_NAME,
65 context->amsMgrContext->amsDeviceId.id);
67 cbData.cb = &AmsMgrDiscoveryCallback;
68 cbData.context = (void*)context;
71 * If no good response was received for this discovery request,
72 * PE would be blocked forever waiting for AMS service to respond with the ACE.
73 * Need logic to reset the PE state and send ACCESS_DENIED response,
74 * when discovery response from AMS service is not received within certain time.
76 OIC_LOG_V(INFO, TAG,"AMS Manager Sending Multicast Discovery with URI = %s", uri);
77 ret = OCDoResource(NULL, OC_REST_DISCOVER, uri, NULL, NULL,
78 CT_DEFAULT, OC_LOW_QOS, &cbData, NULL, 0);
81 OIC_LOG(INFO, TAG, "Leaving DiscoverAmsService");
85 static OCStackApplicationResult AmsMgrDiscoveryCallback(void *ctx, OCDoHandle handle,
86 OCClientResponse * clientResponse)
88 OIC_LOG_V(INFO, TAG, "%s Begin", __func__ );
92 !clientResponse->payload||
93 (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)||
94 (OC_STACK_OK != clientResponse->result))
96 OIC_LOG_V(ERROR, TAG, "%s Invalid Response ", __func__);
97 return OC_STACK_KEEP_TRANSACTION;
101 PEContext_t *context = (PEContext_t *) ctx;
102 if (context->state != AWAITING_AMS_RESPONSE)
104 OIC_LOG_V(ERROR, TAG, "%s Invalid PE State ", __func__);
105 return OC_STACK_DELETE_TRANSACTION;
108 OicSecDoxm_t *doxm = NULL;
110 OIC_LOG_V(INFO, TAG, "Doxm DeviceId Discovery response = %s\n",
111 ((OCSecurityPayload*)clientResponse->payload)->securityData);
112 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
113 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
115 //As doxm is NULL amsmgr can't test if response from trusted AMS service
116 //so keep the transaction.
117 if (OC_STACK_OK == CBORPayloadToDoxm(payload, size, &doxm))
119 OIC_LOG_V(ERROR, TAG, "%s : Unable to convert CBOR to Binary",__func__);
120 return OC_STACK_KEEP_TRANSACTION;
123 OicUuid_t deviceId = {.id={}};
124 memcpy(&deviceId, &doxm->deviceID, sizeof(deviceId));
127 /* TODO : By assuming that the first response received is the actual
128 * AMS service, a 'bad device' can cause DoS attack.
130 if (memcmp(&context->amsMgrContext->amsDeviceId, &deviceId,
131 sizeof(context->amsMgrContext->amsDeviceId)) == 0)
133 OIC_LOG(INFO, TAG, "AMS Manager Sending unicast discovery to get secured port info");
134 //Sending Unicast discovery to get secure port information
135 if (OC_STACK_OK == SendUnicastSecurePortDiscovery(context, &clientResponse->devAddr,
136 clientResponse->connType))
138 context->retVal = ACCESS_WAITING_FOR_AMS;
139 return OC_STACK_DELETE_TRANSACTION;
142 context->retVal = ACCESS_DENIED_AMS_SERVICE_ERROR;
143 SRMSendResponse(context->retVal);
144 return OC_STACK_DELETE_TRANSACTION;
147 OCStackResult SendUnicastSecurePortDiscovery(PEContext_t *context,OCDevAddr *devAddr,
148 OCConnectivityType connType)
150 OIC_LOG(INFO, TAG, "IN SendUnicastSecurePortDiscovery");
152 const char RES_DOXM_QUERY_FMT[] = "%s?%s=%s";
153 OCCallbackData cbData = {.context=NULL};
154 char uri[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {};
155 snprintf(uri, sizeof(uri), RES_DOXM_QUERY_FMT, OC_RSRVD_WELL_KNOWN_URI,
156 OC_RSRVD_RESOURCE_TYPE, OIC_RSRC_TYPE_SEC_DOXM);
158 cbData.cb = &SecurePortDiscoveryCallback;
159 cbData.context = context;
161 OIC_LOG_V(INFO, TAG, "AMS Manager Sending Unicast Discovery with URI = %s", uri);
163 return OCDoResource(NULL, OC_REST_DISCOVER, uri, devAddr, NULL,
164 connType, OC_LOW_QOS, &cbData, NULL, 0);
167 static OCStackApplicationResult SecurePortDiscoveryCallback(void *ctx, OCDoHandle handle,
168 OCClientResponse * clientResponse)
170 OIC_LOG(INFO, TAG, "In SecurePortDiscoveryCallback");
174 !clientResponse->payload||
175 (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)||
176 (OC_STACK_OK != clientResponse->result))
178 OIC_LOG_V(ERROR, TAG, "%s Invalid Response ", __func__);
179 SRMSendResponse(ACCESS_DENIED_AMS_SERVICE_ERROR);
180 return OC_STACK_DELETE_TRANSACTION;
183 PEContext_t *context = (PEContext_t *) ctx;
186 if (context->state != AWAITING_AMS_RESPONSE)
188 OIC_LOG_V(ERROR, TAG, "%s Invalid PE State ", __func__);
189 context->retVal = ACCESS_DENIED_AMS_SERVICE_ERROR;
190 SRMSendResponse(context->retVal);
191 return OC_STACK_DELETE_TRANSACTION;
194 OCResourcePayload *resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
196 //Verifying if the ID of the sender is an AMS service that this device trusts.
198 memcmp(context->amsMgrContext->amsDeviceId.id,
199 ((OCDiscoveryPayload*)clientResponse->payload)->sid,
201 sizeof(context->amsMgrContext->amsDeviceId.id)) != 0)
203 OIC_LOG_V(ERROR, TAG, "%s Invalid AMS device", __func__);
204 context->retVal = ACCESS_DENIED_AMS_SERVICE_ERROR;
205 SRMSendResponse(context->retVal);
206 return OC_STACK_DELETE_TRANSACTION;
209 if (resPayload && resPayload->secure)
211 if(OC_STACK_OK == SendAclReq(context, &clientResponse->devAddr, clientResponse->connType,
214 return OC_STACK_DELETE_TRANSACTION;
217 OIC_LOG(INFO, TAG, "Can not find secure port information");
219 context->retVal = ACCESS_DENIED_AMS_SERVICE_ERROR;
220 SRMSendResponse(context->retVal);
221 return OC_STACK_DELETE_TRANSACTION;
224 OCStackResult SendAclReq(PEContext_t *context, OCDevAddr *devAddr, OCConnectivityType connType,
225 uint16_t securedPort)
227 OCStackResult ret = OC_STACK_ERROR;
228 const char GET_ACE_QUERY_FMT[] = "%s?%s=%s;%s=%s";
229 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {};
231 char uri[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {};
232 OCCallbackData cbData = {.context=NULL};
233 OCDevAddr destAddr = {.adapter = OC_ADAPTER_IP};
237 VERIFY_NON_NULL(TAG, context, ERROR);
238 VERIFY_NON_NULL(TAG, devAddr, ERROR);
240 ret = ConvertUuidToStr(&context->subject, &subID);
241 if(OC_STACK_OK != ret)
243 OIC_LOG(ERROR, TAG, "SendAclReq : Failed to canonical UUID encoding");
244 return OC_STACK_ERROR;
247 snprintf(uri, sizeof(uri), GET_ACE_QUERY_FMT, OIC_RSRC_ACL_URI,
248 OIC_JSON_SUBJECTID_NAME, subID,
249 OIC_JSON_RESOURCES_NAME, context->resource);
252 cbData.cb = &AmsMgrAclReqCallback;
253 cbData.context = context;
257 destAddr.flags = (OCTransportFlags)(destAddr.flags | OC_FLAG_SECURE);
258 destAddr.port = securedPort;
260 OIC_LOG_V(INFO, TAG, "AMS Manager Sending Unicast ACL request with URI = %s", uri);
261 ret = OCDoResource(NULL, OC_REST_GET, uri, &destAddr, NULL,
262 connType, OC_LOW_QOS, &cbData, NULL, 0);
265 OIC_LOG_V(INFO, TAG, "%s returns %d ", __func__, ret);
269 static OCStackApplicationResult AmsMgrAclReqCallback(void *ctx, OCDoHandle handle,
270 OCClientResponse * clientResponse)
272 OIC_LOG_V(INFO, TAG, "%s Begin", __func__ );
275 PEContext_t *context = (PEContext_t *) ctx;
276 SRMAccessResponse_t rsps;
280 !clientResponse->payload||
281 (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type) ||
282 (clientResponse->result != OC_STACK_OK))
284 OIC_LOG_V(ERROR, TAG, "%s Invalid Response ", __func__);
285 SRMSendResponse(ACCESS_DENIED_AMS_SERVICE_ERROR);
286 return OC_STACK_DELETE_TRANSACTION;
289 if (context->state != AWAITING_AMS_RESPONSE)
291 OIC_LOG_V(ERROR, TAG, "%s Invalid State ", __func__);
292 context->retVal = ACCESS_DENIED_AMS_SERVICE_ERROR;
293 SRMSendResponse(context->retVal);
294 return OC_STACK_DELETE_TRANSACTION;
297 // Verify before installing ACL if the ID of the sender of this ACL is an AMS
298 //service that this device trusts.
299 rsps = ACCESS_DENIED;
300 if((UUID_LENGTH == clientResponse->identity.id_length) &&
301 memcmp(context->amsMgrContext->amsDeviceId.id, clientResponse->identity.id,
302 sizeof(context->amsMgrContext->amsDeviceId.id)) == 0)
304 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
306 InstallNewACL(((OCSecurityPayload*)clientResponse->payload)->securityData, size);
307 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
309 OIC_LOG_V(INFO, TAG, "%s : Calling checkPermission", __func__);
310 rsps = CheckPermission(context, &context->subject, context->resource, context->permission);
311 VERIFY_SUCCESS(TAG, (true == IsAccessGranted(rsps)), ERROR);
313 OIC_LOG_V(INFO, TAG, "%sAccess granted, Calling SRMCallCARequestHandler", __func__);
314 context->retVal = ACCESS_GRANTED;
315 SRMSendResponse(context->retVal);
316 return OC_STACK_DELETE_TRANSACTION;
320 context->retVal = ACCESS_DENIED_AMS_SERVICE_ERROR;
321 SRMSendResponse(context->retVal);
322 return OC_STACK_DELETE_TRANSACTION;
325 OCStackResult UpdateAmsMgrContext(PEContext_t *context, const CAEndpoint_t *endpoint,
326 const CARequestInfo_t *requestInfo)
328 OCStackResult ret = OC_STACK_ERROR;
330 if (!context->amsMgrContext)
335 //The AmsMgr context endpoint and requestInfo will be free from ,
336 //AmsMgrAclReqCallback function
337 if (context->amsMgrContext->endpoint)
339 OICFree(context->amsMgrContext->endpoint);
340 context->amsMgrContext->endpoint = NULL;
342 context->amsMgrContext->endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t ));
343 VERIFY_NON_NULL(TAG, context->amsMgrContext->endpoint, ERROR);
344 *context->amsMgrContext->endpoint = *endpoint;
346 if (context->amsMgrContext->requestInfo)
348 FreeCARequestInfo(context->amsMgrContext->requestInfo);
349 context->amsMgrContext->requestInfo = NULL;
351 context->amsMgrContext->requestInfo = CACloneRequestInfo(requestInfo);
352 VERIFY_NON_NULL(TAG, context->amsMgrContext->requestInfo, ERROR);
358 void FreeCARequestInfo(CARequestInfo_t *requestInfo)
360 if (NULL == requestInfo)
362 OIC_LOG_V(DEBUG, TAG, "%s: Can't free memory. Received NULL requestInfo", __func__);
365 OICFree(requestInfo->info.token);
366 OICFree(requestInfo->info.options);
367 OICFree(requestInfo->info.payload);
368 OICFree(requestInfo->info.resourceUri);
369 OICFree(requestInfo);
373 //This method checks for Amacl resource. If Amacl is found then it fills up
374 //context->amsMgrContext->amsDeviceId with amsID of the Amacl else leaves it empty.
375 bool FoundAmaclForRequest(PEContext_t *context)
377 OIC_LOG_V(INFO, TAG, "%s:no ACL found. Searching for AMACL",__func__);
380 VERIFY_NON_NULL(TAG, context, ERROR);
381 memset(&context->amsMgrContext->amsDeviceId, 0, sizeof(context->amsMgrContext->amsDeviceId));
383 //Call amacl resource function to get the AMS service deviceID for the resource
384 if (OC_STACK_OK == AmaclGetAmsDeviceId(context->resource, &context->amsMgrContext->amsDeviceId))
386 OIC_LOG_V(INFO, TAG, "%s:AMACL found for the requested resource %s",
387 __func__, context->resource);
392 OIC_LOG_V(INFO, TAG, "%s:AMACL found for the requested resource %s",
393 __func__, context->resource);
401 void ProcessAMSRequest(PEContext_t *context)
403 OicUuid_t emptyUuid = {.id={}};
404 OIC_LOG_V(INFO, TAG, "Entering %s", __func__);
407 if((false == context->matchingAclFound) && (false == context->amsProcessing))
409 context->amsProcessing = true;
411 //Checking if context AMS deviceId is empty
412 if(memcmp(&context->amsMgrContext->amsDeviceId, &emptyUuid, sizeof(OicUuid_t)) != 0 )
414 if(OC_STACK_OK == DiscoverAmsService(context))
416 context->retVal = ACCESS_WAITING_FOR_AMS;
417 OIC_LOG_V(INFO, TAG, "Leaving %s(WAITING_FOR_AMS)", __func__);
418 context->state = AWAITING_AMS_RESPONSE;
422 context->retVal = ACCESS_DENIED_AMS_SERVICE_ERROR;
429 OIC_LOG_V(INFO, TAG, "Leaving %s(context is NULL)", __func__);