Coverage Report

Created: 2026-08-13 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsrtp/include/srtp_priv.h
Line
Count
Source
1
/*
2
 * srtp_priv.h
3
 *
4
 * private internal data structures and functions for libSRTP
5
 *
6
 * David A. McGrew
7
 * Cisco Systems, Inc.
8
 */
9
/*
10
 *
11
 * Copyright (c) 2001-2017 Cisco Systems, Inc.
12
 * All rights reserved.
13
 *
14
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions
16
 * are met:
17
 *
18
 *   Redistributions of source code must retain the above copyright
19
 *   notice, this list of conditions and the following disclaimer.
20
 *
21
 *   Redistributions in binary form must reproduce the above
22
 *   copyright notice, this list of conditions and the following
23
 *   disclaimer in the documentation and/or other materials provided
24
 *   with the distribution.
25
 *
26
 *   Neither the name of the Cisco Systems, Inc. nor the names of its
27
 *   contributors may be used to endorse or promote products derived
28
 *   from this software without specific prior written permission.
29
 *
30
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41
 * OF THE POSSIBILITY OF SUCH DAMAGE.
42
 *
43
 */
44
45
#ifndef SRTP_PRIV_H
46
#define SRTP_PRIV_H
47
48
// Leave this as the top level import. Ensures the existence of defines
49
#ifdef HAVE_CONFIG_H
50
#include <config.h>
51
#endif
52
53
#include "srtp.h"
54
#include "rdbx.h"
55
#include "rdb.h"
56
#include "cipher.h"
57
#include "auth.h"
58
#include "aes.h"
59
#include "crypto_kernel.h"
60
61
#ifdef __cplusplus
62
extern "C" {
63
#endif
64
65
0
#define SRTP_VER_STRING PACKAGE_STRING
66
0
#define SRTP_VERSION PACKAGE_VERSION
67
68
typedef struct srtp_stream_ctx_t_ srtp_stream_ctx_t;
69
typedef srtp_stream_ctx_t *srtp_stream_t;
70
typedef struct srtp_stream_list_ctx_t_ *srtp_stream_list_t;
71
72
typedef struct srtp_crypto_policy_t {
73
    srtp_cipher_type_id_t cipher_type;
74
    size_t cipher_key_len;
75
    srtp_auth_type_id_t auth_type;
76
    size_t auth_key_len;
77
    size_t auth_tag_len;
78
    srtp_sec_serv_t sec_serv;
79
} srtp_crypto_policy_t;
80
81
typedef struct srtp_master_key_t {
82
    uint8_t key[SRTP_MAX_KEY_LEN];
83
    size_t key_len;
84
    size_t salt_len;
85
    uint8_t mki_id[SRTP_MAX_MKI_LEN];
86
    size_t mki_id_len;
87
} srtp_master_key_t;
88
89
typedef struct srtp_policy_ctx_t_ {
90
    srtp_profile_t profile; /**< The SRTP profile that this policy applies to */
91
    srtp_ssrc_t ssrc;       /**< The SSRC value of stream, or the    */
92
                            /**< flags SSRC_ANY_INBOUND or           */
93
                            /**< SSRC_ANY_OUTBOUND if key sharing    */
94
                            /**< is used for this policy element.    */
95
    srtp_crypto_policy_t rtp;  /**< SRTP crypto policy.                 */
96
    srtp_crypto_policy_t rtcp; /**< SRTCP crypto policy.                */
97
    srtp_master_key_t master_keys[SRTP_MAX_NUM_MASTER_KEYS];
98
    size_t num_master_keys; /** Number of master keys                */
99
    bool use_mki;           /** Whether MKI is in use                */
100
    size_t mki_size;        /** Size of MKI when in use              */
101
    size_t window_size;     /**< The window size to use for replay   */
102
                            /**< protection.                         */
103
    bool allow_repeat_tx;   /**< Whether retransmissions of          */
104
                            /**< packets with the same sequence      */
105
                            /**< number are allowed.                 */
106
                            /**< (Note that such repeated            */
107
                            /**< transmissions must have the same    */
108
                            /**< RTP payload, or a severe security   */
109
                            /**< weakness is introduced!)            */
110
    uint8_t enc_xtn_hdr[SRTP_MAX_NUM_ENC_HDR_XTND_IDS]; /**< List of header ids
111
                                                           to encrypt.      */
112
    size_t enc_xtn_hdr_count; /**< Number of entries in list of header */
113
                              /**<  ids.                               */
114
    bool use_cryptex;         /**< Encrypt header block and CSRCs with */
115
                              /**< cryptex, RFC 9335.                  */
116
} srtp_policy_ctx_t_;
117
118
static inline bool srtp_policy_is_null_cipher_null_auth(
119
    const srtp_policy_t policy)
120
43.8k
{
121
43.8k
    return policy != NULL && policy->rtp.cipher_type == SRTP_NULL_CIPHER &&
122
36.4k
           policy->rtp.auth_type == SRTP_NULL_AUTH &&
123
31.3k
           policy->rtcp.cipher_type == SRTP_NULL_CIPHER &&
124
31.3k
           policy->rtcp.auth_type == SRTP_NULL_AUTH;
125
43.8k
}
Unexecuted instantiation: fuzzer.c:srtp_policy_is_null_cipher_null_auth
srtp.c:srtp_policy_is_null_cipher_null_auth
Line
Count
Source
120
19.1k
{
121
19.1k
    return policy != NULL && policy->rtp.cipher_type == SRTP_NULL_CIPHER &&
122
19.1k
           policy->rtp.auth_type == SRTP_NULL_AUTH &&
123
19.1k
           policy->rtcp.cipher_type == SRTP_NULL_CIPHER &&
124
19.1k
           policy->rtcp.auth_type == SRTP_NULL_AUTH;
125
19.1k
}
srtp_policy.c:srtp_policy_is_null_cipher_null_auth
Line
Count
Source
120
24.6k
{
121
24.6k
    return policy != NULL && policy->rtp.cipher_type == SRTP_NULL_CIPHER &&
122
17.2k
           policy->rtp.auth_type == SRTP_NULL_AUTH &&
123
12.1k
           policy->rtcp.cipher_type == SRTP_NULL_CIPHER &&
124
12.1k
           policy->rtcp.auth_type == SRTP_NULL_AUTH;
125
24.6k
}
126
127
static inline bool srtp_policy_is_valid_window_size(size_t window_size)
128
64.8k
{
129
64.8k
    return window_size == 0 || (window_size >= 64 && window_size < 0x8000);
130
64.8k
}
Unexecuted instantiation: fuzzer.c:srtp_policy_is_valid_window_size
srtp.c:srtp_policy_is_valid_window_size
Line
Count
Source
128
16.8k
{
129
16.8k
    return window_size == 0 || (window_size >= 64 && window_size < 0x8000);
130
16.8k
}
srtp_policy.c:srtp_policy_is_valid_window_size
Line
Count
Source
128
48.0k
{
129
48.0k
    return window_size == 0 || (window_size >= 64 && window_size < 0x8000);
130
48.0k
}
131
132
/*
133
 * the following declarations are libSRTP internal functions
134
 */
135
136
/*
137
 * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
138
 * to ssrc, or NULL if no stream exists for that ssrc
139
 */
140
srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc);
141
142
/*
143
 * libsrtp internal datatypes
144
 */
