Dictionary.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_DICTIONARY_HPP
  28. #define ZT_DICTIONARY_HPP
  29. #include <stdint.h>
  30. #include <string>
  31. #include <vector>
  32. #include <stdexcept>
  33. #include <algorithm>
  34. #include "Constants.hpp"
  35. #include "Utils.hpp"
  36. // Three fields are added/updated by sign()
  37. #define ZT_DICTIONARY_SIGNATURE "~!ed25519"
  38. #define ZT_DICTIONARY_SIGNATURE_IDENTITY "~!sigid"
  39. #define ZT_DICTIONARY_SIGNATURE_TIMESTAMP "~!sigts"
  40. namespace ZeroTier {
  41. class Identity;
  42. /**
  43. * Simple key/value dictionary with string serialization
  44. *
  45. * The serialization format is a flat key=value with backslash escape.
  46. * It does not support comments or other syntactic complexities. It is
  47. * human-readable if the keys and values in the dictionary are also
  48. * human-readable. Otherwise it might contain unprintable characters.
  49. *
  50. * Keys beginning with "~!" are reserved for signature data fields.
  51. *
  52. * It's stored as a simple vector and can be linearly scanned or
  53. * binary searched. Dictionaries are only used for very small things
  54. * outside the core loop, so this is not a significant performance
  55. * issue and it reduces memory use and code footprint.
  56. */
  57. class Dictionary : public std::vector< std::pair<std::string,std::string> >
  58. {
  59. public:
  60. Dictionary() {}
  61. /**
  62. * @param s String-serialized dictionary
  63. * @param maxlen Maximum length of buffer
  64. */
  65. Dictionary(const char *s,unsigned int maxlen) { fromString(s,maxlen); }
  66. /**
  67. * @param s String-serialized dictionary
  68. */
  69. Dictionary(const std::string &s) { fromString(s.c_str(),(unsigned int)s.length()); }
  70. iterator find(const std::string &key);
  71. const_iterator find(const std::string &key) const;
  72. /**
  73. * Get a key, returning a default if not present
  74. *
  75. * @param key Key to look up
  76. * @param dfl Default if not present
  77. * @return Value or default
  78. */
  79. inline const std::string &get(const std::string &key,const std::string &dfl) const
  80. {
  81. const_iterator e(find(key));
  82. if (e == end())
  83. return dfl;
  84. return e->second;
  85. }
  86. /**
  87. * @param key Key to get
  88. * @param dfl Default boolean result if key not found or empty (default: false)
  89. * @return Boolean value of key
  90. */
  91. bool getBoolean(const std::string &key,bool dfl = false) const;
  92. /**
  93. * @param key Key to get
  94. * @param dfl Default value if not present (default: 0)
  95. * @return Value converted to unsigned 64-bit int or 0 if not found
  96. */
  97. inline uint64_t getUInt(const std::string &key,uint64_t dfl = 0) const
  98. {
  99. const_iterator e(find(key));
  100. if (e == end())
  101. return dfl;
  102. return Utils::strToU64(e->second.c_str());
  103. }
  104. /**
  105. * @param key Key to get
  106. * @param dfl Default value if not present (default: 0)
  107. * @return Value converted to unsigned 64-bit int or 0 if not found
  108. */
  109. inline uint64_t getHexUInt(const std::string &key,uint64_t dfl = 0) const
  110. {
  111. const_iterator e(find(key));
  112. if (e == end())
  113. return dfl;
  114. return Utils::hexStrToU64(e->second.c_str());
  115. }
  116. /**
  117. * @param key Key to get
  118. * @param dfl Default value if not present (default: 0)
  119. * @return Value converted to signed 64-bit int or 0 if not found
  120. */
  121. inline int64_t getInt(const std::string &key,int64_t dfl = 0) const
  122. {
  123. const_iterator e(find(key));
  124. if (e == end())
  125. return dfl;
  126. return Utils::strTo64(e->second.c_str());
  127. }
  128. std::string &operator[](const std::string &key);
  129. /**
  130. * @param key Key to set
  131. * @param value String value
  132. */
  133. inline void set(const std::string &key,const char *value)
  134. {
  135. (*this)[key] = value;
  136. }
  137. /**
  138. * @param key Key to set
  139. * @param value String value
  140. */
  141. inline void set(const std::string &key,const std::string &value)
  142. {
  143. (*this)[key] = value;
  144. }
  145. /**
  146. * @param key Key to set
  147. * @param value Boolean value
  148. */
  149. inline void set(const std::string &key,bool value)
  150. {
  151. (*this)[key] = ((value) ? "1" : "0");
  152. }
  153. /**
  154. * @param key Key to set
  155. * @param value Integer value
  156. */
  157. inline void set(const std::string &key,uint64_t value)
  158. {
  159. char tmp[24];
  160. Utils::snprintf(tmp,sizeof(tmp),"%llu",(unsigned long long)value);
  161. (*this)[key] = tmp;
  162. }
  163. /**
  164. * @param key Key to set
  165. * @param value Integer value
  166. */
  167. inline void set(const std::string &key,int64_t value)
  168. {
  169. char tmp[24];
  170. Utils::snprintf(tmp,sizeof(tmp),"%lld",(long long)value);
  171. (*this)[key] = tmp;
  172. }
  173. /**
  174. * @param key Key to set
  175. * @param value Integer value
  176. */
  177. inline void setHex(const std::string &key,uint64_t value)
  178. {
  179. char tmp[24];
  180. Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value);
  181. (*this)[key] = tmp;
  182. }
  183. /**
  184. * @param key Key to check
  185. * @return True if dictionary contains key
  186. */
  187. inline bool contains(const std::string &key) const { return (find(key) != end()); }
  188. /**
  189. * @return String-serialized dictionary
  190. */
  191. std::string toString() const;
  192. /**
  193. * Clear and initialize from a string
  194. *
  195. * @param s String-serialized dictionary
  196. * @param maxlen Maximum length of string buffer
  197. */
  198. void fromString(const char *s,unsigned int maxlen);
  199. inline void fromString(const std::string &s) { fromString(s.c_str(),(unsigned int)s.length()); }
  200. void updateFromString(const char *s,unsigned int maxlen);
  201. inline void update(const char *s,unsigned int maxlen) { updateFromString(s, maxlen); }
  202. inline void update(const std::string &s) { updateFromString(s.c_str(),(unsigned int)s.length()); }
  203. /**
  204. * @return True if this dictionary is cryptographically signed
  205. */
  206. inline bool hasSignature() const { return (find(ZT_DICTIONARY_SIGNATURE) != end()); }
  207. /**
  208. * @return Signing identity in string-serialized format or empty string if none
  209. */
  210. inline std::string signingIdentity() const { return get(ZT_DICTIONARY_SIGNATURE_IDENTITY,std::string()); }
  211. /**
  212. * @return Signature timestamp in milliseconds since epoch or 0 if none
  213. */
  214. uint64_t signatureTimestamp() const;
  215. /**
  216. * @param key Key to erase
  217. */
  218. void eraseKey(const std::string &key);
  219. /**
  220. * Remove any signature from this dictionary
  221. */
  222. inline void removeSignature()
  223. {
  224. eraseKey(ZT_DICTIONARY_SIGNATURE);
  225. eraseKey(ZT_DICTIONARY_SIGNATURE_IDENTITY);
  226. eraseKey(ZT_DICTIONARY_SIGNATURE_TIMESTAMP);
  227. }
  228. /**
  229. * Add or update signature fields with a signature of all other keys and values
  230. *
  231. * @param with Identity to sign with (must have secret key)
  232. * @param now Current time
  233. * @return True on success
  234. */
  235. bool sign(const Identity &id,uint64_t now);
  236. /**
  237. * Verify signature against an identity
  238. *
  239. * @param id Identity to verify against
  240. * @return True if signature verification OK
  241. */
  242. bool verify(const Identity &id) const;
  243. private:
  244. void _mkSigBuf(std::string &buf) const;
  245. static void _appendEsc(const char *data,unsigned int len,std::string &to);
  246. };
  247. } // namespace ZeroTier
  248. #endif