4
* test driver for DTLS-SRTP functions
11
* Copyright (c) 2001-2006 Cisco Systems, Inc.
12
* All rights reserved.
14
* Redistribution and use in source and binary forms, with or without
15
* modification, are permitted provided that the following conditions
18
* Redistributions of source code must retain the above copyright
19
* notice, this list of conditions and the following disclaimer.
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.
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.
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.
45
#include <stdio.h> /* for printf() */
46
#include "getopt_s.h" /* for local getopt() */
47
#include "srtp_priv.h"
53
srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc);
56
usage(char *prog_name) {
57
printf("usage: %s [ -t ][ -c ][ -v ][-d <debug_module> ]* [ -l ]\n"
58
" -d <mod> turn on debugging module <mod>\n"
59
" -l list debugging modules\n", prog_name);
64
main(int argc, char *argv[]) {
65
unsigned do_list_mods = 0;
69
printf("dtls_srtp_driver\n");
71
/* initialize srtp library */
74
printf("error: srtp init failed with error code %d\n", err);
78
/* process input arguments */
80
q = getopt_s(argc, argv, "ld:");
88
err = crypto_kernel_set_debug_module(optarg_s, 1);
90
printf("error: set debug module (%s) failed\n", optarg_s);
100
err = crypto_kernel_list_debug_modules();
102
printf("error: list of debug modules failed\n");
107
printf("testing dtls_srtp...");
108
err = test_dtls_srtp();
110
printf("\nerror (code %d)\n", err);
121
srtp_hdr_t *test_packet;
122
int test_packet_len = 80;
124
srtp_policy_t policy;
125
uint8_t key[SRTP_MAX_KEY_LEN];
126
uint8_t salt[SRTP_MAX_KEY_LEN];
127
unsigned int key_len, salt_len;
128
srtp_profile_t profile;
131
/* create a 'null' SRTP session */
132
err = srtp_create(&s, NULL);
137
* verify that packet-processing functions behave properly - we
138
* expect that these functions will return err_status_no_ctx
140
test_packet = srtp_create_test_packet(80, 0xa5a5a5a5);
141
if (test_packet == NULL)
142
return err_status_alloc_fail;
143
err = srtp_protect(s, test_packet, &test_packet_len);
144
if (err != err_status_no_ctx) {
145
printf("wrong return value from srtp_protect() (got code %d)\n",
147
return err_status_fail;
149
err = srtp_unprotect(s, test_packet, &test_packet_len);
150
if (err != err_status_no_ctx) {
151
printf("wrong return value from srtp_unprotect() (got code %d)\n",
153
return err_status_fail;
155
err = srtp_protect_rtcp(s, test_packet, &test_packet_len);
156
if (err != err_status_no_ctx) {
157
printf("wrong return value from srtp_protect_rtcp() (got code %d)\n",
159
return err_status_fail;
161
err = srtp_unprotect_rtcp(s, test_packet, &test_packet_len);
162
if (err != err_status_no_ctx) {
163
printf("wrong return value from srtp_unprotect_rtcp() (got code %d)\n",
165
return err_status_fail;
170
* set keys to known values for testing
172
profile = srtp_profile_aes128_cm_sha1_80;
173
key_len = srtp_profile_get_master_key_length(profile);
174
salt_len = srtp_profile_get_master_salt_length(profile);
175
memset(key, 0xff, key_len);
176
memset(salt, 0xee, salt_len);
177
append_salt_to_key(key, key_len, salt, salt_len);
180
/* initialize SRTP policy from profile */
181
err = crypto_policy_set_from_profile_for_rtp(&policy.rtp, profile);
183
err = crypto_policy_set_from_profile_for_rtcp(&policy.rtcp, profile);
185
policy.ssrc.type = ssrc_any_inbound;
188
err = srtp_add_stream(s, &policy);
192
return err_status_ok;
198
* srtp_create_test_packet(len, ssrc) returns a pointer to a
199
* (malloced) example RTP packet whose data field has the length given
200
* by pkt_octet_len and the SSRC value ssrc. The total length of the
201
* packet is twelve octets longer, since the header is at the
202
* beginning. There is room at the end of the packet for a trailer,
203
* and the four octets following the packet are filled with 0xff
204
* values to enable testing for overwrites.
206
* note that the location of the test packet can (and should) be
207
* deallocated with the free() call once it is no longer needed.
211
srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc) {
215
int bytes_in_hdr = 12;
217
/* allocate memory for test packet */
218
hdr = malloc(pkt_octet_len + bytes_in_hdr
219
+ SRTP_MAX_TRAILER_LEN + 4);
223
hdr->version = 2; /* RTP version two */
224
hdr->p = 0; /* no padding needed */
225
hdr->x = 0; /* no header extension */
226
hdr->cc = 0; /* no CSRCs */
227
hdr->m = 0; /* marker bit */
228
hdr->pt = 0xf; /* payload type */
229
hdr->seq = htons(0x1234); /* sequence number */
230
hdr->ts = htonl(0xdecafbad); /* timestamp */
231
hdr->ssrc = htonl(ssrc); /* synch. source */
233
buffer = (uint8_t *)hdr;
234
buffer += bytes_in_hdr;
236
/* set RTP data to 0xab */
237
for (i=0; i < pkt_octet_len; i++)
240
/* set post-data value to 0xffff to enable overrun checking */
241
for (i=0; i < SRTP_MAX_TRAILER_LEN+4; i++)