netwerk/srtp/src/include/srtp_priv.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * srtp_priv.h
michael@0 3 *
michael@0 4 * private internal data structures and functions for libSRTP
michael@0 5 *
michael@0 6 * David A. McGrew
michael@0 7 * Cisco Systems, Inc.
michael@0 8 */
michael@0 9 /*
michael@0 10 *
michael@0 11 * Copyright (c) 2001-2006 Cisco Systems, Inc.
michael@0 12 * All rights reserved.
michael@0 13 *
michael@0 14 * Redistribution and use in source and binary forms, with or without
michael@0 15 * modification, are permitted provided that the following conditions
michael@0 16 * are met:
michael@0 17 *
michael@0 18 * Redistributions of source code must retain the above copyright
michael@0 19 * notice, this list of conditions and the following disclaimer.
michael@0 20 *
michael@0 21 * Redistributions in binary form must reproduce the above
michael@0 22 * copyright notice, this list of conditions and the following
michael@0 23 * disclaimer in the documentation and/or other materials provided
michael@0 24 * with the distribution.
michael@0 25 *
michael@0 26 * Neither the name of the Cisco Systems, Inc. nor the names of its
michael@0 27 * contributors may be used to endorse or promote products derived
michael@0 28 * from this software without specific prior written permission.
michael@0 29 *
michael@0 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
michael@0 33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
michael@0 34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
michael@0 35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
michael@0 36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
michael@0 37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
michael@0 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
michael@0 41 * OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 42 *
michael@0 43 */
michael@0 44
michael@0 45 #ifndef SRTP_PRIV_H
michael@0 46 #define SRTP_PRIV_H
michael@0 47
michael@0 48 #include "srtp.h"
michael@0 49 #include "rdbx.h"
michael@0 50 #include "rdb.h"
michael@0 51 #include "integers.h"
michael@0 52
michael@0 53 /*
michael@0 54 * an srtp_hdr_t represents the srtp header
michael@0 55 *
michael@0 56 * in this implementation, an srtp_hdr_t is assumed to be 32-bit aligned
michael@0 57 *
michael@0 58 * (note that this definition follows that of RFC 1889 Appendix A, but
michael@0 59 * is not identical)
michael@0 60 */
michael@0 61
michael@0 62 #ifndef WORDS_BIGENDIAN
michael@0 63
michael@0 64 /*
michael@0 65 * srtp_hdr_t represents an RTP or SRTP header. The bit-fields in
michael@0 66 * this structure should be declared "unsigned int" instead of
michael@0 67 * "unsigned char", but doing so causes the MS compiler to not
michael@0 68 * fully pack the bit fields.
michael@0 69 */
michael@0 70
michael@0 71 typedef struct {
michael@0 72 unsigned char cc:4; /* CSRC count */
michael@0 73 unsigned char x:1; /* header extension flag */
michael@0 74 unsigned char p:1; /* padding flag */
michael@0 75 unsigned char version:2; /* protocol version */
michael@0 76 unsigned char pt:7; /* payload type */
michael@0 77 unsigned char m:1; /* marker bit */
michael@0 78 uint16_t seq; /* sequence number */
michael@0 79 uint32_t ts; /* timestamp */
michael@0 80 uint32_t ssrc; /* synchronization source */
michael@0 81 } srtp_hdr_t;
michael@0 82
michael@0 83 #else /* BIG_ENDIAN */
michael@0 84
michael@0 85 typedef struct {
michael@0 86 unsigned char version:2; /* protocol version */
michael@0 87 unsigned char p:1; /* padding flag */
michael@0 88 unsigned char x:1; /* header extension flag */
michael@0 89 unsigned char cc:4; /* CSRC count */
michael@0 90 unsigned char m:1; /* marker bit */
michael@0 91 unsigned pt:7; /* payload type */
michael@0 92 uint16_t seq; /* sequence number */
michael@0 93 uint32_t ts; /* timestamp */
michael@0 94 uint32_t ssrc; /* synchronization source */
michael@0 95 } srtp_hdr_t;
michael@0 96
michael@0 97 #endif
michael@0 98
michael@0 99 typedef struct {
michael@0 100 uint16_t profile_specific; /* profile-specific info */
michael@0 101 uint16_t length; /* number of 32-bit words in extension */
michael@0 102 } srtp_hdr_xtnd_t;
michael@0 103
michael@0 104
michael@0 105 /*
michael@0 106 * srtcp_hdr_t represents a secure rtcp header
michael@0 107 *
michael@0 108 * in this implementation, an srtcp header is assumed to be 32-bit
michael@0 109 * alinged
michael@0 110 */
michael@0 111
michael@0 112 #ifndef WORDS_BIGENDIAN
michael@0 113
michael@0 114 typedef struct {
michael@0 115 unsigned char rc:5; /* reception report count */
michael@0 116 unsigned char p:1; /* padding flag */
michael@0 117 unsigned char version:2; /* protocol version */
michael@0 118 unsigned char pt:8; /* payload type */
michael@0 119 uint16_t len; /* length */
michael@0 120 uint32_t ssrc; /* synchronization source */
michael@0 121 } srtcp_hdr_t;
michael@0 122
michael@0 123 typedef struct {
michael@0 124 unsigned int index:31; /* srtcp packet index in network order! */
michael@0 125 unsigned int e:1; /* encrypted? 1=yes */
michael@0 126 /* optional mikey/etc go here */
michael@0 127 /* and then the variable-length auth tag */
michael@0 128 } srtcp_trailer_t;
michael@0 129
michael@0 130
michael@0 131 #else /* BIG_ENDIAN */
michael@0 132
michael@0 133 typedef struct {
michael@0 134 unsigned char version:2; /* protocol version */
michael@0 135 unsigned char p:1; /* padding flag */
michael@0 136 unsigned char rc:5; /* reception report count */
michael@0 137 unsigned char pt:8; /* payload type */
michael@0 138 uint16_t len; /* length */
michael@0 139 uint32_t ssrc; /* synchronization source */
michael@0 140 } srtcp_hdr_t;
michael@0 141
michael@0 142 typedef struct {
michael@0 143 unsigned int version:2; /* protocol version */
michael@0 144 unsigned int p:1; /* padding flag */
michael@0 145 unsigned int count:5; /* varies by packet type */
michael@0 146 unsigned int pt:8; /* payload type */
michael@0 147 uint16_t length; /* len of uint32s of packet less header */
michael@0 148 } rtcp_common_t;
michael@0 149
michael@0 150 typedef struct {
michael@0 151 unsigned int e:1; /* encrypted? 1=yes */
michael@0 152 unsigned int index:31; /* srtcp packet index */
michael@0 153 /* optional mikey/etc go here */
michael@0 154 /* and then the variable-length auth tag */
michael@0 155 } srtcp_trailer_t;
michael@0 156
michael@0 157 #endif
michael@0 158
michael@0 159
michael@0 160 /*
michael@0 161 * the following declarations are libSRTP internal functions
michael@0 162 */
michael@0 163
michael@0 164 /*
michael@0 165 * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
michael@0 166 * to ssrc, or NULL if no stream exists for that ssrc
michael@0 167 */
michael@0 168
michael@0 169 srtp_stream_t
michael@0 170 srtp_get_stream(srtp_t srtp, uint32_t ssrc);
michael@0 171
michael@0 172
michael@0 173 /*
michael@0 174 * srtp_stream_init_keys(s, k) (re)initializes the srtp_stream_t s by
michael@0 175 * deriving all of the needed keys using the KDF and the key k.
michael@0 176 */
michael@0 177
michael@0 178
michael@0 179 err_status_t
michael@0 180 srtp_stream_init_keys(srtp_stream_t srtp, const void *key);
michael@0 181
michael@0 182 /*
michael@0 183 * srtp_stream_init(s, p) initializes the srtp_stream_t s to
michael@0 184 * use the policy at the location p
michael@0 185 */
michael@0 186 err_status_t
michael@0 187 srtp_stream_init(srtp_stream_t srtp,
michael@0 188 const srtp_policy_t *p);
michael@0 189
michael@0 190
michael@0 191 /*
michael@0 192 * libsrtp internal datatypes
michael@0 193 */
michael@0 194
michael@0 195 typedef enum direction_t {
michael@0 196 dir_unknown = 0,
michael@0 197 dir_srtp_sender = 1,
michael@0 198 dir_srtp_receiver = 2
michael@0 199 } direction_t;
michael@0 200
michael@0 201 /*
michael@0 202 * an srtp_stream_t has its own SSRC, encryption key, authentication
michael@0 203 * key, sequence number, and replay database
michael@0 204 *
michael@0 205 * note that the keys might not actually be unique, in which case the
michael@0 206 * cipher_t and auth_t pointers will point to the same structures
michael@0 207 */
michael@0 208
michael@0 209 typedef struct srtp_stream_ctx_t {
michael@0 210 uint32_t ssrc;
michael@0 211 cipher_t *rtp_cipher;
michael@0 212 auth_t *rtp_auth;
michael@0 213 rdbx_t rtp_rdbx;
michael@0 214 sec_serv_t rtp_services;
michael@0 215 cipher_t *rtcp_cipher;
michael@0 216 auth_t *rtcp_auth;
michael@0 217 rdb_t rtcp_rdb;
michael@0 218 sec_serv_t rtcp_services;
michael@0 219 key_limit_ctx_t *limit;
michael@0 220 direction_t direction;
michael@0 221 int allow_repeat_tx;
michael@0 222 ekt_stream_t ekt;
michael@0 223 struct srtp_stream_ctx_t *next; /* linked list of streams */
michael@0 224 } srtp_stream_ctx_t;
michael@0 225
michael@0 226
michael@0 227 /*
michael@0 228 * an srtp_ctx_t holds a stream list and a service description
michael@0 229 */
michael@0 230
michael@0 231 typedef struct srtp_ctx_t {
michael@0 232 srtp_stream_ctx_t *stream_list; /* linked list of streams */
michael@0 233 srtp_stream_ctx_t *stream_template; /* act as template for other streams */
michael@0 234 } srtp_ctx_t;
michael@0 235
michael@0 236
michael@0 237
michael@0 238 /*
michael@0 239 * srtp_handle_event(srtp, srtm, evnt) calls the event handling
michael@0 240 * function, if there is one.
michael@0 241 *
michael@0 242 * This macro is not included in the documentation as it is
michael@0 243 * an internal-only function.
michael@0 244 */
michael@0 245
michael@0 246 #define srtp_handle_event(srtp, strm, evnt) \
michael@0 247 if(srtp_event_handler) { \
michael@0 248 srtp_event_data_t data; \
michael@0 249 data.session = srtp; \
michael@0 250 data.stream = strm; \
michael@0 251 data.event = evnt; \
michael@0 252 srtp_event_handler(&data); \
michael@0 253 }
michael@0 254
michael@0 255
michael@0 256 #endif /* SRTP_PRIV_H */

mercurial