Locator.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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_LOCATOR_HPP
  27. #define ZT_LOCATOR_HPP
  28. #include "Constants.hpp"
  29. #include "Identity.hpp"
  30. #include "InetAddress.hpp"
  31. #include "Utils.hpp"
  32. #include "Buffer.hpp"
  33. #include "SHA512.hpp"
  34. #include "Str.hpp"
  35. #include <algorithm>
  36. #include <vector>
  37. #define ZT_LOCATOR_MAX_PHYSICAL_ADDRESSES 255
  38. #define ZT_LOCATOR_MAX_VIRTUAL_ADDRESSES 255
  39. namespace ZeroTier {
  40. /**
  41. * Signed information about a node's location on the network
  42. *
  43. * A locator can be stored in DNS as a series of TXT records with a DNS name
  44. * that includes a public key that can be used to validate the locator's
  45. * signature. That way DNS records can't be spoofed even if no DNSSEC or
  46. * anything else is present to secure DNS.
  47. */
  48. class Locator
  49. {
  50. public:
  51. Locator() : _signatureLength(0) {}
  52. inline const std::vector<InetAddress> &phy() const { return _physical; }
  53. inline const std::vector<Identity> &virt() const { return _virtual; }
  54. inline void add(const InetAddress &ip)
  55. {
  56. if (_physical.size() < ZT_LOCATOR_MAX_PHYSICAL_ADDRESSES)
  57. _physical.push_back(ip);
  58. }
  59. inline void add(const Identity &zt)
  60. {
  61. if (_virtual.size() < ZT_LOCATOR_MAX_VIRTUAL_ADDRESSES)
  62. _virtual.push_back(zt);
  63. }
  64. inline void finish(const Identity &id,const int64_t ts)
  65. {
  66. _ts = ts;
  67. _id = id;
  68. std::sort(_physical.begin(),_physical.end());
  69. _physical.erase(std::unique(_physical.begin(),_physical.end()),_physical.end());
  70. std::sort(_virtual.begin(),_virtual.end());
  71. _virtual.erase(std::unique(_virtual.begin(),_virtual.end()),_virtual.end());
  72. }
  73. inline bool sign(const Identity &signingId)
  74. {
  75. if (!signingId.hasPrivate())
  76. return false;
  77. if (signingId == _id) {
  78. _signedBy.zero();
  79. } else {
  80. _signedBy = signingId;
  81. }
  82. Buffer<65536> *tmp = new Buffer<65536>();
  83. try {
  84. serialize(*tmp,true);
  85. _signatureLength = signingId.sign(tmp->data(),tmp->size(),_signature,ZT_SIGNATURE_BUFFER_SIZE);
  86. delete tmp;
  87. return (_signatureLength > 0);
  88. } catch ( ... ) {
  89. delete tmp;
  90. return false;
  91. }
  92. }
  93. inline bool verify() const
  94. {
  95. if ((_signatureLength == 0)||(_signatureLength > sizeof(_signature)))
  96. return false;
  97. Buffer<65536> *tmp = nullptr;
  98. try {
  99. tmp = new Buffer<65536>();
  100. serialize(*tmp,true);
  101. const bool ok = (_signedBy) ? _signedBy.verify(tmp->data(),tmp->size(),_signature,_signatureLength) : _id.verify(tmp->data(),tmp->size(),_signature,_signatureLength);
  102. delete tmp;
  103. return ok;
  104. } catch ( ... ) {
  105. if (tmp) delete tmp;
  106. return false;
  107. }
  108. }
  109. inline std::vector<Str> makeTxtRecords(const uint8_t p384SigningKeyPublic[ZT_ECC384_PUBLIC_KEY_SIZE],const uint8_t p384SigningKeyPrivate[ZT_ECC384_PUBLIC_KEY_SIZE])
  110. {
  111. uint8_t s384[48],dnsSig[ZT_ECC384_SIGNATURE_SIZE];
  112. char enc[256];
  113. Buffer<65536> *const tmp = new Buffer<65536>();
  114. serialize(*tmp,false);
  115. SHA384(s384,tmp->data(),tmp->size());
  116. ECC384ECDSASign(p384SigningKeyPrivate,s384,dnsSig);
  117. tmp->append(dnsSig,ZT_ECC384_SIGNATURE_SIZE);
  118. // Blob must be broken into multiple TXT records that must remain sortable so they are prefixed by a hex value.
  119. // 186-byte chunks yield 248-byte base64 chunks which leaves some margin below the limit of 255.
  120. std::vector<Str> txtRecords;
  121. for(unsigned int p=0;p<tmp->size();p+=186) {
  122. unsigned int rem = tmp->size() - p;
  123. if (rem > 186) rem = 186;
  124. Utils::b64e(((const uint8_t *)tmp->data()) + p,rem,enc,sizeof(enc));
  125. txtRecords.push_back(Str());
  126. txtRecords.back() << Utils::HEXCHARS[(p >> 4) & 0xf] << Utils::HEXCHARS[p & 0xf] << enc;
  127. }
  128. delete tmp;
  129. return txtRecords;
  130. }
  131. template<typename I>
  132. inline bool decodeTxtRecords(I start,I end,const uint8_t p384SigningKeyPublic[ZT_ECC384_PUBLIC_KEY_SIZE])
  133. {
  134. uint8_t dec[256],s384[48];
  135. Buffer<65536> *tmp = nullptr;
  136. try {
  137. tmp = new Buffer<65536>();
  138. while (start != end) {
  139. tmp->append(dec,Utils::b64d(start->c_str(),dec,sizeof(dec)));
  140. ++start;
  141. }
  142. if (tmp->size() <= ZT_ECC384_SIGNATURE_SIZE) {
  143. delete tmp;
  144. return false;
  145. }
  146. SHA384(s384,tmp->data(),tmp->size() - ZT_ECC384_SIGNATURE_SIZE);
  147. if (!ECC384ECDSAVerify(p384SigningKeyPublic,s384,((const uint8_t *)tmp->data()) + (tmp->size() - ZT_ECC384_SIGNATURE_SIZE))) {
  148. delete tmp;
  149. return false;
  150. }
  151. deserialize(*tmp,0);
  152. delete tmp;
  153. return verify();
  154. } catch ( ... ) {
  155. if (tmp) delete tmp;
  156. return false;
  157. }
  158. }
  159. template<unsigned int C>
  160. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  161. {
  162. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  163. b.append((uint8_t)0); // version/flags, currently 0
  164. b.append((uint64_t)_ts);
  165. _id.serialise(b,false);
  166. if (_signedBy) {
  167. b.append((uint8_t)1); // number of signers, current max is 1
  168. _signedBy.serialize(b,false);
  169. } else {
  170. b.append((uint8_t)0); // signer is _id
  171. }
  172. b.append((uint8_t)_physical.size());
  173. for(std::vector<InetAddress>::const_iterator i(_physical.begin());i!=_physical.end();++i)
  174. i->serialize(b);
  175. b.append((uint8_t)_virtual.size());
  176. for(std::vector<Identity>::const_iterator i(_virtual.begin());i!=_virtual.end();++i)
  177. i->serialize(b,false);
  178. if (!forSign) {
  179. b.append((uint16_t)_signatureLength);
  180. b.append(_signature,_signatureLength);
  181. }
  182. b.append((uint16_t)0); // length of additional fields, currently 0
  183. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  184. }
  185. template<unsigned int C>
  186. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  187. {
  188. unsigned int p = startAt;
  189. if (b[p++] != 0)
  190. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
  191. _ts = (int64_t)b.template at<uint64_t>(p); p += 8;
  192. p += _id.deserialize(b,p);
  193. const unsigned int signerCount = b[p++];
  194. if (signerCount > 1)
  195. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  196. if (signerCount == 1) {
  197. p += _signedBy.deserialize(b,p);
  198. } else {
  199. _signedBy.zero();
  200. }
  201. const unsigned int physicalCount = b[p++];
  202. _physical.resize(physicalCount);
  203. for(unsigned int i=0;i<physicalCount;++i)
  204. p += _physical[i].deserialize(b,p);
  205. const unsigned int virtualCount = b[p++];
  206. _virtual.resize(virtualCount);
  207. for(unsigned int i=0;i<virtualCount;++i)
  208. p += _virtual[i].deserialize(b,p);
  209. _signatureLen = b.template at<uint16_t>(p); p += 2;
  210. if (_signatureLength > ZT_SIGNATURE_BUFFER_SIZE)
  211. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  212. memcpy(_signature,b.field(p,_signatureLength),_signatureLength);
  213. p += _signatureLength;
  214. p += b.template at<uint16_t>(p); p += 2;
  215. if (p > b.size())
  216. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  217. return (p - startAt);
  218. }
  219. private:
  220. int64_t _ts;
  221. Identity _id;
  222. Identity _signedBy; // signed by _id if nil/zero
  223. std::vector<InetAddress> _physical;
  224. std::vector<Identity> _virtual;
  225. unsigned int _signatureLength;
  226. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  227. };
  228. } // namespace ZeroTier
  229. #endif