Files
ZeroTierOne/node/Capability.cpp
T

67 lines
1.8 KiB
C++
Raw Normal View History

2025-08-06 12:10:18 -04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
2016-08-04 09:02:35 -07:00
*
2025-08-06 12:10:18 -04:00
* (c) ZeroTier, Inc.
* https://www.zerotier.com/
2016-08-04 09:02:35 -07:00
*/
#include "Capability.hpp"
2025-07-03 11:26:23 -04:00
2016-08-04 09:02:35 -07:00
#include "Identity.hpp"
2016-08-04 10:39:28 -07:00
#include "Network.hpp"
2017-08-23 16:42:17 -07:00
#include "Node.hpp"
2025-07-03 11:26:23 -04:00
#include "RuntimeEnvironment.hpp"
#include "Switch.hpp"
#include "Topology.hpp"
2016-08-04 09:02:35 -07:00
namespace ZeroTier {
2025-07-03 11:26:23 -04:00
int Capability::verify(const RuntimeEnvironment* RR, void* tPtr) const
2016-08-04 09:02:35 -07:00
{
try {
// There must be at least one entry, and sanity check for bad chain max length
2025-07-03 11:26:23 -04:00
if ((_maxCustodyChainLength < 1) || (_maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH)) {
2016-08-04 10:39:28 -07:00
return -1;
2023-05-01 14:48:16 -04:00
}
2016-08-04 10:39:28 -07:00
// Validate all entries in chain of custody
2016-08-04 09:02:35 -07:00
Buffer<(sizeof(Capability) * 2)> tmp;
2025-07-03 11:26:23 -04:00
this->serialize(tmp, true);
for (unsigned int c = 0; c < _maxCustodyChainLength; ++c) {
2016-08-04 10:39:28 -07:00
if (c == 0) {
2025-07-03 11:26:23 -04:00
if ((! _custody[c].to) || (! _custody[c].from) || (_custody[c].from != Network::controllerFor(_nwid))) {
return -1; // the first entry must be present and from the network's controller
2023-05-01 14:48:16 -04:00
}
2025-07-03 11:26:23 -04:00
}
else {
if (! _custody[c].to) {
return 0; // all previous entries were valid, so we are valid
}
else if ((! _custody[c].from) || (_custody[c].from != _custody[c - 1].to)) {
return -1; // otherwise if we have another entry it must be from the previous holder in the chain
2023-05-01 14:48:16 -04:00
}
2016-08-04 10:39:28 -07:00
}
2025-07-03 11:26:23 -04:00
const Identity id(RR->topology->getIdentity(tPtr, _custody[c].from));
2016-08-04 09:02:35 -07:00
if (id) {
2025-07-03 11:26:23 -04:00
if (! id.verify(tmp.data(), tmp.size(), _custody[c].signature)) {
2016-08-04 09:02:35 -07:00
return -1;
2023-05-01 14:48:16 -04:00
}
2025-07-03 11:26:23 -04:00
}
else {
RR->sw->requestWhois(tPtr, RR->node->now(), _custody[c].from);
2016-08-04 09:02:35 -07:00
return 1;
}
}
// We reached max custody chain length and everything was valid
2016-08-04 09:02:35 -07:00
return 0;
2025-07-03 11:26:23 -04:00
}
catch (...) {
}
2016-08-04 10:39:28 -07:00
return -1;
2016-08-04 09:02:35 -07:00
}
2025-07-03 11:26:23 -04:00
} // namespace ZeroTier