Merge branch 'master' into easysetup
[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 "oic_malloc.h"
23 #include "logger.h"
24 #include "credresource.h"
25 #include "ocrandom.h"
26 #include "base64.h"
27 #include "stdbool.h"
28 #include "securevirtualresourcetypes.h"
29
30 #define TAG "SRPAPI-CG"
31
32 /**
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.
38  * */
39 #define PM_VERIFY_SUCCESS(tag, op, errCode, logLevel) { if (!(op)) \
40                        {OC_LOG((logLevel), tag, #op " failed!!"); res = errCode; goto bail;} }
41 /**
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.
46  * */
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;} }
49
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)
54 {
55
56     if (NULL == ptDeviceId || NULL == firstDeviceId || NULL != *firstCred || \
57         NULL == secondDeviceId || NULL != *secondCred)
58     {
59         OC_LOG(INFO, TAG, "Invalid params");
60         return OC_STACK_INVALID_PARAM;
61     }
62     if(!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
63     {
64         OC_LOG(INFO, TAG, "Invalid key size");
65         return OC_STACK_INVALID_PARAM;
66     }
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;
72
73     size_t privDataKeySize = keySize;
74
75     privData = (uint8_t*) OICCalloc(privDataKeySize,sizeof(uint8_t));
76     PM_VERIFY_NON_NULL(TAG, privData, OC_STACK_NO_MEMORY, ERROR);
77
78     OCFillRandomMem(privData,privDataKeySize);
79
80     uint32_t outLen = 0;
81
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,
86                                  memReq, &outLen);
87     PM_VERIFY_SUCCESS(TAG, B64_OK == b64Ret, OC_STACK_ERROR, ERROR);
88
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);
92
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);
96
97     *firstCred = tempFirstCred;
98     *secondCred = tempSecondCred;
99     res = OC_STACK_OK;
100
101 bail:
102     OICFree(privData);
103     OICFree(base64Buff);
104
105     if(res != OC_STACK_OK)
106     {
107         OICFree(tempFirstCred);
108         OICFree(tempSecondCred);
109         *firstCred = NULL;
110         *secondCred = NULL;
111     }
112
113     return res;
114 }