Credential.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *RR,void *tPtr,const Capability &credential) const
  80. {
  81. try {
  82. // There must be at least one entry, and sanity check for bad chain max length
  83. if ((credential._maxCustodyChainLength < 1)||(credential._maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  84. return Credential::VERIFY_BAD_SIGNATURE;
  85. // Validate all entries in chain of custody
  86. Buffer<(sizeof(Capability) * 2)> tmp;
  87. credential.serialize(tmp,true);
  88. for(unsigned int c=0;c<credential._maxCustodyChainLength;++c) {
  89. if (c == 0) {
  90. if ((!credential._custody[c].to)||(!credential._custody[c].from)||(credential._custody[c].from != Network::controllerFor(credential._nwid)))
  91. return Credential::VERIFY_BAD_SIGNATURE; // the first entry must be present and from the network's controller
  92. } else {
  93. if (!credential._custody[c].to)
  94. return Credential::VERIFY_OK; // all previous entries were valid, so we are valid
  95. else if ((!credential._custody[c].from)||(credential._custody[c].from != credential._custody[c-1].to))
  96. return Credential::VERIFY_BAD_SIGNATURE; // otherwise if we have another entry it must be from the previous holder in the chain
  97. }
  98. const Identity id(RR->topology->getIdentity(tPtr,credential._custody[c].from));
  99. if (id) {
  100. if (!id.verify(tmp.data(),tmp.size(),credential._custody[c].signature,credential._custody[c].signatureLength))
  101. return Credential::VERIFY_BAD_SIGNATURE;
  102. } else {
  103. RR->sw->requestWhois(tPtr,RR->node->now(),credential._custody[c].from);
  104. return Credential::VERIFY_NEED_IDENTITY;
  105. }
  106. }
  107. // We reached max custody chain length and everything was valid
  108. return Credential::VERIFY_OK;
  109. } catch ( ... ) {}
  110. return Credential::VERIFY_BAD_SIGNATURE;
  111. }
  112. } // namespace ZeroTier