Imported Upstream version 0.9.2
[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
22 #include "provisioningmanager.h"
23 #include "credentialgenerator.h"
24 #include "oic_malloc.h"
25 #include "logger.h"
26 #include "credresource.h"
27 #include "ocrandom.h"
28 #include "base64.h"
29 #define TAG "SPProvisionAPI"
30 #define KEY_LENGTH 16
31
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)
37 {
38
39     if (NULL == ptDeviceId || NULL == firstDeviceId || NULL == secondDeviceId)
40     {
41         return SP_RESULT_INVALID_PARAM;
42     }
43     uint8_t privData[KEY_LENGTH] = {0,};
44     OCFillRandomMem(privData, KEY_LENGTH);
45
46     uint32_t outLen = 0;
47     char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(privData)) + 1] = {};
48     B64Result b64Ret = b64Encode(privData, sizeof(privData), base64Buff,
49                                 sizeof(base64Buff), &outLen);
50     if (B64_OK != b64Ret)
51     {
52         OC_LOG(ERROR, TAG, "Error while encoding key");
53         return SP_RESULT_INTERNAL_ERROR;
54     }
55
56     // TODO currently owner array is 1. only provisioning tool's id.
57     OicSecCred_t *tempFirstCred =  GenerateCredential(secondDeviceId, type, NULL, base64Buff, 1,
58                                    ptDeviceId);
59     if (NULL == tempFirstCred)
60     {
61         OC_LOG(ERROR, TAG, "Error while generating credential.");
62         return SP_RESULT_INTERNAL_ERROR;
63     }
64     // TODO currently owner array is 1. only provisioning tool's id.
65     OicSecCred_t *tempSecondCred =  GenerateCredential(firstDeviceId, type, NULL, base64Buff, 1,
66                                     ptDeviceId);
67     if (NULL == tempSecondCred)
68     {
69         DeleteCredList(tempFirstCred);
70         OC_LOG(ERROR, TAG, "Error while generating credential.");
71         return SP_RESULT_INTERNAL_ERROR;
72     }
73     *firstCred = tempFirstCred;
74     *secondCred = tempSecondCred;
75     return SP_RESULT_SUCCESS;
76 }