Kea 3.1.1
managed_key.cc
Go to the documentation of this file.
1// Copyright (C) 2021-2025 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
10#include <gss_tsig_context.h>
11#include <gss_tsig_log.h>
12#include <managed_key.h>
14#include <cstring>
15#include <iostream>
16
17using namespace isc;
18using namespace isc::data;
19using namespace isc::dns;
20using namespace isc::util;
21using namespace std;
22
23namespace isc {
24namespace gss_tsig {
25
26string
28 switch (status) {
29 case NOT_READY:
30 return ("not yet ready");
31 case USABLE:
32 return ("usable");
33 case EXPIRED:
34 return ("expired");
35 default:
36 return ("in error");
37 }
38}
39
40string
41ManagedKey::genName(const string& suffix) {
42 uint32_t n;
43 vector<uint8_t> r = isc::cryptolink::random(sizeof(uint32_t));
44 memmove(&n, &r[0], sizeof(uint32_t));
45 ostringstream s;
46 s << n << "." << suffix;
47 return (s.str());
48}
49
51 : GssTsigKey(name), parent_id_(""), status_(NOT_READY),
52 tkey_status_(TKeyExchange::OTHER), tkey_ex_(), mutex_(new mutex()) {
53}
54
55void
57 bool success = true;
58 {
59 lock_guard<mutex> lock(*mutex_);
60 setTKeyStatus(tkey_status);
61 if (tkey_status == TKeyExchange::SUCCESS) {
63 } else {
65 success = false;
66 }
67 }
68 if (success) {
71 .arg(getKeyName().toText(true));
72 } else {
74 .arg(getKeyName().toText(true))
75 .arg(TKeyExchange::statusToText(tkey_status));
76 }
77 if (getTKeyExchange() && getTKeyExchange()->getIOService()) {
78 getTKeyExchange()->getIOService()->post([this]() { getTKeyExchange().reset(); });
79 }
80}
81
85
86 // Name.
87 map->set("name", Element::create(getKeyNameStr()));
88
89 // Parent.
90 map->set("server-id", Element::create(getParentID()));
91
92 // Status.
93 map->set("status", Element::create(ManagedKey::statusToText(status_)));
94
95 // Per status extra information.
96 switch (status_) {
97 case USABLE:
98 case EXPIRED:
99 // Security context lifetime.
100 try {
101 if (sec_ctx_.get() && sec_ctx_.get()->get()) {
102 uint32_t lifetime = sec_ctx_->getLifetime();
103 map->set("security-context-lifetime",
104 Element::create(static_cast<long long>(lifetime)));
105 }
106 } catch (...) {
107 // Just ignore errors.
108 }
109 break;
110 case IN_ERROR:
111 // TKEY status.
112 map->set("tkey-status",
114 break;
115 default:
116 // TKEY exchange.
117 map->set("tkey-exchange", Element::create(!!tkey_ex_));
118 break;
119 }
120
121 // Inception date.
122 map->set("inception-date", Element::create(clockToText(inception_)));
123
124 // Expire date.
125 map->set("expire-date", Element::create(clockToText(expire_)));
126
127 return (map);
128}
129
134
135} // end of namespace isc::gss_tsig
136} // end of namespace isc
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
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:53
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:60
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.
void setTKeyStatus(TKeyExchange::Status tkey_status)
Set the TKEY exchange status.
Definition managed_key.h:95
TKeyExchangePtr & getTKeyExchange()
Get the TKEY exchange.
void setStatus(Status status)
Set the key status.
Definition managed_key.h:81
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.
@ EXPIRED
Expired (no longer usable).
Definition managed_key.h:35
@ IN_ERROR
Setup failed.
Definition managed_key.h:36
@ NOT_READY
Not yet ready (not yet usable).
Definition managed_key.h:33
The TKeyExchange class handles communication with the DNS server.
Status
A status code of the TKeyExchange.
@ SUCCESS
Response received and is ok.
static std::string statusToText(Status status)
Convert a status to its textual form.
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:28
boost::shared_ptr< TSIGContext > TSIGContextPtr
Definition tsig.h:435
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.