Dictionary.cpp 5.7 KB

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