Pack.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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 <iostream>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include "Pack.hpp"
  31. #include "BlobArray.hpp"
  32. #include "Utils.hpp"
  33. #include <openssl/sha.h>
  34. namespace ZeroTier {
  35. std::vector<const Pack::Entry *> Pack::getAll() const
  36. {
  37. std::vector<const Entry *> v;
  38. for(std::map<std::string,Entry>::const_iterator e=_entries.begin();e!=_entries.end();++e)
  39. v.push_back(&(e->second));
  40. return v;
  41. }
  42. const Pack::Entry *Pack::get(const std::string &name) const
  43. {
  44. std::map<std::string,Entry>::const_iterator e(_entries.find(name));
  45. return ((e == _entries.end()) ? (const Entry *)0 : &(e->second));
  46. }
  47. const Pack::Entry *Pack::put(const std::string &name,const std::string &content)
  48. {
  49. SHA256_CTX sha;
  50. Pack::Entry &e = _entries[name];
  51. e.name = name;
  52. e.content = content;
  53. SHA256_Init(&sha);
  54. SHA256_Update(&sha,content.data(),content.length());
  55. SHA256_Final(e.sha256,&sha);
  56. e.signedBy.zero();
  57. e.signature.assign((const char *)0,0);
  58. return &e;
  59. }
  60. void Pack::clear()
  61. {
  62. _entries.clear();
  63. }
  64. std::string Pack::serialize() const
  65. {
  66. BlobArray archive;
  67. for(std::map<std::string,Entry>::const_iterator e=_entries.begin();e!=_entries.end();++e) {
  68. BlobArray entry;
  69. entry.push_back(e->second.name);
  70. entry.push_back(e->second.content);
  71. entry.push_back(std::string((const char *)e->second.sha256,sizeof(e->second.sha256)));
  72. entry.push_back(std::string((const char *)e->second.signedBy.data(),e->second.signedBy.size()));
  73. entry.push_back(e->second.signature);
  74. archive.push_back(entry.serialize());
  75. }
  76. std::string ser(archive.serialize());
  77. std::string comp;
  78. Utils::compress(ser.begin(),ser.end(),Utils::StringAppendOutput(comp));
  79. return comp;
  80. }
  81. bool Pack::deserialize(const void *sd,unsigned int sdlen)
  82. {
  83. unsigned char dig[32];
  84. SHA256_CTX sha;
  85. std::string decomp;
  86. if (!Utils::decompress(((const char *)sd),((const char *)sd) + sdlen,Utils::StringAppendOutput(decomp)))
  87. return false;
  88. BlobArray archive;
  89. archive.deserialize(decomp.data(),decomp.length());
  90. clear();
  91. for(BlobArray::const_iterator i=archive.begin();i!=archive.end();++i) {
  92. BlobArray entry;
  93. entry.deserialize(i->data(),i->length());
  94. if (entry.size() != 5) return false;
  95. if (entry[2].length() != 32) return false; // SHA-256
  96. if (entry[3].length() != ZT_ADDRESS_LENGTH) return false; // Address
  97. Pack::Entry &e = _entries[entry[0]];
  98. e.name = entry[0];
  99. e.content = entry[1];
  100. SHA256_Init(&sha);
  101. SHA256_Update(&sha,e.content.data(),e.content.length());
  102. SHA256_Final(dig,&sha);
  103. if (memcmp(dig,entry[2].data(),32)) return false; // integrity check failed
  104. memcpy(e.sha256,dig,32);
  105. if (entry[3].length() == ZT_ADDRESS_LENGTH)
  106. e.signedBy = entry[3].data();
  107. else e.signedBy.zero();
  108. e.signature = entry[4];
  109. }
  110. return true;
  111. }
  112. bool Pack::signAll(const Identity &id)
  113. {
  114. for(std::map<std::string,Entry>::iterator e=_entries.begin();e!=_entries.end();++e) {
  115. e->second.signedBy = id.address();
  116. e->second.signature = id.sign(e->second.sha256);
  117. if (!e->second.signature.length())
  118. return false;
  119. }
  120. return true;
  121. }
  122. std::vector<const Pack::Entry *> Pack::verifyAll(const Identity &id,bool mandatory) const
  123. {
  124. std::vector<const Entry *> bad;
  125. for(std::map<std::string,Entry>::const_iterator e=_entries.begin();e!=_entries.end();++e) {
  126. if ((e->second.signedBy)&&(e->second.signature.length())) {
  127. if (id.address() != e->second.signedBy)
  128. bad.push_back(&(e->second));
  129. else if (!id.verifySignature(e->second.sha256,e->second.signature.data(),e->second.signature.length()))
  130. bad.push_back(&(e->second));
  131. } else if (mandatory)
  132. bad.push_back(&(e->second));
  133. }
  134. return bad;
  135. }
  136. } // namespace ZeroTier