Dictionary.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Dictionary.hpp"
  14. #include "Identity.hpp"
  15. namespace ZeroTier {
  16. static const FCV<char, 8> s_signatureFingerprint("@Si", 4);
  17. static const FCV<char, 8> s_signatureData("@Ss", 4);
  18. Dictionary::Dictionary()
  19. {
  20. }
  21. Vector<uint8_t> &Dictionary::operator[](const char *k)
  22. {
  23. FCV<char, 8> key;
  24. return m_entries[s_key(key, k)];
  25. }
  26. const Vector<uint8_t> &Dictionary::operator[](const char *k) const
  27. {
  28. static const Vector<uint8_t> s_emptyEntry;
  29. FCV<char, 8> key;
  30. SortedMap<FCV<char, 8>, Vector<uint8_t> >::const_iterator e(m_entries.find(s_key(key, k)));
  31. return (e == m_entries.end()) ? s_emptyEntry : e->second;
  32. }
  33. void Dictionary::add(const char *k, bool v)
  34. {
  35. Vector<uint8_t> &e = (*this)[k];
  36. e.resize(2);
  37. e[0] = (uint8_t) (v ? '1' : '0');
  38. e[1] = 0;
  39. }
  40. void Dictionary::add(const char *k, const Address &v)
  41. {
  42. Vector<uint8_t> &e = (*this)[k];
  43. e.resize(ZT_ADDRESS_STRING_SIZE_MAX);
  44. v.toString((char *) e.data());
  45. }
  46. void Dictionary::add(const char *k, const char *v)
  47. {
  48. if ((v) && (*v)) {
  49. Vector<uint8_t> &e = (*this)[k];
  50. e.clear();
  51. while (*v)
  52. e.push_back((uint8_t) *(v++));
  53. }
  54. }
  55. void Dictionary::add(const char *k, const void *data, unsigned int len)
  56. {
  57. Vector<uint8_t> &e = (*this)[k];
  58. if (len != 0) {
  59. e.assign((const uint8_t *) data, (const uint8_t *) data + len);
  60. } else {
  61. e.clear();
  62. }
  63. }
  64. bool Dictionary::getB(const char *k, bool dfl) const
  65. {
  66. const Vector<uint8_t> &e = (*this)[k];
  67. if (!e.empty()) {
  68. switch ((char) e[0]) {
  69. case '1':
  70. case 't':
  71. case 'T':
  72. case 'y':
  73. case 'Y':
  74. return true;
  75. default:
  76. return false;
  77. }
  78. }
  79. return dfl;
  80. }
  81. uint64_t Dictionary::getUI(const char *k, uint64_t dfl) const
  82. {
  83. uint8_t tmp[18];
  84. uint64_t v = dfl;
  85. const Vector<uint8_t> &e = (*this)[k];
  86. if (!e.empty()) {
  87. if (e.back() != 0) {
  88. const unsigned long sl = e.size();
  89. Utils::copy(tmp, e.data(), (sl > 17) ? 17 : sl);
  90. tmp[17] = 0;
  91. return Utils::unhex((const char *) tmp);
  92. }
  93. return Utils::unhex((const char *) e.data());
  94. }
  95. return v;
  96. }
  97. char *Dictionary::getS(const char *k, char *v, const unsigned int cap) const
  98. {
  99. if (cap == 0) // sanity check
  100. return;
  101. const Vector<uint8_t> &e = (*this)[k];
  102. unsigned int i = 0;
  103. const unsigned int last = cap - 1;
  104. for (;;) {
  105. if ((i == last) || (i >= (unsigned int)e.size()))
  106. break;
  107. v[i] = (char) e[i];
  108. ++i;
  109. }
  110. v[i] = 0;
  111. return v;
  112. }
  113. bool Dictionary::sign(const Identity &signer)
  114. {
  115. Vector<uint8_t> data;
  116. encode(data, true);
  117. uint8_t sig[ZT_SIGNATURE_BUFFER_SIZE];
  118. const unsigned int siglen = signer.sign(data.data(), (unsigned int) data.size(), sig, ZT_SIGNATURE_BUFFER_SIZE);
  119. if (siglen == 0)
  120. return false;
  121. uint8_t fp[ZT_ADDRESS_LENGTH + ZT_FINGERPRINT_HASH_SIZE];
  122. signer.fingerprint().address().copyTo(fp);
  123. Utils::copy<ZT_FINGERPRINT_HASH_SIZE>(fp + ZT_ADDRESS_LENGTH, signer.fingerprint().hash());
  124. m_entries[s_signatureFingerprint].assign(fp, fp + ZT_ADDRESS_LENGTH + ZT_FINGERPRINT_HASH_SIZE);
  125. m_entries[s_signatureData].assign(sig, sig + siglen);
  126. return true;
  127. }
  128. Fingerprint Dictionary::signer() const
  129. {
  130. SortedMap<FCV<char, 8>, Vector<uint8_t> >::const_iterator sigfp(m_entries.find(s_signatureFingerprint));
  131. Fingerprint fp;
  132. if ((sigfp != m_entries.end()) && (sigfp->second.size() == (ZT_ADDRESS_LENGTH + ZT_FINGERPRINT_HASH_SIZE))) {
  133. fp.apiFingerprint()->address = Address(sigfp->second.data()).toInt();
  134. Utils::copy<ZT_FINGERPRINT_HASH_SIZE>(fp.apiFingerprint()->hash, sigfp->second.data() + ZT_ADDRESS_LENGTH);
  135. }
  136. return fp;
  137. }
  138. bool Dictionary::verify(const Identity &signer) const
  139. {
  140. SortedMap< FCV<char, 8>, Vector<uint8_t> >::const_iterator sigfp(m_entries.find(s_signatureFingerprint));
  141. if (
  142. (sigfp == m_entries.end()) ||
  143. (sigfp->second.size() != (ZT_ADDRESS_LENGTH + ZT_FINGERPRINT_HASH_SIZE)) ||
  144. (Address(sigfp->second.data()) != signer.address()) ||
  145. (memcmp(sigfp->second.data() + ZT_ADDRESS_LENGTH,signer.fingerprint().hash(),ZT_FINGERPRINT_HASH_SIZE) != 0))
  146. return false;
  147. SortedMap< FCV<char, 8>, Vector<uint8_t> >::const_iterator sig(m_entries.find(s_signatureData));
  148. if ((sig == m_entries.end()) || (sig->second.empty()))
  149. return false;
  150. Vector<uint8_t> data;
  151. encode(data, true);
  152. return signer.verify(data.data(),(unsigned int)data.size(),sig->second.data(),(unsigned int)sig->second.size());
  153. }
  154. void Dictionary::clear()
  155. {
  156. m_entries.clear();
  157. }
  158. void Dictionary::encode(Vector<uint8_t> &out, const bool omitSignatureFields) const
  159. {
  160. out.clear();
  161. for (SortedMap<FCV<char, 8>, Vector<uint8_t> >::const_iterator ti(m_entries.begin());ti != m_entries.end();++ti) {
  162. if ((!omitSignatureFields) || ((ti->first != s_signatureFingerprint) && (ti->first != s_signatureData))) {
  163. s_appendKey(out, ti->first.data());
  164. for (Vector<uint8_t>::const_iterator i(ti->second.begin());i != ti->second.end();++i)
  165. s_appendValueByte(out, *i);
  166. out.push_back((uint8_t) '\n');
  167. }
  168. }
  169. out.push_back(0);
  170. }
  171. bool Dictionary::decode(const void *data, unsigned int len)
  172. {
  173. clear();
  174. FCV<char, 8> k;
  175. Vector<uint8_t> *v = nullptr;
  176. bool escape = false;
  177. for (unsigned int di = 0;di < len;++di) {
  178. uint8_t c = reinterpret_cast<const uint8_t *>(data)[di];
  179. if (!c) break;
  180. if (v) {
  181. if (escape) {
  182. escape = false;
  183. switch (c) {
  184. case 48:
  185. v->push_back(0);
  186. break;
  187. case 101:
  188. v->push_back(61);
  189. break;
  190. case 110:
  191. v->push_back(10);
  192. break;
  193. case 114:
  194. v->push_back(13);
  195. break;
  196. default:
  197. v->push_back(c);
  198. break;
  199. }
  200. } else {
  201. if (c == (uint8_t) '\n') {
  202. k.clear();
  203. v = nullptr;
  204. } else if (c == 92) { // backslash
  205. escape = true;
  206. } else {
  207. v->push_back(c);
  208. }
  209. }
  210. } else {
  211. if ((c < 33) || (c > 126) || (c == 92)) {
  212. return false;
  213. } else if (c == (uint8_t) '=') {
  214. k.push_back(0);
  215. v = &m_entries[k];
  216. } else if (k.size() < 7) {
  217. k.push_back(c);
  218. } else {
  219. return false;
  220. }
  221. }
  222. }
  223. return true;
  224. }
  225. } // namespace ZeroTier