CertificateOfOwnership.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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: 2025-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 "CertificateOfOwnership.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "Identity.hpp"
  16. #include "Topology.hpp"
  17. #include "Switch.hpp"
  18. #include "Network.hpp"
  19. #include "Node.hpp"
  20. namespace ZeroTier {
  21. int CertificateOfOwnership::verify(const RuntimeEnvironment *RR,void *tPtr) const
  22. {
  23. if ((!_signedBy)||(_signedBy != Network::controllerFor(_networkId)))
  24. return -1;
  25. const Identity id(RR->topology->getIdentity(tPtr,_signedBy));
  26. if (!id) {
  27. RR->sw->requestWhois(tPtr,RR->node->now(),_signedBy);
  28. return 1;
  29. }
  30. try {
  31. Buffer<(sizeof(CertificateOfOwnership) + 64)> tmp;
  32. this->serialize(tmp,true);
  33. return (id.verify(tmp.data(),tmp.size(),_signature) ? 0 : -1);
  34. } catch ( ... ) {
  35. return -1;
  36. }
  37. }
  38. bool CertificateOfOwnership::_owns(const CertificateOfOwnership::Thing &t,const void *v,unsigned int l) const
  39. {
  40. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  41. if (_thingTypes[i] == (uint8_t)t) {
  42. unsigned int k = 0;
  43. while (k < l) {
  44. if (reinterpret_cast<const uint8_t *>(v)[k] != _thingValues[i][k])
  45. break;
  46. ++k;
  47. }
  48. if (k == l)
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. } // namespace ZeroTier