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 "secureresourcemanager.h"
26 #include "resourcemanager.h"
27 #include "credresource.h"
28 #include "policyengine.h"
29 #include "oic_string.h"
30 #include "srmresourcestrings.h"
34 //Request Callback handler
35 static CARequestCallback gRequestHandler = NULL;
36 //Response Callback handler
37 static CAResponseCallback gResponseHandler = NULL;
38 //Error Callback handler
39 static CAErrorCallback gErrorHandler = NULL;
40 //Persistent Storage callback handler for open/read/write/close/unlink
41 static OCPersistentStorage *gPersistentStorageHandler = NULL;
42 //Provisioning response callback
43 static SPResponseCallback gSPResponseHandler = NULL;
46 * A single global Policy Engine context will suffice as long
47 * as SRM is single-threaded.
49 PEContext_t g_policyEngineContext;
52 * @brief function to register provisoning API's response callback.
53 * @param respHandler response handler callback.
55 void SRMRegisterProvisioningResponseHandler(SPResponseCallback respHandler)
57 gSPResponseHandler = respHandler;
60 * @brief Handle the request from the SRM.
61 * @param endPoint [IN] Endpoint object from which the response is received.
62 * @param requestInfo [IN] Information for the request.
65 void SRMRequestHandler(const CAEndpoint_t *endPoint, const CARequestInfo_t *requestInfo)
67 OC_LOG(INFO, TAG, "Received request from remote device");
69 if (!endPoint || !requestInfo)
71 OC_LOG(ERROR, TAG, "Invalid arguments");
76 OicUuid_t subjectId = {.id = {0}};
77 memcpy(subjectId.id, requestInfo->info.identity.id, sizeof(subjectId.id));
79 //Check the URI has the query and skip it before checking the permission
80 char *uri = strstr(requestInfo->info.resourceUri, "?");
84 position = uri - requestInfo->info.resourceUri;
86 if (position > MAX_URI_LENGTH)
88 OC_LOG(ERROR, TAG, "URI length is too long");
91 SRMAccessResponse_t response = ACCESS_DENIED;
94 char newUri[MAX_URI_LENGTH + 1];
95 OICStrcpyPartial(newUri, MAX_URI_LENGTH + 1, requestInfo->info.resourceUri, position);
96 //Skip query and pass the newUri.
97 response = CheckPermission(&g_policyEngineContext, &subjectId,
99 GetPermissionFromCAMethod_t(requestInfo->method));
103 //Pass resourceUri if there is no query info.
104 response = CheckPermission(&g_policyEngineContext, &subjectId,
105 requestInfo->info.resourceUri,
106 GetPermissionFromCAMethod_t(requestInfo->method));
108 if (IsAccessGranted(response) && gRequestHandler)
110 return (gRequestHandler(endPoint, requestInfo));
113 // Form a 'access deny' or 'Error' response and send to peer
114 CAResponseInfo_t responseInfo = {.result = CA_EMPTY};
115 memcpy(&responseInfo.info, &(requestInfo->info), sizeof(responseInfo.info));
116 responseInfo.info.payload = NULL;
117 if (!gRequestHandler)
119 responseInfo.result = CA_INTERNAL_SERVER_ERROR;
124 * TODO Enhance this logic more to decide between
125 * CA_UNAUTHORIZED_REQ or CA_FORBIDDEN_REQ depending
126 * upon SRMAccessResponseReasonCode_t
128 responseInfo.result = CA_UNAUTHORIZED_REQ;
131 if (CA_STATUS_OK != CASendResponse(endPoint, &responseInfo))
133 OC_LOG(ERROR, TAG, "Failed in sending response to a unauthorized request!");
138 * @brief Handle the response from the SRM.
139 * @param endPoint [IN] The remote endpoint.
140 * @param responseInfo [IN] Response information from the endpoint.
143 void SRMResponseHandler(const CAEndpoint_t *endPoint, const CAResponseInfo_t *responseInfo)
145 OC_LOG(INFO, TAG, "Received response from remote device");
147 // isProvResponse flag is to check whether response is catered by provisioning APIs or not.
148 // When token sent by CA response matches with token generated by provisioning request,
149 // gSPResponseHandler returns true and response is not sent to RI layer. In case
150 // gSPResponseHandler is null and isProvResponse is false response then the response is for
152 bool isProvResponse = false;
154 if (gSPResponseHandler)
156 isProvResponse = gSPResponseHandler(endPoint, responseInfo);
158 if (!isProvResponse && gResponseHandler)
160 gResponseHandler(endPoint, responseInfo);
166 * @brief Handle the error from the SRM.
167 * @param endPoint [IN] The remote endpoint.
168 * @param errorInfo [IN] Error information from the endpoint.
171 void SRMErrorHandler(const CAEndpoint_t *endPoint, const CAErrorInfo_t *errorInfo)
173 OC_LOG(INFO, TAG, "Received error from remote device");
176 gErrorHandler(endPoint, errorInfo);
182 * @brief Register request and response callbacks.
183 * Requests and responses are delivered in these callbacks.
184 * @param reqHandler [IN] Request handler callback ( for GET,PUT ..etc)
185 * @param respHandler [IN] Response handler callback.
187 * OC_STACK_OK - No errors; Success
188 * OC_STACK_INVALID_PARAM - invalid parameter
190 OCStackResult SRMRegisterHandler(CARequestCallback reqHandler,
191 CAResponseCallback respHandler,
192 CAErrorCallback errHandler)
194 OC_LOG(INFO, TAG, "SRMRegisterHandler !!");
195 if( !reqHandler || !respHandler || !errHandler)
197 OC_LOG(ERROR, TAG, "Callback handlers are invalid");
198 return OC_STACK_INVALID_PARAM;
200 gRequestHandler = reqHandler;
201 gResponseHandler = respHandler;
202 gErrorHandler = errHandler;
205 #if defined(__WITH_DTLS__)
206 CARegisterHandler(SRMRequestHandler, SRMResponseHandler, SRMErrorHandler);
208 CARegisterHandler(reqHandler, respHandler, errHandler);
209 #endif /* __WITH_DTLS__ */
214 * @brief Register Persistent storage callback.
215 * @param persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
217 * OC_STACK_OK - No errors; Success
218 * OC_STACK_INVALID_PARAM - Invalid parameter
220 OCStackResult SRMRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler)
222 OC_LOG(INFO, TAG, "SRMRegisterPersistentStorageHandler !!");
223 if(!persistentStorageHandler)
225 OC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
226 return OC_STACK_INVALID_PARAM;
228 gPersistentStorageHandler = persistentStorageHandler;
233 * @brief Get Persistent storage handler pointer.
235 * The pointer to Persistent Storage callback handler
238 OCPersistentStorage* SRMGetPersistentStorageHandler()
240 return gPersistentStorageHandler;
245 * @brief Initialize all secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
246 * @retval OC_STACK_OK for Success, otherwise some error value
248 OCStackResult SRMInitSecureResources()
250 // TODO: temporarily returning OC_STACK_OK every time until default
251 // behavior (for when SVR DB is missing) is settled.
252 InitSecureResources();
254 #if defined(__WITH_DTLS__)
255 CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials);
256 #endif // (__WITH_DTLS__)
262 * @brief Perform cleanup for secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
265 void SRMDeInitSecureResources()
267 DestroySecureResources();
271 * @brief Initialize Policy Engine.
272 * @return OC_STACK_OK for Success, otherwise some error value.
274 OCStackResult SRMInitPolicyEngine()
276 return InitPolicyEngine(&g_policyEngineContext);
280 * @brief Cleanup Policy Engine.
283 void SRMDeInitPolicyEngine()
285 return DeInitPolicyEngine(&g_policyEngineContext);
289 * @brief Check the security resource URI.
290 * @param uri [IN] Pointers to security resource URI.
291 * @return true if the URI is one of security resources, otherwise false.
293 bool SRMIsSecurityResourceURI(const char* uri)
301 if (strcmp(uri, OIC_RSRC_AMACL_URI) == 0 || strcmp(uri, OIC_RSRC_ACL_URI) == 0
302 || strcmp(uri, OIC_RSRC_PSTAT_URI) == 0
303 || strncmp(OIC_RSRC_DOXM_URI, uri, sizeof(OIC_RSRC_DOXM_URI) - 1) == 0
304 || strcmp(uri, OIC_RSRC_CRED_URI) == 0 || strcmp(uri, OIC_RSRC_SVC_URI) == 0)