Coverage Report

Created: 2026-09-14 06:20

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_rcc_mode_t rcc_mode; /**< RFC 4771 RCC integrity transform     */
117
                              /**< mode for SRTP (default none).        */
118
    uint16_t roc_tx_rate;     /**< RFC 4771 ROC transmission rate R:    */
119
                              /**< the ROC is carried in packets whose  */
120
                              /**< sequence number is 0 modulo R. A     */
121
                              /**< value of 0 is treated as 1.          */
122
} srtp_policy_ctx_t_;
123
124
static inline bool srtp_policy_is_null_cipher_null_auth(
125
    const srtp_policy_t policy)
126
43.8k
{
127
43.8k
    return policy != NULL && policy->rtp.cipher_type == SRTP_NULL_CIPHER &&
128
36.4k
           policy->rtp.auth_type == SRTP_NULL_AUTH &&
129
31.3k
           policy->rtcp.cipher_type == SRTP_NULL_CIPHER &&
130
31.3k
           policy->rtcp.auth_type == SRTP_NULL_AUTH;
131
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
126
19.1k
{
127
19.1k
    return policy != NULL && policy->rtp.cipher_type == SRTP_NULL_CIPHER &&
128
19.1k
           policy->rtp.auth_type == SRTP_NULL_AUTH &&
129
19.1k
           policy->rtcp.cipher_type == SRTP_NULL_CIPHER &&
130
19.1k
           policy->rtcp.auth_type == SRTP_NULL_AUTH;
131
19.1k
}
srtp_policy.c:srtp_policy_is_null_cipher_null_auth
Line
Count
Source
126
24.6k
{
127
24.6k
    return policy != NULL && policy->rtp.cipher_type == SRTP_NULL_CIPHER &&
128
17.2k
           policy->rtp.auth_type == SRTP_NULL_AUTH &&
129
12.1k
           policy->rtcp.cipher_type == SRTP_NULL_CIPHER &&
130
12.1k
           policy->rtcp.auth_type == SRTP_NULL_AUTH;
131
24.6k
}
132
133
static inline bool srtp_policy_is_valid_window_size(size_t window_size)
134
64.8k
{
135
64.8k
    return window_size == 0 || (window_size >= 64 && window_size < 0x8000);
136
64.8k
}
Unexecuted instantiation: fuzzer.c:srtp_policy_is_valid_window_size
srtp.c:srtp_policy_is_valid_window_size
Line
Count
Source
134
16.8k
{
135
16.8k
    return window_size == 0 || (window_size >= 64 && window_size < 0x8000);
136
16.8k
}
srtp_policy.c:srtp_policy_is_valid_window_size
Line
Count
Source
134
48.0k
{
135
48.0k
    return window_size == 0 || (window_size >= 64 && window_size < 0x8000);
136
48.0k
}
137
138
/*
139
 * the following declarations are libSRTP internal functions
140
 */
141
142
/*
143
 * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
144
 * to ssrc, or NULL if no stream exists for that ssrc
145
 */
146
srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc);
147
148
/*
149
 * libsrtp internal datatypes
150
 */
151
typedef enum direction_t {
152
    dir_unknown = 0,
153
    dir_srtp_sender = 1,
154
    dir_srtp_receiver = 2
155
} direction_t;
156
157
/*
158
 * srtp_session_keys_t will contain the encryption, hmac, salt keys
159
 * for both SRTP and SRTCP.  The session keys will also contain the
160
 * MKI ID which is used to identify the session keys.
161
 */
162
typedef struct srtp_session_keys_t {
163
    srtp_cipher_t *rtp_cipher;
164
    srtp_cipher_t *rtp_xtn_hdr_cipher;
165
    srtp_auth_t *rtp_auth;
166
    srtp_cipher_t *rtcp_cipher;
167
    srtp_auth_t *rtcp_auth;
168
    uint8_t salt[SRTP_AEAD_SALT_LEN];
169
    uint8_t c_salt[SRTP_AEAD_SALT_LEN];
170
    uint8_t *mki_id;
171
    srtp_key_limit_ctx_t *limit;
172
} srtp_session_keys_t;
173
174
/*
175
 * an srtp_stream_t has its own SSRC, encryption key, authentication
176
 * key, sequence number, and replay database
177
 *
178
 * note that the keys might not actually be unique, in which case the
179
 * srtp_cipher_t and srtp_auth_t pointers will point to the same structures
180
 */
181
typedef struct srtp_stream_ctx_t_ {
182
    uint32_t ssrc;
183
    srtp_session_keys_t *session_keys;
184
    size_t num_master_keys;
185
    bool use_mki;
186
    size_t mki_size;
187
    srtp_rdbx_t rtp_rdbx;
188
    srtp_sec_serv_t rtp_services;
189
    srtp_rdb_t rtcp_rdb;
190
    srtp_sec_serv_t rtcp_services;
191
    direction_t direction;
192
    bool allow_repeat_tx;
193
    uint8_t *enc_xtn_hdr;
194
    size_t enc_xtn_hdr_count;
195
    uint32_t pending_roc;
196
    bool use_cryptex;
197
    srtp_rcc_mode_t rcc_mode; /* RFC 4771 RCC integrity transform mode      */
198
    uint16_t roc_tx_rate;     /* RFC 4771 ROC transmission rate R (>= 1)    */
199
} strp_stream_ctx_t_;
200
201
/*
202
 * an srtp_ctx_t holds a stream list and a service description
203
 */
