Merge "Merge branch 'master' into notification-service" into notification-service
[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 for given credId.
61  *
62  * @param credId for which credential is required.
63  *
64  * @return reference to @ref OicSecCred_t, if credential is found, else NULL, if credential
65  * not found.
66  */
67 OicSecCred_t* GetCredResourceDataByCredId(const uint16_t credId);
68
69 /**
70  * This function converts credential data into CBOR format.
71  * Caller needs to invoke 'free' when done using returned string.
72  *
73  * @param cred is the pointer to instance of OicSecCred_t structure.
74  * @param cborPayload is the CBOR converted value.
75  * @param cborSize is the size of the CBOR.
76  * @param secureFlag shows fill or not private key.
77  *
78  * @return ::OC_STACK_OK if conversion is successful, else ::OC_STACK_ERROR if unsuccessful.
79  */
80 OCStackResult CredToCBORPayload(const OicSecCred_t* cred, uint8_t **cborPayload,
81                                 size_t *cborSize, int secureFlag);
82
83 /**
84  * This function generates the bin credential data.
85  *
86  * @param subject pointer to subject of this credential.
87  * @param credType credential type.
88  * @param publicData public data such as public key.
89  * @param privateData private data such as private key.
90  * @param rownerID Resource owner's UUID.
91  *
92  * @return pointer to instance of @ref OicSecCred_t if successful. else NULL in case of error.
93
94  */
95 OicSecCred_t * GenerateCredential(const OicUuid_t* subject, OicSecCredType_t credType,
96                      const OicSecCert_t * publicData, const OicSecKey_t * privateData,
97                      const OicUuid_t * rownerID);
98
99 /**
100  * This function adds the new cred to the credential list.
101  *
102  * @param cred is the pointer to new credential.
103  *
104  * @return ::OC_STACK_OK, cred not NULL and persistent storage gets updated.
105  * ::OC_STACK_ERROR, cred is NULL or fails to update persistent storage.
106  */
107 OCStackResult AddCredential(OicSecCred_t * cred);
108
109 /**
110  * Function to remove the credential from SVR DB.
111  *
112  * @param credId is the Credential ID to be deleted.
113  *
114  * @return ::OC_STACK_OK for success, or errorcode otherwise.
115  */
116 OCStackResult RemoveCredential(const OicUuid_t *credId);
117
118 #if defined(__WITH_DTLS__)
119 /**
120  * This internal callback is used by lower stack (i.e. CA layer) to
121  * retrieve PSK credentials from RI security layer.
122  *
123  * @param type of PSK data required by CA layer during DTLS handshake.
124  * @param desc Additional request information.
125  * @param desc_len is the actual length of desc.
126  * @param result  is must be filled with the requested information.
127  * @param result_length is the maximum size of @p result.
128  *
129  * @return The number of bytes written to @p result or a value
130  *         less than zero on error.
131  */
132 int32_t GetDtlsPskCredentials( CADtlsPskCredType_t type,
133               const unsigned char *desc, size_t desc_len,
134               unsigned char *result, size_t result_length);
135
136 /**
137  * Add temporal PSK to PIN based OxM.
138  *
139  * @param tmpSubject is the UUID of target device
140  * @param credType is the type of credential to be added
141  * @param pin is the numeric characters
142  * @param pinSize is the length of 'pin'
143  * @param rownerID Resource owner's UUID
144  * @param tmpCredSubject is the generated credential's subject.
145  *
146  * @return ::OC_STACK_OK for success or else errorcode.
147  */
148 OCStackResult AddTmpPskWithPIN(const OicUuid_t* tmpSubject, OicSecCredType_t credType,
149                             const char * pin, size_t pinSize,
150                             const OicUuid_t * rownerID,
151                             OicUuid_t* tmpCredSubject);
152
153 #endif /* __WITH_DTLS__ */
154
155 #ifdef __WITH_X509__
156 /**
157  * This function is used toretrieve certificate credentials from RI security layer.
158  *
159  * @param credInfo is the binary structure containing certificate credentials
160  *
161  * @return 0 on success.
162  */
163 int GetDtlsX509Credentials(CADtlsX509Creds_t *credInfo);
164 #endif /*__WITH_X509__*/
165
166 /**
167  * Function to deallocate allocated memory to OicSecCred_t.
168  *
169  * @param cred pointer to cred type.
170  *
171  */
172 void DeleteCredList(OicSecCred_t* cred);
173
174 /**
175  * Internal function to update resource owner
176  *
177  * @param newROwner new owner
178  *
179  * @retval ::OC_STACK_OK for Success, otherwise some error value
180  */
181 OCStackResult SetCredRownerId(const OicUuid_t* newROwner);
182
183 /**
184  * Gets the OicUuid_t value for the rownerid of the cred resource.
185  *
186  * @param rowneruuid a pointer to be assigned to the rowneruuid property
187  * @return ::OC_STACK_OK if rowneruuid is assigned correctly, else ::OC_STACK_ERROR.
188  */
189 OCStackResult GetCredRownerId(OicUuid_t *rowneruuid);
190
191 #ifdef __WITH_TLS__
192 /**
193  * @def CA_SUBJECT_ID
194  * @brief subject uuid for credential with CA certificates
195  */
196 #define CA_SUBJECT_ID ("00000000-0000-0000-0000-000000000000")
197 void GetPkixInfo(PkiInfo_t * inf);
198 #endif
199 #ifdef __cplusplus
200 }
201 #endif
202
203 #endif //IOTVT_SRM_CREDR_H