1 /* *****************************************************************
\r
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
19 * *****************************************************************/
\r
20 #ifndef _POSIX_C_SOURCE
\r
21 #define _POSIX_C_SOURCE 200112L
\r
23 #ifdef HAVE_UNISTD_H
\r
29 #ifdef HAVE_SYS_TIME_H
\r
30 #include <sys/time.h>
\r
32 #ifdef HAVE_STRING_H
\r
36 #include "ocstack.h"
\r
37 #include "oic_malloc.h"
\r
38 #include "oic_string.h"
\r
42 #include "ocpayload.h"
\r
43 #include "payload_logging.h"
\r
44 #include "cainterface.h"
\r
46 #include "directpairing.h"
\r
47 #include "srmresourcestrings.h" //@note: SRM's internal header
\r
48 #include "doxmresource.h" //@note: SRM's internal header
\r
49 #include "pconfresource.h" //@note: SRM's internal header
\r
50 #include "dpairingresource.h" //@note: SRM's internal header
\r
51 #include "credresource.h"
\r
53 #include "pmtypes.h"
\r
54 #include "pmutility.h"
\r
56 #include "srmutility.h"
\r
58 #ifdef __WITH_DTLS__
\r
64 static const uint16_t CBOR_SIZE = 1024;
\r
67 * Structure to carry direct-pairing API data to callback.
\r
69 typedef struct DPairData
\r
71 OCDirectPairingDev_t *peer; /**< Pointer to pairing target info.**/
\r
72 char pin[DP_PIN_LENGTH]; /**< PIN **/
\r
73 OCDirectPairingResultCB resultCallback; /**< Pointer to result callback.**/
\r
74 void *userCtx; /** < user context to pass in callback **/
\r
77 static OCDirectPairingDev_t *g_dp_paired = NULL;
\r
78 static OCDirectPairingDev_t *g_dp_discover = NULL;
\r
79 static DPairData_t *g_dp_proceed_ctx = NULL;
\r
83 * Function to search node in linked list that matches given IP and port.
\r
85 * @param[in] pList List of OCProvisionDev_t.
\r
86 * @param[in] addr address of target device.
\r
87 * @param[in] port port of remote server.
\r
89 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
\r
91 OCDirectPairingDev_t* getDev(OCDirectPairingDev_t **ppList, const char* addr, const uint16_t port)
\r
95 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
\r
99 OCDirectPairingDev_t *ptr = NULL;
\r
100 LL_FOREACH(*ppList, ptr)
\r
102 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
\r
114 * Add device information to list.
\r
116 * @param[in] pList List of OCProvisionDev_t.
\r
117 * @param[in] addr address of target device.
\r
118 * @param[in] port port of remote server.
\r
119 * @param[in] adapter adapter type of endpoint.
\r
120 * @param[in] doxm pointer to doxm instance.
\r
121 * @param[in] connType connectivity type of endpoint
\r
123 * @return OC_STACK_OK for success and errorcode otherwise.
\r
125 OCStackResult addDev(OCDirectPairingDev_t **ppList, OCDevAddr *endpoint,
\r
126 OCConnectivityType conn, OicSecPconf_t *pconf)
\r
128 if(NULL == endpoint || NULL == pconf)
\r
130 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
\r
131 return OC_STACK_INVALID_PARAM;
\r
134 OCDirectPairingDev_t *ptr = getDev(ppList, endpoint->addr, endpoint->port);
\r
137 ptr = (OCDirectPairingDev_t *)OICCalloc(1, sizeof (OCDirectPairingDev_t));
\r
140 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
\r
141 return OC_STACK_NO_MEMORY;
\r
144 memcpy(&ptr->endpoint, endpoint, sizeof(OCDevAddr));
\r
145 ptr->connType = conn;
\r
146 ptr->securePort = DEFAULT_SECURE_PORT;
\r
147 ptr->edp = pconf->edp;
\r
148 ptr->prm = pconf->prm;
\r
149 pconf->prm = NULL; // to prevent free
\r
150 ptr->prmLen = pconf->prmLen;
\r
151 memcpy(&ptr->deviceID, &pconf->deviceID, sizeof(OicUuid_t));
\r
152 memcpy(&ptr->rowner, &pconf->rownerID, sizeof(OicUuid_t));
\r
155 LL_PREPEND(*ppList, ptr);
\r
156 OIC_LOG(INFO, TAG, "device added !");
\r
159 return OC_STACK_OK;
\r
164 * Add device information to list.
\r
166 * @param[in] ppList List of OCProvisionDev_t.
\r
167 * @param[in] pDev target device.
\r
169 * @return OC_STACK_OK for success and errorcode otherwise.
\r
171 OCStackResult addDev2(OCDirectPairingDev_t **ppList, OCDirectPairingDev_t *pDev)
\r
175 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
\r
176 return OC_STACK_INVALID_PARAM;
\r
179 OCDirectPairingDev_t *ptr = getDev(ppList, pDev->endpoint.addr, pDev->endpoint.port);
\r
182 ptr = (OCDirectPairingDev_t *)OICCalloc(1, sizeof (OCDirectPairingDev_t));
\r
185 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
\r
186 return OC_STACK_NO_MEMORY;
\r
189 memcpy(&ptr->endpoint, &pDev->endpoint, sizeof(OCDevAddr));
\r
190 ptr->connType = pDev->connType;
\r
191 ptr->securePort = pDev->securePort;
\r
192 ptr->edp = pDev->edp;
\r
193 ptr->prmLen = pDev->prmLen;
\r
194 ptr->prm = (OicSecPrm_t*)OICCalloc(ptr->prmLen, sizeof (OicSecPrm_t));
\r
195 if (NULL == ptr->prm)
\r
197 OIC_LOG(ERROR, TAG, "Error while allocating memory for prm !!");
\r
198 return OC_STACK_NO_MEMORY;
\r
200 memcpy(ptr->prm, pDev->prm, sizeof(OicSecPrm_t)*ptr->prmLen);
\r
201 memcpy(&ptr->deviceID, &pDev->deviceID, sizeof(OicUuid_t));
\r
202 memcpy(&ptr->rowner, &pDev->rowner, sizeof(OicUuid_t));
\r
205 LL_PREPEND(*ppList, ptr);
\r
206 OIC_LOG(INFO, TAG, "device added !");
\r
209 return OC_STACK_OK;
\r
215 * This function deletes list of provision target devices
\r
217 * @param[in] pDevicesList List of OCProvisionDev_t.
\r
219 void delList(OCDirectPairingDev_t *pList)
\r
223 OCDirectPairingDev_t *del = NULL, *tmp = NULL;
\r
224 LL_FOREACH_SAFE(pList, del, tmp)
\r
226 LL_DELETE(pList, del);
\r
227 if (del && del->prm)
\r
235 bool DPGenerateQuery(bool isSecure,
\r
236 const char* address, const uint16_t port,
\r
237 const OCConnectivityType connType,
\r
238 char* buffer, size_t bufferSize, const char* uri)
\r
240 if(!address || !buffer || !uri)
\r
242 OIC_LOG(ERROR, TAG, "DPGenerateQuery : Invalid parameters.");
\r
246 static char QPREFIX_COAP[] = "coap://";
\r
247 static char QPREFIX_COAPS[] = "coaps://";
\r
248 static char QPREFIX_COAP_TCP[] = "coap+tcp://";
\r
249 static char QPREFIX_COAPS_TCP[] = "coaps+tcp://";
\r
252 char* prefix = (isSecure == true) ? QPREFIX_COAPS : QPREFIX_COAP;
\r
254 switch(connType & CT_MASK_ADAPTER)
\r
256 // @todo: Remove this ifdef. On Arduino, CT_ADAPTER_TCP resolves to the same value
\r
257 // as CT_ADAPTER_IP, resulting in a compiler error.
\r
259 #ifndef WITH_ARDUINO
\r
260 case CT_ADAPTER_TCP:
\r
261 prefix = (isSecure == true) ? QPREFIX_COAPS_TCP : QPREFIX_COAP_TCP;
\r
264 case CT_ADAPTER_IP:
\r
265 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
\r
268 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
\r
269 prefix, address, port, uri);
\r
272 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
\r
273 prefix, address, port, uri);
\r
276 OIC_LOG(ERROR, TAG, "Unknown address format.");
\r
279 // snprintf return value check
\r
282 OIC_LOG_V(ERROR, TAG, "DPGenerateQuery : Error (snprintf) %d\n", snRet);
\r
285 else if ((size_t)snRet >= bufferSize)
\r
287 OIC_LOG_V(ERROR, TAG, "DPGenerateQuery : Truncated (snprintf) %d\n", snRet);
\r
292 #ifndef WITH_ARDUINO
\r
293 // TODO: We need to verify tinyDTLS in below cases
\r
294 case CT_ADAPTER_GATT_BTLE:
\r
295 case CT_ADAPTER_RFCOMM_BTEDR:
\r
296 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
\r
301 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
\r
308 const OCDirectPairingDev_t* DPGetDiscoveredDevices()
\r
310 return g_dp_discover;
\r
313 const OCDirectPairingDev_t* DPGetPairedDevices()
\r
315 return g_dp_paired;
\r
318 void DPDeleteLists()
\r
320 delList(g_dp_discover);
\r
321 delList(g_dp_paired);
\r
325 * Callback handler of FinalizeDirectPairing.
\r
327 * @param[in] ctx ctx value passed to callback from calling function.
\r
328 * @param[in] UNUSED handle to an invocation
\r
329 * @param[in] clientResponse Response from queries to remote servers.
\r
330 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
\r
331 * and OC_STACK_KEEP_TRANSACTION to keep it.
\r
333 static OCStackApplicationResult DirectPairingFinalizeHandler(void *ctx, OCDoHandle UNUSED,
\r
334 OCClientResponse *clientResponse)
\r
336 OIC_LOG_V(INFO, TAG, "IN DirectPairingFinalizeHandler()");
\r
340 OIC_LOG(ERROR, TAG, "Context is Null");
\r
341 return OC_STACK_DELETE_TRANSACTION;
\r
345 DPairData_t *dpairData = (DPairData_t*)ctx;
\r
346 OCDirectPairingDev_t *peer = dpairData->peer;
\r
347 OCDirectPairingResultCB resultCallback = dpairData->resultCallback;
\r
349 if (clientResponse)
\r
351 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
\r
354 OIC_LOG(INFO, TAG, "DirectPairingFinalizeHandler : success PUT"
\r
355 " request to /oic/sec/dpairing");
\r
357 CAEndpoint_t endpoint;
\r
358 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
\r
359 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, peer->endpoint.addr);
\r
360 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
\r
361 endpoint.port = peer->securePort;
\r
363 OicUuid_t ptDeviceID = {.id={0}};
\r
364 if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
\r
366 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
\r
367 resultCallback(dpairData->userCtx, peer, OC_STACK_ERROR);
\r
368 return OC_STACK_DELETE_TRANSACTION;
\r
371 #ifdef __WITH_DTLS__
\r
372 res = SavePairingPSK((OCDevAddr*)&endpoint, &peer->deviceID, &ptDeviceID, false);
\r
373 if(OC_STACK_OK != res)
\r
375 OIC_LOG(ERROR, TAG, "Failed to PairingPSK generation");
\r
376 resultCallback(dpairData->userCtx, peer, res);
\r
377 return OC_STACK_DELETE_TRANSACTION;
\r
380 // close temporary sesion
\r
381 CAResult_t caResult = CACloseDtlsSession((const CAEndpoint_t*)&endpoint);
\r
382 if(CA_STATUS_OK != caResult)
\r
384 OIC_LOG(INFO, TAG, "Fail to close temporary dtls session");
\r
387 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL, CA_ADAPTER_IP);
\r
388 if(CA_STATUS_OK != caResult)
\r
390 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
\r
392 #endif // __WITH_DTLS__
\r
394 OIC_LOG(INFO, TAG, "Direct-Papring was successfully completed.");
\r
396 // update paired list
\r
397 OCDirectPairingDev_t *dev = getDev(&g_dp_discover, peer->endpoint.addr,
\r
398 peer->endpoint.port);
\r
399 res = addDev2(&g_dp_paired, dev);
\r
400 if (OC_STACK_OK != res)
\r
402 OIC_LOG(ERROR, TAG, "Error while adding a device to paired list.");
\r
405 resultCallback(dpairData->userCtx, peer, OC_STACK_OK);
\r
407 return OC_STACK_DELETE_TRANSACTION;
\r
411 OIC_LOG(INFO, TAG, "Direct-Papring received error response.");
\r
416 OIC_LOG(ERROR, TAG, "DirectPairingFinalizeHandler received Null clientResponse");
\r
419 resultCallback(dpairData->userCtx, peer, OC_STACK_ERROR);
\r
420 OICFree(dpairData);
\r
421 return OC_STACK_DELETE_TRANSACTION;
\r
425 * Finalize direct-pairing .
\r
427 * @param[in] peer target device to establish direct-pairing.
\r
428 * @param[in] resultCallback result event callback.
\r
430 * @return OC_STACK_OK on success otherwise error.
\r
432 OCStackResult FinalizeDirectPairing(void *ctx, OCDirectPairingDev_t* peer,
\r
433 OCDirectPairingResultCB resultCallback)
\r
437 return OC_STACK_INVALID_PARAM;
\r
440 OicUuid_t deviceID = {.id={0}};
\r
441 if (OC_STACK_OK != GetDoxmDeviceID(&deviceID))
\r
443 OIC_LOG(ERROR, TAG, "Error while retrieving device ID");
\r
444 return OC_STACK_ERROR;
\r
447 OicSecDpairing_t dpair;
\r
448 memset(&dpair, 0, sizeof(OicSecDpairing_t));
\r
449 dpair.spm = (OicSecPrm_t)PRM_NOT_ALLOWED;
\r
450 memcpy(&dpair.pdeviceID, &deviceID, sizeof(OicUuid_t));
\r
452 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
\r
455 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
\r
456 return OC_STACK_NO_MEMORY;
\r
458 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
\r
460 OCStackResult ret = DpairingToCBORPayload(&dpair, &(secPayload->securityData),
\r
461 &(secPayload->payloadSize));
\r
463 if(OC_STACK_OK != ret)
\r
465 OICFree(secPayload);
\r
466 OIC_LOG(ERROR, TAG, "Failed to DpairingToCBORPayload");
\r
467 return OC_STACK_NO_MEMORY;
\r
469 OIC_LOG(INFO, TAG, "DPARING CBOR data:");
\r
470 OIC_LOG_BUFFER(INFO, TAG, secPayload->securityData, secPayload->payloadSize);
\r
472 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
\r
473 if(!DPGenerateQuery(true,
\r
474 peer->endpoint.addr,
\r
477 query, sizeof(query), OIC_RSRC_DPAIRING_URI))
\r
479 OIC_LOG(ERROR, TAG, "DPDirectPairing : Failed to generate query");
\r
480 return OC_STACK_ERROR;
\r
482 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
\r
484 DPairData_t *dpairData = (DPairData_t *) OICCalloc(1, sizeof(DPairData_t));
\r
485 if (dpairData == NULL)
\r
487 OICFree(secPayload->securityData);
\r
488 OICFree(secPayload);
\r
489 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
\r
490 return OC_STACK_NO_MEMORY;
\r
492 dpairData->peer = peer;
\r
493 dpairData->resultCallback = resultCallback;
\r
494 dpairData->userCtx = ctx;
\r
496 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
\r
497 cbData.cb = DirectPairingFinalizeHandler;
\r
498 cbData.context = (void*)dpairData;
\r
501 OCMethod method = OC_REST_PUT;
\r
502 OCDoHandle handle = NULL;
\r
503 OIC_LOG(DEBUG, TAG, "Sending DPAIRNG setting to resource server");
\r
504 ret = OCDoResource(&handle, method, query,
\r
505 &peer->endpoint, (OCPayload*)secPayload,
\r
506 peer->connType, OC_LOW_QOS, &cbData, NULL, 0);
\r
507 if(OC_STACK_OK != ret)
\r
509 OIC_LOG(ERROR, TAG, "error in OCDoResource");
\r
510 return OC_STACK_ERROR;
\r
513 return OC_STACK_OK;
\r
517 * Function to handle the handshake result in Direct-Pairing.
\r
518 * This function will be invoked after DTLS handshake
\r
519 * @param endPoint [IN] The remote endpoint.
\r
520 * @param errorInfo [IN] Error information from the endpoint.
\r
523 void DirectPairingDTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
\r
525 OIC_LOG_V(INFO, TAG, "IN DirectPairingDTLSHandshakeCB");
\r
528 if(g_dp_proceed_ctx && g_dp_proceed_ctx->peer && endpoint && info)
\r
530 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
\r
531 endpoint->addr, endpoint->port, info->result);
\r
533 OCDirectPairingDev_t *peer = g_dp_proceed_ctx->peer;
\r
534 OCDirectPairingResultCB resultCallback = g_dp_proceed_ctx->resultCallback;
\r
537 //Make sure the address matches.
\r
538 if(strncmp(peer->endpoint.addr, endpoint->addr, sizeof(endpoint->addr)) == 0 &&
\r
539 peer->securePort == endpoint->port)
\r
541 //In case of success, send next coaps request.
\r
542 if(CA_STATUS_OK == info->result)
\r
544 OIC_LOG(INFO, TAG, "Now, finalize Direct-Pairing procedure.");
\r
546 res = FinalizeDirectPairing(g_dp_proceed_ctx->userCtx, peer, resultCallback);
\r
547 if(OC_STACK_OK != res)
\r
549 OIC_LOG(ERROR, TAG, "Failed to finalize direct-pairing");
\r
550 resultCallback(g_dp_proceed_ctx->userCtx, peer, res);
\r
553 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
\r
555 OIC_LOG(INFO, TAG, "DirectPairingDTLSHandshakeCB - Authentication failed");
\r
556 resultCallback(g_dp_proceed_ctx->userCtx, peer, OC_STACK_AUTHENTICATION_FAILURE);
\r
559 #ifdef __WITH_DTLS__
\r
560 CARegisterDTLSHandshakeCallback(NULL);
\r
561 #endif // __WITH_DTLS__
\r
562 res = RemoveCredential(&peer->deviceID);
\r
563 if(OC_STACK_RESOURCE_DELETED != res)
\r
565 OIC_LOG_V(ERROR, TAG, "Failed to remove temporal PSK : %d", res);
\r
568 OICFree(g_dp_proceed_ctx);
\r
569 g_dp_proceed_ctx = NULL;
\r
573 OIC_LOG_V(INFO, TAG, "DirectPairingDTLSHandshakeCB - Not matched to peer address");
\r
577 OIC_LOG_V(INFO, TAG, "OUT DirectPairingDTLSHandshakeCB");
\r
581 * Callback handler of DPDirectPairing.
\r
583 * @param[in] ctx ctx value passed to callback from calling function.
\r
584 * @param[in] UNUSED handle to an invocation
\r
585 * @param[in] clientResponse Response from queries to remote servers.
\r
586 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
\r
587 * and OC_STACK_KEEP_TRANSACTION to keep it.
\r
589 static OCStackApplicationResult DirectPairingHandler(void *ctx, OCDoHandle UNUSED,
\r
590 OCClientResponse *clientResponse)
\r
592 OIC_LOG_V(INFO, TAG, "IN DirectPairingHandler.");
\r
596 OIC_LOG(ERROR, TAG, "Context is Null");
\r
597 return OC_STACK_DELETE_TRANSACTION;
\r
600 OCStackResult res = OC_STACK_ERROR;
\r
601 DPairData_t *dpairData = (DPairData_t*)ctx;
\r
602 OCDirectPairingResultCB resultCallback = (OCDirectPairingResultCB)dpairData->resultCallback;
\r
603 OicUuid_t subjectId = {.id={0}};
\r
605 if (clientResponse)
\r
607 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
\r
610 OIC_LOG(INFO, TAG, "DirectPairingHandler : success POST request to /oic/sec/dpairing");
\r
612 #ifdef __WITH_DTLS__
\r
613 // Add temporary psk
\r
614 res = AddTmpPskWithPIN(&dpairData->peer->deviceID,
\r
615 SYMMETRIC_PAIR_WISE_KEY,
\r
616 (char*)dpairData->pin, DP_PIN_LENGTH,
\r
617 &dpairData->peer->rowner, &subjectId);
\r
618 VERIFY_SUCCESS(TAG, OC_STACK_OK == res, ERROR);
\r
621 // Start to establish a secure channel with Pin-based PSK cipher suite
\r
622 CAResult_t caresult;
\r
624 caresult = CAEnableAnonECDHCipherSuite(false);
\r
625 VERIFY_SUCCESS(TAG, CA_STATUS_OK == caresult, ERROR);
\r
627 caresult = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256, CA_ADAPTER_IP);
\r
628 VERIFY_SUCCESS(TAG, CA_STATUS_OK == caresult, ERROR);
\r
630 //Register proceeding peer info. & DTLS event handler to catch the dtls event while handshake
\r
631 g_dp_proceed_ctx = dpairData;
\r
632 res = CARegisterDTLSHandshakeCallback(DirectPairingDTLSHandshakeCB);
\r
633 VERIFY_SUCCESS(TAG, CA_STATUS_OK == caresult, ERROR);
\r
636 CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
\r
637 VERIFY_NON_NULL(TAG, endpoint, FATAL);
\r
638 memcpy(endpoint,&dpairData->peer->endpoint,sizeof(CAEndpoint_t));
\r
639 endpoint->port = dpairData->peer->securePort;
\r
640 OIC_LOG_V(INFO, TAG, "Initiate DTLS handshake to %s(%d)", endpoint->addr,
\r
643 caresult = CAInitiateHandshake(endpoint);
\r
645 VERIFY_SUCCESS(TAG, CA_STATUS_OK == caresult, ERROR);
\r
646 #endif // __WITH_DTLS__
\r
653 OIC_LOG(INFO, TAG, "DirectPairingHandler : fail POST request to /oic/sec/dpairing");
\r
658 OIC_LOG(ERROR, TAG, "DirectPairingHandler received Null clientResponse");
\r
661 #ifdef __WITH_DTLS__
\r
663 #endif // __WITH_DTLS__
\r
665 if (OC_STACK_OK != res)
\r
667 if (0 < strlen((const char*)subjectId.id))
\r
669 RemoveCredential(&dpairData->peer->deviceID);
\r
670 OICFree(dpairData);
\r
671 g_dp_proceed_ctx = NULL;
\r
674 resultCallback(dpairData->userCtx, dpairData->peer, res);
\r
676 OIC_LOG_V(INFO, TAG, "OUT DirectPairingHandler.");
\r
677 return OC_STACK_DELETE_TRANSACTION;
\r
681 * Start direct-pairing .
\r
683 * @param[in] peer target device to establish direct-pairing.
\r
684 * @param[in] pmSel selected pairing method.
\r
685 * @param[in] pinNumber secret value for dtls connection.
\r
687 * @return OC_STACK_OK on success otherwise error.
\r
689 OCStackResult DPDirectPairing(void *ctx, OCDirectPairingDev_t* peer, OicSecPrm_t pmSel,
\r
690 char *pinNumber, OCDirectPairingResultCB resultCallback)
\r
692 if(NULL == peer || NULL == pinNumber)
\r
694 return OC_STACK_INVALID_PARAM;
\r
697 OicUuid_t deviceID = {.id={0}};
\r
698 if (OC_STACK_OK != GetDoxmDeviceID(&deviceID))
\r
700 OIC_LOG(ERROR, TAG, "Error while retrieving device ID");
\r
701 return OC_STACK_ERROR;
\r
704 OicSecDpairing_t dpair;
\r
705 memset(&dpair, 0, sizeof(OicSecDpairing_t));
\r
707 memcpy(&dpair.pdeviceID, &deviceID, sizeof(OicUuid_t));
\r
709 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
\r
712 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
\r
713 return OC_STACK_NO_MEMORY;
\r
715 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
\r
717 OCStackResult ret = DpairingToCBORPayload(&dpair, &(secPayload->securityData),
\r
718 &(secPayload->payloadSize));
\r
720 if(OC_STACK_OK != ret)
\r
722 OICFree(secPayload);
\r
723 OIC_LOG(ERROR, TAG, "Failed to DpairingToCBORPayload");
\r
724 return OC_STACK_NO_MEMORY;
\r
726 OIC_LOG(INFO, TAG, "DPARING CBOR data:");
\r
727 OIC_LOG_BUFFER(INFO, TAG, secPayload->securityData, secPayload->payloadSize);
\r
729 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
\r
730 if(!DPGenerateQuery(false,
\r
731 peer->endpoint.addr,
\r
732 peer->endpoint.port,
\r
733 //peer->securePort,
\r
735 query, sizeof(query), OIC_RSRC_DPAIRING_URI))
\r
737 OIC_LOG(ERROR, TAG, "DPDirectPairing : Failed to generate query");
\r
738 return OC_STACK_ERROR;
\r
740 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
\r
742 DPairData_t *dpairData = (DPairData_t *) OICCalloc(1, sizeof(DPairData_t));
\r
743 if (dpairData == NULL)
\r
745 OICFree(secPayload->securityData);
\r
746 OICFree(secPayload);
\r
747 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
\r
748 return OC_STACK_NO_MEMORY;
\r
750 dpairData->peer = peer;
\r
751 memcpy(dpairData->pin, pinNumber, DP_PIN_LENGTH);
\r
752 dpairData->resultCallback = resultCallback;
\r
753 dpairData->userCtx = ctx;
\r
755 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
\r
756 cbData.cb = DirectPairingHandler;
\r
757 cbData.context = (void*)dpairData;
\r
760 OCMethod method = OC_REST_POST;
\r
761 OCDoHandle handle = NULL;
\r
762 OIC_LOG(DEBUG, TAG, "Sending DPAIRNG setting to resource server");
\r
763 ret = OCDoResource(&handle, method, query,
\r
764 &peer->endpoint, (OCPayload*)secPayload,
\r
765 peer->connType, OC_LOW_QOS, &cbData, NULL, 0);
\r
766 if(OC_STACK_OK != ret)
\r
768 OIC_LOG(ERROR, TAG, "error in OCDoResource");
\r
769 return OC_STACK_ERROR;
\r
772 return OC_STACK_OK;
\r
777 * Callback handler for getting secure port information using /oic/res discovery.
\r
779 * @param[in] ctx user context
\r
780 * @param[in] handle Handle for response
\r
781 * @param[in] clientResponse Response information(It will contain payload)
\r
783 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
\r
784 * OC_STACK_DELETE_TRANSACTION to delete it.
\r
786 static OCStackApplicationResult DirectPairingPortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
\r
787 OCClientResponse *clientResponse)
\r
789 OIC_LOG(INFO, TAG, "Callback Context for Direct-Pairing Secure Port DISCOVER "
\r
790 "query recvd successfully");
\r
794 if (clientResponse)
\r
796 if (NULL == clientResponse->payload)
\r
798 OIC_LOG(INFO, TAG, "Skiping Null payload");
\r
802 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
\r
804 OIC_LOG(INFO, TAG, "Wrong payload type");
\r
805 return OC_STACK_DELETE_TRANSACTION;
\r
808 uint16_t securePort = 0;
\r
809 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
\r
810 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
\r
812 if (resPayload && resPayload->secure)
\r
814 securePort = resPayload->port;
\r
818 OIC_LOG(INFO, TAG, "Can not find secure port information.");
\r
819 return OC_STACK_DELETE_TRANSACTION;
\r
822 OCDirectPairingDev_t *ptr = getDev(&g_dp_discover,
\r
823 clientResponse->devAddr.addr, clientResponse->devAddr.port);
\r
826 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
\r
827 return OC_STACK_DELETE_TRANSACTION;
\r
829 ptr->securePort = securePort;
\r
831 OIC_LOG(INFO, TAG, "Exiting DirectPairingPortDiscoveryHandler.");
\r
834 return OC_STACK_DELETE_TRANSACTION;
\r
838 OIC_LOG(INFO, TAG, "Skiping Null response");
\r
840 return OC_STACK_DELETE_TRANSACTION;
\r
844 * Callback handler for DPDeviceDiscovery API.
\r
846 * @param[in] ctx User context
\r
847 * @param[in] handle Handler for response
\r
848 * @param[in] clientResponse Response information (It will contain payload)
\r
849 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
\r
850 * OC_STACK_DELETE_TRANSACTION to delete it.
\r
852 static OCStackApplicationResult DirectPairingDiscoveryHandler(void* ctx, OCDoHandle UNUSED,
\r
853 OCClientResponse * clientResponse)
\r
855 OIC_LOG(INFO, TAG, "Callback Context for Direct-Pairing DISCOVER query recvd successfully");
\r
859 if (clientResponse)
\r
861 OIC_LOG_V(INFO, TAG, "StackResult: %d", clientResponse->result);
\r
862 OIC_LOG_V(INFO, TAG,
\r
863 "Device =============> Discovered @ %s:%d",
\r
864 clientResponse->devAddr.addr,
\r
865 clientResponse->devAddr.port);
\r
867 if (NULL == clientResponse->payload)
\r
869 OIC_LOG(INFO, TAG, "Skiping Null payload");
\r
870 return OC_STACK_KEEP_TRANSACTION;
\r
872 if (OC_STACK_OK != clientResponse->result)
\r
874 OIC_LOG(INFO, TAG, "Error in response");
\r
875 return OC_STACK_KEEP_TRANSACTION;
\r
878 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
\r
879 OicSecPconf_t *pconf = NULL;
\r
881 OCStackResult res = CBORPayloadToPconf(
\r
882 ((OCSecurityPayload*)clientResponse->payload)->securityData,
\r
884 if (OC_STACK_OK != res )
\r
886 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
\r
887 return OC_STACK_KEEP_TRANSACTION;
\r
893 OCDevAddr endpoint;
\r
894 memcpy(&endpoint, &clientResponse->devAddr, sizeof(OCDevAddr));
\r
896 OCStackResult res = addDev(&g_dp_discover, &endpoint,
\r
897 clientResponse->connType, pconf);
\r
898 DeletePconfBinData(pconf);
\r
899 if (OC_STACK_OK != res)
\r
901 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
\r
902 return OC_STACK_KEEP_TRANSACTION;
\r
906 char rsrc_uri[MAX_URI_LENGTH+1] = {0};
\r
907 int wr_len = snprintf(rsrc_uri, sizeof(rsrc_uri), "%s?%s=%s",
\r
908 OC_RSRVD_WELL_KNOWN_URI, OC_RSRVD_RESOURCE_TYPE, OIC_RSRC_TYPE_SEC_DPAIRING);
\r
909 if(wr_len <= 0 || (size_t)wr_len >= sizeof(rsrc_uri))
\r
911 OIC_LOG(ERROR, TAG, "rsrc_uri_string_print failed");
\r
912 return OC_STACK_KEEP_TRANSACTION;
\r
915 //Try to the unicast discovery to getting secure port
\r
916 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
\r
917 if(!DPGenerateQuery(false,
\r
918 clientResponse->devAddr.addr, clientResponse->devAddr.port,
\r
919 clientResponse->connType,
\r
920 query, sizeof(query), rsrc_uri))
\r
922 OIC_LOG(ERROR, TAG, "DirectPairingDiscoveryHandler : Failed to generate query");
\r
923 return OC_STACK_KEEP_TRANSACTION;
\r
925 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
\r
927 OCCallbackData cbData;
\r
928 cbData.cb = &DirectPairingPortDiscoveryHandler;
\r
929 cbData.context = NULL;
\r
931 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
\r
932 clientResponse->connType, OC_LOW_QOS, &cbData, NULL, 0);
\r
933 if(OC_STACK_OK != ret)
\r
935 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
\r
936 return OC_STACK_KEEP_TRANSACTION;
\r
940 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
\r
943 return OC_STACK_KEEP_TRANSACTION;
\r
948 OIC_LOG(INFO, TAG, "Skiping Null response");
\r
951 return OC_STACK_DELETE_TRANSACTION;
\r
953 #ifndef WITH_ARDUINO
\r
955 * Discover direct-pairing devices in the same IP subnet. .
\r
957 * @param[in] waittime Timeout in seconds.
\r
959 * @return OC_STACK_OK on success otherwise error.
\r
961 OCStackResult DPDeviceDiscovery(unsigned short waittime)
\r
963 OIC_LOG(DEBUG, TAG, "IN DPDeviceDiscovery");
\r
967 delList(g_dp_discover);
\r
968 g_dp_discover = NULL;
\r
973 const char DP_DISCOVERY_QUERY[] = "/oic/sec/pconf";
\r
975 OCCallbackData cbData;
\r
976 cbData.cb = DirectPairingDiscoveryHandler;
\r
977 cbData.context = NULL;
\r
980 /* Start a DP discovery query*/
\r
981 OIC_LOG_V(INFO, TAG, "Initiating Direct-Pairing Discovery : %s\n", DP_DISCOVERY_QUERY);
\r
982 OCDoHandle handle = NULL;
\r
983 ret = OCDoResource(&handle, OC_REST_DISCOVER, DP_DISCOVERY_QUERY, 0, 0, CT_DEFAULT,
\r
984 OC_LOW_QOS, &cbData, NULL, 0);
\r
985 if (ret != OC_STACK_OK)
\r
987 OIC_LOG(ERROR, TAG, "OCStack resource error");
\r
993 int clock_res = -1;
\r
994 #if defined(_MSC_VER)
\r
995 time_t startTime = NULL;
\r
996 clock_res = (time(&startTime) == -1);
\r
998 struct timespec startTime = {.tv_sec=0, .tv_nsec=0};
\r
999 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
\r
1000 clock_res = clock_gettime(CLOCK_MONOTONIC, &startTime);
\r
1003 if (0 != clock_res)
\r
1005 OIC_LOG(ERROR, TAG, "clock error");
\r
1006 if(OC_STACK_OK != OCCancel(handle, OC_LOW_QOS, NULL, 0))
\r
1008 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
\r
1010 return OC_STACK_ERROR;
\r
1015 #if defined(_MSC_VER)
\r
1016 time_t currTime = NULL;
\r
1017 clock_res = (time(&currTime) == -1);
\r
1019 struct timespec currTime = {.tv_sec=0, .tv_nsec=0};
\r
1020 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
\r
1021 clock_res = clock_gettime(CLOCK_MONOTONIC, &currTime);
\r
1024 if (0 != clock_res)
\r
1026 OIC_LOG(ERROR, TAG, "clock error");
\r
1027 ret = OC_STACK_ERROR;
\r
1030 #if defined(_MSC_VER)
\r
1031 long elapsed = currTime - startTime;
\r
1033 long elapsed = (currTime.tv_sec - startTime.tv_sec);
\r
1035 if (elapsed > waittime)
\r
1041 struct timespec timeout = {.tv_sec=0, .tv_nsec=100000000L};
\r
1043 nanosleep(&timeout, NULL);
\r
1047 // Waiting for each response.
\r
1048 ret = OCCancel(handle, OC_LOW_QOS, NULL, 0);
\r
1049 if (OC_STACK_OK != ret)
\r
1051 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
\r
1053 OIC_LOG(DEBUG, TAG, "OUT DPDeviceDiscovery");
\r