145
typedef enum direction_t {
146
    dir_unknown = 0,
147
    dir_srtp_sender = 1,
148
    dir_srtp_receiver = 2
149
} direction_t;
150
151
/*
152
 * srtp_session_keys_t will contain the encryption, hmac, salt keys
153
 * for both SRTP and SRTCP.  The session keys will also contain the
154
 * MKI ID which is used to identify the session keys.
155
 */
156
typedef struct srtp_session_keys_t {
157
    srtp_cipher_t *rtp_cipher;
158
    srtp_cipher_t *rtp_xtn_hdr_cipher;
159
    srtp_auth_t *rtp_auth;
160
    srtp_cipher_t *rtcp_cipher;
161
    srtp_auth_t *rtcp_auth;
162
    uint8_t salt[SRTP_AEAD_SALT_LEN];
163
    uint8_t c_salt[SRTP_AEAD_SALT_LEN];
164
    uint8_t *mki_id;
165
    srtp_key_limit_ctx_t *limit;
166
} srtp_session_keys_t;
167
168
/*
169
 * an srtp_stream_t has its own SSRC, encryption key, authentication
170
 * key, sequence number, and replay database
171
 *
172
 * note that the keys might not actually be unique, in which case the
173
 * srtp_cipher_t and srtp_auth_t pointers will point to the same structures
174
 */
175
typedef struct srtp_stream_ctx_t_ {
176
    uint32_t ssrc;
177
    srtp_session_keys_t *session_keys;
178
    size_t num_master_keys;
179
    bool use_mki;
180
    size_t mki_size;
181
    srtp_rdbx_t rtp_rdbx;
182
    srtp_sec_serv_t rtp_services;
183
    srtp_rdb_t rtcp_rdb;
184
    srtp_sec_serv_t rtcp_services;
185
    direction_t direction;
186
    bool allow_repeat_tx;
187
    uint8_t *enc_xtn_hdr;
188
    size_t enc_xtn_hdr_count;
189
    uint32_t pending_roc;
190
    bool use_cryptex;
191
} strp_stream_ctx_t_;
192
193
/*
194
 * an srtp_ctx_t holds a stream list and a service description
195
 */
