1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 #include "credentialgenerator.h"
22 #include "oic_malloc.h"
24 #include "credresource.h"
28 #include "securevirtualresourcetypes.h"
30 #define TAG "SRPAPI-CG"
33 * @def PM_VERIFY_SUCCESS
34 * @brief Macro to verify success of operation.
35 * eg: PM_VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), OC_STACK_ERROR, ERROR);
36 * @note Invoking function must define "bail:" label for goto functionality to work correctly and
37 * must define "OCStackResult res" for setting error code.
39 #define PM_VERIFY_SUCCESS(tag, op, errCode, logLevel) { if (!(op)) \
40 {OC_LOG((logLevel), tag, #op " failed!!"); res = errCode; goto bail;} }
42 * @def PM_VERIFY_NON_NULL
43 * @brief Macro to verify argument is not equal to NULL.
44 * eg: PM_VERIFY_NON_NULL(TAG, ptrData, ERROR);
45 * @note Invoking function must define "bail:" label for goto functionality to work correctly.
47 #define PM_VERIFY_NON_NULL(tag, arg, errCode, logLevel) { if (NULL == (arg)) \
48 { OC_LOG((logLevel), tag, #arg " is NULL"); res = errCode; goto bail;} }
50 OCStackResult PMGeneratePairWiseCredentials(OicSecCredType_t type, size_t keySize,
51 const OicUuid_t *ptDeviceId,
52 const OicUuid_t *firstDeviceId, const OicUuid_t *secondDeviceId,
53 OicSecCred_t **firstCred, OicSecCred_t **secondCred)
56 if (NULL == ptDeviceId || NULL == firstDeviceId || NULL != *firstCred || \
57 NULL == secondDeviceId || NULL != *secondCred)
59 OC_LOG(INFO, TAG, "Invalid params");
60 return OC_STACK_INVALID_PARAM;
62 if(!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
64 OC_LOG(INFO, TAG, "Invalid key size");
65 return OC_STACK_INVALID_PARAM;
67 OCStackResult res = OC_STACK_ERROR;
68 uint8_t* privData = NULL;
69 char* base64Buff = NULL;
70 OicSecCred_t *tempFirstCred = NULL;
71 OicSecCred_t *tempSecondCred = NULL;
73 size_t privDataKeySize = keySize;
75 privData = (uint8_t*) OICCalloc(privDataKeySize,sizeof(uint8_t));
76 PM_VERIFY_NON_NULL(TAG, privData, OC_STACK_NO_MEMORY, ERROR);
78 OCFillRandomMem(privData,privDataKeySize);
82 base64Buff = (char*) OICCalloc(B64ENCODE_OUT_SAFESIZE(privDataKeySize) + 1, sizeof(char));
83 PM_VERIFY_NON_NULL(TAG, base64Buff, OC_STACK_NO_MEMORY, ERROR);
84 int memReq = (B64ENCODE_OUT_SAFESIZE(privDataKeySize) + 1) * sizeof(char);
85 B64Result b64Ret = b64Encode(privData, privDataKeySize*sizeof(uint8_t), base64Buff,
87 PM_VERIFY_SUCCESS(TAG, B64_OK == b64Ret, OC_STACK_ERROR, ERROR);
89 // TODO: currently owner array is 1. only provisioning tool's id.
90 tempFirstCred = GenerateCredential(secondDeviceId, type, NULL, base64Buff, 1, ptDeviceId);
91 PM_VERIFY_NON_NULL(TAG, tempFirstCred, OC_STACK_ERROR, ERROR);
93 // TODO: currently owner array is 1. only provisioning tool's id.
94 tempSecondCred = GenerateCredential(firstDeviceId, type, NULL, base64Buff, 1, ptDeviceId);
95 PM_VERIFY_NON_NULL(TAG, tempSecondCred, OC_STACK_ERROR, ERROR);
97 *firstCred = tempFirstCred;
98 *secondCred = tempSecondCred;
105 if(res != OC_STACK_OK)
107 OICFree(tempFirstCred);
108 OICFree(tempSecondCred);