Coverage Report

Created: 2026-11-20 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pdns/pdns/dnsdistdist/dnsdist-ipcrypt2.cc
Line
Count
Source
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
23
#ifdef HAVE_IPCRYPT2
24
#include <memory>
25
#endif
26
#include <stdexcept>
27
#include <string>
28
#include <sys/socket.h>
29
30
#include "dnsdist-ipcrypt2.hh"
31
#ifdef HAVE_IPCRYPT2
32
#include "ipcrypt2.h"
33
#endif
34
#include "iputils.hh"
35
36
// ipcrypt2 namespace does not have to be dnsdist-specific
37
namespace pdns::ipcrypt2
38
{
39
IPCrypt2::IPCrypt2([[maybe_unused]] const IPCryptMethod& method, [[maybe_unused]] const std::string& key) :
40
0
  d_method(method)
41
0
{
42
0
#ifdef HAVE_IPCRYPT2
43
0
  switch (method) {
44
0
  case IPCryptMethod::pfx: {
45
0
    if (key.size() != IPCRYPT_PFX_KEYBYTES) {
46
0
      throw std::runtime_error("Key for IPCrypt PFX method is not " + std::to_string(IPCRYPT_PFX_KEYBYTES) + " bytes");
47
0
    }
48
0
    d_ipcryptCtxPfx = std::make_unique<IPCryptPFX>();
49
0
    auto ret = ipcrypt_pfx_init(d_ipcryptCtxPfx.get(), reinterpret_cast<const uint8_t*>(key.data()));
50
0
    if (ret != 0) {
51
0
      throw std::runtime_error("Could not initialize IPCrypt2 PFX context");
52
0
    }
53
0
  } break;
54
0
  default:
55
0
    throw std::runtime_error("Unsupported IPCrypt2 method");
56
0
    break;
57
0
  }
58
#else
59
  throw std::runtime_error("IPCrypt2 is not supported");
60
#endif
61
0
}
62
63
IPCrypt2::~IPCrypt2()
64
0
{
65
0
#ifdef HAVE_IPCRYPT2
66
0
  switch (d_method) {
67
0
  case IPCryptMethod::pfx:
68
0
    if (d_ipcryptCtxPfx != nullptr) {
69
0
      ipcrypt_pfx_deinit(d_ipcryptCtxPfx.get());
70
0
    }
71
0
    break;
72
0
  default:
73
0
    return;
74
0
  }
75
0
#endif
76
0
};
77
78
ComboAddress IPCrypt2::encrypt([[maybe_unused]] const ComboAddress& address) const // NOLINT(readability-convert-member-functions-to-static)
79
0
{
80
0
#ifdef HAVE_IPCRYPT2
81
0
  switch (d_method) {
82
0
  case IPCryptMethod::pfx: {
83
0
    uint8_t ip16[16];
84
0
    struct sockaddr_storage sa;
85
0
    if (address.isIPv4()) {
86
0
      std::memcpy(&sa, &address.sin4, sizeof(sockaddr_in));
87
0
    }
88
0
    else {
89
0
      std::memcpy(&sa, &address.sin6, sizeof(sockaddr_in6));
90
0
    }
91
0
    ipcrypt_sockaddr_to_ip16(ip16, reinterpret_cast<sockaddr*>(&sa));
92
0
    ipcrypt_pfx_encrypt_ip16(d_ipcryptCtxPfx.get(), ip16);
93
0
    ipcrypt_ip16_to_sockaddr(&sa, ip16);
94
0
    if (address.isIPv4()) {
95
0
      return ComboAddress(reinterpret_cast<sockaddr_in*>(&sa));
96
0
    }
97
0
    else {
98
0
      return ComboAddress(reinterpret_cast<sockaddr_in6*>(&sa));
99
0
    }
100
0
  } break;
101
0
  default:
102
0
    throw std::runtime_error("Unsupported method");
103
0
    break;
104
0
  }
105
#else
106
  throw std::runtime_error("IPCrypt2 is not supported");
107
#endif
108
0
}
109
}