Add PKIX resources
[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 #define TAG PCF("SRM-RM")
36
37 /**
38  * This method is used by all secure resource modules to send responses to REST queries.
39  *
40  * @param ehRequest pointer to entity handler request data structure.
41  * @param ehRet result code from entity handler.
42  * @param rspPayload response payload in JSON.
43  *
44  * @retval  OC_STACK_OK for Success, otherwise some error value
45  */
46 OCStackResult SendSRMResponse(const OCEntityHandlerRequest *ehRequest,
47         OCEntityHandlerResult ehRet, const char *rspPayload)
48 {
49     OC_LOG (INFO, TAG, PCF("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 /**
69  * Initialize all secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
70  *
71  * @retval  OC_STACK_OK for Success, otherwise some error value
72  */
73 OCStackResult InitSecureResources( )
74 {
75     OCStackResult ret;
76
77     /*
78      * doxm resource should be initialized first as it contains the DeviceID
79      * which MAY be used during initialization of other resources.
80      */
81
82     ret = InitDoxmResource();
83
84     if(OC_STACK_OK == ret)
85     {
86         ret = InitPstatResource();
87     }
88     if(OC_STACK_OK == ret)
89     {
90         ret = InitACLResource();
91     }
92     if(OC_STACK_OK == ret)
93     {
94         ret = InitCredResource();
95     }
96 #ifdef __WITH_X509__
97     if(OC_STACK_OK == ret)
98     {
99         ret = InitCRLResource();
100     }
101 #endif // __WITH_X509__
102     if(OC_STACK_OK == ret)
103     {
104         ret = InitSVCResource();
105         }
106         if(OC_STACK_OK == ret)
107     {
108         ret = InitAmaclResource();
109     }
110     if(OC_STACK_OK != ret)
111     {
112         //TODO: Update the default behavior if one of the SVR fails
113         DestroySecureResources();
114     }
115     return ret;
116 }
117
118 /**
119  * Perform cleanup for secure resources ( /oic/sec/cred, /oic/sec/acl, /oic/sec/pstat etc).
120  *
121  * @retval  OC_STACK_OK for Success, otherwise some error value
122  */
123 OCStackResult DestroySecureResources( )
124 {
125     DeInitACLResource();
126     DeInitCredResource();
127     DeInitDoxmResource();
128     DeInitPstatResource();
129
130     return OC_STACK_OK;
131 }