Capability.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Capability.hpp"
  14. #include "Identity.hpp"
  15. #include "Network.hpp"
  16. #include "Node.hpp"
  17. #include "RuntimeEnvironment.hpp"
  18. #include "Switch.hpp"
  19. #include "Topology.hpp"
  20. namespace ZeroTier {
  21. int Capability::verify(const RuntimeEnvironment* RR, void* tPtr) const
  22. {
  23. try {
  24. // There must be at least one entry, and sanity check for bad chain max length
  25. if ((_maxCustodyChainLength < 1) || (_maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH)) {
  26. return -1;
  27. }
  28. // Validate all entries in chain of custody
  29. Buffer<(sizeof(Capability) * 2)> tmp;
  30. this->serialize(tmp, true);
  31. for (unsigned int c = 0; c < _maxCustodyChainLength; ++c) {
  32. if (c == 0) {
  33. if ((! _custody[c].to) || (! _custody[c].from) || (_custody[c].from != Network::controllerFor(_nwid))) {
  34. return -1; // the first entry must be present and from the network's controller
  35. }
  36. }
  37. else {
  38. if (! _custody[c].to) {
  39. return 0; // all previous entries were valid, so we are valid
  40. }
  41. else if ((! _custody[c].from) || (_custody[c].from != _custody[c - 1].to)) {
  42. return -1; // otherwise if we have another entry it must be from the previous holder in the chain
  43. }
  44. }
  45. const Identity id(RR->topology->getIdentity(tPtr, _custody[c].from));
  46. if (id) {
  47. if (! id.verify(tmp.data(), tmp.size(), _custody[c].signature)) {
  48. return -1;
  49. }
  50. }
  51. else {
  52. RR->sw->requestWhois(tPtr, RR->node->now(), _custody[c].from);
  53. return 1;
  54. }
  55. }
  56. // We reached max custody chain length and everything was valid
  57. return 0;
  58. }
  59. catch (...) {
  60. }
  61. return -1;
  62. }
  63. } // namespace ZeroTier