]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2078] use virtual function to create TSIGContext
authorRazvan Becheriu <razvan@isc.org>
Thu, 9 Sep 2021 11:38:12 +0000 (14:38 +0300)
committerRazvan Becheriu <razvan@isc.org>
Fri, 10 Sep 2021 15:04:10 +0000 (18:04 +0300)
src/bin/d2/dns_client.cc
src/lib/d2srv/d2_tsig_key.cc
src/lib/d2srv/d2_tsig_key.h
src/lib/dns/tsig.cc
src/lib/dns/tsigkey.cc

index 77d3a09bd8c8b17ce19dd158451163e2622de5df..de929bc549abe505fac8a168fe041a5062a57f62 100644 (file)
@@ -207,7 +207,7 @@ DNSClientImpl::doUpdate(asiolink::IOService& io_service,
     // pointer.  Message marshalling uses non-null context is the indicator
     // that TSIG should be used.
     if (tsig_key) {
-        tsig_context_ = tsigContextFactory(tsig_key);
+        tsig_context_ = tsig_key->createContext();
         tsig_key_name_ = tsig_key->getKeyName().toText();
     } else {
         tsig_context_.reset();
index 57d05adaf578fa4e24fe1268c522ab6f201c5abc..196d278b041366ebb6ad4b03dd6161a800bceb92 100644 (file)
@@ -59,11 +59,9 @@ D2TsigKey::resetStats() {
 }
 
 TSIGContextPtr
-defaultTsigContextFactory(D2TsigKeyPtr tsig_key) {
-    return (TSIGContextPtr(new TSIGContext(*tsig_key)));
+D2TsigKey::createContext() {
+    return (TSIGContextPtr(new TSIGContext(*this)));
 }
 
-TSIGContextFactory tsigContextFactory = &defaultTsigContextFactory;
-
 } // namespace d2
 } // namespace isc
index 54efc47ebbc1b6e2b9310a780372bf589d8faebc..b5bb6358a17328d282ed1b3aa2447ad588f19936 100644 (file)
@@ -52,6 +52,13 @@ public:
     ///
     virtual void resetStats();
 
+    /// @brief Create TSIG context.
+    ///
+    /// @note Derived classes can implement their own specific context.
+    ///
+    /// @return The specific @ref TSIGContext of the @ref TSIGKey.
+    virtual dns::TSIGContextPtr createContext();
+
 private:
     /// @brief Initialize key statistics.
     void initStats();
@@ -60,18 +67,6 @@ private:
 /// @brief Type of pointer to a D2 TSIG key.
 typedef boost::shared_ptr<D2TsigKey> D2TsigKeyPtr;
 
-/// @brief TSIGContext factory type.
-typedef dns::TSIGContextPtr (*TSIGContextFactory)(D2TsigKeyPtr);
-
-/// @brief The TSIGContext factory.
-extern TSIGContextFactory tsigContextFactory;
-
-/// @brief Default TSIGContext factory.
-///
-/// @param tsig_key a D2 TSIG key.
-/// @return a pointer to a TSIG context.
-dns::TSIGContextPtr defaultTsigContextFactory(D2TsigKeyPtr tsig_key);
-
 } // namespace d2
 } // namespace isc
 
index 10bc51abbcbb178e0d8d65a68ba2c0bc2b423de8..eeaccef026ef41055129af352f842f4118f490b4 100644 (file)
@@ -56,8 +56,7 @@ struct TSIGContext::TSIGContextImpl {
                     TSIGError error = TSIGError::NOERROR()) :
         state_(INIT), key_(key), error_(error),
         previous_timesigned_(0), digest_len_(0),
