123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- #include <stdio.h> /* for printf() */
- #include "getopt_s.h"
- #include "srtp_priv.h"
- srtp_err_status_t test_dtls_srtp(void);
- srtp_hdr_t *srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc);
- void usage(char *prog_name)
- {
- printf("usage: %s [ -t ][ -c ][ -v ][-d <debug_module> ]* [ -l ]\n"
- " -d <mod> turn on debugging module <mod>\n"
- " -l list debugging modules\n",
- prog_name);
- exit(1);
- }
- int main(int argc, char *argv[])
- {
- unsigned do_list_mods = 0;
- int q;
- srtp_err_status_t err;
- printf("dtls_srtp_driver\n");
-
- err = srtp_init();
- if (err) {
- printf("error: srtp init failed with error code %d\n", err);
- exit(1);
- }
-
- while (1) {
- q = getopt_s(argc, argv, "ld:");
- if (q == -1)
- break;
- switch (q) {
- case 'l':
- do_list_mods = 1;
- break;
- case 'd':
- err = srtp_crypto_kernel_set_debug_module(optarg_s, 1);
- if (err) {
- printf("error: set debug module (%s) failed\n", optarg_s);
- exit(1);
- }
- break;
- default:
- usage(argv[0]);
- }
- }
- if (do_list_mods) {
- err = srtp_crypto_kernel_list_debug_modules();
- if (err) {
- printf("error: list of debug modules failed\n");
- exit(1);
- }
- }
- printf("testing dtls_srtp...");
- err = test_dtls_srtp();
- if (err) {
- printf("\nerror (code %d)\n", err);
- exit(1);
- }
- printf("passed\n");
-
- err = srtp_shutdown();
- if (err) {
- printf("error: srtp shutdown failed with error code %d\n", err);
- exit(1);
- }
- return 0;
- }
- srtp_err_status_t test_dtls_srtp(void)
- {
- srtp_hdr_t *test_packet;
- int test_packet_len = 80;
- srtp_t s;
- srtp_policy_t policy;
- uint8_t key[SRTP_MAX_KEY_LEN];
- uint8_t salt[SRTP_MAX_KEY_LEN];
- unsigned int key_len, salt_len;
- srtp_profile_t profile;
- srtp_err_status_t err;
- memset(&policy, 0x0, sizeof(srtp_policy_t));
-
- err = srtp_create(&s, NULL);
- if (err)
- return err;
-
- test_packet = srtp_create_test_packet(80, 0xa5a5a5a5);
- if (test_packet == NULL)
- return srtp_err_status_alloc_fail;
- err = srtp_protect(s, test_packet, &test_packet_len);
- if (err != srtp_err_status_no_ctx) {
- printf("wrong return value from srtp_protect() (got code %d)\n", err);
- return srtp_err_status_fail;
- }
- err = srtp_unprotect(s, test_packet, &test_packet_len);
- if (err != srtp_err_status_no_ctx) {
- printf("wrong return value from srtp_unprotect() (got code %d)\n", err);
- return srtp_err_status_fail;
- }
- err = srtp_protect_rtcp(s, test_packet, &test_packet_len);
- if (err != srtp_err_status_no_ctx) {
- printf("wrong return value from srtp_protect_rtcp() (got code %d)\n",
- err);
- return srtp_err_status_fail;
- }
- err = srtp_unprotect_rtcp(s, test_packet, &test_packet_len);
- if (err != srtp_err_status_no_ctx) {
- printf("wrong return value from srtp_unprotect_rtcp() (got code %d)\n",
- err);
- return srtp_err_status_fail;
- }
-
- profile = srtp_profile_aes128_cm_sha1_80;
- key_len = srtp_profile_get_master_key_length(profile);
- salt_len = srtp_profile_get_master_salt_length(profile);
- memset(key, 0xff, key_len);
- memset(salt, 0xee, salt_len);
- srtp_append_salt_to_key(key, key_len, salt, salt_len);
- policy.key = key;
-
- err = srtp_crypto_policy_set_from_profile_for_rtp(&policy.rtp, profile);
- if (err)
- return err;
- err = srtp_crypto_policy_set_from_profile_for_rtcp(&policy.rtcp, profile);
- if (err)
- return err;
- policy.ssrc.type = ssrc_any_inbound;
- policy.ekt = NULL;
- policy.window_size = 128;
- policy.allow_repeat_tx = 0;
- policy.next = NULL;
- err = srtp_add_stream(s, &policy);
- if (err)
- return err;
- err = srtp_dealloc(s);
- if (err)
- return err;
- free(test_packet);
- return srtp_err_status_ok;
- }
- srtp_hdr_t *srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc)
- {
- int i;
- uint8_t *buffer;
- srtp_hdr_t *hdr;
- int bytes_in_hdr = 12;
-
- hdr = malloc(pkt_octet_len + bytes_in_hdr + SRTP_MAX_TRAILER_LEN + 4);
- if (!hdr)
- return NULL;
- hdr->version = 2;
- hdr->p = 0;
- hdr->x = 0;
- hdr->cc = 0;
- hdr->m = 0;
- hdr->pt = 0xf;
- hdr->seq = htons(0x1234);
- hdr->ts = htonl(0xdecafbad);
- hdr->ssrc = htonl(ssrc);
- buffer = (uint8_t *)hdr;
- buffer += bytes_in_hdr;
-
- for (i = 0; i < pkt_octet_len; i++)
- *buffer++ = 0xab;
-
- for (i = 0; i < SRTP_MAX_TRAILER_LEN + 4; i++)
- *buffer++ = 0xff;
- return hdr;
- }
|