Capability.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Capability.hpp"
  19. #include "RuntimeEnvironment.hpp"
  20. #include "Identity.hpp"
  21. #include "Topology.hpp"
  22. #include "Switch.hpp"
  23. #include "Network.hpp"
  24. namespace ZeroTier {
  25. int Capability::verify(const RuntimeEnvironment *RR) const
  26. {
  27. try {
  28. // There must be at least one entry, and sanity check for bad chain max length
  29. if ((_maxCustodyChainLength < 1)||(_maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  30. return -1;
  31. // Validate all entries in chain of custody
  32. Buffer<(sizeof(Capability) * 2)> tmp;
  33. this->serialize(tmp,true);
  34. for(unsigned int c=0;c<_maxCustodyChainLength;++c) {
  35. if (c == 0) {
  36. if ((!_custody[c].to)||(!_custody[c].from)||(_custody[c].from != Network::controllerFor(_nwid)))
  37. return -1; // the first entry must be present and from the network's controller
  38. } else {
  39. if (!_custody[c].to)
  40. return 0; // all previous entries were valid, so we are valid
  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. const Identity id(RR->topology->getIdentity(_custody[c].from));
  45. if (id) {
  46. if (!id.verify(tmp.data(),tmp.size(),_custody[c].signature))
  47. return -1;
  48. } else {
  49. RR->sw->requestWhois(_custody[c].from);
  50. return 1;
  51. }
  52. }
  53. // We reached max custody chain length and everything was valid
  54. return 0;
  55. } catch ( ... ) {}
  56. return -1;
  57. }
  58. } // namespace ZeroTier