Dictionary.cpp 4.4 KB

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