Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / oxmrandompin.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
21 #include <memory.h>
22
23 #include "ocstack.h"
24 #include "securevirtualresourcetypes.h"
25 #include "doxmresource.h"
26 #include "credresource.h"
27 #include "cacommon.h"
28 #include "cainterface.h"
29 #include "ocrandom.h"
30 #include "oic_malloc.h"
31 #include "logger.h"
32 #include "pbkdf2.h"
33 #include "global.h"
34 #include "base64.h"
35 #include "oxmrandompin.h"
36 #include "ownershiptransfermanager.h"
37 #include "pinoxmcommon.h"
38
39 #define TAG "OXM_RandomPIN"
40
41 char* CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx)
42 {
43     if(!otmCtx || !otmCtx->selectedDeviceInfo)
44     {
45         return NULL;
46     }
47
48     otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_RANDOM_DEVICE_PIN;
49
50     OicUuid_t uuidPT = {.id={0}};
51     if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
52     {
53         OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
54         return NULL;
55     }
56     memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id, UUID_LENGTH);
57
58     return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
59 }
60
61 char* CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx)
62 {
63     if(!otmCtx || !otmCtx->selectedDeviceInfo)
64     {
65         return NULL;
66     }
67
68     OicUuid_t uuidPT = {.id={0}};
69
70     if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
71     {
72         OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
73         return NULL;
74     }
75     memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
76     otmCtx->selectedDeviceInfo->doxm->owned = true;
77
78     return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
79 }
80
81 OCStackResult InputPinCodeCallback(OTMContext_t* otmCtx)
82 {
83     if(!otmCtx || !otmCtx->selectedDeviceInfo)
84     {
85         return OC_STACK_INVALID_PARAM;
86     }
87
88     uint8_t pinData[OXM_RANDOM_PIN_SIZE + 1];
89
90     OCStackResult res = InputPin((char*)pinData, OXM_RANDOM_PIN_SIZE + 1);
91     if(OC_STACK_OK != res)
92     {
93         OC_LOG(ERROR, TAG, "Failed to input PIN");
94         return res;
95     }
96
97     OicUuid_t deviceUUID = {.id={0}};
98     if (OC_STACK_OK != GetDoxmDeviceID(&deviceUUID))
99     {
100         OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
101         return OC_STACK_ERROR;
102     }
103
104     res = AddTmpPskWithPIN(&otmCtx->selectedDeviceInfo->doxm->deviceID,
105                            SYMMETRIC_PAIR_WISE_KEY,
106                            (char*)pinData, OXM_RANDOM_PIN_SIZE,
107                            1, &deviceUUID, &otmCtx->subIdForPinOxm);
108     if(res != OC_STACK_OK)
109     {
110         OC_LOG_V(ERROR, TAG, "Failed to save the temporal PSK : %d", res);
111     }
112
113     return res;
114 }
115
116 OCStackResult CreateSecureSessionRandomPinCallbak(OTMContext_t* otmCtx)
117 {
118     OC_LOG(INFO, TAG, "IN CreateSecureSessionRandomPinCallbak");
119
120     if(!otmCtx || !otmCtx->selectedDeviceInfo)
121     {
122         return OC_STACK_INVALID_PARAM;
123     }
124
125     CAResult_t caresult = CAEnableAnonECDHCipherSuite(false);
126     if (CA_STATUS_OK != caresult)
127     {
128         OC_LOG_V(ERROR, TAG, "Unable to disable anon cipher suite");
129         return OC_STACK_ERROR;
130     }
131     OC_LOG(INFO, TAG, "Anonymous cipher suite disabled.");
132
133     caresult  = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256);
134     if (CA_STATUS_OK != caresult)
135     {
136         OC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256");
137         return OC_STACK_ERROR;
138     }
139     OC_LOG(INFO, TAG, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 cipher suite selected.");
140
141
142     OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
143     CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
144     if(NULL == endpoint)
145     {
146         return OC_STACK_NO_MEMORY;
147     }
148     memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
149     endpoint->port = selDevInfo->securePort;
150     caresult = CAInitiateHandshake(endpoint);
151     OICFree(endpoint);
152     if (CA_STATUS_OK != caresult)
153     {
154         OC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
155         return OC_STACK_ERROR;
156     }
157
158     OC_LOG(INFO, TAG, "OUT CreateSecureSessionRandomPinCallbak");
159
160     return OC_STACK_OK;
161 }