CertificateOfOwnership.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #include "CertificateOfOwnership.hpp"
  9. #include "Identity.hpp"
  10. #include "Network.hpp"
  11. #include "Node.hpp"
  12. #include "RuntimeEnvironment.hpp"
  13. #include "Switch.hpp"
  14. #include "Topology.hpp"
  15. namespace ZeroTier {
  16. int CertificateOfOwnership::verify(const RuntimeEnvironment* RR, void* tPtr) const
  17. {
  18. if ((! _signedBy) || (_signedBy != Network::controllerFor(_networkId))) {
  19. return -1;
  20. }
  21. const Identity id(RR->topology->getIdentity(tPtr, _signedBy));
  22. if (! id) {
  23. RR->sw->requestWhois(tPtr, RR->node->now(), _signedBy);
  24. return 1;
  25. }
  26. try {
  27. Buffer<(sizeof(CertificateOfOwnership) + 64)> tmp;
  28. this->serialize(tmp, true);
  29. return (id.verify(tmp.data(), tmp.size(), _signature) ? 0 : -1);
  30. }
  31. catch (...) {
  32. return -1;
  33. }
  34. }
  35. bool CertificateOfOwnership::_owns(const CertificateOfOwnership::Thing& t, const void* v, unsigned int l) const
  36. {
  37. for (unsigned int i = 0, j = _thingCount; i < j; ++i) {
  38. if (_thingTypes[i] == (uint8_t)t) {
  39. unsigned int k = 0;
  40. while (k < l) {
  41. if (reinterpret_cast<const uint8_t*>(v)[k] != _thingValues[i][k]) {
  42. break;
  43. }
  44. ++k;
  45. }
  46. if (k == l) {
  47. return true;
  48. }
  49. }
  50. }
  51. return false;
  52. }
  53. } // namespace ZeroTier