Coverage Report

Created: 2026-03-07 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pdns/pdns/dnsdistdist/dnscrypt.hh
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
#pragma once
23
#include "config.h"
24
#include <memory>
25
26
#ifndef HAVE_DNSCRYPT
27
28
/* let's just define a few types and values so that the rest of
29
   the code can ignore whether DNSCrypt support is available */
30
#define DNSCRYPT_MAX_RESPONSE_PADDING_AND_MAC_SIZE (0)
31
32
class DNSCryptContext
33
{
34
};
35
36
class DNSCryptQuery
37
{
38
  DNSCryptQuery(const std::shared_ptr<DNSCryptContext>& ctx) :
39
    d_ctx(ctx)
40
0
  {
41
0
  }
42
43
private:
44
  std::shared_ptr<DNSCryptContext> d_ctx{nullptr};
45
};
46
47
#else /* HAVE_DNSCRYPT */
48
49
#include <string>
50
#include <vector>
51
#include <arpa/inet.h>
52
53
#include <sodium.h>
54
55
#include "dnsname.hh"
56
#include "lock.hh"
57
#include "noinitvector.hh"
58
59
#define DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE (crypto_sign_ed25519_PUBLICKEYBYTES)
60
#define DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE (crypto_sign_ed25519_SECRETKEYBYTES)
61
#define DNSCRYPT_SIGNATURE_SIZE (crypto_sign_ed25519_BYTES)
62
63
#define DNSCRYPT_PUBLIC_KEY_SIZE (crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES)
64
#define DNSCRYPT_PRIVATE_KEY_SIZE (crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES)
65
#define DNSCRYPT_NONCE_SIZE (crypto_box_curve25519xsalsa20poly1305_NONCEBYTES)
66
#define DNSCRYPT_BEFORENM_SIZE (crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES)
67
#define DNSCRYPT_MAC_SIZE (crypto_box_curve25519xsalsa20poly1305_MACBYTES)
68
69
#ifdef HAVE_CRYPTO_BOX_CURVE25519XCHACHA20POLY1305_EASY
70
static_assert(crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES == crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES, "DNSCrypt public key size should be the same for all exchange versions");
71
static_assert(crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES == crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES, "DNSCrypt private key size should be the same for all exchange versions");
72
static_assert(crypto_box_curve25519xchacha20poly1305_NONCEBYTES == crypto_box_curve25519xsalsa20poly1305_NONCEBYTES, "DNSCrypt nonce size should be the same for all exchange versions");
73
static_assert(crypto_box_curve25519xsalsa20poly1305_MACBYTES == crypto_box_curve25519xchacha20poly1305_MACBYTES, "DNSCrypt MAC size should be the same for all exchange versions");
74
static_assert(crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES == crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES, "DNSCrypt BEFORENM size should be the same for all exchange versions");
75
#endif /* HAVE_CRYPTO_BOX_CURVE25519XCHACHA20POLY1305_EASY */
76
77
#define DNSCRYPT_CERT_MAGIC_SIZE (4)
78
#define DNSCRYPT_CERT_MAGIC_VALUE \
79
  {0x44, 0x4e, 0x53, 0x43}
80
#define DNSCRYPT_CERT_PROTOCOL_MINOR_VERSION_VALUE \
81
  {0x00, 0x00}
82
#define DNSCRYPT_CLIENT_MAGIC_SIZE (8)
83
#define DNSCRYPT_RESOLVER_MAGIC \
84
  {0x72, 0x36, 0x66, 0x6e, 0x76, 0x57, 0x6a, 0x38}
85
#define DNSCRYPT_RESOLVER_MAGIC_SIZE (8)
86
#define DNSCRYPT_PADDED_BLOCK_SIZE (64)
87
#define DNSCRYPT_MAX_TCP_PADDING_SIZE (256)
88
#define DNSCRYPT_MAX_RESPONSE_PADDING_SIZE (256)
89
#define DNSCRYPT_MAX_RESPONSE_PADDING_AND_MAC_SIZE (DNSCRYPT_MAX_RESPONSE_PADDING_SIZE + DNSCRYPT_MAC_SIZE)
90
91
/* "The client must check for new certificates every hour", so let's use one hour TTL */
92
#define DNSCRYPT_CERTIFICATE_RESPONSE_TTL (3600)
93
94
static_assert(DNSCRYPT_CLIENT_MAGIC_SIZE <= DNSCRYPT_PUBLIC_KEY_SIZE, "DNSCrypt Client Nonce size should be smaller or equal to public key size.");
95
96
#define DNSCRYPT_CERT_ES_VERSION1_VALUE \
97
  {0x00, 0x01}
