Dictionary.hpp 7.3 KB

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