Credential.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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: 2025-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 "CapabilityCredential.hpp"
  17. #include "TagCredential.hpp"
  18. #include "MembershipCredential.hpp"
  19. #include "OwnershipCredential.hpp"
  20. #include "RevocationCredential.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_MEMBERSHIP_CREDENTIAL_MARSHAL_SIZE_MAX > ZT_BUF_MEM_SIZE
  38. #error ZT_MEMBERSHIP_CREDENTIAL_MARSHAL_SIZE_MAX exceeds maximum buffer size
  39. #endif
  40. namespace ZeroTier {
  41. template<typename CRED>
  42. static ZT_INLINE Credential::VerifyResult p_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::s_verify(const RuntimeEnvironment *RR, void *tPtr, const RevocationCredential &credential) { return p_credVerify(RR, tPtr, credential); }
  61. Credential::VerifyResult Credential::s_verify(const RuntimeEnvironment *RR, void *tPtr, const TagCredential &credential) { return p_credVerify(RR, tPtr, credential); }
  62. Credential::VerifyResult Credential::s_verify(const RuntimeEnvironment *RR, void *tPtr, const CapabilityCredential &credential) { return p_credVerify(RR, tPtr, credential); }
  63. Credential::VerifyResult Credential::s_verify(const RuntimeEnvironment *RR, void *tPtr, const OwnershipCredential &credential) { return p_credVerify(RR, tPtr, credential); }
  64. Credential::VerifyResult Credential::s_verify(const RuntimeEnvironment *RR, void *tPtr, const MembershipCredential &credential)
  65. {
  66. // Sanity check network ID.
  67. if ((!credential.m_signedBy) || (credential.m_signedBy != Network::controllerFor(credential.m_networkId)))
  68. return Credential::VERIFY_BAD_SIGNATURE;
  69. // If we don't know the peer, get its identity. This shouldn't happen here but should be handled.
  70. const SharedPtr<Peer> peer(RR->topology->peer(tPtr,credential.m_signedBy));
  71. if (!peer)
  72. return Credential::VERIFY_NEED_IDENTITY;
  73. // Now verify the controller's signature.
  74. uint64_t buf[ZT_MEMBERSHIP_CREDENTIAL_MARSHAL_SIZE_MAX / 8];
  75. const unsigned int bufSize = credential.m_fillSigningBuf(buf);
  76. return peer->identity().verify(buf, bufSize, credential.m_signature, credential.m_signatureLength) ? Credential::VERIFY_OK : Credential::VERIFY_BAD_SIGNATURE;
  77. }
  78. } // namespace ZeroTier