Credential.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c)2013-2020 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: 2024-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. // These are compile-time asserts to make sure temporary marshal buffers here and
  24. // also in NtworkConfig.cpp are always large enough to marshal all credential types.
  25. #if ZT_TAG_MARSHAL_SIZE_MAX > ZT_BUF_MEM_SIZE
  26. #error ZT_TAG_MARSHAL_SIZE_MAX exceeds maximum buffer size
  27. #endif
  28. #if ZT_CAPABILITY_MARSHAL_SIZE_MAX > ZT_BUF_MEM_SIZE
  29. #error ZT_CAPABILITY_MARSHAL_SIZE_MAX exceeds maximum buffer size
  30. #endif
  31. #if ZT_REVOCATION_MARSHAL_SIZE_MAX > ZT_BUF_MEM_SIZE
  32. #error ZT_REVOCATION_MARSHAL_SIZE_MAX exceeds maximum buffer size
  33. #endif
  34. #if ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX > ZT_BUF_MEM_SIZE
  35. #error ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX exceeds maximum buffer size
  36. #endif
  37. #if ZT_CERTIFICATEOFMEMBERSHIP_MARSHAL_SIZE_MAX > ZT_BUF_MEM_SIZE
  38. #error ZT_CERTIFICATEOFMEMBERSHIP_MARSHAL_SIZE_MAX exceeds maximum buffer size
  39. #endif
  40. namespace ZeroTier {
  41. template<typename CRED>
  42. static ZT_ALWAYS_INLINE Credential::VerifyResult _credVerify(const RuntimeEnvironment *RR,void *tPtr,CRED credential)
  43. {
  44. uint8_t tmp[ZT_BUF_MEM_SIZE + 16];
  45. const Address signedBy(credential.signer());
  46. const uint64_t networkId = credential.networkId();
  47. if ((!signedBy)||(signedBy != Network::controllerFor(networkId)))
  48. return Credential::VERIFY_BAD_SIGNATURE;
  49. const SharedPtr<Peer> peer(RR->topology->get(tPtr,signedBy));
  50. if (!peer) {
  51. RR->sw->requestWhois(tPtr,RR->node->now(),signedBy);
  52. return Credential::VERIFY_NEED_IDENTITY;
  53. }
  54. try {
  55. int l = credential.marshal(tmp,true);
  56. if (l <= 0)
  57. return Credential::VERIFY_BAD_SIGNATURE;
  58. return (peer->identity().verify(tmp,(unsigned int)l,credential.signature(),credential.signatureLength()) ? Credential::VERIFY_OK : Credential::VERIFY_BAD_SIGNATURE);
  59. } catch ( ... ) {}
  60. return Credential::VERIFY_BAD_SIGNATURE;
  61. }
  62. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const Revocation &credential) const { return _credVerify(RR,tPtr,credential); }
  63. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const Tag &credential) const { return _credVerify(RR,tPtr,credential); }
  64. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const CertificateOfOwnership &credential) const { return _credVerify(RR,tPtr,credential); }
  65. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const CertificateOfMembership &credential) const
  66. {
  67. if ((!credential._signedBy)||(credential._signedBy != Network::controllerFor(credential.networkId()))||(credential._qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS))
  68. return Credential::VERIFY_BAD_SIGNATURE;
  69. const SharedPtr<Peer> peer(RR->topology->get(tPtr,credential._signedBy));
  70. if (!peer) {
  71. RR->sw->requestWhois(tPtr,RR->node->now(),credential._signedBy);
  72. return Credential::VERIFY_NEED_IDENTITY;
  73. }
  74. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  75. unsigned int ptr = 0;
  76. for(unsigned int i=0;i<credential._qualifierCount;++i) {
  77. buf[ptr++] = Utils::hton(credential._qualifiers[i].id);
  78. buf[ptr++] = Utils::hton(credential._qualifiers[i].value);
  79. buf[ptr++] = Utils::hton(credential._qualifiers[i].maxDelta);
  80. }
  81. return (peer->identity().verify(buf,ptr * sizeof(uint64_t),credential._signature,credential._signatureLength) ? Credential::VERIFY_OK : Credential::VERIFY_BAD_SIGNATURE);
  82. }
  83. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *RR,void *tPtr,const Capability &credential) const
  84. {
  85. uint8_t tmp[ZT_CAPABILITY_MARSHAL_SIZE_MAX + 16];
  86. try {
  87. // There must be at least one entry, and sanity check for bad chain max length
  88. if ((credential._maxCustodyChainLength < 1)||(credential._maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  89. return Credential::VERIFY_BAD_SIGNATURE;
  90. int l = credential.marshal(tmp,true);
  91. if (l <= 0)
  92. return Credential::VERIFY_BAD_SIGNATURE;
  93. // Validate all entries in chain of custody
  94. for(unsigned int c=0;c<credential._maxCustodyChainLength;++c) {
  95. if (c == 0) {
  96. if ((!credential._custody[c].to)||(!credential._custody[c].from)||(credential._custody[c].from != Network::controllerFor(credential._nwid)))
  97. return Credential::VERIFY_BAD_SIGNATURE; // the first entry must be present and from the network's controller
  98. } else {
  99. if (!credential._custody[c].to)
  100. return Credential::VERIFY_OK; // all previous entries were valid, so we are valid
  101. else if ((!credential._custody[c].from)||(credential._custody[c].from != credential._custody[c-1].to))
  102. return Credential::VERIFY_BAD_SIGNATURE; // otherwise if we have another entry it must be from the previous holder in the chain
  103. }
  104. const SharedPtr<Peer> peer(RR->topology->get(tPtr,credential._custody[c].from));
  105. if (peer) {
  106. if (!peer->identity().verify(tmp,(unsigned int)l,credential._custody[c].signature,credential._custody[c].signatureLength))
  107. return Credential::VERIFY_BAD_SIGNATURE;
  108. } else {
  109. RR->sw->requestWhois(tPtr,RR->node->now(),credential._custody[c].from);
  110. return Credential::VERIFY_NEED_IDENTITY;
  111. }
  112. }
  113. // We reached max custody chain length and everything was valid
  114. return Credential::VERIFY_OK;
  115. } catch ( ... ) {}
  116. return Credential::VERIFY_BAD_SIGNATURE;
  117. }
  118. } // namespace ZeroTier