Dictionary.hpp 7.2 KB

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