Merge branch 'master' into easysetup
[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 "svcresource.h"
28 #include "amaclresource.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
31 #include "logger.h"
32 #include "utlist.h"
33 #include <string.h>
34
35 #define TAG "SRM-RM"
36
37 #ifdef __WITH_X509__
38 #include "crlresource.h"
39 #endif // __WITH_X509__
40
41 /**
42  * This method is used by all secure resource modules to send responses to REST queries.
43  *
44  * @param ehRequest pointer to entity handler request data structure.
45  * @param ehRet result code from entity handler.
46  * @param rspPayload response payload in JSON.
47  *
48  * @retval  OC_STACK_OK for Success, otherwise some error value
49  */
50 OCStackResult SendSRMResponse(const OCEntityHandlerRequest *ehRequest,
51         OCEntityHandlerResult ehRet, const char *rspPayload)
52 {
53     OC_LOG (DEBUG, TAG, "SRM sending SRM response");
54     OCEntityHandlerResponse response = {.requestHandle = NULL};
55     if (ehRequest)
56     {
57         OCSecurityPayload ocPayload = {.base = {.type = PAYLOAD_TYPE_INVALID}};
58
59         response.requestHandle = ehRequest->requestHandle;
60         response.resourceHandle = ehRequest->resource;
61         response.ehResult = ehRet;
62         response.payload = (OCPayload*)(&ocPayload);
63         response.payload->type = PAYLOAD_TYPE_SECURITY;
64         ((OCSecurityPayload*)response.payload)->securityData = (char *)rspPayload;
65         response.persistentBufferFlag = 0;
66
67         return OCDoResponse(&response);
68     }
69     return OC_STACK_ERROR;
70 }
71
72 /**
73  * Initialize all secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
74  *
75  * @retval  OC_STACK_OK for Success, otherwise some error value
76  */
77 OCStackResult InitSecureResources( )
78 {
79     OCStackResult ret;
80
81     /*
82      * doxm resource should be initialized first as it contains the DeviceID
83      * which MAY be used during initialization of other resources.
84      */
85
86     ret = InitDoxmResource();
87
88     if(OC_STACK_OK == ret)
89     {
90         ret = InitPstatResource();
91     }
92     if(OC_STACK_OK == ret)
93     {
94         ret = InitACLResource();
95     }
96     if(OC_STACK_OK == ret)
97     {
98         ret = InitCredResource();
99     }
100 #ifdef __WITH_X509__
101     if(OC_STACK_OK == ret)
102     {
103         ret = InitCRLResource();
104     }
105 #endif // __WITH_X509__
106     if(OC_STACK_OK == ret)
107     {
108         ret = InitSVCResource();
109         }
110         if(OC_STACK_OK == ret)
111     {
112         ret = InitAmaclResource();
113     }
114     if(OC_STACK_OK != ret)
115     {
116         //TODO: Update the default behavior if one of the SVR fails
117         DestroySecureResources();
118     }
119     return ret;
120 }
121
122 /**
123  * Perform cleanup for secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
124  *
125  * @retval  OC_STACK_OK for Success, otherwise some error value
126  */
127 OCStackResult DestroySecureResources( )
128 {
129     DeInitACLResource();
130     DeInitCredResource();
131     DeInitDoxmResource();
132     DeInitPstatResource();
133 #ifdef __WITH_X509__
134     DeInitCRLResource();
135 #endif // __WITH_X509__
136     DeInitSVCResource();
137     DeInitAmaclResource();
138
139     return OC_STACK_OK;
140 }