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