'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.
cipher_verified: If True, the cipher\'s signature is not verified again.
api_version: The api version we should use.
remote_public_key: The public key of the source.
Returns:
An rdf_flows.GrrMessage.AuthorizationState.'

def VerifyMessageSignature(self, response_comms, signed_message_list, cipher, cipher_verified, api_version, remote_public_key):

    if ((not cipher_verified) and (not cipher.VerifyCipherSignature(remote_public_key))):
        stats.STATS.IncrementCounter('grr_unauthenticated_messages')
        return rdf_flows.GrrMessage.AuthorizationState.UNAUTHENTICATED
    try:
        client_id = cipher.cipher_metadata.source
        try:
            client = self.client_cache.Get(client_id)
        except KeyError:
            client = aff4.FACTORY.Create(client_id, aff4.AFF4Object.classes['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))
        remote_time = (client.Get(client.Schema.CLOCK) or 0)
        client_time = (signed_message_list.timestamp or 0)
        if (client_time < long((remote_time - rdfvalue.Duration('1h')))):
            logging.warning('Message    desynchronized    for    %s:    %s    >=    %s', client_id, long(remote_time), int(client_time))
            return rdf_flows.GrrMessage.AuthorizationState.DESYNCHRONIZED
        stats.STATS.IncrementCounter('grr_authenticated_messages')
        if (client_time > long(remote_time)):
            client.Set(client.Schema.CLOCK, rdfvalue.RDFDatetime(client_time))
            client.Set(client.Schema.PING, rdfvalue.RDFDatetime.Now())
            for label in client.Get(client.Schema.LABELS, []):
                stats.STATS.IncrementCounter('client_pings_by_label', fields=[label.name])
        else:
            logging.warning('Out    of    order    message    for    %s:    %s    >=    %s', client_id, long(remote_time), int(client_time))
        client.Flush(sync=False)
    except communicator.UnknownClientCert:
        pass
    return rdf_flows.GrrMessage.AuthorizationState.AUTHENTICATED
