Skip to content

Commit 6e23ad8

Browse files
authored
Merge pull request #790 from pabuhler/policy-api
Introduce the new opaque srtp_policy_t API and move policy construction, validation, and crypto policy handling behind the policy API. This replaces direct use of legacy policy structs across SRTP, tests, tools, and fuzzing code; adds policy create/update/inspect/destroy helpers; updates encrypted header extension and cryptex policy handling; and validates profile master key/salt lengths plus invalid no-key null/null policy combinations.
2 parents 2499c94 + e2dd96f commit 6e23ad8

22 files changed

Lines changed: 3562 additions & 2650 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ test/rtp_decoder
4646
test/rtpw
4747
test/srtp_driver
4848
test/test_srtp
49+
test/test_srtp_policy

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ endif()
143143

144144
set(SOURCES_C
145145
srtp/srtp.c
146+
srtp/srtp_policy.c
146147
)
147148

148149
set(CIPHERS_SOURCES_C
@@ -471,6 +472,17 @@ if(LIBSRTP_TEST_APPS)
471472
endif()
472473
target_link_libraries(test_srtp srtp3)
473474
add_test(test_srtp test_srtp)
475+
476+
add_executable(test_srtp_policy test/test_srtp_policy.c test/util.c)
477+
target_set_warnings(
478+
TARGET
479+
test_srtp_policy
480+
ENABLE
481+
${ENABLE_WARNINGS}
482+
AS_ERRORS
483+
${ENABLE_WARNINGS_AS_ERRORS})
484+
target_link_libraries(test_srtp_policy srtp3)
485+
add_test(test_srtp_policy test_srtp_policy)
474486
endif()
475487

476488
find_program(BASH_PROGRAM bash)

Makefile.in

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ runtest: test
4747
$(FIND_LIBRARIES) crypto/test/cipher_driver$(EXE) -v >/dev/null
4848
$(FIND_LIBRARIES) crypto/test/kernel_driver$(EXE) -v >/dev/null
4949
$(FIND_LIBRARIES) test/test_srtp$(EXE) >/dev/null
50+
$(FIND_LIBRARIES) test/test_srtp_policy$(EXE) >/dev/null
5051
$(FIND_LIBRARIES) test/rdbx_driver$(EXE) -v >/dev/null
5152
$(FIND_LIBRARIES) test/srtp_driver$(EXE) -v >/dev/null
5253
$(FIND_LIBRARIES) test/roc_driver$(EXE) -v >/dev/null
@@ -61,6 +62,7 @@ endif
6162
runtest-valgrind: test
6263
@echo "running libsrtp3 test applications... (valgrind)"
6364
valgrind --error-exitcode=1 --leak-check=full --suppressions=./valgrind.supp test/test_srtp$(EXE) -v >/dev/null
65+
valgrind --error-exitcode=1 --leak-check=full --suppressions=./valgrind.supp test/test_srtp_policy$(EXE) -v >/dev/null
6466
valgrind --error-exitcode=1 --leak-check=full --suppressions=./valgrind.supp test/srtp_driver$(EXE) -v >/dev/null
6567
@echo "libsrtp3 test applications passed. (valgrind)"
6668

@@ -156,7 +158,7 @@ cryptobj = $(ciphers) $(hashes) $(math) $(kernel) $(replay)
156158

157159
# libsrtp3.a (implements srtp processing)
158160

159-
srtpobj = srtp/srtp.o
161+
srtpobj = srtp/srtp.o srtp/srtp_policy.o
160162

161163
libsrtp3.a: $(srtpobj) $(cryptobj) $(gdoi)
162164
$(AR) cr libsrtp3.a $^
@@ -187,7 +189,7 @@ crypto_testapp = $(AES_CALC) crypto/test/cipher_driver$(EXE) \
187189

188190
testapp = $(crypto_testapp) test/srtp_driver$(EXE) test/replay_driver$(EXE) \
189191
test/roc_driver$(EXE) test/rdbx_driver$(EXE) test/rtpw$(EXE) \
190-
test/test_srtp$(EXE)
192+
test/test_srtp$(EXE) test/test_srtp_policy$(EXE)
191193

192194
ifeq (1, $(HAVE_PCAP))
193195
testapp += test/rtp_decoder$(EXE)
@@ -211,6 +213,9 @@ crypto/test/aes_calc$(EXE): crypto/test/aes_calc.c test/util.c
211213
test/test_srtp$(EXE): test/test_srtp.c
212214
$(COMPILE) -I$(srcdir)/test $(LDFLAGS) -o $@ $^ $(LIBS) $(SRTPLIB)
213215

216+
test/test_srtp_policy$(EXE): test/test_srtp_policy.c test/util.c
217+
$(COMPILE) -I$(srcdir)/test $(LDFLAGS) -o $@ $^ $(LIBS) $(SRTPLIB)
218+
214219
crypto/test/datatypes_driver$(EXE): crypto/test/datatypes_driver.c test/util.c
215220
$(COMPILE) -I$(srcdir)/test $(LDFLAGS) -o $@ $^ $(LIBS) $(SRTPLIB)
216221

README.md

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,14 @@ receives from each sender.
136136

137137
In libSRTP, a session is created using the function `srtp_create()`.
138138
The policy to be implemented in the session is passed into this
139-
function as an `srtp_policy_t` structure. A single one of these
140-
structures describes the policy of a single stream. These structures
141-
can also be linked together to form an entire session policy. A linked
142-
list of `srtp_policy_t` structures is equivalent to a session policy.
143-
In such a policy, we refer to a single `srtp_policy_t` as an *element*.
144-
145-
An `srtp_policy_t` structure contains two `srtp_crypto_policy_t` structures
146-
that describe the cryptograhic policies for RTP and RTCP, as well as
147-
the SRTP master key and the SSRC value. The SSRC describes what to
148-
protect (e.g. which stream), and the `srtp_crypto_policy_t` structures
149-
describe how to protect it. The key is contained in a policy element
150-
because it simplifies the interface to the library. In many cases, it
151-
is desirable to use the same cryptographic policies across all of the
152-
streams in a session, but to use a distinct key for each stream. A
153-
`srtp_crypto_policy_t` structure can be initialized by using either the
154-
`srtp_crypto_policy_set_rtp_default()` or `srtp_crypto_policy_set_rtcp_default()`
155-
functions, which set a crypto policy structure to the default policies
156-
for RTP and RTCP protection, respectively.
139+
function as an opaque `srtp_policy_t` handle. A single policy handle
140+
describes one stream policy. To configure multiple streams, create a
141+
session and add additional policies with `srtp_stream_add()`.
142+
143+
A policy handle is configured with `srtp_policy_set_*` functions. At a
144+
minimum, this includes SSRC selection, profile selection, and key/salt
145+
material. The profile configures RTP/RTCP crypto policy settings, while the
146+
SSRC selector identifies how and where that policy is applied.
157147

158148
--------------------------------------------------------------------------------
159149

@@ -181,7 +171,7 @@ traffic from a particular source a *stream*. Each stream has its own
181171
SSRC, sequence number, rollover counter, and other data. A particular
182172
choice of options, cryptographic mechanisms, and keys is called a
183173
*policy*. Each stream within a session can have a distinct policy
184-
applied to it. A session policy is a collection of stream policies.
174+
applied to it.
185175

186176
A single policy can be used for all of the streams in a given session,
187177
though the case in which a single *key* is shared across multiple
@@ -202,7 +192,7 @@ in which a key is used for both inbound and outbound data.
202192
This library supports all of the mandatory-to-implement features of
203193
SRTP (as defined in [RFC 3711](https://tools.ietf.org/html/rfc3711)). Some of these
204194
features can be selected (or de-selected) at run time by setting an
205-
appropriate policy; this is done using the structure `srtp_policy_t`.
195+
appropriate policy using an `srtp_policy_t` handle.
206196
Some other behaviors of the protocol can be adapted by defining an
207197
approriate event handler for the exceptional events; see the SRTPevents
208198
section in the generated documentation.
@@ -467,11 +457,9 @@ set master key/salt to C1EEC3717DA76195BB878578790AF71C/4EE9F859E197A414A78D5ABC
467457
<a name="example-code"></a>
468458
## Example Code
469459

470-
This section provides a simple example of how to use libSRTP. The
471-
example code lacks error checking, but is functional. Here we assume
472-
that the value ssrc is already set to describe the SSRC of the stream
473-
that we are sending, and that the functions `get_rtp_packet()` and
474-
`send_srtp_packet()` are available to us. The former puts an RTP packet
460+
This section provides a simple example of how to use libSRTP. Here we assume
461+
that the functions `get_rtp_packet()` and `send_srtp_packet()` are available
462+
to us. The former puts an RTP packet
475463
into the buffer and returns the number of octets written to that
476464
buffer. The latter sends the RTP packet in the buffer, given the
477465
length as its second argument.
@@ -480,39 +468,41 @@ length as its second argument.
480468
srtp_t session;
481469
srtp_policy_t policy;
482470

483-
// Set key to predetermined value
484-
uint8_t key[30] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
485-
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
486-
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
487-
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D};
471+
// Set key/salt to predetermined values.
472+
uint8_t master_key[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
473+
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
474+
uint8_t master_salt[14] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
475+
0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D};
488476

489-
// initialize libSRTP
477+
// Initialize libSRTP.
490478
srtp_init();
491479

492-
// default policy values
493-
memset(&policy, 0x0, sizeof(srtp_policy_t));
480+
// Create and configure an opaque policy handle.
481+
srtp_policy_create(&policy);
482+
srtp_policy_set_ssrc(policy, (srtp_ssrc_t){ssrc_any_outbound, 0});
483+
srtp_policy_set_profile(policy, srtp_profile_aes128_cm_sha1_80);
484+
srtp_policy_add_key(policy, master_key, sizeof(master_key),
485+
master_salt, sizeof(master_salt), NULL, 0);
494486

495-
// set policy to describe a policy for an SRTP stream
496-
srtp_crypto_policy_set_rtp_default(&policy.rtp);
497-
srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
498-
policy.ssrc = ssrc;
499-
policy.key = key;
500-
policy.next = NULL;
487+
// Allocate and initialize the SRTP session.
488+
srtp_create(&session, policy);
501489

502-
// allocate and initialize the SRTP session
503-
srtp_create(&session, &policy);
490+
srtp_policy_destroy(policy);
504491

505-
// main loop: get rtp packets, send srtp packets
492+
// Main loop: get RTP packets, send SRTP packets.
506493
while (1) {
507494
char rtp_buffer[2048];
508495
size_t rtp_len;
509496
char srtp_buffer[2048];
510497
size_t srtp_len = sizeof(srtp_buffer);
511498

512-
len = get_rtp_packet(rtp_buffer);
499+
rtp_len = get_rtp_packet(rtp_buffer);
513500
srtp_protect(session, rtp_buffer, rtp_len, srtp_buffer, &srtp_len);
514501
send_srtp_packet(srtp_buffer, srtp_len);
515502
}
503+
504+
srtp_dealloc(session);
505+
srtp_shutdown();
516506
~~~
517507
518508
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)