32ccefeffe2646e61240c6a9e1a01501a693607e
[platform/upstream/iotivity.git] / resource / csdk / security / include / internal / credresource.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_CREDR_H
22 #define IOTVT_SRM_CREDR_H
23
24 #include "cainterface.h"
25 #include "securevirtualresourcetypes.h"
26 #include "octypes.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /**
33  * Initialize credential resource by loading data from persistent storage.
34  *
35  * @return ::OC_STACK_OK, if initialization is successful, else ::OC_STACK_ERROR if
36  * initialization fails.
37  */
38 OCStackResult InitCredResource();
39
40 /**
41  * Perform cleanup for credential resources.
42  *
43  * @return ::OC_STACK_OK, if no errors. ::OC_STACK_ERROR, if stack process error.
44  * ::OC_STACK_NO_RESOURCE, if resource not found.
45  * ::OC_STACK_INVALID_PARAM, if invalid param.
46  */
47 OCStackResult DeInitCredResource();
48
49 /**
50  * This method is used by tinydtls/SRM to retrieve credential for given subject.
51  *
52  * @param subjectId for which credential is required.
53  *
54  * @return reference to @ref OicSecCred_t, if credential is found, else NULL, if credential
55  * not found.
56  */
57 OicSecCred_t* GetCredResourceData(const OicUuid_t* subjectId);
58
59 /**
60  * This method is used by SRM to retrieve credential entry for given credId.
61  *
62  * @note Caller needs to release this memory by calling DeleteCredList().
63  *
64  * @param credId for which credential is required.
65  *
66  * @return reference to @ref OicSecCred_t, if credential is found, else NULL, if credential
67  * not found.
68  */
69 OicSecCred_t* GetCredEntryByCredId(const uint16_t credId);
70
71 /**
72  * This function converts credential data into CBOR format.
73  * Caller needs to invoke 'free' when done using returned string.
74  *
75  * @param cred is the pointer to instance of OicSecCred_t structure.
76  * @param cborPayload is the CBOR converted value.
77  * @param cborSize is the size of the CBOR.
78  * @param secureFlag shows fill or not private key.
79  *
80  * @return ::OC_STACK_OK if conversion is successful, else ::OC_STACK_ERROR if unsuccessful.
81  */
82 OCStackResult CredToCBORPayload(const OicSecCred_t* cred, uint8_t **cborPayload,
83                                 size_t *cborSize, int secureFlag);
84
85 #ifdef _ENABLE_MULTIPLE_OWNER_
86 /**
87  * Function to check the credential access of SubOwner
88  *
89  * @param[in] uuid SubOwner's UUID
90  * @param[in] cborPayload CBOR payload of credential
91  * @param[in] size Byte length of cborPayload
92  *
93  * @return ::true for valid access, otherwise invalid access
94  */
95 bool IsValidCredentialAccessForSubOwner(const OicUuid_t* uuid, const uint8_t *cborPayload, size_t size);
96 #endif //_ENABLE_MULTIPLE_OWNER_
97
98 /**
99  * This function generates the bin credential data.
100  *
101  * @param subject pointer to subject of this credential.
102  * @param credType credential type.
103  * @param publicData public data such as public key.
104  * @param privateData private data such as private key.
105  * @param rownerID Resource owner's UUID.
106  * @param eownerID Entry owner's UUID.
107  *
108  * @return pointer to instance of @ref OicSecCred_t if successful. else NULL in case of error.
109
110  */
111 OicSecCred_t * GenerateCredential(const OicUuid_t* subject, OicSecCredType_t credType,
112                      const OicSecCert_t * publicData, const OicSecKey_t * privateData,
113                      const OicUuid_t * rownerID, const OicUuid_t * eownerID);
114
115 /**
116  * This function adds the new cred to the credential list.
117  *
118  * @param cred is the pointer to new credential.
119  *
120  * @return ::OC_STACK_OK, cred not NULL and persistent storage gets updated.
121  * ::OC_STACK_ERROR, cred is NULL or fails to update persistent storage.
122  */
123 OCStackResult AddCredential(OicSecCred_t * cred);
124
125 /**
126  * Function to remove the credential from SVR DB.
127  *
128  * @param subject is the Credential Subject to be deleted.
129  *
130  * @return ::OC_STACK_OK for success, or errorcode otherwise.
131  */
132 OCStackResult RemoveCredential(const OicUuid_t *subject);
133
134 /**
135  * Function to remove the credential from SVR DB.
136  *
137  * @param credId is the Credential ID to be deleted.
138  *
139  * @return ::OC_STACK_OK for success, or errorcode otherwise.
140  */
141 OCStackResult RemoveCredentialByCredId(uint16_t credId);
142
143 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
144 /**
145  * This internal callback is used by lower stack (i.e. CA layer) to
146  * retrieve PSK credentials from RI security layer.
147  *
148  * @param type of PSK data required by CA layer during DTLS handshake.
149  * @param desc Additional request information.
150  * @param desc_len is the actual length of desc.
151  * @param result  is must be filled with the requested information.
152  * @param result_length is the maximum size of @p result.
153  *
154  * @return The number of bytes written to @p result or a value
155  *         less than zero on error.
156  */
157 int32_t GetDtlsPskCredentials( CADtlsPskCredType_t type,
158               const unsigned char *desc, size_t desc_len,
159               unsigned char *result, size_t result_length);
160
161 /**
162  * Add temporal PSK to PIN based OxM.
163  *
164  * @param tmpSubject is the UUID of target device
165  * @param credType is the type of credential to be added
166  * @param pin is the numeric characters
167  * @param pinSize is the length of 'pin'
168  * @param rownerID Resource owner's UUID
169  * @param tmpCredSubject is the generated credential's subject.
170  *
171  * @return ::OC_STACK_OK for success or else errorcode.
172  */
173 OCStackResult AddTmpPskWithPIN(const OicUuid_t* tmpSubject, OicSecCredType_t credType,
174                             const char * pin, size_t pinSize,
175                             const OicUuid_t * rownerID,
176                             OicUuid_t* tmpCredSubject);
177
178 #endif // __WITH_DTLS__ or __WITH_TLS__
179
180 /**
181  * Function to getting credential list
182  *
183  * @return ::credential list
184  */
185 const OicSecCred_t* GetCredList();
186
187 /**
188  * Function to deallocate allocated memory to OicSecCred_t.
189  *
190  * @param cred pointer to cred type.
191  *
192  */
193 void DeleteCredList(OicSecCred_t* cred);
194
195 /**
196  * Internal function to update resource owner
197  *
198  * @param newROwner new owner
199  *
200  * @retval ::OC_STACK_OK for Success, otherwise some error value
201  */
202 OCStackResult SetCredRownerId(const OicUuid_t* newROwner);
203
204 /**
205  * Gets the OicUuid_t value for the rownerid of the cred resource.
206  *
207  * @param rowneruuid a pointer to be assigned to the rowneruuid property
208  * @return ::OC_STACK_OK if rowneruuid is assigned correctly, else ::OC_STACK_ERROR.
209  */
210 OCStackResult GetCredRownerId(OicUuid_t *rowneruuid);
211
212 #if defined(__WITH_TLS__) || defined(__WITH_DTLS__)
213 /**
214  * Used by mbedTLS to retrieve trusted CA certificates
215  *
216  * @param[out] crt certificates to be filled.
217  * @param[in] usage credential usage string.
218  */
219 void GetDerCaCert(ByteArray_t * crt, const char * usage);
220 /**
221  * Used by mbedTLS to retrieve own certificate chain
222  *
223  * @param[out] crt certificate chain to be filled.
224  * @param[in] usage credential usage string.
225  */
226 void GetDerOwnCert(ByteArray_t * crt, const char * usage);
227 /**
228  * Used by mbedTLS to retrieve owm private key
229  *
230  * @param[out] key key to be filled.
231  * @param[in] usage credential usage string.
232  */
233 void GetDerKey(ByteArray_t * key, const char * usage);
234 /**
235  * Used by CA to retrieve credential types
236  *
237  * @param[out] key key to be filled.
238  * @param[in] usage credential usage string.
239  */
240 void InitCipherSuiteListInternal(bool *list, const char * usage);
241 #endif // __WITH_TLS__
242 #ifdef __cplusplus
243 }
244 #endif
245
246 #endif //IOTVT_SRM_CREDR_H