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