From 3505d5574e1d87ab8af9ea38337bfa0a1ca6381d Mon Sep 17 00:00:00 2001 From: Samuel Pitoiset Date: Thu, 5 Jul 2012 13:05:46 +0200 Subject: rtmp: Rename rtmp_calc_digest to ff_rtmp_calc_digest and make it global MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- libavformat/rtmp.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'libavformat/rtmp.h') diff --git a/libavformat/rtmp.h b/libavformat/rtmp.h index f9d9900268..6a19f2e1e1 100644 --- a/libavformat/rtmp.h +++ b/libavformat/rtmp.h @@ -29,6 +29,9 @@ #define RTMP_HANDSHAKE_PACKET_SIZE 1536 +#define HMAC_IPAD_VAL 0x36 +#define HMAC_OPAD_VAL 0x5C + /** * emulated Flash client version - 9.0.124.2 on Linux * @{ @@ -40,4 +43,18 @@ #define RTMP_CLIENT_VER4 2 /** @} */ //version defines +/** + * Calculate HMAC-SHA2 digest for RTMP handshake packets. + * + * @param src input buffer + * @param len input buffer length (should be 1536) + * @param gap offset in buffer where 32 bytes should not be taken into account + * when calculating digest (since it will be used to store that digest) + * @param key digest key + * @param keylen digest key length + * @param dst buffer where calculated digest will be stored (32 bytes) + */ +int ff_rtmp_calc_digest(const uint8_t *src, int len, int gap, + const uint8_t *key, int keylen, uint8_t *dst); + #endif /* AVFORMAT_RTMP_H */ -- cgit v1.2.3 From 0e31088b6c57e7d495deda0abaf5de5adb2c18fa Mon Sep 17 00:00:00 2001 From: Samuel Pitoiset Date: Thu, 5 Jul 2012 13:06:07 +0200 Subject: rtmp: Add ff_rtmp_calc_digest_pos() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function is used for calculating digest position for RTMP handshake packets. Signed-off-by: Martin Storsjö --- libavformat/rtmp.h | 11 +++++++++++ libavformat/rtmpproto.c | 26 ++++++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'libavformat/rtmp.h') diff --git a/libavformat/rtmp.h b/libavformat/rtmp.h index 6a19f2e1e1..b9c5f1e430 100644 --- a/libavformat/rtmp.h +++ b/libavformat/rtmp.h @@ -57,4 +57,15 @@ int ff_rtmp_calc_digest(const uint8_t *src, int len, int gap, const uint8_t *key, int keylen, uint8_t *dst); +/** + * Calculate digest position for RTMP handshake packets. + * + * @param buf input buffer (should be 1536 bytes) + * @param off offset in buffer where to start calculating digest position + * @param mod_val value used for computing modulo + * @param add_val value added at the end (after computing modulo) + */ +int ff_rtmp_calc_digest_pos(const uint8_t *buf, int off, int mod_val, + int add_val); + #endif /* AVFORMAT_RTMP_H */ diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c index 42856dbf64..64704502e9 100644 --- a/libavformat/rtmpproto.c +++ b/libavformat/rtmpproto.c @@ -632,6 +632,18 @@ int ff_rtmp_calc_digest(const uint8_t *src, int len, int gap, return 0; } +int ff_rtmp_calc_digest_pos(const uint8_t *buf, int off, int mod_val, + int add_val) +{ + int i, digest_pos = 0; + + for (i = 0; i < 4; i++) + digest_pos += buf[i + off]; + digest_pos = digest_pos % mod_val + add_val; + + return digest_pos; +} + /** * Put HMAC-SHA2 digest of packet data (except for the bytes where this digest * will be stored) into that packet. @@ -641,12 +653,9 @@ int ff_rtmp_calc_digest(const uint8_t *src, int len, int gap, */ static int rtmp_handshake_imprint_with_digest(uint8_t *buf) { - int i, digest_pos = 0; - int ret; + int ret, digest_pos; - for (i = 8; i < 12; i++) - digest_pos += buf[i]; - digest_pos = (digest_pos % 728) + 12; + digest_pos = ff_rtmp_calc_digest_pos(buf, 8, 728, 12); ret = ff_rtmp_calc_digest(buf, RTMP_HANDSHAKE_PACKET_SIZE, digest_pos, rtmp_player_key, PLAYER_KEY_OPEN_PART_LEN, @@ -666,13 +675,10 @@ static int rtmp_handshake_imprint_with_digest(uint8_t *buf) */ static int rtmp_validate_digest(uint8_t *buf, int off) { - int i, digest_pos = 0; uint8_t digest[32]; - int ret; + int ret, digest_pos; - for (i = 0; i < 4; i++) - digest_pos += buf[i + off]; - digest_pos = (digest_pos % 728) + off + 4; + digest_pos = ff_rtmp_calc_digest_pos(buf, off, 728, off + 4); ret = ff_rtmp_calc_digest(buf, RTMP_HANDSHAKE_PACKET_SIZE, digest_pos, rtmp_server_key, SERVER_KEY_OPEN_PART_LEN, -- cgit v1.2.3