98
#define DNSCRYPT_CERT_ES_VERSION2_VALUE \
99
  {0x00, 0x02}
100
101
class DNSCryptContext;
102
103
struct DNSCryptCertSignedData
104
{
105
  using ResolverPublicKeyType = std::array<unsigned char, DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE>;
106
  using ResolverPrivateKeyType = std::array<unsigned char, DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE>;
107
  using ClientMagicType = std::array<unsigned char, DNSCRYPT_CLIENT_MAGIC_SIZE>;
108
  ResolverPublicKeyType resolverPK{};
109
  ClientMagicType clientMagic{};
110
  uint32_t serial{0};
111
  uint32_t tsStart{0};
112
  uint32_t tsEnd{0};
113
};
114
115
static_assert(sizeof(DNSCryptCertSignedData) == (DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE + DNSCRYPT_CLIENT_MAGIC_SIZE + 12));
116
static_assert(std::is_trivially_copyable_v<DNSCryptCertSignedData> == true);
117
118
class DNSCryptCert
119
{
120
public:
121
  uint32_t getSerial() const
122
  {
123
    return ntohl(signedData.serial);
124
  }
125
  uint32_t getTSStart() const
126
  {
127
    return signedData.tsStart;
128
  }
129
  uint32_t getTSEnd() const
130
  {
131
    return signedData.tsEnd;
132
  }
133
  bool isValid(time_t now) const
134
  {
135
    // coverity[store_truncates_time_t]
136
    return ntohl(getTSStart()) <= static_cast<uint32_t>(now) && static_cast<uint32_t>(now) <= ntohl(getTSEnd());
137
  }
138
  using ESVersionType = std::array<unsigned char, 2>;
139
  using ProtocolMinorVersionType = std::array<unsigned char, 2>;
140
  using CertMagicType = std::array<unsigned char, DNSCRYPT_CERT_MAGIC_SIZE>;
141
  CertMagicType magic{};
142
  ESVersionType esVersion{};
143
  ProtocolMinorVersionType protocolMinorVersion{};
144
  std::array<unsigned char, DNSCRYPT_SIGNATURE_SIZE> signature{};
145
  DNSCryptCertSignedData signedData;
146
};
147
148
static_assert((sizeof(DNSCryptCertSignedData) + DNSCRYPT_SIGNATURE_SIZE) == 116, "Dnscrypt cert signed data size + signature size should be 116!");
149
static_assert(sizeof(DNSCryptCert) == 124, "Dnscrypt cert size should be 124!");
150
151
using DNSCryptClientNonceType = std::array<unsigned char, DNSCRYPT_NONCE_SIZE / 2>;
152
using DNSCryptNonceType = std::array<unsigned char, DNSCRYPT_NONCE_SIZE>;
153
using DNSCryptPublicKeyType = std::array<unsigned char, DNSCRYPT_PUBLIC_KEY_SIZE>;
154
using DNSCryptClientMagicType = std::array<unsigned char, DNSCRYPT_CLIENT_MAGIC_SIZE>;
155
156
struct DNSCryptQueryHeader
157
{
158
  DNSCryptClientMagicType clientMagic;
159
  DNSCryptPublicKeyType clientPK;
160
  DNSCryptClientNonceType clientNonce;
161
};
162
163
static_assert(sizeof(DNSCryptQueryHeader) == 52, "Dnscrypt query header size should be 52!");
164
static_assert(std::is_trivially_copyable_v<DNSCryptQueryHeader> == true);
165
166
struct DNSCryptResponseHeader
167
{
168
  // a const std::array is not trivially copyable, unfortunately
169
  const unsigned char resolverMagic[DNSCRYPT_RESOLVER_MAGIC_SIZE] = DNSCRYPT_RESOLVER_MAGIC;
170
  DNSCryptNonceType nonce;
171
};
172
173
static_assert(sizeof(DNSCryptResponseHeader) == (DNSCRYPT_RESOLVER_MAGIC_SIZE + DNSCRYPT_NONCE_SIZE), "Dnscrypt response header size is incorrect!");
174
static_assert(std::is_trivially_copyable_v<DNSCryptResponseHeader> == true);
175
176
typedef enum
177
{
178
  VERSION1,
179
  VERSION2
180
} DNSCryptExchangeVersion;
181
182
class DNSCryptPrivateKey
183
{
184
public:
185
  DNSCryptPrivateKey();
186
  ~DNSCryptPrivateKey();
187
  void loadFromFile(const std::string& keyFile);
188
  void saveToFile(const std::string& keyFile) const;
189
190
  using PrivateKeyType = std::array<unsigned char, DNSCRYPT_PRIVATE_KEY_SIZE>;
191
  PrivateKeyType key{};
192
};
193
194
struct DNSCryptCertificatePair
195
{
196
  using PublicKeyType = std::array<unsigned char, DNSCRYPT_PUBLIC_KEY_SIZE>;
197
  PublicKeyType publicKey;
198
  DNSCryptCert cert;
199
  DNSCryptPrivateKey privateKey;
200
  bool active{false};
201
};
202
203
class DNSCryptQuery
204
{
205
public:
206
  DNSCryptQuery(const std::shared_ptr<DNSCryptContext>& ctx) :
207
    d_ctx(ctx)
208
  {
209
    memset(&d_header, 0, sizeof(d_header));
210
#ifdef HAVE_CRYPTO_BOX_EASY_AFTERNM
211
    memset(&d_sharedKey, 0, sizeof(d_sharedKey));
212
#endif /* HAVE_CRYPTO_BOX_EASY_AFTERNM */
213
  }
214
215
  ~DNSCryptQuery();
216
217
  bool isValid() const
218
  {
219
    return d_valid;
220
  }
221
222
  const DNSName& getQName() const
223
  {
224
    return d_qname;
225
  }
226
227
  uint16_t getID() const
228
  {
229
    return d_id;
230
  }
231
232
  const DNSCryptClientMagicType& getClientMagic() const
233
  {
234
    return d_header.clientMagic;
235
  }
236
237
  bool isEncrypted() const
238
  {
239
    return d_encrypted;
240
  }
241
242
  void setCertificatePair(const std::shared_ptr<DNSCryptCertificatePair>& pair)
243
  {
244
    d_pair = pair;
245
  }
246
247
  void parsePacket(PacketBuffer& packet, bool tcp, time_t now);
248
  void getDecrypted(bool tcp, PacketBuffer& packet);
249
  void getCertificateResponse(time_t now, PacketBuffer& response) const;
250
  int encryptResponse(PacketBuffer& response, size_t maxResponseSize, bool tcp);
251
252
  static constexpr size_t s_minUDPLength = 256;
253
254
private:
255
  static void fillServerNonce(DNSCryptNonceType& nonce);
256
257
  DNSCryptExchangeVersion getVersion() const;
258
#ifdef HAVE_CRYPTO_BOX_EASY_AFTERNM
259
  int computeSharedKey();
260
#endif /* HAVE_CRYPTO_BOX_EASY_AFTERNM */
261
  uint16_t computePaddingSize(uint16_t unpaddedLen, size_t maxLen) const;
262
  bool parsePlaintextQuery(const PacketBuffer& packet);
263
  bool isEncryptedQuery(const PacketBuffer& packet, bool tcp, time_t now);
264
265
  DNSCryptQueryHeader d_header;
266
#ifdef HAVE_CRYPTO_BOX_EASY_AFTERNM
267
  std::array<unsigned char, crypto_box_BEFORENMBYTES> d_sharedKey;
268
#endif /* HAVE_CRYPTO_BOX_EASY_AFTERNM */
269
  DNSName d_qname;
270
  std::shared_ptr<DNSCryptContext> d_ctx{nullptr};
271
  std::shared_ptr<DNSCryptCertificatePair> d_pair{nullptr};
272
  uint16_t d_id{0};
273
  uint16_t d_len{0};
274
  uint16_t d_paddedLen{0};
275
  bool d_encrypted{false};
276
  bool d_valid{false};
277
278
#ifdef HAVE_CRYPTO_BOX_EASY_AFTERNM
279
  bool d_sharedKeyComputed{false};
280
#endif /* HAVE_CRYPTO_BOX_EASY_AFTERNM */
281
};
282
283
class DNSCryptContext
284
{
285
public:
286
  static void generateProviderKeys(DNSCryptCertSignedData::ResolverPublicKeyType& publicKey, DNSCryptCertSignedData::ResolverPrivateKeyType& privateKey);
287
  static std::string getProviderFingerprint(const DNSCryptCertSignedData::ResolverPublicKeyType& publicKey);
288
  static void generateCertificate(uint32_t serial, time_t begin, time_t end, const DNSCryptExchangeVersion& version, const DNSCryptCertSignedData::ResolverPrivateKeyType& providerPrivateKey, DNSCryptPrivateKey& privateKey, DNSCryptCert& cert);
289
  static void saveCertFromFile(const DNSCryptCert& cert, const std::string& filename);
290
  static std::string certificateDateToStr(uint32_t date);
291
  static void generateResolverKeyPair(DNSCryptPrivateKey& privK, DNSCryptPublicKeyType& pubK);
292
  static void setExchangeVersion(const DNSCryptExchangeVersion& version, DNSCryptCert::ESVersionType& esVersion);
293
  static DNSCryptExchangeVersion getExchangeVersion(const DNSCryptCert::ESVersionType& esVersion);
294
  static DNSCryptExchangeVersion getExchangeVersion(const DNSCryptCert& cert);
295
  static int encryptQuery(PacketBuffer& packet, size_t maximumSize, const DNSCryptCertificatePair::PublicKeyType& clientPublicKey, const DNSCryptPrivateKey& clientPrivateKey, const DNSCryptClientNonceType& clientNonce, bool tcp, const std::shared_ptr<DNSCryptCert>& cert);
296
297
  struct CertKeyPaths
298
  {
299
    std::string cert;
300
    std::string key;
301
  };
302
303
  DNSCryptContext(const std::string& pName, const std::vector<CertKeyPaths>& certKeys);
304
  DNSCryptContext(const std::string& pName, const DNSCryptCert& certificate, const DNSCryptPrivateKey& pKey);
305
  ~DNSCryptContext();
306
307
  void reloadCertificates();
308
  void loadNewCertificate(const std::string& certFile, const std::string& keyFile, bool active = true, bool reload = false);
309
  void addNewCertificate(const DNSCryptCert& newCert, const DNSCryptPrivateKey& newKey, bool active = true, bool reload = false);
310
311
  void markActive(uint32_t serial);
312
  void markInactive(uint32_t serial);
313
  void removeInactiveCertificate(uint32_t serial);
314
  std::vector<std::shared_ptr<DNSCryptCertificatePair>> getCertificates();
315
  const DNSName& getProviderName() const { return providerName; }
316
317
  bool magicMatchesAPublicKey(DNSCryptQuery& query, time_t now);
318
  void getCertificateResponse(time_t now, const DNSName& qname, uint16_t qid, PacketBuffer& response);
319
320
private:
321
  static void computePublicKeyFromPrivate(const DNSCryptPrivateKey& privK, DNSCryptCertificatePair::PublicKeyType& pubK);
322
  static void loadCertFromFile(const std::string& filename, DNSCryptCert& dest);
323
  static std::shared_ptr<DNSCryptCertificatePair> loadCertificatePair(const std::string& certFile, const std::string& keyFile);
324
325
  void addNewCertificate(std::shared_ptr<DNSCryptCertificatePair>& newCert, bool reload = false);
326
327
  SharedLockGuarded<std::vector<std::shared_ptr<DNSCryptCertificatePair>>> d_certs;
328
  SharedLockGuarded<std::vector<CertKeyPaths>> d_certKeyPaths;
329
  DNSName providerName;
330
};
331
332
bool generateDNSCryptCertificate(const std::string& providerPrivateKeyFile, uint32_t serial, time_t begin, time_t end, DNSCryptExchangeVersion version, DNSCryptCert& certOut, DNSCryptPrivateKey& keyOut);
333
334
#endif