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