Add security version resource
[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 <string.h>
22 #include "resourcemanager.h"
23 #include "securevirtualresourcetypes.h"
24 #include "aclresource.h"
25 #include "pstatresource.h"
26 #include "doxmresource.h"
27 #include "credresource.h"
28 #include "svcresource.h"
29 #include "amaclresource.h"
30 #include "oic_malloc.h"
31 #include "oic_string.h"
32 #include "logger.h"
33 #include "utlist.h"
34
35 //#ifdef DIRECT_PAIRING
36 #include "pconfresource.h"
37 #include "dpairingresource.h"
38 //#endif // DIRECT_PAIRING
39 #include "verresource.h"
40
41 #define TAG "SRM-RM"
42
43 #ifdef __WITH_X509__
44 #include "crlresource.h"
45 #endif // __WITH_X509__
46
47 OCStackResult SendSRMResponse(const OCEntityHandlerRequest *ehRequest,
48         OCEntityHandlerResult ehRet, const char *rspPayload)
49 {
50     OIC_LOG (DEBUG, TAG, "SRM sending SRM response");
51     OCEntityHandlerResponse response = {.requestHandle = NULL};
52     if (ehRequest)
53     {
54         OCSecurityPayload ocPayload = {.base = {.type = PAYLOAD_TYPE_INVALID}};
55
56         response.requestHandle = ehRequest->requestHandle;
57         response.resourceHandle = ehRequest->resource;
58         response.ehResult = ehRet;
59         response.payload = (OCPayload*)(&ocPayload);
60         response.payload->type = PAYLOAD_TYPE_SECURITY;
61         ((OCSecurityPayload*)response.payload)->securityData = (char *)rspPayload;
62         response.persistentBufferFlag = 0;
63
64         return OCDoResponse(&response);
65     }
66     return OC_STACK_ERROR;
67 }
68
69 OCStackResult SendSRMCBORResponse(const OCEntityHandlerRequest *ehRequest,
70         OCEntityHandlerResult ehRet, uint8_t *cborPayload, size_t size)
71 {
72     OIC_LOG(DEBUG, TAG, "SRM sending SRM response");
73     OCEntityHandlerResponse response = {.requestHandle = NULL};
74     OCStackResult ret = OC_STACK_ERROR;
75
76     if (ehRequest)
77     {
78         OCSecurityPayload ocPayload = {.base = {.type = PAYLOAD_TYPE_INVALID}};
79
80         response.requestHandle = ehRequest->requestHandle;
81         response.resourceHandle = ehRequest->resource;
82         response.ehResult = ehRet;
83         response.payload = (OCPayload *)(&ocPayload);
84         response.payload->type = PAYLOAD_TYPE_SECURITY;
85         ((OCSecurityPayload *)response.payload)->securityData1 = cborPayload;
86         ((OCSecurityPayload *)response.payload)->payloadSize = size;
87         response.persistentBufferFlag = 0;
88
89         ret = OCDoResponse(&response);
90     }
91     return ret;
92 }
93
94 OCStackResult InitSecureResources( )
95 {
96     OCStackResult ret;
97
98     /*
99      * doxm resource should be initialized first as it contains the DeviceID
100      * which MAY be used during initialization of other resources.
101      */
102
103     ret = InitDoxmResource();
104
105     if(OC_STACK_OK == ret)
106     {
107         ret = InitPstatResource();
108     }
109     if(OC_STACK_OK == ret)
110     {
111         ret = InitACLResource();
112     }
113     if(OC_STACK_OK == ret)
114     {
115         ret = InitCredResource();
116     }
117 #ifdef __WITH_X509__
118     if(OC_STACK_OK == ret)
119     {
120         ret = InitCRLResource();
121     }
122 #endif // __WITH_X509__
123     if(OC_STACK_OK == ret)
124     {
125         ret = InitSVCResource();
126     }
127     if(OC_STACK_OK == ret)
128     {
129         ret = InitAmaclResource();
130     }
131 //#ifdef DIRECT_PAIRING
132     if(OC_STACK_OK == ret)
133     {
134         ret = InitPconfResource();
135     }
136     if(OC_STACK_OK == ret)
137     {
138         ret = InitDpairingResource();
139     }
140 //#endif // DIRECT_PAIRING
141     if(OC_STACK_OK == ret)
142     {
143         ret = InitVerResource();
144     }
145     if(OC_STACK_OK != ret)
146     {
147         //TODO: Update the default behavior if one of the SVR fails
148         DestroySecureResources();
149     }
150     return ret;
151 }
152
153 OCStackResult DestroySecureResources( )
154 {
155     DeInitACLResource();
156     DeInitCredResource();
157     DeInitDoxmResource();
158     DeInitPstatResource();
159 #ifdef __WITH_X509__
160     DeInitCRLResource();
161 #endif // __WITH_X509__
162     DeInitSVCResource();
163     DeInitAmaclResource();
164 //#ifdef DIRECT_PAIRING
165     DeInitPconfResource();
166     DeInitDpairingResource();
167 //#endif // DIRECT_PAIRING
168     DeInitVerResource();
169
170     return OC_STACK_OK;
171 }