]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsseckeeper.hh
Merge pull request #8715 from rgacogne/auth-hashed-key-cache
[thirdparty/pdns.git] / pdns / dnsseckeeper.hh
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 <string>
24 #include <string.h>
25 #include <vector>
26 #include <boost/logic/tribool.hpp>
27 #include <boost/multi_index_container.hpp>
28 #include <boost/multi_index/hashed_index.hpp>
29 #include <boost/multi_index/ordered_index.hpp>
30 #include <boost/tuple/tuple_comparison.hpp>
31 #include <boost/multi_index/key_extractors.hpp>
32 #include <boost/multi_index/sequenced_index.hpp>
33 #include "dnssecinfra.hh"
34 #include "dnsrecords.hh"
35 #include "ueberbackend.hh"
36
37 using namespace ::boost::multi_index;
38
39 class DNSSECKeeper : public boost::noncopyable
40 {
41 public:
42 enum keytype_t { KSK, ZSK, CSK };
43 enum keyalgorithm_t : uint8_t {
44 RSAMD5=1,
45 DH=2,
46 DSA=3,
47 RSASHA1=5,
48 DSANSEC3SHA1=6,
49 RSASHA1NSEC3SHA1=7,
50 RSASHA256=8,
51 RSASHA512=10,
52 ECCGOST=12,
53 ECDSA256=13,
54 ECDSA384=14,
55 ED25519=15,
56 ED448=16
57 };
58
59 enum dsdigestalgorithm_t : uint8_t {
60 DIGEST_SHA1=1,
61 DIGEST_SHA256=2,
62 DIGEST_GOST=3,
63 DIGEST_SHA384=4
64 };
65
66 struct KeyMetaData
67 {
68 string fname;
69 unsigned int id;
70 bool active;
71 keytype_t keyType;
72 bool hasSEPBit;
73 };
74 typedef std::pair<DNSSECPrivateKey, KeyMetaData> keymeta_t;
75 typedef std::vector<keymeta_t > keyset_t;
76
77 static string keyTypeToString(const keytype_t &keyType)
78 {
79 switch(keyType) {
80 case DNSSECKeeper::KSK:
81 return("KSK");
82 case DNSSECKeeper::ZSK:
83 return("ZSK");
84 case DNSSECKeeper::CSK:
85 return("CSK");
86 default:
87 return("UNKNOWN");
88 }
89 }
90
91 /*
92 * Returns the algorithm number based on the mnemonic (or old PowerDNS value of) a string.
93 * See https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml for the mapping
94 */
95 static int shorthand2algorithm(const string &algorithm)
96 {
97 if (pdns_iequals(algorithm, "rsamd5")) return RSAMD5;
98 if (pdns_iequals(algorithm, "dh")) return DH;
99 if (pdns_iequals(algorithm, "dsa")) return DSA;
100 if (pdns_iequals(algorithm, "rsasha1")) return RSASHA1;
101 if (pdns_iequals(algorithm, "dsa-nsec3-sha1")) return DSANSEC3SHA1;
102 if (pdns_iequals(algorithm, "rsasha1-nsec3-sha1")) return RSASHA1NSEC3SHA1;
103 if (pdns_iequals(algorithm, "rsasha256")) return RSASHA256;
104 if (pdns_iequals(algorithm, "rsasha512")) return RSASHA512;
105 if (pdns_iequals(algorithm, "ecc-gost")) return ECCGOST;
106 if (pdns_iequals(algorithm, "gost")) return ECCGOST;
107 if (pdns_iequals(algorithm, "ecdsa256")) return ECDSA256;
108 if (pdns_iequals(algorithm, "ecdsap256sha256")) return ECDSA256;
109 if (pdns_iequals(algorithm, "ecdsa384")) return ECDSA384;
110 if (pdns_iequals(algorithm, "ecdsap384sha384")) return ECDSA384;
111 if (pdns_iequals(algorithm, "ed25519")) return ED25519;
112 if (pdns_iequals(algorithm, "ed448")) return ED448;
113 if (pdns_iequals(algorithm, "indirect")) return 252;
114 if (pdns_iequals(algorithm, "privatedns")) return 253;
115 if (pdns_iequals(algorithm, "privateoid")) return 254;
116 return -1;
117 }
118
119 /*
120 * Returns the mnemonic from https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml
121 */
122 static string algorithm2name(uint8_t algo) {
123 switch(algo) {
124 case 0:
125 case 4:
126 case 9:
127 case 11:
128 return "Reserved";
129 case RSAMD5:
130 return "RSAMD5";
131 case DH:
132 return "DH";
133 case DSA:
134 return "DSA";
135 case RSASHA1:
136 return "RSASHA1";
137 case DSANSEC3SHA1:
138 return "DSA-NSEC3-SHA1";
139 case RSASHA1NSEC3SHA1:
140 return "RSASHA1-NSEC3-SHA1";
141 case RSASHA256:
142 return "RSASHA256";
143 case RSASHA512:
144 return "RSASHA512";
145 case ECCGOST:
146 return "ECC-GOST";
147 case ECDSA256:
148 return "ECDSAP256SHA256";
149 case ECDSA384:
150 return "ECDSAP384SHA384";
151 case ED25519:
152 return "ED25519";
153 case ED448:
154 return "ED448";
155 case 252:
156 return "INDIRECT";
157 case 253:
158 return "PRIVATEDNS";
159 case 254:
160 return "PRIVATEOID";
161 default:
162 return "Unallocated/Reserved";
163 }
164 }
165
166 private:
167 UeberBackend* d_keymetadb;
168 bool d_ourDB;
169
170 public:
171 DNSSECKeeper() : d_keymetadb( new UeberBackend("key-only")), d_ourDB(true)
172 {
173
174 }
175
176 DNSSECKeeper(UeberBackend* db) : d_keymetadb(db), d_ourDB(false)
177 {
178 }
179
180 ~DNSSECKeeper()
181 {
182 if(d_ourDB)
183 delete d_keymetadb;
184 }
185
186 static uint64_t dbdnssecCacheSizes(const std::string& str);
187 static void clearAllCaches();
188 static void clearCaches(const DNSName& name);
189
190 bool doesDNSSEC();
191 bool isSecuredZone(const DNSName& zone);
192 keyset_t getEntryPoints(const DNSName& zname);
193 keyset_t getKeys(const DNSName& zone, bool useCache = true);
194 DNSSECPrivateKey getKeyById(const DNSName& zone, unsigned int id);
195 bool addKey(const DNSName& zname, bool setSEPBit, int algorithm, int64_t& id, int bits=0, bool active=true);
196 bool addKey(const DNSName& zname, const DNSSECPrivateKey& dpk, int64_t& id, bool active=true);
197 bool removeKey(const DNSName& zname, unsigned int id);
198 bool activateKey(const DNSName& zname, unsigned int id);
199 bool deactivateKey(const DNSName& zname, unsigned int id);
200 bool checkKeys(const DNSName& zname, vector<string>* errorMessages = nullptr);
201
202 bool getNSEC3PARAM(const DNSName& zname, NSEC3PARAMRecordContent* n3p=0, bool* narrow=0);
203 bool checkNSEC3PARAM(const NSEC3PARAMRecordContent& ns3p, string& msg);
204 bool setNSEC3PARAM(const DNSName& zname, const NSEC3PARAMRecordContent& n3p, const bool& narrow=false);
205 bool unsetNSEC3PARAM(const DNSName& zname);
206 bool getPreRRSIGs(UeberBackend& db, const DNSName& signer, const DNSName& qname, const DNSName& wildcardname, const QType& qtype, DNSResourceRecord::Place, vector<DNSZoneRecord>& rrsigs, uint32_t signTTL);
207 bool isPresigned(const DNSName& zname);
208 bool setPresigned(const DNSName& zname);
209 bool unsetPresigned(const DNSName& zname);
210 bool setPublishCDNSKEY(const DNSName& zname);
211 void getPublishCDNSKEY(const DNSName& zname, std::string& value);
212 bool unsetPublishCDNSKEY(const DNSName& zname);
213 bool setPublishCDS(const DNSName& zname, const string& digestAlgos);
214 void getPublishCDS(const DNSName& zname, std::string& value);
215 bool unsetPublishCDS(const DNSName& zname);
216
217 bool TSIGGrantsAccess(const DNSName& zone, const DNSName& keyname);
218 bool getTSIGForAccess(const DNSName& zone, const ComboAddress& master, DNSName* keyname);
219
220 void startTransaction(const DNSName& zone, int zone_id)
221 {
222 (*d_keymetadb->backends.begin())->startTransaction(zone, zone_id);
223 }
224
225 void commitTransaction()
226 {
227 (*d_keymetadb->backends.begin())->commitTransaction();
228 }
229
230 void getFromMetaOrDefault(const DNSName& zname, const std::string& key, std::string& value, const std::string& defaultvalue);
231 bool getFromMeta(const DNSName& zname, const std::string& key, std::string& value);
232 void getSoaEdit(const DNSName& zname, std::string& value);
233 bool unSecureZone(const DNSName& zone, std::string& error, std::string& info);
234 bool rectifyZone(const DNSName& zone, std::string& error, std::string& info, bool doTransaction);
235 private:
236
237
238 struct KeyCacheEntry
239 {
240 typedef vector<DNSSECKeeper::keymeta_t> keys_t;
241
242 uint32_t getTTD() const
243 {
244 return d_ttd;
245 }
246
247 DNSName d_domain;
248 mutable keys_t d_keys;
249 unsigned int d_ttd;
250 };
251
252 struct METACacheEntry
253 {
254 uint32_t getTTD() const
255 {
256 return d_ttd;
257 }
258
259 DNSName d_domain;
260 mutable std::string d_key, d_value;
261 mutable bool d_isset;
262 unsigned int d_ttd;
263
264 };
265
266 struct KeyCacheTag{};
267 struct CompositeTag{};
268 struct SequencedTag{};
269
270 typedef multi_index_container<
271 KeyCacheEntry,
272 indexed_by<
273 hashed_unique<tag<KeyCacheTag>,member<KeyCacheEntry, DNSName, &KeyCacheEntry::d_domain> >,
274 sequenced<tag<SequencedTag>>
275 >
276 > keycache_t;
277 typedef multi_index_container<
278 METACacheEntry,
279 indexed_by<
280 ordered_unique<tag<CompositeTag>,
281 composite_key<
282 METACacheEntry,
283 member<METACacheEntry, DNSName, &METACacheEntry::d_domain> ,
284 member<METACacheEntry, std::string, &METACacheEntry::d_key>
285 >, composite_key_compare<std::less<DNSName>, CIStringCompare> >,
286 sequenced<tag<SequencedTag>>
287 >
288 > metacache_t;
289
290 void cleanup();
291
292 static keycache_t s_keycache;
293 static metacache_t s_metacache;
294 static pthread_rwlock_t s_metacachelock;
295 static pthread_rwlock_t s_keycachelock;
296 static AtomicCounter s_ops;
297 static time_t s_last_prune;
298
299 public:
300 void preRemoval(const KeyCacheEntry&)
301 {
302 }
303 void preRemoval(const METACacheEntry&)
304 {
305 }
306 };
307
308 class DNSPacket;
309 uint32_t localtime_format_YYYYMMDDSS(time_t t, uint32_t seq);
310 // for SOA-EDIT
311 uint32_t calculateEditSOA(uint32_t old_serial, DNSSECKeeper& dk, const DNSName& zonename);
312 uint32_t calculateEditSOA(uint32_t old_serial, const string& kind, const DNSName& zonename);
313 // for SOA-EDIT-DNSUPDATE/API
314 bool increaseSOARecord(DNSResourceRecord& dr, const string& increaseKind, const string& editKind);
315 bool makeIncreasedSOARecord(SOAData& sd, const string& increaseKind, const string& editKind, DNSResourceRecord& rrout);
316 DNSZoneRecord makeEditedDNSZRFromSOAData(DNSSECKeeper& dk, const SOAData& sd, DNSResourceRecord::Place place=DNSResourceRecord::ANSWER);