1 /*
2 * FreeRTOS V202212.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
24 *
25 */
26
27 /**
28 * @file mbedtls_bio_tcp_sockets_wrapper.c
29 * @brief Implements mbed TLS platform send/receive functions for the TCP sockets wrapper.
30 */
31
32 /* MbedTLS includes. */
33 #if !defined( MBEDTLS_CONFIG_FILE )
34 #include "mbedtls/mbedtls_config.h"
35 #else
36 #include MBEDTLS_CONFIG_FILE
37 #endif
38
39 #include "threading_alt.h"
40 #include "mbedtls/entropy.h"
41 #include "mbedtls/ssl.h"
42
43 /* TCP Sockets Wrapper include.*/
44 #include "tcp_sockets_wrapper.h"
45
46 /* MbedTLS Bio TCP sockets wrapper include. */
47 #include "mbedtls_bio_tcp_sockets_wrapper.h"
48
49 /**
50 * @brief Sends data over TCP socket.
51 *
52 * @param[in] ctx The network context containing the socket handle.
53 * @param[in] buf Buffer containing the bytes to send.
54 * @param[in] len Number of bytes to send from the buffer.
55 *
56 * @return Number of bytes sent on success; else a negative value.
57 */
xMbedTLSBioTCPSocketsWrapperSend(void * ctx,const unsigned char * buf,size_t len)58 int xMbedTLSBioTCPSocketsWrapperSend( void * ctx,
59 const unsigned char * buf,
60 size_t len )
61 {
62 int32_t xReturnStatus;
63
64 configASSERT( ctx != NULL );
65 configASSERT( buf != NULL );
66
67 xReturnStatus = TCP_Sockets_Send( ( Socket_t ) ctx, buf, len );
68
69 switch( xReturnStatus )
70 {
71 /* Socket was closed or just got closed. */
72 case TCP_SOCKETS_ERRNO_ENOTCONN:
73 /* Not enough memory for the socket to create either an Rx or Tx stream. */
74 case TCP_SOCKETS_ERRNO_ENOMEM:
75 /* Socket is not valid, is not a TCP socket, or is not bound. */
76 case TCP_SOCKETS_ERRNO_EINVAL:
77 /* Socket received a signal, causing the read operation to be aborted. */
78 case TCP_SOCKETS_ERRNO_EINTR:
79 xReturnStatus = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
80 break;
81
82 /* A timeout occurred before any data could be sent. */
83 case TCP_SOCKETS_ERRNO_ENOSPC:
84 xReturnStatus = MBEDTLS_ERR_SSL_TIMEOUT;
85 break;
86
87 default:
88 break;
89 }
90
91 return ( int ) xReturnStatus;
92 }
93
94 /**
95 * @brief Receives data from TCP socket.
96 *
97 * @param[in] ctx The network context containing the socket handle.
98 * @param[out] buf Buffer to receive bytes into.
99 * @param[in] len Number of bytes to receive from the network.
100 *
101 * @return Number of bytes received if successful; Negative value on error.
102 */
xMbedTLSBioTCPSocketsWrapperRecv(void * ctx,unsigned char * buf,size_t len)103 int xMbedTLSBioTCPSocketsWrapperRecv( void * ctx,
104 unsigned char * buf,
105 size_t len )
106 {
107 int32_t xReturnStatus;
108
109 configASSERT( ctx != NULL );
110 configASSERT( buf != NULL );
111
112 xReturnStatus = TCP_Sockets_Recv( ( Socket_t ) ctx, buf, len );
113
114 switch( xReturnStatus )
115 {
116 /* No data could be sent because the socket was or just got closed. */
117 case TCP_SOCKETS_ERRNO_ENOTCONN:
118 /* No data could be sent because there was insufficient memory. */
119 case TCP_SOCKETS_ERRNO_ENOMEM:
120 /* No data could be sent because xSocket was not a valid TCP socket. */
121 case TCP_SOCKETS_ERRNO_EINVAL:
122 xReturnStatus = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
123 break;
124
125 /* A timeout occurred before any data could be received. */
126 case 0:
127 xReturnStatus = MBEDTLS_ERR_SSL_WANT_READ;
128 break;
129
130 default:
131 break;
132 }
133
134 return ( int ) xReturnStatus;
135 }
136