196
typedef struct srtp_ctx_t_ {
197
    srtp_stream_list_t stream_list;             /* linked list of streams     */
198
    struct srtp_stream_ctx_t_ *stream_template; /* act as template for other  */
199
                                                /* streams                    */
200
    void *user_data;                            /* user custom data           */
201
} srtp_ctx_t_;
202
203
/*
204
 * srtp_hdr_t represents an RTP or SRTP header.  The bit-fields in
205
 * this structure should be declared "unsigned int" instead of
206
 * "unsigned char", but doing so causes the MS compiler to not
207
 * fully pack the bit fields.
208
 *
209
 * In this implementation, an srtp_hdr_t is assumed to be 32-bit aligned
210
 *
211
 * (note that this definition follows that of RFC 1889 Appendix A, but
212
 * is not identical)
213
 */
214
215
#ifndef WORDS_BIGENDIAN
216
217
typedef struct {
218
    unsigned char cc : 4;      /* CSRC count             */
219
    unsigned char x : 1;       /* header extension flag  */
220
    unsigned char p : 1;       /* padding flag           */
221
    unsigned char version : 2; /* protocol version       */
222
    unsigned char pt : 7;      /* payload type           */
223
    unsigned char m : 1;       /* marker bit             */
224
    uint16_t seq;              /* sequence number        */
225
    uint32_t ts;               /* timestamp              */
226
    uint32_t ssrc;             /* synchronization source */
227
} srtp_hdr_t;
228
229
#else /*  BIG_ENDIAN */
230
231
typedef struct {
232
    unsigned char version : 2; /* protocol version       */
233
    unsigned char p : 1;       /* padding flag           */
234
    unsigned char x : 1;       /* header extension flag  */
235
    unsigned char cc : 4;      /* CSRC count             */
236
    unsigned char m : 1;       /* marker bit             */
237
    unsigned char pt : 7;      /* payload type           */
238
    uint16_t seq;              /* sequence number        */
239
    uint32_t ts;               /* timestamp              */
240
    uint32_t ssrc;             /* synchronization source */
241
} srtp_hdr_t;
242
243
#endif
244
245
typedef struct {
246
    uint16_t profile_specific; /* profile-specific info               */
247
    uint16_t length;           /* number of 32-bit words in extension */
248
} srtp_hdr_xtnd_t;
249
250
/*
251
 * srtcp_hdr_t represents a secure rtcp header
252
 *
253
 * in this implementation, an srtcp header is assumed to be 32-bit
254
 * aligned
255
 */
256
257
#ifndef WORDS_BIGENDIAN
258
259
typedef struct {
260
    unsigned char rc : 5;      /* reception report count */
261
    unsigned char p : 1;       /* padding flag           */
262
    unsigned char version : 2; /* protocol version       */
263
    unsigned char pt : 8;      /* payload type           */
264
    uint16_t len;              /* length                 */
265
    uint32_t ssrc;             /* synchronization source */
266
} srtcp_hdr_t;
267
268
typedef struct {
269
    unsigned int index : 31; /* srtcp packet index in network order!  */
270
    unsigned int e : 1;      /* encrypted? 1=yes                      */
271
                             /* optional mikey/etc go here            */
272
                             /* and then the variable-length auth tag */
273
} srtcp_trailer_t;
274
275
#else /*  BIG_ENDIAN */
276
277
typedef struct {
278
    unsigned char version : 2; /* protocol version       */
279
    unsigned char p : 1;       /* padding flag           */
280
    unsigned char rc : 5;      /* reception report count */
281
    unsigned char pt : 8;      /* payload type           */
282
    uint16_t len;              /* length                 */
283
    uint32_t ssrc;             /* synchronization source */
284
} srtcp_hdr_t;
285
286
typedef struct {
287
    unsigned int e : 1;      /* encrypted? 1=yes                      */
288
    unsigned int index : 31; /* srtcp packet index                    */
289
                             /* optional mikey/etc go here            */
290
                             /* and then the variable-length auth tag */
291
} srtcp_trailer_t;
292
293
#endif
294
295
/*
296
 * srtp_handle_event(srtp, srtm, evnt) calls the event handling
297
 * function, if there is one.
298
 *
299
 * This macro is not included in the documentation as it is
300
 * an internal-only function.
301
 */
302
303
#define srtp_handle_event(srtp, strm, evnt)                                    \
304
325
    if (srtp_event_handler) {                                                  \
305
325
        srtp_event_data_t data;                                                \
306
325
        data.session = srtp;                                                   \
307
325
        data.ssrc = ntohl(strm->ssrc);                                         \
308
325
        data.event = evnt;                                                     \
309
325
        srtp_event_handler(&data);                                             \
310
325
    }
311
312
#ifdef __cplusplus
313
}
314
#endif
315
316
#endif /* SRTP_PRIV_H */