| OLD | NEW |
| (Empty) |
| 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 memset(&policy, 0, sizeof(policy)); | |
| 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 append_salt_to_key(key, key_len, salt, salt_len); | |
| 186 policy.key = key; | |
| 187 | |
| 188 /* initialize SRTP policy from profile */ | |
| 189 err = crypto_policy_set_from_profile_for_rtp(&policy.rtp, profile); | |
| 190 if (err) return err; | |
| 191 err = crypto_policy_set_from_profile_for_rtcp(&policy.rtcp, profile); | |
| 192 if (err) return err; | |
| 193 policy.ssrc.type = ssrc_any_inbound; | |
| 194 policy.ekt = NULL; | |
| 195 policy.window_size = 128; | |
| 196 policy.allow_repeat_tx = 0; | |
| 197 policy.next = NULL; | |
| 198 | |
| 199 err = srtp_add_stream(s, &policy); | |
| 200 if (err) | |
| 201 return err; | |
| 202 | |
| 203 err = srtp_dealloc(s); | |
| 204 if (err) | |
| 205 return err; | |
| 206 | |
| 207 free(test_packet); | |
| 208 | |
| 209 return err_status_ok; | |
| 210 } | |
| 211 | |
| 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 | |
| 227 srtp_hdr_t * | |
| 228 srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc) { | |
| 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 | |
| 236 + SRTP_MAX_TRAILER_LEN + 4); | |
| 237 if (!hdr) | |
| 238 return NULL; | |
| 239 | |
| 240 hdr->version = 2; /* RTP version two */ | |
| 241 hdr->p = 0; /* no padding needed */ | |
| 242 hdr->x = 0; /* no header extension */ | |
| 243 hdr->cc = 0; /* no CSRCs */ | |
| 244 hdr->m = 0; /* marker bit */ | |
| 245 hdr->pt = 0xf; /* payload type */ | |
| 246 hdr->seq = htons(0x1234); /* sequence number */ | |
| 247 hdr->ts = htonl(0xdecafbad); /* timestamp */ | |
| 248 hdr->ssrc = htonl(ssrc); /* synch. source */ | |
| 249 | |
| 250 buffer = (uint8_t *)hdr; | |
| 251 buffer += bytes_in_hdr; | |
| 252 | |
| 253 /* set RTP data to 0xab */ | |
| 254 for (i=0; i < pkt_octet_len; i++) | |
| 255 *buffer++ = 0xab; | |
| 256 | |
| 257 /* set post-data value to 0xffff to enable overrun checking */ | |
| 258 for (i=0; i < SRTP_MAX_TRAILER_LEN+4; i++) | |
| 259 *buffer++ = 0xff; | |
| 260 | |
| 261 return hdr; | |
| 262 } | |
| OLD | NEW |