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"
23 #include "oic_malloc.h"
24 #include "oic_string.h"
25 #include "ocpayload.h"
26 #include "payload_logging.h"
27 #include "credresource.h"
29 #include "srmutility.h"
31 #include "securevirtualresourcetypes.h"
33 #define TAG "SRPAPI-CG"
35 OCStackResult PMGeneratePairWiseCredentials(OicSecCredType_t type, size_t keySize,
36 const OicUuid_t *ptDeviceId, const OicUuid_t *firstDeviceId,
37 const OicUuid_t *secondDeviceId, OicSecCred_t **firstCred, OicSecCred_t **secondCred)
39 if (NULL == ptDeviceId || NULL == firstDeviceId || NULL == firstCred || NULL != *firstCred || \
40 NULL == secondDeviceId || NULL == secondCred || NULL != *secondCred)
42 OIC_LOG(INFO, TAG, "Invalid params");
43 return OC_STACK_INVALID_PARAM;
45 if(!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
47 OIC_LOG(INFO, TAG, "Invalid key size");
48 return OC_STACK_INVALID_PARAM;
50 OCStackResult res = OC_STACK_ERROR;
51 OicSecCred_t *tempFirstCred = NULL;
52 OicSecCred_t *tempSecondCred = NULL;
54 size_t privDataKeySize = keySize;
56 uint8_t *privData = (uint8_t *)OICCalloc(privDataKeySize, sizeof(uint8_t));
57 VERIFY_NON_NULL(TAG, privData, ERROR);
58 OicSecKey_t privKey = {privData, keySize};
60 OCFillRandomMem(privData, privDataKeySize);
62 // TODO: currently owner array is 1. only provisioning tool's id.
63 tempFirstCred = GenerateCredential(secondDeviceId, type, NULL, &privKey, ptDeviceId, NULL);
64 VERIFY_NON_NULL(TAG, tempFirstCred, ERROR);
66 // TODO: currently owner array is 1. only provisioning tool's id.
67 tempSecondCred = GenerateCredential(firstDeviceId, type, NULL, &privKey, ptDeviceId, NULL);
68 VERIFY_NON_NULL(TAG, tempSecondCred, ERROR);
70 *firstCred = tempFirstCred;
71 *secondCred = tempSecondCred;
77 if(res != OC_STACK_OK)
79 OICFree(tempFirstCred);
80 OICFree(tempSecondCred);