Kea 3.3.3
managed_key.cc
Go to the documentation of this file.
1// Copyright (C) 2021-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
14
15#include <cstring>
16#include <iostream>
17
18using namespace isc;
19using namespace isc::data;
20using namespace isc::dns;
21using namespace isc::util;
22using namespace std;
23
24namespace isc {
25namespace gss_tsig {
26
27string
29 switch (status) {
30 case NOT_READY:
31 return ("not yet ready");
32 case USABLE:
33 return ("usable");
34 case EXPIRED:
35 return ("expired");
36 default:
37 return ("in error");
38 }
39}
40
41string
42ManagedKey::genName(const string& suffix) {
43 uint32_t n;
44 vector<uint8_t> r = isc::cryptolink::random(sizeof(uint32_t));
45 memmove(&n, &r[0], sizeof(uint32_t));
46 ostringstream s;
47 s << n << "." << suffix;
48 return (s.str());
49}
50
52 : GssTsigKey(name), parent_id_(""), status_(NOT_READY),
53 tkey_status_(TKeyExchange::OTHER), tkey_ex_(), mutex_(new mutex()) {
54}
55
56void
58 bool success = true;
59 {
60 lock_guard<mutex> lock(*mutex_);
61 setTKeyStatus(tkey_status);
62 if (tkey_status == TKeyExchange::SUCCESS) {
64 } else {
66 success = false;
67 }
68 }
69 if (success) {
72 .arg(getKeyName().toText(true));
73 } else {
75 .arg(getKeyName().toText(true))
76 .arg(TKeyExchange::statusToText(tkey_status));
77 }
78 if (getTKeyExchange() && getTKeyExchange()->getIOService()) {
79 getTKeyExchange()->getIOService()->post([this]() { getTKeyExchange().reset(); });
80 }
81}
82
86
87 // Name.
88 map->set("name", Element::create(getKeyNameStr()));
89
90 // Parent.
91 map->set("server-id", Element::create(getParentID()));
92
93 // Status.
94 map->set("status", Element::create(ManagedKey::statusToText(status_)));
95
96 // Per status extra information.
97 switch (status_) {
98 case USABLE:
99 case EXPIRED:
100 // Security context lifetime.
101 try {
102 if (sec_ctx_.get() && sec_ctx_.get()->get()) {
103 uint32_t lifetime = sec_ctx_->getLifetime();
104 map->set("security-context-lifetime",
105 Element::create(static_cast<long long>(lifetime)));
106 }
107 } catch (...) {
108 // Just ignore errors.
109 }
110 break;
111 case IN_ERROR:
112 // TKEY status.
113 map->set("tkey-status",
115 break;
116 default:
117 // TKEY exchange.
118 map->set("tkey-exchange", Element::create(!!tkey_ex_));
119 break;
120 }
121
122 // Inception date.
123 map->set("inception-date", Element::create(clockToText(inception_)));
124
125 // Expire date.
126 map->set("expire-date", Element::create(clockToText(expire_)));
127
128 return (map);
129}
130
135
136} // end of namespace isc::gss_tsig
137} // end of namespace isc
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:300
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:355
const Name & getKeyName() const
Getter Methods.
Definition tsigkey.cc:204
std::string toText() const
Converts the TSIGKey to a string value.
Definition tsigkey.cc:234
GSS-TSIG overwrite of the DNS TSIGContext class.
std::chrono::system_clock::time_point expire_
The key expire date.
GssTsigKey(const std::string &key_name, gss_ctx_id_t sec_ctx=GSS_C_NO_CONTEXT)
Constructor.
std::unique_ptr< GssApiSecCtx > sec_ctx_
GSS-API security context.
std::chrono::system_clock::time_point inception_
The key inception date.
std::string getKeyNameStr() const
Get the key name as a string.
Definition managed_key.h:54
static std::string genName(const std::string &server)
Create a random name from a suffix.
std::string getParentID() const
Get the DNS server (parent) ID.
Definition managed_key.h:61
virtual dns::TSIGContextPtr createContext()
Create GssTsigContext context.
isc::data::ElementPtr toElement() const
Unparse a key object.
boost::scoped_ptr< std::mutex > mutex_
Mutex for protecting key state.
@ EXPIRED
Expired (no longer usable).
Definition managed_key.h:36
@ IN_ERROR
Setup failed.
Definition managed_key.h:37
@ NOT_READY
Not yet ready (not yet usable).
Definition managed_key.h:34
void setTKeyStatus(TKeyExchange::Status tkey_status)
Set the TKEY exchange status.
Definition managed_key.h:96
TKeyExchangePtr & getTKeyExchange()
Get the TKEY exchange.
void setStatus(Status status)
Set the key status.
Definition managed_key.h:82
static std::string statusToText(Status status)
Convert a status to its textual form.
ManagedKey(const std::string &name)
Constructor.
void operator()(TKeyExchange::Status tkey_status)
The TKEY exchange completion handler.
The TKeyExchange class handles communication with the DNS server.
static std::string statusToText(Status status)
Convert a status to its textual form.
Status
A status code of the TKeyExchange.
@ SUCCESS
Response received and is ok.
Implements a TSIGContext derived class which can be used as the value of TSIGContext pointers so with...
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
boost::shared_ptr< TSIGContext > TSIGContextPtr
Definition tsig.h:436
const isc::log::MessageID GSS_TSIG_NEW_KEY_SETUP_FAILED
boost::shared_ptr< GssTsigContext > GssTsigContextPtr
Type of pointer to a GSS-TSIG context.
const isc::log::MessageID GSS_TSIG_NEW_KEY_SETUP_SUCCEED
isc::log::Logger gss_tsig_logger("gss-tsig-hooks")
const int DBGLVL_TRACE_BASIC
Trace basic operations.
std::string clockToText(std::chrono::system_clock::time_point t, size_t fsecs_precision)
Converts chrono time point structure to text.
Defines the logger used by the top-level component of kea-lfc.