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