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
242242 It is only sent if the option is specified or if rtmp_live
243243 is set to live.
244244
245+@item rtmp_swfhash
246+SHA256 hash of the decompressed SWF file (32 bytes).
247+
248+@item rtmp_swfsize
249+Size of the decompressed SWF file, required for SWFVerification.
250+
245251 @item rtmp_swfurl
246252 URL of the SWF player for the media. By default no value will be sent.
247253
libavformat/rtmpproto.c+66−0
@@ -91,7 +91,11 @@ typedef struct RTMPContext { · 90 unmodified lines
9191 int nb_invokes; ///< keeps track of invoke messages
9292 char* tcurl; ///< url of the target stream
9393 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
9497 char* swfurl; ///< url of the swf player
98+ char swfverification[42]; ///< hash of the SWF verification
9599 char* pageurl; ///< url of the web page
96100 char* subscribe; ///< name of live stream to subscribe
97101 int server_bw; ///< server bandwidth
@@ -592,6 +596,27 @@ static int gen_pong(URLContext *s, RTMPContext *rt, RTMPPacket *ppkt) · 494 unmodified lines
592596 return rtmp_send_packet(rt, &pkt, 0);
593597 }
594598
599+/**
600+ * Generate SWF verification message and send it to the server.
601+ */
602+static 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+
595620 /**
596621 * Generate server bandwidth message and send it to the server.
597622 */
@@ -776,6 +801,30 @@ static int rtmp_validate_digest(uint8_t *buf, int off) · 178 unmodified lines
776801 return 0;
777802 }
778803
804+static 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+
779828 /**
780829 * Perform handshake with the server by means of exchanging pseudorandom data
781830 * signed with HMAC-SHA2 digest.
@@ -866,6 +915,14 @@ static int rtmp_handshake(URLContext *s, RTMPContext *rt) · 84 unmodified lines
866915 }
867916 }
868917
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+
869926 ret = ff_rtmp_calc_digest(tosend + 1 + client_pos, 32, 0,
870927 rtmp_server_key, sizeof(rtmp_server_key),
871928 digest);
@@ -1001,6 +1058,13 @@ static int handle_ping(URLContext *s, RTMPPacket *pkt) · 129 unmodified lines
10011058 if (t == 6) {
10021059 if ((ret = gen_pong(s, rt, pkt)) < 0)
10031060 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+ }
10041068 }
10051069
10061070 return 0;
@@ -1717,6 +1781,8 @@ static const AVOption rtmp_options[] = { · 710 unmodified lines
17171781 {"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},
17181782 {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
17191783 {"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},
17201786 {"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},
17211787 {"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},
17221788 { NULL },
libavformat/version.h+1−1
@@ -31,7 +31,7 @@ · 30 unmodified lines
3131
3232 #define LIBAVFORMAT_VERSION_MAJOR 54
3333 #define LIBAVFORMAT_VERSION_MINOR 13
34−#define LIBAVFORMAT_VERSION_MICRO 2
34+#define LIBAVFORMAT_VERSION_MICRO 3
3535
3636 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
3737 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