Capability.hpp 6.7 KB

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