Credential.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Constants.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "Credential.hpp"
  16. #include "Capability.hpp"
  17. #include "Tag.hpp"
  18. #include "CertificateOfMembership.hpp"
  19. #include "CertificateOfOwnership.hpp"
  20. #include "Revocation.hpp"
  21. #include "Switch.hpp"
  22. #include "Network.hpp"
  23. #include "ScopedPtr.hpp"
  24. namespace ZeroTier {
  25. template<typename CRED>
  26. static inline Credential::VerifyResult _credVerify(const RuntimeEnvironment *const RR,void *tPtr,CRED credential)
  27. {
  28. const Address signedBy(credential.signer());
  29. const uint64_t networkId = credential.networkId();
  30. if ((!signedBy)||(signedBy != Network::controllerFor(networkId)))
  31. return Credential::VERIFY_BAD_SIGNATURE;
  32. const Identity id(RR->topology->getIdentity(tPtr,signedBy));
  33. if (!id) {
  34. RR->sw->requestWhois(tPtr,RR->node->now(),signedBy);
  35. return Credential::VERIFY_NEED_IDENTITY;
  36. }
  37. try {
  38. ScopedPtr< Buffer<(sizeof(CRED) + 64)> > tmp(new Buffer<(sizeof(CRED) + 64)>());
  39. credential.serialize(*tmp,true);
  40. const Credential::VerifyResult result = (id.verify(tmp->data(),tmp->size(),credential.signature(),credential.signatureLength()) ? Credential::VERIFY_OK : Credential::VERIFY_BAD_SIGNATURE);
  41. return result;
  42. } catch ( ... ) {}
  43. return Credential::VERIFY_BAD_SIGNATURE;
  44. }
  45. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const Revocation &credential) const { return _credVerify(RR,tPtr,credential); }
  46. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const Tag &credential) const { return _credVerify(RR,tPtr,credential); }
  47. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const CertificateOfOwnership &credential) const { return _credVerify(RR,tPtr,credential); }
  48. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const CertificateOfMembership &credential) const
  49. {
  50. if ((!credential._signedBy)||(credential._signedBy != Network::controllerFor(credential.networkId()))||(credential._qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS))
  51. return Credential::VERIFY_BAD_SIGNATURE;
  52. const Identity id(RR->topology->getIdentity(tPtr,credential._signedBy));
  53. if (!id) {
  54. RR->sw->requestWhois(tPtr,RR->node->now(),credential._signedBy);
  55. return Credential::VERIFY_NEED_IDENTITY;
  56. }
  57. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  58. unsigned int ptr = 0;
  59. for(unsigned int i=0;i<credential._qualifierCount;++i) {
  60. buf[ptr++] = Utils::hton(credential._qualifiers[i].id);
  61. buf[ptr++] = Utils::hton(credential._qualifiers[i].value);
  62. buf[ptr++] = Utils::hton(credential._qualifiers[i].maxDelta);
  63. }
  64. return (id.verify(buf,ptr * sizeof(uint64_t),credential._signature,credential._signatureLength) ? Credential::VERIFY_OK : Credential::VERIFY_BAD_SIGNATURE);
  65. }
  66. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *RR,void *tPtr,const Capability &credential) const
  67. {
  68. try {
  69. // There must be at least one entry, and sanity check for bad chain max length
  70. if ((credential._maxCustodyChainLength < 1)||(credential._maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  71. return Credential::VERIFY_BAD_SIGNATURE;
  72. // Validate all entries in chain of custody
  73. Buffer<(sizeof(Capability) * 2)> tmp;
  74. credential.serialize(tmp,true);
  75. for(unsigned int c=0;c<credential._maxCustodyChainLength;++c) {
  76. if (c == 0) {
  77. if ((!credential._custody[c].to)||(!credential._custody[c].from)||(credential._custody[c].from != Network::controllerFor(credential._nwid)))
  78. return Credential::VERIFY_BAD_SIGNATURE; // the first entry must be present and from the network's controller
  79. } else {
  80. if (!credential._custody[c].to)
  81. return Credential::VERIFY_OK; // all previous entries were valid, so we are valid
  82. else if ((!credential._custody[c].from)||(credential._custody[c].from != credential._custody[c-1].to))
  83. return Credential::VERIFY_BAD_SIGNATURE; // otherwise if we have another entry it must be from the previous holder in the chain
  84. }
  85. const Identity id(RR->topology->getIdentity(tPtr,credential._custody[c].from));
  86. if (id) {
  87. if (!id.verify(tmp.data(),tmp.size(),credential._custody[c].signature,credential._custody[c].signatureLength))
  88. return Credential::VERIFY_BAD_SIGNATURE;
  89. } else {
  90. RR->sw->requestWhois(tPtr,RR->node->now(),credential._custody[c].from);
  91. return Credential::VERIFY_NEED_IDENTITY;
  92. }
  93. }
  94. // We reached max custody chain length and everything was valid
  95. return Credential::VERIFY_OK;
  96. } catch ( ... ) {}
  97. return Credential::VERIFY_BAD_SIGNATURE;
  98. }
  99. } // namespace ZeroTier