History

rtmp: Add support for SWFVerification

Specifies how the server verifies client SWF files before allowing the files to connect to an application. Verifying SWF files is a security measure that prevents someone from creating their own SWF files that can attempt to stream your resources.

Samuel Pitoiset committed 14 years ago

Commit 635ac8e1 parent 661454a3 files · +73 −1Browse files

Unified · Split

3 files
  1. doc/protocols.texi +6 −0
  2. libavformat/rtmpproto.c +66 −0
  3. libavformat/version.h +1 −1
doc/protocols.texi+6−0
@@ -242,6 +242,12 @@ Name of live stream to subscribe to. By default no value will be sent. · 241 unmodified lines
242It is only sent if the option is specified or if rtmp_live242It is only sent if the option is specified or if rtmp_live
243is set to live.243is set to live.
244 244
245@item rtmp_swfhash
246SHA256 hash of the decompressed SWF file (32 bytes).
247
248@item rtmp_swfsize
249Size of the decompressed SWF file, required for SWFVerification.
250
245@item rtmp_swfurl251@item rtmp_swfurl
246URL of the SWF player for the media. By default no value will be sent.252URL of the SWF player for the media. By default no value will be sent.
247 253
libavformat/rtmpproto.c+66−0
@@ -91,7 +91,11 @@ typedef struct RTMPContext { · 90 unmodified lines
91 int nb_invokes; ///< keeps track of invoke messages91 int nb_invokes; ///< keeps track of invoke messages
92 char* tcurl; ///< url of the target stream92 char* tcurl; ///< url of the target stream
93 char* flashver; ///< version of the flash plugin93 char* flashver; ///< version of the flash plugin
94 char* swfhash; ///< SHA256 hash of the decompressed SWF file (32 bytes)
95 int swfhash_len; ///< length of the SHA256 hash
96 int swfsize; ///< size of the decompressed SWF file
94 char* swfurl; ///< url of the swf player97 char* swfurl; ///< url of the swf player
98 char swfverification[42]; ///< hash of the SWF verification
95 char* pageurl; ///< url of the web page99 char* pageurl; ///< url of the web page
96 char* subscribe; ///< name of live stream to subscribe100 char* subscribe; ///< name of live stream to subscribe
97 int server_bw; ///< server bandwidth101 int server_bw; ///< server bandwidth
@@ -592,6 +596,27 @@ static int gen_pong(URLContext *s, RTMPContext *rt, RTMPPacket *ppkt) · 494 unmodified lines
592 return rtmp_send_packet(rt, &pkt, 0);596 return rtmp_send_packet(rt, &pkt, 0);
593}597}
594 598
599/**
600 * Generate SWF verification message and send it to the server.
601 */
602static int gen_swf_verification(URLContext *s, RTMPContext *rt)
603{
604 RTMPPacket pkt;
605 uint8_t *p;
606 int ret;
607
608 av_log(s, AV_LOG_DEBUG, "Sending SWF verification...\n");
609 if ((ret = ff_rtmp_packet_create(&pkt, RTMP_NETWORK_CHANNEL, RTMP_PT_PING,
610 0, 44)) < 0)
611 return ret;
612
613 p = pkt.data;
614 bytestream_put_be16(&p, 27);
615 memcpy(p, rt->swfverification, 42);
616
617 return rtmp_send_packet(rt, &pkt, 0);
618}
619
595/**620/**
596 * Generate server bandwidth message and send it to the server.621 * Generate server bandwidth message and send it to the server.
597 */622 */
@@ -776,6 +801,30 @@ static int rtmp_validate_digest(uint8_t *buf, int off) · 178 unmodified lines
776 return 0;801 return 0;
777}802}
778 803
804static int rtmp_calc_swf_verification(URLContext *s, RTMPContext *rt,
805 uint8_t *buf)
806{
807 uint8_t *p;
808 int ret;
809
810 if (rt->swfhash_len != 32) {
811 av_log(s, AV_LOG_ERROR,
812 "Hash of the decompressed SWF file is not 32 bytes long.\n");
813 return AVERROR(EINVAL);
814 }
815
816 p = &rt->swfverification[0];
817 bytestream_put_byte(&p, 1);
818 bytestream_put_byte(&p, 1);
819 bytestream_put_be32(&p, rt->swfsize);
820 bytestream_put_be32(&p, rt->swfsize);
821
822 if ((ret = ff_rtmp_calc_digest(rt->swfhash, 32, 0, buf, 32, p)) < 0)
823 return ret;
824
825 return 0;
826}
827
779/**828/**
780 * Perform handshake with the server by means of exchanging pseudorandom data829 * Perform handshake with the server by means of exchanging pseudorandom data
781 * signed with HMAC-SHA2 digest.830 * signed with HMAC-SHA2 digest.
@@ -866,6 +915,14 @@ static int rtmp_handshake(URLContext *s, RTMPContext *rt) · 84 unmodified lines
866 }915 }
867 }916 }
868 917
918 /* Generate SWFVerification token (SHA256 HMAC hash of decompressed SWF,
919 * key are the last 32 bytes of the server handshake. */
920 if (rt->swfsize) {
921 if ((ret = rtmp_calc_swf_verification(s, rt, serverdata + 1 +
922 RTMP_HANDSHAKE_PACKET_SIZE - 32)) < 0)
923 return ret;
924 }
925
869 ret = ff_rtmp_calc_digest(tosend + 1 + client_pos, 32, 0,926 ret = ff_rtmp_calc_digest(tosend + 1 + client_pos, 32, 0,
870 rtmp_server_key, sizeof(rtmp_server_key),927 rtmp_server_key, sizeof(rtmp_server_key),
871 digest);928 digest);
@@ -1001,6 +1058,13 @@ static int handle_ping(URLContext *s, RTMPPacket *pkt) · 129 unmodified lines
1001 if (t == 6) {1058 if (t == 6) {
1002 if ((ret = gen_pong(s, rt, pkt)) < 0)1059 if ((ret = gen_pong(s, rt, pkt)) < 0)
1003 return ret;1060 return ret;
1061 } else if (t == 26) {
1062 if (rt->swfsize) {
1063 if ((ret = gen_swf_verification(s, rt)) < 0)
1064 return ret;
1065 } else {
1066 av_log(s, AV_LOG_WARNING, "Ignoring SWFVerification request.\n");
1067 }
1004 }1068 }
1005 1069
1006 return 0;1070 return 0;
@@ -1717,6 +1781,8 @@ static const AVOption rtmp_options[] = { · 710 unmodified lines
1717 {"rtmp_pageurl", "URL of the web page in which the media was embedded. By default no value will be sent.", OFFSET(pageurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},1781 {"rtmp_pageurl", "URL of the web page in which the media was embedded. By default no value will be sent.", OFFSET(pageurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
1718 {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},1782 {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
1719 {"rtmp_subscribe", "Name of live stream to subscribe to. Defaults to rtmp_playpath.", OFFSET(subscribe), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},1783 {"rtmp_subscribe", "Name of live stream to subscribe to. Defaults to rtmp_playpath.", OFFSET(subscribe), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
1784 {"rtmp_swfhash", "SHA256 hash of the decompressed SWF file (32 bytes).", OFFSET(swfhash), AV_OPT_TYPE_BINARY, .flags = DEC},
1785 {"rtmp_swfsize", "Size of the decompressed SWF file, required for SWFVerification.", OFFSET(swfsize), AV_OPT_TYPE_INT, {0}, 0, INT_MAX, DEC},
1720 {"rtmp_swfurl", "URL of the SWF player. By default no value will be sent", OFFSET(swfurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},1786 {"rtmp_swfurl", "URL of the SWF player. By default no value will be sent", OFFSET(swfurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
1721 {"rtmp_tcurl", "URL of the target stream. Defaults to proto://host[:port]/app.", OFFSET(tcurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},1787 {"rtmp_tcurl", "URL of the target stream. Defaults to proto://host[:port]/app.", OFFSET(tcurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
1722 { NULL },1788 { NULL },
libavformat/version.h+1−1
@@ -31,7 +31,7 @@ · 30 unmodified lines
31 31
32#define LIBAVFORMAT_VERSION_MAJOR 5432#define LIBAVFORMAT_VERSION_MAJOR 54
33#define LIBAVFORMAT_VERSION_MINOR 1333#define LIBAVFORMAT_VERSION_MINOR 13
34#define LIBAVFORMAT_VERSION_MICRO 234#define LIBAVFORMAT_VERSION_MICRO 3
35 35
36#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \36#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 LIBAVFORMAT_VERSION_MINOR, \37 LIBAVFORMAT_VERSION_MINOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t