2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
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"
37 using namespace ::boost::multi_index;
39 class DNSSECKeeper : public boost::noncopyable
42 enum keytype_t { KSK, ZSK, CSK };
43 enum keyalgorithm_t : uint8_t {
59 enum dsdigestalgorithm_t : uint8_t {
75 typedef std::pair<DNSSECPrivateKey, KeyMetaData> keymeta_t;
76 typedef std::vector<keymeta_t > keyset_t;
78 static string keyTypeToString(const keytype_t &keyType)
81 case DNSSECKeeper::KSK:
83 case DNSSECKeeper::ZSK:
85 case DNSSECKeeper::CSK:
93 * Returns the algorithm number based on the mnemonic (or old PowerDNS value of) a string.
94 * See https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml for the mapping
96 static int shorthand2algorithm(const string &algorithm)
98 if (pdns_iequals(algorithm, "rsamd5")) return RSAMD5;
99 if (pdns_iequals(algorithm, "dh")) return DH;
100 if (pdns_iequals(algorithm, "dsa")) return DSA;
101 if (pdns_iequals(algorithm, "rsasha1")) return RSASHA1;
102 if (pdns_iequals(algorithm, "dsa-nsec3-sha1")) return DSANSEC3SHA1;
103 if (pdns_iequals(algorithm, "rsasha1-nsec3-sha1")) return RSASHA1NSEC3SHA1;
104 if (pdns_iequals(algorithm, "rsasha256")) return RSASHA256;
105 if (pdns_iequals(algorithm, "rsasha512")) return RSASHA512;
106 if (pdns_iequals(algorithm, "ecc-gost")) return ECCGOST;
107 if (pdns_iequals(algorithm, "gost")) return ECCGOST;
108 if (pdns_iequals(algorithm, "ecdsa256")) return ECDSA256;
109 if (pdns_iequals(algorithm, "ecdsap256sha256")) return ECDSA256;
110 if (pdns_iequals(algorithm, "ecdsa384")) return ECDSA384;
111 if (pdns_iequals(algorithm, "ecdsap384sha384")) return ECDSA384;
112 if (pdns_iequals(algorithm, "ed25519")) return ED25519;
113 if (pdns_iequals(algorithm, "ed448")) return ED448;
114 if (pdns_iequals(algorithm, "indirect")) return 252;
115 if (pdns_iequals(algorithm, "privatedns")) return 253;
116 if (pdns_iequals(algorithm, "privateoid")) return 254;
121 * Returns the mnemonic from https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml
123 static string algorithm2name(uint8_t algo) {
139 return "DSA-NSEC3-SHA1";
140 case RSASHA1NSEC3SHA1:
141 return "RSASHA1-NSEC3-SHA1";
149 return "ECDSAP256SHA256";
151 return "ECDSAP384SHA384";
163 return "Unallocated/Reserved";
168 UeberBackend* d_keymetadb;
172 DNSSECKeeper() : d_keymetadb( new UeberBackend("key-only")), d_ourDB(true)
177 DNSSECKeeper(UeberBackend* db) : d_keymetadb(db), d_ourDB(false)
187 static uint64_t dbdnssecCacheSizes(const std::string& str);
188 static void clearAllCaches();
189 static void clearCaches(const DNSName& name);
192 bool isSecuredZone(const DNSName& zone);
193 keyset_t getEntryPoints(const DNSName& zname);
194 keyset_t getKeys(const DNSName& zone, bool useCache = true);
195 DNSSECPrivateKey getKeyById(const DNSName& zone, unsigned int id);
196 bool addKey(const DNSName& zname, bool setSEPBit, int algorithm, int64_t& id, int bits=0, bool active=true, bool published=true);
197 bool addKey(const DNSName& zname, const DNSSECPrivateKey& dpk, int64_t& id, bool active=true, bool published=true);
198 bool removeKey(const DNSName& zname, unsigned int id);
199 bool activateKey(const DNSName& zname, unsigned int id);
200 bool deactivateKey(const DNSName& zname, unsigned int id);
201 bool publishKey(const DNSName& zname, unsigned int id);
202 bool unpublishKey(const DNSName& zname, unsigned int id);
203 bool checkKeys(const DNSName& zname, vector<string>* errorMessages = nullptr);
205 bool getNSEC3PARAM(const DNSName& zname, NSEC3PARAMRecordContent* n3p=0, bool* narrow=0);
206 bool checkNSEC3PARAM(const NSEC3PARAMRecordContent& ns3p, string& msg);
207 bool setNSEC3PARAM(const DNSName& zname, const NSEC3PARAMRecordContent& n3p, const bool& narrow=false);
208 bool unsetNSEC3PARAM(const DNSName& zname);
209 bool getPreRRSIGs(UeberBackend& db, const DNSName& signer, const DNSName& qname, const DNSName& wildcardname, const QType& qtype, DNSResourceRecord::Place, vector<DNSZoneRecord>& rrsigs, uint32_t signTTL);
210 bool isPresigned(const DNSName& zname);
211 bool setPresigned(const DNSName& zname);
212 bool unsetPresigned(const DNSName& zname);
213 bool setPublishCDNSKEY(const DNSName& zname);
214 void getPublishCDNSKEY(const DNSName& zname, std::string& value);
215 bool unsetPublishCDNSKEY(const DNSName& zname);
216 bool setPublishCDS(const DNSName& zname, const string& digestAlgos);
217 void getPublishCDS(const DNSName& zname, std::string& value);
218 bool unsetPublishCDS(const DNSName& zname);
220 bool TSIGGrantsAccess(const DNSName& zone, const DNSName& keyname);
221 bool getTSIGForAccess(const DNSName& zone, const ComboAddress& master, DNSName* keyname);
223 void startTransaction(const DNSName& zone, int zone_id)
225 (*d_keymetadb->backends.begin())->startTransaction(zone, zone_id);
228 void commitTransaction()
230 (*d_keymetadb->backends.begin())->commitTransaction();
233 void getFromMetaOrDefault(const DNSName& zname, const std::string& key, std::string& value, const std::string& defaultvalue);
234 bool getFromMeta(const DNSName& zname, const std::string& key, std::string& value);
235 void getSoaEdit(const DNSName& zname, std::string& value);
236 bool unSecureZone(const DNSName& zone, std::string& error, std::string& info);
237 bool rectifyZone(const DNSName& zone, std::string& error, std::string& info, bool doTransaction);
239 static void setMaxEntries(size_t maxEntries);
246 typedef vector<DNSSECKeeper::keymeta_t> keys_t;
248 uint32_t getTTD() const
254 mutable keys_t d_keys;
258 struct METACacheEntry
260 uint32_t getTTD() const
266 mutable std::string d_key, d_value;
267 mutable bool d_isset;
272 struct KeyCacheTag{};
273 struct CompositeTag{};
274 struct SequencedTag{};
276 typedef multi_index_container<
279 hashed_unique<tag<KeyCacheTag>,member<KeyCacheEntry, DNSName, &KeyCacheEntry::d_domain> >,
280 sequenced<tag<SequencedTag>>
284 typedef multi_index_container<
287 ordered_unique<tag<CompositeTag>,
290 member<METACacheEntry, DNSName, &METACacheEntry::d_domain> ,
291 member<METACacheEntry, std::string, &METACacheEntry::d_key>
292 >, composite_key_compare<std::less<DNSName>, CIStringCompare> >,
293 sequenced<tag<SequencedTag>>
299 static keycache_t s_keycache;
300 static metacache_t s_metacache;
301 static pthread_rwlock_t s_metacachelock;
302 static pthread_rwlock_t s_keycachelock;
303 static AtomicCounter s_ops;
304 static time_t s_last_prune;
305 static size_t s_maxEntries;
308 void preRemoval(const KeyCacheEntry&)
311 void preRemoval(const METACacheEntry&)
317 uint32_t localtime_format_YYYYMMDDSS(time_t t, uint32_t seq);
319 uint32_t calculateEditSOA(uint32_t old_serial, DNSSECKeeper& dk, const DNSName& zonename);
320 uint32_t calculateEditSOA(uint32_t old_serial, const string& kind, const DNSName& zonename);
321 // for SOA-EDIT-DNSUPDATE/API
322 bool increaseSOARecord(DNSResourceRecord& dr, const string& increaseKind, const string& editKind);
323 bool makeIncreasedSOARecord(SOAData& sd, const string& increaseKind, const string& editKind, DNSResourceRecord& rrout);
324 DNSZoneRecord makeEditedDNSZRFromSOAData(DNSSECKeeper& dk, const SOAData& sd, DNSResourceRecord::Place place=DNSResourceRecord::ANSWER);