Dictionary.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #ifndef ZT_DICTIONARY_HPP
  14. #define ZT_DICTIONARY_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include "Address.hpp"
  18. #include "Buf.hpp"
  19. #include "Containers.hpp"
  20. #include "C25519.hpp"
  21. #include "ECC384.hpp"
  22. #define ZT_DICTIONARY_SIGNATURE_KEY "@S"
  23. namespace ZeroTier {
  24. class Identity;
  25. /**
  26. * A simple key-value store for short keys
  27. *
  28. * This data structure is used for network configurations, node meta-data,
  29. * and other open-definition protocol objects.
  30. *
  31. * If this seems a little odd, it is. It dates back to the very first alpha
  32. * versions of ZeroTier and if it were redesigned today we'd use some kind
  33. * of simple or standardized binary encoding. Nevertheless it is efficient
  34. * and it works so there is no need to change it and break backward
  35. * compatibility.
  36. *
  37. * Use of the append functions is faster than building and then encoding a
  38. * dictionary for creating outbound packets.
  39. */
  40. class Dictionary
  41. {
  42. public:
  43. typedef SortedMap< String, Vector < uint8_t > >
  44. ::const_iterator const_iterator;
  45. Dictionary();
  46. /**
  47. * Get a reference to a value
  48. *
  49. * @param k Key to look up
  50. * @return Reference to value
  51. */
  52. Vector <uint8_t> &operator[](const char *k);
  53. /**
  54. * Get a const reference to a value
  55. *
  56. * @param k Key to look up
  57. * @return Reference to value or to empty vector if not found
  58. */
  59. const Vector <uint8_t> &operator[](const char *k) const;
  60. /**
  61. * @return Start of key->value pairs
  62. */
  63. ZT_INLINE const_iterator begin() const noexcept
  64. { return m_entries.begin(); }
  65. /**
  66. * @return End of key->value pairs
  67. */
  68. ZT_INLINE const_iterator end() const noexcept
  69. { return m_entries.end(); }
  70. /**
  71. * Add a boolean as '1' or '0'
  72. */
  73. void add(const char *k, bool v);
  74. /**
  75. * Add an integer as a hexadecimal string value
  76. *
  77. * @param k Key to set
  78. * @param v Integer to set, will be cast to uint64_t and stored as hex
  79. */
  80. template< typename I >
  81. ZT_INLINE void add(const char *const k, I v)
  82. {
  83. char buf[17];
  84. add(k, Utils::hex((uint64_t)(v), buf));
  85. }
  86. /**
  87. * Add an address in 10-digit hex string format
  88. */
  89. void add(const char *k, const Address &v);
  90. /**
  91. * Add a C string as a value
  92. */
  93. void add(const char *k, const char *v);
  94. /**
  95. * Add a binary blob as a value
  96. */
  97. void add(const char *k, const void *data, unsigned int len);
  98. /**
  99. * Get a boolean
  100. *
  101. * @param k Key to look up
  102. * @param dfl Default value (default: false)
  103. * @return Value of key or default if not found
  104. */
  105. bool getB(const char *k, bool dfl = false) const;
  106. /**
  107. * Get an integer
  108. *
  109. * @param k Key to look up
  110. * @param dfl Default value (default: 0)
  111. * @return Value of key or default if not found
  112. */
  113. uint64_t getUI(const char *k, uint64_t dfl = 0) const;
  114. /**
  115. * Get a C string
  116. *
  117. * If the buffer is too small the string will be truncated, but the
  118. * buffer will always end in a terminating null no matter what.
  119. *
  120. * @param k Key to look up
  121. * @param v Buffer to hold string
  122. * @param cap Maximum size of string (including terminating null)
  123. */
  124. char *getS(const char *k, char *v, unsigned int cap) const;
  125. /**
  126. * Get an object supporting the marshal/unmarshal interface pattern
  127. *
  128. * @param k Key to look up
  129. * @param obj Object to unmarshal() into
  130. * @return True if unmarshal was successful
  131. */
  132. template< typename T >
  133. ZT_INLINE bool getO(const char *k, T &obj) const
  134. {
  135. const Vector< uint8_t > &d = (*this)[k];
  136. if (d.empty())
  137. return false;
  138. return (obj.unmarshal(d.data(), (unsigned int)d.size()) > 0);
  139. }
  140. /**
  141. * Erase all entries in dictionary
  142. */
  143. void clear();
  144. /**
  145. * @return Number of entries
  146. */
  147. ZT_INLINE unsigned int size() const noexcept
  148. { return m_entries.size(); }
  149. /**
  150. * @return True if dictionary is not empty
  151. */
  152. ZT_INLINE bool empty() const noexcept
  153. { return m_entries.empty(); }
  154. /**
  155. * Encode to a string in the supplied vector
  156. *
  157. * @param out String encoded dictionary
  158. * @param omitSignatureFields If true omit the signature fields from encoding (default: false)
  159. */
  160. void encode(Vector <uint8_t> &out, bool omitSignatureFields = false) const;
  161. /**
  162. * Decode a string encoded dictionary
  163. *
  164. * This will decode up to 'len' but will also abort if it finds a
  165. * null/zero as this could be a C string.
  166. *
  167. * @param data Data to decode
  168. * @param len Length of data
  169. * @return True if dictionary was formatted correctly and valid, false on error
  170. */
  171. bool decode(const void *data, unsigned int len);
  172. /**
  173. * Sign this dictionary with both an Ed25519 key and a NIST P-384 key.
  174. *
  175. * This is currently used just for signing root sets for the root subscribe
  176. * feature. It uses both key types for more crypto cowbell.
  177. *
  178. * @param c25519PrivateKey Curve25519 combined key (C25519 and ed25519), though only ed25519 part is used
  179. * @param c25519PublicKey Public part of Curve25519 combined key
  180. * @param p384PrivateKey NIST P-384 private key
  181. * @param p384PublicKey NIST P-384 public key
  182. */
  183. void sign(
  184. const uint8_t c25519PrivateKey[ZT_C25519_COMBINED_PRIVATE_KEY_SIZE],
  185. const uint8_t c25519PublicKey[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE],
  186. const uint8_t p384PrivateKey[ZT_ECC384_PRIVATE_KEY_SIZE],
  187. const uint8_t p384PublicKey[ZT_ECC384_PUBLIC_KEY_SIZE]);
  188. /**
  189. * Verify this dictionary's signature
  190. *
  191. * @param c25519PublicKey Curve25519 public key
  192. * @param p384PublicKey P-384 public key
  193. * @return True if signatures are valid
  194. */
  195. bool verify(
  196. const uint8_t c25519PublicKey[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE],
  197. const uint8_t p384PublicKey[ZT_ECC384_PUBLIC_KEY_SIZE]) const;
  198. /**
  199. * Append a key=value pair to a buffer (vector or FCV)
  200. *
  201. * @param out Buffer
  202. * @param k Key (must be <= 8 characters)
  203. * @param v Value
  204. */
  205. template< typename V >
  206. ZT_INLINE static void append(V &out, const char *const k, const bool v)
  207. {
  208. s_appendKey(out, k);
  209. out.push_back((uint8_t)(v ? '1' : '0'));
  210. out.push_back((uint8_t)'\n');
  211. }
  212. /**
  213. * Append a key=value pair to a buffer (vector or FCV)
  214. *
  215. * @param out Buffer
  216. * @param k Key (must be <= 8 characters)
  217. * @param v Value
  218. */
  219. template< typename V >
  220. ZT_INLINE static void append(V &out, const char *const k, const Address v)
  221. {
  222. s_appendKey(out, k);
  223. const uint64_t a = v.toInt();
  224. static_assert(ZT_ADDRESS_LENGTH_HEX == 10, "this must be rewritten for any change in address length");
  225. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 36U) & 0xfU]);
  226. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 32U) & 0xfU]);
  227. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 28U) & 0xfU]);
  228. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 24U) & 0xfU]);
  229. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 20U) & 0xfU]);
  230. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 16U) & 0xfU]);
  231. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 12U) & 0xfU]);
  232. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 8U) & 0xfU]);
  233. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 4U) & 0xfU]);
  234. out.push_back((uint8_t)Utils::HEXCHARS[a & 0xfU]);
  235. out.push_back((uint8_t)'\n');
  236. }
  237. /**
  238. * Append a key=value pair to a buffer (vector or FCV)
  239. *
  240. * @param out Buffer
  241. * @param k Key (must be <= 8 characters)
  242. * @param v Value
  243. */
  244. template< typename V >
  245. ZT_INLINE static void append(V &out, const char *const k, const uint64_t v)
  246. {
  247. char buf[17];
  248. Utils::hex(v, buf);
  249. unsigned int i = 0;
  250. while (buf[i])
  251. out.push_back((uint8_t)buf[i++]);
  252. out.push_back((uint8_t)'\n');
  253. }
  254. template< typename V >
  255. ZT_INLINE static void append(V &out, const char *const k, const int64_t v)
  256. { append(out, k, (uint64_t)v); }
  257. template< typename V >
  258. ZT_INLINE static void append(V &out, const char *const k, const uint32_t v)
  259. { append(out, k, (uint64_t)v); }
  260. template< typename V >
  261. ZT_INLINE static void append(V &out, const char *const k, const int32_t v)
  262. { append(out, k, (uint64_t)v); }
  263. template< typename V >
  264. ZT_INLINE static void append(V &out, const char *const k, const uint16_t v)
  265. { append(out, k, (uint64_t)v); }
  266. template< typename V >
  267. ZT_INLINE static void append(V &out, const char *const k, const int16_t v)
  268. { append(out, k, (uint64_t)v); }
  269. template< typename V >
  270. ZT_INLINE static void append(V &out, const char *const k, const uint8_t v)
  271. { append(out, k, (uint64_t)v); }
  272. template< typename V >
  273. ZT_INLINE static void append(V &out, const char *const k, const int8_t v)
  274. { append(out, k, (uint64_t)v); }
  275. /**
  276. * Append a key=value pair to a buffer (vector or FCV)
  277. *
  278. * @param out Buffer
  279. * @param k Key (must be <= 8 characters)
  280. * @param v Value
  281. */
  282. template< typename V >
  283. ZT_INLINE static void append(V &out, const char *const k, const char *v)
  284. {
  285. if ((v) && (*v)) {
  286. s_appendKey(out, k);
  287. while (*v)
  288. s_appendValueByte(out, (uint8_t)*(v++));
  289. out.push_back((uint8_t)'\n');
  290. }
  291. }
  292. /**
  293. * Append a key=value pair to a buffer (vector or FCV)
  294. *
  295. * @param out Buffer
  296. * @param k Key (must be <= 8 characters)
  297. * @param v Value
  298. * @param vlen Value length in bytes
  299. */
  300. template< typename V >
  301. ZT_INLINE static void append(V &out, const char *const k, const void *const v, const unsigned int vlen)
  302. {
  303. s_appendKey(out, k);
  304. for (unsigned int i = 0; i < vlen; ++i)
  305. s_appendValueByte(out, reinterpret_cast<const uint8_t *>(v)[i]);
  306. out.push_back((uint8_t)'\n');
  307. }
  308. /**
  309. * Append a packet ID as raw bytes in the provided byte order
  310. *
  311. * @param out Buffer
  312. * @param k Key (must be <= 8 characters)
  313. * @param pid Packet ID
  314. */
  315. template< typename V >
  316. static ZT_INLINE void appendPacketId(V &out, const char *const k, const uint64_t pid)
  317. { append(out, k, &pid, 8); }
  318. /**
  319. * Append key=value with any object implementing the correct marshal interface
  320. *
  321. * @param out Buffer
  322. * @param k Key (must be <= 8 characters)
  323. * @param v Marshal-able object
  324. * @return Bytes appended or negative on error (return value of marshal())
  325. */
  326. template< typename V, typename T >
  327. static ZT_INLINE int appendObject(V &out, const char *const k, const T &v)
  328. {
  329. uint8_t tmp[4096]; // large enough for any current object
  330. if (T::marshalSizeMax() > sizeof(tmp))
  331. return -1;
  332. const int mlen = v.marshal(tmp);
  333. if (mlen > 0)
  334. append(out, k, tmp, (unsigned int)mlen);
  335. return mlen;
  336. }
  337. private:
  338. template< typename V >
  339. ZT_INLINE static void s_appendValueByte(V &out, const uint8_t c)
  340. {
  341. switch (c) {
  342. case 0:
  343. out.push_back(92); // backslash
  344. out.push_back(48);
  345. break;
  346. case 10:
  347. out.push_back(92);
  348. out.push_back(110);
  349. break;
  350. case 13:
  351. out.push_back(92);
  352. out.push_back(114);
  353. break;
  354. case 61:
  355. out.push_back(92);
  356. out.push_back(101);
  357. break;
  358. case 92:
  359. out.push_back(92);
  360. out.push_back(92);
  361. break;
  362. default:
  363. out.push_back(c);
  364. break;
  365. }
  366. }
  367. template< typename V >
  368. ZT_INLINE static void s_appendKey(V &out, const char *const k)
  369. {
  370. for (unsigned int i = 0; i < 7; ++i) {
  371. const char kc = k[i];
  372. if (kc == 0)
  373. break;
  374. if ((kc >= 33) && (kc <= 126) && (kc != 61) && (kc != 92)) // printable ASCII with no spaces, equals, or backslash
  375. out.push_back((uint8_t)kc);
  376. }
  377. out.push_back((uint8_t)'=');
  378. }
  379. ZT_INLINE static String s_key(const char *k) noexcept
  380. {
  381. String buf;
  382. buf.clear();
  383. for (unsigned int i = 0; i < 7; ++i) {
  384. const char kc = k[i];
  385. if (kc == 0)
  386. break;
  387. if ((kc >= 33) && (kc <= 126) && (kc != 61) && (kc != 92)) // printable ASCII with no spaces, equals, or backslash
  388. buf.push_back(kc);
  389. }
  390. buf.push_back(0);
  391. return buf;
  392. }
  393. SortedMap <String, Vector< uint8_t >> m_entries;
  394. };
  395. } // namespace ZeroTier
  396. #endif