1 /******************************************************************
3 * Copyright 2014 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 ******************************************************************/
27 #include "cainterface.h"
28 #include "caremotehandler.h"
29 #include "camessagehandler.h"
30 #include "caprotocolmessage.h"
31 #include "canetworkconfigurator.h"
32 #include "cainterfacecontroller.h"
35 #include "caadapternetdtls.h"
39 #include "catcpadapter.h"
41 #include "ca_adapter_net_tls.h"
45 CAGlobals_t caglobals = { .clientFlags = 0,
48 #define TAG "OIC_CA_CONN_MGR"
50 static bool g_isInitialized = false;
53 // CAAdapterNetDTLS will register the callback.
54 // Taking callback all the way through adapters not the right approach, hence calling here.
55 extern void CADTLSSetCredentialsCallback(CAGetDTLSPskCredentialsHandler credCallback);
59 // CAAdapterNetDTLS will register the callback.
60 // Taking callback all the way through adapters not the right approach, hence calling here.
61 extern void CADTLSSetX509CredentialsCallback(CAGetDTLSX509CredentialsHandler credCallback);
62 extern void CADTLSSetCrlCallback(CAGetDTLSCrlHandler crlCallback);
66 extern void CAsetPkixInfoCallback(CAgetPkixInfoHandler infCallback);
67 extern void CAsetTlsCredentialsCallback(CAGetDTLSPskCredentialsHandler credCallback);
71 CAResult_t CAInitialize()
73 OIC_LOG_V(DEBUG, TAG, "IoTivity version is v%s", IOTIVITY_VERSION);
74 OIC_LOG(DEBUG, TAG, "CAInitialize");
78 if (0 != OCSeedRandom())
80 OIC_LOG(ERROR, TAG, "Seed Random Failed");
83 CAResult_t res = CAInitializeMessageHandler();
84 if (res != CA_STATUS_OK)
86 OIC_LOG(ERROR, TAG, "CAInitialize has failed");
89 g_isInitialized = true;
97 OIC_LOG(DEBUG, TAG, "CATerminate");
101 CATerminateMessageHandler();
102 CATerminateNetworkType();
104 g_isInitialized = false;
108 CAResult_t CAStartListeningServer()
110 OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
112 if (!g_isInitialized)
114 return CA_STATUS_NOT_INITIALIZED;
117 return CAStartListeningServerAdapters();
120 CAResult_t CAStopListeningServer()
122 OIC_LOG(DEBUG, TAG, "CAStopListeningServer");
124 if (!g_isInitialized)
126 return CA_STATUS_NOT_INITIALIZED;
129 return CAStopListeningServerAdapters();
132 CAResult_t CAStartDiscoveryServer()
134 OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
136 if (!g_isInitialized)
138 return CA_STATUS_NOT_INITIALIZED;
141 return CAStartDiscoveryServerAdapters();
144 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
145 CAErrorCallback ErrorHandler)
147 OIC_LOG(DEBUG, TAG, "CARegisterHandler");
149 if (!g_isInitialized)
151 OIC_LOG(DEBUG, TAG, "CA is not initialized");
155 CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
159 CAResult_t CARegisterDTLSHandshakeCallback(CAErrorCallback dtlsHandshakeCallback)
161 OIC_LOG(DEBUG, TAG, "CARegisterDTLSHandshakeCallback");
163 if (!g_isInitialized)
165 return CA_STATUS_NOT_INITIALIZED;
168 CADTLSSetHandshakeCallback(dtlsHandshakeCallback);
173 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSPskCredentialsHandler GetDTLSCredentialsHandler)
175 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
177 if (!g_isInitialized)
179 return CA_STATUS_NOT_INITIALIZED;
182 CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
185 #endif //__WITH_DTLS__
187 CAResult_t CAregisterTlsHandshakeCallback(CAErrorCallback tlsHandshakeCallback)
189 OIC_LOG(DEBUG, TAG, "CARegisterTlsHandshakeCallback");
193 return CA_STATUS_NOT_INITIALIZED;
196 CAsetTlsHandshakeCallback(tlsHandshakeCallback);
200 CAResult_t CAregisterTlsCredentialsHandler(CAGetDTLSPskCredentialsHandler getTlsCredentialsHandler)
202 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
204 if (!g_isInitialized)
206 return CA_STATUS_NOT_INITIALIZED;
208 CAsetTlsCredentialsCallback(getTlsCredentialsHandler);
209 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
213 CAResult_t CAregisterPkixInfoHandler(CAgetPkixInfoHandler getPkixInfoHandler)
215 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
217 if (!g_isInitialized)
219 return CA_STATUS_NOT_INITIALIZED;
221 CAsetPkixInfoCallback(getPkixInfoHandler);
222 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
228 CAResult_t CARegisterDTLSX509CredentialsHandler(CAGetDTLSX509CredentialsHandler GetDTLSX509CredentialsHandler)
230 OIC_LOG(DEBUG, TAG, "CARegisterDTLSX509CredentialsHandler");
232 if (!g_isInitialized)
234 return CA_STATUS_NOT_INITIALIZED;
237 CADTLSSetX509CredentialsCallback(GetDTLSX509CredentialsHandler);
241 CAResult_t CARegisterDTLSCrlHandler(CAGetDTLSCrlHandler GetDTLSCrlHandler)
243 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCrlHandler");
245 if (!g_isInitialized)
247 return CA_STATUS_NOT_INITIALIZED;
250 CADTLSSetCrlCallback(GetDTLSCrlHandler);
253 #endif //__WITH_X509__
255 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
256 CATransportAdapter_t adapter,
259 CAEndpoint_t **object)
263 OIC_LOG(ERROR, TAG, "Invalid Parameter");
264 return CA_STATUS_INVALID_PARAM;
267 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
270 return CA_STATUS_FAILED;
276 void CADestroyEndpoint(CAEndpoint_t *rep)
278 OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
283 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
285 OIC_LOG(DEBUG, TAG, "CAGenerateToken");
287 return CAGenerateTokenInternal(token, tokenLength);
290 void CADestroyToken(CAToken_t token)
292 OIC_LOG(DEBUG, TAG, "CADestroyToken");
294 CADestroyTokenInternal(token);
296 OIC_LOG(DEBUG, TAG, "OUT");
299 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
301 OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
303 if (!g_isInitialized)
305 return CA_STATUS_NOT_INITIALIZED;
308 return CAGetNetworkInformationInternal(info, size);
311 static CAResult_t CASendMessageMultiAdapter(const CAEndpoint_t *object, const void *sendMsg,
312 CADataType_t dataType)
314 OIC_LOG(DEBUG, TAG, "CASendMessageMultipleAdapter");
316 CATransportAdapter_t connTypes[] = {
319 ,CA_ADAPTER_GATT_BTLE
322 ,CA_ADAPTER_RFCOMM_BTEDR
328 ,CA_ADAPTER_REMOTE_ACCESS
335 CAEndpoint_t *cloneEp = CACloneEndpoint(object);
338 OIC_LOG(ERROR, TAG, "Failed to clone CAEndpoint");
339 return CA_MEMORY_ALLOC_FAILED;
342 CAResult_t ret = CA_STATUS_OK;
343 size_t numConnTypes = sizeof(connTypes) / sizeof(connTypes[0]);
345 for (size_t i = 0; i < numConnTypes && ret == CA_STATUS_OK; i++)
347 cloneEp->adapter = connTypes[i];
348 ret = CADetachSendMessage(cloneEp, sendMsg, dataType);
350 CAFreeEndpoint(cloneEp);
354 CAResult_t CASendRequest(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
356 OIC_LOG(DEBUG, TAG, "CASendRequest");
358 if (!g_isInitialized)
360 return CA_STATUS_NOT_INITIALIZED;
363 if (requestInfo && requestInfo->isMulticast &&
364 (object->adapter == CA_DEFAULT_ADAPTER || object->adapter == CA_ALL_ADAPTERS))
366 return CASendMessageMultiAdapter(object, requestInfo, CA_REQUEST_DATA);
370 return CADetachSendMessage(object, requestInfo, CA_REQUEST_DATA);
374 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
376 OIC_LOG(DEBUG, TAG, "CASendResponse");
378 if (!g_isInitialized)
380 return CA_STATUS_NOT_INITIALIZED;
383 if (!responseInfo || !object)
385 return CA_STATUS_INVALID_PARAM;
388 if (responseInfo->isMulticast &&
389 (object->adapter == CA_DEFAULT_ADAPTER || object->adapter == CA_ALL_ADAPTERS))
391 return CASendMessageMultiAdapter(object, responseInfo, responseInfo->info.dataType);
395 return CADetachSendMessage(object, responseInfo, responseInfo->info.dataType);
399 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
401 OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
403 if (!g_isInitialized)
405 return CA_STATUS_NOT_INITIALIZED;
408 CAResult_t res = CA_STATUS_OK;
410 if (interestedNetwork & CA_ADAPTER_IP)
412 res = CAAddNetworkType(CA_ADAPTER_IP);
413 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
415 else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
417 res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
418 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
420 else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
422 res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
423 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
427 else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
429 res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
430 OIC_LOG_V(DEBUG, TAG,
431 "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
436 else if (interestedNetwork & CA_ADAPTER_TCP)
438 res = CAAddNetworkType(CA_ADAPTER_TCP);
439 OIC_LOG_V(DEBUG, TAG,
440 "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
443 else if (interestedNetwork & CA_ADAPTER_NFC)
445 res = CAAddNetworkType(CA_ADAPTER_NFC);
446 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_ADAPTER_NFC) function returns result : %d", res);
450 res = CA_NOT_SUPPORTED;
455 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
457 OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
459 if (!g_isInitialized)
461 return CA_STATUS_NOT_INITIALIZED;
464 CAResult_t res = CA_STATUS_OK;
466 if (nonInterestedNetwork & CA_ADAPTER_IP)
468 res = CARemoveNetworkType(CA_ADAPTER_IP);
469 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
471 else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
473 res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
474 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
476 else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
478 res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
479 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
482 else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
484 res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
485 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
492 else if (nonInterestedNetwork & CA_ADAPTER_TCP)
494 res = CARemoveNetworkType(CA_ADAPTER_TCP);
495 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
502 res = CA_STATUS_FAILED;
507 CAResult_t CAHandleRequestResponse()
509 if (!g_isInitialized)
511 OIC_LOG(ERROR, TAG, "not initialized");
512 return CA_STATUS_NOT_INITIALIZED;
515 CAHandleRequestResponseCallbacks();
520 #if defined (__WITH_DTLS__) || defined(__WITH_TLS__)
521 CAResult_t CASelectCipherSuite(const uint16_t cipher, CATransportAdapter_t adapter)
523 OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
524 if(CA_ADAPTER_IP == adapter)
526 if (CA_STATUS_OK != CADtlsSelectCipherSuite(cipher))
528 return CA_STATUS_FAILED;
532 else if(CA_ADAPTER_TCP == adapter)
534 if (CA_STATUS_OK != CAsetTlsCipherSuite(cipher))
536 return CA_STATUS_FAILED;
543 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
545 OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
548 if (CA_STATUS_OK != CADtlsEnableAnonECDHCipherSuite(enable))
550 return CA_STATUS_FAILED;
554 // TLS_ECDH_ANON_WITH_AES_128_CBC_SHA256 0xFF00 replaces 0xC018
555 // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 0xC037
556 if (CA_STATUS_OK != CAsetTlsCipherSuite(enable ? 0xFF00 : 0xC037))
558 return CA_STATUS_FAILED;
564 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
565 const uint8_t* label, const size_t labelLen,
566 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
567 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
568 uint8_t* ownerPSK, const size_t ownerPSKSize)
570 OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
572 CAResult_t res = CA_STATUS_OK;
574 //newOwnerLabel and prevOwnerLabe can be NULL
575 if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
577 return CA_STATUS_INVALID_PARAM;
580 if(CA_ADAPTER_IP == endpoint->adapter)
582 res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
583 rsrcServerDeviceID, rsrcServerDeviceIDLen,
584 provServerDeviceID, provServerDeviceIDLen,
585 ownerPSK, ownerPSKSize);
590 res = CAtlsGenerateOwnerPSK(endpoint, ownerPSK, ownerPSKSize,
591 rsrcServerDeviceID, rsrcServerDeviceIDLen);
594 if (CA_STATUS_OK != res)
596 OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
599 OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
604 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
606 OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
607 CAResult_t res = CA_STATUS_OK;
611 return CA_STATUS_INVALID_PARAM;
614 res = CADtlsInitiateHandshake(endpoint);
615 if (CA_STATUS_OK != res)
617 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
620 OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
625 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
627 OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
628 CAResult_t res = CA_STATUS_OK;
632 return CA_STATUS_INVALID_PARAM;
635 res = CADtlsClose(endpoint);
636 if (CA_STATUS_OK != res)
638 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
641 OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
646 #endif /* __WITH_DTLS__ */
649 void CARegisterKeepAliveHandler(CAKeepAliveConnectionCallback ConnHandler)
651 CATCPSetKeepAliveCallbacks(ConnHandler);