CertificateOfOwnership.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "CertificateOfOwnership.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. void CertificateOfOwnership::addThing(const InetAddress &ip)
  35. {
  36. if (_thingCount >= ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) return;
  37. if (ip.ss_family == AF_INET) {
  38. _thingTypes[_thingCount] = THING_IPV4_ADDRESS;
  39. memcpy(_thingValues[_thingCount],&(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr),4);
  40. ++_thingCount;
  41. } else if (ip.ss_family == AF_INET6) {
  42. _thingTypes[_thingCount] = THING_IPV6_ADDRESS;
  43. memcpy(_thingValues[_thingCount],reinterpret_cast<const struct sockaddr_in6 *>(&ip)->sin6_addr.s6_addr,16);
  44. ++_thingCount;
  45. }
  46. }
  47. bool CertificateOfOwnership::sign(const Identity &signer)
  48. {
  49. if (signer.hasPrivate()) {
  50. Buffer<sizeof(CertificateOfOwnership) + 64> tmp;
  51. _signedBy = signer.address();
  52. this->serialize(tmp,true);
  53. _signatureLength = signer.sign(tmp.data(),tmp.size(),_signature,sizeof(_signature));
  54. return true;
  55. }
  56. return false;
  57. }
  58. int CertificateOfOwnership::verify(const RuntimeEnvironment *RR,void *tPtr) const
  59. {
  60. if ((!_signedBy)||(_signedBy != Network::controllerFor(_networkId)))
  61. return -1;
  62. const Identity id(RR->topology->getIdentity(tPtr,_signedBy));
  63. if (!id) {
  64. RR->sw->requestWhois(tPtr,RR->node->now(),_signedBy);
  65. return 1;
  66. }
  67. try {
  68. Buffer<(sizeof(CertificateOfOwnership) + 64)> tmp;
  69. this->serialize(tmp,true);
  70. return (id.verify(tmp.data(),tmp.size(),_signature,_signatureLength) ? 0 : -1);
  71. } catch ( ... ) {
  72. return -1;
  73. }
  74. }
  75. bool CertificateOfOwnership::_owns(const CertificateOfOwnership::Thing &t,const void *v,unsigned int l) const
  76. {
  77. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  78. if (_thingTypes[i] == (uint8_t)t) {
  79. unsigned int k = 0;
  80. while (k < l) {
  81. if (reinterpret_cast<const uint8_t *>(v)[k] != _thingValues[i][k])
  82. break;
  83. ++k;
  84. }
  85. if (k == l)
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. } // namespace ZeroTier