Tag.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_TAG_HPP
  14. #define ZT_TAG_HPP
  15. #include "Address.hpp"
  16. #include "Buffer.hpp"
  17. #include "C25519.hpp"
  18. #include "Constants.hpp"
  19. #include "Credential.hpp"
  20. #include "Identity.hpp"
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. namespace ZeroTier {
  26. class RuntimeEnvironment;
  27. /**
  28. * A tag that can be associated with members and matched in rules
  29. *
  30. * Capabilities group rules, while tags group members subject to those
  31. * rules. Tag values can be matched in rules, and tags relevant to a
  32. * capability are presented along with it.
  33. *
  34. * E.g. a capability might be "can speak Samba/CIFS within your
  35. * department." This cap might have a rule to allow TCP/137 but
  36. * only if a given tag ID's value matches between two peers. The
  37. * capability is what members can do, while the tag is who they are.
  38. * Different departments might have tags with the same ID but different
  39. * values.
  40. *
  41. * Unlike capabilities tags are signed only by the issuer and are never
  42. * transferable.
  43. */
  44. class Tag : public Credential {
  45. public:
  46. static inline Credential::Type credentialType()
  47. {
  48. return Credential::CREDENTIAL_TYPE_TAG;
  49. }
  50. Tag() : _id(0), _value(0), _networkId(0), _ts(0)
  51. {
  52. memset(_signature.data, 0, sizeof(_signature.data));
  53. }
  54. /**
  55. * @param nwid Network ID
  56. * @param ts Timestamp
  57. * @param issuedTo Address to which this tag was issued
  58. * @param id Tag ID
  59. * @param value Tag value
  60. */
  61. Tag(const uint64_t nwid, const int64_t ts, const Address& issuedTo, const uint32_t id, const uint32_t value) : _id(id), _value(value), _networkId(nwid), _ts(ts), _issuedTo(issuedTo), _signedBy()
  62. {
  63. memset(_signature.data, 0, sizeof(_signature.data));
  64. }
  65. inline uint32_t id() const
  66. {
  67. return _id;
  68. }
  69. inline const uint32_t& value() const
  70. {
  71. return _value;
  72. }
  73. inline uint64_t networkId() const
  74. {
  75. return _networkId;
  76. }
  77. inline int64_t timestamp() const
  78. {
  79. return _ts;
  80. }
  81. inline const Address& issuedTo() const
  82. {
  83. return _issuedTo;
  84. }
  85. inline const Address& signedBy() const
  86. {
  87. return _signedBy;
  88. }
  89. /**
  90. * Sign this tag
  91. *
  92. * @param signer Signing identity, must have private key
  93. * @return True if signature was successful
  94. */
  95. inline bool sign(const Identity& signer)
  96. {
  97. if (signer.hasPrivate()) {
  98. Buffer<sizeof(Tag) + 64> tmp;
  99. _signedBy = signer.address();
  100. this->serialize(tmp, true);
  101. _signature = signer.sign(tmp.data(), tmp.size());
  102. return true;
  103. }
  104. return false;
  105. }
  106. /**
  107. * Check this tag's signature
  108. *
  109. * @param RR Runtime environment to allow identity lookup for signedBy
  110. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  111. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or tag
  112. */
  113. int verify(const RuntimeEnvironment* RR, void* tPtr) const;
  114. template <unsigned int C> inline void serialize(Buffer<C>& b, const bool forSign = false) const
  115. {
  116. if (forSign) {
  117. b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  118. }
  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) {
  132. b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  133. }
  134. }
  135. template <unsigned int C> inline unsigned int deserialize(const Buffer<C>& b, unsigned int startAt = 0)
  136. {
  137. unsigned int p = startAt;
  138. *this = Tag();
  139. _networkId = b.template at<uint64_t>(p);
  140. p += 8;
  141. _ts = b.template at<uint64_t>(p);
  142. p += 8;
  143. _id = b.template at<uint32_t>(p);
  144. p += 4;
  145. _value = b.template at<uint32_t>(p);
  146. p += 4;
  147. _issuedTo.setTo(b.field(p, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  148. p += ZT_ADDRESS_LENGTH;
  149. _signedBy.setTo(b.field(p, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  150. p += ZT_ADDRESS_LENGTH;
  151. if (b[p++] == 1) {
  152. if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN) {
  153. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  154. }
  155. p += 2;
  156. memcpy(_signature.data, b.field(p, ZT_C25519_SIGNATURE_LEN), ZT_C25519_SIGNATURE_LEN);
  157. p += ZT_C25519_SIGNATURE_LEN;
  158. }
  159. else {
  160. p += 2 + b.template at<uint16_t>(p);
  161. }
  162. p += 2 + b.template at<uint16_t>(p);
  163. if (p > b.size()) {
  164. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  165. }
  166. return (p - startAt);
  167. }
  168. // Provides natural sort order by ID
  169. inline bool operator<(const Tag& t) const
  170. {
  171. return (_id < t._id);
  172. }
  173. inline bool operator==(const Tag& t) const
  174. {
  175. return (memcmp(this, &t, sizeof(Tag)) == 0);
  176. }
  177. inline bool operator!=(const Tag& t) const
  178. {
  179. return (memcmp(this, &t, sizeof(Tag)) != 0);
  180. }
  181. // For searching sorted arrays or lists of Tags by ID
  182. struct IdComparePredicate {
  183. inline bool operator()(const Tag& a, const Tag& b) const
  184. {
  185. return (a.id() < b.id());
  186. }
  187. inline bool operator()(const uint32_t a, const Tag& b) const
  188. {
  189. return (a < b.id());
  190. }
  191. inline bool operator()(const Tag& a, const uint32_t b) const
  192. {
  193. return (a.id() < b);
  194. }
  195. inline bool operator()(const Tag* a, const Tag* b) const
  196. {
  197. return (a->id() < b->id());
  198. }
  199. inline bool operator()(const Tag* a, const Tag& b) const
  200. {
  201. return (a->id() < b.id());
  202. }
  203. inline bool operator()(const Tag& a, const Tag* b) const
  204. {
  205. return (a.id() < b->id());
  206. }
  207. inline bool operator()(const uint32_t a, const Tag* b) const
  208. {
  209. return (a < b->id());
  210. }
  211. inline bool operator()(const Tag* a, const uint32_t b) const
  212. {
  213. return (a->id() < b);
  214. }
  215. inline bool operator()(const uint32_t a, const uint32_t b) const
  216. {
  217. return (a < b);
  218. }
  219. };
  220. private:
  221. uint32_t _id;
  222. uint32_t _value;
  223. uint64_t _networkId;
  224. int64_t _ts;
  225. Address _issuedTo;
  226. Address _signedBy;
  227. C25519::Signature _signature;
  228. };
  229. } // namespace ZeroTier
  230. #endif