Skip to content

Commit bbcd6b7

Browse files
author
evgeny-nadymov
committed
Add stub for p2p calls encryption
1 parent a85cf79 commit bbcd6b7

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

‎package-lock.json

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"homepage": "https://evgeny-nadymov.github.io/telegram-react",
33
"name": "telegram_react",
4-
"version": "0.0.991",
4+
"version": "0.0.992",
55
"private": true,
66
"dependencies": {
7-
"tdweb": "^1.7.2",
87
"@material-ui/core": "^4.9.7",
98
"@material-ui/icons": "^4.9.1",
109
"@material-ui/lab": "^4.0.0-alpha.46",
1110
"classnames": "^2.2.6",
11+
"crypto-js": "^4.0.0",
1212
"emoji-mart": "^3.0.0",
1313
"emoji-regex": "^8.0.0",
1414
"i18next": "^19.3.3",
@@ -22,7 +22,8 @@
2222
"react-i18next": "^11.3.4",
2323
"react-router-dom": "^5.0.0",
2424
"react-scripts": "^3.1.1",
25-
"react-transition-group": "^4.3.0"
25+
"react-transition-group": "^4.3.0",
26+
"tdweb": "^1.7.2"
2627
},
2728
"lint-staged": {
2829
"src/**/*.{js,jsx,json,css}": [

‎src/Calls/P2P/P2PEncryptor.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2018-present, Evgeny Nadymov
3+
*
4+
* This source code is licensed under the GPL v.3.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import CryptoJS from 'crypto-js';
9+
10+
const MTPROTO_ENCRYPTION = false;
11+
12+
export default class P2PEncryptor {
13+
constructor(key) {
14+
const p2pKey = CryptoJS.enc.Base64.parse(key);
15+
16+
this.key = CryptoJS.enc.Hex.parse('3132333435363738393031323334353641424344454647484940414243444546');
17+
this.iv = CryptoJS.enc.Hex.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f');
18+
this.mode = CryptoJS.mode.CTR;
19+
this.padding = CryptoJS.pad.NoPadding;
20+
}
21+
22+
encryptToBase64(str) {
23+
if (MTPROTO_ENCRYPTION) {
24+
const { key, iv, mode, padding } = this;
25+
26+
const encrypted = CryptoJS.AES.encrypt(str, key, {
27+
mode,
28+
iv,
29+
padding
30+
});
31+
32+
return encrypted.toString();
33+
} else {
34+
return btoa(str);
35+
}
36+
}
37+
38+
decryptFromBase64(base64) {
39+
if (MTPROTO_ENCRYPTION) {
40+
const { key, iv, mode, padding } = this;
41+
42+
const decrypted = CryptoJS.AES.decrypt(base64, key, {
43+
mode,
44+
iv,
45+
padding
46+
});
47+
48+
return decrypted.toString(CryptoJS.enc.Utf8);
49+
} else {
50+
return atob(base64);
51+
}
52+
}
53+
};

‎src/Stores/CallStore.js

+32-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import AppStore from './ApplicationStore';
1919
import LStore from './LocalizationStore';
2020
import UserStore from './UserStore';
2121
import TdLibController from '../Controllers/TdLibController';
22+
import P2PEncryptor from '../Calls/P2P/P2PEncryptor';
2223

2324
const JOIN_TRACKS = true;
2425
const UNIFY_SDP = true;
@@ -243,10 +244,18 @@ class CallStore extends EventEmitter {
243244
const { call_id, data } = update;
244245

245246
try {
246-
const signalingData = JSON.parse(atob(data));
247-
LOG_P2P_CALL('[update] updateNewCallSignalingData', update, signalingData);
248-
if (this.p2pCallsEnabled) {
249-
this.p2pApplyCallSignalingData(call_id, signalingData);
247+
const { currentCall } = this;
248+
if (currentCall) {
249+
const { encryptor } = currentCall;
250+
if (encryptor) {
251+
const decryptedData = encryptor.decryptFromBase64(data);
252+
const signalingData = JSON.parse(decryptedData);
253+
// const signalingData = JSON.parse(atob(data));
254+
LOG_P2P_CALL('[update] updateNewCallSignalingData', update, signalingData);
255+
if (this.p2pCallsEnabled) {
256+
this.p2pApplyCallSignalingData(call_id, signalingData);
257+
}
258+
}
250259
}
251260
} catch (e) {
252261
ERROR_P2P_CALL('[update] updateNewSignalingData parse', update);
@@ -1643,13 +1652,21 @@ class CallStore extends EventEmitter {
16431652
});
16441653
}
16451654

1646-
p2pSendCallSignalingData(callId, data) {
1647-
LOG_P2P_CALL('[tdlib] sendCallSignalingData', callId, data);
1648-
TdLibController.send({
1649-
'@type': 'sendCallSignalingData',
1650-
call_id: callId,
1651-
data: btoa(data)
1652-
});
1655+
p2pSendCallSignalingData(callId, str) {
1656+
LOG_P2P_CALL('[tdlib] sendCallSignalingData', callId, str);
1657+
const { currentCall } = this;
1658+
if (currentCall) {
1659+
const { encryptor } = currentCall;
1660+
if (encryptor) {
1661+
const data = encryptor.encryptToBase64(str);
1662+
1663+
TdLibController.send({
1664+
'@type': 'sendCallSignalingData',
1665+
call_id: callId,
1666+
data
1667+
});
1668+
}
1669+
}
16531670
}
16541671

16551672
p2pGetConfiguration(state){
@@ -1741,6 +1758,8 @@ class CallStore extends EventEmitter {
17411758
if (!state) return;
17421759
if (state['@type'] !== 'callStateReady') return;
17431760

1761+
const { encryption_key } = state;
1762+
17441763
const outputStream = new MediaStream();
17451764

17461765
const configuration = this.p2pGetConfiguration(state);
@@ -1783,7 +1802,8 @@ class CallStore extends EventEmitter {
17831802
callId: id,
17841803
connection,
17851804
inputStream: null,
1786-
outputStream
1805+
outputStream,
1806+
encryptor: new P2PEncryptor(encryption_key)
17871807
};
17881808
LOG_P2P_CALL('p2pJoinCall currentCall', this.currentCall);
17891809

0 commit comments

Comments
 (0)