Capability.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_CAPABILITY_HPP
  27. #define ZT_CAPABILITY_HPP
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "Constants.hpp"
  32. #include "Credential.hpp"
  33. #include "Address.hpp"
  34. #include "C25519.hpp"
  35. #include "Utils.hpp"
  36. #include "Buffer.hpp"
  37. #include "Identity.hpp"
  38. #include "../include/ZeroTierOne.h"
  39. namespace ZeroTier {
  40. class RuntimeEnvironment;
  41. /**
  42. * A set of grouped and signed network flow rules
  43. *
  44. * On the sending side the sender does the following for each packet:
  45. *
  46. * (1) Evaluates its capabilities in ascending order of ID to determine
  47. * which capability allows it to transmit this packet.
  48. * (2) If it has not done so lately, it then sends this capability to the
  49. * receiving peer ("presents" it).
  50. * (3) The sender then sends the packet.
  51. *
  52. * On the receiving side the receiver evaluates the capabilities presented
  53. * by the sender. If any valid un-expired capability allows this packet it
  54. * is accepted.
  55. *
  56. * Note that this is after evaluation of network scope rules and only if
  57. * network scope rules do not deliver an explicit match.
  58. *
  59. * Capabilities support a chain of custody. This is currently unused but
  60. * in the future would allow the publication of capabilities that can be
  61. * handed off between nodes. Limited transferability of capabilities is
  62. * a feature of true capability based security.
  63. */
  64. class Capability : public Credential
  65. {
  66. public:
  67. static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_CAPABILITY; }
  68. Capability()
  69. {
  70. memset(this,0,sizeof(Capability));
  71. }
  72. /**
  73. * @param id Capability ID
  74. * @param nwid Network ID
  75. * @param ts Timestamp (at controller)
  76. * @param mccl Maximum custody chain length (1 to create non-transferable capability)
  77. * @param rules Network flow rules for this capability
  78. * @param ruleCount Number of flow rules
  79. */
  80. Capability(uint32_t id,uint64_t nwid,int64_t ts,unsigned int mccl,const ZT_VirtualNetworkRule *rules,unsigned int ruleCount)
  81. {
  82. memset(this,0,sizeof(Capability));
  83. _nwid = nwid;
  84. _ts = ts;
  85. _id = id;
  86. _maxCustodyChainLength = (mccl > 0) ? ((mccl < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH) ? mccl : (unsigned int)ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH) : 1;
  87. _ruleCount = (ruleCount < ZT_MAX_CAPABILITY_RULES) ? ruleCount : ZT_MAX_CAPABILITY_RULES;
  88. if (_ruleCount)
  89. ZT_FAST_MEMCPY(_rules,rules,sizeof(ZT_VirtualNetworkRule) * _ruleCount);
  90. }
  91. /**
  92. * @return Rules -- see ruleCount() for size of array
  93. */
  94. inline const ZT_VirtualNetworkRule *rules() const { return _rules; }
  95. /**
  96. * @return Number of rules in rules()
  97. */
  98. inline unsigned int ruleCount() const { return _ruleCount; }
  99. /**
  100. * @return ID and evaluation order of this capability in network
  101. */
  102. inline uint32_t id() const { return _id; }
  103. /**
  104. * @return Network ID for which this capability was issued
  105. */
  106. inline uint64_t networkId() const { return _nwid; }
  107. /**
  108. * @return Timestamp
  109. */
  110. inline int64_t timestamp() const { return _ts; }
  111. /**
  112. * @return Last 'to' address in chain of custody
  113. */
  114. inline Address issuedTo() const
  115. {
  116. Address i2;
  117. for(unsigned int i=0;i<ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH;++i) {
  118. if (!_custody[i].to)
  119. return i2;
  120. else i2 = _custody[i].to;
  121. }
  122. return i2;
  123. }
  124. /**
  125. * Sign this capability and add signature to its chain of custody
  126. *
  127. * If this returns false, this object should be considered to be
  128. * in an undefined state and should be discarded. False can be returned
  129. * if there is no more room for signatures (max chain length reached)
  130. * or if the 'from' identity does not include a secret key to allow
  131. * it to sign anything.
  132. *
  133. * @param from Signing identity (must have secret)
  134. * @param to Recipient of this signature
  135. * @return True if signature successful and chain of custody appended
  136. */
  137. inline bool sign(const Identity &from,const Address &to)
  138. {
  139. try {
  140. for(unsigned int i=0;((i<_maxCustodyChainLength)&&(i<ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH));++i) {
  141. if (!(_custody[i].to)) {
  142. Buffer<(sizeof(Capability) * 2)> tmp;
  143. this->serialize(tmp,true);
  144. _custody[i].to = to;
  145. _custody[i].from = from.address();
  146. _custody[i].signature = from.sign(tmp.data(),tmp.size());
  147. return true;
  148. }
  149. }
  150. } catch ( ... ) {}
  151. return false;
  152. }
  153. /**
  154. * Verify this capability's chain of custody and signatures
  155. *
  156. * @param RR Runtime environment to provide for peer lookup, etc.
  157. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or chain
  158. */
  159. int verify(const RuntimeEnvironment *RR,void *tPtr) const;
  160. template<unsigned int C>
  161. static inline void serializeRules(Buffer<C> &b,const ZT_VirtualNetworkRule *rules,unsigned int ruleCount)
  162. {
  163. for(unsigned int i=0;i<ruleCount;++i) {
  164. // Each rule consists of its 8-bit type followed by the size of that type's
  165. // field followed by field data. The inclusion of the size will allow non-supported
  166. // rules to be ignored but still parsed.
  167. b.append((uint8_t)rules[i].t);
  168. switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x3f)) {
  169. default:
  170. b.append((uint8_t)0);
  171. break;
  172. case ZT_NETWORK_RULE_ACTION_TEE:
  173. case ZT_NETWORK_RULE_ACTION_WATCH:
  174. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  175. b.append((uint8_t)14);
  176. b.append((uint64_t)rules[i].v.fwd.address);
  177. b.append((uint32_t)rules[i].v.fwd.flags);
  178. b.append((uint16_t)rules[i].v.fwd.length); // unused for redirect
  179. break;
  180. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  181. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  182. b.append((uint8_t)5);
  183. Address(rules[i].v.zt).appendTo(b);
  184. break;
  185. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  186. b.append((uint8_t)2);
  187. b.append((uint16_t)rules[i].v.vlanId);
  188. break;
  189. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  190. b.append((uint8_t)1);
  191. b.append((uint8_t)rules[i].v.vlanPcp);
  192. break;
  193. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  194. b.append((uint8_t)1);
  195. b.append((uint8_t)rules[i].v.vlanDei);
  196. break;
  197. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  198. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  199. b.append((uint8_t)6);
  200. b.append(rules[i].v.mac,6);
  201. break;
  202. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  203. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  204. b.append((uint8_t)5);
  205. b.append(&(rules[i].v.ipv4.ip),4);
  206. b.append((uint8_t)rules[i].v.ipv4.mask);
  207. break;
  208. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  209. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  210. b.append((uint8_t)17);
  211. b.append(rules[i].v.ipv6.ip,16);
  212. b.append((uint8_t)rules[i].v.ipv6.mask);
  213. break;
  214. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  215. b.append((uint8_t)3);
  216. b.append((uint8_t)rules[i].v.ipTos.mask);
  217. b.append((uint8_t)rules[i].v.ipTos.value[0]);
  218. b.append((uint8_t)rules[i].v.ipTos.value[1]);
  219. break;
  220. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  221. b.append((uint8_t)1);
  222. b.append((uint8_t)rules[i].v.ipProtocol);
  223. break;
  224. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  225. b.append((uint8_t)2);
  226. b.append((uint16_t)rules[i].v.etherType);
  227. break;
  228. case ZT_NETWORK_RULE_MATCH_ICMP:
  229. b.append((uint8_t)3);
  230. b.append((uint8_t)rules[i].v.icmp.type);
  231. b.append((uint8_t)rules[i].v.icmp.code);
  232. b.append((uint8_t)rules[i].v.icmp.flags);
  233. break;
  234. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  235. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  236. b.append((uint8_t)4);
  237. b.append((uint16_t)rules[i].v.port[0]);
  238. b.append((uint16_t)rules[i].v.port[1]);
  239. break;
  240. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  241. b.append((uint8_t)8);
  242. b.append((uint64_t)rules[i].v.characteristics);
  243. break;
  244. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  245. b.append((uint8_t)4);
  246. b.append((uint16_t)rules[i].v.frameSize[0]);
  247. b.append((uint16_t)rules[i].v.frameSize[1]);
  248. break;
  249. case ZT_NETWORK_RULE_MATCH_RANDOM:
  250. b.append((uint8_t)4);
  251. b.append((uint32_t)rules[i].v.randomProbability);
  252. break;
  253. case ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE:
  254. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND:
  255. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR:
  256. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR:
  257. case ZT_NETWORK_RULE_MATCH_TAGS_EQUAL:
  258. case ZT_NETWORK_RULE_MATCH_TAG_SENDER:
  259. case ZT_NETWORK_RULE_MATCH_TAG_RECEIVER:
  260. b.append((uint8_t)8);
  261. b.append((uint32_t)rules[i].v.tag.id);
  262. b.append((uint32_t)rules[i].v.tag.value);
  263. break;
  264. case ZT_NETWORK_RULE_MATCH_INTEGER_RANGE:
  265. b.append((uint8_t)19);
  266. b.append((uint64_t)rules[i].v.intRange.start);
  267. b.append((uint64_t)(rules[i].v.intRange.start + (uint64_t)rules[i].v.intRange.end)); // more future-proof
  268. b.append((uint16_t)rules[i].v.intRange.idx);
  269. b.append((uint8_t)rules[i].v.intRange.format);
  270. break;
  271. }
  272. }
  273. }
  274. template<unsigned int C>
  275. static inline void deserializeRules(const Buffer<C> &b,unsigned int &p,ZT_VirtualNetworkRule *rules,unsigned int &ruleCount,const unsigned int maxRuleCount)
  276. {
  277. while ((ruleCount < maxRuleCount)&&(p < b.size())) {
  278. rules[ruleCount].t = (uint8_t)b[p++];
  279. const unsigned int fieldLen = (unsigned int)b[p++];
  280. switch((ZT_VirtualNetworkRuleType)(rules[ruleCount].t & 0x3f)) {
  281. default:
  282. break;
  283. case ZT_NETWORK_RULE_ACTION_TEE:
  284. case ZT_NETWORK_RULE_ACTION_WATCH:
  285. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  286. rules[ruleCount].v.fwd.address = b.template at<uint64_t>(p);
  287. rules[ruleCount].v.fwd.flags = b.template at<uint32_t>(p + 8);
  288. rules[ruleCount].v.fwd.length = b.template at<uint16_t>(p + 12);
  289. break;
  290. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  291. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  292. rules[ruleCount].v.zt = Address(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH).toInt();
  293. break;
  294. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  295. rules[ruleCount].v.vlanId = b.template at<uint16_t>(p);
  296. break;
  297. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  298. rules[ruleCount].v.vlanPcp = (uint8_t)b[p];
  299. break;
  300. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  301. rules[ruleCount].v.vlanDei = (uint8_t)b[p];
  302. break;
  303. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  304. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  305. ZT_FAST_MEMCPY(rules[ruleCount].v.mac,b.field(p,6),6);
  306. break;
  307. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  308. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  309. ZT_FAST_MEMCPY(&(rules[ruleCount].v.ipv4.ip),b.field(p,4),4);
  310. rules[ruleCount].v.ipv4.mask = (uint8_t)b[p + 4];
  311. break;
  312. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  313. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  314. ZT_FAST_MEMCPY(rules[ruleCount].v.ipv6.ip,b.field(p,16),16);
  315. rules[ruleCount].v.ipv6.mask = (uint8_t)b[p + 16];
  316. break;
  317. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  318. rules[ruleCount].v.ipTos.mask = (uint8_t)b[p];
  319. rules[ruleCount].v.ipTos.value[0] = (uint8_t)b[p+1];
  320. rules[ruleCount].v.ipTos.value[1] = (uint8_t)b[p+2];
  321. break;
  322. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  323. rules[ruleCount].v.ipProtocol = (uint8_t)b[p];
  324. break;
  325. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  326. rules[ruleCount].v.etherType = b.template at<uint16_t>(p);
  327. break;
  328. case ZT_NETWORK_RULE_MATCH_ICMP:
  329. rules[ruleCount].v.icmp.type = (uint8_t)b[p];
  330. rules[ruleCount].v.icmp.code = (uint8_t)b[p+1];
  331. rules[ruleCount].v.icmp.flags = (uint8_t)b[p+2];
  332. break;
  333. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  334. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  335. rules[ruleCount].v.port[0] = b.template at<uint16_t>(p);
  336. rules[ruleCount].v.port[1] = b.template at<uint16_t>(p + 2);
  337. break;
  338. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  339. rules[ruleCount].v.characteristics = b.template at<uint64_t>(p);
  340. break;
  341. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  342. rules[ruleCount].v.frameSize[0] = b.template at<uint16_t>(p);
  343. rules[ruleCount].v.frameSize[1] = b.template at<uint16_t>(p + 2);
  344. break;
  345. case ZT_NETWORK_RULE_MATCH_RANDOM:
  346. rules[ruleCount].v.randomProbability = b.template at<uint32_t>(p);
  347. break;
  348. case ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE:
  349. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND:
  350. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR:
  351. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR:
  352. case ZT_NETWORK_RULE_MATCH_TAGS_EQUAL:
  353. case ZT_NETWORK_RULE_MATCH_TAG_SENDER:
  354. case ZT_NETWORK_RULE_MATCH_TAG_RECEIVER:
  355. rules[ruleCount].v.tag.id = b.template at<uint32_t>(p);
  356. rules[ruleCount].v.tag.value = b.template at<uint32_t>(p + 4);
  357. break;
  358. case ZT_NETWORK_RULE_MATCH_INTEGER_RANGE:
  359. rules[ruleCount].v.intRange.start = b.template at<uint64_t>(p);
  360. rules[ruleCount].v.intRange.end = (uint32_t)(b.template at<uint64_t>(p + 8) - rules[ruleCount].v.intRange.start);
  361. rules[ruleCount].v.intRange.idx = b.template at<uint16_t>(p + 16);
  362. rules[ruleCount].v.intRange.format = (uint8_t)b[p + 18];
  363. break;
  364. }
  365. p += fieldLen;
  366. ++ruleCount;
  367. }
  368. }
  369. template<unsigned int C>
  370. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  371. {
  372. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  373. // These are the same between Tag and Capability
  374. b.append(_nwid);
  375. b.append(_ts);
  376. b.append(_id);
  377. b.append((uint16_t)_ruleCount);
  378. serializeRules(b,_rules,_ruleCount);
  379. b.append((uint8_t)_maxCustodyChainLength);
  380. if (!forSign) {
  381. for(unsigned int i=0;;++i) {
  382. if ((i < _maxCustodyChainLength)&&(i < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH)&&(_custody[i].to)) {
  383. _custody[i].to.appendTo(b);
  384. _custody[i].from.appendTo(b);
  385. b.append((uint8_t)1); // 1 == Ed25519 signature
  386. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
  387. b.append(_custody[i].signature.data,ZT_C25519_SIGNATURE_LEN);
  388. } else {
  389. b.append((unsigned char)0,ZT_ADDRESS_LENGTH); // zero 'to' terminates chain
  390. break;
  391. }
  392. }
  393. }
  394. // This is the size of any additional fields, currently 0.
  395. b.append((uint16_t)0);
  396. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  397. }
  398. template<unsigned int C>
  399. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  400. {
  401. memset(this,0,sizeof(Capability));
  402. unsigned int p = startAt;
  403. _nwid = b.template at<uint64_t>(p); p += 8;
  404. _ts = b.template at<uint64_t>(p); p += 8;
  405. _id = b.template at<uint32_t>(p); p += 4;
  406. const unsigned int rc = b.template at<uint16_t>(p); p += 2;
  407. if (rc > ZT_MAX_CAPABILITY_RULES)
  408. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  409. deserializeRules(b,p,_rules,_ruleCount,rc);
  410. _maxCustodyChainLength = (unsigned int)b[p++];
  411. if ((_maxCustodyChainLength < 1)||(_maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  412. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  413. for(unsigned int i=0;;++i) {
  414. const Address to(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  415. if (!to)
  416. break;
  417. if ((i >= _maxCustodyChainLength)||(i >= ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  418. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  419. _custody[i].to = to;
  420. _custody[i].from.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  421. if (b[p++] == 1) {
  422. if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN)
  423. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  424. p += 2;
  425. ZT_FAST_MEMCPY(_custody[i].signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  426. } else {
  427. p += 2 + b.template at<uint16_t>(p);
  428. }
  429. }
  430. p += 2 + b.template at<uint16_t>(p);
  431. if (p > b.size())
  432. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  433. return (p - startAt);
  434. }
  435. // Provides natural sort order by ID
  436. inline bool operator<(const Capability &c) const { return (_id < c._id); }
  437. inline bool operator==(const Capability &c) const { return (memcmp(this,&c,sizeof(Capability)) == 0); }
  438. inline bool operator!=(const Capability &c) const { return (memcmp(this,&c,sizeof(Capability)) != 0); }
  439. private:
  440. uint64_t _nwid;
  441. int64_t _ts;
  442. uint32_t _id;
  443. unsigned int _maxCustodyChainLength;
  444. unsigned int _ruleCount;
  445. ZT_VirtualNetworkRule _rules[ZT_MAX_CAPABILITY_RULES];
  446. struct {
  447. Address to;
  448. Address from;
  449. C25519::Signature signature;
  450. } _custody[ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH];
  451. };
  452. } // namespace ZeroTier
  453. #endif