Capability.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_CAPABILITY_HPP
  19. #define ZT_CAPABILITY_HPP
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "Constants.hpp"
  24. #include "Address.hpp"
  25. #include "C25519.hpp"
  26. #include "Utils.hpp"
  27. #include "Buffer.hpp"
  28. #include "Identity.hpp"
  29. #include "../include/ZeroTierOne.h"
  30. namespace ZeroTier {
  31. class RuntimeEnvironment;
  32. /**
  33. * A set of grouped and signed network flow rules
  34. *
  35. * On the sending side the sender does the following for each packet:
  36. *
  37. * (1) Evaluates its capabilities in ascending order of ID to determine
  38. * which capability allows it to transmit this packet.
  39. * (2) If it has not done so lately, it then sends this capability to the
  40. * receving peer ("presents" it).
  41. * (3) The sender then sends the packet.
  42. *
  43. * On the receiving side the receiver evaluates the capabilities presented
  44. * by the sender. If any valid un-expired capability allows this packet it
  45. * is accepted.
  46. *
  47. * Note that this is after evaluation of network scope rules and only if
  48. * network scope rules do not deliver an explicit match.
  49. */
  50. class Capability
  51. {
  52. public:
  53. Capability()
  54. {
  55. memset(this,0,sizeof(Capability));
  56. }
  57. /**
  58. * @param id Capability ID
  59. * @param nwid Network ID
  60. * @param ts Timestamp (at controller)
  61. * @param mccl Maximum custody chain length (1 to create non-transferrable capability)
  62. * @param rules Network flow rules for this capability
  63. * @param ruleCount Number of flow rules
  64. */
  65. Capability(uint32_t id,uint64_t nwid,uint64_t ts,unsigned int mccl,const ZT_VirtualNetworkRule *rules,unsigned int ruleCount)
  66. {
  67. memset(this,0,sizeof(Capability));
  68. _nwid = nwid;
  69. _ts = ts;
  70. _id = id;
  71. _maxCustodyChainLength = (mccl > 0) ? ((mccl < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH) ? mccl : (unsigned int)ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH) : 1;
  72. _ruleCount = (ruleCount < ZT_MAX_CAPABILITY_RULES) ? ruleCount : ZT_MAX_CAPABILITY_RULES;
  73. if (_ruleCount)
  74. memcpy(_rules,rules,sizeof(ZT_VirtualNetworkRule) * _ruleCount);
  75. }
  76. /**
  77. * @return Rules -- see ruleCount() for size of array
  78. */
  79. inline const ZT_VirtualNetworkRule *rules() const { return _rules; }
  80. /**
  81. * @return Number of rules in rules()
  82. */
  83. inline unsigned int ruleCount() const { return _ruleCount; }
  84. /**
  85. * @return ID and evaluation order of this capability in network
  86. */
  87. inline uint32_t id() const { return _id; }
  88. /**
  89. * @return Network ID for which this capability was issued
  90. */
  91. inline uint64_t networkId() const { return _nwid; }
  92. /**
  93. * @return Timestamp
  94. */
  95. inline uint64_t timestamp() const { return _ts; }
  96. /**
  97. * @return Last 'to' address in chain of custody
  98. */
  99. inline Address issuedTo() const
  100. {
  101. Address i2;
  102. for(unsigned 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. inline bool sign(const Identity &from,const Address &to)
  123. {
  124. try {
  125. for(unsigned int i=0;((i<_maxCustodyChainLength)&&(i<ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH));++i) {
  126. if (!(_custody[i].to)) {
  127. Buffer<(sizeof(Capability) * 2)> tmp;
  128. this->serialize(tmp,true);
  129. _custody[i].to = to;
  130. _custody[i].from = from.address();
  131. _custody[i].signature = from.sign(tmp.data(),tmp.size());
  132. return true;
  133. }
  134. }
  135. } catch ( ... ) {}
  136. return false;
  137. }
  138. /**
  139. * Verify this capability's chain of custody and signatures
  140. *
  141. * @param RR Runtime environment to provide for peer lookup, etc.
  142. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or chain
  143. */
  144. int verify(const RuntimeEnvironment *RR) const;
  145. template<unsigned int C>
  146. static inline void serializeRules(Buffer<C> &b,const ZT_VirtualNetworkRule *rules,unsigned int ruleCount)
  147. {
  148. for(unsigned int i=0;i<ruleCount;++i) {
  149. // Each rule consists of its 8-bit type followed by the size of that type's
  150. // field followed by field data. The inclusion of the size will allow non-supported
  151. // rules to be ignored but still parsed.
  152. b.append((uint8_t)rules[i].t);
  153. switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x3f)) {
  154. //case ZT_NETWORK_RULE_ACTION_DROP:
  155. //case ZT_NETWORK_RULE_ACTION_ACCEPT:
  156. //case ZT_NETWORK_RULE_ACTION_DEBUG_LOG:
  157. default:
  158. b.append((uint8_t)0);
  159. break;
  160. case ZT_NETWORK_RULE_ACTION_TEE:
  161. case ZT_NETWORK_RULE_ACTION_WATCH:
  162. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  163. b.append((uint8_t)14);
  164. b.append((uint64_t)rules[i].v.fwd.address);
  165. b.append((uint32_t)rules[i].v.fwd.flags);
  166. b.append((uint16_t)rules[i].v.fwd.length); // unused for redirect
  167. break;
  168. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  169. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  170. b.append((uint8_t)5);
  171. Address(rules[i].v.zt).appendTo(b);
  172. break;
  173. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  174. b.append((uint8_t)2);
  175. b.append((uint16_t)rules[i].v.vlanId);
  176. break;
  177. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  178. b.append((uint8_t)1);
  179. b.append((uint8_t)rules[i].v.vlanPcp);
  180. break;
  181. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  182. b.append((uint8_t)1);
  183. b.append((uint8_t)rules[i].v.vlanDei);
  184. break;
  185. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  186. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  187. b.append((uint8_t)6);
  188. b.append(rules[i].v.mac,6);
  189. break;
  190. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  191. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  192. b.append((uint8_t)5);
  193. b.append(&(rules[i].v.ipv4.ip),4);
  194. b.append((uint8_t)rules[i].v.ipv4.mask);
  195. break;
  196. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  197. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  198. b.append((uint8_t)17);
  199. b.append(rules[i].v.ipv6.ip,16);
  200. b.append((uint8_t)rules[i].v.ipv6.mask);
  201. break;
  202. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  203. b.append((uint8_t)3);
  204. b.append((uint8_t)rules[i].v.ipTos.mask);
  205. b.append((uint8_t)rules[i].v.ipTos.value[0]);
  206. b.append((uint8_t)rules[i].v.ipTos.value[1]);
  207. break;
  208. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  209. b.append((uint8_t)1);
  210. b.append((uint8_t)rules[i].v.ipProtocol);
  211. break;
  212. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  213. b.append((uint8_t)2);
  214. b.append((uint16_t)rules[i].v.etherType);
  215. break;
  216. case ZT_NETWORK_RULE_MATCH_ICMP:
  217. b.append((uint8_t)3);
  218. b.append((uint8_t)rules[i].v.icmp.type);
  219. b.append((uint8_t)rules[i].v.icmp.code);
  220. b.append((uint8_t)rules[i].v.icmp.flags);
  221. break;
  222. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  223. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  224. b.append((uint8_t)4);
  225. b.append((uint16_t)rules[i].v.port[0]);
  226. b.append((uint16_t)rules[i].v.port[1]);
  227. break;
  228. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  229. b.append((uint8_t)8);
  230. b.append((uint64_t)rules[i].v.characteristics);
  231. break;
  232. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  233. b.append((uint8_t)4);
  234. b.append((uint16_t)rules[i].v.frameSize[0]);
  235. b.append((uint16_t)rules[i].v.frameSize[1]);
  236. break;
  237. case ZT_NETWORK_RULE_MATCH_RANDOM:
  238. b.append((uint8_t)4);
  239. b.append((uint32_t)rules[i].v.randomProbability);
  240. break;
  241. case ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE:
  242. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND:
  243. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR:
  244. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR:
  245. b.append((uint8_t)8);
  246. b.append((uint32_t)rules[i].v.tag.id);
  247. b.append((uint32_t)rules[i].v.tag.value);
  248. break;
  249. }
  250. }
  251. }
  252. template<unsigned int C>
  253. static inline void deserializeRules(const Buffer<C> &b,unsigned int &p,ZT_VirtualNetworkRule *rules,unsigned int &ruleCount,const unsigned int maxRuleCount)
  254. {
  255. while ((ruleCount < maxRuleCount)&&(p < b.size())) {
  256. rules[ruleCount].t = (uint8_t)b[p++];
  257. const unsigned int fieldLen = (unsigned int)b[p++];
  258. switch((ZT_VirtualNetworkRuleType)(rules[ruleCount].t & 0x3f)) {
  259. default:
  260. break;
  261. case ZT_NETWORK_RULE_ACTION_TEE:
  262. case ZT_NETWORK_RULE_ACTION_WATCH:
  263. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  264. rules[ruleCount].v.fwd.address = b.template at<uint64_t>(p);
  265. rules[ruleCount].v.fwd.flags = b.template at<uint32_t>(p + 8);
  266. rules[ruleCount].v.fwd.length = b.template at<uint16_t>(p + 12);
  267. break;
  268. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  269. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  270. rules[ruleCount].v.zt = Address(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH).toInt();
  271. break;
  272. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  273. rules[ruleCount].v.vlanId = b.template at<uint16_t>(p);
  274. break;
  275. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  276. rules[ruleCount].v.vlanPcp = (uint8_t)b[p];
  277. break;
  278. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  279. rules[ruleCount].v.vlanDei = (uint8_t)b[p];
  280. break;
  281. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  282. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  283. memcpy(rules[ruleCount].v.mac,b.field(p,6),6);
  284. break;
  285. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  286. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  287. memcpy(&(rules[ruleCount].v.ipv4.ip),b.field(p,4),4);
  288. rules[ruleCount].v.ipv4.mask = (uint8_t)b[p + 4];
  289. break;
  290. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  291. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  292. memcpy(rules[ruleCount].v.ipv6.ip,b.field(p,16),16);
  293. rules[ruleCount].v.ipv6.mask = (uint8_t)b[p + 16];
  294. break;
  295. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  296. rules[ruleCount].v.ipTos.mask = (uint8_t)b[p];
  297. rules[ruleCount].v.ipTos.value[0] = (uint8_t)b[p+1];
  298. rules[ruleCount].v.ipTos.value[1] = (uint8_t)b[p+2];
  299. break;
  300. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  301. rules[ruleCount].v.ipProtocol = (uint8_t)b[p];
  302. break;
  303. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  304. rules[ruleCount].v.etherType = b.template at<uint16_t>(p);
  305. break;
  306. case ZT_NETWORK_RULE_MATCH_ICMP:
  307. rules[ruleCount].v.icmp.type = (uint8_t)b[p];
  308. rules[ruleCount].v.icmp.code = (uint8_t)b[p+1];
  309. rules[ruleCount].v.icmp.flags = (uint8_t)b[p+2];
  310. break;
  311. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  312. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  313. rules[ruleCount].v.port[0] = b.template at<uint16_t>(p);
  314. rules[ruleCount].v.port[1] = b.template at<uint16_t>(p + 2);
  315. break;
  316. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  317. rules[ruleCount].v.characteristics = b.template at<uint64_t>(p);
  318. break;
  319. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  320. rules[ruleCount].v.frameSize[0] = b.template at<uint16_t>(p);
  321. rules[ruleCount].v.frameSize[1] = b.template at<uint16_t>(p + 2);
  322. break;
  323. case ZT_NETWORK_RULE_MATCH_RANDOM:
  324. rules[ruleCount].v.randomProbability = b.template at<uint32_t>(p);
  325. break;
  326. case ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE:
  327. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND:
  328. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR:
  329. case ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR:
  330. case ZT_NETWORK_RULE_MATCH_TAGS_EQUAL:
  331. rules[ruleCount].v.tag.id = b.template at<uint32_t>(p);
  332. rules[ruleCount].v.tag.value = b.template at<uint32_t>(p + 4);
  333. break;
  334. }
  335. p += fieldLen;
  336. ++ruleCount;
  337. }
  338. }
  339. template<unsigned int C>
  340. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  341. {
  342. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  343. // These are the same between Tag and Capability
  344. b.append(_nwid);
  345. b.append(_ts);
  346. b.append(_id);
  347. b.append((uint16_t)_ruleCount);
  348. serializeRules(b,_rules,_ruleCount);
  349. b.append((uint8_t)_maxCustodyChainLength);
  350. if (!forSign) {
  351. for(unsigned int i=0;;++i) {
  352. if ((i < _maxCustodyChainLength)&&(i < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH)&&(_custody[i].to)) {
  353. _custody[i].to.appendTo(b);
  354. _custody[i].from.appendTo(b);
  355. b.append((uint8_t)1); // 1 == Ed25519 signature
  356. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
  357. b.append(_custody[i].signature.data,ZT_C25519_SIGNATURE_LEN);
  358. } else {
  359. b.append((unsigned char)0,ZT_ADDRESS_LENGTH); // zero 'to' terminates chain
  360. break;
  361. }
  362. }
  363. }
  364. // This is the size of any additional fields, currently 0.
  365. b.append((uint16_t)0);
  366. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  367. }
  368. template<unsigned int C>
  369. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  370. {
  371. memset(this,0,sizeof(Capability));
  372. unsigned int p = startAt;
  373. _nwid = b.template at<uint64_t>(p); p += 8;
  374. _ts = b.template at<uint64_t>(p); p += 8;
  375. _id = b.template at<uint32_t>(p); p += 4;
  376. const unsigned int rc = b.template at<uint16_t>(p); p += 2;
  377. if (rc > ZT_MAX_CAPABILITY_RULES)
  378. throw std::runtime_error("rule overflow");
  379. deserializeRules(b,p,_rules,_ruleCount,rc);
  380. _maxCustodyChainLength = (unsigned int)b[p++];
  381. if ((_maxCustodyChainLength < 1)||(_maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  382. throw std::runtime_error("invalid max custody chain length");
  383. for(unsigned int i=0;;++i) {
  384. const Address to(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  385. if (!to)
  386. break;
  387. if ((i >= _maxCustodyChainLength)||(i >= ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  388. throw std::runtime_error("unterminated custody chain");
  389. _custody[i].to = to;
  390. _custody[i].from.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  391. if (b[p++] == 1) {
  392. if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN)
  393. throw std::runtime_error("invalid signature");
  394. p += 2;
  395. memcpy(_custody[i].signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  396. } else {
  397. p += 2 + b.template at<uint16_t>(p);
  398. }
  399. }
  400. p += 2 + b.template at<uint16_t>(p);
  401. if (p > b.size())
  402. throw std::runtime_error("extended field overflow");
  403. return (p - startAt);
  404. }
  405. // Provides natural sort order by ID
  406. inline bool operator<(const Capability &c) const { return (_id < c._id); }
  407. inline bool operator==(const Capability &c) const { return (memcmp(this,&c,sizeof(Capability)) == 0); }
  408. inline bool operator!=(const Capability &c) const { return (memcmp(this,&c,sizeof(Capability)) != 0); }
  409. private:
  410. uint64_t _nwid;
  411. uint64_t _ts;
  412. uint32_t _id;
  413. unsigned int _maxCustodyChainLength;
  414. unsigned int _ruleCount;
  415. ZT_VirtualNetworkRule _rules[ZT_MAX_CAPABILITY_RULES];
  416. struct {
  417. Address to;
  418. Address from;
  419. C25519::Signature signature;
  420. } _custody[ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH];
  421. };
  422. } // namespace ZeroTier
  423. #endif