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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "cainterface.h"
25 #include "resourcemanager.h"
26 #include "credresource.h"
27 #include "policyengine.h"
28 #include "srmutility.h"
30 #include "oic_string.h"
31 #include "oic_malloc.h"
32 #include "securevirtualresourcetypes.h"
33 #include "secureresourcemanager.h"
34 #include "srmresourcestrings.h"
35 #include "ocresourcehandler.h"
38 #include "pkix_interface.h"
43 #include "crlresource.h"
44 #endif // __WITH_X509__
46 //Request Callback handler
47 static CARequestCallback gRequestHandler = NULL;
48 //Response Callback handler
49 static CAResponseCallback gResponseHandler = NULL;
50 //Error Callback handler
51 static CAErrorCallback gErrorHandler = NULL;
52 //Persistent Storage callback handler for open/read/write/close/unlink
53 static OCPersistentStorage *gPersistentStorageHandler = NULL;
54 //Provisioning response callback
55 static SPResponseCallback gSPResponseHandler = NULL;
58 * A single global Policy Engine context will suffice as long
59 * as SRM is single-threaded.
61 PEContext_t g_policyEngineContext;
64 * Function to register provisoning API's response callback.
65 * @param respHandler response handler callback.
67 void SRMRegisterProvisioningResponseHandler(SPResponseCallback respHandler)
69 gSPResponseHandler = respHandler;
72 void SetResourceRequestType(PEContext_t *context, const char *resourceUri)
74 context->resourceType = GetSvrTypeFromUri(resourceUri);
77 static void SRMSendUnAuthorizedAccessresponse(PEContext_t *context)
79 CAResponseInfo_t responseInfo = {.result = CA_EMPTY};
81 if (NULL == context ||
82 NULL == context->amsMgrContext->requestInfo)
84 OIC_LOG_V(ERROR, TAG, "%s : NULL Parameter(s)",__func__);
88 memcpy(&responseInfo.info, &(context->amsMgrContext->requestInfo->info),
89 sizeof(responseInfo.info));
90 responseInfo.info.payload = NULL;
91 responseInfo.result = CA_UNAUTHORIZED_REQ;
92 responseInfo.info.dataType = CA_RESPONSE_DATA;
94 if (CA_STATUS_OK == CASendResponse(context->amsMgrContext->endpoint, &responseInfo))
96 OIC_LOG(DEBUG, TAG, "Succeed in sending response to a unauthorized request!");
100 OIC_LOG(ERROR, TAG, "Failed in sending response to a unauthorized request!");
104 void SRMSendResponse(SRMAccessResponse_t responseVal)
106 OIC_LOG(DEBUG, TAG, "Sending response to remote device");
108 if (IsAccessGranted(responseVal) && gRequestHandler)
110 OIC_LOG_V(INFO, TAG, "%s : Access granted. Passing Request to RI layer", __func__);
111 if (!g_policyEngineContext.amsMgrContext->endpoint ||
112 !g_policyEngineContext.amsMgrContext->requestInfo)
114 OIC_LOG_V(ERROR, TAG, "%s : Invalid arguments", __func__);
115 SRMSendUnAuthorizedAccessresponse(&g_policyEngineContext);
118 gRequestHandler(g_policyEngineContext.amsMgrContext->endpoint,
119 g_policyEngineContext.amsMgrContext->requestInfo);
123 OIC_LOG_V(INFO, TAG, "%s : ACCESS_DENIED.", __func__);
124 SRMSendUnAuthorizedAccessresponse(&g_policyEngineContext);
128 //Resetting PE state to AWAITING_REQUEST
129 SetPolicyEngineState(&g_policyEngineContext, AWAITING_REQUEST);
133 * Handle the request from the SRM.
135 * @param endPoint object from which the response is received.
136 * @param requestInfo contains information for the request.
138 void SRMRequestHandler(const CAEndpoint_t *endPoint, const CARequestInfo_t *requestInfo)
140 OIC_LOG(DEBUG, TAG, "Received request from remote device");
142 bool isRequestOverSecureChannel = false;
143 if (!endPoint || !requestInfo)
145 OIC_LOG(ERROR, TAG, "Invalid arguments");
149 // Copy the subjectID
150 OicUuid_t subjectId = {.id = {0}};
151 OicUuid_t nullSubjectId = {.id = {0}};
152 memcpy(subjectId.id, requestInfo->info.identity.id, sizeof(subjectId.id));
154 // if subject id is null that means request is sent thru coap.
155 if (memcmp(subjectId.id, nullSubjectId.id, sizeof(subjectId.id)) != 0)
157 OIC_LOG(INFO, TAG, "request over secure channel");
158 isRequestOverSecureChannel = true;
161 //Check the URI has the query and skip it before checking the permission
162 char *uri = strstr(requestInfo->info.resourceUri, "?");
166 //Skip query and pass the resource uri
167 position = uri - requestInfo->info.resourceUri;
171 position = strlen(requestInfo->info.resourceUri);
173 if (MAX_URI_LENGTH < position || 0 > position)
175 OIC_LOG(ERROR, TAG, "Incorrect URI length");
178 SRMAccessResponse_t response = ACCESS_DENIED;
179 char newUri[MAX_URI_LENGTH + 1];
180 OICStrcpyPartial(newUri, MAX_URI_LENGTH + 1, requestInfo->info.resourceUri, position);
182 SetResourceRequestType(&g_policyEngineContext, newUri);
184 // Form a 'Error', 'slow response' or 'access deny' response and send to peer
185 CAResponseInfo_t responseInfo = {.result = CA_EMPTY};
186 memcpy(&responseInfo.info, &(requestInfo->info), sizeof(responseInfo.info));
187 responseInfo.info.payload = NULL;
188 responseInfo.info.dataType = CA_RESPONSE_DATA;
190 OCResource *resPtr = FindResourceByUri(newUri);
193 // check whether request is for secure resource or not and it should not be a SVR resource
194 if (((resPtr->resourceProperties) & OC_SECURE)
195 && (g_policyEngineContext.resourceType == NOT_A_SVR_RESOURCE))
197 // if resource is secure and request is over insecure channel
198 if (!isRequestOverSecureChannel)
200 // Reject all the requests over coap for secure resource.
201 responseInfo.result = CA_FORBIDDEN_REQ;
202 if (CA_STATUS_OK != CASendResponse(endPoint, &responseInfo))
204 OIC_LOG(ERROR, TAG, "Failed in sending response to a unauthorized request!");
211 //New request are only processed if the policy engine state is AWAITING_REQUEST.
212 if (AWAITING_REQUEST == g_policyEngineContext.state)
214 OIC_LOG_V(DEBUG, TAG, "Processing request with uri, %s for method, %d",
215 requestInfo->info.resourceUri, requestInfo->method);
216 response = CheckPermission(&g_policyEngineContext, &subjectId, newUri,
217 GetPermissionFromCAMethod_t(requestInfo->method));
221 OIC_LOG_V(INFO, TAG, "PE state %d. Ignoring request with uri, %s for method, %d",
222 g_policyEngineContext.state, requestInfo->info.resourceUri, requestInfo->method);
225 if (IsAccessGranted(response) && gRequestHandler)
227 gRequestHandler(endPoint, requestInfo);
231 VERIFY_NON_NULL(TAG, gRequestHandler, ERROR);
233 if (ACCESS_WAITING_FOR_AMS == response)
235 OIC_LOG(INFO, TAG, "Sending slow response");
237 UpdateAmsMgrContext(&g_policyEngineContext, endPoint, requestInfo);
238 responseInfo.result = CA_EMPTY;
239 responseInfo.info.type = CA_MSG_ACKNOWLEDGE;
244 * TODO Enhance this logic more to decide between
245 * CA_UNAUTHORIZED_REQ or CA_FORBIDDEN_REQ depending
246 * upon SRMAccessResponseReasonCode_t
248 OIC_LOG(INFO, TAG, "Sending for regular response");
249 responseInfo.result = CA_UNAUTHORIZED_REQ;
252 if (CA_STATUS_OK != CASendResponse(endPoint, &responseInfo))
254 OIC_LOG(ERROR, TAG, "Failed in sending response to a unauthorized request!");
258 responseInfo.result = CA_INTERNAL_SERVER_ERROR;
259 if (CA_STATUS_OK != CASendResponse(endPoint, &responseInfo))
261 OIC_LOG(ERROR, TAG, "Failed in sending response to a unauthorized request!");
266 * Handle the response from the SRM.
268 * @param endPoint points to the remote endpoint.
269 * @param responseInfo contains response information from the endpoint.
271 void SRMResponseHandler(const CAEndpoint_t *endPoint, const CAResponseInfo_t *responseInfo)
273 OIC_LOG(DEBUG, TAG, "Received response from remote device");
275 // isProvResponse flag is to check whether response is catered by provisioning APIs or not.
276 // When token sent by CA response matches with token generated by provisioning request,
277 // gSPResponseHandler returns true and response is not sent to RI layer. In case
278 // gSPResponseHandler is null and isProvResponse is false response then the response is for
280 bool isProvResponse = false;
282 if (gSPResponseHandler)
284 isProvResponse = gSPResponseHandler(endPoint, responseInfo);
286 if (!isProvResponse && gResponseHandler)
288 gResponseHandler(endPoint, responseInfo);
293 * Handle the error from the SRM.
295 * @param endPoint is the remote endpoint.
296 * @param errorInfo contains error information from the endpoint.
298 void SRMErrorHandler(const CAEndpoint_t *endPoint, const CAErrorInfo_t *errorInfo)
300 OIC_LOG_V(INFO, TAG, "Received error from remote device with result, %d for request uri, %s",
301 errorInfo->result, errorInfo->info.resourceUri);
304 gErrorHandler(endPoint, errorInfo);
308 OCStackResult SRMRegisterHandler(CARequestCallback reqHandler,
309 CAResponseCallback respHandler,
310 CAErrorCallback errHandler)
312 OIC_LOG(DEBUG, TAG, "SRMRegisterHandler !!");
313 if( !reqHandler || !respHandler || !errHandler)
315 OIC_LOG(ERROR, TAG, "Callback handlers are invalid");
316 return OC_STACK_INVALID_PARAM;
318 gRequestHandler = reqHandler;
319 gResponseHandler = respHandler;
320 gErrorHandler = errHandler;
323 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
324 CARegisterHandler(SRMRequestHandler, SRMResponseHandler, SRMErrorHandler);
326 CARegisterHandler(reqHandler, respHandler, errHandler);
327 #endif /* __WITH_DTLS__ */
331 OCStackResult SRMRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler)
333 OIC_LOG(DEBUG, TAG, "SRMRegisterPersistentStorageHandler !!");
334 if(!persistentStorageHandler)
336 OIC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
337 return OC_STACK_INVALID_PARAM;
339 gPersistentStorageHandler = persistentStorageHandler;
343 OCPersistentStorage* SRMGetPersistentStorageHandler()
345 return gPersistentStorageHandler;
348 OCStackResult SRMInitSecureResources()
350 // TODO: temporarily returning OC_STACK_OK every time until default
351 // behavior (for when SVR DB is missing) is settled.
352 InitSecureResources();
353 OCStackResult ret = OC_STACK_OK;
354 #if defined(__WITH_DTLS__)
355 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
357 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
358 ret = OC_STACK_ERROR;
362 if (CA_STATUS_OK != CAregisterTlsCredentialsHandler(GetDtlsPskCredentials))
364 OIC_LOG(ERROR, TAG, "Failed to revert TLS credential handler.");
365 ret = OC_STACK_ERROR;
367 CAregisterPkixInfoHandler(GetPkixInfo);
368 CAregisterGetCredentialTypesHandler(InitCipherSuiteList);
370 #if defined(__WITH_X509__)
371 CARegisterDTLSX509CredentialsHandler(GetDtlsX509Credentials);
372 CARegisterDTLSCrlHandler(GetDerCrl);
373 #endif // (__WITH_X509__)
378 void SRMDeInitSecureResources()
380 DestroySecureResources();
383 OCStackResult SRMInitPolicyEngine()
385 return InitPolicyEngine(&g_policyEngineContext);
388 void SRMDeInitPolicyEngine()
390 DeInitPolicyEngine(&g_policyEngineContext);
393 bool SRMIsSecurityResourceURI(const char* uri)
400 const char *rsrcs[] = {
409 OIC_RSRC_DPAIRING_URI,
411 OC_RSRVD_PROV_CRL_URL
414 // Remove query from Uri for resource string comparison
415 size_t uriLen = strlen(uri);
416 char *query = strchr (uri, '?');
419 uriLen = query - uri;
422 for (size_t i = 0; i < sizeof(rsrcs)/sizeof(rsrcs[0]); i++)
424 size_t svrLen = strlen(rsrcs[i]);
426 if ((uriLen == svrLen) &&
427 (strncmp(uri, rsrcs[i], svrLen) == 0))
437 * Get the Secure Virtual Resource (SVR) type from the URI.
438 * @param uri [IN] Pointer to URI in question.
439 * @return The OicSecSvrType_t of the URI passed (note: if not a Secure Virtual
440 Resource, e.g. /a/light, will return "NOT_A_SVR_TYPE" enum value)
442 static const char URI_QUERY_CHAR = '?';
443 OicSecSvrType_t GetSvrTypeFromUri(const char* uri)
447 return NOT_A_SVR_RESOURCE;
450 // Remove query from Uri for resource string comparison
451 size_t uriLen = strlen(uri);
452 char *query = strchr (uri, URI_QUERY_CHAR);
455 uriLen = query - uri;
460 svrLen = strlen(OIC_RSRC_ACL_URI);
463 if(0 == strncmp(uri, OIC_RSRC_ACL_URI, svrLen))
465 return OIC_R_ACL_TYPE;
469 svrLen = strlen(OIC_RSRC_AMACL_URI);
472 if(0 == strncmp(uri, OIC_RSRC_AMACL_URI, svrLen))
474 return OIC_R_AMACL_TYPE;
478 svrLen = strlen(OIC_RSRC_CRED_URI);
481 if(0 == strncmp(uri, OIC_RSRC_CRED_URI, svrLen))
483 return OIC_R_CRED_TYPE;
487 svrLen = strlen(OIC_RSRC_CRL_URI);
490 if(0 == strncmp(uri, OIC_RSRC_CRL_URI, svrLen))
492 return OIC_R_CRL_TYPE;
496 svrLen = strlen(OIC_RSRC_DOXM_URI);
499 if(0 == strncmp(uri, OIC_RSRC_DOXM_URI, svrLen))
501 return OIC_R_DOXM_TYPE;
505 svrLen = strlen(OIC_RSRC_DPAIRING_URI);
508 if(0 == strncmp(uri, OIC_RSRC_DPAIRING_URI, svrLen))
510 return OIC_R_DPAIRING_TYPE;
514 svrLen = strlen(OIC_RSRC_PCONF_URI);
517 if(0 == strncmp(uri, OIC_RSRC_PCONF_URI, svrLen))
519 return OIC_R_PCONF_TYPE;
523 svrLen = strlen(OIC_RSRC_PSTAT_URI);
526 if(0 == strncmp(uri, OIC_RSRC_PSTAT_URI, svrLen))
528 return OIC_R_PSTAT_TYPE;
532 svrLen = strlen(OIC_RSRC_SVC_URI);
535 if(0 == strncmp(uri, OIC_RSRC_SVC_URI, svrLen))
537 return OIC_R_SVC_TYPE;
541 svrLen = strlen(OIC_RSRC_SACL_URI);
544 if(0 == strncmp(uri, OIC_RSRC_SACL_URI, svrLen))
546 return OIC_R_SACL_TYPE;
550 return NOT_A_SVR_RESOURCE;