def VerifyMessageSignature(self, response_comms, signed_message_list,
                             cipher, api_version):
    """Verifies the message list signature.

    In the server we check that the timestamp is later than the ping timestamp
    stored with the client. This ensures that client responses can not be
    replayed.

    Args:
       response_comms: The raw response_comms rdfvalue.
       signed_message_list: The SignedMessageList rdfvalue from the server.
       cipher: The cipher object that should be used to verify the message.
       api_version: The api version we should use.

    Returns:
       a rdf_flows.GrrMessage.AuthorizationState.
    """
    result = rdf_flows.GrrMessage.AuthorizationState.UNAUTHENTICATED
    try:
      if cipher.signature_verified:
        result = rdf_flows.GrrMessage.AuthorizationState.AUTHENTICATED

      if result == rdf_flows.GrrMessage.AuthorizationState.AUTHENTICATED:
        client_id = cipher.cipher_metadata.source
        try:
          client = self.client_cache.Get(client_id)
        except KeyError:
          client = aff4.FACTORY.Create(client_id,
                                       "VFSGRRClient", mode="rw",
                                       token=self.token)
          self.client_cache.Put(client_id, client)
          stats.STATS.SetGaugeValue("grr_frontendserver_client_cache_size",
                                    len(self.client_cache))

        ip = response_comms.orig_request.source_ip
        client.Set(client.Schema.CLIENT_IP(ip))

        # The very first packet we see from the client we do not have its clock
        remote_time = client.Get(client.Schema.CLOCK) or 0
        client_time = signed_message_list.timestamp or 0
        if client_time > long(remote_time):
          stats.STATS.IncrementCounter("grr_authenticated_messages")

          # Update the client and server timestamps.
          client.Set(client.Schema.CLOCK, rdfvalue.RDFDatetime(client_time))
          client.Set(client.Schema.PING, rdfvalue.RDFDatetime().Now())

        else:
          logging.debug("Message desynchronized for %s: %s >= %s",
                        client_id, long(remote_time), int(client_time))
          # This is likely an old message
          return rdf_flows.GrrMessage.AuthorizationState.DESYNCHRONIZED

        # If we are prepared to live with a slight risk of replay we can
        # remove this.
        client.Flush()

    except communicator.UnknownClientCert:
      pass

    if result != rdf_flows.GrrMessage.AuthorizationState.AUTHENTICATED:
      stats.STATS.IncrementCounter("grr_unauthenticated_messages")

    return result
dataset/ETHPy150Open google/grr/grr/lib/flow.py/ServerCommunicator.VerifyMessageSignature/original