Dictionary.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. void Dictionary::fromString(const char *s)
  33. {
  34. clear();
  35. bool escapeState = false;
  36. std::string keyBuf;
  37. std::string *element = &keyBuf;
  38. while (*s) {
  39. if (escapeState) {
  40. escapeState = false;
  41. switch(*s) {
  42. case '0':
  43. element->push_back((char)0);
  44. break;
  45. case 'r':
  46. element->push_back('\r');
  47. break;
  48. case 'n':
  49. element->push_back('\n');
  50. break;
  51. default:
  52. element->push_back(*s);
  53. break;
  54. }
  55. } else {
  56. if (*s == '\\') {
  57. escapeState = true;
  58. } else if (*s == '=') {
  59. if (element == &keyBuf)
  60. element = &((*this)[keyBuf]);
  61. } else if ((*s == '\r')||(*s == '\n')) {
  62. if ((element == &keyBuf)&&(keyBuf.length() > 0))
  63. (*this)[keyBuf];
  64. keyBuf = "";
  65. element = &keyBuf;
  66. } else element->push_back(*s);
  67. }
  68. ++s;
  69. }
  70. if ((element == &keyBuf)&&(keyBuf.length() > 0))
  71. (*this)[keyBuf];
  72. }
  73. bool Dictionary::sign(const Identity &id)
  74. {
  75. try {
  76. std::string buf;
  77. _mkSigBuf(buf);
  78. char nows[32];
  79. Utils::snprintf(nows,sizeof(nows),"%llx",(unsigned long long)Utils::now());
  80. C25519::Signature sig(id.sign(buf.data(),buf.length()));
  81. (*this)[ZT_DICTIONARY_SIGNATURE] = Utils::hex(sig.data,sig.size());
  82. (*this)[ZT_DICTIONARY_SIGNATURE_IDENTITY] = id.toString(false);
  83. (*this)[ZT_DICTIONARY_SIGNATURE_TIMESTAMP] = nows;
  84. return true;
  85. } catch ( ... ) {
  86. return false;
  87. }
  88. }
  89. bool Dictionary::verify(const Identity &id) const
  90. {
  91. try {
  92. std::string buf;
  93. _mkSigBuf(buf);
  94. const_iterator sig(find(ZT_DICTIONARY_SIGNATURE));
  95. if (sig == end())
  96. return false;
  97. std::string sigbin(Utils::unhex(sig->second));
  98. return id.verify(buf.data(),buf.length(),sigbin.data(),sigbin.length());
  99. } catch ( ... ) {
  100. return false;
  101. }
  102. }
  103. void Dictionary::_mkSigBuf(std::string &buf) const
  104. {
  105. unsigned long pairs = 0;
  106. for(const_iterator i(begin());i!=end();++i) {
  107. if ((i->first.length() < 2)||( (i->first[0] != '~')&&(i->first[1] != '!') )) {
  108. buf.append(i->first);
  109. buf.push_back('=');
  110. buf.append(i->second);
  111. buf.push_back('\0');
  112. ++pairs;
  113. }
  114. }
  115. buf.push_back((char)0xff);
  116. buf.push_back((char)((pairs >> 24) & 0xff)); // pad with number of key/value pairs at end
  117. buf.push_back((char)((pairs >> 16) & 0xff));
  118. buf.push_back((char)((pairs >> 8) & 0xff));
  119. buf.push_back((char)(pairs & 0xff));
  120. }
  121. void Dictionary::_appendEsc(const char *data,unsigned int len,std::string &to)
  122. {
  123. for(unsigned int i=0;i<len;++i) {
  124. switch(data[i]) {
  125. case 0:
  126. to.append("\\0");
  127. break;
  128. case '\r':
  129. to.append("\\r");
  130. break;
  131. case '\n':
  132. to.append("\\n");
  133. break;
  134. case '\\':
  135. to.append("\\\\");
  136. break;
  137. case '=':
  138. to.append("\\=");
  139. break;
  140. default:
  141. to.push_back(data[i]);
  142. break;
  143. }
  144. }
  145. }
  146. } // namespace ZeroTier