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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "cainterface.h"
24 #include "secureresourcemanager.h"
25 #include "resourcemanager.h"
26 #include "credresource.h"
27 #include "policyengine.h"
28 #include "oic_string.h"
33 //Request Callback handler
34 static CARequestCallback gRequestHandler = NULL;
35 //Response Callback handler
36 static CAResponseCallback gResponseHandler = NULL;
37 //Error Callback handler
38 static CAErrorCallback gErrorHandler = NULL;
39 //Persistent Storage callback handler for open/read/write/close/unlink
40 static OCPersistentStorage *gPersistentStorageHandler = NULL;
41 //Provisioning response callback
42 static SPResponseCallback gSPResponseHandler = NULL;
45 * A single global Policy Engine context will suffice as long
46 * as SRM is single-threaded.
48 PEContext_t g_policyEngineContext;
51 * @brief function to register provisoning API's response callback.
52 * @param respHandler response handler callback.
54 void SRMRegisterProvisioningResponseHandler(SPResponseCallback respHandler)
56 gSPResponseHandler = respHandler;
59 * @brief Handle the request from the SRM.
60 * @param endPoint [IN] Endpoint object from which the response is received.
61 * @param requestInfo [IN] Information for the request.
64 void SRMRequestHandler(const CAEndpoint_t *endPoint, const CARequestInfo_t *requestInfo)
66 OC_LOG(INFO, TAG, "Received request from remote device");
68 if (!endPoint || !requestInfo)
70 OC_LOG(ERROR, TAG, "Invalid arguments");
75 OicUuid_t subjectId = {.id = {0}};
76 memcpy(subjectId.id, requestInfo->info.identity.id, sizeof(subjectId.id));
78 //Check the URI has the query and skip it before checking the permission
79 char *uri = strstr(requestInfo->info.resourceUri, "?");
83 position = uri - requestInfo->info.resourceUri;
85 if (position > MAX_URI_LENGTH)
87 OC_LOG(ERROR, TAG, "URI length is too long");
90 SRMAccessResponse_t response = ACCESS_DENIED;
93 char newUri[MAX_URI_LENGTH + 1];
94 OICStrcpyPartial(newUri, MAX_URI_LENGTH + 1, requestInfo->info.resourceUri, position);
95 //Skip query and pass the newUri.
96 response = CheckPermission(&g_policyEngineContext, &subjectId,
98 GetPermissionFromCAMethod_t(requestInfo->method));
102 //Pass resourceUri if there is no query info.
103 response = CheckPermission(&g_policyEngineContext, &subjectId,
104 requestInfo->info.resourceUri,
105 GetPermissionFromCAMethod_t(requestInfo->method));
107 if (IsAccessGranted(response) && gRequestHandler)
109 return (gRequestHandler(endPoint, requestInfo));
112 // Form a 'access deny' or 'Error' response and send to peer
113 CAResponseInfo_t responseInfo = {.result = CA_EMPTY};
114 memcpy(&responseInfo.info, &(requestInfo->info), sizeof(responseInfo.info));
115 responseInfo.info.payload = NULL;
116 if (!gRequestHandler)
118 responseInfo.result = CA_INTERNAL_SERVER_ERROR;
123 * TODO Enhance this logic more to decide between
124 * CA_UNAUTHORIZED_REQ or CA_FORBIDDEN_REQ depending
125 * upon SRMAccessResponseReasonCode_t
127 responseInfo.result = CA_UNAUTHORIZED_REQ;
130 if (CA_STATUS_OK != CASendResponse(endPoint, &responseInfo))
132 OC_LOG(ERROR, TAG, "Failed in sending response to a unauthorized request!");
137 * @brief Handle the response from the SRM.
138 * @param endPoint [IN] The remote endpoint.
139 * @param responseInfo [IN] Response information from the endpoint.
142 void SRMResponseHandler(const CAEndpoint_t *endPoint, const CAResponseInfo_t *responseInfo)
144 OC_LOG(INFO, TAG, "Received response from remote device");
146 // isProvResponse flag is to check whether response is catered by provisioning APIs or not.
147 // When token sent by CA response matches with token generated by provisioning request,
148 // gSPResponseHandler returns true and response is not sent to RI layer. In case
149 // gSPResponseHandler is null and isProvResponse is false response then the response is for
151 bool isProvResponse = false;
153 if (gSPResponseHandler)
155 isProvResponse = gSPResponseHandler(endPoint, responseInfo);
157 if (!isProvResponse && gResponseHandler)
159 gResponseHandler(endPoint, responseInfo);
165 * @brief Handle the error from the SRM.
166 * @param endPoint [IN] The remote endpoint.
167 * @param errorInfo [IN] Error information from the endpoint.
170 void SRMErrorHandler(const CAEndpoint_t *endPoint, const CAErrorInfo_t *errorInfo)
172 OC_LOG(INFO, TAG, "Received error from remote device");
175 gErrorHandler(endPoint, errorInfo);
181 * @brief Register request and response callbacks.
182 * Requests and responses are delivered in these callbacks.
183 * @param reqHandler [IN] Request handler callback ( for GET,PUT ..etc)
184 * @param respHandler [IN] Response handler callback.
186 * OC_STACK_OK - No errors; Success
187 * OC_STACK_INVALID_PARAM - invalid parameter
189 OCStackResult SRMRegisterHandler(CARequestCallback reqHandler,
190 CAResponseCallback respHandler,
191 CAErrorCallback errHandler)
193 OC_LOG(INFO, TAG, "SRMRegisterHandler !!");
194 if( !reqHandler || !respHandler || !errHandler)
196 OC_LOG(ERROR, TAG, "Callback handlers are invalid");
197 return OC_STACK_INVALID_PARAM;
199 gRequestHandler = reqHandler;
200 gResponseHandler = respHandler;
201 gErrorHandler = errHandler;
204 #if defined(__WITH_DTLS__)
205 CARegisterHandler(SRMRequestHandler, SRMResponseHandler, SRMErrorHandler);
207 CARegisterHandler(reqHandler, respHandler, errHandler);
208 #endif /* __WITH_DTLS__ */
213 * @brief Register Persistent storage callback.
214 * @param persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
216 * OC_STACK_OK - No errors; Success
217 * OC_STACK_INVALID_PARAM - Invalid parameter
219 OCStackResult SRMRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler)
221 OC_LOG(INFO, TAG, "SRMRegisterPersistentStorageHandler !!");
222 if(!persistentStorageHandler)
224 OC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
225 return OC_STACK_INVALID_PARAM;
227 gPersistentStorageHandler = persistentStorageHandler;
232 * @brief Get Persistent storage handler pointer.
234 * The pointer to Persistent Storage callback handler
237 OCPersistentStorage* SRMGetPersistentStorageHandler()
239 return gPersistentStorageHandler;
244 * @brief Initialize all secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
245 * @retval OC_STACK_OK for Success, otherwise some error value
247 OCStackResult SRMInitSecureResources()
249 // TODO: temporarily returning OC_STACK_OK every time until default
250 // behavior (for when SVR DB is missing) is settled.
251 InitSecureResources();
253 #if defined(__WITH_DTLS__)
254 CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials);
255 #endif // (__WITH_DTLS__)
261 * @brief Perform cleanup for secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
264 void SRMDeInitSecureResources()
266 DestroySecureResources();
270 * @brief Initialize Policy Engine.
271 * @return OC_STACK_OK for Success, otherwise some error value.
273 OCStackResult SRMInitPolicyEngine()
275 return InitPolicyEngine(&g_policyEngineContext);
279 * @brief Cleanup Policy Engine.
282 void SRMDeInitPolicyEngine()
284 return DeInitPolicyEngine(&g_policyEngineContext);