00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00029 #include "libavutil/blowfish.h"
00030 #include "libavutil/intreadwrite.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/rc4.h"
00033 #include "libavutil/xtea.h"
00034
00035 #include "internal.h"
00036 #include "rtmp.h"
00037 #include "rtmpdh.h"
00038 #include "rtmpcrypt.h"
00039 #include "url.h"
00040
00041
00042 typedef struct RTMPEContext {
00043 const AVClass *class;
00044 URLContext *stream;
00045 FF_DH *dh;
00046 struct AVRC4 key_in;
00047 struct AVRC4 key_out;
00048 int handshaked;
00049 int tunneling;
00050 } RTMPEContext;
00051
00052 static const uint8_t rtmpe8_keys[16][16] = {
00053 { 0xbf, 0xf0, 0x34, 0xb2, 0x11, 0xd9, 0x08, 0x1f,
00054 0xcc, 0xdf, 0xb7, 0x95, 0x74, 0x8d, 0xe7, 0x32 },
00055 { 0x08, 0x6a, 0x5e, 0xb6, 0x17, 0x43, 0x09, 0x0e,
00056 0x6e, 0xf0, 0x5a, 0xb8, 0xfe, 0x5a, 0x39, 0xe2 },
00057 { 0x7b, 0x10, 0x95, 0x6f, 0x76, 0xce, 0x05, 0x21,
00058 0x23, 0x88, 0xa7, 0x3a, 0x44, 0x01, 0x49, 0xa1 },
00059 { 0xa9, 0x43, 0xf3, 0x17, 0xeb, 0xf1, 0x1b, 0xb2,
00060 0xa6, 0x91, 0xa5, 0xee, 0x17, 0xf3, 0x63, 0x39 },
00061 { 0x7a, 0x30, 0xe0, 0x0a, 0xb5, 0x29, 0xe2, 0x2c,
00062 0xa0, 0x87, 0xae, 0xa5, 0xc0, 0xcb, 0x79, 0xac },
00063 { 0xbd, 0xce, 0x0c, 0x23, 0x2f, 0xeb, 0xde, 0xff,
00064 0x1c, 0xfa, 0xae, 0x16, 0x11, 0x23, 0x23, 0x9d },
00065 { 0x55, 0xdd, 0x3f, 0x7b, 0x77, 0xe7, 0xe6, 0x2e,
00066 0x9b, 0xb8, 0xc4, 0x99, 0xc9, 0x48, 0x1e, 0xe4 },
00067 { 0x40, 0x7b, 0xb6, 0xb4, 0x71, 0xe8, 0x91, 0x36,
00068 0xa7, 0xae, 0xbf, 0x55, 0xca, 0x33, 0xb8, 0x39 },
00069 { 0xfc, 0xf6, 0xbd, 0xc3, 0xb6, 0x3c, 0x36, 0x97,
00070 0x7c, 0xe4, 0xf8, 0x25, 0x04, 0xd9, 0x59, 0xb2 },
00071 { 0x28, 0xe0, 0x91, 0xfd, 0x41, 0x95, 0x4c, 0x4c,
00072 0x7f, 0xb7, 0xdb, 0x00, 0xe3, 0xa0, 0x66, 0xf8 },
00073 { 0x57, 0x84, 0x5b, 0x76, 0x4f, 0x25, 0x1b, 0x03,
00074 0x46, 0xd4, 0x5b, 0xcd, 0xa2, 0xc3, 0x0d, 0x29 },
00075 { 0x0a, 0xcc, 0xee, 0xf8, 0xda, 0x55, 0xb5, 0x46,
00076 0x03, 0x47, 0x34, 0x52, 0x58, 0x63, 0x71, 0x3b },
00077 { 0xb8, 0x20, 0x75, 0xdc, 0xa7, 0x5f, 0x1f, 0xee,
00078 0xd8, 0x42, 0x68, 0xe8, 0xa7, 0x2a, 0x44, 0xcc },
00079 { 0x07, 0xcf, 0x6e, 0x9e, 0xa1, 0x6d, 0x7b, 0x25,
00080 0x9f, 0xa7, 0xae, 0x6c, 0xd9, 0x2f, 0x56, 0x29 },
00081 { 0xfe, 0xb1, 0xea, 0xe4, 0x8c, 0x8c, 0x3c, 0xe1,
00082 0x4e, 0x00, 0x64, 0xa7, 0x6a, 0x38, 0x7c, 0x2a },
00083 { 0x89, 0x3a, 0x94, 0x27, 0xcc, 0x30, 0x13, 0xa2,
00084 0xf1, 0x06, 0x38, 0x5b, 0xa8, 0x29, 0xf9, 0x27 }
00085 };
00086
00087 static const uint8_t rtmpe9_keys[16][24] = {
00088 { 0x79, 0x34, 0x77, 0x4c, 0x67, 0xd1, 0x38, 0x3a, 0xdf, 0xb3, 0x56, 0xbe,
00089 0x8b, 0x7b, 0xd0, 0x24, 0x38, 0xe0, 0x73, 0x58, 0x41, 0x5d, 0x69, 0x67, },
00090 { 0x46, 0xf6, 0xb4, 0xcc, 0x01, 0x93, 0xe3, 0xa1, 0x9e, 0x7d, 0x3c, 0x65,
00091 0x55, 0x86, 0xfd, 0x09, 0x8f, 0xf7, 0xb3, 0xc4, 0x6f, 0x41, 0xca, 0x5c, },
00092 { 0x1a, 0xe7, 0xe2, 0xf3, 0xf9, 0x14, 0x79, 0x94, 0xc0, 0xd3, 0x97, 0x43,
00093 0x08, 0x7b, 0xb3, 0x84, 0x43, 0x2f, 0x9d, 0x84, 0x3f, 0x21, 0x01, 0x9b, },
00094 { 0xd3, 0xe3, 0x54, 0xb0, 0xf7, 0x1d, 0xf6, 0x2b, 0x5a, 0x43, 0x4d, 0x04,
00095 0x83, 0x64, 0x3e, 0x0d, 0x59, 0x2f, 0x61, 0xcb, 0xb1, 0x6a, 0x59, 0x0d, },
00096 { 0xc8, 0xc1, 0xe9, 0xb8, 0x16, 0x56, 0x99, 0x21, 0x7b, 0x5b, 0x36, 0xb7,
00097 0xb5, 0x9b, 0xdf, 0x06, 0x49, 0x2c, 0x97, 0xf5, 0x95, 0x48, 0x85, 0x7e, },
00098 { 0xeb, 0xe5, 0xe6, 0x2e, 0xa4, 0xba, 0xd4, 0x2c, 0xf2, 0x16, 0xe0, 0x8f,
00099 0x66, 0x23, 0xa9, 0x43, 0x41, 0xce, 0x38, 0x14, 0x84, 0x95, 0x00, 0x53, },
00100 { 0x66, 0xdb, 0x90, 0xf0, 0x3b, 0x4f, 0xf5, 0x6f, 0xe4, 0x9c, 0x20, 0x89,
00101 0x35, 0x5e, 0xd2, 0xb2, 0xc3, 0x9e, 0x9f, 0x7f, 0x63, 0xb2, 0x28, 0x81, },
00102 { 0xbb, 0x20, 0xac, 0xed, 0x2a, 0x04, 0x6a, 0x19, 0x94, 0x98, 0x9b, 0xc8,
00103 0xff, 0xcd, 0x93, 0xef, 0xc6, 0x0d, 0x56, 0xa7, 0xeb, 0x13, 0xd9, 0x30, },
00104 { 0xbc, 0xf2, 0x43, 0x82, 0x09, 0x40, 0x8a, 0x87, 0x25, 0x43, 0x6d, 0xe6,
00105 0xbb, 0xa4, 0xb9, 0x44, 0x58, 0x3f, 0x21, 0x7c, 0x99, 0xbb, 0x3f, 0x24, },
00106 { 0xec, 0x1a, 0xaa, 0xcd, 0xce, 0xbd, 0x53, 0x11, 0xd2, 0xfb, 0x83, 0xb6,
00107 0xc3, 0xba, 0xab, 0x4f, 0x62, 0x79, 0xe8, 0x65, 0xa9, 0x92, 0x28, 0x76, },
00108 { 0xc6, 0x0c, 0x30, 0x03, 0x91, 0x18, 0x2d, 0x7b, 0x79, 0xda, 0xe1, 0xd5,
00109 0x64, 0x77, 0x9a, 0x12, 0xc5, 0xb1, 0xd7, 0x91, 0x4f, 0x96, 0x4c, 0xa3, },
00110 { 0xd7, 0x7c, 0x2a, 0xbf, 0xa6, 0xe7, 0x85, 0x7c, 0x45, 0xad, 0xff, 0x12,
00111 0x94, 0xd8, 0xde, 0xa4, 0x5c, 0x3d, 0x79, 0xa4, 0x44, 0x02, 0x5d, 0x22, },
00112 { 0x16, 0x19, 0x0d, 0x81, 0x6a, 0x4c, 0xc7, 0xf8, 0xb8, 0xf9, 0x4e, 0xcd,
00113 0x2c, 0x9e, 0x90, 0x84, 0xb2, 0x08, 0x25, 0x60, 0xe1, 0x1e, 0xae, 0x18, },
00114 { 0xe9, 0x7c, 0x58, 0x26, 0x1b, 0x51, 0x9e, 0x49, 0x82, 0x60, 0x61, 0xfc,
00115 0xa0, 0xa0, 0x1b, 0xcd, 0xf5, 0x05, 0xd6, 0xa6, 0x6d, 0x07, 0x88, 0xa3, },
00116 { 0x2b, 0x97, 0x11, 0x8b, 0xd9, 0x4e, 0xd9, 0xdf, 0x20, 0xe3, 0x9c, 0x10,
00117 0xe6, 0xa1, 0x35, 0x21, 0x11, 0xf9, 0x13, 0x0d, 0x0b, 0x24, 0x65, 0xb2, },
00118 { 0x53, 0x6a, 0x4c, 0x54, 0xac, 0x8b, 0x9b, 0xb8, 0x97, 0x29, 0xfc, 0x60,
00119 0x2c, 0x5b, 0x3a, 0x85, 0x68, 0xb5, 0xaa, 0x6a, 0x44, 0xcd, 0x3f, 0xa7, },
00120 };
00121
00122 int ff_rtmpe_gen_pub_key(URLContext *h, uint8_t *buf)
00123 {
00124 RTMPEContext *rt = h->priv_data;
00125 int offset, ret;
00126
00127 if (!(rt->dh = ff_dh_init(1024)))
00128 return AVERROR(ENOMEM);
00129
00130 offset = ff_rtmp_calc_digest_pos(buf, 768, 632, 8);
00131 if (offset < 0)
00132 return offset;
00133
00134
00135 if ((ret = ff_dh_generate_public_key(rt->dh)) < 0)
00136 return ret;
00137
00138
00139 if ((ret = ff_dh_write_public_key(rt->dh, buf + offset, 128)) < 0)
00140 return ret;
00141
00142 return 0;
00143 }
00144
00145 int ff_rtmpe_compute_secret_key(URLContext *h, const uint8_t *serverdata,
00146 const uint8_t *clientdata, int type)
00147 {
00148 RTMPEContext *rt = h->priv_data;
00149 uint8_t secret_key[128], digest[32];
00150 int server_pos, client_pos;
00151 int ret;
00152
00153 if (type) {
00154 if ((server_pos = ff_rtmp_calc_digest_pos(serverdata, 1532, 632, 772)) < 0)
00155 return server_pos;
00156 } else {
00157 if ((server_pos = ff_rtmp_calc_digest_pos(serverdata, 768, 632, 8)) < 0)
00158 return server_pos;
00159 }
00160
00161 if ((client_pos = ff_rtmp_calc_digest_pos(clientdata, 768, 632, 8)) < 0)
00162 return client_pos;
00163
00164
00165 if ((ret = ff_dh_compute_shared_secret_key(rt->dh, serverdata + server_pos,
00166 128, secret_key)) < 0)
00167 return ret;
00168
00169
00170 if ((ret = ff_rtmp_calc_digest(serverdata + server_pos, 128, 0, secret_key,
00171 128, digest)) < 0)
00172 return ret;
00173 av_rc4_init(&rt->key_out, digest, 16 * 8, 1);
00174
00175
00176 if ((ret = ff_rtmp_calc_digest(clientdata + client_pos, 128, 0, secret_key,
00177 128, digest)) < 0)
00178 return ret;
00179 av_rc4_init(&rt->key_in, digest, 16 * 8, 1);
00180
00181 return 0;
00182 }
00183
00184 static void rtmpe8_sig(const uint8_t *in, uint8_t *out, int key_id)
00185 {
00186 struct AVXTEA ctx;
00187
00188 av_xtea_init(&ctx, rtmpe8_keys[key_id]);
00189 av_xtea_crypt(&ctx, out, in, 1, NULL, 0);
00190 }
00191
00192 static void rtmpe9_sig(const uint8_t *in, uint8_t *out, int key_id)
00193 {
00194 struct AVBlowfish ctx;
00195 uint32_t xl, xr;
00196
00197 xl = AV_RL32(in);
00198 xr = AV_RL32(in + 4);
00199
00200 av_blowfish_init(&ctx, rtmpe9_keys[key_id], 24);
00201 av_blowfish_crypt_ecb(&ctx, &xl, &xr, 0);
00202
00203 AV_WL32(out, xl);
00204 AV_WL32(out + 4, xr);
00205 }
00206
00207 void ff_rtmpe_encrypt_sig(URLContext *h, uint8_t *sig, const uint8_t *digest,
00208 int type)
00209 {
00210 int i;
00211
00212 for (i = 0; i < 32; i += 8) {
00213 if (type == 8) {
00214
00215 rtmpe8_sig(sig + i, sig + i, digest[i] % 15);
00216 } else if (type == 9) {
00217
00218 rtmpe9_sig(sig + i, sig + i, digest[i] % 15);
00219 }
00220 }
00221 }
00222
00223 int ff_rtmpe_update_keystream(URLContext *h)
00224 {
00225 RTMPEContext *rt = h->priv_data;
00226 char buf[RTMP_HANDSHAKE_PACKET_SIZE];
00227
00228
00229 av_rc4_crypt(&rt->key_in, buf, NULL, sizeof(buf), NULL, 1);
00230 av_rc4_crypt(&rt->key_out, buf, NULL, sizeof(buf), NULL, 1);
00231
00232
00233 rt->handshaked = 1;
00234
00235 return 0;
00236 }
00237
00238 static int rtmpe_close(URLContext *h)
00239 {
00240 RTMPEContext *rt = h->priv_data;
00241
00242 ff_dh_free(rt->dh);
00243 ffurl_close(rt->stream);
00244
00245 return 0;
00246 }
00247
00248 static int rtmpe_open(URLContext *h, const char *uri, int flags)
00249 {
00250 RTMPEContext *rt = h->priv_data;
00251 char host[256], url[1024];
00252 int ret, port;
00253
00254 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri);
00255
00256 if (rt->tunneling) {
00257 if (port < 0)
00258 port = 80;
00259 ff_url_join(url, sizeof(url), "ffrtmphttp", NULL, host, port, NULL);
00260 } else {
00261 if (port < 0)
00262 port = 1935;
00263 ff_url_join(url, sizeof(url), "tcp", NULL, host, port, NULL);
00264 }
00265
00266
00267 if ((ret = ffurl_open(&rt->stream, url, AVIO_FLAG_READ_WRITE,
00268 &h->interrupt_callback, NULL)) < 0) {
00269 rtmpe_close(h);
00270 return ret;
00271 }
00272
00273 return 0;
00274 }
00275
00276 static int rtmpe_read(URLContext *h, uint8_t *buf, int size)
00277 {
00278 RTMPEContext *rt = h->priv_data;
00279 int ret;
00280
00281 rt->stream->flags |= h->flags & AVIO_FLAG_NONBLOCK;
00282 ret = ffurl_read(rt->stream, buf, size);
00283 rt->stream->flags &= ~AVIO_FLAG_NONBLOCK;
00284
00285 if (ret < 0 && ret != AVERROR_EOF)
00286 return ret;
00287
00288 if (rt->handshaked && ret > 0) {
00289
00290 av_rc4_crypt(&rt->key_in, buf, buf, ret, NULL, 1);
00291 }
00292
00293 return ret;
00294 }
00295
00296 static int rtmpe_write(URLContext *h, const uint8_t *buf, int size)
00297 {
00298 RTMPEContext *rt = h->priv_data;
00299 int ret;
00300
00301 if (rt->handshaked) {
00302
00303 av_rc4_crypt(&rt->key_out, buf, buf, size, NULL, 1);
00304 }
00305
00306 if ((ret = ffurl_write(rt->stream, buf, size)) < 0)
00307 return ret;
00308
00309 return size;
00310 }
00311
00312 #define OFFSET(x) offsetof(RTMPEContext, x)
00313 #define DEC AV_OPT_FLAG_DECODING_PARAM
00314
00315 static const AVOption ffrtmpcrypt_options[] = {
00316 {"ffrtmpcrypt_tunneling", "Use a HTTP tunneling connection (RTMPTE).", OFFSET(tunneling), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC},
00317 { NULL },
00318 };
00319
00320 static const AVClass ffrtmpcrypt_class = {
00321 .class_name = "ffrtmpcrypt",
00322 .item_name = av_default_item_name,
00323 .option = ffrtmpcrypt_options,
00324 .version = LIBAVUTIL_VERSION_INT,
00325 };
00326
00327 URLProtocol ff_ffrtmpcrypt_protocol = {
00328 .name = "ffrtmpcrypt",
00329 .url_open = rtmpe_open,
00330 .url_read = rtmpe_read,
00331 .url_write = rtmpe_write,
00332 .url_close = rtmpe_close,
00333 .priv_data_size = sizeof(RTMPEContext),
00334 .flags = URL_PROTOCOL_FLAG_NETWORK,
00335 .priv_data_class = &ffrtmpcrypt_class,
00336 };