CertificateOfRepresentation.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. class CertificateOfRepresentation
  31. {
  32. public:
  33. CertificateOfRepresentation()
  34. {
  35. memset(this,0,sizeof(CertificateOfRepresentation));
  36. }
  37. inline uint64_t timestamp() const { return _timestamp; }
  38. inline const Address &representative(const unsigned int i) const { return _reps[i]; }
  39. inline unsigned int repCount() const { return _repCount; }
  40. inline void clear()
  41. {
  42. memset(this,0,sizeof(CertificateOfRepresentation));
  43. }
  44. /**
  45. * Add a representative if space remains
  46. *
  47. * @param r Representative to add
  48. * @return True if representative was added
  49. */
  50. inline bool addRepresentative(const Address &r)
  51. {
  52. if (_repCount < ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES) {
  53. _reps[_repCount++] = r;
  54. return true;
  55. }
  56. return false;
  57. }
  58. /**
  59. * Sign this COR with my identity
  60. *
  61. * @param myIdentity This node's identity
  62. * @param ts COR timestamp for establishing new vs. old
  63. */
  64. inline void sign(const Identity &myIdentity,const uint64_t ts)
  65. {
  66. _timestamp = ts;
  67. Buffer<sizeof(CertificateOfRepresentation) + 32> tmp;
  68. this->serialize(tmp,true);
  69. _signature = myIdentity.sign(tmp.data(),tmp.size());
  70. }
  71. /**
  72. * Verify this COR's signature
  73. *
  74. * @param senderIdentity Identity of sender of COR
  75. * @return True if COR is valid
  76. */
  77. inline bool verify(const Identity &senderIdentity)
  78. {
  79. try {
  80. Buffer<sizeof(CertificateOfRepresentation) + 32> tmp;
  81. this->serialize(tmp,true);
  82. return senderIdentity.verify(tmp.data(),tmp.size(),_signature.data,ZT_C25519_SIGNATURE_LEN);
  83. } catch ( ... ) {
  84. return false;
  85. }
  86. }
  87. template<unsigned int C>
  88. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  89. {
  90. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  91. b.append((uint64_t)_timestamp);
  92. b.append((uint16_t)_repCount);
  93. for(unsigned int i=0;i<_repCount;++i)
  94. _reps[i].appendTo(b);
  95. if (!forSign) {
  96. b.append((uint8_t)1); // 1 == Ed25519 signature
  97. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN);
  98. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  99. }
  100. b.append((uint16_t)0); // size of any additional fields, currently 0
  101. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  102. }
  103. template<unsigned int C>
  104. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  105. {
  106. clear();
  107. unsigned int p = startAt;
  108. _timestamp = b.template at<uint64_t>(p); p += 8;
  109. const unsigned int rc = b.template at<uint16_t>(p); p += 2;
  110. for(unsigned int i=0;i<rc;++i) {
  111. if (i < ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES)
  112. _reps[i].setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
  113. p += ZT_ADDRESS_LENGTH;
  114. }
  115. _repCount = (rc > ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES) ? ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES : rc;
  116. if (b[p++] == 1) {
  117. if (b.template at<uint16_t>(p) == ZT_C25519_SIGNATURE_LEN) {
  118. p += 2;
  119. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN);
  120. p += ZT_C25519_SIGNATURE_LEN;
  121. } else throw std::runtime_error("invalid signature");
  122. } else {
  123. p += 2 + b.template at<uint16_t>(p);
  124. }
  125. p += 2 + b.template at<uint16_t>(p);
  126. if (p > b.size())
  127. throw std::runtime_error("extended field overflow");
  128. return (p - startAt);
  129. }
  130. private:
  131. uint64_t _timestamp;
  132. Address _reps[ZT_CERTIFICATEOFREPRESENTATION_MAX_ADDRESSES];
  133. unsigned int _repCount;
  134. C25519::Signature _signature;
  135. };
  136. } // namespace ZeroTier
  137. #endif