David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 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-2006 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 | |
Teerapap Changwichukarn | 6cffe24 | 2014-09-24 11:24:07 +0800 | [diff] [blame] | 48 | #include "config.h" |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 49 | #include "srtp.h" |
| 50 | #include "rdbx.h" |
| 51 | #include "rdb.h" |
| 52 | #include "integers.h" |
jfigus | 4494760 | 2014-10-08 13:08:52 -0400 | [diff] [blame] | 53 | #include "cipher.h" |
| 54 | #include "auth.h" |
| 55 | #include "aes.h" |
| 56 | #include "key.h" |
| 57 | #include "crypto_kernel.h" |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 58 | |
David Benjamin | 5f1b982 | 2016-07-26 18:18:01 -0400 | [diff] [blame] | 59 | #ifdef __cplusplus |
| 60 | extern "C" { |
| 61 | #endif |
| 62 | |
jfigus | f62b64d | 2014-10-08 13:53:57 -0400 | [diff] [blame] | 63 | #define SRTP_VER_STRING PACKAGE_STRING |
| 64 | #define SRTP_VERSION PACKAGE_VERSION |
| 65 | |
Pascal Bühler | f15c7e0 | 2017-01-25 13:15:21 +0100 | [diff] [blame] | 66 | typedef struct srtp_stream_ctx_t_ srtp_stream_ctx_t; |
| 67 | typedef srtp_stream_ctx_t *srtp_stream_t; |
| 68 | |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 69 | /* |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 70 | * the following declarations are libSRTP internal functions |
| 71 | */ |
| 72 | |
| 73 | /* |
| 74 | * srtp_get_stream(ssrc) returns a pointer to the stream corresponding |
| 75 | * to ssrc, or NULL if no stream exists for that ssrc |
| 76 | */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 77 | srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc); |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | /* |
| 81 | * srtp_stream_init_keys(s, k) (re)initializes the srtp_stream_t s by |
| 82 | * deriving all of the needed keys using the KDF and the key k. |
| 83 | */ |
Geir Istad | 626e9e8 | 2017-02-20 18:15:05 +0100 | [diff] [blame] | 84 | srtp_err_status_t srtp_stream_init_keys(srtp_stream_ctx_t *srtp, |
| 85 | srtp_master_key_t *master_key, |
| 86 | const unsigned int current_mki_index); |
Ryan Hooper | fe5d8b8 | 2016-12-15 14:53:58 -0500 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * srtp_stream_init_all_master_keys(s, k, m) (re)initializes the srtp_stream_t s by |
| 90 | * deriving all of the needed keys for all the master keys using the KDF and the keys from k. |
| 91 | */ |
Ryan Hooper | 89a288b | 2016-12-22 16:22:47 -0500 | [diff] [blame] | 92 | srtp_err_status_t srtp_steam_init_all_master_keys(srtp_stream_ctx_t *srtp, |
Geir Istad | 626e9e8 | 2017-02-20 18:15:05 +0100 | [diff] [blame] | 93 | unsigned char *key, |
| 94 | srtp_master_key_t **keys, |
Ryan Hooper | fe5d8b8 | 2016-12-15 14:53:58 -0500 | [diff] [blame] | 95 | const unsigned int max_master_keys); |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 96 | |
| 97 | /* |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 98 | * srtp_stream_init(s, p) initializes the srtp_stream_t s to |
| 99 | * use the policy at the location p |
| 100 | */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 101 | srtp_err_status_t srtp_stream_init(srtp_stream_t srtp, const srtp_policy_t *p); |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | /* |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 105 | * libsrtp internal datatypes |
| 106 | */ |
| 107 | |
| 108 | typedef enum direction_t { |
| 109 | dir_unknown = 0, |
| 110 | dir_srtp_sender = 1, |
| 111 | dir_srtp_receiver = 2 |
| 112 | } direction_t; |
| 113 | |
Ryan Hooper | fe5d8b8 | 2016-12-15 14:53:58 -0500 | [diff] [blame] | 114 | /* |
| 115 | * srtp_session_keys_t will contain the encryption, hmac, salt keys |
| 116 | * for both SRTP and SRTCP. The session keys will also contain the |
| 117 | * MKI ID which is used to identify the session keys. |
| 118 | */ |
| 119 | typedef struct srtp_session_keys_t { |
| 120 | srtp_cipher_t *rtp_cipher; |
| 121 | srtp_cipher_t *rtp_xtn_hdr_cipher; |
| 122 | srtp_auth_t *rtp_auth; |
| 123 | srtp_cipher_t *rtcp_cipher; |
| 124 | srtp_auth_t *rtcp_auth; |
| 125 | uint8_t salt[SRTP_AEAD_SALT_LEN]; |
| 126 | uint8_t c_salt[SRTP_AEAD_SALT_LEN]; |
| 127 | uint8_t *mki_id; |
| 128 | unsigned int mki_size; |
Ryan Hooper | f38d893 | 2017-01-31 16:11:54 -0500 | [diff] [blame] | 129 | srtp_key_limit_ctx_t *limit; |
Ryan Hooper | fe5d8b8 | 2016-12-15 14:53:58 -0500 | [diff] [blame] | 130 | } srtp_session_keys_t; |
| 131 | |
| 132 | |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 133 | /* |
| 134 | * an srtp_stream_t has its own SSRC, encryption key, authentication |
| 135 | * key, sequence number, and replay database |
| 136 | * |
| 137 | * note that the keys might not actually be unique, in which case the |
jfigus | 9a84043 | 2014-11-19 15:48:21 -0500 | [diff] [blame] | 138 | * srtp_cipher_t and srtp_auth_t pointers will point to the same structures |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 139 | */ |
| 140 | |
jfigus | a9ac898 | 2014-10-31 14:49:31 -0400 | [diff] [blame] | 141 | typedef struct srtp_stream_ctx_t_ { |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 142 | uint32_t ssrc; |
Ryan Hooper | 89a288b | 2016-12-22 16:22:47 -0500 | [diff] [blame] | 143 | srtp_session_keys_t *session_keys; |
Ryan Hooper | fe5d8b8 | 2016-12-15 14:53:58 -0500 | [diff] [blame] | 144 | unsigned int num_master_keys; |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 145 | srtp_rdbx_t rtp_rdbx; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 146 | srtp_sec_serv_t rtp_services; |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 147 | srtp_rdb_t rtcp_rdb; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 148 | srtp_sec_serv_t rtcp_services; |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 149 | direction_t direction; |
Jonathan Lennox | dcee5c6 | 2010-05-17 22:08:40 +0000 | [diff] [blame] | 150 | int allow_repeat_tx; |
jfigus | c5887e7 | 2014-11-06 09:46:18 -0500 | [diff] [blame] | 151 | srtp_ekt_stream_t ekt; |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 152 | int *enc_xtn_hdr; |
| 153 | int enc_xtn_hdr_count; |
jfigus | a9ac898 | 2014-10-31 14:49:31 -0400 | [diff] [blame] | 154 | struct srtp_stream_ctx_t_ *next; /* linked list of streams */ |
| 155 | } strp_stream_ctx_t_; |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | /* |
| 159 | * an srtp_ctx_t holds a stream list and a service description |
| 160 | */ |
| 161 | |
jfigus | a9ac898 | 2014-10-31 14:49:31 -0400 | [diff] [blame] | 162 | typedef struct srtp_ctx_t_ { |
| 163 | struct srtp_stream_ctx_t_ *stream_list; /* linked list of streams */ |
| 164 | struct srtp_stream_ctx_t_ *stream_template; /* act as template for other streams */ |
Iñaki Baz Castillo | 241fec3 | 2014-08-21 00:51:00 +0200 | [diff] [blame] | 165 | void *user_data; /* user custom data */ |
jfigus | a9ac898 | 2014-10-31 14:49:31 -0400 | [diff] [blame] | 166 | } srtp_ctx_t_; |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 167 | |
| 168 | |
| 169 | |
| 170 | /* |
| 171 | * srtp_handle_event(srtp, srtm, evnt) calls the event handling |
| 172 | * function, if there is one. |
| 173 | * |
| 174 | * This macro is not included in the documentation as it is |
| 175 | * an internal-only function. |
| 176 | */ |
| 177 | |
| 178 | #define srtp_handle_event(srtp, strm, evnt) \ |
| 179 | if(srtp_event_handler) { \ |
| 180 | srtp_event_data_t data; \ |
| 181 | data.session = srtp; \ |
Pascal Bühler | f15c7e0 | 2017-01-25 13:15:21 +0100 | [diff] [blame] | 182 | data.ssrc = ntohl(strm->ssrc); \ |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 183 | data.event = evnt; \ |
| 184 | srtp_event_handler(&data); \ |
| 185 | } |
| 186 | |
David Benjamin | 5f1b982 | 2016-07-26 18:18:01 -0400 | [diff] [blame] | 187 | #ifdef __cplusplus |
| 188 | } |
| 189 | #endif |
David McGrew | 3c45e0c | 2006-07-12 00:50:56 +0000 | [diff] [blame] | 190 | |
| 191 | #endif /* SRTP_PRIV_H */ |