Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TLSSocket.cpp Source File

TLSSocket.cpp

00001 /*
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "TLSSocket.h"
00019 
00020 #define TRACE_GROUP "TLSS"
00021 #include "mbed-trace/mbed_trace.h"
00022 
00023 #if !defined(MBED_CONF_NSAPI_OFFLOAD_TLSSOCKET) || !(MBED_CONF_NSAPI_OFFLOAD_TLSSOCKET)
00024 
00025 // This class requires Mbed TLS SSL/TLS client code
00026 #if defined(MBEDTLS_SSL_CLI_C)
00027 
00028 nsapi_error_t TLSSocket::connect(const char *host, uint16_t port)
00029 {
00030     nsapi_error_t ret = NSAPI_ERROR_OK ;
00031     if (!is_handshake_started()) {
00032         ret = tcp_socket.connect(host, port);
00033         if (ret == NSAPI_ERROR_OK  || ret == NSAPI_ERROR_IN_PROGRESS ) {
00034             set_hostname(host);
00035         }
00036         if (ret != NSAPI_ERROR_OK  && ret != NSAPI_ERROR_IS_CONNECTED ) {
00037             return ret;
00038         }
00039     }
00040     return TLSSocketWrapper::start_handshake(ret == NSAPI_ERROR_OK );
00041 }
00042 
00043 TLSSocket::~TLSSocket()
00044 {
00045     /* Transport is a member of TLSSocket which is derived from TLSSocketWrapper.
00046      * Make sure that TLSSocketWrapper::close() is called before the transport is
00047      * destroyed.
00048      */
00049     close();
00050 }
00051 #endif // MBEDTLS_SSL_CLI_C
00052 
00053 #else // MBED_CONF_NSAPI_OFFLOAD_TLSSOCKET
00054 
00055 TLSSocket::TLSSocket()
00056 {
00057 }
00058 
00059 TLSSocket::~TLSSocket()
00060 {
00061 }
00062 
00063 
00064 nsapi_error_t TLSSocket::set_hostname(const char *hostname)
00065 {
00066     return setsockopt(NSAPI_TLSSOCKET_LEVEL , NSAPI_TLSSOCKET_SET_HOSTNAME , hostname, strlen(hostname));
00067 }
00068 
00069 nsapi_error_t TLSSocket::set_root_ca_cert(const void *root_ca, size_t len)
00070 {
00071     return setsockopt(NSAPI_TLSSOCKET_LEVEL , NSAPI_TLSSOCKET_SET_CACERT , root_ca, len);
00072 }
00073 
00074 nsapi_error_t TLSSocket::set_root_ca_cert(const char *root_ca_pem)
00075 {
00076     return set_root_ca_cert(root_ca_pem, strlen(root_ca_pem));
00077 }
00078 
00079 nsapi_error_t TLSSocket::set_client_cert_key(const void *client_cert, size_t client_cert_len,
00080                                              const void *client_private_key_pem, size_t client_private_key_len)
00081 {
00082     nsapi_error_t ret = setsockopt(NSAPI_TLSSOCKET_LEVEL , NSAPI_TLSSOCKET_SET_CLCERT , client_cert, client_cert_len);
00083     if (ret == NSAPI_ERROR_OK ) {
00084         ret = setsockopt(NSAPI_TLSSOCKET_LEVEL , NSAPI_TLSSOCKET_SET_CLKEY , client_private_key_pem, client_private_key_len);
00085     }
00086     return ret;
00087 }
00088 
00089 nsapi_error_t TLSSocket::set_client_cert_key(const char *client_cert_pem, const char *client_private_key_pem)
00090 {
00091     return set_client_cert_key(client_cert_pem, strlen(client_cert_pem), client_private_key_pem, strlen(client_private_key_pem));
00092 }
00093 
00094 nsapi_error_t TLSSocket::connect(const char *host, uint16_t port)
00095 {
00096     nsapi_error_t ret = enable_tlssocket();
00097     if (ret == NSAPI_ERROR_OK ) {
00098         ret = TCPSocket::connect(host, port);
00099     }
00100     return ret;
00101 }
00102 
00103 nsapi_error_t TLSSocket::connect(const SocketAddress &address)
00104 {
00105     nsapi_error_t ret = enable_tlssocket();
00106     if (ret == NSAPI_ERROR_OK ) {
00107         ret = TCPSocket::connect(address);
00108     }
00109     return ret;
00110 }
00111 
00112 nsapi_error_t TLSSocket::enable_tlssocket()
00113 {
00114     bool enabled = true;
00115     return setsockopt(NSAPI_TLSSOCKET_LEVEL , NSAPI_TLSSOCKET_ENABLE , &enabled, sizeof(enabled));
00116 }
00117 
00118 #endif // MBED_CONF_NSAPI_OFFLOAD_TLSSOCKET