dtls_srtp_driver.c 7.8 KB

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