Dictionary.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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,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(),buf.length(),sigbin.data(),sigbin.length());
  106. } catch ( ... ) {
  107. return false;
  108. }
  109. }
  110. void Dictionary::_mkSigBuf(std::string &buf) const
  111. {
  112. unsigned long pairs = 0;
  113. for(const_iterator i(begin());i!=end();++i) {
  114. if (i->first != ZT_DICTIONARY_SIGNATURE) {
  115. buf.append(i->first);
  116. buf.push_back('=');
  117. buf.append(i->second);
  118. buf.push_back('\0');
  119. ++pairs;
  120. }
  121. }
  122. buf.push_back((char)0xff);
  123. buf.push_back((char)((pairs >> 24) & 0xff)); // pad with number of key/value pairs at end
  124. buf.push_back((char)((pairs >> 16) & 0xff));
  125. buf.push_back((char)((pairs >> 8) & 0xff));
  126. buf.push_back((char)(pairs & 0xff));
  127. }
  128. void Dictionary::_appendEsc(const char *data,unsigned int len,std::string &to)
  129. {
  130. for(unsigned int i=0;i<len;++i) {
  131. switch(data[i]) {
  132. case 0:
  133. to.append("\\0");
  134. break;
  135. case '\r':
  136. to.append("\\r");
  137. break;
  138. case '\n':
  139. to.append("\\n");
  140. break;
  141. case '\\':
  142. to.append("\\\\");
  143. break;
  144. case '=':
  145. to.append("\\=");
  146. break;
  147. default:
  148. to.push_back(data[i]);
  149. break;
  150. }
  151. }
  152. }
  153. } // namespace ZeroTier