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 * *****************************************************************/
22 #include "provisioningmanager.h"
23 #include "credentialgenerator.h"
24 #include "oic_malloc.h"
26 #include "credresource.h"
29 #define TAG "SPProvisionAPI"
32 SPResult SPGeneratePairWiseCredentials(OicSecCredType_t type, const OicUuid_t *ptDeviceId,
33 const OicUuid_t *firstDeviceId,
34 const OicUuid_t *secondDeviceId,
35 OicSecCred_t **firstCred,
36 OicSecCred_t **secondCred)
39 if (NULL == ptDeviceId || NULL == firstDeviceId || NULL == secondDeviceId)
41 return SP_RESULT_INVALID_PARAM;
43 uint8_t privData[KEY_LENGTH] = {0,};
44 OCFillRandomMem(privData, KEY_LENGTH);
47 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(privData)) + 1] = {};
48 B64Result b64Ret = b64Encode(privData, sizeof(privData), base64Buff,
49 sizeof(base64Buff), &outLen);
52 OC_LOG(ERROR, TAG, "Error while encoding key");
53 return SP_RESULT_INTERNAL_ERROR;
56 // TODO currently owner array is 1. only provisioning tool's id.
57 OicSecCred_t *tempFirstCred = GenerateCredential(secondDeviceId, type, NULL, base64Buff, 1,
59 if (NULL == tempFirstCred)
61 OC_LOG(ERROR, TAG, "Error while generating credential.");
62 return SP_RESULT_INTERNAL_ERROR;
64 // TODO currently owner array is 1. only provisioning tool's id.
65 OicSecCred_t *tempSecondCred = GenerateCredential(firstDeviceId, type, NULL, base64Buff, 1,
67 if (NULL == tempSecondCred)
69 DeleteCredList(tempFirstCred);
70 OC_LOG(ERROR, TAG, "Error while generating credential.");
71 return SP_RESULT_INTERNAL_ERROR;
73 *firstCred = tempFirstCred;
74 *secondCred = tempSecondCred;
75 return SP_RESULT_SUCCESS;