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 "ocsecurityconfig.h"
25 #include "securevirtualresourcetypes.h"
26 #include "doxmresource.h"
27 #include "credresource.h"
29 #include "cainterface.h"
31 #include "oic_malloc.h"
36 #include "oxmrandompin.h"
37 #include "ownershiptransfermanager.h"
38 #include "pinoxmcommon.h"
40 #define TAG "OXM_RandomPIN"
42 char* CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx)
44 if(!otmCtx || !otmCtx->selectedDeviceInfo)
49 otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_RANDOM_DEVICE_PIN;
51 OicUuid_t uuidPT = {.id={0}};
52 if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
54 OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
57 memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id, UUID_LENGTH);
59 return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
62 char* CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx)
64 if(!otmCtx || !otmCtx->selectedDeviceInfo)
69 OicUuid_t uuidPT = {.id={0}};
71 if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
73 OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
76 memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
77 otmCtx->selectedDeviceInfo->doxm->owned = true;
79 return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
82 OCStackResult InputPinCodeCallback(OTMContext_t* otmCtx)
84 if(!otmCtx || !otmCtx->selectedDeviceInfo)
86 return OC_STACK_INVALID_PARAM;
89 uint8_t pinData[OXM_RANDOM_PIN_SIZE + 1];
91 OCStackResult res = InputPin((char*)pinData, OXM_RANDOM_PIN_SIZE + 1);
92 if(OC_STACK_OK != res)
94 OC_LOG(ERROR, TAG, "Failed to input PIN");
98 OicUuid_t deviceUUID = {.id={0}};
99 if (OC_STACK_OK != GetDoxmDeviceID(&deviceUUID))
101 OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
102 return OC_STACK_ERROR;
105 res = AddTmpPskWithPIN(&otmCtx->selectedDeviceInfo->doxm->deviceID,
106 SYMMETRIC_PAIR_WISE_KEY,
107 (char*)pinData, OXM_RANDOM_PIN_SIZE,
108 1, &deviceUUID, &otmCtx->tempCredId);
109 if(res != OC_STACK_OK)
111 OC_LOG_V(ERROR, TAG, "Failed to save the temporal PSK : %d", res);
117 OCStackResult CreateSecureSessionRandomPinCallbak(OTMContext_t* otmCtx)
119 OC_LOG(INFO, TAG, "IN CreateSecureSessionRandomPinCallbak");
121 if(!otmCtx || !otmCtx->selectedDeviceInfo)
123 return OC_STACK_INVALID_PARAM;
126 OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
127 CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
130 return OC_STACK_NO_MEMORY;
132 memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
133 endpoint->port = selDevInfo->securePort;
134 CAResult_t caresult = CAInitiateHandshake(endpoint);
136 if (CA_STATUS_OK != caresult)
138 OC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
139 return OC_STACK_ERROR;
142 OC_LOG(INFO, TAG, "OUT CreateSecureSessionRandomPinCallbak");