Security CBOR conversion
[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, size_t size)
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         ((OCSecurityPayload *)response.payload)->payloadSize = size;
86         response.persistentBufferFlag = 0;
87
88         ret = OCDoResponse(&response);
89     }
90     return ret;
91 }
92
93 OCStackResult InitSecureResources( )
94 {
95     OCStackResult ret;
96
97     /*
98      * doxm resource should be initialized first as it contains the DeviceID
99      * which MAY be used during initialization of other resources.
100      */
101
102     ret = InitDoxmResource();
103
104     if(OC_STACK_OK == ret)
105     {
106         ret = InitPstatResource();
107     }
108     if(OC_STACK_OK == ret)
109     {
110         ret = InitACLResource();
111     }
112     if(OC_STACK_OK == ret)
113     {
114         ret = InitCredResource();
115     }
116 #ifdef __WITH_X509__
117     if(OC_STACK_OK == ret)
118     {
119         ret = InitCRLResource();
120     }
121 #endif // __WITH_X509__
122     if(OC_STACK_OK == ret)
123     {
124         ret = InitSVCResource();
125     }
126     if(OC_STACK_OK == ret)
127     {
128         ret = InitAmaclResource();
129     }
130 //#ifdef DIRECT_PAIRING
131     if(OC_STACK_OK == ret)
132     {
133         ret = InitPconfResource();
134     }
135     if(OC_STACK_OK == ret)
136     {
137         ret = InitDpairingResource();
138     }
139 //#endif // DIRECT_PAIRING
140     if(OC_STACK_OK != ret)
141     {
142         //TODO: Update the default behavior if one of the SVR fails
143         DestroySecureResources();
144     }
145     return ret;
146 }
147
148 OCStackResult DestroySecureResources( )
149 {
150     DeInitACLResource();
151     DeInitCredResource();
152     DeInitDoxmResource();
153     DeInitPstatResource();
154 #ifdef __WITH_X509__
155     DeInitCRLResource();
156 #endif // __WITH_X509__
157     DeInitSVCResource();
158     DeInitAmaclResource();
159 //#ifdef DIRECT_PAIRING
160     DeInitPconfResource();
161     DeInitDpairingResource();
162 //#endif // DIRECT_PAIRING
163
164     return OC_STACK_OK;
165 }