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"
32 #include "crlresource.h"
33 #endif // __WITH_X509__
35 #define TAG PCF("SRM")
37 //Request Callback handler
38 static CARequestCallback gRequestHandler = NULL;
39 //Response Callback handler
40 static CAResponseCallback gResponseHandler = NULL;
41 //Error Callback handler
42 static CAErrorCallback gErrorHandler = NULL;
43 //Persistent Storage callback handler for open/read/write/close/unlink
44 static OCPersistentStorage *gPersistentStorageHandler = NULL;
45 //Provisioning response callback
46 static SPResponseCallback gSPResponseHandler = NULL;
49 * A single global Policy Engine context will suffice as long
50 * as SRM is single-threaded.
52 PEContext_t g_policyEngineContext;
55 * @brief function to register provisoning API's response callback.
56 * @param respHandler response handler callback.
58 void SRMRegisterProvisioningResponseHandler(SPResponseCallback respHandler)
60 gSPResponseHandler = respHandler;
63 * @brief Handle the request from the SRM.
64 * @param endPoint [IN] Endpoint object from which the response is received.
65 * @param requestInfo [IN] Information for the request.
68 void SRMRequestHandler(const CAEndpoint_t *endPoint, const CARequestInfo_t *requestInfo)
70 OC_LOG(INFO, TAG, PCF("Received request from remote device"));
72 if (!endPoint || !requestInfo)
74 OC_LOG(ERROR, TAG, PCF("Invalid arguments"));
79 OicUuid_t subjectId = {.id = {0}};
80 memcpy(subjectId.id, requestInfo->info.identity.id, sizeof(subjectId.id));
82 //Check the URI has the query and skip it before checking the permission
83 char *uri = strstr(requestInfo->info.resourceUri, "?");
87 position = uri - requestInfo->info.resourceUri;
89 if (position > MAX_URI_LENGTH)
91 OC_LOG(ERROR, TAG, PCF("URI length is too long"));
94 SRMAccessResponse_t response = ACCESS_DENIED;
97 char newUri[MAX_URI_LENGTH + 1];
98 OICStrcpyPartial(newUri, MAX_URI_LENGTH + 1, requestInfo->info.resourceUri, position);
99 //Skip query and pass the newUri.
100 response = CheckPermission(&g_policyEngineContext, &subjectId,
102 GetPermissionFromCAMethod_t(requestInfo->method));
106 //Pass resourceUri if there is no query info.
107 response = CheckPermission(&g_policyEngineContext, &subjectId,
108 requestInfo->info.resourceUri,
109 GetPermissionFromCAMethod_t(requestInfo->method));
111 if (IsAccessGranted(response) && gRequestHandler)
113 return (gRequestHandler(endPoint, requestInfo));
116 // Form a 'access deny' or 'Error' response and send to peer
117 CAResponseInfo_t responseInfo = {.result = CA_EMPTY};
118 memcpy(&responseInfo.info, &(requestInfo->info), sizeof(responseInfo.info));
119 responseInfo.info.payload = NULL;
120 if (!gRequestHandler)
122 responseInfo.result = CA_INTERNAL_SERVER_ERROR;
127 * TODO Enhance this logic more to decide between
128 * CA_UNAUTHORIZED_REQ or CA_FORBIDDEN_REQ depending
129 * upon SRMAccessResponseReasonCode_t
131 responseInfo.result = CA_UNAUTHORIZED_REQ;
134 if (CA_STATUS_OK != CASendResponse(endPoint, &responseInfo))
136 OC_LOG(ERROR, TAG, PCF("Failed in sending response to a unauthorized request!"));
141 * @brief Handle the response from the SRM.
142 * @param endPoint [IN] The remote endpoint.
143 * @param responseInfo [IN] Response information from the endpoint.
146 void SRMResponseHandler(const CAEndpoint_t *endPoint, const CAResponseInfo_t *responseInfo)
148 OC_LOG(INFO, TAG, PCF("Received response from remote device"));
150 // isProvResponse flag is to check whether response is catered by provisioning APIs or not.
151 // When token sent by CA response matches with token generated by provisioning request,
152 // gSPResponseHandler returns true and response is not sent to RI layer. In case
153 // gSPResponseHandler is null and isProvResponse is false response then the response is for
155 bool isProvResponse = false;
157 if (gSPResponseHandler)
159 isProvResponse = gSPResponseHandler(endPoint, responseInfo);
161 if (!isProvResponse && gResponseHandler)
163 gResponseHandler(endPoint, responseInfo);
169 * @brief Handle the error from the SRM.
170 * @param endPoint [IN] The remote endpoint.
171 * @param errorInfo [IN] Error information from the endpoint.
174 void SRMErrorHandler(const CAEndpoint_t *endPoint, const CAErrorInfo_t *errorInfo)
176 OC_LOG(INFO, TAG, PCF("Received error from remote device"));
179 gErrorHandler(endPoint, errorInfo);
185 * @brief Register request and response callbacks.
186 * Requests and responses are delivered in these callbacks.
187 * @param reqHandler [IN] Request handler callback ( for GET,PUT ..etc)
188 * @param respHandler [IN] Response handler callback.
190 * OC_STACK_OK - No errors; Success
191 * OC_STACK_INVALID_PARAM - invalid parameter
193 OCStackResult SRMRegisterHandler(CARequestCallback reqHandler,
194 CAResponseCallback respHandler,
195 CAErrorCallback errHandler)
197 OC_LOG(INFO, TAG, PCF("SRMRegisterHandler !!"));
198 if( !reqHandler || !respHandler || !errHandler)
200 OC_LOG(ERROR, TAG, PCF("Callback handlers are invalid"));
201 return OC_STACK_INVALID_PARAM;
203 gRequestHandler = reqHandler;
204 gResponseHandler = respHandler;
205 gErrorHandler = errHandler;
208 #if defined(__WITH_DTLS__)
209 CARegisterHandler(SRMRequestHandler, SRMResponseHandler, SRMErrorHandler);
211 CARegisterHandler(reqHandler, respHandler, errHandler);
212 #endif /* __WITH_DTLS__ */
217 * @brief Register Persistent storage callback.
218 * @param persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
220 * OC_STACK_OK - No errors; Success
221 * OC_STACK_INVALID_PARAM - Invalid parameter
223 OCStackResult SRMRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler)
225 OC_LOG(INFO, TAG, PCF("SRMRegisterPersistentStorageHandler !!"));
226 if(!persistentStorageHandler)
228 OC_LOG(ERROR, TAG, PCF("The persistent storage handler is invalid"));
229 return OC_STACK_INVALID_PARAM;
231 gPersistentStorageHandler = persistentStorageHandler;
236 * @brief Get Persistent storage handler pointer.
238 * The pointer to Persistent Storage callback handler
241 OCPersistentStorage* SRMGetPersistentStorageHandler()
243 return gPersistentStorageHandler;
248 * @brief Initialize all secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
249 * @retval OC_STACK_OK for Success, otherwise some error value
251 OCStackResult SRMInitSecureResources()
253 // TODO: temporarily returning OC_STACK_OK every time until default
254 // behavior (for when SVR DB is missing) is settled.
255 InitSecureResources();
257 #if defined(__WITH_DTLS__)
258 CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials);
259 #endif // (__WITH_DTLS__)
260 #if defined(__WITH_X509__)
261 CARegisterDTLSX509CredentialsHandler(GetDtlsX509Credentials);
262 CARegisterDTLSCrlHandler(GetDerCrl);
263 #endif // (__WITH_X509__)
269 * @brief Perform cleanup for secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
272 void SRMDeInitSecureResources()
274 DestroySecureResources();
278 * @brief Initialize Policy Engine.
279 * @return OC_STACK_OK for Success, otherwise some error value.
281 OCStackResult SRMInitPolicyEngine()
283 return InitPolicyEngine(&g_policyEngineContext);
287 * @brief Cleanup Policy Engine.
290 void SRMDeInitPolicyEngine()
292 return DeInitPolicyEngine(&g_policyEngineContext);