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 
48 #include "srtp.h"
49 #include "rdbx.h"
50 #include "rdb.h"
51 #include "integers.h"
52 
53 /*
54  * an srtp_hdr_t represents the srtp header
55  *
56  * in this implementation, an srtp_hdr_t is assumed to be 32-bit aligned
57  *
58  * (note that this definition follows that of RFC 1889 Appendix A, but
59  * is not identical)
60  */
61 
62 #ifndef WORDS_BIGENDIAN
63 
64 /*
65  * srtp_hdr_t represents an RTP or SRTP header.  The bit-fields in
66  * this structure should be declared "unsigned int" instead of
67  * "unsigned char", but doing so causes the MS compiler to not
68  * fully pack the bit fields.
69  */
70 
71 typedef struct {
72   unsigned char cc:4;	/* CSRC count             */
73   unsigned char x:1;	/* header extension flag  */
74   unsigned char p:1;	/* padding flag           */
75   unsigned char version:2; /* protocol version    */
76   unsigned char pt:7;	/* payload type           */
77   unsigned char m:1;	/* marker bit             */
78   uint16_t seq;		/* sequence number        */
79   uint32_t ts;		/* timestamp              */
80   uint32_t ssrc;	/* synchronization source */
81 } srtp_hdr_t;
82 
83 #else /*  BIG_ENDIAN */
84 
85 typedef struct {
86   unsigned char version:2; /* protocol version    */
87   unsigned char p:1;	/* padding flag           */
88   unsigned char x:1;	/* header extension flag  */
89   unsigned char cc:4;	/* CSRC count             */
90   unsigned char m:1;	/* marker bit             */
91   unsigned pt:7;	/* payload type           */
92   uint16_t seq;		/* sequence number        */
93   uint32_t ts;		/* timestamp              */
94   uint32_t ssrc;	/* synchronization source */
95 } srtp_hdr_t;
96 
97 #endif
98 
99 typedef struct {
100   uint16_t profile_specific;    /* profile-specific info               */
101   uint16_t length;              /* number of 32-bit words in extension */
102 } srtp_hdr_xtnd_t;
103 
104 
105 /*
106  * srtcp_hdr_t represents a secure rtcp header
107  *
108  * in this implementation, an srtcp header is assumed to be 32-bit
109  * alinged
110  */
111 
112 #ifndef WORDS_BIGENDIAN
113 
114 typedef struct {
115   unsigned char rc:5;		/* reception report count */
116   unsigned char p:1;		/* padding flag           */
117   unsigned char version:2;	/* protocol version       */
118   unsigned char pt:8;		/* payload type           */
119   uint16_t len;			/* length                 */
120   uint32_t ssrc;	       	/* synchronization source */
121 } srtcp_hdr_t;
122 
123 typedef struct {
124   unsigned int index:31;    /* srtcp packet index in network order! */
125   unsigned int e:1;         /* encrypted? 1=yes */
126   /* optional mikey/etc go here */
127   /* and then the variable-length auth tag */
128 } srtcp_trailer_t;
129 
130 
131 #else /*  BIG_ENDIAN */
132 
133 typedef struct {
134   unsigned char version:2;	/* protocol version       */
135   unsigned char p:1;		/* padding flag           */
136   unsigned char rc:5;		/* reception report count */
137   unsigned char pt:8;		/* payload type           */
138   uint16_t len;			/* length                 */
139   uint32_t ssrc;	       	/* synchronization source */
140 } srtcp_hdr_t;
141 
142 typedef struct {
143   unsigned int version:2;  /* protocol version                     */
144   unsigned int p:1;        /* padding flag                         */
145   unsigned int count:5;    /* varies by packet type                */
146   unsigned int pt:8;       /* payload type                         */
147   uint16_t length;         /* len of uint32s of packet less header */
148 } rtcp_common_t;
149 
150 typedef struct {
151   unsigned int e:1;         /* encrypted? 1=yes */
152   unsigned int index:31;    /* srtcp packet index */
153   /* optional mikey/etc go here */
154   /* and then the variable-length auth tag */
155 } srtcp_trailer_t;
156 
157 #endif
158 
159 
160 /*
161  * the following declarations are libSRTP internal functions
162  */
163 
164 /*
165  * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
166  * to ssrc, or NULL if no stream exists for that ssrc
167  */
168 
169 srtp_stream_t
170 srtp_get_stream(srtp_t srtp, uint32_t ssrc);
171 
172 
173 /*
174  * srtp_stream_init_keys(s, k) (re)initializes the srtp_stream_t s by
175  * deriving all of the needed keys using the KDF and the key k.
176  */
177 
178 
179 err_status_t
180 srtp_stream_init_keys(srtp_stream_t srtp, const void *key);
181 
182 /*
183  * srtp_stream_init(s, p) initializes the srtp_stream_t s to
184  * use the policy at the location p
185  */
186 err_status_t
187 srtp_stream_init(srtp_stream_t srtp,
188 		 const srtp_policy_t *p);
189 
190 
191 /*
192  * Uninitializes and Deallocates a stream.
193  */
194 err_status_t
195 srtp_stream_uninit_and_dealloc(srtp_stream_t stream,
196                                srtp_stream_t stream_template);
197 
198 /*
199  * libsrtp internal datatypes
200  */
201 
202 typedef enum direction_t {
203   dir_unknown       = 0,
204   dir_srtp_sender   = 1,
205   dir_srtp_receiver = 2
206 } direction_t;
207 
208 /*
209  * an srtp_stream_t has its own SSRC, encryption key, authentication
210  * key, sequence number, and replay database
211  *
212  * note that the keys might not actually be unique, in which case the
213  * cipher_t and auth_t pointers will point to the same structures
214  */
215 
216 typedef struct srtp_stream_ctx_t {
217   uint32_t   ssrc;
218   cipher_t  *rtp_cipher;
219   auth_t    *rtp_auth;
220   rdbx_t     rtp_rdbx;
221   sec_serv_t rtp_services;
222   cipher_t  *rtcp_cipher;
223   auth_t    *rtcp_auth;
224   rdb_t      rtcp_rdb;
225   sec_serv_t rtcp_services;
226   key_limit_ctx_t *limit;
227   direction_t direction;
228   int        allow_repeat_tx;
229   ekt_stream_t ekt;
230   struct srtp_stream_ctx_t *next;   /* linked list of streams */
231 } srtp_stream_ctx_t;
232 
233 
234 /*
235  * an srtp_ctx_t holds a stream list and a service description
236  */
237 
238 typedef struct srtp_ctx_t {
239   srtp_stream_ctx_t *stream_list;     /* linked list of streams            */
240   srtp_stream_ctx_t *stream_template; /* act as template for other streams */
241 } srtp_ctx_t;
242 
243 
244 
245 /*
246  * srtp_handle_event(srtp, srtm, evnt) calls the event handling
247  * function, if there is one.
248  *
249  * This macro is not included in the documentation as it is
250  * an internal-only function.
251  */
252 
253 #define srtp_handle_event(srtp, strm, evnt)         \
254    if(srtp_event_handler) {                         \
255       srtp_event_data_t data;                       \
256       data.session = srtp;                          \
257       data.stream  = strm;                          \
258       data.event   = evnt;                          \
259       srtp_event_handler(&data);                    \
260 }
261 
262 
263 #endif /* SRTP_PRIV_H */
264