summaryrefslogtreecommitdiff
path: root/nx_secure/src/nx_secure_dtls_session_send.c
blob: f677773fb0bbcbdef037a4d23e7457c834702c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Secure Component                                                 */
/**                                                                       */
/**    Datagram Transport Layer Security (DTLS)                           */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SECURE_SOURCE_CODE

#include "nx_secure_dtls.h"

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_secure_dtls_session_send                        PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sends data using an active DTLS session, handling     */
/*    all encryption and hashing before sending data over the UDP socket. */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    dtls_session                          DTLS control block            */
/*    packet_ptr                            Pointer to packet data        */
/*    ip_address                            Remote IP address             */
/*    port                                  Remote port                   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_secure_dtls_send_record           Send DTLS encrypted record    */
/*    tx_mutex_get                          Get protection mutex          */
/*    tx_mutex_put                          Put protection mutex          */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*    nx_secure_dtls_server_session_send    Server session send packet    */
/*                                                                        */
/**************************************************************************/
UINT _nx_secure_dtls_session_send(NX_SECURE_DTLS_SESSION *dtls_session, NX_PACKET *packet_ptr,
                                  NXD_ADDRESS *ip_address, UINT port)
{
#ifdef NX_SECURE_ENABLE_DTLS
UINT status;

    /* Get the protection. */
    tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);

    /* Check that the passed-in DTLS session matches the ip_address and port passed in by the caller. */
    if (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_version == 0)
    {

        /* If the IP Address and port are uninitialized, set them now (possibly an error?). */
        NX_SECURE_MEMCPY(&dtls_session -> nx_secure_dtls_remote_ip_address, ip_address, sizeof(NXD_ADDRESS)); /* Use case of memcpy is verified. */
        dtls_session -> nx_secure_dtls_local_port = port;
    }
    else if ((dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_version != ip_address -> nxd_ip_version) ||
             (dtls_session -> nx_secure_dtls_remote_port != port))
    {

        /* Release the protection. */
        tx_mutex_put(&_nx_secure_tls_protection);

        /* IP address and port don't match - probably caller error. */
        return(NX_SECURE_TLS_SEND_ADDRESS_MISMATCH);
    }
    else
    {
#ifndef NX_DISABLE_IPV4
        if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
        {
            if (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v4 != ip_address -> nxd_ip_address.v4)
            {

                /* Release the protection. */
                tx_mutex_put(&_nx_secure_tls_protection);

                /* IP address and port don't match - probably caller error. */
                return(NX_SECURE_TLS_SEND_ADDRESS_MISMATCH);
            }
        }
#endif /* !NX_DISABLE_IPV4  */

#ifdef FEATURE_NX_IPV6
        if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
        {
            if ((dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[0] != ip_address -> nxd_ip_address.v6[0]) ||
                (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[1] != ip_address -> nxd_ip_address.v6[1]) ||
                (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[2] != ip_address -> nxd_ip_address.v6[2]) ||
                (dtls_session -> nx_secure_dtls_remote_ip_address.nxd_ip_address.v6[3] != ip_address -> nxd_ip_address.v6[3]))
            {

                /* Release the protection. */
                tx_mutex_put(&_nx_secure_tls_protection);

                /* IP address and port don't match - probably caller error. */
                return(NX_SECURE_TLS_SEND_ADDRESS_MISMATCH);
            }
        }
#endif /* FEATURE_NX_IPV6 */
    }

    status = _nx_secure_dtls_send_record(dtls_session, packet_ptr, NX_SECURE_TLS_APPLICATION_DATA, NX_WAIT_FOREVER);

    /* Release the protection. */
    tx_mutex_put(&_nx_secure_tls_protection);

    return(status);
#else
    NX_PARAMETER_NOT_USED(dtls_session);
    NX_PARAMETER_NOT_USED(packet_ptr);
    NX_PARAMETER_NOT_USED(ip_address);
    NX_PARAMETER_NOT_USED(port);

    return(NX_NOT_SUPPORTED);
#endif /* NX_SECURE_ENABLE_DTLS */
}