Skip to content

Commit 2cd83b4

Browse files
authored
Merge pull request #9392 from michalpasztamobica/tlssocket_documentation_update
Documentation of TLSSocket behavior on AUTH_FAILURE
2 parents af52c30 + 2cda5d2 commit 2cd83b4

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

UNITTESTS/features/netsocket/DTLSSocketWrapper/test_DTLSSocketWrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ TEST_F(TestDTLSSocketWrapper, connect_fail_ctr_drbg_seed)
163163
stack.return_value = NSAPI_ERROR_OK;
164164
const SocketAddress a("127.0.0.1", 1024);
165165
stack.return_socketAddress = a;
166-
EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_PARAMETER);
166+
EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_AUTH_FAILURE);
167167
mbedtls_stub.crt_expected_int = 0;
168168
}
169169

@@ -175,7 +175,7 @@ TEST_F(TestDTLSSocketWrapper, connect_fail_ssl_setup)
175175
stack.return_value = NSAPI_ERROR_OK;
176176
const SocketAddress a("127.0.0.1", 1024);
177177
stack.return_socketAddress = a;
178-
EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_PARAMETER);
178+
EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_AUTH_FAILURE);
179179
}
180180

181181
/* send */

UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ TEST_F(TestTLSSocketWrapper, connect_fail_ctr_drbg_seed)
159159
mbedtls_stub.crt_expected_int = 1; // mbedtls_ctr_drbg_seed error
160160
stack.return_value = NSAPI_ERROR_OK;
161161
const SocketAddress a("127.0.0.1", 1024);
162-
EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_PARAMETER);
162+
EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_AUTH_FAILURE);
163163
mbedtls_stub.crt_expected_int = 0;
164164
}
165165

@@ -171,7 +171,7 @@ TEST_F(TestTLSSocketWrapper, connect_fail_ssl_setup)
171171
mbedtls_stub.retArray[1] = 2; // mbedtls_ssl_setup error
172172
stack.return_value = NSAPI_ERROR_OK;
173173
const SocketAddress a("127.0.0.1", 1024);
174-
EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_PARAMETER);
174+
EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_AUTH_FAILURE);
175175
}
176176

177177
TEST_F(TestTLSSocketWrapper, connect_handshake_fail_ssl_handshake)

features/netsocket/Socket.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class Socket {
6464
* To reset the peer address, there must be zero initialized(default constructor) SocketAddress
6565
* objects in the address parameter.
6666
*
67+
* @note If connect() fails it is recommended to close the Socket and create
68+
* a new one before attempting to reconnect.
69+
*
6770
* @param address The SocketAddress of the remote peer.
6871
* @return NSAPI_ERROR_OK on success, negative error code on failure.
6972
*/

features/netsocket/TLSSocket.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class TLSSocket : public TLSSocketWrapper {
8282
* Initiates a connection to a remote server specified by either
8383
* a domain name or an IP address and port.
8484
*
85+
* @note: In case connect() returns NSAPI_ERROR_AUTH_FAILURE,
86+
* the socket must be freed either by calling close() or destroying it.
87+
*
8588
* @param host Hostname of the remote host.
8689
* @param port Port of the remote host.
8790
* @return 0 on success, negative error code on failure.

features/netsocket/TLSSocketWrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ nsapi_error_t TLSSocketWrapper::start_handshake(bool first_call)
171171
(const unsigned char *) DRBG_PERS,
172172
sizeof(DRBG_PERS))) != 0) {
173173
print_mbedtls_error("mbedtls_crt_drbg_init", ret);
174-
return NSAPI_ERROR_PARAMETER;
174+
return NSAPI_ERROR_AUTH_FAILURE;
175175
}
176176

177177
mbedtls_ssl_conf_rng(get_ssl_config(), mbedtls_ctr_drbg_random, &_ctr_drbg);
@@ -186,7 +186,7 @@ nsapi_error_t TLSSocketWrapper::start_handshake(bool first_call)
186186
tr_debug("mbedtls_ssl_setup()");
187187
if ((ret = mbedtls_ssl_setup(&_ssl, get_ssl_config())) != 0) {
188188
print_mbedtls_error("mbedtls_ssl_setup", ret);
189-
return NSAPI_ERROR_PARAMETER;
189+
return NSAPI_ERROR_AUTH_FAILURE;
190190
}
191191

192192
_transport->set_blocking(false);

features/netsocket/TLSSocketWrapper.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class TLSSocketWrapper : public Socket {
7676
void set_hostname(const char *hostname);
7777

7878
/** Sets the certification of Root CA.
79+
*
80+
* @note Must be called before calling connect()
7981
*
8082
* @param root_ca Root CA Certificate in any Mbed TLS-supported format.
8183
* @param len Length of certificate (including terminating 0 for PEM).
@@ -84,9 +86,10 @@ class TLSSocketWrapper : public Socket {
8486
nsapi_error_t set_root_ca_cert(const void *root_ca, size_t len);
8587

8688
/** Sets the certification of Root CA.
89+
*
90+
* @note Must be called before calling connect()
8791
*
8892
* @param root_ca_pem Root CA Certificate in PEM format.
89-
* @return 0 on success, negative error code on failure.
9093
*/
9194
nsapi_error_t set_root_ca_cert(const char *root_ca_pem);
9295

@@ -136,6 +139,10 @@ class TLSSocketWrapper : public Socket {
136139

137140
/* = Functions inherited from Socket = */
138141
virtual nsapi_error_t close();
142+
/*
143+
* @note: In case connect() returns an error, the state of the socket is
144+
* unspecified. A new socket should be created before reconnecting.
145+
*/
139146
virtual nsapi_error_t connect(const SocketAddress &address = SocketAddress());
140147
virtual nsapi_size_or_error_t sendto(const SocketAddress &address, const void *data, nsapi_size_t size);
141148
virtual nsapi_size_or_error_t recvfrom(SocketAddress *address,

0 commit comments

Comments
 (0)