1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef IOTVT_SRM_CREDR_H
22 #define IOTVT_SRM_CREDR_H
24 #include "cainterface.h"
25 #include "securevirtualresourcetypes.h"
34 * Initialize credential resource by loading data from persistent storage.
36 * @return ::OC_STACK_OK, if initialization is successful, else ::OC_STACK_ERROR if
37 * initialization fails.
39 OCStackResult InitCredResource();
42 * Perform cleanup for credential resources.
44 * @return ::OC_STACK_OK, if no errors. ::OC_STACK_ERROR, if stack process error.
45 * ::OC_STACK_NO_RESOURCE, if resource not found.
46 * ::OC_STACK_INVALID_PARAM, if invalid param.
48 OCStackResult DeInitCredResource();
51 * This method is used by tinydtls/SRM to retrieve credential for given subject.
53 * @param subjectId for which credential is required.
55 * @return reference to @ref OicSecCred_t, if credential is found, else NULL, if credential
58 OicSecCred_t* GetCredResourceData(const OicUuid_t* subjectId);
61 * This method is used by SRM to retrieve credential entry for given credId.
63 * @note Caller needs to release this memory by calling DeleteCredList().
65 * @param credId for which credential is required.
67 * @return reference to @ref OicSecCred_t, if credential is found, else NULL, if credential
70 OicSecCred_t* GetCredEntryByCredId(const uint16_t credId);
73 * This function converts credential data into CBOR format.
74 * Caller needs to invoke 'free' when done using returned string.
76 * @param cred is the pointer to instance of OicSecCred_t structure.
77 * @param cborPayload is the CBOR converted value.
78 * @param cborSize is the size of the CBOR.
79 * @param secureFlag shows fill or not private key.
81 * @return ::OC_STACK_OK if conversion is successful, else ::OC_STACK_ERROR if unsuccessful.
83 OCStackResult CredToCBORPayload(const OicSecCred_t* cred, uint8_t **cborPayload,
84 size_t *cborSize, int secureFlag);
88 * Function to check the credential access of SubOwner
90 * @param[in] uuid SubOwner's UUID
91 * @param[in] cborPayload CBOR payload of credential
92 * @param[in] size Byte length of cborPayload
94 * @return ::true for valid access, otherwise invalid access
96 bool IsValidCredentialAccessForSubOwner(const OicUuid_t* uuid, const uint8_t *cborPayload, size_t size);
97 #endif //MULTIPLE_OWNER
100 * This function generates the bin credential data.
102 * @param subject pointer to subject of this credential.
103 * @param credType credential type.
104 * @param publicData public data such as public key.
105 * @param privateData private data such as private key.
106 * @param rownerID Resource owner's UUID.
107 * @param eownerID Entry owner's UUID.
109 * @return pointer to instance of @ref OicSecCred_t if successful. else NULL in case of error.
112 OicSecCred_t * GenerateCredential(const OicUuid_t* subject, OicSecCredType_t credType,
113 const OicSecKey_t * publicData, const OicSecKey_t * privateData,
114 const OicUuid_t * rownerID, const OicUuid_t * eownerID);
117 * This function adds the new cred to the credential list.
119 * @param cred is the pointer to new credential.
121 * @return ::OC_STACK_OK, cred not NULL and persistent storage gets updated.
122 * ::OC_STACK_ERROR, cred is NULL or fails to update persistent storage.
124 OCStackResult AddCredential(OicSecCred_t * cred);
127 * Function to remove the credential from SVR DB.
129 * @param subject is the Credential Subject to be deleted.
131 * @return ::OC_STACK_OK for success, or errorcode otherwise.
133 OCStackResult RemoveCredential(const OicUuid_t *subject);
136 * Function to remove the credential from SVR DB.
138 * @param credId is the Credential ID to be deleted.
140 * @return ::OC_STACK_OK for success, or errorcode otherwise.
142 OCStackResult RemoveCredentialByCredId(uint16_t credId);
144 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
146 * This internal callback is used by lower stack (i.e. CA layer) to
147 * retrieve PSK credentials from RI security layer.
149 * @param type of PSK data required by CA layer during DTLS handshake.
150 * @param desc Additional request information.
151 * @param desc_len is the actual length of desc.
152 * @param result is must be filled with the requested information.
153 * @param result_length is the maximum size of @p result.
155 * @return The number of bytes written to @p result or a value
156 * less than zero on error.
158 int32_t GetDtlsPskCredentials( CADtlsPskCredType_t type,
159 const unsigned char *desc, size_t desc_len,
160 unsigned char *result, size_t result_length);
163 * Add temporal PSK to PIN based OxM.
165 * @param tmpSubject is the UUID of target device
166 * @param credType is the type of credential to be added
167 * @param pin is the numeric characters
168 * @param pinSize is the length of 'pin'
169 * @param rownerID Resource owner's UUID
170 * @param tmpCredSubject is the generated credential's subject.
172 * @return ::OC_STACK_OK for success or else errorcode.
174 OCStackResult AddTmpPskWithPIN(const OicUuid_t* tmpSubject, OicSecCredType_t credType,
175 const char * pin, size_t pinSize,
176 const OicUuid_t * rownerID,
177 OicUuid_t* tmpCredSubject);
179 #endif // __WITH_DTLS__ or __WITH_TLS__
182 * Function to getting credential list
184 * @return instance of @ref OicSecCred_t
186 const OicSecCred_t* GetCredList();
189 * Function to deallocate allocated memory to OicSecCred_t.
191 * @param cred pointer to cred type.
194 void DeleteCredList(OicSecCred_t* cred);
197 * Internal function to update resource owner
199 * @param newROwner new owner
201 * @retval ::OC_STACK_OK for Success, otherwise some error value
203 OCStackResult SetCredRownerId(const OicUuid_t* newROwner);
206 * Gets the OicUuid_t value for the rownerid of the cred resource.
208 * @param rowneruuid a pointer to be assigned to the rowneruuid property
209 * @return ::OC_STACK_OK if rowneruuid is assigned correctly, else ::OC_STACK_ERROR.
211 OCStackResult GetCredRownerId(OicUuid_t *rowneruuid);
213 #if defined(__WITH_TLS__) || defined(__WITH_DTLS__)
215 * Used by role certificate validator to get CA certificates as PEM
217 * @param[out] crt certificates to be filled.
218 * @param[in] usage credential usage string.
220 OCStackResult GetPemCaCert(ByteArray_t * crt, const char * usage);
222 * Used by mbedTLS to retrieve own certificate chain
224 * @param[out] crt certificate chain to be filled.
225 * @param[in] usage credential usage string.
227 void GetPemOwnCert(ByteArray_t * crt, const char * usage);
229 * Used by mbedTLS to retrieve owm private key
231 * @param[out] key key to be filled.
232 * @param[in] usage credential usage string.
234 void GetDerKey(ByteArray_t * key, const char * usage);
236 * Used by CA to retrieve credential types
238 * @param[out] key key to be filled.
239 * @param[in] usage credential usage string.
241 void InitCipherSuiteListInternal(bool *list, const char * usage);
242 #endif // __WITH_TLS__
244 // Helpers shared by cred and roles resources
245 CborError SerializeEncodingToCbor(CborEncoder *rootMap, const char *tag, const OicSecKey_t *value);
246 CborError SerializeSecOptToCbor(CborEncoder *rootMap, const char *tag, const OicSecOpt_t *value);
247 CborError DeserializeEncodingFromCbor(CborValue *rootMap, OicSecKey_t *value);
248 CborError DeserializeSecOptFromCbor(CborValue *rootMap, OicSecOpt_t *value);
254 #endif //IOTVT_SRM_CREDR_H