1 [![CMake Build](https://github.com/cisco/libsrtp/actions/workflows/cmake.yml/badge.svg)](https://github.com/cisco/libsrtp/actions/workflows/cmake.yml)
2 [![Build Status](https://travis-ci.org/cisco/libsrtp.svg?branch=master)](https://travis-ci.org/cisco/libsrtp)
3 [![Coverity Scan Build Status](https://scan.coverity.com/projects/14274/badge.svg)](https://scan.coverity.com/projects/cisco-libsrtp)
4 [![OSS-Fuzz Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/systemd.svg)](https://oss-fuzz-build-logs.storage.googleapis.com/index.html#libsrtp)
6 <a name="introduction-to-libsrtp"></a>
7 # Introduction to libSRTP
9 This package provides an implementation of the Secure Real-time
10 Transport Protocol (SRTP), the Universal Security Transform (UST), and
11 a supporting cryptographic kernel. The SRTP API is documented in include/srtp.h,
12 and the library is in libsrtp2.a (after compilation).
14 This document describes libSRTP, the Open Source Secure RTP library
15 from Cisco Systems, Inc. RTP is the Real-time Transport Protocol, an
16 IETF standard for the transport of real-time data such as telephony,
17 audio, and video, defined by [RFC 3550](https://tools.ietf.org/html/rfc3550).
18 Secure RTP (SRTP) is an RTP profile for providing confidentiality to RTP data
19 and authentication to the RTP header and payload. SRTP is an IETF Standard,
20 defined in [RFC 3711](https://tools.ietf.org/html/rfc3711), and was developed
21 in the IETF Audio/Video Transport (AVT) Working Group. This library supports
22 all of the mandatory features of SRTP, but not all of the optional features. See
23 the [Supported Features](#supported-features) section for more detailed information.
25 This document is also used to generate the documentation files in the /doc/
26 folder where a more detailed reference to the libSRTP API and related functions
27 can be created (requires installing doxygen.). The reference material is created
28 automatically from comments embedded in some of the C header files. The
29 documentation is organized into modules in order to improve its clarity. These
30 modules do not directly correspond to files. An underlying cryptographic kernel
31 provides much of the basic functionality of libSRTP but is mostly undocumented
32 because it does its work behind the scenes.
34 --------------------------------------------------------------------------------
36 <a name="contact-us"></a>
39 - [libsrtp@lists.packetizer.com](mailto:libsrtp@lists.packetizer.com) general mailing list for news / announcements / discussions. This is an open list, see
40 [https://lists.packetizer.com/mailman/listinfo/libsrtp](https://lists.packetizer.com/mailman/listinfo/libsrtp) for singing up.
42 - [libsrtp-security@lists.packetizer.com](mailto:libsrtp-security@lists.packetizer.com) for disclosing security issues to the libsrtp maintenance team. This is a closed list but anyone can send to it.
45 --------------------------------------------------------------------------------
47 <a name="contents"></a>
50 - [Introduction to libSRTP](#introduction-to-libsrtp)
51 - [Contact Us](#contact-us)
52 - [Contents](#contents)
53 - [License and Disclaimer](#license-and-disclaimer)
54 - [libSRTP Overview](#libsrtp-overview)
55 - [Secure RTP Background](#secure-rtp-background)
56 - [Supported Features](#supported-features)
57 - [Implementation Notes](#implementation-notes)
58 - [Installing and Building libSRTP](#installing-and-building-libsrtp)
59 - [Changing Build Configuration](#changing-build-configuration)
60 - [Using Visual Studio](#using-visual-studio)
61 - [Applications](#applications)
62 - [Example Code](#example-code)
64 - [References](#references)
66 --------------------------------------------------------------------------------
68 <a name="license-and-disclaimer"></a>
69 # License and Disclaimer
71 libSRTP is distributed under the following license, which is included
72 in the source code distribution. It is reproduced in the manual in
73 case you got the library from another source.
75 > Copyright (c) 2001-2017 Cisco Systems, Inc. All rights reserved.
77 > Redistribution and use in source and binary forms, with or without
78 > modification, are permitted provided that the following conditions
81 > - Redistributions of source code must retain the above copyright
82 > notice, this list of conditions and the following disclaimer.
83 > - Redistributions in binary form must reproduce the above copyright
84 > notice, this list of conditions and the following disclaimer in
85 > the documentation and/or other materials provided with the distribution.
86 > - Neither the name of the Cisco Systems, Inc. nor the names of its
87 > contributors may be used to endorse or promote products derived
88 > from this software without specific prior written permission.
90 > THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
91 > "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
92 > LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
93 > FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
94 > COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
95 > INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
96 > (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
97 > SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
98 > HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
99 > STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
100 > ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
101 > OF THE POSSIBILITY OF SUCH DAMAGE.
103 --------------------------------------------------------------------------------
105 <a name="libsrtp-overview"></a>
108 libSRTP provides functions for protecting RTP and RTCP. RTP packets
109 can be encrypted and authenticated (using the `srtp_protect()`
110 function), turning them into SRTP packets. Similarly, SRTP packets
111 can be decrypted and have their authentication verified (using the
112 `srtp_unprotect()` function), turning them into RTP packets. Similar
113 functions apply security to RTCP packets.
115 The typedef `srtp_stream_t` points to a structure holding all of the
116 state associated with an SRTP stream, including the keys and
117 parameters for cipher and message authentication functions and the
118 anti-replay data. A particular `srtp_stream_t` holds the information
119 needed to protect a particular RTP and RTCP stream. This datatype
120 is intentionally opaque in order to better seperate the libSRTP
121 API from its implementation.
123 Within an SRTP session, there can be multiple streams, each
124 originating from a particular sender. Each source uses a distinct
125 stream context to protect the RTP and RTCP stream that it is
126 originating. The typedef `srtp_t` points to a structure holding all of
127 the state associated with an SRTP session. There can be multiple
128 stream contexts associated with a single `srtp_t`. A stream context
129 cannot exist indepent from an `srtp_t`, though of course an `srtp_t` can
130 be created that contains only a single stream context. A device
131 participating in an SRTP session must have a stream context for each
132 source in that session, so that it can process the data that it
133 receives from each sender.
135 In libSRTP, a session is created using the function `srtp_create()`.
136 The policy to be implemented in the session is passed into this
137 function as an `srtp_policy_t` structure. A single one of these
138 structures describes the policy of a single stream. These structures
139 can also be linked together to form an entire session policy. A linked
140 list of `srtp_policy_t` structures is equivalent to a session policy.
141 In such a policy, we refer to a single `srtp_policy_t` as an *element*.
143 An `srtp_policy_t` structure contains two `srtp_crypto_policy_t` structures
144 that describe the cryptograhic policies for RTP and RTCP, as well as
145 the SRTP master key and the SSRC value. The SSRC describes what to
146 protect (e.g. which stream), and the `srtp_crypto_policy_t` structures
147 describe how to protect it. The key is contained in a policy element
148 because it simplifies the interface to the library. In many cases, it
149 is desirable to use the same cryptographic policies across all of the
150 streams in a session, but to use a distinct key for each stream. A
151 `srtp_crypto_policy_t` structure can be initialized by using either the
152 `srtp_crypto_policy_set_rtp_default()` or `srtp_crypto_policy_set_rtcp_default()`
153 functions, which set a crypto policy structure to the default policies
154 for RTP and RTCP protection, respectively.
156 --------------------------------------------------------------------------------
158 <a name="secure-rtp-background"></a>
159 ## Secure RTP Background
161 In this section we review SRTP and introduce some terms that are used
162 in libSRTP. An RTP session is defined by a pair of destination
163 transport addresses, that is, a network address plus a pair of UDP
164 ports for RTP and RTCP. RTCP, the RTP control protocol, is used to
165 coordinate between the participants in an RTP session, e.g. to provide
166 feedback from receivers to senders. An *SRTP session* is
167 similarly defined; it is just an RTP session for which the SRTP
168 profile is being used. An SRTP session consists of the traffic sent
169 to the SRTP or SRTCP destination transport addresses. Each
170 participant in a session is identified by a synchronization source
171 (SSRC) identifier. Some participants may not send any SRTP traffic;
172 they are called receivers, even though they send out SRTCP traffic,
173 such as receiver reports.
175 RTP allows multiple sources to send RTP and RTCP traffic during the
176 same session. The synchronization source identifier (SSRC) is used to
177 distinguish these sources. In libSRTP, we call the SRTP and SRTCP
178 traffic from a particular source a *stream*. Each stream has its own
179 SSRC, sequence number, rollover counter, and other data. A particular
180 choice of options, cryptographic mechanisms, and keys is called a
181 *policy*. Each stream within a session can have a distinct policy
182 applied to it. A session policy is a collection of stream policies.
184 A single policy can be used for all of the streams in a given session,
185 though the case in which a single *key* is shared across multiple
186 streams requires care. When key sharing is used, the SSRC values that
187 identify the streams **must** be distinct. This requirement can be
188 enforced by using the convention that each SRTP and SRTCP key is used
189 for encryption by only a single sender. In other words, the key is
190 shared only across streams that originate from a particular device (of
191 course, other SRTP participants will need to use the key for
192 decryption). libSRTP supports this enforcement by detecting the case
193 in which a key is used for both inbound and outbound data.
195 --------------------------------------------------------------------------------
197 <a name="supported-features"></a>
198 ## Supported Features
200 This library supports all of the mandatory-to-implement features of
201 SRTP (as defined in [RFC 3711](https://tools.ietf.org/html/rfc3711)). Some of these
202 features can be selected (or de-selected) at run time by setting an
203 appropriate policy; this is done using the structure `srtp_policy_t`.
204 Some other behaviors of the protocol can be adapted by defining an
205 approriate event handler for the exceptional events; see the SRTPevents
206 section in the generated documentation.
208 Some options that are described in the SRTP specification are not
209 supported. This includes
211 - key derivation rates other than zero,
213 - the use of the packet index to select between master keys.
215 The user should be aware that it is possible to misuse this libary,
216 and that the result may be that the security level it provides is
217 inadequate. If you are implementing a feature using this library, you
218 will want to read the Security Considerations section of [RFC 3711](https://tools.ietf.org/html/rfc3711#section-9).
219 In addition, it is important that you read and understand the
220 terms outlined in the [License and Disclaimer](#license-and-disclaimer) section.
222 --------------------------------------------------------------------------------
224 <a name="implementation-notes"></a>
225 ## Implementation Notes
227 * The `srtp_protect()` function assumes that the buffer holding the
228 rtp packet has enough storage allocated that the authentication
229 tag can be written to the end of that packet. If this assumption
230 is not valid, memory corruption will ensue.
232 * Automated tests for the crypto functions are provided through
233 the `cipher_type_self_test()` and `auth_type_self_test()` functions.
234 These functions should be used to test each port of this code
237 * Replay protection is contained in the crypto engine, and
238 tests for it are provided.
240 * This implementation provides calls to initialize, protect, and
241 unprotect RTP packets, and makes as few as possible assumptions
242 about how these functions will be called. For example, the
243 caller is not expected to provide packets in order (though if
244 they're called more than 65k out of sequence, synchronization
247 * The sequence number in the rtp packet is used as the low 16 bits
248 of the sender's local packet index. Note that RTP will start its
249 sequence number in a random place, and the SRTP layer just jumps
250 forward to that number at its first invocation. An earlier
251 version of this library used initial sequence numbers that are
252 less than 32,768; this trick is no longer required as the
253 `rdbx_estimate_index(...)` function has been made smarter.
255 * The replay window for (S)RTCP is hardcoded to 128 bits in length.
257 --------------------------------------------------------------------------------
259 <a name="installing-and-building-libsrtp"></a>
260 # Installing and Building libSRTP
262 To install libSRTP, download the latest release of the distribution
263 from [https://github.com/cisco/libsrtp/releases](https://github.com/cisco/libsrtp/releases).
264 You probably want to get the most recent release. Unpack the distribution and
265 extract the source files; the directory into which the source files
266 will go is named `libsrtp-A-B-C` where `A` is the version number, `B` is the
267 major release number and `C` is the minor release number.
269 libSRTP uses the GNU `autoconf` and `make` utilities (BSD make will not work; if
270 both versions of make are on your platform, you can invoke GNU make as
271 `gmake`.). In the `libsrtp` directory, run the configure script and then
275 ./configure [ options ]
279 The configure script accepts the following options:
282 -------------------------------|--------------------
283 \-\-help \-h | Display help
284 \-\-enable-debug-logging | Enable debug logging in all modules
285 \-\-enable-log-stdout | Enable logging to stdout
286 \-\-enable-openssl | Enable OpenSSL crypto engine
287 \-\-enable-openssl-kdf | Enable OpenSSL KDF algorithm
288 \-\-with-log-file | Use file for logging
289 \-\-with-openssl-dir | Location of OpenSSL installation
291 By default there is no log output, logging can be enabled to be output to stdout
292 or a given file using the configure options.
294 This package has been tested on the following platforms: Mac OS X
295 (powerpc-apple-darwin1.4), Cygwin (i686-pc-cygwin), Solaris
296 (sparc-sun-solaris2.6), RedHat Linux 7.1 and 9 (i686-pc-linux), and
297 OpenBSD (sparc-unknown-openbsd2.7).
299 --------------------------------------------------------------------------------
301 <a name="changing-build-configuration"></a>
302 ## Changing Build Configuration
304 To build the `./configure` script mentioned above, libSRTP relies on the
305 [automake](https://www.gnu.org/software/automake/) toolchain. Since
306 `./configure` is built from `configure.in` by automake, if you make changes in
307 how `./configure` works (e.g., to add a new library dependency), you will need
308 to rebuild `./configure` and commit the updated version. In addition to
309 automake itself, you will need to have the `pkgconfig` tools installed as well.
311 For example, on macOS:
314 brew install automake pkgconfig
319 --------------------------------------------------------------------------------
320 <a name="using-visual-studio"></a>
321 ## Using Visual Studio
323 On Windows one can use Visual Studio via CMake. CMake can be downloaded here:
324 https://cmake.org/ . To create Visual Studio build files, for example run the
328 # Create build subdirectory
333 cmake .. -G "Visual Studio 15 2017"
335 # Or for 64 bit project files
336 cmake .. -G "Visual Studio 15 2017 Win64"
339 --------------------------------------------------------------------------------
340 <a name="using-meson"></a>
343 On all platforms including Windows, one can build using [Meson](https://mesonbuild.org).
344 Steps to download Meson are here: https://mesonbuild.com/Getting-meson.html
346 To build with Meson, you can do something like:
349 # Setup the build subdirectory
350 meson setup --prefix=/path/to/prefix builddir
353 meson compile -C builddir
356 meson test -C builddir
358 # Optionally, install
359 meson install -C builddir
362 To build with Visual Studio, run the above commands from inside a Visual Studio
363 command prompt, or run `vcvarsall.bat` with the appropriate arguments inside
366 Note that you can also replace the above commands with the appropriate `ninja`
367 targets: `ninja -C build`, `ninja -C build test`, `ninja -C build install`.
369 --------------------------------------------------------------------------------
371 <a name="applications"></a>
374 Several test drivers and a simple and portable srtp application are
375 included in the `test/` subdirectory.
377 Test driver | Function tested
379 kernel_driver | crypto kernel (ciphers, auth funcs, rng)
380 srtp_driver | srtp in-memory tests (does not use the network)
381 rdbx_driver | rdbx (extended replay database)
382 roc_driver | extended sequence number functions
383 replay_driver | replay database
384 cipher_driver | ciphers
385 auth_driver | hash functions
387 The app `rtpw` is a simple rtp application which reads words from
388 `/usr/dict/words` and then sends them out one at a time using [s]rtp.
389 Manual srtp keying uses the -k option; automated key management
390 using gdoi will be added later.
394 rtpw [[-d <debug>]* [-k|b <key> [-a][-e <key size>][-g]] [-s | -r] dest_ip dest_port] | [-l]
397 Either the -s (sender) or -r (receiver) option must be chosen. The
398 values `dest_ip`, `dest_port` are the IP address and UDP port to which
399 the dictionary will be sent, respectively.
405 -s | (S)RTP sender - causes app to send words
406 -r | (S)RTP receive - causes app to receive words
407 -k <key> | use SRTP master key <key>, where the key is a hexadecimal (without the leading "0x")
408 -b <key> | same as -k but with base64 encoded key
409 -e <keysize> | encrypt/decrypt (for data confidentiality) (requires use of -k option as well) (use 128, 192, or 256 for keysize)
410 -g | use AES-GCM mode (must be used with -e)
411 -a | message authentication (requires use of -k option as well)
412 -l | list the available debug modules
413 -d <debug> | turn on debugging for module <debug>
415 In order to get random 30-byte values for use as key/salt pairs , you
416 can use the following bash function to format the output of
417 `/dev/random` (where that device is available).
421 cat /dev/random | od --read-bytes=32 --width=32 -x | awk '{ print $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16 }'
425 An example of an SRTP session using two rtpw programs follows:
428 set k=c1eec3717da76195bb878578790af71c4ee9f859e197a414a78d5abc7451
430 [sh1]$ test/rtpw -s -k $k -e 128 -a 0.0.0.0 9999
431 Security services: confidentiality message authentication
432 set master key/salt to C1EEC3717DA76195BB878578790AF71C/4EE9F859E197A414A78D5ABC7451
433 setting SSRC to 2078917053
440 [sh2]$ test/rtpw -r -k $k -e 128 -a 0.0.0.0 9999
441 security services: confidentiality message authentication
442 set master key/salt to C1EEC3717DA76195BB878578790AF71C/4EE9F859E197A414A78D5ABC7451
443 19 octets received from SSRC 2078917053 word: A
444 19 octets received from SSRC 2078917053 word: a
445 20 octets received from SSRC 2078917053 word: aa
446 21 octets received from SSRC 2078917053 word: aal
450 --------------------------------------------------------------------------------
452 <a name="example-code"></a>
455 This section provides a simple example of how to use libSRTP. The
456 example code lacks error checking, but is functional. Here we assume
457 that the value ssrc is already set to describe the SSRC of the stream
458 that we are sending, and that the functions `get_rtp_packet()` and
459 `send_srtp_packet()` are available to us. The former puts an RTP packet
460 into the buffer and returns the number of octets written to that
461 buffer. The latter sends the RTP packet in the buffer, given the
462 length as its second argument.
466 srtp_policy_t policy;
468 // Set key to predetermined value
469 uint8_t key[30] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
470 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
471 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
472 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D};
474 // initialize libSRTP
477 // default policy values
478 memset(&policy, 0x0, sizeof(srtp_policy_t));
480 // set policy to describe a policy for an SRTP stream
481 srtp_crypto_policy_set_rtp_default(&policy.rtp);
482 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
487 // allocate and initialize the SRTP session
488 srtp_create(&session, &policy);
490 // main loop: get rtp packets, send srtp packets
492 char rtp_buffer[2048];
495 len = get_rtp_packet(rtp_buffer);
496 srtp_protect(session, rtp_buffer, &len);
497 send_srtp_packet(rtp_buffer, len);
501 --------------------------------------------------------------------------------
503 <a name="credits"></a>
506 The original implementation and documentation of libSRTP was written
507 by David McGrew of Cisco Systems, Inc. in order to promote the use,
508 understanding, and interoperability of Secure RTP. Michael Jerris
509 contributed support for building under MSVC. Andris Pavenis
510 contributed many important fixes. Brian West contributed changes to
511 enable dynamic linking. Yves Shumann reported documentation bugs.
512 Randell Jesup contributed a working SRTCP implementation and other
513 fixes. Steve Underwood contributed x86_64 portability changes. We also give
514 thanks to Fredrik Thulin, Brian Weis, Mark Baugher, Jeff Chan, Bill
515 Simon, Douglas Smith, Bill May, Richard Preistley, Joe Tardo and
516 others for contributions, comments, and corrections.
518 This reference material, when applicable, in this documenation was generated
519 using the doxygen utility for automatic documentation of source code.
521 Copyright 2001-2005 by David A. McGrew, Cisco Systems, Inc.
523 --------------------------------------------------------------------------------
525 <a name="references"></a>
528 SRTP and ICM References
531 Secure RTP is defined in [RFC 3711](https://tools.ietf.org/html/rfc3711).
532 The counter mode definition is in [Section 4.1.1](https://tools.ietf.org/html/rfc3711#section-4.1.1).
534 SHA-1 is defined in [FIPS PUB 180-4](http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
536 HMAC is defined in [RFC 2104](https://tools.ietf.org/html/rfc2104)
537 and HMAC-SHA1 test vectors are available
538 in [RFC 2202](https://tools.ietf.org/html/rfc2202#section-3).
540 AES-GCM usage in SRTP is defined in [RFC 7714](https://tools.ietf.org/html/rfc7714)