Tag.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_TAG_HPP
  19. #define ZT_TAG_HPP
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "Constants.hpp"
  25. #include "C25519.hpp"
  26. #include "Address.hpp"
  27. #include "Identity.hpp"
  28. #include "Buffer.hpp"
  29. namespace ZeroTier {
  30. class RuntimeEnvironment;
  31. /**
  32. * A tag that can be associated with members and matched in rules
  33. *
  34. * Capabilities group rules, while tags group members subject to those
  35. * rules. Tag values can be matched in rules, and tags relevant to a
  36. * capability are presented along with it.
  37. *
  38. * E.g. a capability might be "can speak Samba/CIFS within your
  39. * department." This cap might have a rule to allow TCP/137 but
  40. * only if a given tag ID's value matches between two peers. The
  41. * capability is what members can do, while the tag is who they are.
  42. * Different departments might have tags with the same ID but different
  43. * values.
  44. *
  45. * Unlike capabilities tags are signed only by the issuer and are never
  46. * transferrable.
  47. */
  48. class Tag
  49. {
  50. public:
  51. Tag()
  52. {
  53. memset(this,0,sizeof(Tag));
  54. }
  55. /**
  56. * @param nwid Network ID
  57. * @param expiration Tag expiration relative to network config timestamp
  58. * @param issuedTo Address to which this tag was issued
  59. * @param id Tag ID
  60. * @param value Tag value
  61. */
  62. Tag(const uint64_t nwid,const uint64_t expiration,const Address &issuedTo,const uint32_t id,const uint32_t value) :
  63. _nwid(nwid),
  64. _expiration(expiration),
  65. _id(id),
  66. _value(value),
  67. _issuedTo(issuedTo),
  68. _signedBy()
  69. {
  70. }
  71. inline uint64_t networkId() const { return _nwid; }
  72. inline uint64_t expiration() const { return _expiration; }
  73. inline uint32_t id() const { return _id; }
  74. inline uint32_t value() const { return _value; }
  75. inline const Address &issuedTo() const { return _issuedTo; }
  76. inline const Address &signedBy() const { return _signedBy; }
  77. /**
  78. * Sign this tag
  79. *
  80. * @param signer Signing identity, must have private key
  81. * @return True if signature was successful
  82. */
  83. inline bool sign(const Identity &signer)
  84. {
  85. try {
  86. Buffer<(sizeof(Tag) * 2)> tmp;
  87. _signedBy = signer.address();
  88. this->serialize(tmp,true);
  89. _signature = signer.sign(tmp.data(),tmp.size());
  90. return true;
  91. } catch ( ... ) {}
  92. return false;
  93. }
  94. /**
  95. * Check this tag's signature
  96. *
  97. * @param RR Runtime environment to allow identity lookup for signedBy
  98. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or tag
  99. */
  100. int verify(const RuntimeEnvironment *RR) const;
  101. template<unsigned int C>
  102. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  103. {
  104. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  105. b.append(_nwid);
  106. b.append(_expiration);
  107. b.append(_id);
  108. b.append(_value);
  109. _issuedTo.appendTo(b);
  110. _signedBy.appendTo(b);
  111. if (!forSign) {
  112. b.append((uint8_t)1); // 1 == Ed25519
  113. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
  114. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  115. }
  116. b.append((uint16_t)0); // length of additional fields, currently 0
  117. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  118. }
  119. template<unsigned int C>
  120. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  121. {
  122. unsigned int p = startAt;
  123. _nwid = b.template at<uint64_t>(p); p += 8;
  124. _expiration = b.template at<uint64_t>(p); p += 8;
  125. _id = b.template at<uint32_t>(p); p += 4;
  126. _value = b.template at<uint32_t>(p); p += 4;
  127. _issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  128. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  129. if (b[p++] != 1)
  130. throw std::runtime_error("unrecognized signature type");
  131. if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN)
  132. throw std::runtime_error("invalid signature length");
  133. p += 2;
  134. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  135. p += 2 + b.template at<uint16_t>(p);
  136. if (p > b.size())
  137. throw std::runtime_error("extended field overflow");
  138. return (p - startAt);
  139. }
  140. // Provides natural sort order by ID
  141. inline bool operator<(const Tag &t) const { return (_id < t._id); }
  142. inline bool operator==(const Tag &t) const { return (memcmp(this,&t,sizeof(Tag)) == 0); }
  143. inline bool operator!=(const Tag &t) const { return (memcmp(this,&t,sizeof(Tag)) != 0); }
  144. private:
  145. uint64_t _nwid;
  146. uint64_t _expiration;
  147. uint32_t _id;
  148. uint32_t _value;
  149. Address _issuedTo;
  150. Address _signedBy;
  151. C25519::Signature _signature;
  152. };
  153. } // namespace ZeroTier
  154. #endif