Tag.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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_TAG_HPP
  27. #define ZT_TAG_HPP
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "Constants.hpp"
  33. #include "Credential.hpp"
  34. #include "C25519.hpp"
  35. #include "Address.hpp"
  36. #include "Identity.hpp"
  37. #include "Buffer.hpp"
  38. namespace ZeroTier {
  39. class RuntimeEnvironment;
  40. /**
  41. * A tag that can be associated with members and matched in rules
  42. *
  43. * Capabilities group rules, while tags group members subject to those
  44. * rules. Tag values can be matched in rules, and tags relevant to a
  45. * capability are presented along with it.
  46. *
  47. * E.g. a capability might be "can speak Samba/CIFS within your
  48. * department." This cap might have a rule to allow TCP/137 but
  49. * only if a given tag ID's value matches between two peers. The
  50. * capability is what members can do, while the tag is who they are.
  51. * Different departments might have tags with the same ID but different
  52. * values.
  53. *
  54. * Unlike capabilities tags are signed only by the issuer and are never
  55. * transferable.
  56. */
  57. class Tag : public Credential
  58. {
  59. public:
  60. static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_TAG; }
  61. Tag() :
  62. _id(0),
  63. _value(0),
  64. _networkId(0),
  65. _ts(0)
  66. {
  67. }
  68. /**
  69. * @param nwid Network ID
  70. * @param ts Timestamp
  71. * @param issuedTo Address to which this tag was issued
  72. * @param id Tag ID
  73. * @param value Tag value
  74. */
  75. Tag(const uint64_t nwid,const int64_t ts,const Address &issuedTo,const uint32_t id,const uint32_t value) :
  76. _id(id),
  77. _value(value),
  78. _networkId(nwid),
  79. _ts(ts),
  80. _issuedTo(issuedTo),
  81. _signedBy()
  82. {
  83. }
  84. inline uint32_t id() const { return _id; }
  85. inline const uint32_t &value() const { return _value; }
  86. inline uint64_t networkId() const { return _networkId; }
  87. inline int64_t timestamp() const { return _ts; }
  88. inline const Address &issuedTo() const { return _issuedTo; }
  89. inline const Address &signedBy() const { return _signedBy; }
  90. /**
  91. * Sign this tag
  92. *
  93. * @param signer Signing identity, must have private key
  94. * @return True if signature was successful
  95. */
  96. inline bool sign(const Identity &signer)
  97. {
  98. if (signer.hasPrivate()) {
  99. Buffer<sizeof(Tag) + 64> tmp;
  100. _signedBy = signer.address();
  101. this->serialize(tmp,true);
  102. _signature = signer.sign(tmp.data(),tmp.size());
  103. return true;
  104. }
  105. return false;
  106. }
  107. /**
  108. * Check this tag's signature
  109. *
  110. * @param RR Runtime environment to allow identity lookup for signedBy
  111. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  112. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or tag
  113. */
  114. int verify(const RuntimeEnvironment *RR,void *tPtr) const;
  115. template<unsigned int C>
  116. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  117. {
  118. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  119. b.append(_networkId);
  120. b.append(_ts);
  121. b.append(_id);
  122. b.append(_value);
  123. _issuedTo.appendTo(b);
  124. _signedBy.appendTo(b);
  125. if (!forSign) {
  126. b.append((uint8_t)1); // 1 == Ed25519
  127. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
  128. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  129. }
  130. b.append((uint16_t)0); // length of additional fields, currently 0
  131. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  132. }
  133. template<unsigned int C>
  134. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  135. {
  136. unsigned int p = startAt;
  137. *this = Tag();
  138. _networkId = b.template at<uint64_t>(p); p += 8;
  139. _ts = b.template at<uint64_t>(p); p += 8;
  140. _id = b.template at<uint32_t>(p); p += 4;
  141. _value = b.template at<uint32_t>(p); p += 4;
  142. _issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  143. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  144. if (b[p++] == 1) {
  145. if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN)
  146. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  147. p += 2;
  148. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  149. } else {
  150. p += 2 + b.template at<uint16_t>(p);
  151. }
  152. p += 2 + b.template at<uint16_t>(p);
  153. if (p > b.size())
  154. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  155. return (p - startAt);
  156. }
  157. // Provides natural sort order by ID
  158. inline bool operator<(const Tag &t) const { return (_id < t._id); }
  159. inline bool operator==(const Tag &t) const { return (memcmp(this,&t,sizeof(Tag)) == 0); }
  160. inline bool operator!=(const Tag &t) const { return (memcmp(this,&t,sizeof(Tag)) != 0); }
  161. // For searching sorted arrays or lists of Tags by ID
  162. struct IdComparePredicate
  163. {
  164. inline bool operator()(const Tag &a,const Tag &b) const { return (a.id() < b.id()); }
  165. inline bool operator()(const uint32_t a,const Tag &b) const { return (a < b.id()); }
  166. inline bool operator()(const Tag &a,const uint32_t b) const { return (a.id() < b); }
  167. inline bool operator()(const Tag *a,const Tag *b) const { return (a->id() < b->id()); }
  168. inline bool operator()(const Tag *a,const Tag &b) const { return (a->id() < b.id()); }
  169. inline bool operator()(const Tag &a,const Tag *b) const { return (a.id() < b->id()); }
  170. inline bool operator()(const uint32_t a,const Tag *b) const { return (a < b->id()); }
  171. inline bool operator()(const Tag *a,const uint32_t b) const { return (a->id() < b); }
  172. inline bool operator()(const uint32_t a,const uint32_t b) const { return (a < b); }
  173. };
  174. private:
  175. uint32_t _id;
  176. uint32_t _value;
  177. uint64_t _networkId;
  178. int64_t _ts;
  179. Address _issuedTo;
  180. Address _signedBy;
  181. C25519::Signature _signature;
  182. };
  183. } // namespace ZeroTier
  184. #endif