1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <dns/exceptions.h>
#include <dns/messagerenderer.h>
#include <dns/rrclass.h>
#include <dns/rrttl.h>
#include <dns/tsigrecord.h>
#include <util/buffer.h>

#include <ostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::util;
using namespace isc::dns::rdata;

namespace {
// Internally used constants:

// Size in octets for the RR type, class TTL, RDLEN fields.
const size_t RR_COMMON_LEN = 10;

// Size in octets for the fixed part of TSIG RDATAs.
// - Time Signed (6)
// - Fudge (2)
// - MAC Size (2)
// - Original ID (2)
// - Error (2)
// - Other Len (2)
const size_t RDATA_COMMON_LEN = 16;
}

namespace isc {
namespace dns {
TSIGRecord::TSIGRecord(const Name& key_name,
                       const rdata::any::TSIG& tsig_rdata) :
    key_name_(key_name), rdata_(tsig_rdata),
    length_(RR_COMMON_LEN + RDATA_COMMON_LEN + key_name_.getLength() +
            rdata_.getAlgorithm().getLength() +
            rdata_.getMACSize() + rdata_.getOtherLen()) {
}

namespace {
// This is a straightforward wrapper of dynamic_cast<const any::TSIG&>.
// We use this so that we can throw the DNSMessageFORMERR exception when
// unexpected type of RDATA is detected in the member initialization list
// of the constructor below.
const any::TSIG&
castToTSIGRdata(const rdata::Rdata& rdata) {
    const any::TSIG* tsig_rdata =
        dynamic_cast<const any::TSIG*>(&rdata);
    if (!tsig_rdata) {
        isc_throw(DNSMessageFORMERR,
                  "TSIG record is being constructed from "
                  "incompatible RDATA: " << rdata.toText());
    }
    return (*tsig_rdata);
}
}

TSIGRecord::TSIGRecord(const Name& name, const RRClass& rrclass,
                       const RRTTL& ttl, const rdata::Rdata& rdata,
                       size_t length) :
    key_name_(name), rdata_(castToTSIGRdata(rdata)), length_(length) {
    if (rrclass != getClass()) {
        isc_throw(DNSMessageFORMERR, "Unexpected TSIG RR class: " << rrclass);
    }
    if (ttl != RRTTL(TSIG_TTL)) {
        isc_throw(DNSMessageFORMERR, "Unexpected TSIG TTL: " << ttl);
    }
}

const RRClass&
TSIGRecord::getClass() {
    return (RRClass::ANY());
}

const RRTTL&
TSIGRecord::getTTL() {
    static RRTTL ttl(TSIG_TTL);
    return (ttl);
}

namespace {
template <typename OUTPUT>
void
toWireCommon(OUTPUT& output, const rdata::any::TSIG& rdata) {
    // RR type, class, TTL are fixed constants.
    RRType::TSIG().toWire(output);
    TSIGRecord::getClass().toWire(output);
    output.writeUint32(TSIGRecord::TSIG_TTL);

    // RDLEN
    output.writeUint16(RDATA_COMMON_LEN + rdata.getAlgorithm().getLength() +
                       rdata.getMACSize() + rdata.getOtherLen());

    // TSIG RDATA
    rdata.toWire(output);
}
}

uint32_t
TSIGRecord::toWire(AbstractMessageRenderer& renderer) const {
    // If adding the TSIG would exceed the size limit, don't do it.
    if (renderer.getLength() + length_ > renderer.getLengthLimit()) {
        renderer.setTruncated();
        return (0);
    }

    // key name = owner.  note that we disable compression.
    renderer.writeName(key_name_, false);
    toWireCommon(renderer, rdata_);
    return (1);
}

uint32_t
TSIGRecord::toWire(OutputBuffer& buffer) const {
    key_name_.toWire(buffer);
    toWireCommon(buffer, rdata_);
    return (1);
}

std::string
TSIGRecord::toText() const {
    return (key_name_.toText() + " " + RRTTL(TSIG_TTL).toText() + " " +
            getClass().toText() + " " + RRType::TSIG().toText() + " " +
            rdata_.toText() + "\n");
}

std::ostream&
operator<<(std::ostream& os, const TSIGRecord& record) {
    return (os << record.toText());
}
} // namespace dns
} // namespace isc