Credential.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "Network.hpp"
  22. #include "Topology.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_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->peer(tPtr,signedBy));
  50. if (!peer)
  51. return Credential::VERIFY_NEED_IDENTITY;
  52. try {
  53. int l = credential.marshal(tmp,true);
  54. if (l <= 0)
  55. return Credential::VERIFY_BAD_SIGNATURE;
  56. return (peer->identity().verify(tmp,(unsigned int)l,credential.signature(),credential.signatureLength()) ? Credential::VERIFY_OK : Credential::VERIFY_BAD_SIGNATURE);
  57. } catch ( ... ) {}
  58. return Credential::VERIFY_BAD_SIGNATURE;
  59. }
  60. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const Revocation &credential) const { return _credVerify(RR,tPtr,credential); }
  61. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const Tag &credential) const { return _credVerify(RR,tPtr,credential); }
  62. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const CertificateOfOwnership &credential) const { return _credVerify(RR,tPtr,credential); }
  63. Credential::VerifyResult Credential::_verify(const RuntimeEnvironment *const RR,void *tPtr,const CertificateOfMembership &credential) const
  64. {
  65. if ((!credential._signedBy)||(credential._signedBy != Network::controllerFor(credential.networkId()))||(credential._qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS))
  66. return Credential::VERIFY_BAD_SIGNATURE;
  67. const SharedPtr<Peer> peer(RR->topology->peer(tPtr,credential._signedBy));
  68. if (!peer)
  69. return Credential::VERIFY_NEED_IDENTITY;
  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 (peer->identity().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. uint8_t tmp[ZT_CAPABILITY_MARSHAL_SIZE_MAX + 16];
  82. try {
  83. // There must be at least one entry, and sanity check for bad chain max length
  84. if ((credential._maxCustodyChainLength < 1)||(credential._maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  85. return Credential::VERIFY_BAD_SIGNATURE;
  86. int l = credential.marshal(tmp,true);
  87. if (l <= 0)
  88. return Credential::VERIFY_BAD_SIGNATURE;
  89. // Validate all entries in chain of custody
  90. for(unsigned int c=0;c<credential._maxCustodyChainLength;++c) {
  91. if (c == 0) {
  92. if ((!credential._custody[c].to)||(!credential._custody[c].from)||(credential._custody[c].from != Network::controllerFor(credential._nwid)))
  93. return Credential::VERIFY_BAD_SIGNATURE; // the first entry must be present and from the network's controller
  94. } else {
  95. if (!credential._custody[c].to)
  96. return Credential::VERIFY_OK; // all previous entries were valid, so we are valid
  97. else if ((!credential._custody[c].from)||(credential._custody[c].from != credential._custody[c-1].to))
  98. return Credential::VERIFY_BAD_SIGNATURE; // otherwise if we have another entry it must be from the previous holder in the chain
  99. }
  100. const SharedPtr<Peer> peer(RR->topology->peer(tPtr,credential._custody[c].from));
  101. if (peer) {
  102. if (!peer->identity().verify(tmp,(unsigned int)l,credential._custody[c].signature,credential._custody[c].signatureLength))
  103. return Credential::VERIFY_BAD_SIGNATURE;
  104. } else {
  105. return Credential::VERIFY_NEED_IDENTITY;
  106. }
  107. }
  108. // We reached max custody chain length and everything was valid
  109. return Credential::VERIFY_OK;
  110. } catch ( ... ) {}
  111. return Credential::VERIFY_BAD_SIGNATURE;
  112. }
  113. } // namespace ZeroTier