rtmpcrypt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * RTMPE network protocol
  3. * Copyright (c) 2008-2009 Andrej Stepanchuk
  4. * Copyright (c) 2009-2010 Howard Chu
  5. * Copyright (c) 2012 Samuel Pitoiset
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * RTMPE protocol
  26. */
  27. #include "libavutil/blowfish.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/rc4.h"
  31. #include "libavutil/xtea.h"
  32. #include "internal.h"
  33. #include "rtmp.h"
  34. #include "rtmpdh.h"
  35. #include "rtmpcrypt.h"
  36. #include "url.h"
  37. /* protocol handler context */
  38. typedef struct RTMPEContext {
  39. const AVClass *class;
  40. URLContext *stream; ///< TCP stream
  41. FF_DH *dh; ///< Diffie-Hellman context
  42. struct AVRC4 key_in; ///< RC4 key used for decrypt data
  43. struct AVRC4 key_out; ///< RC4 key used for encrypt data
  44. int handshaked; ///< flag indicating when the handshake is performed
  45. int tunneling; ///< use a HTTP connection (RTMPTE)
  46. } RTMPEContext;
  47. static const uint8_t rtmpe8_keys[16][16] = {
  48. { 0xbf, 0xf0, 0x34, 0xb2, 0x11, 0xd9, 0x08, 0x1f,
  49. 0xcc, 0xdf, 0xb7, 0x95, 0x74, 0x8d, 0xe7, 0x32 },
  50. { 0x08, 0x6a, 0x5e, 0xb6, 0x17, 0x43, 0x09, 0x0e,
  51. 0x6e, 0xf0, 0x5a, 0xb8, 0xfe, 0x5a, 0x39, 0xe2 },
  52. { 0x7b, 0x10, 0x95, 0x6f, 0x76, 0xce, 0x05, 0x21,
  53. 0x23, 0x88, 0xa7, 0x3a, 0x44, 0x01, 0x49, 0xa1 },
  54. { 0xa9, 0x43, 0xf3, 0x17, 0xeb, 0xf1, 0x1b, 0xb2,
  55. 0xa6, 0x91, 0xa5, 0xee, 0x17, 0xf3, 0x63, 0x39 },
  56. { 0x7a, 0x30, 0xe0, 0x0a, 0xb5, 0x29, 0xe2, 0x2c,
  57. 0xa0, 0x87, 0xae, 0xa5, 0xc0, 0xcb, 0x79, 0xac },
  58. { 0xbd, 0xce, 0x0c, 0x23, 0x2f, 0xeb, 0xde, 0xff,
  59. 0x1c, 0xfa, 0xae, 0x16, 0x11, 0x23, 0x23, 0x9d },
  60. { 0x55, 0xdd, 0x3f, 0x7b, 0x77, 0xe7, 0xe6, 0x2e,
  61. 0x9b, 0xb8, 0xc4, 0x99, 0xc9, 0x48, 0x1e, 0xe4 },
  62. { 0x40, 0x7b, 0xb6, 0xb4, 0x71, 0xe8, 0x91, 0x36,
  63. 0xa7, 0xae, 0xbf, 0x55, 0xca, 0x33, 0xb8, 0x39 },
  64. { 0xfc, 0xf6, 0xbd, 0xc3, 0xb6, 0x3c, 0x36, 0x97,
  65. 0x7c, 0xe4, 0xf8, 0x25, 0x04, 0xd9, 0x59, 0xb2 },
  66. { 0x28, 0xe0, 0x91, 0xfd, 0x41, 0x95, 0x4c, 0x4c,
  67. 0x7f, 0xb7, 0xdb, 0x00, 0xe3, 0xa0, 0x66, 0xf8 },
  68. { 0x57, 0x84, 0x5b, 0x76, 0x4f, 0x25, 0x1b, 0x03,
  69. 0x46, 0xd4, 0x5b, 0xcd, 0xa2, 0xc3, 0x0d, 0x29 },
  70. { 0x0a, 0xcc, 0xee, 0xf8, 0xda, 0x55, 0xb5, 0x46,
  71. 0x03, 0x47, 0x34, 0x52, 0x58, 0x63, 0x71, 0x3b },
  72. { 0xb8, 0x20, 0x75, 0xdc, 0xa7, 0x5f, 0x1f, 0xee,
  73. 0xd8, 0x42, 0x68, 0xe8, 0xa7, 0x2a, 0x44, 0xcc },
  74. { 0x07, 0xcf, 0x6e, 0x9e, 0xa1, 0x6d, 0x7b, 0x25,
  75. 0x9f, 0xa7, 0xae, 0x6c, 0xd9, 0x2f, 0x56, 0x29 },
  76. { 0xfe, 0xb1, 0xea, 0xe4, 0x8c, 0x8c, 0x3c, 0xe1,
  77. 0x4e, 0x00, 0x64, 0xa7, 0x6a, 0x38, 0x7c, 0x2a },
  78. { 0x89, 0x3a, 0x94, 0x27, 0xcc, 0x30, 0x13, 0xa2,
  79. 0xf1, 0x06, 0x38, 0x5b, 0xa8, 0x29, 0xf9, 0x27 }
  80. };
  81. static const uint8_t rtmpe9_keys[16][24] = {
  82. { 0x79, 0x34, 0x77, 0x4c, 0x67, 0xd1, 0x38, 0x3a, 0xdf, 0xb3, 0x56, 0xbe,
  83. 0x8b, 0x7b, 0xd0, 0x24, 0x38, 0xe0, 0x73, 0x58, 0x41, 0x5d, 0x69, 0x67, },
  84. { 0x46, 0xf6, 0xb4, 0xcc, 0x01, 0x93, 0xe3, 0xa1, 0x9e, 0x7d, 0x3c, 0x65,
  85. 0x55, 0x86, 0xfd, 0x09, 0x8f, 0xf7, 0xb3, 0xc4, 0x6f, 0x41, 0xca, 0x5c, },
  86. { 0x1a, 0xe7, 0xe2, 0xf3, 0xf9, 0x14, 0x79, 0x94, 0xc0, 0xd3, 0x97, 0x43,
  87. 0x08, 0x7b, 0xb3, 0x84, 0x43, 0x2f, 0x9d, 0x84, 0x3f, 0x21, 0x01, 0x9b, },
  88. { 0xd3, 0xe3, 0x54, 0xb0, 0xf7, 0x1d, 0xf6, 0x2b, 0x5a, 0x43, 0x4d, 0x04,
  89. 0x83, 0x64, 0x3e, 0x0d, 0x59, 0x2f, 0x61, 0xcb, 0xb1, 0x6a, 0x59, 0x0d, },
  90. { 0xc8, 0xc1, 0xe9, 0xb8, 0x16, 0x56, 0x99, 0x21, 0x7b, 0x5b, 0x36, 0xb7,
  91. 0xb5, 0x9b, 0xdf, 0x06, 0x49, 0x2c, 0x97, 0xf5, 0x95, 0x48, 0x85, 0x7e, },
  92. { 0xeb, 0xe5, 0xe6, 0x2e, 0xa4, 0xba, 0xd4, 0x2c, 0xf2, 0x16, 0xe0, 0x8f,
  93. 0x66, 0x23, 0xa9, 0x43, 0x41, 0xce, 0x38, 0x14, 0x84, 0x95, 0x00, 0x53, },
  94. { 0x66, 0xdb, 0x90, 0xf0, 0x3b, 0x4f, 0xf5, 0x6f, 0xe4, 0x9c, 0x20, 0x89,
  95. 0x35, 0x5e, 0xd2, 0xb2, 0xc3, 0x9e, 0x9f, 0x7f, 0x63, 0xb2, 0x28, 0x81, },
  96. { 0xbb, 0x20, 0xac, 0xed, 0x2a, 0x04, 0x6a, 0x19, 0x94, 0x98, 0x9b, 0xc8,
  97. 0xff, 0xcd, 0x93, 0xef, 0xc6, 0x0d, 0x56, 0xa7, 0xeb, 0x13, 0xd9, 0x30, },
  98. { 0xbc, 0xf2, 0x43, 0x82, 0x09, 0x40, 0x8a, 0x87, 0x25, 0x43, 0x6d, 0xe6,
  99. 0xbb, 0xa4, 0xb9, 0x44, 0x58, 0x3f, 0x21, 0x7c, 0x99, 0xbb, 0x3f, 0x24, },
  100. { 0xec, 0x1a, 0xaa, 0xcd, 0xce, 0xbd, 0x53, 0x11, 0xd2, 0xfb, 0x83, 0xb6,
  101. 0xc3, 0xba, 0xab, 0x4f, 0x62, 0x79, 0xe8, 0x65, 0xa9, 0x92, 0x28, 0x76, },
  102. { 0xc6, 0x0c, 0x30, 0x03, 0x91, 0x18, 0x2d, 0x7b, 0x79, 0xda, 0xe1, 0xd5,
  103. 0x64, 0x77, 0x9a, 0x12, 0xc5, 0xb1, 0xd7, 0x91, 0x4f, 0x96, 0x4c, 0xa3, },
  104. { 0xd7, 0x7c, 0x2a, 0xbf, 0xa6, 0xe7, 0x85, 0x7c, 0x45, 0xad, 0xff, 0x12,
  105. 0x94, 0xd8, 0xde, 0xa4, 0x5c, 0x3d, 0x79, 0xa4, 0x44, 0x02, 0x5d, 0x22, },
  106. { 0x16, 0x19, 0x0d, 0x81, 0x6a, 0x4c, 0xc7, 0xf8, 0xb8, 0xf9, 0x4e, 0xcd,
  107. 0x2c, 0x9e, 0x90, 0x84, 0xb2, 0x08, 0x25, 0x60, 0xe1, 0x1e, 0xae, 0x18, },
  108. { 0xe9, 0x7c, 0x58, 0x26, 0x1b, 0x51, 0x9e, 0x49, 0x82, 0x60, 0x61, 0xfc,
  109. 0xa0, 0xa0, 0x1b, 0xcd, 0xf5, 0x05, 0xd6, 0xa6, 0x6d, 0x07, 0x88, 0xa3, },
  110. { 0x2b, 0x97, 0x11, 0x8b, 0xd9, 0x4e, 0xd9, 0xdf, 0x20, 0xe3, 0x9c, 0x10,
  111. 0xe6, 0xa1, 0x35, 0x21, 0x11, 0xf9, 0x13, 0x0d, 0x0b, 0x24, 0x65, 0xb2, },
  112. { 0x53, 0x6a, 0x4c, 0x54, 0xac, 0x8b, 0x9b, 0xb8, 0x97, 0x29, 0xfc, 0x60,
  113. 0x2c, 0x5b, 0x3a, 0x85, 0x68, 0xb5, 0xaa, 0x6a, 0x44, 0xcd, 0x3f, 0xa7, },
  114. };
  115. int ff_rtmpe_gen_pub_key(URLContext *h, uint8_t *buf)
  116. {
  117. RTMPEContext *rt = h->priv_data;
  118. int offset, ret;
  119. if (!(rt->dh = ff_dh_init(1024)))
  120. return AVERROR(ENOMEM);
  121. offset = ff_rtmp_calc_digest_pos(buf, 768, 632, 8);
  122. if (offset < 0)
  123. return offset;
  124. /* generate a Diffie-Hellmann public key */
  125. if ((ret = ff_dh_generate_public_key(rt->dh)) < 0)
  126. return ret;
  127. /* write the public key into the handshake buffer */
  128. if ((ret = ff_dh_write_public_key(rt->dh, buf + offset, 128)) < 0)
  129. return ret;
  130. return 0;
  131. }
  132. int ff_rtmpe_compute_secret_key(URLContext *h, const uint8_t *serverdata,
  133. const uint8_t *clientdata, int type)
  134. {
  135. RTMPEContext *rt = h->priv_data;
  136. uint8_t secret_key[128], digest[32];
  137. int server_pos, client_pos;
  138. int ret;
  139. if (type) {
  140. if ((server_pos = ff_rtmp_calc_digest_pos(serverdata, 1532, 632, 772)) < 0)
  141. return server_pos;
  142. } else {
  143. if ((server_pos = ff_rtmp_calc_digest_pos(serverdata, 768, 632, 8)) < 0)
  144. return server_pos;
  145. }
  146. if ((client_pos = ff_rtmp_calc_digest_pos(clientdata, 768, 632, 8)) < 0)
  147. return client_pos;
  148. /* compute the shared secret secret in order to compute RC4 keys */
  149. if ((ret = ff_dh_compute_shared_secret_key(rt->dh, serverdata + server_pos,
  150. 128, secret_key)) < 0)
  151. return ret;
  152. /* set output key */
  153. if ((ret = ff_rtmp_calc_digest(serverdata + server_pos, 128, 0, secret_key,
  154. 128, digest)) < 0)
  155. return ret;
  156. av_rc4_init(&rt->key_out, digest, 16 * 8, 1);
  157. /* set input key */
  158. if ((ret = ff_rtmp_calc_digest(clientdata + client_pos, 128, 0, secret_key,
  159. 128, digest)) < 0)
  160. return ret;
  161. av_rc4_init(&rt->key_in, digest, 16 * 8, 1);
  162. return 0;
  163. }
  164. static void rtmpe8_sig(const uint8_t *in, uint8_t *out, int key_id)
  165. {
  166. struct AVXTEA ctx;
  167. av_xtea_init(&ctx, rtmpe8_keys[key_id]);
  168. av_xtea_crypt(&ctx, out, in, 1, NULL, 0);
  169. }
  170. static void rtmpe9_sig(const uint8_t *in, uint8_t *out, int key_id)
  171. {
  172. struct AVBlowfish ctx;
  173. uint32_t xl, xr;
  174. xl = AV_RL32(in);
  175. xr = AV_RL32(in + 4);
  176. av_blowfish_init(&ctx, rtmpe9_keys[key_id], 24);
  177. av_blowfish_crypt_ecb(&ctx, &xl, &xr, 0);
  178. AV_WL32(out, xl);
  179. AV_WL32(out + 4, xr);
  180. }
  181. void ff_rtmpe_encrypt_sig(URLContext *h, uint8_t *sig, const uint8_t *digest,
  182. int type)
  183. {
  184. int i;
  185. for (i = 0; i < 32; i += 8) {
  186. if (type == 8) {
  187. /* RTMPE type 8 uses XTEA on the signature */
  188. rtmpe8_sig(sig + i, sig + i, digest[i] % 15);
  189. } else if (type == 9) {
  190. /* RTMPE type 9 uses Blowfish on the signature */
  191. rtmpe9_sig(sig + i, sig + i, digest[i] % 15);
  192. }
  193. }
  194. }
  195. int ff_rtmpe_update_keystream(URLContext *h)
  196. {
  197. RTMPEContext *rt = h->priv_data;
  198. char buf[RTMP_HANDSHAKE_PACKET_SIZE];
  199. /* skip past 1536 bytes of the RC4 bytestream */
  200. av_rc4_crypt(&rt->key_in, buf, NULL, sizeof(buf), NULL, 1);
  201. av_rc4_crypt(&rt->key_out, buf, NULL, sizeof(buf), NULL, 1);
  202. /* the next requests will be encrypted using RC4 keys */
  203. rt->handshaked = 1;
  204. return 0;
  205. }
  206. static int rtmpe_close(URLContext *h)
  207. {
  208. RTMPEContext *rt = h->priv_data;
  209. ff_dh_free(rt->dh);
  210. ffurl_close(rt->stream);
  211. return 0;
  212. }
  213. static int rtmpe_open(URLContext *h, const char *uri, int flags)
  214. {
  215. RTMPEContext *rt = h->priv_data;
  216. char host[256], url[1024];
  217. int ret, port;
  218. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri);
  219. if (rt->tunneling) {
  220. if (port < 0)
  221. port = 80;
  222. ff_url_join(url, sizeof(url), "ffrtmphttp", NULL, host, port, NULL);
  223. } else {
  224. if (port < 0)
  225. port = 1935;
  226. ff_url_join(url, sizeof(url), "tcp", NULL, host, port, NULL);
  227. }
  228. /* open the tcp or ffrtmphttp connection */
  229. if ((ret = ffurl_open(&rt->stream, url, AVIO_FLAG_READ_WRITE,
  230. &h->interrupt_callback, NULL)) < 0) {
  231. rtmpe_close(h);
  232. return ret;
  233. }
  234. return 0;
  235. }
  236. static int rtmpe_read(URLContext *h, uint8_t *buf, int size)
  237. {
  238. RTMPEContext *rt = h->priv_data;
  239. int ret;
  240. rt->stream->flags |= h->flags & AVIO_FLAG_NONBLOCK;
  241. ret = ffurl_read(rt->stream, buf, size);
  242. rt->stream->flags &= ~AVIO_FLAG_NONBLOCK;
  243. if (ret < 0 && ret != AVERROR_EOF)
  244. return ret;
  245. if (rt->handshaked && ret > 0) {
  246. /* decrypt data received by the server */
  247. av_rc4_crypt(&rt->key_in, buf, buf, ret, NULL, 1);
  248. }
  249. return ret;
  250. }
  251. static int rtmpe_write(URLContext *h, const uint8_t *buf, int size)
  252. {
  253. RTMPEContext *rt = h->priv_data;
  254. int ret;
  255. if (rt->handshaked) {
  256. /* encrypt data to send to the server */
  257. av_rc4_crypt(&rt->key_out, buf, buf, size, NULL, 1);
  258. }
  259. if ((ret = ffurl_write(rt->stream, buf, size)) < 0)
  260. return ret;
  261. return size;
  262. }
  263. #define OFFSET(x) offsetof(RTMPEContext, x)
  264. #define DEC AV_OPT_FLAG_DECODING_PARAM
  265. static const AVOption ffrtmpcrypt_options[] = {
  266. {"ffrtmpcrypt_tunneling", "Use a HTTP tunneling connection (RTMPTE).", OFFSET(tunneling), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC},
  267. { NULL },
  268. };
  269. static const AVClass ffrtmpcrypt_class = {
  270. .class_name = "ffrtmpcrypt",
  271. .item_name = av_default_item_name,
  272. .option = ffrtmpcrypt_options,
  273. .version = LIBAVUTIL_VERSION_INT,
  274. };
  275. URLProtocol ff_ffrtmpcrypt_protocol = {
  276. .name = "ffrtmpcrypt",
  277. .url_open = rtmpe_open,
  278. .url_read = rtmpe_read,
  279. .url_write = rtmpe_write,
  280. .url_close = rtmpe_close,
  281. .priv_data_size = sizeof(RTMPEContext),
  282. .flags = URL_PROTOCOL_FLAG_NETWORK,
  283. .priv_data_class = &ffrtmpcrypt_class,
  284. };