Credential.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "Constants.hpp"
  27. #include "RuntimeEnvironment.hpp"
  28. #include "Credential.hpp"
  29. #include "Capability.hpp"
  30. #include "Tag.hpp"
  31. #include "CertificateOfMembership.hpp"
  32. #include "CertificateOfOwnership.hpp"
  33. #include "Revocation.hpp"
  34. #include "Switch.hpp"
  35. #include "Network.hpp"
  36. namespace ZeroTier {
  37. template<typename CRED>
  38. static inline Credential::VerifyResult _credVerify(const RuntimeEnvironment *const RR,void *tPtr,CRED credential)
  39. {
  40. const Address signedBy(credential.signer());
  41. const uint64_t networkId = credential.networkId();
  42. if ((!signedBy)||(signedBy != Network::controllerFor(networkId)))
  43. return Credential::VERIFY_BAD_SIGNATURE;
  44. const Identity id(RR->topology->getIdentity(tPtr,signedBy));
  45. if (!id) {
  46. RR->sw->requestWhois(tPtr,RR->node->now(),signedBy);
  47. return Credential::VERIFY_NEED_IDENTITY;
  48. }
  49. try {
  50. Buffer<(sizeof(CRED) + 64)> *const tmp = new Buffer<(sizeof(CRED) + 64)>();
  51. credential.serialize(*tmp,true);
  52. const Credential::VerifyResult result = (id.verify(tmp->data(),tmp->size(),credential.signature(),credential.signatureLength()) ? Credential::VERIFY_OK : Credential::VERIFY_BAD_SIGNATURE);
  53. delete tmp;
  54. return result;
  55. } catch ( ... ) {}
  56. return Credential::VERIFY_BAD_SIGNATURE;
  57. }
  58. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const Revocation &credential) const { return _credVerify(RR,tPtr,credential); }
  59. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const Tag &credential) const { return _credVerify(RR,tPtr,credential); }
  60. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const CertificateOfOwnership &credential) const { return _credVerify(RR,tPtr,credential); }
  61. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const CertificateOfMembership &credential) const
  62. {
  63. if ((!credential._signedBy)||(credential._signedBy != Network::controllerFor(credential.networkId()))||(credential._qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS))
  64. return Credential::VERIFY_BAD_SIGNATURE;
  65. const Identity id(RR->topology->getIdentity(tPtr,credential._signedBy));
  66. if (!id) {
  67. RR->sw->requestWhois(tPtr,RR->node->now(),credential._signedBy);
  68. return Credential::VERIFY_NEED_IDENTITY;
  69. }
  70. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  71. unsigned int ptr = 0;
  72. for(unsigned int i=0;i<credential._qualifierCount;++i) {
  73. buf[ptr++] = Utils::hton(credential._qualifiers[i].id);
  74. buf[ptr++] = Utils::hton(credential._qualifiers[i].value);
  75. buf[ptr++] = Utils::hton(credential._qualifiers[i].maxDelta);
  76. }
  77. return (id.verify(buf,ptr * sizeof(uint64_t),credential._signature,credential._signatureLength) ? Credential::VERIFY_OK : Credential::VERIFY_BAD_SIGNATURE);
  78. }
  79. } // namespace ZeroTier