Dictionary.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #include "Dictionary.hpp"
  28. #include "C25519.hpp"
  29. #include "Identity.hpp"
  30. #include "Utils.hpp"
  31. namespace ZeroTier {
  32. Dictionary::iterator Dictionary::find(const std::string &key)
  33. {
  34. for(iterator i(begin());i!=end();++i) {
  35. if (i->first == key)
  36. return i;
  37. }
  38. return end();
  39. }
  40. Dictionary::const_iterator Dictionary::find(const std::string &key) const
  41. {
  42. for(const_iterator i(begin());i!=end();++i) {
  43. if (i->first == key)
  44. return i;
  45. }
  46. return end();
  47. }
  48. bool Dictionary::getBoolean(const std::string &key,bool dfl) const
  49. {
  50. const_iterator e(find(key));
  51. if (e == end())
  52. return dfl;
  53. if (e->second.length() < 1)
  54. return dfl;
  55. switch(e->second[0]) {
  56. case '1':
  57. case 't':
  58. case 'T':
  59. case 'y':
  60. case 'Y':
  61. return true;
  62. }
  63. return false;
  64. }
  65. std::string &Dictionary::operator[](const std::string &key)
  66. {
  67. for(iterator i(begin());i!=end();++i) {
  68. if (i->first == key)
  69. return i->second;
  70. }
  71. push_back(std::pair<std::string,std::string>(key,std::string()));
  72. std::sort(begin(),end());
  73. for(iterator i(begin());i!=end();++i) {
  74. if (i->first == key)
  75. return i->second;
  76. }
  77. return front().second; // should be unreachable!
  78. }
  79. std::string Dictionary::toString() const
  80. {
  81. std::string s;
  82. for(const_iterator kv(begin());kv!=end();++kv) {
  83. _appendEsc(kv->first.data(),(unsigned int)kv->first.length(),s);
  84. s.push_back('=');
  85. _appendEsc(kv->second.data(),(unsigned int)kv->second.length(),s);
  86. s.append(ZT_EOL_S);
  87. }
  88. return s;
  89. }
  90. void Dictionary::updateFromString(const char *s,unsigned int maxlen)
  91. {
  92. bool escapeState = false;
  93. std::string keyBuf;
  94. std::string *element = &keyBuf;
  95. const char *end = s + maxlen;
  96. while ((*s)&&(s < end)) {
  97. if (escapeState) {
  98. escapeState = false;
  99. switch(*s) {
  100. case '0':
  101. element->push_back((char)0);
  102. break;
  103. case 'r':
  104. element->push_back('\r');
  105. break;
  106. case 'n':
  107. element->push_back('\n');
  108. break;
  109. default:
  110. element->push_back(*s);
  111. break;
  112. }
  113. } else {
  114. if (*s == '\\') {
  115. escapeState = true;
  116. } else if (*s == '=') {
  117. if (element == &keyBuf)
  118. element = &((*this)[keyBuf]);
  119. } else if ((*s == '\r')||(*s == '\n')) {
  120. if ((element == &keyBuf)&&(keyBuf.length() > 0))
  121. (*this)[keyBuf];
  122. keyBuf = "";
  123. element = &keyBuf;
  124. } else element->push_back(*s);
  125. }
  126. ++s;
  127. }
  128. if ((element == &keyBuf)&&(keyBuf.length() > 0))
  129. (*this)[keyBuf];
  130. }
  131. void Dictionary::fromString(const char *s,unsigned int maxlen)
  132. {
  133. clear();
  134. updateFromString(s,maxlen);
  135. }
  136. void Dictionary::eraseKey(const std::string &key)
  137. {
  138. for(iterator i(begin());i!=end();++i) {
  139. if (i->first == key) {
  140. this->erase(i);
  141. return;
  142. }
  143. }
  144. }
  145. bool Dictionary::sign(const Identity &id,uint64_t now)
  146. {
  147. try {
  148. // Sign identity and timestamp fields too. If there's an existing
  149. // signature, _mkSigBuf() ignores it.
  150. char nows[32];
  151. Utils::snprintf(nows,sizeof(nows),"%llx",(unsigned long long)now);
  152. (*this)[ZT_DICTIONARY_SIGNATURE_IDENTITY] = id.toString(false);
  153. (*this)[ZT_DICTIONARY_SIGNATURE_TIMESTAMP] = nows;
  154. // Create a blob to hash and sign from fields in sorted order
  155. std::string buf;
  156. _mkSigBuf(buf);
  157. // Add signature field
  158. C25519::Signature sig(id.sign(buf.data(),(unsigned int)buf.length()));
  159. (*this)[ZT_DICTIONARY_SIGNATURE] = Utils::hex(sig.data,(unsigned int)sig.size());
  160. return true;
  161. } catch ( ... ) {
  162. // Probably means identity has no secret key field
  163. removeSignature();
  164. return false;
  165. }
  166. }
  167. bool Dictionary::verify(const Identity &id) const
  168. {
  169. try {
  170. std::string buf;
  171. _mkSigBuf(buf);
  172. const_iterator sig(find(ZT_DICTIONARY_SIGNATURE));
  173. if (sig == end())
  174. return false;
  175. std::string sigbin(Utils::unhex(sig->second));
  176. return id.verify(buf.data(),(unsigned int)buf.length(),sigbin.data(),(unsigned int)sigbin.length());
  177. } catch ( ... ) {
  178. return false;
  179. }
  180. }
  181. uint64_t Dictionary::signatureTimestamp() const
  182. {
  183. const_iterator ts(find(ZT_DICTIONARY_SIGNATURE_TIMESTAMP));
  184. if (ts == end())
  185. return 0;
  186. return Utils::hexStrToU64(ts->second.c_str());
  187. }
  188. void Dictionary::_mkSigBuf(std::string &buf) const
  189. {
  190. unsigned long pairs = 0;
  191. for(const_iterator i(begin());i!=end();++i) {
  192. if (i->first != ZT_DICTIONARY_SIGNATURE) {
  193. buf.append(i->first);
  194. buf.push_back('=');
  195. buf.append(i->second);
  196. buf.push_back('\0');
  197. ++pairs;
  198. }
  199. }
  200. buf.push_back((char)0xff);
  201. buf.push_back((char)((pairs >> 24) & 0xff)); // pad with number of key/value pairs at end
  202. buf.push_back((char)((pairs >> 16) & 0xff));
  203. buf.push_back((char)((pairs >> 8) & 0xff));
  204. buf.push_back((char)(pairs & 0xff));
  205. }
  206. void Dictionary::_appendEsc(const char *data,unsigned int len,std::string &to)
  207. {
  208. for(unsigned int i=0;i<len;++i) {
  209. switch(data[i]) {
  210. case 0:
  211. to.append("\\0");
  212. break;
  213. case '\r':
  214. to.append("\\r");
  215. break;
  216. case '\n':
  217. to.append("\\n");
  218. break;
  219. case '\\':
  220. to.append("\\\\");
  221. break;
  222. case '=':
  223. to.append("\\=");
  224. break;
  225. default:
  226. to.push_back(data[i]);
  227. break;
  228. }
  229. }
  230. }
  231. } // namespace ZeroTier