Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / security / src / resourcemanager.c
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "resourcemanager.h"
22 #include "securevirtualresourcetypes.h"
23 #include "aclresource.h"
24 #include "pstatresource.h"
25 #include "doxmresource.h"
26 #include "credresource.h"
27 #include "oic_malloc.h"
28 #include "oic_string.h"
29 #include "logger.h"
30 #include "utlist.h"
31 #include <string.h>
32
33 #define TAG PCF("SRM-RM")
34
35 /**
36  * This method is used by all secure resource modules to send responses to REST queries.
37  *
38  * @param ehRequest pointer to entity handler request data structure.
39  * @param ehRet result code from entity handler.
40  * @param rspPayload response payload in JSON.
41  *
42  * @retval  OC_STACK_OK for Success, otherwise some error value
43  */
44 OCStackResult SendSRMResponse(const OCEntityHandlerRequest *ehRequest,
45         OCEntityHandlerResult ehRet, const char *rspPayload)
46 {
47     OC_LOG (INFO, TAG, PCF("SRM sending SRM response"));
48     OCEntityHandlerResponse response = {};
49     if (ehRequest)
50     {
51         OCSecurityPayload ocPayload = {};
52
53         response.requestHandle = ehRequest->requestHandle;
54         response.resourceHandle = ehRequest->resource;
55         response.ehResult = ehRet;
56         response.payload = (OCPayload*)(&ocPayload);
57         response.payload->type = PAYLOAD_TYPE_SECURITY;
58         ((OCSecurityPayload*)response.payload)->securityData = (char *)rspPayload;
59         response.persistentBufferFlag = 0;
60
61         return OCDoResponse(&response);
62     }
63     return OC_STACK_ERROR;
64 }
65
66 /**
67  * Initialize all secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
68  *
69  * @retval  OC_STACK_OK for Success, otherwise some error value
70  */
71 OCStackResult InitSecureResources( )
72 {
73     OCStackResult ret;
74
75     /*
76      * doxm resource should be initialized first as it contains the DeviceID
77      * which MAY be used during initialization of other resources.
78      */
79
80     ret = InitDoxmResource();
81
82     if(OC_STACK_OK == ret)
83     {
84         ret = InitPstatResource();
85     }
86     if(OC_STACK_OK == ret)
87     {
88         ret = InitACLResource();
89     }
90     if(OC_STACK_OK == ret)
91     {
92         ret = InitCredResource();
93     }
94     if(OC_STACK_OK != ret)
95     {
96         //TODO: Update the default behavior if one of the SVR fails
97         DestroySecureResources();
98     }
99     return ret;
100 }
101
102 /**
103  * Perform cleanup for secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
104  *
105  * @retval  OC_STACK_OK for Success, otherwise some error value
106  */
107 OCStackResult DestroySecureResources( )
108 {
109     DeInitACLResource();
110     DeInitCredResource();
111     DeInitDoxmResource();
112     DeInitPstatResource();
113
114     return OC_STACK_OK;
115 }