Tag.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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: 2023-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 <stdint.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "Constants.hpp"
  20. #include "Credential.hpp"
  21. #include "C25519.hpp"
  22. #include "Address.hpp"
  23. #include "Identity.hpp"
  24. #include "Buffer.hpp"
  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. {
  46. friend class Credential;
  47. public:
  48. static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_TAG; }
  49. inline Tag() :
  50. _id(0),
  51. _value(0),
  52. _networkId(0),
  53. _ts(0),
  54. _signatureLength(0)
  55. {
  56. }
  57. /**
  58. * @param nwid Network ID
  59. * @param ts Timestamp
  60. * @param issuedTo Address to which this tag was issued
  61. * @param id Tag ID
  62. * @param value Tag value
  63. */
  64. inline Tag(const uint64_t nwid,const int64_t ts,const Address &issuedTo,const uint32_t id,const uint32_t value) :
  65. _id(id),
  66. _value(value),
  67. _networkId(nwid),
  68. _ts(ts),
  69. _issuedTo(issuedTo),
  70. _signedBy(),
  71. _signatureLength(0)
  72. {
  73. }
  74. ZT_ALWAYS_INLINE uint32_t id() const { return _id; }
  75. ZT_ALWAYS_INLINE const uint32_t &value() const { return _value; }
  76. ZT_ALWAYS_INLINE uint64_t networkId() const { return _networkId; }
  77. ZT_ALWAYS_INLINE int64_t timestamp() const { return _ts; }
  78. ZT_ALWAYS_INLINE const Address &issuedTo() const { return _issuedTo; }
  79. ZT_ALWAYS_INLINE const Address &signer() const { return _signedBy; }
  80. ZT_ALWAYS_INLINE const uint8_t *signature() const { return _signature; }
  81. ZT_ALWAYS_INLINE unsigned int signatureLength() const { return _signatureLength; }
  82. /**
  83. * Sign this tag
  84. *
  85. * @param signer Signing identity, must have private key
  86. * @return True if signature was successful
  87. */
  88. inline bool sign(const Identity &signer)
  89. {
  90. if (signer.hasPrivate()) {
  91. Buffer<sizeof(Tag) + 64> tmp;
  92. _signedBy = signer.address();
  93. this->serialize(tmp,true);
  94. _signatureLength = signer.sign(tmp.data(),tmp.size(),_signature,sizeof(_signature));
  95. return true;
  96. }
  97. return false;
  98. }
  99. /**
  100. * Check this tag's signature
  101. *
  102. * @param RR Runtime environment to allow identity lookup for signedBy
  103. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  104. */
  105. inline Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const { return _verify(RR,tPtr,*this); }
  106. template<unsigned int C>
  107. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  108. {
  109. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  110. b.append(_networkId);
  111. b.append(_ts);
  112. b.append(_id);
  113. b.append(_value);
  114. _issuedTo.appendTo(b);
  115. _signedBy.appendTo(b);
  116. if (!forSign) {
  117. b.append((uint8_t)1);
  118. b.append((uint16_t)_signatureLength);
  119. b.append(_signature,_signatureLength);
  120. }
  121. b.append((uint16_t)0); // length of additional fields, currently 0
  122. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  123. }
  124. template<unsigned int C>
  125. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  126. {
  127. unsigned int p = startAt;
  128. *this = Tag();
  129. _networkId = b.template at<uint64_t>(p); p += 8;
  130. _ts = b.template at<uint64_t>(p); p += 8;
  131. _id = b.template at<uint32_t>(p); p += 4;
  132. _value = b.template at<uint32_t>(p); p += 4;
  133. _issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  134. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  135. if (b[p++] == 1) {
  136. _signatureLength = b.template at<uint16_t>(p);
  137. if (_signatureLength > sizeof(_signature))
  138. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  139. p += 2;
  140. memcpy(_signature,b.field(p,_signatureLength),_signatureLength); p += _signatureLength;
  141. } else {
  142. p += 2 + b.template at<uint16_t>(p);
  143. }
  144. p += 2 + b.template at<uint16_t>(p);
  145. if (p > b.size())
  146. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  147. return (p - startAt);
  148. }
  149. // Provides natural sort order by ID
  150. inline bool operator<(const Tag &t) const { return (_id < t._id); }
  151. inline bool operator==(const Tag &t) const { return (memcmp(this,&t,sizeof(Tag)) == 0); }
  152. inline bool operator!=(const Tag &t) const { return (memcmp(this,&t,sizeof(Tag)) != 0); }
  153. // For searching sorted arrays or lists of Tags by ID
  154. struct IdComparePredicate
  155. {
  156. inline bool operator()(const Tag &a,const Tag &b) const { return (a.id() < b.id()); }
  157. inline bool operator()(const uint32_t a,const Tag &b) const { return (a < b.id()); }
  158. inline bool operator()(const Tag &a,const uint32_t b) const { return (a.id() < b); }
  159. inline bool operator()(const Tag *a,const Tag *b) const { return (a->id() < b->id()); }
  160. inline bool operator()(const Tag *a,const Tag &b) const { return (a->id() < b.id()); }
  161. inline bool operator()(const Tag &a,const Tag *b) const { return (a.id() < b->id()); }
  162. inline bool operator()(const uint32_t a,const Tag *b) const { return (a < b->id()); }
  163. inline bool operator()(const Tag *a,const uint32_t b) const { return (a->id() < b); }
  164. inline bool operator()(const uint32_t a,const uint32_t b) const { return (a < b); }
  165. };
  166. private:
  167. uint32_t _id;
  168. uint32_t _value;
  169. uint64_t _networkId;
  170. int64_t _ts;
  171. Address _issuedTo;
  172. Address _signedBy;
  173. unsigned int _signatureLength;
  174. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  175. };
  176. } // namespace ZeroTier
  177. #endif