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 OCStackResult CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
43 if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
45 return OC_STACK_INVALID_PARAM;
48 otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_RANDOM_DEVICE_PIN;
50 return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);
53 OCStackResult CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
55 if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
57 return OC_STACK_INVALID_PARAM;
60 OicUuid_t uuidPT = {.id={0}};
64 if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
66 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
67 return OC_STACK_ERROR;
69 memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
71 return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);
74 OCStackResult InputPinCodeCallback(OTMContext_t *otmCtx)
76 if (!otmCtx || !otmCtx->selectedDeviceInfo)
78 return OC_STACK_INVALID_PARAM;
81 uint8_t pinData[OXM_RANDOM_PIN_SIZE + 1];
83 OCStackResult res = InputPin((char*)pinData, OXM_RANDOM_PIN_SIZE + 1);
84 if (OC_STACK_OK != res)
86 OIC_LOG(ERROR, TAG, "Failed to input PIN");
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.
95 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskForRandomPinOxm))
97 OIC_LOG(ERROR, TAG, "Failed to register DTLS credentials handler for random PIN OxM.");
101 //Set the device id to derive temporal PSK
102 SetUuidForRandomPinOxm(&(otmCtx->selectedDeviceInfo->doxm->deviceID));
107 OCStackResult CreateSecureSessionRandomPinCallback(OTMContext_t* otmCtx)
109 OIC_LOG(INFO, TAG, "IN CreateSecureSessionRandomPinCallbak");
111 if (!otmCtx || !otmCtx->selectedDeviceInfo)
113 return OC_STACK_INVALID_PARAM;
116 CAResult_t caresult = CAEnableAnonECDHCipherSuite(false);
117 if (CA_STATUS_OK != caresult)
119 OIC_LOG_V(ERROR, TAG, "Unable to disable anon cipher suite");
120 return OC_STACK_ERROR;
122 OIC_LOG(INFO, TAG, "Anonymous cipher suite disabled.");
124 caresult = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256);
125 if (CA_STATUS_OK != caresult)
127 OIC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256");
128 return OC_STACK_ERROR;
130 OIC_LOG(INFO, TAG, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 cipher suite selected.");
132 OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
133 CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
134 if (NULL == endpoint)
136 return OC_STACK_NO_MEMORY;
138 memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
139 endpoint->port = selDevInfo->securePort;
140 caresult = CAInitiateHandshake(endpoint);
142 if (CA_STATUS_OK != caresult)
144 OIC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
145 return OC_STACK_ERROR;
148 OIC_LOG(INFO, TAG, "OUT CreateSecureSessionRandomPinCallbak");