Capability.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "Capability.hpp"
  27. #include "RuntimeEnvironment.hpp"
  28. #include "Identity.hpp"
  29. #include "Topology.hpp"
  30. #include "Switch.hpp"
  31. #include "Network.hpp"
  32. #include "Node.hpp"
  33. namespace ZeroTier {
  34. bool Capability::sign(const Identity &from,const Address &to)
  35. {
  36. try {
  37. for(unsigned int i=0;((i<_maxCustodyChainLength)&&(i<ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH));++i) {
  38. if (!(_custody[i].to)) {
  39. Buffer<(sizeof(Capability) * 2)> tmp;
  40. this->serialize(tmp,true);
  41. _custody[i].to = to;
  42. _custody[i].from = from.address();
  43. _custody[i].signatureLength = from.sign(tmp.data(),tmp.size(),_custody[i].signature,sizeof(_custody[i].signature));
  44. return true;
  45. }
  46. }
  47. } catch ( ... ) {}
  48. return false;
  49. }
  50. int Capability::verify(const RuntimeEnvironment *RR,void *tPtr) const
  51. {
  52. try {
  53. // There must be at least one entry, and sanity check for bad chain max length
  54. if ((_maxCustodyChainLength < 1)||(_maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  55. return -1;
  56. // Validate all entries in chain of custody
  57. Buffer<(sizeof(Capability) * 2)> tmp;
  58. this->serialize(tmp,true);
  59. for(unsigned int c=0;c<_maxCustodyChainLength;++c) {
  60. if (c == 0) {
  61. if ((!_custody[c].to)||(!_custody[c].from)||(_custody[c].from != Network::controllerFor(_nwid)))
  62. return -1; // the first entry must be present and from the network's controller
  63. } else {
  64. if (!_custody[c].to)
  65. return 0; // all previous entries were valid, so we are valid
  66. else if ((!_custody[c].from)||(_custody[c].from != _custody[c-1].to))
  67. return -1; // otherwise if we have another entry it must be from the previous holder in the chain
  68. }
  69. const Identity id(RR->topology->getIdentity(tPtr,_custody[c].from));
  70. if (id) {
  71. if (!id.verify(tmp.data(),tmp.size(),_custody[c].signature,_custody[c].signatureLength))
  72. return -1;
  73. } else {
  74. RR->sw->requestWhois(tPtr,RR->node->now(),_custody[c].from);
  75. return 1;
  76. }
  77. }
  78. // We reached max custody chain length and everything was valid
  79. return 0;
  80. } catch ( ... ) {}
  81. return -1;
  82. }
  83. } // namespace ZeroTier