1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * *****************************************************************/
24 #include "securevirtualresourcetypes.h"
25 #include "doxmresource.h"
26 #include "credresource.h"
28 #include "cainterface.h"
30 #include "oic_malloc.h"
35 #include "oxmrandompin.h"
36 #include "ownershiptransfermanager.h"
37 #include "pinoxmcommon.h"
39 #define TAG "OXM_RandomPIN"
41 char* CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx)
43 if(!otmCtx || !otmCtx->selectedDeviceInfo)
48 otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_RANDOM_DEVICE_PIN;
50 OicUuid_t uuidPT = {.id={0}};
51 if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
53 OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
56 memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id, UUID_LENGTH);
58 return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
61 char* CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx)
63 if(!otmCtx || !otmCtx->selectedDeviceInfo)
68 OicUuid_t uuidPT = {.id={0}};
70 if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
72 OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
75 memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
76 otmCtx->selectedDeviceInfo->doxm->owned = true;
78 return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
81 OCStackResult InputPinCodeCallback(OTMContext_t* otmCtx)
83 if(!otmCtx || !otmCtx->selectedDeviceInfo)
85 return OC_STACK_INVALID_PARAM;
88 uint8_t pinData[OXM_RANDOM_PIN_SIZE + 1];
90 OCStackResult res = InputPin((char*)pinData, OXM_RANDOM_PIN_SIZE + 1);
91 if(OC_STACK_OK != res)
93 OC_LOG(ERROR, TAG, "Failed to input PIN");
97 OicUuid_t deviceUUID = {.id={0}};
98 if (OC_STACK_OK != GetDoxmDeviceID(&deviceUUID))
100 OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
101 return OC_STACK_ERROR;
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)
110 OC_LOG_V(ERROR, TAG, "Failed to save the temporal PSK : %d", res);
116 OCStackResult CreateSecureSessionRandomPinCallbak(OTMContext_t* otmCtx)
118 OC_LOG(INFO, TAG, "IN CreateSecureSessionRandomPinCallbak");
120 if(!otmCtx || !otmCtx->selectedDeviceInfo)
122 return OC_STACK_INVALID_PARAM;
125 OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
126 CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
129 return OC_STACK_NO_MEMORY;
131 memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
132 endpoint->port = selDevInfo->securePort;
133 CAResult_t caresult = CAInitiateHandshake(endpoint);
135 if (CA_STATUS_OK != caresult)
137 OC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
138 return OC_STACK_ERROR;
141 OC_LOG(INFO, TAG, "OUT CreateSecureSessionRandomPinCallbak");