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-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 #include <stdio.h> /* for printf() */ 46 #include "getopt_s.h" /* for local getopt() */ 47 #include "srtp_priv.h" 48 49 err_status_t 50 test_dtls_srtp(void); 51 52 srtp_hdr_t * 53 srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc); 54 55 void 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); 60 exit(1); 61 } 62 63 int 64 main(int argc, char *argv[]) { 65 unsigned do_list_mods = 0; 66 int q; 67 err_status_t err; 68 69 printf("dtls_srtp_driver\n"); 70 71 /* initialize srtp library */ 72 err = srtp_init(); 73 if (err) { 74 printf("error: srtp init failed with error code %d\n", err); 75 exit(1); 76 } 77 78 /* process input arguments */ 79 while (1) { 80 q = getopt_s(argc, argv, "ld:"); 81 if (q == -1) 82 break; 83 switch (q) { 84 case 'l': 85 do_list_mods = 1; 86 break; 87 case 'd': 88 err = crypto_kernel_set_debug_module(optarg_s, 1); 89 if (err) { 90 printf("error: set debug module (%s) failed\n", optarg_s); 91 exit(1); 92 } 93 break; 94 default: 95 usage(argv[0]); 96 } 97 } 98 99 if (do_list_mods) { 100 err = crypto_kernel_list_debug_modules(); 101 if (err) { 102 printf("error: list of debug modules failed\n"); 103 exit(1); 104 } 105 } 106 107 printf("testing dtls_srtp..."); 108 err = test_dtls_srtp(); 109 if (err) { 110 printf("\nerror (code %d)\n", err); 111 exit(1); 112 } 113 printf("passed\n"); 114 115 /* shut down srtp library */ 116 err = srtp_shutdown(); 117 if (err) { 118 printf("error: srtp shutdown failed with error code %d\n", err); 119 exit(1); 120 } 121 122 return 0; 123 } 124 125 126 err_status_t 127 test_dtls_srtp(void) { 128 srtp_hdr_t *test_packet; 129 int test_packet_len = 80; 130 srtp_t s; 131 srtp_policy_t policy; 132 uint8_t key[SRTP_MAX_KEY_LEN]; 133 uint8_t salt[SRTP_MAX_KEY_LEN]; 134 unsigned int key_len, salt_len; 135 srtp_profile_t profile; 136 err_status_t err; 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 err_status_no_ctx 146 */ 147 test_packet = srtp_create_test_packet(80, 0xa5a5a5a5); 148 if (test_packet == NULL) 149 return err_status_alloc_fail; 150 err = srtp_protect(s, test_packet, &test_packet_len); 151 if (err != err_status_no_ctx) { 152 printf("wrong return value from srtp_protect() (got code %d)\n", 153 err); 154 return err_status_fail; 155 } 156 err = srtp_unprotect(s, test_packet, &test_packet_len); 157 if (err != err_status_no_ctx) { 158 printf("wrong return value from srtp_unprotect() (got code %d)\n", 159 err); 160 return err_status_fail; 161 } 162 err = srtp_protect_rtcp(s, test_packet, &test_packet_len); 163 if (err != err_status_no_ctx) { 164 printf("wrong return value from srtp_protect_rtcp() (got code %d)\n", 165 err); 166 return err_status_fail; 167 } 168 err = srtp_unprotect_rtcp(s, test_packet, &test_packet_len); 169 if (err != err_status_no_ctx) { 170 printf("wrong return value from srtp_unprotect_rtcp() (got code %d)\n", 171 err); 172 return err_status_fail; 173 } 174 175 176 /* 177 * set keys to known values for testing 178 */ 179 profile = srtp_profile_aes128_cm_sha1_80; 180 key_len = srtp_profile_get_master_key_length(profile); 181 salt_len = srtp_profile_get_master_salt_length(profile); 182 memset(key, 0xff, key_len); 183 memset(salt, 0xee, salt_len); 184 append_salt_to_key(key, key_len, salt, salt_len); 185 policy.key = key; 186 187 /* initialize SRTP policy from profile */ 188 err = crypto_policy_set_from_profile_for_rtp(&policy.rtp, profile); 189 if (err) return err; 190 err = crypto_policy_set_from_profile_for_rtcp(&policy.rtcp, profile); 191 if (err) return err; 192 policy.ssrc.type = ssrc_any_inbound; 193 policy.ekt = NULL; 194 policy.window_size = 128; 195 policy.allow_repeat_tx = 0; 196 policy.next = NULL; 197 198 err = srtp_add_stream(s, &policy); 199 if (err) 200 return err; 201 202 err = srtp_dealloc(s); 203 if (err) 204 return err; 205 206 free(test_packet); 207 208 return err_status_ok; 209 } 210 211 212 213 /* 214 * srtp_create_test_packet(len, ssrc) returns a pointer to a 215 * (malloced) example RTP packet whose data field has the length given 216 * by pkt_octet_len and the SSRC value ssrc. The total length of the 217 * packet is twelve octets longer, since the header is at the 218 * beginning. There is room at the end of the packet for a trailer, 219 * and the four octets following the packet are filled with 0xff 220 * values to enable testing for overwrites. 221 * 222 * note that the location of the test packet can (and should) be 223 * deallocated with the free() call once it is no longer needed. 224 */ 225 226 srtp_hdr_t * 227 srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc) { 228 int i; 229 uint8_t *buffer; 230 srtp_hdr_t *hdr; 231 int bytes_in_hdr = 12; 232 233 /* allocate memory for test packet */ 234 hdr = malloc(pkt_octet_len + bytes_in_hdr 235 + 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