@@ -65925,6 +65925,103 @@ static int test_wolfSSL_dtls13_null_cipher(void)
6592565925 return TEST_SKIPPED;
6592665926}
6592765927#endif
65928+ #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
65929+ !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
65930+ !defined(SINGLE_THREADED)
65931+
65932+ static int test_dtls_msg_get_connected_port(int fd, word16 *port)
65933+ {
65934+ SOCKADDR_S peer;
65935+ XSOCKLENT len;
65936+ int ret;
65937+
65938+ XMEMSET((byte*)&peer, 0, sizeof(peer));
65939+ len = sizeof(peer);
65940+ ret = getpeername(fd, (SOCKADDR*)&peer, &len);
65941+ if (ret != 0 || len > sizeof(peer))
65942+ return -1;
65943+ switch (peer.ss_family) {
65944+ #ifdef WOLFSSL_IPV6
65945+ case WOLFSSL_IP6: {
65946+ *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
65947+ break;
65948+ }
65949+ #endif /* WOLFSSL_IPV6 */
65950+ case WOLFSSL_IP4:
65951+ *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
65952+ break;
65953+ default:
65954+ return -1;
65955+ }
65956+ return 0;
65957+ }
65958+
65959+ static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
65960+ {
65961+ char buf[1] = {'t'};
65962+ SOCKADDR_IN_T addr;
65963+ int sock_fd;
65964+ word16 port;
65965+ int err;
65966+
65967+ (void)ssl;
65968+ (void)ctx;
65969+
65970+ err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
65971+ if (err != 0)
65972+ return -1;
65973+
65974+ sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
65975+ if (sock_fd == -1)
65976+ return -1;
65977+ build_addr(&addr, wolfSSLIP, port, 1, 0);
65978+
65979+ /* send a packet to the server. Being another socket, the kernel will ensure
65980+ * the source port will be different. */
65981+ err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
65982+ sizeof(addr));
65983+
65984+ close(sock_fd);
65985+ if (err == -1)
65986+ return -1;
65987+
65988+ return 0;
65989+ }
65990+
65991+ /* setup a SSL session but just after the handshake send a packet to the server
65992+ * with a source address different than the one of the connected client. The I/O
65993+ * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
65994+ * is done in test_dtls_msg_from_other_peer_cb */
65995+ static int test_dtls_msg_from_other_peer(void)
65996+ {
65997+ callback_functions client_cbs;
65998+ callback_functions server_cbs;
65999+
66000+ XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
66001+ XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
66002+
66003+ client_cbs.method = wolfDTLSv1_2_client_method;
66004+ server_cbs.method = wolfDTLSv1_2_server_method;
66005+ client_cbs.doUdp = 1;
66006+ server_cbs.doUdp = 1;
66007+
66008+ test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
66009+ test_dtls_msg_from_other_peer_cb);
66010+
66011+ if (client_cbs.return_code != WOLFSSL_SUCCESS ||
66012+ server_cbs.return_code != WOLFSSL_SUCCESS)
66013+ return TEST_FAIL;
66014+
66015+ return TEST_SUCCESS;
66016+ }
66017+ #else
66018+ static int test_dtls_msg_from_other_peer(void)
66019+ {
66020+ return TEST_SKIPPED;
66021+ }
66022+ #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
66023+ * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
66024+ * !defined(SINGLE_THREADED) */
6592866025/*----------------------------------------------------------------------------*
6592966026 | Main
6593066027 *----------------------------------------------------------------------------*/
@@ -66963,6 +67060,7 @@ TEST_CASE testCases[] = {
6696367060 TEST_DECL(test_override_alt_cert_chain),
6696467061 TEST_DECL(test_dtls13_bad_epoch_ch),
6696567062 TEST_DECL(test_wolfSSL_dtls13_null_cipher),
67063+ TEST_DECL(test_dtls_msg_from_other_peer),
6696667064 /* If at some point a stub get implemented this test should fail indicating
6696767065 * a need to implement a new test case
6696867066 */
0 commit comments