From e3a1764d359a7e8ef616e0f1d1896c83eeef0c81 Mon Sep 17 00:00:00 2001 From: "amine.chbari.etu" <amine@DESKTOP-QK66G6C> Date: Fri, 5 Apr 2024 22:51:53 +0200 Subject: [PATCH] =?UTF-8?q?declarer=20une=20fonction=20encrypt=5Frtmpe()?= =?UTF-8?q?=20pour=20regrouper=20le=20code=20dupliqu=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/net/rtmp/InboundHandshake.java | 73 +++++++++---------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/server/src/main/java/org/red5/server/net/rtmp/InboundHandshake.java b/server/src/main/java/org/red5/server/net/rtmp/InboundHandshake.java index a0c5b618..373de97e 100644 --- a/server/src/main/java/org/red5/server/net/rtmp/InboundHandshake.java +++ b/server/src/main/java/org/red5/server/net/rtmp/InboundHandshake.java @@ -63,6 +63,35 @@ public class InboundHandshake extends RTMPHandshake { return decodeClientRequest1(in); } + + + + /** + * encrypts the response to the client request C1 + * this was duplicated code 176 - 195 and 241 - 259 from original file + * regroup it in one method + */ + public void encrypt_rtmpe(byte handshaketype, int DIGEST_LENGTH, byte[] signatureResp, byte[] digestresp){ + + switch (handshakeType) { + case RTMPConnection.RTMP_ENCRYPTED_XTEA: + log.debug("RTMPE type 8 XTEA"); + // encrypt signatureResp + for (int i = 0; i < DIGEST_LENGTH; i += 8) { + encryptXtea(signatureResp, i, digestresp[i] % 15); + } + break; + case RTMPConnection.RTMP_ENCRYPTED_BLOWFISH: + log.debug("RTMPE type 9 Blowfish"); + // encrypt signatureResp + for (int i = 0; i < DIGEST_LENGTH; i += 8) { + encryptBlowfish(signatureResp, i, digestresp[i] % 15); + } + break; + } + } + + /** * Decodes the first client request (C1) and returns a server response (S0S1). * @@ -173,26 +202,8 @@ public class InboundHandshake extends RTMPHandshake { calculateHMAC_SHA256(c1, 0, (Constants.HANDSHAKE_SIZE - DIGEST_LENGTH), digestResp, DIGEST_LENGTH, signatureResponse, 0); log.debug("Signature response: {}", Hex.encodeHexString(signatureResponse)); if (useEncryption()) { - switch (handshakeType) { - case RTMPConnection.RTMP_ENCRYPTED: - log.debug("RTMPE type 6"); - // we dont encrypt signatureResp for type 6 - break; - case RTMPConnection.RTMP_ENCRYPTED_XTEA: - log.debug("RTMPE type 8 XTEA"); - // encrypt signatureResp - for (int i = 0; i < DIGEST_LENGTH; i += 8) { - encryptXtea(signatureResponse, i, digestResp[i] % 15); - } - break; - case RTMPConnection.RTMP_ENCRYPTED_BLOWFISH: - log.debug("RTMPE type 9 Blowfish"); - // encrypt signatureResp - for (int i = 0; i < DIGEST_LENGTH; i += 8) { - encryptBlowfish(signatureResponse, i, digestResp[i] % 15); - } - break; - } + // replace the switch block with called function + encrypt_rtmpe(handshakeType, DIGEST_LENGTH, signatureResponse, digestResp); } // copy signature into C1 as S2 System.arraycopy(signatureResponse, 0, c1, (Constants.HANDSHAKE_SIZE - DIGEST_LENGTH), DIGEST_LENGTH); @@ -238,25 +249,9 @@ public class InboundHandshake extends RTMPHandshake { calculateHMAC_SHA256(s1, digestPosServer, DIGEST_LENGTH, GENUINE_FP_KEY, GENUINE_FP_KEY.length, digest, 0); calculateHMAC_SHA256(c2, 0, Constants.HANDSHAKE_SIZE - DIGEST_LENGTH, digest, DIGEST_LENGTH, signature, 0); if (useEncryption()) { - switch (handshakeType) { - case RTMPConnection.RTMP_ENCRYPTED: - log.debug("RTMPE type 6"); - break; - case RTMPConnection.RTMP_ENCRYPTED_XTEA: - log.debug("RTMPE type 8 XTEA"); - // encrypt signature - for (int i = 0; i < DIGEST_LENGTH; i += 8) { - encryptXtea(signature, i, digest[i] % 15); - } - break; - case RTMPConnection.RTMP_ENCRYPTED_BLOWFISH: - log.debug("RTMPE type 9 Blowfish"); - // encrypt signature - for (int i = 0; i < DIGEST_LENGTH; i += 8) { - encryptBlowfish(signature, i, digest[i] % 15); - } - break; - } + // replace the switch block with called function + encrypt_rtmpe(handshakeType, DIGEST_LENGTH, signature, digest); + // update 'encoder / decoder state' for the RC4 keys both parties *pretend* as if handshake part 2 (1536 bytes) was encrypted // effectively this hides / discards the first few bytes of encrypted session which is known to increase the secure-ness of RC4 // RC4 state is just a function of number of bytes processed so far that's why we just run 1536 arbitrary bytes through the keys below -- GitLab