replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / credentialgenerator.c
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics 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 #include <string.h>
21 #include "credentialgenerator.h"
22 #include "base64.h"
23 #include "oic_malloc.h"
24 #include "oic_string.h"
25 #include "ocpayload.h"
26 #include "payload_logging.h"
27 #include "credresource.h"
28 #include "ocrandom.h"
29 #include "srmutility.h"
30 #include "stdbool.h"
31 #include "securevirtualresourcetypes.h"
32
33 #define TAG "OIC_SRPAPI_CG"
34
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)
38 {
39     if (NULL == ptDeviceId || NULL == firstDeviceId || NULL == firstCred || NULL != *firstCred || \
40         NULL == secondDeviceId || NULL == secondCred || NULL != *secondCred)
41     {
42         OIC_LOG(INFO, TAG, "Invalid params");
43         return OC_STACK_INVALID_PARAM;
44     }
45     if(!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
46     {
47         OIC_LOG(INFO, TAG, "Invalid key size");
48         return OC_STACK_INVALID_PARAM;
49     }
50     OCStackResult res = OC_STACK_ERROR;
51     OicSecCred_t *tempFirstCred = NULL;
52     OicSecCred_t *tempSecondCred = NULL;
53
54     size_t privDataKeySize = keySize;
55
56     uint8_t *privData = (uint8_t *)OICCalloc(privDataKeySize, sizeof(uint8_t));
57     VERIFY_NON_NULL(TAG, privData, ERROR);
58     OicSecKey_t privKey = {.data=privData, .len=keySize};
59
60     OCFillRandomMem(privData, privDataKeySize);
61
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);
65
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);
69
70     *firstCred = tempFirstCred;
71     *secondCred = tempSecondCred;
72     res = OC_STACK_OK;
73
74 exit:
75     OICClearMemory(privData, privDataKeySize);
76     OICFree(privData);
77
78     if(res != OC_STACK_OK)
79     {
80         OICFree(tempFirstCred);
81         OICFree(tempSecondCred);
82         *firstCred = NULL;
83         *secondCred = NULL;
84     }
85
86     return res;
87 }