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"
23 #include "policyengine.h"
25 #include "resourcemanager.h"
26 #include "securevirtualresourcetypes.h"
27 #include "srmresourcestrings.h"
29 #include "aclresource.h"
30 #include "srmutility.h"
31 #include "doxmresource.h"
32 #include "iotvticalendar.h"
33 #include "pstatresource.h"
34 #include "dpairingresource.h"
35 #include "pconfresource.h"
36 #include "amaclresource.h"
37 #include "credresource.h"
41 uint16_t GetPermissionFromCAMethod_t(const CAMethod_t method)
47 perm = (uint16_t)PERMISSION_READ;
49 case CA_POST: // For now we treat all PUT & POST as Write
50 case CA_PUT: // because we don't know if resource exists yet.
51 perm = (uint16_t)PERMISSION_WRITE;
54 perm = (uint16_t)PERMISSION_DELETE;
56 default: // if not recognized, must assume requesting full control
57 perm = (uint16_t)PERMISSION_FULL_CONTROL;
64 * Compares two OicUuid_t structs.
66 * @return true if the two OicUuid_t structs are equal, else false.
68 static bool UuidCmp(OicUuid_t *firstId, OicUuid_t *secondId)
70 // TODO use VERIFY macros to check for null when they are merged.
71 if(NULL == firstId || NULL == secondId)
75 // Check empty uuid string
76 if('\0' == firstId->id[0] || '\0' == secondId->id[0])
80 for(int i = 0; i < UUID_LENGTH; i++)
82 if(firstId->id[i] != secondId->id[i])
90 void SetPolicyEngineState(PEContext_t *context, const PEState_t state)
97 // Clear stateful context variables.
98 memset(&context->subject, 0, sizeof(context->subject));
99 memset(&context->resource, 0, sizeof(context->resource));
100 context->permission = 0x0;
101 context->matchingAclFound = false;
102 context->amsProcessing = false;
103 context->retVal = ACCESS_DENIED_POLICY_ENGINE_ERROR;
105 FreeCARequestInfo(context->amsMgrContext->requestInfo);
106 OICFree(context->amsMgrContext->endpoint);
107 memset(context->amsMgrContext, 0, sizeof(AmsMgrContext_t));
110 context->state = state;
114 * Compare the request's subject to DevOwner.
116 * @return true if context->subjectId == GetDoxmDevOwner(), else false.
118 static bool IsRequestFromDevOwner(PEContext_t *context)
128 if(OC_STACK_OK == GetDoxmDevOwnerId(&ownerid))
130 retVal = UuidCmp(&context->subject, &ownerid);
136 // TODO - remove these function placeholders as they are implemented
137 // in the resource entity handler code.
138 // Note that because many SVRs do not have a rowner, in those cases we
139 // just return "OC_STACK_ERROR" which results in a "false" return by
140 // IsRequestFromResourceOwner().
141 // As these SVRs are revised to have a rowner, these functions should be
142 // replaced (see pstatresource.c for example of GetPstatRownerId).
144 OCStackResult GetCrlRownerId(OicUuid_t *rowner)
147 return OC_STACK_ERROR;
150 OCStackResult GetSaclRownerId(OicUuid_t *rowner)
153 return OC_STACK_ERROR;
156 OCStackResult GetSvcRownerId(OicUuid_t *rowner)
159 return OC_STACK_ERROR;
162 static GetSvrRownerId_t GetSvrRownerId[OIC_SEC_SVR_TYPE_COUNT] = {
176 * Compare the request's subject to resource.ROwner.
178 * @return true if context->subjectId equals SVR rowner id, else return false
180 bool IsRequestFromResourceOwner(PEContext_t *context)
183 OicUuid_t resourceOwner;
190 if((OIC_R_ACL_TYPE <= context->resourceType) && \
191 (OIC_SEC_SVR_TYPE_COUNT > context->resourceType))
193 if(OC_STACK_OK == GetSvrRownerId[(int)context->resourceType](&resourceOwner))
195 retVal = UuidCmp(&context->subject, &resourceOwner);
201 OIC_LOG(INFO, TAG, "PE.IsRequestFromResourceOwner(): returning true");
205 OIC_LOG(INFO, TAG, "PE.IsRequestFromResourceOwner(): returning false");
211 inline static bool IsRequestSubjectEmpty(PEContext_t *context)
213 OicUuid_t emptySubject = {.id={}};
220 return (memcmp(&context->subject, &emptySubject, sizeof(OicUuid_t)) == 0) ?
225 * Bitwise check to see if 'permission' contains 'request'.
227 * @param permission is the allowed CRUDN permission.
228 * @param request is the CRUDN permission being requested.
230 * @return true if 'permission' bits include all 'request' bits.
232 static inline bool IsPermissionAllowingRequest(const uint16_t permission,
233 const uint16_t request)
235 if (request == (request & permission))
246 * Compare the passed subject to the wildcard (aka anonymous) subjectId.
248 * @return true if 'subject' is the wildcard, false if it is not.
250 static inline bool IsWildCardSubject(OicUuid_t *subject)
257 // Because always comparing to string literal, use strcmp()
258 if(0 == memcmp(subject, &WILDCARD_SUBJECT_ID, sizeof(OicUuid_t)))
269 * Copy the subject, resource and permission into the context fields.
271 static void CopyParamsToContext(PEContext_t *context,
272 const OicUuid_t *subjectId,
273 const char *resource,
274 const uint16_t requestedPermission)
278 if (NULL == context || NULL == subjectId || NULL == resource)
283 memcpy(&context->subject, subjectId, sizeof(OicUuid_t));
285 // Copy the resource string into context.
286 length = strlen(resource) + 1;
289 strncpy(context->resource, resource, length);
290 context->resource[length - 1] = '\0';
293 // Assign the permission field.
294 context->permission = requestedPermission;
298 * Check whether 'resource' is getting accessed within the valid time period.
300 * @param acl is the ACL to check.
302 * @return true if access is within valid time period or if the period or recurrence is not present.
303 * false if period and recurrence present and the access is not within valid time period.
305 static bool IsAccessWithinValidTime(const OicSecAcl_t *acl)
307 #ifndef WITH_ARDUINO //Period & Recurrence not supported on Arduino due
308 //lack of absolute time
309 if (NULL== acl || NULL == acl->periods || 0 == acl->prdRecrLen)
314 //periods & recurrences rules are paired.
315 if (NULL == acl->recurrences)
320 for (size_t i = 0; i < acl->prdRecrLen; i++)
322 if (IOTVTICAL_VALID_ACCESS == IsRequestWithinValidTime(acl->periods[i],
323 acl->recurrences[i]))
325 OIC_LOG(INFO, TAG, "Access request is in allowed time period");
329 OIC_LOG(ERROR, TAG, "Access request is in invalid time period");
338 * Check whether 'resource' is in the passed ACL.
340 * @param resource is the resource being searched.
341 * @param acl is the ACL to check.
343 * @return true if 'resource' found, otherwise false.
345 static bool IsResourceInAcl(const char *resource, const OicSecAcl_t *acl)
347 if (NULL== acl || NULL == resource)
352 for (size_t n = 0; n < acl->resourcesLen; n++)
354 if (0 == strcmp(resource, acl->resources[n]) || // TODO null terms?
355 0 == strcmp(WILDCARD_RESOURCE_URI, acl->resources[n]))
365 * Find ACLs containing context->subject.
366 * Search each ACL for requested resource.
367 * If resource found, check for context->permission and period validity.
368 * If the ACL is not found locally and AMACL for the resource is found
369 * then sends the request to AMS service for the ACL.
370 * Set context->retVal to result from first ACL found which contains
371 * correct subject AND resource.
373 static void ProcessAccessRequest(PEContext_t *context)
375 OIC_LOG(DEBUG, TAG, "Entering ProcessAccessRequest()");
378 const OicSecAcl_t *currentAcl = NULL;
379 OicSecAcl_t *savePtr = NULL;
381 // Start out assuming subject not found.
382 context->retVal = ACCESS_DENIED_SUBJECT_NOT_FOUND;
384 // Loop through all ACLs with a matching Subject searching for the right
385 // ACL for this request.
388 OIC_LOG_V(DEBUG, TAG, "%s: getting ACL..." ,__func__);
389 currentAcl = GetACLResourceData(&context->subject, &savePtr);
391 if (NULL != currentAcl)
393 // Found the subject, so how about resource?
394 OIC_LOG_V(DEBUG, TAG, "%s:found ACL matching subject" ,__func__);
396 // Subject was found, so err changes to Rsrc not found for now.
397 context->retVal = ACCESS_DENIED_RESOURCE_NOT_FOUND;
398 OIC_LOG_V(DEBUG, TAG, "%s:Searching for resource..." ,__func__);
399 if (IsResourceInAcl(context->resource, currentAcl))
401 OIC_LOG_V(INFO, TAG, "%s:found matching resource in ACL" ,__func__);
402 context->matchingAclFound = true;
404 // Found the resource, so it's down to valid period & permission.
405 context->retVal = ACCESS_DENIED_INVALID_PERIOD;
406 if (IsAccessWithinValidTime(currentAcl))
408 context->retVal = ACCESS_DENIED_INSUFFICIENT_PERMISSION;
409 if (IsPermissionAllowingRequest(currentAcl->permission, context->permission))
411 context->retVal = ACCESS_GRANTED;
418 OIC_LOG_V(INFO, TAG, "%s:no ACL found matching subject for resource %s",__func__, context->resource);
420 } while ((NULL != currentAcl) && (false == context->matchingAclFound));
422 if (IsAccessGranted(context->retVal))
424 OIC_LOG_V(INFO, TAG, "%s:Leaving ProcessAccessRequest(ACCESS_GRANTED)", __func__);
428 OIC_LOG_V(INFO, TAG, "%s:Leaving ProcessAccessRequest(ACCESS_DENIED)", __func__);
433 OIC_LOG_V(ERROR, TAG, "%s:Leaving ProcessAccessRequest(context is NULL)", __func__);
437 SRMAccessResponse_t CheckPermission(PEContext_t *context,
438 const OicUuid_t *subjectId,
439 const char *resource,
440 const uint16_t requestedPermission)
442 SRMAccessResponse_t retVal = ACCESS_DENIED_POLICY_ENGINE_ERROR;
444 VERIFY_NON_NULL(TAG, context, ERROR);
445 VERIFY_NON_NULL(TAG, subjectId, ERROR);
446 VERIFY_NON_NULL(TAG, resource, ERROR);
448 // Each state machine context can only be processing one request at a time.
449 // Therefore if the context is not in AWAITING_REQUEST or AWAITING_AMS_RESPONSE
450 // state, return error. Otherwise, change to BUSY state and begin processing request.
451 if (AWAITING_REQUEST == context->state || AWAITING_AMS_RESPONSE == context->state)
453 if (AWAITING_REQUEST == context->state)
455 SetPolicyEngineState(context, BUSY);
456 CopyParamsToContext(context, subjectId, resource, requestedPermission);
459 // Before doing any processing, check if request coming
460 // from DevOwner and if so, always GRANT.
461 if (IsRequestFromDevOwner(context))
463 context->retVal = ACCESS_GRANTED;
465 // Then check if request is for a SVR and coming from rowner
466 else if (IsRequestFromResourceOwner(context))
468 context->retVal = ACCESS_GRANTED;
470 // Else request is a "normal" request that must be tested against ACL
473 OicUuid_t saveSubject = {.id={}};
474 bool isSubEmpty = IsRequestSubjectEmpty(context);
476 ProcessAccessRequest(context);
478 // If matching ACL not found, and subject != wildcard, try wildcard.
479 if ((false == context->matchingAclFound) && \
480 (false == IsWildCardSubject(&context->subject)))
482 //Saving subject for Amacl check
483 memcpy(&saveSubject, &context->subject,sizeof(OicUuid_t));
485 //Setting context subject to WILDCARD_SUBJECT_ID
486 //TODO: change ProcessAccessRequest method signature to
487 //ProcessAccessRequest(context, subject) so that context
488 //subject is not tempered.
489 memset(&context->subject, 0, sizeof(context->subject));
490 memcpy(&context->subject, &WILDCARD_SUBJECT_ID,sizeof(OicUuid_t));
491 ProcessAccessRequest(context); // TODO anonymous subj can result
492 // in confusing err code return.
495 //No local ACE found for the request so checking Amacl resource
496 if (ACCESS_GRANTED != context->retVal)
498 //If subject is not empty then restore the original subject
499 //else keep the subject to WILDCARD_SUBJECT_ID
502 memcpy(&context->subject, &saveSubject, sizeof(OicUuid_t));
505 //FoundAmaclForRequest method checks for Amacl and fills up
506 //context->amsMgrContext->amsDeviceId with the AMS deviceId
507 //if Amacl was found for the requested resource.
508 if(FoundAmaclForRequest(context))
510 ProcessAMSRequest(context);
517 context->retVal = ACCESS_DENIED_POLICY_ENGINE_ERROR;
520 // Capture retVal before resetting state for next request.
521 retVal = context->retVal;
523 if (!context->amsProcessing)
525 OIC_LOG(INFO, TAG, "Resetting PE context and PE State to AWAITING_REQUEST");
526 SetPolicyEngineState(context, AWAITING_REQUEST);
533 OCStackResult InitPolicyEngine(PEContext_t *context)
537 return OC_STACK_ERROR;
540 context->amsMgrContext = (AmsMgrContext_t *)OICCalloc(1, sizeof(AmsMgrContext_t));
541 if(NULL == context->amsMgrContext)
543 return OC_STACK_ERROR;
546 SetPolicyEngineState(context, AWAITING_REQUEST);
550 void DeInitPolicyEngine(PEContext_t *context)
554 SetPolicyEngineState(context, STOPPED);
555 OICFree(context->amsMgrContext);