204
typedef struct srtp_ctx_t_ {
205
    srtp_stream_list_t stream_list;             /* linked list of streams     */
206
    struct srtp_stream_ctx_t_ *stream_template; /* act as template for other  */
207
                                                /* streams                    */
208
    void *user_data;                            /* user custom data           */
209
} srtp_ctx_t_;
210
211
/*
212
 * srtp_hdr_t represents an RTP or SRTP header.  The bit-fields in
213
 * this structure should be declared "unsigned int" instead of
214
 * "unsigned char", but doing so causes the MS compiler to not
215
 * fully pack the bit fields.
216
 *
217
 * In this implementation, an srtp_hdr_t is assumed to be 32-bit aligned
218
 *
219
 * (note that this definition follows that of RFC 1889 Appendix A, but
220
 * is not identical)
221
 */
222
223
#ifndef WORDS_BIGENDIAN
224
225
typedef struct {
226
    unsigned char cc : 4;      /* CSRC count             */
227
    unsigned char x : 1;       /* header extension flag  */
228
    unsigned char p : 1;       /* padding flag           */
229
    unsigned char version : 2; /* protocol version       */
230
    unsigned char pt : 7;      /* payload type           */
231
    unsigned char m : 1;       /* marker bit             */
232
    uint16_t seq;              /* sequence number        */
233
    uint32_t ts;               /* timestamp              */
234
    uint32_t ssrc;             /* synchronization source */
235
} srtp_hdr_t;
236
237
#else /*  BIG_ENDIAN */
238
239
typedef struct {
240
    unsigned char version : 2; /* protocol version       */
241
    unsigned char p : 1;       /* padding flag           */
242
    unsigned char x : 1;       /* header extension flag  */
243
    unsigned char cc : 4;      /* CSRC count             */
244
    unsigned char m : 1;       /* marker bit             */
245
    unsigned char pt : 7;      /* payload type           */
246
    uint16_t seq;              /* sequence number        */
247
    uint32_t ts;               /* timestamp              */
248
    uint32_t ssrc;             /* synchronization source */
249
} srtp_hdr_t;
250
251
#endif
252
253
typedef struct {
254
    uint16_t profile_specific; /* profile-specific info               */
255
    uint16_t length;           /* number of 32-bit words in extension */
256
} srtp_hdr_xtnd_t;
257
258
/*
259
 * srtcp_hdr_t represents a secure rtcp header
260
 *
261
 * in this implementation, an srtcp header is assumed to be 32-bit
262
 * aligned
263
 */
264
265
#ifndef WORDS_BIGENDIAN
266
267
typedef struct {
268
    unsigned char rc : 5;      /* reception report count */
269
    unsigned char p : 1;       /* padding flag           */
270
    unsigned char version : 2; /* protocol version       */
271
    unsigned char pt : 8;      /* payload type           */
272
    uint16_t len;              /* length                 */
273
    uint32_t ssrc;             /* synchronization source */
274
} srtcp_hdr_t;
275
276
typedef struct {
277
    unsigned int index : 31; /* srtcp packet index in network order!  */
278
    unsigned int e : 1;      /* encrypted? 1=yes                      */
279
                             /* optional mikey/etc go here            */
280
                             /* and then the variable-length auth tag */
281
} srtcp_trailer_t;
282
283
#else /*  BIG_ENDIAN */
284
285
typedef struct {
286
    unsigned char version : 2; /* protocol version       */
287
    unsigned char p : 1;       /* padding flag           */
288
    unsigned char rc : 5;      /* reception report count */
289
    unsigned char pt : 8;      /* payload type           */
290
    uint16_t len;              /* length                 */
291
    uint32_t ssrc;             /* synchronization source */
292
} srtcp_hdr_t;
293
294
typedef struct {
295
    unsigned int e : 1;      /* encrypted? 1=yes                      */
296
    unsigned int index : 31; /* srtcp packet index                    */
297
                             /* optional mikey/etc go here            */
298
                             /* and then the variable-length auth tag */
299
} srtcp_trailer_t;
300
301
#endif
302
303
/*
304
 * srtp_handle_event(srtp, srtm, evnt) calls the event handling
305
 * function, if there is one.
306
 *
307
 * This macro is not included in the documentation as it is
308
 * an internal-only function.
309
 */
310
311
#define srtp_handle_event(srtp, strm, evnt)                                    \
312
325
    if (srtp_event_handler) {                                                  \
313
325
        srtp_event_data_t data;                                                \
314
325
        data.session = srtp;                                                   \
315
325
        data.ssrc = ntohl(strm->ssrc);                                         \
316
325
        data.event = evnt;                                                     \
317
325
        srtp_event_handler(&data);                                             \
318
325
    }
319
320
#ifdef __cplusplus
321
}
322
#endif
323
324
#endif /* SRTP_PRIV_H */