Capability.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 the sender was allowed to send this.
  50. * (2) Evaluates its own capabilities to determine if it should receive
  51. * and process 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 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 expiration Expiration relative to network config timestamp
  68. * @param name Capability short name (max strlen == ZT_MAX_CAPABILITY_NAME_LENGTH, overflow ignored)
  69. * @param mccl Maximum custody chain length (1 to create non-transferrable capability)
  70. * @param rules Network flow rules for this capability
  71. * @param ruleCount Number of flow rules
  72. */
  73. Capability(uint32_t id,uint64_t nwid,uint64_t expiration,const char *name,unsigned int mccl,const ZT_VirtualNetworkRule *rules,unsigned int ruleCount)
  74. {
  75. memset(this,0,sizeof(Capability));
  76. _nwid = nwid;
  77. _expiration = expiration;
  78. _id = id;
  79. _maxCustodyChainLength = (mccl > 0) ? ((mccl < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH) ? mccl : (unsigned int)ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH) : 1;
  80. _ruleCount = (ruleCount < ZT_MAX_CAPABILITY_RULES) ? ruleCount : ZT_MAX_CAPABILITY_RULES;
  81. if (_ruleCount)
  82. memcpy(_rules,rules,sizeof(ZT_VirtualNetworkRule) * _ruleCount);
  83. }
  84. /**
  85. * @return Rules -- see ruleCount() for size of array
  86. */
  87. inline const ZT_VirtualNetworkRule *rules() const { return _rules; }
  88. /**
  89. * @return Number of rules in rules()
  90. */
  91. inline unsigned int ruleCount() const { return _ruleCount; }
  92. /**
  93. * @return ID and evaluation order of this capability in network
  94. */
  95. inline uint32_t id() const { return _id; }
  96. /**
  97. * @return Network ID for which this capability was issued
  98. */
  99. inline uint64_t networkId() const { return _nwid; }
  100. /**
  101. * Sign this capability and add signature to its chain of custody
  102. *
  103. * If this returns false, this object should be considered to be
  104. * in an undefined state and should be discarded. False can be returned
  105. * if there is no more room for signatures (max chain length reached)
  106. * or if the 'from' identity does not include a secret key to allow
  107. * it to sign anything.
  108. *
  109. * @param from Signing identity (must have secret)
  110. * @param to Recipient of this signature
  111. * @return True if signature successful and chain of custody appended
  112. */
  113. inline bool sign(const Identity &from,const Address &to)
  114. {
  115. try {
  116. Buffer<(sizeof(Capability) * 2)> tmp;
  117. for(unsigned int i=0;((i<_maxCustodyChainLength)&&(i<ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH));++i) {
  118. if (!(_custody[i].to)) {
  119. _custody[i].to = to;
  120. _custody[i].from = from.address();
  121. this->serialize(tmp,true);
  122. _custody[i].signature = from.sign(tmp.data(),tmp.size());
  123. return true;
  124. }
  125. }
  126. } catch ( ... ) {}
  127. return false;
  128. }
  129. /**
  130. * Verify this capability's chain of custody
  131. *
  132. * This returns a tri-state result. A return value of zero indicates that
  133. * the chain of custody is valid and all signatures are okay. A positive
  134. * return value means at least one WHOIS was issued for a missing signing
  135. * identity and we should retry later. A negative return value means that
  136. * this chain or one of its signature is BAD and this capability should
  137. * be discarded.
  138. *
  139. * Note that the entire chain is checked regardless of verifyInChain.
  140. *
  141. * @param RR Runtime environment to provide for peer lookup, etc.
  142. * @param verifyInChain Also check to ensure that this capability was at some point properly issued to this peer (if non-null)
  143. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or chain
  144. */
  145. int verify(const RuntimeEnvironment *RR,const Address &verifyInChain) const;
  146. template<unsigned int C>
  147. static inline void serializeRules(Buffer<C> &b,const ZT_VirtualNetworkRule *rules,unsigned int ruleCount)
  148. {
  149. b.append((uint16_t)ruleCount);
  150. for(unsigned int i=0;i<ruleCount;++i) {
  151. // Each rule consists of its 8-bit type followed by the size of that type's
  152. // field followed by field data. The inclusion of the size will allow non-supported
  153. // rules to be ignored but still parsed.
  154. b.append((uint8_t)rules[i].t);
  155. switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) {
  156. //case ZT_NETWORK_RULE_ACTION_DROP:
  157. //case ZT_NETWORK_RULE_ACTION_ACCEPT:
  158. default:
  159. b.append((uint8_t)0);
  160. break;
  161. case ZT_NETWORK_RULE_ACTION_TEE:
  162. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  163. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  164. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  165. b.append((uint8_t)5);
  166. Address(rules[i].v.zt).appendTo(b);
  167. break;
  168. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  169. b.append((uint8_t)2);
  170. b.append((uint16_t)rules[i].v.vlanId);
  171. break;
  172. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  173. b.append((uint8_t)1);
  174. b.append((uint8_t)rules[i].v.vlanPcp);
  175. break;
  176. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  177. b.append((uint8_t)1);
  178. b.append((uint8_t)rules[i].v.vlanDei);
  179. break;
  180. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  181. b.append((uint8_t)2);
  182. b.append((uint16_t)rules[i].v.etherType);
  183. break;
  184. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  185. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  186. b.append((uint8_t)6);
  187. b.append(rules[i].v.mac,6);
  188. break;
  189. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  190. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  191. b.append((uint8_t)5);
  192. b.append(&(rules[i].v.ipv4.ip),4);
  193. b.append((uint8_t)rules[i].v.ipv4.mask);
  194. break;
  195. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  196. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  197. b.append((uint8_t)17);
  198. b.append(rules[i].v.ipv6.ip,16);
  199. b.append((uint8_t)rules[i].v.ipv6.mask);
  200. break;
  201. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  202. b.append((uint8_t)1);
  203. b.append((uint8_t)rules[i].v.ipTos);
  204. break;
  205. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  206. b.append((uint8_t)1);
  207. b.append((uint8_t)rules[i].v.ipProtocol);
  208. break;
  209. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  210. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  211. b.append((uint8_t)4);
  212. b.append((uint16_t)rules[i].v.port[0]);
  213. b.append((uint16_t)rules[i].v.port[1]);
  214. break;
  215. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  216. b.append((uint8_t)16);
  217. b.append((uint64_t)rules[i].v.characteristics[0]);
  218. b.append((uint64_t)rules[i].v.characteristics[1]);
  219. break;
  220. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  221. b.append((uint8_t)4);
  222. b.append((uint16_t)rules[i].v.frameSize[0]);
  223. b.append((uint16_t)rules[i].v.frameSize[1]);
  224. break;
  225. case ZT_NETWORK_RULE_MATCH_TAG_VALUE_RANGE:
  226. b.append((uint8_t)12);
  227. b.append((uint32_t)rules[i].v.tag.id);
  228. b.append((uint32_t)rules[i].v.tag.value[0]);
  229. b.append((uint32_t)rules[i].v.tag.value[1]);
  230. break;
  231. case ZT_NETWORK_RULE_MATCH_TAG_VALUE_BITS_ALL:
  232. case ZT_NETWORK_RULE_MATCH_TAG_VALUE_BITS_ANY:
  233. b.append((uint8_t)8);
  234. b.append((uint32_t)rules[i].v.tag.id);
  235. b.append((uint32_t)rules[i].v.tag.value[0]);
  236. break;
  237. }
  238. }
  239. }
  240. template<unsigned int C>
  241. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  242. {
  243. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  244. b.append(_id);
  245. b.append(_nwid);
  246. b.append(_expiration);
  247. serializeRules(b,_rules,_ruleCount);
  248. b.append((uint8_t)_maxCustodyChainLength);
  249. for(unsigned int i=0;;++i) {
  250. if ((i < _maxCustodyChainLength)&&(i < ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH)&&(_custody[i].to)) {
  251. _custody[i].to.appendTo(b);
  252. _custody[i].from.appendTo(b);
  253. if (!forSign) {
  254. b.append((uint8_t)1); // 1 == Ed25519 signature
  255. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
  256. b.append(_custody[i].signature.data,ZT_C25519_SIGNATURE_LEN);
  257. }
  258. } else {
  259. b.append((unsigned char)0,ZT_ADDRESS_LENGTH); // zero 'to' terminates chain
  260. break;
  261. }
  262. }
  263. // This is the size of any additional fields. If it is nonzero,
  264. // the last 2 bytes of the next field will be another size field.
  265. b.append((uint16_t)0);
  266. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  267. }
  268. template<unsigned int C>
  269. static inline void deserializeRules(const Buffer<C> &b,unsigned int &p,ZT_VirtualNetworkRule *rules,unsigned int &ruleCount,const unsigned int maxRuleCount)
  270. {
  271. ruleCount = b.template at<uint16_t>(p); p += 2;
  272. if (ruleCount > maxRuleCount)
  273. throw std::runtime_error("rule count overflow");
  274. for(unsigned int i=0;i<ruleCount;++i) {
  275. rules[i].t = (uint8_t)b[p++];
  276. const unsigned int fieldLen = (unsigned int)b[p++];
  277. switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) {
  278. default:
  279. break;
  280. case ZT_NETWORK_RULE_ACTION_TEE:
  281. case ZT_NETWORK_RULE_ACTION_REDIRECT:
  282. case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
  283. case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
  284. rules[i].v.zt = Address(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH).toInt();
  285. break;
  286. case ZT_NETWORK_RULE_MATCH_VLAN_ID:
  287. rules[i].v.vlanId = b.template at<uint16_t>(p);
  288. break;
  289. case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
  290. rules[i].v.vlanPcp = (uint8_t)b[p];
  291. break;
  292. case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
  293. rules[i].v.vlanDei = (uint8_t)b[p];
  294. break;
  295. case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
  296. rules[i].v.etherType = b.template at<uint16_t>(p);
  297. break;
  298. case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
  299. case ZT_NETWORK_RULE_MATCH_MAC_DEST:
  300. memcpy(rules[i].v.mac,b.field(p,6),6);
  301. break;
  302. case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
  303. case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
  304. memcpy(&(rules[i].v.ipv4.ip),b.field(p,4),4);
  305. rules[i].v.ipv4.mask = (uint8_t)b[p + 4];
  306. break;
  307. case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
  308. case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
  309. memcpy(rules[i].v.ipv6.ip,b.field(p,16),16);
  310. rules[i].v.ipv6.mask = (uint8_t)b[p + 16];
  311. break;
  312. case ZT_NETWORK_RULE_MATCH_IP_TOS:
  313. rules[i].v.ipTos = (uint8_t)b[p];
  314. break;
  315. case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
  316. rules[i].v.ipProtocol = (uint8_t)b[p];
  317. break;
  318. case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
  319. case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
  320. rules[i].v.port[0] = b.template at<uint16_t>(p);
  321. rules[i].v.port[1] = b.template at<uint16_t>(p + 2);
  322. break;
  323. case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
  324. rules[i].v.characteristics[0] = b.template at<uint64_t>(p);
  325. rules[i].v.characteristics[1] = b.template at<uint64_t>(p + 8);
  326. break;
  327. case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
  328. rules[i].v.frameSize[0] = b.template at<uint16_t>(p);
  329. rules[i].v.frameSize[0] = b.template at<uint16_t>(p + 2);
  330. break;
  331. case ZT_NETWORK_RULE_MATCH_TAG_VALUE_RANGE:
  332. rules[i].v.tag.id = b.template at<uint32_t>(p);
  333. rules[i].v.tag.value[0] = b.template at<uint32_t>(p + 4);
  334. rules[i].v.tag.value[1] = b.template at<uint32_t>(p + 8);
  335. break;
  336. case ZT_NETWORK_RULE_MATCH_TAG_VALUE_BITS_ALL:
  337. case ZT_NETWORK_RULE_MATCH_TAG_VALUE_BITS_ANY:
  338. rules[i].v.tag.id = b.template at<uint32_t>(p);
  339. rules[i].v.tag.value[0] = b.template at<uint32_t>(p + 4);
  340. break;
  341. }
  342. p += fieldLen;
  343. }
  344. }
  345. template<unsigned int C>
  346. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  347. {
  348. memset(this,0,sizeof(Capability));
  349. unsigned int p = startAt;
  350. _id = b.template at<uint32_t>(p); p += 4;
  351. _nwid = b.template at<uint64_t>(p); p += 8;
  352. _expiration = b.template at<uint64_t>(p); p += 8;
  353. deserializeRules(b,p,_rules,_ruleCount,ZT_MAX_CAPABILITY_RULES);
  354. _maxCustodyChainLength = (unsigned int)b[p++];
  355. if ((_maxCustodyChainLength < 1)||(_maxCustodyChainLength > ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  356. throw std::runtime_error("invalid max custody chain length");
  357. for(unsigned int i;;++i) {
  358. const Address to(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  359. if (!to)
  360. break;
  361. if ((i >= _maxCustodyChainLength)||(i >= ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH))
  362. throw std::runtime_error("unterminated custody chain");
  363. _custody[i].to = to;
  364. _custody[i].from.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  365. memcpy(_custody[i].signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  366. }
  367. p += 2 + b.template at<uint16_t>(p);
  368. if (p > b.size())
  369. throw std::runtime_error("extended field overflow");
  370. return (p - startAt);
  371. }
  372. // Provides natural sort order by ID
  373. inline bool operator<(const Capability &c) const { return (_id < c._id); }
  374. private:
  375. uint64_t _nwid;
  376. uint64_t _expiration;
  377. uint32_t _id;
  378. unsigned int _maxCustodyChainLength;
  379. unsigned int _ruleCount;
  380. ZT_VirtualNetworkRule _rules[ZT_MAX_CAPABILITY_RULES];
  381. struct {
  382. Address to;
  383. Address from;
  384. C25519::Signature signature;
  385. } _custody[ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH];
  386. };
  387. } // namespace ZeroTier
  388. #endif