CertificateOfOwnership.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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 "CertificateOfOwnership.hpp"
  27. #include "RuntimeEnvironment.hpp"
  28. #include "Identity.hpp"
  29. #include "Topology.hpp"
  30. #include "Switch.hpp"
  31. #include "Network.hpp"
  32. namespace ZeroTier {
  33. int CertificateOfOwnership::verify(const RuntimeEnvironment *RR,void *tPtr) const
  34. {
  35. if ((!_signedBy)||(_signedBy != Network::controllerFor(_networkId)))
  36. return -1;
  37. const Identity id(RR->topology->getIdentity(tPtr,_signedBy));
  38. if (!id) {
  39. RR->sw->requestWhois(tPtr,_signedBy);
  40. return 1;
  41. }
  42. try {
  43. Buffer<(sizeof(CertificateOfOwnership) + 64)> tmp;
  44. this->serialize(tmp,true);
  45. return (id.verify(tmp.data(),tmp.size(),_signature) ? 0 : -1);
  46. } catch ( ... ) {
  47. return -1;
  48. }
  49. }
  50. bool CertificateOfOwnership::_owns(const CertificateOfOwnership::Thing &t,const void *v,unsigned int l) const
  51. {
  52. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  53. if (_thingTypes[i] == (uint8_t)t) {
  54. unsigned int k = 0;
  55. while (k < l) {
  56. if (reinterpret_cast<const uint8_t *>(v)[k] != _thingValues[i][k])
  57. break;
  58. ++k;
  59. }
  60. if (k == l)
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. } // namespace ZeroTier