Dictionary.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <map>
  32. #include <stdexcept>
  33. #include "Constants.hpp"
  34. #include "Utils.hpp"
  35. // Three fields are added/updated by sign()
  36. #define ZT_DICTIONARY_SIGNATURE "~!ed25519"
  37. #define ZT_DICTIONARY_SIGNATURE_IDENTITY "~!sigid"
  38. #define ZT_DICTIONARY_SIGNATURE_TIMESTAMP "~!sigts"
  39. namespace ZeroTier {
  40. class Identity;
  41. /**
  42. * Simple key/value dictionary with string serialization
  43. *
  44. * The serialization format is a flat key=value with backslash escape.
  45. * It does not support comments or other syntactic complexities. It is
  46. * human-readable if the keys and values in the dictionary are also
  47. * human-readable. Otherwise it might contain unprintable characters.
  48. *
  49. * Keys beginning with "~!" are reserved for signature data fields.
  50. *
  51. * Note: the signature code depends on std::map<> being sorted, but no
  52. * other code does. So if the underlying data structure is ever swapped
  53. * out for an unsorted one, the signature code will have to be updated
  54. * to sort before composing the string to sign.
  55. */
  56. class Dictionary : public std::map<std::string,std::string>
  57. {
  58. public:
  59. Dictionary() {}
  60. /**
  61. * @param s String-serialized dictionary
  62. * @param maxlen Maximum length of buffer
  63. */
  64. Dictionary(const char *s,unsigned int maxlen) { fromString(s,maxlen); }
  65. /**
  66. * @param s String-serialized dictionary
  67. */
  68. Dictionary(const std::string &s) { fromString(s.c_str(),(unsigned int)s.length()); }
  69. /**
  70. * Get a key, throwing an exception if it is not present
  71. *
  72. * @param key Key to look up
  73. * @return Reference to value
  74. * @throws std::invalid_argument Key not found
  75. */
  76. inline const std::string &get(const std::string &key) const
  77. throw(std::invalid_argument)
  78. {
  79. const_iterator e(find(key));
  80. if (e == end())
  81. throw std::invalid_argument(std::string("missing required field: ")+key);
  82. return e->second;
  83. }
  84. /**
  85. * Get a key, returning a default if not present
  86. *
  87. * @param key Key to look up
  88. * @param dfl Default if not present
  89. * @return Value or default
  90. */
  91. inline const std::string &get(const std::string &key,const std::string &dfl) const
  92. {
  93. const_iterator e(find(key));
  94. if (e == end())
  95. return dfl;
  96. return e->second;
  97. }
  98. /**
  99. * @param key Key to get
  100. * @param dfl Default boolean result if key not found or empty (default: false)
  101. * @return Boolean value of key
  102. */
  103. inline bool getBoolean(const std::string &key,bool dfl = false) const
  104. {
  105. const_iterator e(find(key));
  106. if (e == end())
  107. return dfl;
  108. if (e->second.length() < 1)
  109. return dfl;
  110. switch(e->second[0]) {
  111. case '1':
  112. case 't':
  113. case 'T':
  114. case 'y':
  115. case 'Y':
  116. return true;
  117. }
  118. return false;
  119. }
  120. /**
  121. * @param key Key to get
  122. * @param dfl Default value if not present (default: 0)
  123. * @return Value converted to unsigned 64-bit int or 0 if not found
  124. */
  125. inline uint64_t getUInt(const std::string &key,uint64_t dfl = 0) const
  126. {
  127. const_iterator e(find(key));
  128. if (e == end())
  129. return dfl;
  130. return Utils::strToU64(e->second.c_str());
  131. }
  132. /**
  133. * @param key Key to get
  134. * @param dfl Default value if not present (default: 0)
  135. * @return Value converted to unsigned 64-bit int or 0 if not found
  136. */
  137. inline uint64_t getHexUInt(const std::string &key,uint64_t dfl = 0) const
  138. {
  139. const_iterator e(find(key));
  140. if (e == end())
  141. return dfl;
  142. return Utils::hexStrToU64(e->second.c_str());
  143. }
  144. /**
  145. * @param key Key to get
  146. * @param dfl Default value if not present (default: 0)
  147. * @return Value converted to signed 64-bit int or 0 if not found
  148. */
  149. inline int64_t getInt(const std::string &key,int64_t dfl = 0) const
  150. {
  151. const_iterator e(find(key));
  152. if (e == end())
  153. return dfl;
  154. return Utils::strTo64(e->second.c_str());
  155. }
  156. /**
  157. * @param key Key to set
  158. * @param value String value
  159. */
  160. inline void set(const std::string &key,const char *value)
  161. {
  162. (*this)[key] = value;
  163. }
  164. /**
  165. * @param key Key to set
  166. * @param value String value
  167. */
  168. inline void set(const std::string &key,const std::string &value)
  169. {
  170. (*this)[key] = value;
  171. }
  172. /**
  173. * @param key Key to set
  174. * @param value Boolean value
  175. */
  176. inline void set(const std::string &key,bool value)
  177. {
  178. (*this)[key] = ((value) ? "1" : "0");
  179. }
  180. /**
  181. * @param key Key to set
  182. * @param value Integer value
  183. */
  184. inline void set(const std::string &key,uint64_t value)
  185. {
  186. char tmp[24];
  187. Utils::snprintf(tmp,sizeof(tmp),"%llu",(unsigned long long)value);
  188. (*this)[key] = tmp;
  189. }
  190. /**
  191. * @param key Key to set
  192. * @param value Integer value
  193. */
  194. inline void set(const std::string &key,int64_t value)
  195. {
  196. char tmp[24];
  197. Utils::snprintf(tmp,sizeof(tmp),"%lld",(long long)value);
  198. (*this)[key] = tmp;
  199. }
  200. /**
  201. * @param key Key to set
  202. * @param value Integer value
  203. */
  204. inline void setHex(const std::string &key,uint64_t value)
  205. {
  206. char tmp[24];
  207. Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value);
  208. (*this)[key] = tmp;
  209. }
  210. /**
  211. * @param key Key to check
  212. * @return True if dictionary contains key
  213. */
  214. inline bool contains(const std::string &key) const { return (find(key) != end()); }
  215. /**
  216. * @return String-serialized dictionary
  217. */
  218. inline std::string toString() const
  219. {
  220. std::string s;
  221. for(const_iterator kv(begin());kv!=end();++kv) {
  222. _appendEsc(kv->first.data(),(unsigned int)kv->first.length(),s);
  223. s.push_back('=');
  224. _appendEsc(kv->second.data(),(unsigned int)kv->second.length(),s);
  225. s.append(ZT_EOL_S);
  226. }
  227. return s;
  228. }
  229. /**
  230. * Clear and initialize from a string
  231. *
  232. * @param s String-serialized dictionary
  233. * @param maxlen Maximum length of string buffer
  234. */
  235. void fromString(const char *s,unsigned int maxlen);
  236. inline void fromString(const std::string &s) { fromString(s.c_str(),(unsigned int)s.length()); }
  237. /**
  238. * @return True if this dictionary is cryptographically signed
  239. */
  240. inline bool hasSignature() const { return (find(ZT_DICTIONARY_SIGNATURE) != end()); }
  241. /**
  242. * @return Signing identity in string-serialized format or empty string if none
  243. */
  244. inline std::string signingIdentity() const { return get(ZT_DICTIONARY_SIGNATURE_IDENTITY,std::string()); }
  245. /**
  246. * @return Signature timestamp in milliseconds since epoch or 0 if none
  247. */
  248. uint64_t signatureTimestamp() const;
  249. /**
  250. * Remove any signature from this dictionary
  251. */
  252. inline void removeSignature()
  253. {
  254. erase(ZT_DICTIONARY_SIGNATURE);
  255. erase(ZT_DICTIONARY_SIGNATURE_IDENTITY);
  256. erase(ZT_DICTIONARY_SIGNATURE_TIMESTAMP);
  257. }
  258. /**
  259. * Add or update signature fields with a signature of all other keys and values
  260. *
  261. * @param with Identity to sign with (must have secret key)
  262. * @return True on success
  263. */
  264. bool sign(const Identity &id);
  265. /**
  266. * Verify signature against an identity
  267. *
  268. * @param id Identity to verify against
  269. * @return True if signature verification OK
  270. */
  271. bool verify(const Identity &id) const;
  272. private:
  273. void _mkSigBuf(std::string &buf) const;
  274. static void _appendEsc(const char *data,unsigned int len,std::string &to);
  275. };
  276. } // namespace ZeroTier
  277. #endif