1 /*
2 * rtpw.c
3 *
4 * rtp word sender/receiver
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 *
9 * This app is a simple RTP application intended only for testing
10 * libsrtp. It reads one word at a time from words.txt (or
11 * whatever file is specified as DICT_FILE or with -w), and sends one word out
12 * each USEC_RATE microseconds. Secure RTP protections can be
13 * applied. See the usage() function for more details.
14 *
15 */
16
17 /*
18 *
19 * Copyright (c) 2001-2017, Cisco Systems, Inc.
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 *
26 * Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 *
29 * Redistributions in binary form must reproduce the above
30 * copyright notice, this list of conditions and the following
31 * disclaimer in the documentation and/or other materials provided
32 * with the distribution.
33 *
34 * Neither the name of the Cisco Systems, Inc. nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific prior written permission.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
43 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
44 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 *
51 */
52
53 #ifdef HAVE_CONFIG_H
54 #include <config.h>
55 #endif
56
57 #include "getopt_s.h" /* for local getopt() */
58
59 #include <stdio.h> /* for printf, fprintf */
60 #include <stdlib.h> /* for atoi() */
61 #include <errno.h>
62 #include <signal.h> /* for signal() */
63
64 #include <string.h> /* for strncpy() */
65 #include <time.h> /* for usleep() */
66
67 #ifdef HAVE_UNISTD_H
68 #include <unistd.h> /* for close() */
69 #elif defined(_MSC_VER)
70 #include <io.h> /* for _close() */
71 #define close _close
72 #endif
73 #ifdef HAVE_SYS_SOCKET_H
74 #include <sys/socket.h>
75 #endif
76 #ifdef HAVE_NETINET_IN_H
77 #include <netinet/in.h>
78 #elif defined HAVE_WINSOCK2_H
79 #include <winsock2.h>
80 #include <ws2tcpip.h>
81 #define RTPW_USE_WINSOCK2 1
82 #endif
83 #ifdef HAVE_ARPA_INET_H
84 #include <arpa/inet.h>
85 #endif
86
87 #include "srtp.h"
88 #include "rtp.h"
89 #include "util.h"
90
91 #define DICT_FILE "words.txt"
92 #define USEC_RATE (5e5)
93 #define MAX_WORD_LEN 128
94 #define ADDR_IS_MULTICAST(a) IN_MULTICAST(htonl(a))
95 #define MAX_KEY_LEN 96
96
97 #ifndef HAVE_USLEEP
98 #ifdef HAVE_WINDOWS_H
99 #define usleep(us) Sleep((us) / 1000)
100 #else
101 #define usleep(us) sleep((us) / 1000000)
102 #endif
103 #endif
104
105 /*
106 * the function usage() prints an error message describing how this
107 * program should be called, then calls exit()
108 */
109
110 void usage(char *prog_name);
111
112 /*
113 * leave_group(...) de-registers from a multicast group
114 */
115
116 void leave_group(int sock, struct ip_mreq mreq, char *name);
117
118 /*
119 * setup_signal_handler() sets up a signal handler to trigger
120 * cleanups after an interrupt
121 */
122 int setup_signal_handler(char *name);
123
124 /*
125 * handle_signal(...) handles interrupt signal to trigger cleanups
126 */
127
128 volatile int interrupted = 0;
129
130 /*
131 * program_type distinguishes the [s]rtp sender and receiver cases
132 */
133
134 typedef enum { sender, receiver, unknown } program_type;
135
main(int argc,char * argv[])136 int main(int argc, char *argv[])
137 {
138 char *dictfile = DICT_FILE;
139 FILE *dict;
140 char word[MAX_WORD_LEN];
141 int sock, ret;
142 struct in_addr rcvr_addr;
143 struct sockaddr_in name;
144 struct ip_mreq mreq;
145 #if BEW
146 struct sockaddr_in local;
147 #endif
148 program_type prog_type = unknown;
149 srtp_sec_serv_t sec_servs = sec_serv_none;
150 unsigned char ttl = 5;
151 int c;
152 int key_size = 128;
153 int tag_size = 8;
154 int gcm_on = 0;
155 char *input_key = NULL;
156 int b64_input = 0;
157 char *address = NULL;
158 char key[MAX_KEY_LEN];
159 unsigned short port = 0;
160 rtp_sender_t snd;
161 srtp_policy_t policy;
162 srtp_err_status_t status;
163 int len;
164 int expected_len;
165 int do_list_mods = 0;
166 uint32_t ssrc = 0xdeadbeef; /* ssrc value hardcoded for now */
167 #ifdef RTPW_USE_WINSOCK2
168 WORD wVersionRequested = MAKEWORD(2, 0);
169 WSADATA wsaData;
170
171 ret = WSAStartup(wVersionRequested, &wsaData);
172 if (ret != 0) {
173 fprintf(stderr, "error: WSAStartup() failed: %d\n", ret);
174 exit(1);
175 }
176 #endif
177
178 memset(&policy, 0x0, sizeof(srtp_policy_t));
179
180 printf("Using %s [0x%x]\n", srtp_get_version_string(), srtp_get_version());
181
182 if (setup_signal_handler(argv[0]) != 0) {
183 exit(1);
184 }
185
186 /* initialize srtp library */
187 status = srtp_init();
188 if (status) {
189 printf("error: srtp initialization failed with error code %d\n",
190 status);
191 exit(1);
192 }
193
194 /* check args */
195 while (1) {
196 c = getopt_s(argc, argv, "b:k:rsgt:ae:ld:w:");
197 if (c == -1) {
198 break;
199 }
200 switch (c) {
201 case 'b':
202 b64_input = 1;
203 /* fall thru */
204 case 'k':
205 input_key = optarg_s;
206 break;
207 case 'e':
208 key_size = atoi(optarg_s);
209 if (key_size != 128 && key_size != 256) {
210 printf("error: encryption key size must be 128 or 256 (%d)\n",
211 key_size);
212 exit(1);
213 }
214 sec_servs |= sec_serv_conf;
215 break;
216 case 't':
217 tag_size = atoi(optarg_s);
218 if (tag_size != 8 && tag_size != 16) {
219 printf("error: GCM tag size must be 8 or 16 (%d)\n", tag_size);
220 exit(1);
221 }
222 break;
223 case 'a':
224 sec_servs |= sec_serv_auth;
225 break;
226 case 'g':
227 gcm_on = 1;
228 sec_servs |= sec_serv_auth;
229 break;
230 case 'r':
231 prog_type = receiver;
232 break;
233 case 's':
234 prog_type = sender;
235 break;
236 case 'd':
237 status = srtp_set_debug_module(optarg_s, 1);
238 if (status) {
239 printf("error: set debug module (%s) failed\n", optarg_s);
240 exit(1);
241 }
242 break;
243 case 'l':
244 do_list_mods = 1;
245 break;
246 case 'w':
247 dictfile = optarg_s;
248 break;
249 default:
250 usage(argv[0]);
251 }
252 }
253
254 if (prog_type == unknown) {
255 if (do_list_mods) {
256 status = srtp_list_debug_modules();
257 if (status) {
258 printf("error: list of debug modules failed\n");
259 exit(1);
260 }
261 return 0;
262 } else {
263 printf("error: neither sender [-s] nor receiver [-r] specified\n");
264 usage(argv[0]);
265 }
266 }
267
268 if ((sec_servs && !input_key) || (!sec_servs && input_key)) {
269 /*
270 * a key must be provided if and only if security services have
271 * been requested
272 */
273 usage(argv[0]);
274 }
275
276 if (argc != optind_s + 2) {
277 /* wrong number of arguments */
278 usage(argv[0]);
279 }
280
281 /* get address from arg */
282 address = argv[optind_s++];
283
284 /* get port from arg */
285 port = atoi(argv[optind_s++]);
286
287 /* set address */
288 #ifdef HAVE_INET_ATON
289 if (0 == inet_aton(address, &rcvr_addr)) {
290 fprintf(stderr, "%s: cannot parse IP v4 address %s\n", argv[0],
291 address);
292 exit(1);
293 }
294 if (rcvr_addr.s_addr == INADDR_NONE) {
295 fprintf(stderr, "%s: address error", argv[0]);
296 exit(1);
297 }
298 #else
299 rcvr_addr.s_addr = inet_addr(address);
300 if (0xffffffff == rcvr_addr.s_addr) {
301 fprintf(stderr, "%s: cannot parse IP v4 address %s\n", argv[0],
302 address);
303 exit(1);
304 }
305 #endif
306
307 /* open socket */
308 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
309 if (sock < 0) {
310 int err;
311 #ifdef RTPW_USE_WINSOCK2
312 err = WSAGetLastError();
313 #else
314 err = errno;
315 #endif
316 fprintf(stderr, "%s: couldn't open socket: %d\n", argv[0], err);
317 exit(1);
318 }
319
320 name.sin_addr = rcvr_addr;
321 name.sin_family = PF_INET;
322 name.sin_port = htons(port);
323
324 if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
325 if (prog_type == sender) {
326 ret = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
327 sizeof(ttl));
328 if (ret < 0) {
329 fprintf(stderr, "%s: Failed to set TTL for multicast group",
330 argv[0]);
331 perror("");
332 exit(1);
333 }
334 }
335
336 mreq.imr_multiaddr.s_addr = rcvr_addr.s_addr;
337 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
338 ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq,
339 sizeof(mreq));
340 if (ret < 0) {
341 fprintf(stderr, "%s: Failed to join multicast group", argv[0]);
342 perror("");
343 exit(1);
344 }
345 }
346
347 /* report security services selected on the command line */
348 printf("security services: ");
349 if (sec_servs & sec_serv_conf)
350 printf("confidentiality ");
351 if (sec_servs & sec_serv_auth)
352 printf("message authentication");
353 if (sec_servs == sec_serv_none)
354 printf("none");
355 printf("\n");
356
357 /* set up the srtp policy and master key */
358 if (sec_servs) {
359 /*
360 * create policy structure, using the default mechanisms but
361 * with only the security services requested on the command line,
362 * using the right SSRC value
363 */
364 switch (sec_servs) {
365 case sec_serv_conf_and_auth:
366 if (gcm_on) {
367 #ifdef OPENSSL
368 switch (key_size) {
369 case 128:
370 srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtp);
371 srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtcp);
372 break;
373 case 256:
374 srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy.rtp);
375 srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy.rtcp);
376 break;
377 }
378 #else
379 printf("error: GCM mode only supported when using the OpenSSL "
380 "crypto engine.\n");
381 return 0;
382 #endif
383 } else {
384 switch (key_size) {
385 case 128:
386 srtp_crypto_policy_set_rtp_default(&policy.rtp);
387 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
388 break;
389 case 256:
390 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtp);
391 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
392 break;
393 }
394 }
395 break;
396 case sec_serv_conf:
397 if (gcm_on) {
398 printf(
399 "error: GCM mode must always be used with auth enabled\n");
400 return -1;
401 } else {
402 switch (key_size) {
403 case 128:
404 srtp_crypto_policy_set_aes_cm_128_null_auth(&policy.rtp);
405 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
406 break;
407 case 256:
408 srtp_crypto_policy_set_aes_cm_256_null_auth(&policy.rtp);
409 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
410 break;
411 }
412 }
413 break;
414 case sec_serv_auth:
415 if (gcm_on) {
416 #ifdef OPENSSL
417 switch (key_size) {
418 case 128:
419 srtp_crypto_policy_set_aes_gcm_128_8_only_auth(&policy.rtp);
420 srtp_crypto_policy_set_aes_gcm_128_8_only_auth(
421 &policy.rtcp);
422 break;
423 case 256:
424 srtp_crypto_policy_set_aes_gcm_256_8_only_auth(&policy.rtp);
425 srtp_crypto_policy_set_aes_gcm_256_8_only_auth(
426 &policy.rtcp);
427 break;
428 }
429 #else
430 printf("error: GCM mode only supported when using the OpenSSL "
431 "crypto engine.\n");
432 return 0;
433 #endif
434 } else {
435 srtp_crypto_policy_set_null_cipher_hmac_sha1_80(&policy.rtp);
436 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
437 }
438 break;
439 default:
440 printf("error: unknown security service requested\n");
441 return -1;
442 }
443 policy.ssrc.type = ssrc_specific;
444 policy.ssrc.value = ssrc;
445 policy.key = (uint8_t *)key;
446 policy.ekt = NULL;
447 policy.next = NULL;
448 policy.window_size = 128;
449 policy.allow_repeat_tx = 0;
450 policy.rtp.sec_serv = sec_servs;
451 policy.rtcp.sec_serv = sec_serv_none; /* we don't do RTCP anyway */
452
453 if (gcm_on && tag_size != 8) {
454 policy.rtp.auth_tag_len = tag_size;
455 }
456
457 /*
458 * read key from hexadecimal or base64 on command line into an octet
459 * string
460 */
461 if (b64_input) {
462 int pad;
463 expected_len = (policy.rtp.cipher_key_len * 4) / 3;
464 len = base64_string_to_octet_string(key, &pad, input_key,
465 expected_len);
466 if (pad != 0) {
467 fprintf(stderr, "error: padding in base64 unexpected\n");
468 exit(1);
469 }
470 } else {
471 expected_len = policy.rtp.cipher_key_len * 2;
472 len = hex_string_to_octet_string(key, input_key, expected_len);
473 }
474 /* check that hex string is the right length */
475 if (len < expected_len) {
476 fprintf(stderr, "error: too few digits in key/salt "
477 "(should be %d digits, found %d)\n",
478 expected_len, len);
479 exit(1);
480 }
481 if ((int)strlen(input_key) > policy.rtp.cipher_key_len * 2) {
482 fprintf(stderr, "error: too many digits in key/salt "
483 "(should be %d hexadecimal digits, found %u)\n",
484 policy.rtp.cipher_key_len * 2, (unsigned)strlen(input_key));
485 exit(1);
486 }
487
488 printf("set master key/salt to %s/", octet_string_hex_string(key, 16));
489 printf("%s\n", octet_string_hex_string(key + 16, 14));
490
491 } else {
492 /*
493 * we're not providing security services, so set the policy to the
494 * null policy
495 *
496 * Note that this policy does not conform to the SRTP
497 * specification, since RTCP authentication is required. However,
498 * the effect of this policy is to turn off SRTP, so that this
499 * application is now a vanilla-flavored RTP application.
500 */
501 srtp_crypto_policy_set_null_cipher_hmac_null(&policy.rtp);
502 srtp_crypto_policy_set_null_cipher_hmac_null(&policy.rtcp);
503 policy.key = (uint8_t *)key;
504 policy.ssrc.type = ssrc_specific;
505 policy.ssrc.value = ssrc;
506 policy.window_size = 0;
507 policy.allow_repeat_tx = 0;
508 policy.ekt = NULL;
509 policy.next = NULL;
510 }
511
512 if (prog_type == sender) {
513 #if BEW
514 /* bind to local socket (to match crypto policy, if need be) */
515 memset(&local, 0, sizeof(struct sockaddr_in));
516 local.sin_addr.s_addr = htonl(INADDR_ANY);
517 local.sin_port = htons(port);
518 ret = bind(sock, (struct sockaddr *)&local, sizeof(struct sockaddr_in));
519 if (ret < 0) {
520 fprintf(stderr, "%s: bind failed\n", argv[0]);
521 perror("");
522 exit(1);
523 }
524 #endif /* BEW */
525
526 /* initialize sender's rtp and srtp contexts */
527 snd = rtp_sender_alloc();
528 if (snd == NULL) {
529 fprintf(stderr, "error: malloc() failed\n");
530 exit(1);
531 }
532 rtp_sender_init(snd, sock, name, ssrc);
533 status = rtp_sender_init_srtp(snd, &policy);
534 if (status) {
535 fprintf(stderr, "error: srtp_create() failed with code %d\n",
536 status);
537 exit(1);
538 }
539
540 /* open dictionary */
541 dict = fopen(dictfile, "r");
542 if (dict == NULL) {
543 fprintf(stderr, "%s: couldn't open file %s\n", argv[0], dictfile);
544 if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
545 leave_group(sock, mreq, argv[0]);
546 }
547 exit(1);
548 }
549
550 /* read words from dictionary, then send them off */
551 while (!interrupted && fgets(word, MAX_WORD_LEN, dict) != NULL) {
552 len = strlen(word) + 1; /* plus one for null */
553
554 if (len > MAX_WORD_LEN)
555 printf("error: word %s too large to send\n", word);
556 else {
557 rtp_sendto(snd, word, len);
558 printf("sending word: %s", word);
559 }
560 usleep(USEC_RATE);
561 }
562
563 rtp_sender_deinit_srtp(snd);
564 rtp_sender_dealloc(snd);
565
566 fclose(dict);
567 } else { /* prog_type == receiver */
568 rtp_receiver_t rcvr;
569
570 if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
571 close(sock);
572 fprintf(stderr, "%s: socket bind error\n", argv[0]);
573 perror(NULL);
574 if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
575 leave_group(sock, mreq, argv[0]);
576 }
577 exit(1);
578 }
579
580 rcvr = rtp_receiver_alloc();
581 if (rcvr == NULL) {
582 fprintf(stderr, "error: malloc() failed\n");
583 exit(1);
584 }
585 rtp_receiver_init(rcvr, sock, name, ssrc);
586 status = rtp_receiver_init_srtp(rcvr, &policy);
587 if (status) {
588 fprintf(stderr, "error: srtp_create() failed with code %d\n",
589 status);
590 exit(1);
591 }
592
593 /* get next word and loop */
594 while (!interrupted) {
595 len = MAX_WORD_LEN;
596 if (rtp_recvfrom(rcvr, word, &len) > -1)
597 printf("\tword: %s\n", word);
598 }
599
600 rtp_receiver_deinit_srtp(rcvr);
601 rtp_receiver_dealloc(rcvr);
602 }
603
604 if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
605 leave_group(sock, mreq, argv[0]);
606 }
607
608 #ifdef RTPW_USE_WINSOCK2
609 ret = closesocket(sock);
610 #else
611 ret = close(sock);
612 #endif
613 if (ret < 0) {
614 fprintf(stderr, "%s: Failed to close socket", argv[0]);
615 perror("");
616 }
617
618 status = srtp_shutdown();
619 if (status) {
620 printf("error: srtp shutdown failed with error code %d\n", status);
621 exit(1);
622 }
623
624 #ifdef RTPW_USE_WINSOCK2
625 WSACleanup();
626 #endif
627
628 return 0;
629 }
630
usage(char * string)631 void usage(char *string)
632 {
633 printf("usage: %s [-d <debug>]* [-k <key> [-a][-e]] "
634 "[-s | -r] dest_ip dest_port\n"
635 "or %s -l\n"
636 "where -a use message authentication\n"
637 " -e <key size> use encryption (use 128 or 256 for key size)\n"
638 " -g Use AES-GCM mode (must be used with -e)\n"
639 " -t <tag size> Tag size to use in GCM mode (use 8 or 16)\n"
640 " -k <key> sets the srtp master key given in hexadecimal\n"
641 " -b <key> sets the srtp master key given in base64\n"
642 " -s act as rtp sender\n"
643 " -r act as rtp receiver\n"
644 " -l list debug modules\n"
645 " -d <debug> turn on debugging for module <debug>\n"
646 " -w <wordsfile> use <wordsfile> for input, rather than %s\n",
647 string, string, DICT_FILE);
648 exit(1);
649 }
650
leave_group(int sock,struct ip_mreq mreq,char * name)651 void leave_group(int sock, struct ip_mreq mreq, char *name)
652 {
653 int ret;
654
655 ret = setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreq,
656 sizeof(mreq));
657 if (ret < 0) {
658 fprintf(stderr, "%s: Failed to leave multicast group", name);
659 perror("");
660 }
661 }
662
handle_signal(int signum)663 void handle_signal(int signum)
664 {
665 interrupted = 1;
666 /* Reset handler explicitly, in case we don't have sigaction() (and signal()
667 has BSD semantics), or we don't have SA_RESETHAND */
668 signal(signum, SIG_DFL);
669 }
670
setup_signal_handler(char * name)671 int setup_signal_handler(char *name)
672 {
673 #if HAVE_SIGACTION
674 struct sigaction act;
675 memset(&act, 0, sizeof(act));
676
677 act.sa_handler = handle_signal;
678 sigemptyset(&act.sa_mask);
679 #if defined(SA_RESETHAND)
680 act.sa_flags = SA_RESETHAND;
681 #else
682 act.sa_flags = 0;
683 #endif
684 /* Note that we're not setting SA_RESTART; we want recvfrom to return
685 * EINTR when we signal the receiver. */
686
687 if (sigaction(SIGTERM, &act, NULL) != 0) {
688 fprintf(stderr, "%s: error setting up signal handler", name);
689 perror("");
690 return -1;
691 }
692 #else
693 if (signal(SIGTERM, handle_signal) == SIG_ERR) {
694 fprintf(stderr, "%s: error setting up signal handler", name);
695 perror("");
696 return -1;
697 }
698 #endif
699 return 0;
700 }
701