title: Does TLS prevents replay attacks if the originator is compromised / intentionally wants to cheat
body: <p>Background: I'm working on a mobile application (online chat) that use persistent TCP connections to a backend server. In the previous version, we used a well-known encryption scheme to protect the payload and sign using a shared secret key. This provided sufficient protection against data tampering and reverse engineering. But the traffic was still susceptible to replay attacks: someone can capture the network traffic and replay it and server would accept the requests since it had no idea whether it was real or replayed. To counter this, <strong>we introduced timestamps and nonces which provided protection against replay attacks</strong>.</p>
  
  <p>All well and good. A decision was later made to <strong>use TLS (Transport Layer Security) to protect all communication</strong> between clients and servers. TLS was implemented and to provide an extra layer of protection against Man-in-the-Middle attacks, we pinned the public certificate of the server on clients.</p>
  
  <p>Which brings me to the question: <strong>is it still necessary to use custom encryption and guard against replay attacks since TLS already provides protection against these</strong>? In my research, I found that TLS provides protection during transmission i.e. prevents man in the middle but <strong>what if the originator itself wants to cheat the system and modify payload or replay traffic</strong>? Does TLS protect against an intentional cheater trying to somehow intercept the traffic in the network layer of their OS? My understanding was that TLS provides end-to-end encryption but I have a suspicion that TLS traffic could, in fact, be replayed if the originator wishes to. </p>
  
  <p>I'm also aware of memory-based attacks where the attacker would modify the memory on the device this modifying payload (may be even record and replay.) But even our custom encryption + replay guard won't protect against these. </p>
  
  <p>From: <a href="https://www.owasp.org/index.php/Transport_Layer_Protection_Cheat_Sheet#Benefits" rel="nofollow noreferrer">https://www.owasp.org/index.php/Transport_Layer_Protection_Cheat_Sheet#Benefits</a></p>
  
  <p>"TLS also provides two additional benefits that are commonly overlooked; integrity guarantees and replay prevention. A TLS stream of communication contains built-in controls to prevent tampering with any portion of the encrypted data. In addition, controls are also built-in to prevent a captured stream of TLS data from being replayed at a later time.</p>
  
  <p>It should be noted that TLS provides the above guarantees to data during transmission. TLS does not offer any of these security benefits to data that is at rest. Therefore appropriate security controls must be added to protect data while at rest within the application or within data stores."</p>