1 /* *****************************************************************
3 * Copyright 2016 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 "oxmpreconfpin.h"
36 #include "ownershiptransfermanager.h"
37 #include "pinoxmcommon.h"
38 #include "srmresourcestrings.h"
40 #define TAG "OXM_PreconfigPIN"
42 OCStackResult CreatePreconfigPinBasedSelectOxmPayload(OTMContext_t *otmCtx, uint8_t **payload, size_t *size)
44 if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
46 return OC_STACK_INVALID_PARAM;
49 otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_PRECONFIG_PIN;
51 return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
54 OCStackResult CreatePreconfigPinBasedOwnerTransferPayload(OTMContext_t *otmCtx, uint8_t **payload, size_t *size)
56 if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
58 return OC_STACK_INVALID_PARAM;
61 OicUuid_t uuidPT = {.id={0}};
65 if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
67 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
68 return OC_STACK_ERROR;
70 memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
72 return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
75 OCStackResult LoadPreconfigPinCodeCallback(OTMContext_t *otmCtx)
77 OIC_LOG(INFO, TAG, "IN LoadPreconfigPinCodeCallback");
78 OCStackResult res = OC_STACK_ERROR;
79 OicSecCred_t* cred = GetCredResourceData(&otmCtx->selectedDeviceInfo->doxm->deviceID);
82 OicUuid_t uuid = {.id={0}};
83 OICStrcpy(uuid.id, sizeof(uuid.id), WILDCARD_SUBJECT_ID.id);
84 cred = GetCredResourceData(&uuid);
87 OIC_LOG(ERROR, TAG, "Can not find the Credential for MOT");
88 OIC_LOG(ERROR, TAG, "Please make sure the preconfigured PIN");
89 return OC_STACK_ERROR;
93 uint8_t* pinBuffer = NULL;
95 if(OIC_ENCODING_BASE64 == cred->privateData.encoding)
97 //In case of 'preconfig PIN', secret data(PIN) already exist.
98 pinBufLen = B64DECODE_OUT_SAFESIZE(cred->privateData.len + 1);
99 pinBuffer = (uint8_t*)OICCalloc(1, pinBufLen);
100 if(NULL == pinBuffer)
102 OIC_LOG(ERROR, TAG, "Failed to memory allocation.");
103 return OC_STACK_NO_MEMORY;
106 if(B64_OK != b64Decode(cred->privateData.data, cred->privateData.len, pinBuffer, pinBufLen, &pinLen))
108 OIC_LOG(ERROR, TAG, "Failed to base64 deconding for preconfig PIN");
110 return OC_STACK_ERROR;
114 else if(OIC_ENCODING_RAW == cred->privateData.encoding)
116 pinBuffer = (uint8_t*)OICMalloc(cred->privateData.len + 1);
117 if(NULL == pinBuffer)
119 OIC_LOG(ERROR, TAG, "Failed to memory allocation.");
120 return OC_STACK_NO_MEMORY;
122 memcpy(pinBuffer, cred->privateData.data, cred->privateData.len);
123 pinBuffer[cred->privateData.len] = '\0';
124 pinBufLen = cred->privateData.len;
128 OIC_LOG(ERROR, TAG, "Unknown encoding type for PreConfigured PIN credential");
129 return OC_STACK_ERROR;
132 res = SetPreconfigPin((char*)pinBuffer, pinBufLen);
134 if(OC_STACK_OK != res)
136 OIC_LOG_V(ERROR, TAG, "Failed to save the preconfig PIN : %d", res);
141 if(false == otmCtx->selectedDeviceInfo->doxm->owned)
143 if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskForPreconfPinOxm))
145 OIC_LOG(ERROR, TAG, "Failed to register DTLS credentials handler for random PIN OxM.");
146 res = OC_STACK_ERROR;
149 #ifdef _ENABLE_MULTIPLE_OWNER_
151 else if(true == otmCtx->selectedDeviceInfo->doxm->owned &&
152 otmCtx->selectedDeviceInfo->doxm->mom &&
153 OIC_MULTIPLE_OWNER_DISABLE != otmCtx->selectedDeviceInfo->doxm->mom->mode)
155 if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskForMotPreconfPinOxm))
157 OIC_LOG(ERROR, TAG, "Failed to register DTLS credentials handler for random PIN OxM.");
158 res = OC_STACK_ERROR;
161 #endif //_ENABLE_MULTIPLE_OWNER_
163 //Set the device id to derive temporal PSK
164 SetUuidForPinBasedOxm(&(otmCtx->selectedDeviceInfo->doxm->deviceID));
166 OIC_LOG(INFO, TAG, "OUT LoadPreconfigPinCodeCallback");
171 OCStackResult CreateSecureSessionPreconfigPinCallback(OTMContext_t* otmCtx)
173 OIC_LOG(INFO, TAG, "IN CreateSecureSessionPreconfigPinCallback");
175 if (!otmCtx || !otmCtx->selectedDeviceInfo)
177 return OC_STACK_INVALID_PARAM;
180 CAResult_t caresult = CAEnableAnonECDHCipherSuite(false);
181 if (CA_STATUS_OK != caresult)
183 OIC_LOG_V(ERROR, TAG, "Unable to disable anon cipher suite");
184 return OC_STACK_ERROR;
186 OIC_LOG(INFO, TAG, "Anonymous cipher suite disabled.");
188 caresult = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256, otmCtx->selectedDeviceInfo->endpoint.adapter);
189 if (CA_STATUS_OK != caresult)
191 OIC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256");
192 return OC_STACK_ERROR;
194 OIC_LOG(INFO, TAG, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 cipher suite selected.");
196 OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
197 CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
198 if (NULL == endpoint)
200 return OC_STACK_NO_MEMORY;
202 memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
203 endpoint->port = selDevInfo->securePort;
204 caresult = CAInitiateHandshake(endpoint);
206 if (CA_STATUS_OK != caresult)
208 OIC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
209 return OC_STACK_ERROR;
212 OIC_LOG(INFO, TAG, "OUT CreateSecureSessionPreconfigPinCallback");