Merge branch 'tizen' into tizen_5.5
[platform/upstream/iotivity.git] / resource / csdk / security / include / internal / psinterface.h
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 #ifndef IOTVT_SRM_PSI_H
22 #define IOTVT_SRM_PSI_H
23
24 #if defined (__TIZENRT__)
25 #include <apps/netutils/cJSON.h>
26 #else
27 #include "cJSON.h"
28 #endif
29
30 // Persistent Storage status definition
31 typedef enum
32 {
33     PS_NORMAL = 0,      // Persistent Storage operating normally
34     PS_OPEN_FAIL,       // Failed to open persistent storage
35     PS_PARSE_FAIL,     // Failed to cbor parse persistent storage
36     PS_NO_EXTERNAL_DB_SET = 99, // no external DB set (initial state)
37 } PSStatus_t;
38
39 //SVR DB Validity status
40 typedef enum
41 {
42     SVRDB_NOT_VALID = 0,
43     SVRDB_VALID = 1,
44     SVRDB_NOT_CHECKED = 255
45 } SVRDBValidStatus_t;
46
47 /**
48  * Reads the Secure Virtual Database from PS into dynamically allocated
49  * memory buffer.
50  *
51  * @note Caller of this method MUST use OCFree() method to release memory
52  *       referenced by return value.
53  *
54  * @return char * reference to memory buffer containing SVR database.
55  */
56 char * GetSVRDatabase();
57
58 /**
59  * This method is used by a entity handlers of SVR's to update
60  * SVR database.
61  *
62  * @param rsrcName string denoting the SVR name ("acl", "cred", "pstat" etc).
63  * @param jsonObj JSON object containing the SVR contents.
64  *
65  * @return ::OC_STACK_OK for Success, otherwise some error value
66  */
67 OCStackResult UpdateSVRDatabase(const char* rsrcName, cJSON* jsonObj);
68
69 /**
70  * Reads the Secure Virtual Database from PS
71  *
72  * @note Caller of this method MUST use OCFree() method to release memory
73  *       referenced by return value.
74  *
75  * @param rsrcName is the name of the field for which file content are read.
76                    if the value is NULL it will send the content of the whole file.
77  * @param data is the pointer to the file contents read from the database.
78  * @param size is the size to the file contents read.
79  *
80  * @return ::OC_STACK_OK for Success, otherwise some error value
81  */
82 OCStackResult GetSecureVirtualDatabaseFromPS(const char *rsrcName, uint8_t **data, size_t *size);
83
84 /**
85  * Gets the Secure Virtual Database from the Persistent Storage
86  *
87  * @param ps - Persistent Storage handler
88  * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
89  * @param data - pointer of the returned Secure Virtual Resource(s)
90  * @param size - pointer of the returned size of Secure Virtual Resource(s)
91  *
92  * @return OCStackResult - result of getting Secure Virtual Resource(s)
93  */
94 OCStackResult GetSecureVirtualDatabaseFromPS2(OCPersistentStorage* ps, const char *rsrcName,
95                                               uint8_t **data, size_t *size);
96
97
98 /**
99  * This method converts updates the persistent storage.
100  *
101  * @param rsrcName is the name of the secure resource that will be updated.
102  * @param cborPayload is the pointer holding cbor payload.
103  * @param cborPayload is the size of the cbor payload.
104  *
105  * @return ::OC_STACK_OK for Success, otherwise some error value
106  */
107 OCStackResult UpdateSecureResourceInPS(const char* rsrcName, const uint8_t* cborPayload, size_t size);
108
109 /**
110  * This method resets the secure resources according to the reset profile.
111  *
112  * @return ::OC_STACK_OK for Success, otherwise some error value
113  */
114 OCStackResult ResetSecureResourceInPS(void);
115
116 /**
117  * This method creates the reset profile from the persistent storage.
118  * The reset profile is the copy of the initial state of SVR resources
119  * when secure resources are initiated at first.
120  * In remote reset, the SVR will be reset according to the reset profile.
121  *
122  * @return ::OC_STACK_OK for Success, otherwise some error value
123  */
124 OCStackResult CreateResetProfile(void);
125
126 /**
127  * This method sets the persistent storage status.
128  *
129  * @param status is the current persistent storage status
130  */
131 void SetPSStatus(PSStatus_t status);
132
133 /**
134  * This function return SVR DB state: valid, not valid or was not checked yet
135  *
136  * @return Status of SVR DB
137  */
138 SVRDBValidStatus_t GetSVRDBValidStatus(void);
139
140 /**
141  * This method prints debug log that displays persistent storage status.
142  */
143 void PrintPSStatus(void);
144
145 /**
146  * Init the Persistent Storage Database.
147  */
148 OCStackResult InitPersistentStorageInterface(void);
149
150 /**
151  * DeInit the Persistent Storage Database.
152  */
153 void DeinitPersistentStorageInterface(void);
154
155 /**
156  * API to encrypt the un-encrypted DB file before OCRegisterPersistentStorageHandler
157  * If the API is successful, un-encrypted file will be removed, and if the encrypted file
158  * is currupted, then it restores encrypted file using rescue file.
159  *
160  * @param[in] key key used for encryption
161  * @param[in] psPlain OCPersistentStorage for the plain DB
162  * @param[in] psEnc OCPersistentStorage for the encrypted DB
163  * @param[in] psRescue OCPersistentStorage for the rescue DB
164  *
165  * @return ::OC_STACK_OK on success and other value otherwise.
166  */
167 OCStackResult setSecurePSI(const unsigned char *key, const OCPersistentStorage *psPlain,
168         const OCPersistentStorage *psEnc, const OCPersistentStorage *psRescue);
169
170 /**
171  *
172  * @return pointer on resetSVRDB structure for manipulating with callback
173  */
174 resetSVRDBCB_t*  GetResetSVRDBCB(void);
175
176 /**
177  * API to validate data in SVR DB.
178  * @return ::OC_STACK_OK on success and other value otherwise.
179  */
180 OCStackResult CheckSVRDBValidity(void);
181
182 /**
183  * This function check if ResetPF are created
184  * @return true in positive case, false otherwise.
185  */
186 bool isResetPFExist(void);
187
188 #endif //IOTVT_SRM_PSI_H