CertificateOfTrust.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #include "CertificateOfTrust.hpp"
  19. #include "RuntimeEnvironment.hpp"
  20. #include "Topology.hpp"
  21. #include "Switch.hpp"
  22. namespace ZeroTier {
  23. bool CertificateOfTrust::create(uint64_t ts,uint64_t rls,const Identity &iss,const Identity &tgt,Level l)
  24. {
  25. if ((!iss)||(!iss.hasPrivate()))
  26. return false;
  27. _timestamp = ts;
  28. _roles = rls;
  29. _issuer = iss.address();
  30. _target = tgt;
  31. _level = l;
  32. Buffer<sizeof(Identity) + 64> tmp;
  33. tmp.append(_timestamp);
  34. tmp.append(_roles);
  35. _issuer.appendTo(tmp);
  36. _target.serialize(tmp,false);
  37. tmp.append((uint16_t)_level);
  38. _signature = iss.sign(tmp.data(),tmp.size());
  39. return true;
  40. }
  41. int CertificateOfTrust::verify(const RuntimeEnvironment *RR) const
  42. {
  43. const Identity id(RR->topology->getIdentity(_issuer));
  44. if (!id) {
  45. RR->sw->requestWhois(_issuer);
  46. return 1;
  47. }
  48. Buffer<sizeof(Identity) + 64> tmp;
  49. tmp.append(_timestamp);
  50. tmp.append(_roles);
  51. _issuer.appendTo(tmp);
  52. _target.serialize(tmp,false);
  53. tmp.append((uint16_t)_level);
  54. return (id.verify(tmp.data(),tmp.size(),_signature) ? 0 : -1);
  55. }
  56. } // namespace ZeroTier