CertificateOfRepresentation.hpp 5.4 KB

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