Dictionary.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. void Dictionary::updateFromString(const char *s,unsigned int maxlen)
  33. {
  34. bool escapeState = false;
  35. std::string keyBuf;
  36. std::string *element = &keyBuf;
  37. const char *end = s + maxlen;
  38. while ((*s)&&(s < end)) {
  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. void Dictionary::fromString(const char *s,unsigned int maxlen)
  74. {
  75. clear();
  76. updateFromString(s,maxlen);
  77. }
  78. bool Dictionary::sign(const Identity &id,uint64_t now)
  79. {
  80. try {
  81. // Sign identity and timestamp fields too. If there's an existing
  82. // signature, _mkSigBuf() ignores it.
  83. char nows[32];
  84. Utils::snprintf(nows,sizeof(nows),"%llx",(unsigned long long)now);
  85. (*this)[ZT_DICTIONARY_SIGNATURE_IDENTITY] = id.toString(false);
  86. (*this)[ZT_DICTIONARY_SIGNATURE_TIMESTAMP] = nows;
  87. // Create a blob to hash and sign from fields in sorted order
  88. std::string buf;
  89. _mkSigBuf(buf);
  90. // Add signature field
  91. C25519::Signature sig(id.sign(buf.data(),(unsigned int)buf.length()));
  92. (*this)[ZT_DICTIONARY_SIGNATURE] = Utils::hex(sig.data,(unsigned int)sig.size());
  93. return true;
  94. } catch ( ... ) {
  95. // Probably means identity has no secret key field
  96. removeSignature();
  97. return false;
  98. }
  99. }
  100. bool Dictionary::verify(const Identity &id) const
  101. {
  102. try {
  103. std::string buf;
  104. _mkSigBuf(buf);
  105. const_iterator sig(find(ZT_DICTIONARY_SIGNATURE));
  106. if (sig == end())
  107. return false;
  108. std::string sigbin(Utils::unhex(sig->second));
  109. return id.verify(buf.data(),(unsigned int)buf.length(),sigbin.data(),(unsigned int)sigbin.length());
  110. } catch ( ... ) {
  111. return false;
  112. }
  113. }
  114. uint64_t Dictionary::signatureTimestamp() const
  115. {
  116. const_iterator ts(find(ZT_DICTIONARY_SIGNATURE_TIMESTAMP));
  117. if (ts == end())
  118. return 0;
  119. return Utils::hexStrToU64(ts->second.c_str());
  120. }
  121. void Dictionary::_mkSigBuf(std::string &buf) const
  122. {
  123. unsigned long pairs = 0;
  124. for(const_iterator i(begin());i!=end();++i) {
  125. if (i->first != ZT_DICTIONARY_SIGNATURE) {
  126. buf.append(i->first);
  127. buf.push_back('=');
  128. buf.append(i->second);
  129. buf.push_back('\0');
  130. ++pairs;
  131. }
  132. }
  133. buf.push_back((char)0xff);
  134. buf.push_back((char)((pairs >> 24) & 0xff)); // pad with number of key/value pairs at end
  135. buf.push_back((char)((pairs >> 16) & 0xff));
  136. buf.push_back((char)((pairs >> 8) & 0xff));
  137. buf.push_back((char)(pairs & 0xff));
  138. }
  139. void Dictionary::_appendEsc(const char *data,unsigned int len,std::string &to)
  140. {
  141. for(unsigned int i=0;i<len;++i) {
  142. switch(data[i]) {
  143. case 0:
  144. to.append("\\0");
  145. break;
  146. case '\r':
  147. to.append("\\r");
  148. break;
  149. case '\n':
  150. to.append("\\n");
  151. break;
  152. case '\\':
  153. to.append("\\\\");
  154. break;
  155. case '=':
  156. to.append("\\=");
  157. break;
  158. default:
  159. to.push_back(data[i]);
  160. break;
  161. }
  162. }
  163. }
  164. } // namespace ZeroTier