You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
11KB

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