CertificateOfRepresentation.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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_CERTIFICATEOFREPRESENTATION_HPP
  27. #define ZT_CERTIFICATEOFREPRESENTATION_HPP
  28. #include "Constants.hpp"
  29. #include "Credential.hpp"
  30. #include "Address.hpp"
  31. #include "C25519.hpp"
  32. #include "Identity.hpp"
  33. #include "Buffer.hpp"
  34. /**
  35. * Maximum number of addresses allowed in a COR
  36. */
  37. #define ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES ZT_MAX_UPSTREAMS
  38. namespace ZeroTier {
  39. /**
  40. * A signed enumeration of a node's roots (planet and moons)
  41. *
  42. * This is sent as part of HELLO and attests to which roots a node trusts
  43. * to represent it on the network. Federated roots (moons) can send these
  44. * further upstream to tell global roots which nodes they represent, making
  45. * them reachable via federated roots if they are not reachable directly.
  46. *
  47. * As of 1.2.0 this is sent but not used. Right now nodes still always
  48. * announce to planetary roots no matter what. In the future this can be
  49. * used to implement even better fault tolerance for federation for the
  50. * no roots are reachable case as well as a "privacy mode" where federated
  51. * roots can shield nodes entirely and p2p connectivity behind them can
  52. * be disabled. This will be desirable for a number of use cases.
  53. */
  54. class CertificateOfRepresentation : public Credential
  55. {
  56. public:
  57. static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_COR; }
  58. CertificateOfRepresentation()
  59. {
  60. memset(this,0,sizeof(CertificateOfRepresentation));
  61. }
  62. inline uint32_t id() const { return 0; }
  63. inline uint64_t timestamp() const { return _timestamp; }
  64. inline const Address &representative(const unsigned int i) const { return _reps[i]; }
  65. inline unsigned int repCount() const { return _repCount; }
  66. inline void clear()
  67. {
  68. memset(this,0,sizeof(CertificateOfRepresentation));
  69. }
  70. /**
  71. * Add a representative if space remains
  72. *
  73. * @param r Representative to add
  74. * @return True if representative was added
  75. */
  76. inline bool addRepresentative(const Address &r)
  77. {
  78. if (_repCount < ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES) {
  79. _reps[_repCount++] = r;
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * Sign this COR with my identity
  86. *
  87. * @param myIdentity This node's identity
  88. * @param ts COR timestamp for establishing new vs. old
  89. */
  90. inline void sign(const Identity &myIdentity,const uint64_t ts)
  91. {
  92. _timestamp = ts;
  93. Buffer<sizeof(CertificateOfRepresentation) + 32> tmp;
  94. this->serialize(tmp,true);
  95. _signature = myIdentity.sign(tmp.data(),tmp.size());
  96. }
  97. /**
  98. * Verify this COR's signature
  99. *
  100. * @param senderIdentity Identity of sender of COR
  101. * @return True if COR is valid
  102. */
  103. inline bool verify(const Identity &senderIdentity)
  104. {
  105. try {
  106. Buffer<sizeof(CertificateOfRepresentation) + 32> tmp;
  107. this->serialize(tmp,true);
  108. return senderIdentity.verify(tmp.data(),tmp.size(),_signature.data,ZT_C25519_SIGNATURE_LEN);
  109. } catch ( ... ) {
  110. return false;
  111. }
  112. }
  113. template<unsigned int C>
  114. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  115. {
  116. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  117. b.append((uint64_t)_timestamp);
  118. b.append((uint16_t)_repCount);
  119. for(unsigned int i=0;i<_repCount;++i)
  120. _reps[i].appendTo(b);
  121. if (!forSign) {
  122. b.append((uint8_t)1); // 1 == Ed25519 signature
  123. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN);
  124. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  125. }
  126. b.append((uint16_t)0); // size of any additional fields, currently 0
  127. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  128. }
  129. template<unsigned int C>
  130. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  131. {
  132. clear();
  133. unsigned int p = startAt;
  134. _timestamp = b.template at<uint64_t>(p); p += 8;
  135. const unsigned int rc = b.template at<uint16_t>(p); p += 2;
  136. for(unsigned int i=0;i<rc;++i) {
  137. if (i < ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES)
  138. _reps[i].setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
  139. p += ZT_ADDRESS_LENGTH;
  140. }
  141. _repCount = (rc > ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES) ? ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES : rc;
  142. if (b[p++] == 1) {
  143. if (b.template at<uint16_t>(p) == ZT_C25519_SIGNATURE_LEN) {
  144. p += 2;
  145. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN);
  146. p += ZT_C25519_SIGNATURE_LEN;
  147. } else throw std::runtime_error("invalid signature");
  148. } else {
  149. p += 2 + b.template at<uint16_t>(p);
  150. }
  151. p += 2 + b.template at<uint16_t>(p);
  152. if (p > b.size())
  153. throw std::runtime_error("extended field overflow");
  154. return (p - startAt);
  155. }
  156. private:
  157. uint64_t _timestamp;
  158. Address _reps[ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES];
  159. unsigned int _repCount;
  160. C25519::Signature _signature;
  161. };
  162. } // namespace ZeroTier
  163. #endif