dcf72141b07d06d02c4104e9daa180c1bbcea8e8
[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 "OIC_OXM_RandomPIN"
40
41 OCStackResult CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
42 {
43     if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
44     {
45         return OC_STACK_INVALID_PARAM;
46     }
47
48     otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_RANDOM_DEVICE_PIN;
49
50     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
51 }
52
53 OCStackResult CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
54 {
55     if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
56     {
57         return OC_STACK_INVALID_PARAM;
58     }
59
60     OicUuid_t uuidPT = {.id={0}};
61     *payload = NULL;
62     *size = 0;
63
64     if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
65     {
66         OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
67         return OC_STACK_ERROR;
68     }
69     memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
70
71     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
72 }
73
74 OCStackResult InputPinCodeCallback(OTMContext_t *otmCtx)
75 {
76     if (!otmCtx || !otmCtx->selectedDeviceInfo)
77     {
78         return OC_STACK_INVALID_PARAM;
79     }
80
81     uint8_t pinData[OXM_RANDOM_PIN_MAX_SIZE + 1] = {0};
82
83     OCStackResult res = InputPin((char*)pinData, sizeof(pinData));
84     if (OC_STACK_OK != res)
85     {
86         OIC_LOG(ERROR, TAG, "Failed to input PIN");
87         return res;
88     }
89
90     /**
91      * Since PSK will be used directly while PIN based ownership transfer,
92      * Credential should not be saved into SVR.
93      * For this reason, We will use a temporary get_psk_info callback to random PIN OxM.
94      */
95     //in case of OTM
96     if(!(otmCtx->selectedDeviceInfo->doxm->owned))
97     {
98         if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskForRandomPinOxm))
99         {
100             OIC_LOG(ERROR, TAG, "Failed to register DTLS credentials handler for random PIN OxM.");
101             res = OC_STACK_ERROR;
102         }
103     }
104 #ifdef _ENABLE_MULTIPLE_OWNER_
105     //in case of MOT
106     else if(otmCtx->selectedDeviceInfo->doxm->owned &&
107             otmCtx->selectedDeviceInfo->doxm->mom &&
108             OIC_MULTIPLE_OWNER_DISABLE != otmCtx->selectedDeviceInfo->doxm->mom->mode)
109     {
110         if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskForMotRandomPinOxm))
111         {
112             OIC_LOG(ERROR, TAG, "Failed to register TLS credentials handler for random PIN OxM.");
113             res = OC_STACK_ERROR;
114         }
115     }
116 #endif //_ENABLE_MULTIPLE_OWNER_
117
118     //Set the device id to derive temporal PSK
119     SetUuidForPinBasedOxm(&(otmCtx->selectedDeviceInfo->doxm->deviceID));
120
121     return res;
122 }
123
124 OCStackResult CreateSecureSessionRandomPinCallback(OTMContext_t* otmCtx)
125 {
126     OIC_LOG(INFO, TAG, "IN CreateSecureSessionRandomPinCallbak");
127
128     if (!otmCtx || !otmCtx->selectedDeviceInfo)
129     {
130         return OC_STACK_INVALID_PARAM;
131     }
132
133     CAResult_t caresult = CAEnableAnonECDHCipherSuite(false);
134     if (CA_STATUS_OK != caresult)
135     {
136         OIC_LOG_V(ERROR, TAG, "Unable to disable anon cipher suite");
137         return OC_STACK_ERROR;
138     }
139     OIC_LOG(INFO, TAG, "Anonymous cipher suite disabled.");
140
141     caresult  = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256, otmCtx->selectedDeviceInfo->endpoint.adapter);
142     if (CA_STATUS_OK != caresult)
143     {
144         OIC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256");
145         return OC_STACK_ERROR;
146     }
147     OIC_LOG(INFO, TAG, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 cipher suite selected.");
148
149     OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
150     CAEndpoint_t endpoint;
151     memcpy(&endpoint, &selDevInfo->endpoint, sizeof(CAEndpoint_t));
152
153     if(CA_ADAPTER_IP == endpoint.adapter)
154     {
155         endpoint.port = selDevInfo->securePort;
156         caresult = CAInitiateHandshake(&endpoint);
157     }
158 #ifdef __WITH_TLS__
159     else
160     {
161         endpoint.port = selDevInfo->tcpPort;
162         caresult = CAinitiateSslHandshake(&endpoint);
163     }
164 #endif
165     if (CA_STATUS_OK != caresult)
166     {
167         OIC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
168         return OC_STACK_ERROR;
169     }
170
171     OIC_LOG(INFO, TAG, "OUT CreateSecureSessionRandomPinCallbak");
172
173     return OC_STACK_OK;
174 }