Capability.hpp 15 KB

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