-        last_sig_dist_(-1)
-    {
+        last_sig_dist_(-1) {
         if (error == TSIGError::NOERROR()) {
             // In normal (NOERROR) case, the key should be valid, and we
             // should be able to pre-create a corresponding HMAC object,
@@ -192,8 +191,7 @@ void
 TSIGContext::TSIGContextImpl::digestTSIGVariables(
     HMACPtr hmac, uint16_t rrclass, uint32_t rrttl, uint64_t time_signed,
     uint16_t fudge, uint16_t error, uint16_t otherlen, const void* otherdata,
-    bool time_variables_only) const
-{
+    bool time_variables_only) const {
     // It's bit complicated, but we can still predict the necessary size of
     // the data to be digested.  So we precompute it to avoid possible
     // reallocation inside OutputBuffer (not absolutely necessary, but this
@@ -240,11 +238,11 @@ TSIGContext::TSIGContextImpl::digestTSIGVariables(
 namespace {
 const size_t MESSAGE_HEADER_LEN = 12;
 }
+
 void
 TSIGContext::TSIGContextImpl::digestDNSMessage(HMACPtr hmac,
                                                uint16_t qid, const void* data,
-                                               size_t data_len) const
-{
+                                               size_t data_len) const {
     OutputBuffer buffer(MESSAGE_HEADER_LEN);
     const uint8_t* msgptr = static_cast<const uint8_t*>(data);
 
@@ -266,13 +264,11 @@ TSIGContext::TSIGContextImpl::digestDNSMessage(HMACPtr hmac,
     hmac->update(msgptr, data_len - MESSAGE_HEADER_LEN);
 }
 
-TSIGContext::TSIGContext(const TSIGKey& key) : impl_(new TSIGContextImpl(key))
-{
+TSIGContext::TSIGContext(const TSIGKey& key) : impl_(new TSIGContextImpl(key)) {
 }
 
 TSIGContext::TSIGContext(const Name& key_name, const Name& algorithm_name,
-                         const TSIGKeyRing& keyring) : impl_(NULL)
-{
+                         const TSIGKeyRing& keyring) : impl_(NULL) {
     const TSIGKeyRing::FindResult result(keyring.find(key_name,
                                                       algorithm_name));
     if (result.code == TSIGKeyRing::NOTFOUND) {
@@ -342,8 +338,7 @@ TSIGContext::getError() const {
 
 ConstTSIGRecordPtr
 TSIGContext::sign(const uint16_t qid, const void* const data,
-                  const size_t data_len)
-{
+                  const size_t data_len) {
     if (impl_->state_ == VERIFIED_RESPONSE) {
         isc_throw(TSIGContextError,
                   "TSIG sign attempt after verifying a response");
@@ -426,8 +421,7 @@ TSIGContext::sign(const uint16_t qid, const void* const data,
 
 TSIGError
 TSIGContext::verify(const TSIGRecord* const record, const void* const data,
-                    const size_t data_len)
-{
+                    const size_t data_len) {
     if (impl_->state_ == SENT_RESPONSE) {
         isc_throw(TSIGContextError,
                   "TSIG verify attempt after sending a response");
index 6f7421e55f9341cbb9b7ded45e6ab0c869b575a2..ad94b623cba9d98c1be43354ad94f6e02a30a821 100644 (file)
@@ -98,8 +98,7 @@ TSIGKey::TSIGKeyImpl {
 
 TSIGKey::TSIGKey(const Name& key_name, const Name& algorithm_name,
                  const void* secret, size_t secret_len,
-                 size_t digestbits /*= 0*/) : impl_(NULL)
-{
+                 size_t digestbits /*= 0*/) : impl_(NULL) {
     const HashAlgorithm algorithm = convertAlgorithmName(algorithm_name);
     if ((secret != NULL && secret_len == 0) ||
         (secret == NULL && secret_len != 0)) {
@@ -190,8 +189,8 @@ TSIGKey::TSIGKey(const std::string& str) : impl_(NULL) {
 }
 
 
-TSIGKey::TSIGKey(const TSIGKey& source) : impl_(new TSIGKeyImpl(*source.impl_))
-{}
+TSIGKey::TSIGKey(const TSIGKey& source) : impl_(new TSIGKeyImpl(*source.impl_)) {
+}
 
 TSIGKey&
 TSIGKey::operator=(const TSIGKey& source) {