| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  * dtls_srtp_driver.c | 2  * dtls_srtp_driver.c | 
| 3  * | 3  * | 
| 4  * test driver for DTLS-SRTP functions | 4  * test driver for DTLS-SRTP functions | 
| 5  * | 5  * | 
| 6  * David McGrew | 6  * David McGrew | 
| 7  * Cisco Systems, Inc. | 7  * Cisco Systems, Inc. | 
| 8  */ | 8  */ | 
| 9 /* | 9 /* | 
| 10  * | 10  * | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
| 39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 
| 40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | 40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | 
| 41  * OF THE POSSIBILITY OF SUCH DAMAGE. | 41  * OF THE POSSIBILITY OF SUCH DAMAGE. | 
| 42  * | 42  * | 
| 43  */ | 43  */ | 
| 44 | 44 | 
| 45 #include <stdio.h>    /* for printf()          */ | 45 #include <stdio.h>    /* for printf()          */ | 
| 46 #include "getopt_s.h" /* for local getopt()    */ | 46 #include "getopt_s.h" /* for local getopt()    */ | 
| 47 #include "srtp_priv.h" | 47 #include "srtp_priv.h" | 
| 48 | 48 | 
| 49 err_status_t | 49 srtp_err_status_t | 
| 50 test_dtls_srtp(void); | 50 test_dtls_srtp(void); | 
| 51 | 51 | 
| 52 srtp_hdr_t * | 52 srtp_hdr_t * | 
| 53 srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc); | 53 srtp_create_test_packet(int pkt_octet_len, uint32_t ssrc); | 
| 54 | 54 | 
| 55 void | 55 void | 
| 56 usage(char *prog_name) { | 56 usage(char *prog_name) { | 
| 57   printf("usage: %s [ -t ][ -c ][ -v ][-d <debug_module> ]* [ -l ]\n" | 57   printf("usage: %s [ -t ][ -c ][ -v ][-d <debug_module> ]* [ -l ]\n" | 
| 58          "  -d <mod>   turn on debugging module <mod>\n" | 58          "  -d <mod>   turn on debugging module <mod>\n" | 
| 59          "  -l         list debugging modules\n", prog_name); | 59          "  -l         list debugging modules\n", prog_name); | 
| 60   exit(1); | 60   exit(1); | 
| 61 } | 61 } | 
| 62 | 62 | 
| 63 int | 63 int | 
| 64 main(int argc, char *argv[]) { | 64 main(int argc, char *argv[]) { | 
| 65   unsigned do_list_mods      = 0; | 65   unsigned do_list_mods      = 0; | 
| 66   int q; | 66   int q; | 
| 67   err_status_t err; | 67   srtp_err_status_t err; | 
| 68 | 68 | 
| 69   printf("dtls_srtp_driver\n"); | 69   printf("dtls_srtp_driver\n"); | 
| 70 | 70 | 
| 71   /* initialize srtp library */ | 71   /* initialize srtp library */ | 
| 72   err = srtp_init(); | 72   err = srtp_init(); | 
| 73   if (err) { | 73   if (err) { | 
| 74     printf("error: srtp init failed with error code %d\n", err); | 74     printf("error: srtp init failed with error code %d\n", err); | 
| 75     exit(1); | 75     exit(1); | 
| 76   } | 76   } | 
| 77 | 77 | 
| 78   /* process input arguments */ | 78   /* process input arguments */ | 
| 79   while (1) { | 79   while (1) { | 
| 80     q = getopt_s(argc, argv, "ld:"); | 80     q = getopt_s(argc, argv, "ld:"); | 
| 81     if (q == -1) | 81     if (q == -1) | 
| 82       break; | 82       break; | 
| 83     switch (q) { | 83     switch (q) { | 
| 84     case 'l': | 84     case 'l': | 
| 85       do_list_mods = 1; | 85       do_list_mods = 1; | 
| 86       break; | 86       break; | 
| 87     case 'd': | 87     case 'd': | 
| 88       err = crypto_kernel_set_debug_module(optarg_s, 1); | 88       err = srtp_crypto_kernel_set_debug_module(optarg_s, 1); | 
| 89       if (err) { | 89       if (err) { | 
| 90         printf("error: set debug module (%s) failed\n", optarg_s); | 90         printf("error: set debug module (%s) failed\n", optarg_s); | 
| 91         exit(1); | 91         exit(1); | 
| 92       } | 92       } | 
| 93       break; | 93       break; | 
| 94     default: | 94     default: | 
| 95       usage(argv[0]); | 95       usage(argv[0]); | 
| 96     } | 96     } | 
| 97   } | 97   } | 
| 98 | 98 | 
| 99   if (do_list_mods) { | 99   if (do_list_mods) { | 
| 100     err = crypto_kernel_list_debug_modules(); | 100     err = srtp_crypto_kernel_list_debug_modules(); | 
| 101     if (err) { | 101     if (err) { | 
| 102       printf("error: list of debug modules failed\n"); | 102       printf("error: list of debug modules failed\n"); | 
| 103       exit(1); | 103       exit(1); | 
| 104     } | 104     } | 
| 105   } | 105   } | 
| 106 | 106 | 
| 107   printf("testing dtls_srtp..."); | 107   printf("testing dtls_srtp..."); | 
| 108   err = test_dtls_srtp(); | 108   err = test_dtls_srtp(); | 
| 109   if (err) { | 109   if (err) { | 
| 110     printf("\nerror (code %d)\n", err); | 110     printf("\nerror (code %d)\n", err); | 
| 111     exit(1); | 111     exit(1); | 
| 112   } | 112   } | 
| 113   printf("passed\n"); | 113   printf("passed\n"); | 
| 114 | 114 | 
| 115   /* shut down srtp library */ | 115   /* shut down srtp library */ | 
| 116   err = srtp_shutdown(); | 116   err = srtp_shutdown(); | 
| 117   if (err) { | 117   if (err) { | 
| 118     printf("error: srtp shutdown failed with error code %d\n", err); | 118     printf("error: srtp shutdown failed with error code %d\n", err); | 
| 119     exit(1); | 119     exit(1); | 
| 120   } | 120   } | 
| 121 | 121 | 
| 122   return 0; | 122   return 0; | 
| 123 } | 123 } | 
| 124 | 124 | 
| 125 | 125 | 
| 126 err_status_t | 126 srtp_err_status_t | 
| 127 test_dtls_srtp(void) { | 127 test_dtls_srtp(void) { | 
| 128   srtp_hdr_t *test_packet; | 128   srtp_hdr_t *test_packet; | 
| 129   int test_packet_len = 80; | 129   int test_packet_len = 80; | 
| 130   srtp_t s; | 130   srtp_t s; | 
| 131   srtp_policy_t policy; | 131   srtp_policy_t policy; | 
| 132   uint8_t key[SRTP_MAX_KEY_LEN]; | 132   uint8_t key[SRTP_MAX_KEY_LEN]; | 
| 133   uint8_t salt[SRTP_MAX_KEY_LEN]; | 133   uint8_t salt[SRTP_MAX_KEY_LEN]; | 
| 134   unsigned int key_len, salt_len; | 134   unsigned int key_len, salt_len; | 
| 135   srtp_profile_t profile; | 135   srtp_profile_t profile; | 
| 136   err_status_t err; | 136   srtp_err_status_t err; | 
|  | 137 | 
|  | 138   memset(&policy, 0x0, sizeof(srtp_policy_t)); | 
| 137 | 139 | 
| 138   /* create a 'null' SRTP session */ | 140   /* create a 'null' SRTP session */ | 
| 139   err = srtp_create(&s, NULL); | 141   err = srtp_create(&s, NULL); | 
| 140   if (err) | 142   if (err) | 
| 141     return err; | 143     return err; | 
| 142 | 144 | 
| 143   /* | 145   /* | 
| 144    * verify that packet-processing functions behave properly - we | 146    * verify that packet-processing functions behave properly - we | 
| 145    * expect that these functions will return err_status_no_ctx | 147    * expect that these functions will return srtp_err_status_no_ctx | 
| 146    */ | 148    */ | 
| 147   test_packet = srtp_create_test_packet(80, 0xa5a5a5a5); | 149   test_packet = srtp_create_test_packet(80, 0xa5a5a5a5); | 
| 148   if (test_packet == NULL) | 150   if (test_packet == NULL) | 
| 149     return err_status_alloc_fail; | 151     return srtp_err_status_alloc_fail; | 
| 150   err = srtp_protect(s, test_packet, &test_packet_len); | 152   err = srtp_protect(s, test_packet, &test_packet_len); | 
| 151   if (err != err_status_no_ctx) { | 153   if (err != srtp_err_status_no_ctx) { | 
| 152     printf("wrong return value from srtp_protect() (got code %d)\n", | 154     printf("wrong return value from srtp_protect() (got code %d)\n", | 
| 153            err); | 155            err); | 
| 154     return err_status_fail; | 156     return srtp_err_status_fail; | 
| 155   } | 157   } | 
| 156   err = srtp_unprotect(s, test_packet, &test_packet_len); | 158   err = srtp_unprotect(s, test_packet, &test_packet_len); | 
| 157   if (err != err_status_no_ctx) { | 159   if (err != srtp_err_status_no_ctx) { | 
| 158     printf("wrong return value from srtp_unprotect() (got code %d)\n", | 160     printf("wrong return value from srtp_unprotect() (got code %d)\n", | 
| 159            err); | 161            err); | 
| 160     return err_status_fail; | 162     return srtp_err_status_fail; | 
| 161   } | 163   } | 
| 162   err = srtp_protect_rtcp(s, test_packet, &test_packet_len); | 164   err = srtp_protect_rtcp(s, test_packet, &test_packet_len); | 
| 163   if (err != err_status_no_ctx) { | 165   if (err != srtp_err_status_no_ctx) { | 
| 164     printf("wrong return value from srtp_protect_rtcp() (got code %d)\n", | 166     printf("wrong return value from srtp_protect_rtcp() (got code %d)\n", | 
| 165            err); | 167            err); | 
| 166     return err_status_fail; | 168     return srtp_err_status_fail; | 
| 167   } | 169   } | 
| 168   err = srtp_unprotect_rtcp(s, test_packet, &test_packet_len); | 170   err = srtp_unprotect_rtcp(s, test_packet, &test_packet_len); | 
| 169   if (err != err_status_no_ctx) { | 171   if (err != srtp_err_status_no_ctx) { | 
| 170     printf("wrong return value from srtp_unprotect_rtcp() (got code %d)\n", | 172     printf("wrong return value from srtp_unprotect_rtcp() (got code %d)\n", | 
| 171            err); | 173            err); | 
| 172     return err_status_fail; | 174     return srtp_err_status_fail; | 
| 173   } | 175   } | 
| 174 | 176 | 
| 175 | 177 | 
| 176   /* | 178   /* | 
| 177    * set keys to known values for testing | 179    * set keys to known values for testing | 
| 178    */ | 180    */ | 
| 179   memset(&policy, 0, sizeof(policy)); |  | 
| 180   profile = srtp_profile_aes128_cm_sha1_80; | 181   profile = srtp_profile_aes128_cm_sha1_80; | 
| 181   key_len = srtp_profile_get_master_key_length(profile); | 182   key_len = srtp_profile_get_master_key_length(profile); | 
| 182   salt_len = srtp_profile_get_master_salt_length(profile); | 183   salt_len = srtp_profile_get_master_salt_length(profile); | 
| 183   memset(key, 0xff, key_len); | 184   memset(key, 0xff, key_len); | 
| 184   memset(salt, 0xee, salt_len); | 185   memset(salt, 0xee, salt_len); | 
| 185   append_salt_to_key(key, key_len, salt, salt_len); | 186   srtp_append_salt_to_key(key, key_len, salt, salt_len); | 
| 186   policy.key  = key; | 187   policy.key  = key; | 
| 187 | 188 | 
| 188   /* initialize SRTP policy from profile  */ | 189   /* initialize SRTP policy from profile  */ | 
| 189   err = crypto_policy_set_from_profile_for_rtp(&policy.rtp, profile); | 190   err = srtp_crypto_policy_set_from_profile_for_rtp(&policy.rtp, profile); | 
| 190   if (err) return err; | 191   if (err) return err; | 
| 191   err = crypto_policy_set_from_profile_for_rtcp(&policy.rtcp, profile); | 192   err = srtp_crypto_policy_set_from_profile_for_rtcp(&policy.rtcp, profile); | 
| 192   if (err) return err; | 193   if (err) return err; | 
| 193   policy.ssrc.type  = ssrc_any_inbound; | 194   policy.ssrc.type  = ssrc_any_inbound; | 
| 194   policy.ekt = NULL; | 195   policy.ekt = NULL; | 
| 195   policy.window_size = 128; | 196   policy.window_size = 128; | 
| 196   policy.allow_repeat_tx = 0; | 197   policy.allow_repeat_tx = 0; | 
| 197   policy.next = NULL; | 198   policy.next = NULL; | 
| 198 | 199 | 
| 199   err = srtp_add_stream(s, &policy); | 200   err = srtp_add_stream(s, &policy); | 
| 200   if (err) | 201   if (err) | 
| 201     return err; | 202     return err; | 
| 202 | 203 | 
| 203   err = srtp_dealloc(s); | 204   err = srtp_dealloc(s); | 
| 204   if (err) | 205   if (err) | 
| 205     return err; | 206     return err; | 
| 206 | 207 | 
| 207   free(test_packet); | 208   free(test_packet); | 
| 208 | 209 | 
| 209   return err_status_ok; | 210   return srtp_err_status_ok; | 
| 210 } | 211 } | 
| 211 | 212 | 
| 212 | 213 | 
| 213 | 214 | 
| 214 /* | 215 /* | 
| 215  * srtp_create_test_packet(len, ssrc) returns a pointer to a | 216  * srtp_create_test_packet(len, ssrc) returns a pointer to a | 
| 216  * (malloced) example RTP packet whose data field has the length given | 217  * (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  * 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  * 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  * beginning.  There is room at the end of the packet for a trailer, | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 253   /* set RTP data to 0xab */ | 254   /* set RTP data to 0xab */ | 
| 254   for (i=0; i < pkt_octet_len; i++) | 255   for (i=0; i < pkt_octet_len; i++) | 
| 255     *buffer++ = 0xab; | 256     *buffer++ = 0xab; | 
| 256 | 257 | 
| 257   /* set post-data value to 0xffff to enable overrun checking */ | 258   /* set post-data value to 0xffff to enable overrun checking */ | 
| 258   for (i=0; i < SRTP_MAX_TRAILER_LEN+4; i++) | 259   for (i=0; i < SRTP_MAX_TRAILER_LEN+4; i++) | 
| 259     *buffer++ = 0xff; | 260     *buffer++ = 0xff; | 
| 260 | 261 | 
| 261   return hdr; | 262   return hdr; | 
| 262 } | 263 } | 
| OLD | NEW | 
|---|