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 "experimental/securevirtualresourcetypes.h"
25 #include "doxmresource.h"
26 #include "credresource.h"
28 #include "cainterface.h"
30 #include "oic_malloc.h"
34 #include "oxmrandompin.h"
35 #include "ownershiptransfermanager.h"
36 #include "pinoxmcommon.h"
37 #include "ocstackinternal.h"
38 #include "mbedtls/ssl_ciphersuites.h"
40 #define TAG "OIC_OXM_RandomPIN"
42 OCStackResult CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
44 if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
46 return OC_STACK_INVALID_PARAM;
49 bool propertiesToInclude[DOXM_PROPERTY_COUNT];
50 memset(propertiesToInclude, 0, sizeof(propertiesToInclude));
51 propertiesToInclude[DOXM_OXMSEL] = true;
53 return DoxmToCBORPayloadPartial(otmCtx->selectedDeviceInfo->doxm, payload,
54 size, propertiesToInclude);
57 OCStackResult CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
59 if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
61 return OC_STACK_INVALID_PARAM;
64 OicUuid_t uuidPT = {.id={0}};
68 if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
70 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
71 return OC_STACK_ERROR;
73 memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
75 bool propertiesToInclude[DOXM_PROPERTY_COUNT];
76 memset(propertiesToInclude, 0, sizeof(propertiesToInclude));
77 propertiesToInclude[DOXM_DEVOWNERUUID] = true;
79 return DoxmToCBORPayloadPartial(otmCtx->selectedDeviceInfo->doxm, payload,
80 size, propertiesToInclude);
83 OCStackResult InputPinCodeCallback(OTMContext_t *otmCtx)
85 if (!otmCtx || !otmCtx->selectedDeviceInfo)
87 return OC_STACK_INVALID_PARAM;
90 uint8_t pinData[OXM_RANDOM_PIN_MAX_SIZE + 1] = {0};
92 OCStackResult res = InputPin(otmCtx->selectedDeviceInfo->doxm->deviceID, (char*)pinData, sizeof(pinData));
93 if (OC_STACK_OK != res)
95 OIC_LOG(ERROR, TAG, "Failed to input PIN");
100 * Since PSK will be used directly while PIN based ownership transfer,
101 * Credential should not be saved into SVR.
102 * For this reason, We will use a temporary get_psk_info callback to random PIN OxM.
105 if(!(otmCtx->selectedDeviceInfo->doxm->owned))
107 if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskForRandomPinOxm))
109 OIC_LOG(ERROR, TAG, "Failed to register DTLS credentials handler for random PIN OxM.");
110 res = OC_STACK_ERROR;
113 #ifdef MULTIPLE_OWNER
115 else if(otmCtx->selectedDeviceInfo->doxm->owned &&
116 otmCtx->selectedDeviceInfo->doxm->mom &&
117 OIC_MULTIPLE_OWNER_DISABLE != otmCtx->selectedDeviceInfo->doxm->mom->mode)
119 if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskForMotRandomPinOxm))
121 OIC_LOG(ERROR, TAG, "Failed to register TLS credentials handler for random PIN OxM.");
122 res = OC_STACK_ERROR;
125 #endif //MULTIPLE_OWNER
127 //Set the device id to derive temporal PSK
128 SetUuidForPinBasedOxm(&(otmCtx->selectedDeviceInfo->doxm->deviceID));
133 OCStackResult CreateSecureSessionRandomPinCallback(OTMContext_t* otmCtx)
135 OIC_LOG(INFO, TAG, "IN CreateSecureSessionRandomPinCallback");
137 if (!otmCtx || !otmCtx->selectedDeviceInfo)
139 return OC_STACK_INVALID_PARAM;
142 CAResult_t caresult = CAEnableAnonECDHCipherSuite(false);
143 if (CA_STATUS_OK != caresult)
145 OIC_LOG_V(ERROR, TAG, "Unable to disable anon cipher suite");
146 return OC_STACK_ERROR;
148 OIC_LOG(INFO, TAG, "Anonymous cipher suite disabled.");
150 caresult = CASelectCipherSuite(MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256, otmCtx->selectedDeviceInfo->endpoint.adapter);
151 if (CA_STATUS_OK != caresult)
153 OIC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256");
154 return OC_STACK_ERROR;
156 OIC_LOG(INFO, TAG, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 cipher suite selected.");
158 CAEndpoint_t endpoint;
159 OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
160 CopyDevAddrToEndpoint(&selDevInfo->endpoint, &endpoint);
162 if (CA_ADAPTER_IP == endpoint.adapter)
164 endpoint.port = selDevInfo->securePort;
167 else if (CA_ADAPTER_TCP == endpoint.adapter)
169 endpoint.port = selDevInfo->tcpSecurePort;
173 caresult = CAInitiateHandshake(&endpoint);
174 if (CA_STATUS_OK != caresult)
176 OIC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
177 return OC_STACK_ERROR;
180 OIC_LOG(INFO, TAG, "OUT CreateSecureSessionRandomPinCallback");