Dictionary.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "SHA512.hpp"
  15. namespace ZeroTier {
  16. Dictionary::Dictionary()
  17. {
  18. }
  19. Vector<uint8_t> &Dictionary::operator[](const char *k)
  20. {
  21. return m_entries[s_key(k)];
  22. }
  23. const Vector<uint8_t> &Dictionary::operator[](const char *k) const
  24. {
  25. static const Vector<uint8_t> s_emptyEntry;
  26. SortedMap< String, Vector<uint8_t> >::const_iterator e(m_entries.find(s_key(k)));
  27. return (e == m_entries.end()) ? s_emptyEntry : e->second;
  28. }
  29. void Dictionary::add(const char *k, bool v)
  30. {
  31. Vector<uint8_t> &e = (*this)[k];
  32. e.resize(2);
  33. e[0] = (uint8_t) (v ? '1' : '0');
  34. e[1] = 0;
  35. }
  36. void Dictionary::add(const char *k, const Address &v)
  37. {
  38. Vector<uint8_t> &e = (*this)[k];
  39. e.resize(ZT_ADDRESS_STRING_SIZE_MAX);
  40. v.toString((char *) e.data());
  41. }
  42. void Dictionary::add(const char *k, const char *v)
  43. {
  44. if ((v) && (*v)) {
  45. Vector<uint8_t> &e = (*this)[k];
  46. e.clear();
  47. while (*v)
  48. e.push_back((uint8_t) *(v++));
  49. }
  50. }
  51. void Dictionary::add(const char *k, const void *data, unsigned int len)
  52. {
  53. Vector<uint8_t> &e = (*this)[k];
  54. if (len != 0) {
  55. e.assign((const uint8_t *) data, (const uint8_t *) data + len);
  56. } else {
  57. e.clear();
  58. }
  59. }
  60. bool Dictionary::getB(const char *k, bool dfl) const
  61. {
  62. const Vector<uint8_t> &e = (*this)[k];
  63. if (!e.empty()) {
  64. switch ((char) e[0]) {
  65. case '1':
  66. case 't':
  67. case 'T':
  68. case 'y':
  69. case 'Y':
  70. return true;
  71. default:
  72. return false;
  73. }
  74. }
  75. return dfl;
  76. }
  77. uint64_t Dictionary::getUI(const char *k, uint64_t dfl) const
  78. {
  79. uint8_t tmp[18];
  80. uint64_t v = dfl;
  81. const Vector<uint8_t> &e = (*this)[k];
  82. if (!e.empty()) {
  83. if (e.back() != 0) {
  84. const unsigned long sl = e.size();
  85. Utils::copy(tmp, e.data(), (sl > 17) ? 17 : sl);
  86. tmp[17] = 0;
  87. return Utils::unhex((const char *) tmp);
  88. }
  89. return Utils::unhex((const char *) e.data());
  90. }
  91. return v;
  92. }
  93. char *Dictionary::getS(const char *k, char *v, const unsigned int cap) const
  94. {
  95. if (cap == 0) // sanity check
  96. return v;
  97. const Vector<uint8_t> &e = (*this)[k];
  98. unsigned int i = 0;
  99. const unsigned int last = cap - 1;
  100. for (;;) {
  101. if ((i == last) || (i >= (unsigned int)e.size()))
  102. break;
  103. v[i] = (char) e[i];
  104. ++i;
  105. }
  106. v[i] = 0;
  107. return v;
  108. }
  109. void Dictionary::clear()
  110. {
  111. m_entries.clear();
  112. }
  113. void Dictionary::encode(Vector<uint8_t> &out, const bool omitSignatureFields) const
  114. {
  115. out.clear();
  116. for (SortedMap< String, Vector<uint8_t> >::const_iterator ti(m_entries.begin());ti != m_entries.end();++ti) {
  117. if ((!omitSignatureFields) || ((ti->first != ZT_DICTIONARY_SIGNATURE_KEY))) {
  118. s_appendKey(out, ti->first.data());
  119. for (Vector<uint8_t>::const_iterator i(ti->second.begin());i != ti->second.end();++i)
  120. s_appendValueByte(out, *i);
  121. out.push_back((uint8_t) '\n');
  122. }
  123. }
  124. out.push_back(0);
  125. }
  126. bool Dictionary::decode(const void *data, unsigned int len)
  127. {
  128. clear();
  129. String k;
  130. Vector<uint8_t> *v = nullptr;
  131. bool escape = false;
  132. for (unsigned int di = 0;di < len;++di) {
  133. uint8_t c = reinterpret_cast<const uint8_t *>(data)[di];
  134. if (!c) break;
  135. if (v) {
  136. if (escape) {
  137. escape = false;
  138. switch (c) {
  139. case 48:
  140. v->push_back(0);
  141. break;
  142. case 101:
  143. v->push_back(61);
  144. break;
  145. case 110:
  146. v->push_back(10);
  147. break;
  148. case 114:
  149. v->push_back(13);
  150. break;
  151. default:
  152. v->push_back(c);
  153. break;
  154. }
  155. } else {
  156. if (c == (uint8_t) '\n') {
  157. k.clear();
  158. v = nullptr;
  159. } else if (c == 92) { // backslash
  160. escape = true;
  161. } else {
  162. v->push_back(c);
  163. }
  164. }
  165. } else {
  166. if ((c < 33) || (c > 126) || (c == 92)) {
  167. return false;
  168. } else if (c == (uint8_t) '=') {
  169. k.push_back(0);
  170. v = &m_entries[k];
  171. } else if (k.size() < 7) {
  172. k.push_back(c);
  173. } else {
  174. return false;
  175. }
  176. }
  177. }
  178. return true;
  179. }
  180. void Dictionary::sign(
  181. const uint8_t c25519PrivateKey[ZT_C25519_COMBINED_PRIVATE_KEY_SIZE],
  182. const uint8_t c25519PublicKey[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE],
  183. const uint8_t p384PrivateKey[ZT_ECC384_PRIVATE_KEY_SIZE],
  184. const uint8_t p384PublicKey[ZT_ECC384_PUBLIC_KEY_SIZE])
  185. {
  186. Vector<uint8_t> buf;
  187. encode(buf, true);
  188. uint8_t c25519Signature[ZT_C25519_SIGNATURE_LEN];
  189. C25519::sign(c25519PrivateKey, c25519PublicKey, buf.data(), (unsigned int)buf.size(), c25519Signature);
  190. uint8_t hbuf[ZT_ECC384_SIGNATURE_HASH_SIZE];
  191. static_assert(ZT_ECC384_SIGNATURE_HASH_SIZE == ZT_SHA384_DIGEST_SIZE,"size mismatch");
  192. SHA384(hbuf, buf.data(), (unsigned int)buf.size());
  193. uint8_t p384Signature[ZT_ECC384_SIGNATURE_SIZE];
  194. ECC384ECDSASign(p384PrivateKey, hbuf, p384Signature);
  195. SHA384(hbuf, c25519PublicKey, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, p384PublicKey, ZT_ECC384_PUBLIC_KEY_SIZE);
  196. Dictionary signature;
  197. signature["kh"].assign(hbuf, hbuf + ZT_SHA384_DIGEST_SIZE);
  198. signature["ed25519"].assign(c25519Signature, c25519Signature + ZT_C25519_SIGNATURE_LEN);
  199. signature["p384"].assign(p384Signature, p384Signature + ZT_ECC384_SIGNATURE_SIZE);
  200. signature.encode((*this)[ZT_DICTIONARY_SIGNATURE_KEY], true);
  201. }
  202. bool Dictionary::verify(
  203. const uint8_t c25519PublicKey[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE],
  204. const uint8_t p384PublicKey[ZT_ECC384_PUBLIC_KEY_SIZE]) const
  205. {
  206. try {
  207. const Vector< uint8_t > &data = (*this)[ZT_DICTIONARY_SIGNATURE_KEY];
  208. if (data.empty())
  209. return false;
  210. Dictionary signature;
  211. if (!signature.decode(data.data(), (unsigned int)data.size()))
  212. return false;
  213. const Vector< uint8_t > &p384Signature = signature["p384"];
  214. const Vector< uint8_t > &c25519Signature = signature["ed25519"];
  215. if ((p384Signature.size() != ZT_ECC384_SIGNATURE_SIZE) || (c25519Signature.size() != ZT_C25519_SIGNATURE_LEN))
  216. return false;
  217. Vector< uint8_t > buf;
  218. encode(buf, true);
  219. if (C25519::verify(c25519PublicKey, buf.data(), (unsigned int)buf.size(), c25519Signature.data(), (unsigned int)c25519Signature.size())) {
  220. uint8_t hbuf[ZT_ECC384_SIGNATURE_HASH_SIZE];
  221. SHA384(hbuf, buf.data(), (unsigned int)buf.size());
  222. return ECC384ECDSAVerify(p384PublicKey, hbuf, p384Signature.data());
  223. }
  224. } catch ( ... ) {}
  225. return false;
  226. }
  227. } // namespace ZeroTier