Dictionary.cpp 5.6 KB

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