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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_NO_MAIN
#include <boost/test/unit_test.hpp>
#include "dnscrypt.hh"
#include "dnsname.hh"
#include "dnsparser.hh"
#include "dnswriter.hh"
#include "dolog.hh"
#include <unistd.h>
bool g_verbose{false};
bool g_syslog{true};
bool g_logtimestamps{false};
std::optional<std::ofstream> g_verboseStream{std::nullopt};
BOOST_AUTO_TEST_SUITE(test_dnscrypt_cc)
#ifdef HAVE_DNSCRYPT
// plaintext query for cert
BOOST_AUTO_TEST_CASE(DNSCryptPlaintextQuery) {
DNSCryptPrivateKey resolverPrivateKey;
DNSCryptCert resolverCert;
unsigned char providerPublicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
unsigned char providerPrivateKey[DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE];
time_t now = time(nullptr);
DNSCryptContext::generateProviderKeys(providerPublicKey, providerPrivateKey);
DNSCryptContext::generateCertificate(1, now, now + (24 * 60 * 3600), DNSCryptExchangeVersion::VERSION1, providerPrivateKey, resolverPrivateKey, resolverCert);
auto ctx = std::make_shared<DNSCryptContext>("2.name", resolverCert, resolverPrivateKey);
DNSName name("2.name.");
PacketBuffer plainQuery;
GenericDNSPacketWriter<PacketBuffer> pw(plainQuery, name, QType::TXT, QClass::IN, 0);
pw.getHeader()->rd = 0;
std::shared_ptr<DNSCryptQuery> query = std::make_shared<DNSCryptQuery>(ctx);
query->parsePacket(plainQuery, false, now);
BOOST_CHECK_EQUAL(query->isValid(), true);
BOOST_CHECK_EQUAL(query->isEncrypted(), false);
PacketBuffer response;
query->getCertificateResponse(now, response);
MOADNSParser mdp(false, (char*) response.data(), response.size());
BOOST_CHECK_EQUAL(mdp.d_header.qdcount, 1U);
BOOST_CHECK_EQUAL(mdp.d_header.ancount, 1U);
BOOST_CHECK_EQUAL(mdp.d_header.nscount, 0U);
BOOST_CHECK_EQUAL(mdp.d_header.arcount, 0U);
BOOST_CHECK_EQUAL(mdp.d_qname.toString(), "2.name.");
BOOST_CHECK(mdp.d_qclass == QClass::IN);
BOOST_CHECK(mdp.d_qtype == QType::TXT);
}
// invalid plaintext query (A)
BOOST_AUTO_TEST_CASE(DNSCryptPlaintextQueryInvalidA) {
DNSCryptPrivateKey resolverPrivateKey;
DNSCryptCert resolverCert;
unsigned char providerPublicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
unsigned char providerPrivateKey[DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE];
time_t now = time(nullptr);
DNSCryptContext::generateProviderKeys(providerPublicKey, providerPrivateKey);
DNSCryptContext::generateCertificate(1, now, now + (24 * 60 * 3600), DNSCryptExchangeVersion::VERSION1, providerPrivateKey, resolverPrivateKey, resolverCert);
auto ctx = std::make_shared<DNSCryptContext>("2.name", resolverCert, resolverPrivateKey);
DNSName name("2.name.");
PacketBuffer plainQuery;
GenericDNSPacketWriter<PacketBuffer> pw(plainQuery, name, QType::A, QClass::IN, 0);
pw.getHeader()->rd = 0;
std::shared_ptr<DNSCryptQuery> query = std::make_shared<DNSCryptQuery>(ctx);
query->parsePacket(plainQuery, false, now);
BOOST_CHECK_EQUAL(query->isValid(), false);
}
// invalid plaintext query (wrong provider name)
BOOST_AUTO_TEST_CASE(DNSCryptPlaintextQueryInvalidProviderName) {
DNSCryptPrivateKey resolverPrivateKey;
DNSCryptCert resolverCert;
unsigned char providerPublicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
unsigned char providerPrivateKey[DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE];
time_t now = time(nullptr);
DNSCryptContext::generateProviderKeys(providerPublicKey, providerPrivateKey);
DNSCryptContext::generateCertificate(1, now, now + (24 * 60 * 3600), DNSCryptExchangeVersion::VERSION1, providerPrivateKey, resolverPrivateKey, resolverCert);
auto ctx = std::make_shared<DNSCryptContext>("2.name", resolverCert, resolverPrivateKey);
DNSName name("2.WRONG.name.");
PacketBuffer plainQuery;
GenericDNSPacketWriter<PacketBuffer> pw(plainQuery, name, QType::TXT, QClass::IN, 0);
pw.getHeader()->rd = 0;
std::shared_ptr<DNSCryptQuery> query = std::make_shared<DNSCryptQuery>(ctx);
query->parsePacket(plainQuery, false, now);
BOOST_CHECK_EQUAL(query->isValid(), false);
}
// valid encrypted query
BOOST_AUTO_TEST_CASE(DNSCryptEncryptedQueryValid) {
DNSCryptPrivateKey resolverPrivateKey;
DNSCryptCert resolverCert;
unsigned char providerPublicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
unsigned char providerPrivateKey[DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE];
time_t now = time(nullptr);
DNSCryptContext::generateProviderKeys(providerPublicKey, providerPrivateKey);
DNSCryptContext::generateCertificate(1, now, now + (24 * 60 * 3600), DNSCryptExchangeVersion::VERSION1, providerPrivateKey, resolverPrivateKey, resolverCert);
auto ctx = std::make_shared<DNSCryptContext>("2.name", resolverCert, resolverPrivateKey);
DNSCryptPrivateKey clientPrivateKey;
unsigned char clientPublicKey[DNSCRYPT_PUBLIC_KEY_SIZE];
DNSCryptContext::generateResolverKeyPair(clientPrivateKey, clientPublicKey);
unsigned char clientNonce[DNSCRYPT_NONCE_SIZE / 2] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x09, 0x0A, 0x0B };
DNSName name("www.powerdns.com.");
PacketBuffer plainQuery;
GenericDNSPacketWriter<PacketBuffer> pw(plainQuery, name, QType::AAAA, QClass::IN, 0);
pw.getHeader()->rd = 1;
size_t initialSize = plainQuery.size();
int res = ctx->encryptQuery(plainQuery, 4096, clientPublicKey, clientPrivateKey, clientNonce, false, std::make_shared<DNSCryptCert>(resolverCert));
BOOST_CHECK_EQUAL(res, 0);
BOOST_CHECK(plainQuery.size() > initialSize);
std::shared_ptr<DNSCryptQuery> query = std::make_shared<DNSCryptQuery>(ctx);
query->parsePacket(plainQuery, false, now);
BOOST_CHECK_EQUAL(query->isValid(), true);
BOOST_CHECK_EQUAL(query->isEncrypted(), true);
MOADNSParser mdp(true, (char*) plainQuery.data(), plainQuery.size());
BOOST_CHECK_EQUAL(mdp.d_header.qdcount, 1U);
BOOST_CHECK_EQUAL(mdp.d_header.ancount, 0U);
BOOST_CHECK_EQUAL(mdp.d_header.nscount, 0U);
BOOST_CHECK_EQUAL(mdp.d_header.arcount, 0U);
BOOST_CHECK_EQUAL(mdp.d_qname, name);
BOOST_CHECK(mdp.d_qclass == QClass::IN);
BOOST_CHECK(mdp.d_qtype == QType::AAAA);
}
// valid encrypted query with not enough room
BOOST_AUTO_TEST_CASE(DNSCryptEncryptedQueryValidButShort) {
DNSCryptPrivateKey resolverPrivateKey;
DNSCryptCert resolverCert;
unsigned char providerPublicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
unsigned char providerPrivateKey[DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE];
time_t now = time(nullptr);
DNSCryptContext::generateProviderKeys(providerPublicKey, providerPrivateKey);
DNSCryptContext::generateCertificate(1, now, now + (24 * 60 * 3600), DNSCryptExchangeVersion::VERSION1, providerPrivateKey, resolverPrivateKey, resolverCert);
auto ctx = std::make_shared<DNSCryptContext>("2.name", resolverCert, resolverPrivateKey);
DNSCryptPrivateKey clientPrivateKey;
unsigned char clientPublicKey[DNSCRYPT_PUBLIC_KEY_SIZE];
DNSCryptContext::generateResolverKeyPair(clientPrivateKey, clientPublicKey);
unsigned char clientNonce[DNSCRYPT_NONCE_SIZE / 2] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x09, 0x0A, 0x0B };
DNSName name("www.powerdns.com.");
PacketBuffer plainQuery;
GenericDNSPacketWriter<PacketBuffer> pw(plainQuery, name, QType::AAAA, QClass::IN, 0);
pw.getHeader()->rd = 1;
int res = ctx->encryptQuery(plainQuery, /* not enough room */ plainQuery.size(), clientPublicKey, clientPrivateKey, clientNonce, false, std::make_shared<DNSCryptCert>(resolverCert));
BOOST_CHECK_EQUAL(res, ENOBUFS);
}
// valid encrypted query with old key
BOOST_AUTO_TEST_CASE(DNSCryptEncryptedQueryValidWithOldKey) {
DNSCryptPrivateKey resolverPrivateKey;
DNSCryptCert resolverCert;
unsigned char providerPublicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
unsigned char providerPrivateKey[DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE];
time_t now = time(nullptr);
DNSCryptContext::generateProviderKeys(providerPublicKey, providerPrivateKey);
DNSCryptContext::generateCertificate(1, now, now + (24 * 60 * 3600), DNSCryptExchangeVersion::VERSION1, providerPrivateKey, resolverPrivateKey, resolverCert);
auto ctx = std::make_shared<DNSCryptContext>("2.name", resolverCert, resolverPrivateKey);
DNSCryptPrivateKey clientPrivateKey;
unsigned char clientPublicKey[DNSCRYPT_PUBLIC_KEY_SIZE];
DNSCryptContext::generateResolverKeyPair(clientPrivateKey, clientPublicKey);
unsigned char clientNonce[DNSCRYPT_NONCE_SIZE / 2] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x09, 0x0A, 0x0B };
DNSName name("www.powerdns.com.");
PacketBuffer plainQuery;
GenericDNSPacketWriter<PacketBuffer> pw(plainQuery, name, QType::AAAA, QClass::IN, 0);
pw.getHeader()->rd = 1;
size_t initialSize = plainQuery.size();
int res = ctx->encryptQuery(plainQuery, 4096, clientPublicKey, clientPrivateKey, clientNonce, false, std::make_shared<DNSCryptCert>(resolverCert));
BOOST_CHECK_EQUAL(res, 0);
BOOST_CHECK(plainQuery.size() > initialSize);
DNSCryptCert newResolverCert;
DNSCryptContext::generateCertificate(2, now, now + (24 * 60 * 3600), DNSCryptExchangeVersion::VERSION1, providerPrivateKey, resolverPrivateKey, newResolverCert);
ctx->addNewCertificate(newResolverCert, resolverPrivateKey);
ctx->markInactive(resolverCert.getSerial());
std::shared_ptr<DNSCryptQuery> query = std::make_shared<DNSCryptQuery>(ctx);
query->parsePacket(plainQuery, false, now);
BOOST_CHECK_EQUAL(query->isValid(), true);
BOOST_CHECK_EQUAL(query->isEncrypted(), true);
MOADNSParser mdp(true, (char*) plainQuery.data(), plainQuery.size());
BOOST_CHECK_EQUAL(mdp.d_header.qdcount, 1U);
BOOST_CHECK_EQUAL(mdp.d_header.ancount, 0U);
BOOST_CHECK_EQUAL(mdp.d_header.nscount, 0U);
BOOST_CHECK_EQUAL(mdp.d_header.arcount, 0U);
BOOST_CHECK_EQUAL(mdp.d_qname, name);
BOOST_CHECK(mdp.d_qclass == QClass::IN);
BOOST_CHECK(mdp.d_qtype == QType::AAAA);
}
// valid encrypted query with wrong key
BOOST_AUTO_TEST_CASE(DNSCryptEncryptedQueryInvalidWithWrongKey) {
DNSCryptPrivateKey resolverPrivateKey;
DNSCryptCert resolverCert;
unsigned char providerPublicKey[DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE];
unsigned char providerPrivateKey[DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE];
time_t now = time(nullptr);
DNSCryptContext::generateProviderKeys(providerPublicKey, providerPrivateKey);
DNSCryptContext::generateCertificate(1, now, now + (24 * 60 * 3600), DNSCryptExchangeVersion::VERSION1, providerPrivateKey, resolverPrivateKey, resolverCert);
auto ctx = std::make_shared<DNSCryptContext>("2.name", resolverCert, resolverPrivateKey);
DNSCryptPrivateKey clientPrivateKey;
unsigned char clientPublicKey[DNSCRYPT_PUBLIC_KEY_SIZE];
DNSCryptContext::generateResolverKeyPair(clientPrivateKey, clientPublicKey);
unsigned char clientNonce[DNSCRYPT_NONCE_SIZE / 2] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x09, 0x0A, 0x0B };
DNSName name("www.powerdns.com.");
PacketBuffer plainQuery;
GenericDNSPacketWriter<PacketBuffer> pw(plainQuery, name, QType::AAAA, QClass::IN, 0);
pw.getHeader()->rd = 1;
size_t initialSize = plainQuery.size();
int res = ctx->encryptQuery(plainQuery, 4096, clientPublicKey, clientPrivateKey, clientNonce, false, std::make_shared<DNSCryptCert>(resolverCert));
BOOST_CHECK_EQUAL(res, 0);
BOOST_CHECK(plainQuery.size() > initialSize);
DNSCryptCert newResolverCert;
DNSCryptContext::generateCertificate(2, now, now + (24 * 60 * 3600), DNSCryptExchangeVersion::VERSION1, providerPrivateKey, resolverPrivateKey, newResolverCert);
ctx->addNewCertificate(newResolverCert, resolverPrivateKey);
ctx->markInactive(resolverCert.getSerial());
ctx->removeInactiveCertificate(resolverCert.getSerial());
/* we have removed the old certificate, we can't decrypt this query */
std::shared_ptr<DNSCryptQuery> query = std::make_shared<DNSCryptQuery>(ctx);
query->parsePacket(plainQuery, false, now);
BOOST_CHECK_EQUAL(query->isValid(), false);
}
#endif
BOOST_AUTO_TEST_SUITE_END();
|