int rewriteResponseWithoutEDNS(const PacketBuffer& initialPacket, PacketBuffer& newContent)
{
- assert(initialPacket.size() >= sizeof(dnsheader));
+ if (initialPacket.size() < sizeof(dnsheader)) {
+ return ENOENT;
+ }
+
const dnsheader_aligned dnsHeader(initialPacket.data());
if (ntohs(dnsHeader->arcount) == 0) {
bool slowRewriteEDNSOptionInQueryWithRecords(const PacketBuffer& initialPacket, PacketBuffer& newContent, bool& ednsAdded, uint16_t optionToReplace, bool& optionAdded, bool overrideExisting, const string& newOptionContent)
{
- assert(initialPacket.size() >= sizeof(dnsheader));
+ if (initialPacket.size() < sizeof(dnsheader)) {
+ return false;
+ }
+
const dnsheader_aligned dnsHeader(initialPacket.data());
if (ntohs(dnsHeader->qdcount) == 0) {
int locateEDNSOptRR(const PacketBuffer& packet, uint16_t* optStart, size_t* optLen, bool* last)
{
- assert(optStart != nullptr);
- assert(optLen != nullptr);
- assert(last != nullptr);
+ if (optStart == nullptr || optLen == nullptr || last == nullptr) {
+ throw std::runtime_error("Invalid values passed to locateEDNSOptRR");
+ }
+
const dnsheader_aligned dnsHeader(packet.data());
if (ntohs(dnsHeader->arcount) == 0) {
/* extract the start of the OPT RR in a QUERY packet if any */
int getEDNSOptionsStart(const PacketBuffer& packet, const size_t offset, uint16_t* optRDPosition, size_t* remaining)
{
- assert(optRDPosition != nullptr);
- assert(remaining != nullptr);
+ if (optRDPosition == nullptr || remaining == nullptr) {
+ throw std::runtime_error("Invalid values passed to getEDNSOptionsStart");
+ }
+
const dnsheader_aligned dnsHeader(packet.data());
if (offset >= packet.size()) {
static bool replaceEDNSClientSubnetOption(PacketBuffer& packet, size_t maximumSize, size_t const oldEcsOptionStartPosition, size_t const oldEcsOptionSize, size_t const optRDLenPosition, const string& newECSOption)
{
- assert(oldEcsOptionStartPosition < packet.size());
- assert(optRDLenPosition < packet.size());
+ if (oldEcsOptionStartPosition >= packet.size() || optRDLenPosition >= packet.size()) {
+ throw std::runtime_error("Invalid values passed to replaceEDNSClientSubnetOption");
+ }
if (newECSOption.size() == oldEcsOptionSize) {
/* same size as the existing option */
bool handleEDNSClientSubnet(PacketBuffer& packet, const size_t maximumSize, const size_t qnameWireLength, bool& ednsAdded, bool& ecsAdded, bool overrideExisting, const string& newECSOption)
{
- assert(qnameWireLength <= packet.size());
+ if (qnameWireLength > packet.size()) {
+ throw std::runtime_error("Invalid value passed to handleEDNSClientSubnet");
+ }
const dnsheader_aligned dnsHeader(packet.data());
int rewriteResponseWithoutEDNSOption(const PacketBuffer& initialPacket, const uint16_t optionCodeToSkip, PacketBuffer& newContent)
{
- assert(initialPacket.size() >= sizeof(dnsheader));
+ if (initialPacket.size() < sizeof(dnsheader)) {
+ return ENOENT;
+ }
+
const dnsheader_aligned dnsHeader(initialPacket.data());
if (ntohs(dnsHeader->arcount) == 0) {
/* extract the position (relative to the optRR pointer!) and size of a specific EDNS0 option from a pointer on the beginning rdLen of the OPT RR */
int getEDNSOption(const char* optRR, const size_t len, uint16_t wantedOption, size_t* optionValuePosition, size_t * optionValueSize)
{
- assert(optRR != nullptr);
- assert(optionValuePosition != nullptr);
- assert(optionValueSize != nullptr);
+ if (optRR == nullptr || optionValuePosition == nullptr || optionValueSize == nullptr) {
+ return EINVAL;
+ }
+
size_t pos = 0;
- if (len < DNS_RDLENGTH_SIZE)
+ if (len < DNS_RDLENGTH_SIZE) {
return EINVAL;
+ }
const uint16_t rdLen = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
size_t rdPos = 0;
/* extract all EDNS0 options from a pointer on the beginning rdLen of the OPT RR */
int getEDNSOptions(const char* optRR, const size_t len, EDNSOptionViewMap& options)
{
- assert(optRR != nullptr);
size_t pos = 0;
- if (len < DNS_RDLENGTH_SIZE)
+ if (optRR == nullptr || len < DNS_RDLENGTH_SIZE) {
return EINVAL;
+ }
const uint16_t rdLen = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
size_t rdPos = 0;
const auto inSize = sizeof(address.sin6.sin6_addr.s6_addr);
static_assert(inSize == 16, "We disable padding and so we must assume a data size of 16 bytes");
const auto blockSize = EVP_CIPHER_get_block_size(aes128cbc.get());
- assert(blockSize == 16);
+ if (blockSize != 16) {
+ throw pdns::OpenSSL::error("encryptCA6: unexpected block size");
+ }
EVP_CIPHER_CTX_set_padding(ctx.get(), 0);
int updateLen = 0;
throw pdns::OpenSSL::error("encryptCA6: Could not finalize address encryption");
}
- assert(updateLen + finalLen == inSize);
+ if ((updateLen + finalLen) != inSize) {
+ throw pdns::OpenSSL::error("encryptCA6: unexpected final size");
+ }
#else
AES_KEY wctx;
AES_set_encrypt_key((const unsigned char*)key.c_str(), 128, &wctx);
const auto inSize = sizeof(address.sin6.sin6_addr.s6_addr);
static_assert(inSize == 16, "We disable padding and so we must assume a data size of 16 bytes");
const auto blockSize = EVP_CIPHER_get_block_size(aes128cbc.get());
- assert(blockSize == 16);
+ if (blockSize != 16) {
+ throw pdns::OpenSSL::error("decryptCA6: unexpected block size");
+ }
EVP_CIPHER_CTX_set_padding(ctx.get(), 0);
int updateLen = 0;
throw pdns::OpenSSL::error("decryptCA6: Could not finalize address decryption");
}
- assert(updateLen + finalLen == inSize);
+ if ((updateLen + finalLen) != inSize) {
+ throw pdns::OpenSSL::error("decryptCA6: unexpected final size");
+ }
#else
AES_KEY wctx;
AES_set_decrypt_key((const unsigned char*)key.c_str(), 128, &wctx);