CapabilityCredential.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef ZT_CAPABILITY_HPP
  14. #define ZT_CAPABILITY_HPP
  15. #include "Constants.hpp"
  16. #include "Credential.hpp"
  17. #include "Address.hpp"
  18. #include "C25519.hpp"
  19. #include "Utils.hpp"
  20. #include "Identity.hpp"
  21. #define ZT_VIRTUALNETWORKRULE_MARSHAL_SIZE_MAX 21
  22. #define ZT_CAPABILITY_MARSHAL_SIZE_MAX (8 + 8 + 4 + 1 + 2 + (ZT_VIRTUALNETWORKRULE_MARSHAL_SIZE_MAX * ZT_MAX_CAPABILITY_RULES) + 2 + (5 + 5 + 1 + 2 + ZT_SIGNATURE_BUFFER_SIZE))
  23. namespace ZeroTier {
  24. class RuntimeEnvironment;
  25. /**
  26. * A set of grouped and signed network flow rules for a specific member.
  27. *
  28. * On the sending side the sender does the following for each packet:
  29. *
  30. * (1) Evaluates its capabilities in ascending order of ID to determine
  31. * which capability allows it to transmit this packet.
  32. * (2) If it has not done so lately, it then sends this capability to the
  33. * receiving peer ("presents" it).
  34. * (3) The sender then sends the packet.
  35. *
  36. * On the receiving side the receiver evaluates the capabilities presented
  37. * by the sender. If any valid un-expired capability allows this packet it
  38. * is accepted.
  39. *
  40. * Note that this is after evaluation of network scope rules and only if
  41. * network scope rules do not deliver an explicit match.
  42. */
  43. class CapabilityCredential : public Credential
  44. {
  45. friend class Credential;
  46. public:
  47. static constexpr ZT_CredentialType credentialType() noexcept { return ZT_CREDENTIAL_TYPE_CAPABILITY; }
  48. ZT_INLINE CapabilityCredential() noexcept { memoryZero(this); }
  49. /**
  50. * @param id Capability ID
  51. * @param nwid Network ID
  52. * @param ts Timestamp (at controller)
  53. * @param mccl Maximum custody chain length (1 to create non-transferable capability)
  54. * @param rules Network flow rules for this capability
  55. * @param ruleCount Number of flow rules
  56. */
  57. ZT_INLINE CapabilityCredential(const uint32_t id, const uint64_t nwid, const int64_t ts, const ZT_VirtualNetworkRule *const rules, const unsigned int ruleCount) noexcept : // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  58. m_nwid(nwid),
  59. m_ts(ts),
  60. m_id(id),
  61. m_ruleCount((ruleCount < ZT_MAX_CAPABILITY_RULES) ? ruleCount : ZT_MAX_CAPABILITY_RULES),
  62. m_signatureLength(0)
  63. {
  64. if (m_ruleCount > 0)
  65. Utils::copy(m_rules, rules, sizeof(ZT_VirtualNetworkRule) * m_ruleCount);
  66. }
  67. /**
  68. * @return Rules -- see ruleCount() for size of array
  69. */
  70. ZT_INLINE const ZT_VirtualNetworkRule *rules() const noexcept { return m_rules; }
  71. /**
  72. * @return Number of rules in rules()
  73. */
  74. ZT_INLINE unsigned int ruleCount() const noexcept { return m_ruleCount; }
  75. ZT_INLINE uint32_t id() const noexcept { return m_id; }
  76. ZT_INLINE uint64_t networkId() const noexcept { return m_nwid; }
  77. ZT_INLINE int64_t timestamp() const noexcept { return m_ts; }
  78. ZT_INLINE const Address &issuedTo() const noexcept { return m_issuedTo; }
  79. ZT_INLINE const Address &signer() const noexcept { return m_signedBy; }
  80. ZT_INLINE const uint8_t *signature() const noexcept { return m_signature; }
  81. ZT_INLINE unsigned int signatureLength() const noexcept { return m_signatureLength; }
  82. /**
  83. * Sign this capability and add signature to its chain of custody
  84. *
  85. * If this returns false, this object should be considered to be
  86. * in an undefined state and should be discarded. False can be returned
  87. * if there is no more room for signatures (max chain length reached)
  88. * or if the 'from' identity does not include a secret key to allow
  89. * it to sign anything.
  90. *
  91. * @param from Signing identity (must have secret)
  92. * @param to Recipient of this signature
  93. * @return True if signature successful and chain of custody appended
  94. */
  95. bool sign(const Identity &from,const Address &to) noexcept;
  96. /**
  97. * Verify this capability's chain of custody and signatures
  98. *
  99. * @param RR Runtime environment to provide for peer lookup, etc.
  100. */
  101. ZT_INLINE Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const noexcept { return s_verify(RR, tPtr, *this); }
  102. static constexpr int marshalSizeMax() noexcept { return ZT_CAPABILITY_MARSHAL_SIZE_MAX; }
  103. int marshal(uint8_t data[ZT_CAPABILITY_MARSHAL_SIZE_MAX],bool forSign = false) const noexcept;
  104. int unmarshal(const uint8_t *data,int len) noexcept;
  105. /**
  106. * Marshal a set of virtual network rules
  107. *
  108. * @param data Buffer to store rules (must be at least ruleCount * ZT_VIRTUALNETWORKRULE_MARSHAL_SIZE_MAX)
  109. * @param rules Network rules
  110. * @param ruleCount Number of rules
  111. * @return Number of bytes written or -1 on error
  112. */
  113. static int marshalVirtualNetworkRules(uint8_t *data,const ZT_VirtualNetworkRule *rules,unsigned int ruleCount) noexcept;
  114. /**
  115. * Unmarshal a set of virtual network rules
  116. *
  117. * @param data Rule set to unmarshal
  118. * @param len Length of data
  119. * @param rules Buffer to store rules
  120. * @param ruleCount Result parameter to set to the number of rules decoded
  121. * @param maxRuleCount Capacity of rules buffer
  122. * @return Number of bytes unmarshaled or -1 on error
  123. */
  124. static int unmarshalVirtualNetworkRules(const uint8_t *data,int len,ZT_VirtualNetworkRule *rules,unsigned int &ruleCount,unsigned int maxRuleCount) noexcept;
  125. // Provides natural sort order by ID
  126. ZT_INLINE bool operator<(const CapabilityCredential &c) const noexcept { return (m_id < c.m_id); }
  127. ZT_INLINE bool operator==(const CapabilityCredential &c) const noexcept { return (memcmp(this, &c, sizeof(CapabilityCredential)) == 0); }
  128. ZT_INLINE bool operator!=(const CapabilityCredential &c) const noexcept { return (memcmp(this, &c, sizeof(CapabilityCredential)) != 0); }
  129. private:
  130. uint64_t m_nwid;
  131. int64_t m_ts;
  132. uint32_t m_id;
  133. unsigned int m_ruleCount;
  134. ZT_VirtualNetworkRule m_rules[ZT_MAX_CAPABILITY_RULES];
  135. Address m_issuedTo;
  136. Address m_signedBy;
  137. unsigned int m_signatureLength;
  138. uint8_t m_signature[ZT_SIGNATURE_BUFFER_SIZE];
  139. };
  140. } // namespace ZeroTier
  141. #endif