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