/// <summary>Decrypts the data and then verifys it.</summary>
/// <param name="EncryptedData">The data to decrypt and verify.</param>
/// <returns>The verified and decrypted data.</returns>
public void DecryptAndVerify(SecurityDataMessage sdm)
{
if (_closed)
{
throw new Exception("SecurityHandler: closed");
}
else if (sdm.Epoch != Epoch)
{
throw new Exception(String.Format("Wrong index {0}, it should be {1}.",
sdm.Epoch, Epoch));
}
int seqid = sdm.Seqid;
// Verify the seqid
// If greater than current, new seqid and allow packet
// If less than current but within window, allow packet
// Else throw exception
if (UseWindow)
{
if (seqid == Int32.MaxValue)
{
Close();
throw new Exception("Maximum amount of packets sent over SecurityHandler.");
}
else if (seqid + WINDOW_SIZE < _last_incoming_seqid)
{
throw new Exception(String.Format("Invalid seqid: {0}, current seqid: {1}, window: {2}.",
seqid, _last_incoming_seqid, WINDOW_SIZE));
}
}
sdm.Decrypt(_decryptor);
if (!sdm.Verify(_incoming_auth))
{
throw new Exception("Invalid signature");
}
if (seqid > _last_incoming_seqid)
{
_last_incoming_seqid = seqid;
}
}