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