ae228bf03c11442ee441544157c86192e34d3deb
[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 /**
40  * Reads the Secure Virtual Database from PS into dynamically allocated
41  * memory buffer.
42  *
43  * @note Caller of this method MUST use OCFree() method to release memory
44  *       referenced by return value.
45  *
46  * @return char * reference to memory buffer containing SVR database.
47  */
48 char * GetSVRDatabase();
49
50 /**
51  * This method is used by a entity handlers of SVR's to update
52  * SVR database.
53  *
54  * @param rsrcName string denoting the SVR name ("acl", "cred", "pstat" etc).
55  * @param jsonObj JSON object containing the SVR contents.
56  *
57  * @return ::OC_STACK_OK for Success, otherwise some error value
58  */
59 OCStackResult UpdateSVRDatabase(const char* rsrcName, cJSON* jsonObj);
60
61 /**
62  * Reads the Secure Virtual Database from PS
63  *
64  * @note Caller of this method MUST use OCFree() method to release memory
65  *       referenced by return value.
66  *
67  * @param rsrcName is the name of the field for which file content are read.
68                    if the value is NULL it will send the content of the whole file.
69  * @param data is the pointer to the file contents read from the database.
70  * @param size is the size to the file contents read.
71  *
72  * @return ::OC_STACK_OK for Success, otherwise some error value
73  */
74 OCStackResult GetSecureVirtualDatabaseFromPS(const char *rsrcName, uint8_t **data, size_t *size);
75
76 /**
77  * Gets the Secure Virtual Database from the Persistent Storage
78  *
79  * @param ps - Persistent Storage handler
80  * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
81  * @param data - pointer of the returned Secure Virtual Resource(s)
82  * @param size - pointer of the returned size of Secure Virtual Resource(s)
83  *
84  * @return OCStackResult - result of getting Secure Virtual Resource(s)
85  */
86 OCStackResult GetSecureVirtualDatabaseFromPS2(OCPersistentStorage* ps, const char *rsrcName,
87                                               uint8_t **data, size_t *size);
88
89
90 /**
91  * This method converts updates the persistent storage.
92  *
93  * @param rsrcName is the name of the secure resource that will be updated.
94  * @param cborPayload is the pointer holding cbor payload.
95  * @param cborPayload is the size of the cbor payload.
96  *
97  * @return ::OC_STACK_OK for Success, otherwise some error value
98  */
99 OCStackResult UpdateSecureResourceInPS(const char* rsrcName, const uint8_t* cborPayload, size_t size);
100
101 /**
102  * This method resets the secure resources according to the reset profile.
103  *
104  * @return ::OC_STACK_OK for Success, otherwise some error value
105  */
106 OCStackResult ResetSecureResourceInPS(void);
107
108 /**
109  * This method creates the reset profile from the persistent storage.
110  * The reset profile is the copy of the initial state of SVR resources
111  * when secure resources are initiated at first.
112  * In remote reset, the SVR will be reset according to the reset profile.
113  *
114  * @return ::OC_STACK_OK for Success, otherwise some error value
115  */
116 OCStackResult CreateResetProfile(void);
117
118 /**
119  * This method sets the persistent storage status.
120  *
121  * @param status is the current persistent storage status
122  */
123 void SetPSStatus(PSStatus_t status);
124
125 /**
126  * This method prints debug log that displays persistent storage status.
127  */
128 void PrintPSStatus(void);
129
130 /**
131  * Init the Persistent Storage Database.
132  */
133 OCStackResult InitPersistentStorageInterface(void);
134
135 /**
136  * DeInit the Persistent Storage Database.
137  */
138 void DeinitPersistentStorageInterface(void);
139
140 /**
141  * API to encrypt the un-encrypted DB file before OCRegisterPersistentStorageHandler
142  * If the API is successful, un-encrypted file will be removed, and if the encrypted file
143  * is currupted, then it restores encrypted file using rescue file.
144  *
145  * @param[in] key key used for encryption
146  * @param[in] psPlain OCPersistentStorage for the plain DB
147  * @param[in] psEnc OCPersistentStorage for the encrypted DB
148  * @param[in] psRescue OCPersistentStorage for the rescue DB
149  *
150  * @return ::OC_STACK_OK on success and other value otherwise.
151  */
152 OCStackResult setSecurePSI(const unsigned char *key, const OCPersistentStorage *psPlain,
153         const OCPersistentStorage *psEnc, const OCPersistentStorage *psRescue);
154
155 #endif //IOTVT_SRM_PSI_H