• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * dtls_srtp_driver.c
3  *
4  * test driver for DTLS-SRTP functions
5  *
6  * David McGrew
7  * Cisco Systems, Inc.
8  */
9 /*
10  *
11  * Copyright (c) 2001-2017 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 #include <stdio.h>    /* for printf()          */
46 #include "getopt_s.h" /* for local getopt()    */
47 #include "srtp_priv.h"
48 
49 srtp_err_status_t test_dtls_srtp(void);
50 
51 srtp_hdr_t *srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc);
52 
usage(char * prog_name)53 void usage(char *prog_name)
54 {
55     printf("usage: %s [ -t ][ -c ][ -v ][-d <debug_module> ]* [ -l ]\n"
56            "  -d <mod>   turn on debugging module <mod>\n"
57            "  -l         list debugging modules\n",
58            prog_name);
59     exit(1);
60 }
61 
main(int argc,char * argv[])62 int main(int argc, char *argv[])
63 {
64     unsigned do_list_mods = 0;
65     int q;
66     srtp_err_status_t err;
67 
68     printf("dtls_srtp_driver\n");
69 
70     /* initialize srtp library */
71     err = srtp_init();
72     if (err) {
73         printf("error: srtp init failed with error code %d\n", err);
74         exit(1);
75     }
76 
77     /* process input arguments */
78     while (1) {
79         q = getopt_s(argc, argv, "ld:");
80         if (q == -1)
81             break;
82         switch (q) {
83         case 'l':
84             do_list_mods = 1;
85             break;
86         case 'd':
87             err = srtp_crypto_kernel_set_debug_module(optarg_s, 1);
88             if (err) {
89                 printf("error: set debug module (%s) failed\n", optarg_s);
90                 exit(1);
91             }
92             break;
93         default:
94             usage(argv[0]);
95         }
96     }
97 
98     if (do_list_mods) {
99         err = srtp_crypto_kernel_list_debug_modules();
100         if (err) {
101             printf("error: list of debug modules failed\n");
102             exit(1);
103         }
104     }
105 
106     printf("testing dtls_srtp...");
107     err = test_dtls_srtp();
108     if (err) {
109         printf("\nerror (code %d)\n", err);
110         exit(1);
111     }
112     printf("passed\n");
113 
114     /* shut down srtp library */
115     err = srtp_shutdown();
116     if (err) {
117         printf("error: srtp shutdown failed with error code %d\n", err);
118         exit(1);
119     }
120 
121     return 0;
122 }
123 
test_dtls_srtp(void)124 srtp_err_status_t test_dtls_srtp(void)
125 {
126     srtp_hdr_t *test_packet;
127     int test_packet_len = 80;
128     srtp_t s;
129     srtp_policy_t policy;
130     uint8_t key[SRTP_MAX_KEY_LEN];
131     uint8_t salt[SRTP_MAX_KEY_LEN];
132     unsigned int key_len, salt_len;
133     srtp_profile_t profile;
134     srtp_err_status_t err;
135 
136     memset(&policy, 0x0, sizeof(srtp_policy_t));
137 
138     /* create a 'null' SRTP session */
139     err = srtp_create(&s, NULL);
140     if (err)
141         return err;
142 
143     /*
144      * verify that packet-processing functions behave properly - we
145      * expect that these functions will return srtp_err_status_no_ctx
146      */
147     test_packet = srtp_create_test_packet(80, 0xa5a5a5a5);
148     if (test_packet == NULL)
149         return srtp_err_status_alloc_fail;
150 
151     err = srtp_protect(s, test_packet, &test_packet_len);
152     if (err != srtp_err_status_no_ctx) {
153         printf("wrong return value from srtp_protect() (got code %d)\n", err);
154         return srtp_err_status_fail;
155     }
156 
157     err = srtp_unprotect(s, test_packet, &test_packet_len);
158     if (err != srtp_err_status_no_ctx) {
159         printf("wrong return value from srtp_unprotect() (got code %d)\n", err);
160         return srtp_err_status_fail;
161     }
162 
163     err = srtp_protect_rtcp(s, test_packet, &test_packet_len);
164     if (err != srtp_err_status_no_ctx) {
165         printf("wrong return value from srtp_protect_rtcp() (got code %d)\n",
166                err);
167         return srtp_err_status_fail;
168     }
169 
170     err = srtp_unprotect_rtcp(s, test_packet, &test_packet_len);
171     if (err != srtp_err_status_no_ctx) {
172         printf("wrong return value from srtp_unprotect_rtcp() (got code %d)\n",
173                err);
174         return srtp_err_status_fail;
175     }
176 
177     /*
178      * set keys to known values for testing
179      */
180     profile = srtp_profile_aes128_cm_sha1_80;
181     key_len = srtp_profile_get_master_key_length(profile);
182     salt_len = srtp_profile_get_master_salt_length(profile);
183     memset(key, 0xff, key_len);
184     memset(salt, 0xee, salt_len);
185     srtp_append_salt_to_key(key, key_len, salt, salt_len);
186     policy.key = key;
187 
188     /* initialize SRTP policy from profile  */
189     err = srtp_crypto_policy_set_from_profile_for_rtp(&policy.rtp, profile);
190     if (err)
191         return err;
192     err = srtp_crypto_policy_set_from_profile_for_rtcp(&policy.rtcp, profile);
193     if (err)
194         return err;
195     policy.ssrc.type = ssrc_any_inbound;
196     policy.ekt = NULL;
197     policy.window_size = 128;
198     policy.allow_repeat_tx = 0;
199     policy.next = NULL;
200 
201     err = srtp_add_stream(s, &policy);
202     if (err)
203         return err;
204 
205     err = srtp_dealloc(s);
206     if (err)
207         return err;
208 
209     free(test_packet);
210 
211     return srtp_err_status_ok;
212 }
213 
214 /*
215  * srtp_create_test_packet(len, ssrc) returns a pointer to a
216  * (malloced) example RTP packet whose data field has the length given
217  * by pkt_octet_len and the SSRC value ssrc.  The total length of the
218  * packet is twelve octets longer, since the header is at the
219  * beginning.  There is room at the end of the packet for a trailer,
220  * and the four octets following the packet are filled with 0xff
221  * values to enable testing for overwrites.
222  *
223  * note that the location of the test packet can (and should) be
224  * deallocated with the free() call once it is no longer needed.
225  */
226 
srtp_create_test_packet(int pkt_octet_len,uint32_t ssrc)227 srtp_hdr_t *srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc)
228 {
229     int i;
230     uint8_t *buffer;
231     srtp_hdr_t *hdr;
232     int bytes_in_hdr = 12;
233 
234     /* allocate memory for test packet */
235     hdr = malloc(pkt_octet_len + bytes_in_hdr + SRTP_MAX_TRAILER_LEN + 4);
236     if (!hdr)
237         return NULL;
238 
239     hdr->version = 2;            /* RTP version two     */
240     hdr->p = 0;                  /* no padding needed   */
241     hdr->x = 0;                  /* no header extension */
242     hdr->cc = 0;                 /* no CSRCs            */
243     hdr->m = 0;                  /* marker bit          */
244     hdr->pt = 0xf;               /* payload type        */
245     hdr->seq = htons(0x1234);    /* sequence number     */
246     hdr->ts = htonl(0xdecafbad); /* timestamp           */
247     hdr->ssrc = htonl(ssrc);     /* synch. source       */
248 
249     buffer = (uint8_t *)hdr;
250     buffer += bytes_in_hdr;
251 
252     /* set RTP data to 0xab */
253     for (i = 0; i < pkt_octet_len; i++)
254         *buffer++ = 0xab;
255 
256     /* set post-data value to 0xffff to enable overrun checking */
257     for (i = 0; i < SRTP_MAX_TRAILER_LEN + 4; i++)
258         *buffer++ = 0xff;
259 
260     return hdr;
261 }
262