2012-02-06 21:39:10 +01:00
|
|
|
#ifndef OPENVPN_CRYPTO_DECRYPT_H
|
|
|
|
#define OPENVPN_CRYPTO_DECRYPT_H
|
2011-10-06 02:18:46 +02:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <openvpn/common/types.hpp>
|
|
|
|
#include <openvpn/common/exception.hpp>
|
2012-01-24 02:54:35 +01:00
|
|
|
#include <openvpn/common/memcmp.hpp>
|
2011-10-06 02:18:46 +02:00
|
|
|
#include <openvpn/buffer/buffer.hpp>
|
2011-11-21 07:11:06 +01:00
|
|
|
#include <openvpn/random/prng.hpp>
|
2011-10-25 19:32:26 +02:00
|
|
|
#include <openvpn/frame/frame.hpp>
|
2011-10-06 02:18:46 +02:00
|
|
|
#include <openvpn/crypto/cipher.hpp>
|
|
|
|
#include <openvpn/crypto/digest.hpp>
|
|
|
|
#include <openvpn/crypto/static_key.hpp>
|
|
|
|
#include <openvpn/crypto/packet_id.hpp>
|
2012-02-04 11:24:54 +01:00
|
|
|
#include <openvpn/log/sessionstats.hpp>
|
2011-10-06 02:18:46 +02:00
|
|
|
|
|
|
|
namespace openvpn {
|
|
|
|
|
|
|
|
class Decrypt {
|
|
|
|
public:
|
|
|
|
OPENVPN_SIMPLE_EXCEPTION(unsupported_cipher_mode);
|
2011-12-11 09:28:55 +01:00
|
|
|
|
2012-02-04 11:24:54 +01:00
|
|
|
Error::Type decrypt(BufferAllocated& buf, const PacketID::time_t now)
|
2011-10-06 02:18:46 +02:00
|
|
|
{
|
2011-12-14 12:34:33 +01:00
|
|
|
// skip null packets
|
|
|
|
if (!buf.size())
|
2012-02-04 11:24:54 +01:00
|
|
|
return Error::SUCCESS;
|
2011-12-14 12:34:33 +01:00
|
|
|
|
|
|
|
// verify the HMAC
|
|
|
|
if (hmac.defined())
|
|
|
|
{
|
|
|
|
unsigned char local_hmac[HMACContext::MAX_HMAC_SIZE];
|
|
|
|
const size_t hmac_size = hmac.output_size();
|
|
|
|
const unsigned char *packet_hmac = buf.read_alloc(hmac_size);
|
|
|
|
hmac.hmac(local_hmac, hmac_size, buf.c_data(), buf.size());
|
2012-01-24 02:54:35 +01:00
|
|
|
if (memcmp_secure(local_hmac, packet_hmac, hmac_size))
|
2011-12-14 12:34:33 +01:00
|
|
|
{
|
|
|
|
buf.reset_size();
|
2012-02-04 11:24:54 +01:00
|
|
|
return Error::HMAC_ERROR;
|
2011-12-14 12:34:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt packet ID + payload
|
|
|
|
if (cipher.defined())
|
|
|
|
{
|
|
|
|
unsigned char iv_buf[CipherContext::MAX_IV_SIZE];
|
|
|
|
const size_t iv_size = cipher.iv_size();
|
|
|
|
|
|
|
|
// extract IV from head of packet
|
|
|
|
buf.read(iv_buf, iv_size);
|
|
|
|
|
|
|
|
// initialize work buffer
|
|
|
|
frame->prepare(Frame::DECRYPT_WORK, work);
|
|
|
|
|
|
|
|
// decrypt from buf -> work
|
|
|
|
const size_t decrypt_bytes = cipher.decrypt(iv_buf, work.data(), work.max_size(), buf.c_data(), buf.size());
|
|
|
|
if (!decrypt_bytes)
|
|
|
|
{
|
|
|
|
buf.reset_size();
|
2012-02-04 11:24:54 +01:00
|
|
|
return Error::DECRYPT_ERROR;
|
2011-12-14 12:34:33 +01:00
|
|
|
}
|
|
|
|
work.set_size(decrypt_bytes);
|
|
|
|
|
|
|
|
// handle different cipher modes
|
|
|
|
const int cipher_mode = cipher.cipher_mode();
|
|
|
|
if (cipher_mode == CipherContext::CIPH_CBC_MODE)
|
|
|
|
{
|
|
|
|
if (!verify_packet_id(work, now))
|
|
|
|
{
|
|
|
|
buf.reset_size();
|
2012-02-04 11:24:54 +01:00
|
|
|
return Error::REPLAY_ERROR;
|
2011-12-14 12:34:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw unsupported_cipher_mode();
|
|
|
|
}
|
|
|
|
|
|
|
|
// return cleartext result in buf
|
|
|
|
buf.swap(work);
|
|
|
|
}
|
|
|
|
else // no encryption
|
2011-10-06 02:18:46 +02:00
|
|
|
{
|
2011-12-14 12:34:33 +01:00
|
|
|
if (!verify_packet_id(buf, now))
|
|
|
|
{
|
|
|
|
buf.reset_size();
|
2012-02-04 11:24:54 +01:00
|
|
|
return Error::REPLAY_ERROR;
|
2011-12-14 12:34:33 +01:00
|
|
|
}
|
2011-10-06 02:18:46 +02:00
|
|
|
}
|
2012-02-04 11:24:54 +01:00
|
|
|
return Error::SUCCESS;
|
2011-10-06 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
2011-12-11 09:28:55 +01:00
|
|
|
Frame::Ptr frame;
|
2011-10-06 02:18:46 +02:00
|
|
|
CipherContext cipher;
|
|
|
|
HMACContext hmac;
|
|
|
|
PacketIDReceive pid_recv;
|
|
|
|
|
|
|
|
private:
|
2011-12-13 05:46:56 +01:00
|
|
|
bool verify_packet_id(BufferAllocated& buf, const PacketID::time_t now)
|
2011-10-06 02:18:46 +02:00
|
|
|
{
|
|
|
|
// ignore packet ID if pid_recv is not initialized
|
|
|
|
if (pid_recv.initialized())
|
|
|
|
{
|
|
|
|
const PacketID pid = pid_recv.read_next(buf);
|
2011-11-09 06:52:52 +01:00
|
|
|
if (pid_recv.test(pid, now)) // verify packet ID
|
|
|
|
pid_recv.add(pid, now); // remember packet ID
|
2011-10-06 02:18:46 +02:00
|
|
|
else
|
2011-12-13 05:46:56 +01:00
|
|
|
return false;
|
2011-10-06 02:18:46 +02:00
|
|
|
}
|
2011-12-13 05:46:56 +01:00
|
|
|
return true;
|
2011-10-06 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BufferAllocated work;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace openvpn
|
|
|
|
|
2012-02-06 21:39:10 +01:00
|
|
|
#endif // OPENVPN_CRYPTO_DECRYPT_H
|