SoftwareUpdater.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdint.h>
  22. #include "SoftwareUpdater.hpp"
  23. #include "../version.h"
  24. #include "../node/Constants.hpp"
  25. #include "../node/Utils.hpp"
  26. #include "../node/SHA512.hpp"
  27. #include "../node/Buffer.hpp"
  28. #include "../node/Node.hpp"
  29. #include "../osdep/OSUtils.hpp"
  30. namespace ZeroTier {
  31. SoftwareUpdater::SoftwareUpdater(Node &node,const char *homePath,bool updateDistributor) :
  32. _node(node),
  33. _lastCheckTime(OSUtils::now()), // check in the future in case we just started, in which case we're probably offline
  34. _homePath(homePath)
  35. {
  36. // Load all updates we are distributing if we are an update distributor and have an update-dist.d folder
  37. if (updateDistributor) {
  38. std::string udd(_homePath + ZT_PATH_SEPARATOR_S + "update-dist.d");
  39. std::vector<std::string> ud(OSUtils::listDirectory(udd.c_str()));
  40. for(std::vector<std::string>::iterator u(ud.begin());u!=ud.end();++u) {
  41. // Each update has a companion .json file describing it. Other files are ignored.
  42. if ((u->length() > 5)&&(u->substr(u->length() - 5,5) == ".json")) {
  43. std::string buf;
  44. if (OSUtils::readFile((udd + ZT_PATH_SEPARATOR_S + *u).c_str(),buf)) {
  45. try {
  46. _D d;
  47. d.meta = OSUtils::jsonParse(buf);
  48. std::string metaHash = OSUtils::jsonBinFromHex(d.meta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH]);
  49. if ((metaHash.length() == ZT_SHA512_DIGEST_LEN)&&(OSUtils::readFile((udd + ZT_PATH_SEPARATOR_S + u->substr(0,u->length() - 5)).c_str(),d.bin))) {
  50. uint8_t sha512[ZT_SHA512_DIGEST_LEN];
  51. SHA512::hash(sha512,d.bin.data(),(unsigned int)d.bin.length());
  52. if (!memcmp(sha512,metaHash.data(),ZT_SHA512_DIGEST_LEN)) { // double check that hash in JSON is correct
  53. _dist[Array<uint8_t,16>(sha512)] = d;
  54. }
  55. }
  56. } catch ( ... ) {} // ignore bad meta JSON, etc.
  57. }
  58. }
  59. }
  60. }
  61. }
  62. SoftwareUpdater::~SoftwareUpdater()
  63. {
  64. }
  65. void SoftwareUpdater::handleSoftwareUpdateUserMessage(uint64_t origin,const void *data,unsigned int len)
  66. {
  67. if (!len) return;
  68. try {
  69. const MessageVerb v = (MessageVerb)reinterpret_cast<const uint8_t *>(data)[0];
  70. switch(v) {
  71. case VERB_GET_LATEST:
  72. case VERB_LATEST: {
  73. nlohmann::json req = OSUtils::jsonParse(std::string(reinterpret_cast<const char *>(data) + 1,len - 1));
  74. if (req.is_object()) {
  75. const unsigned int rvMaj = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR],0);
  76. const unsigned int rvMin = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR],0);
  77. const unsigned int rvRev = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION],0);
  78. if (v == VERB_GET_LATEST) {
  79. if (_dist.size() > 0) {
  80. const nlohmann::json *latest = (const nlohmann::json *)0;
  81. const std::string rSigner = OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_EXPECT_SIGNED_BY],"");
  82. for(std::map< Array<uint8_t,16>,_D >::const_iterator d(_dist.begin());d!=_dist.end();++d) {
  83. if (OSUtils::jsonString(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY],"") == rSigner) {
  84. const unsigned int dvMaj = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR],0);
  85. const unsigned int dvMin = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR],0);
  86. const unsigned int dvRev = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION],0);
  87. if (Utils::compareVersion(dvMaj,dvMin,dvRev,rvMaj,rvMin,rvRev) > 0)
  88. latest = &(d->second.meta);
  89. }
  90. }
  91. if (latest) {
  92. std::string lj;
  93. lj.push_back((char)VERB_LATEST);
  94. lj.append(OSUtils::jsonDump(*latest));
  95. _node.sendUserMessage(origin,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,lj.data(),(unsigned int)lj.length());
  96. }
  97. } // else no reply, since we have nothing to distribute
  98. } else { // VERB_LATEST
  99. if ((origin == ZT_SOFTWARE_UPDATE_SERVICE)&&
  100. (Utils::compareVersion(rvMaj,rvMin,rvRev,ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION) > 0)&&
  101. (OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY],"") == ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY)) {
  102. const unsigned long len = (unsigned long)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE],0);
  103. const std::string hash = OSUtils::jsonBinFromHex(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH]);
  104. if ((len <= ZT_SOFTWARE_UPDATE_MAX_SIZE)&&(hash.length() >= 16)) {
  105. if (_latestMeta != req) {
  106. _latestMeta = req;
  107. _latestBin = "";
  108. memcpy(_latestBinHashPrefix.data,hash.data(),16);
  109. _latestBinLength = len;
  110. _latestBinValid = false;
  111. }
  112. Buffer<128> gd;
  113. gd.append((uint8_t)VERB_GET_DATA);
  114. gd.append(_latestBinHashPrefix.data,16);
  115. gd.append((uint32_t)_latestBin.length());
  116. _node.sendUserMessage(ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,gd.data(),gd.size());
  117. }
  118. }
  119. }
  120. }
  121. } break;
  122. case VERB_GET_DATA:
  123. if ((len >= 21)&&(_dist.size() > 0)) {
  124. std::map< Array<uint8_t,16>,_D >::iterator d(_dist.find(Array<uint8_t,16>(reinterpret_cast<const uint8_t *>(data) + 1)));
  125. if (d != _dist.end()) {
  126. unsigned long idx = (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 17) << 24;
  127. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 18) << 16;
  128. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 19) << 8;
  129. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 20);
  130. if (idx < d->second.bin.length()) {
  131. Buffer<ZT_SOFTWARE_UPDATE_CHUNK_SIZE + 128> buf;
  132. buf.append((uint8_t)VERB_DATA);
  133. buf.append(reinterpret_cast<const uint8_t *>(data) + 1,16);
  134. buf.append((uint32_t)idx);
  135. buf.append(d->second.bin.data() + idx,std::max((unsigned long)ZT_SOFTWARE_UPDATE_CHUNK_SIZE,(unsigned long)(d->second.bin.length() - idx)));
  136. _node.sendUserMessage(origin,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,buf.data(),buf.size());
  137. }
  138. }
  139. }
  140. break;
  141. case VERB_DATA:
  142. if ((len >= 21)&&(!memcmp(_latestBinHashPrefix.data,reinterpret_cast<const uint8_t *>(data) + 1,16))) {
  143. unsigned long idx = (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 17) << 24;
  144. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 18) << 16;
  145. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 19) << 8;
  146. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 20);
  147. if (idx == _latestBin.length())
  148. _latestBin.append(reinterpret_cast<const char *>(data) + 21,len - 21);
  149. if (_latestBin.length() < _latestBinLength) {
  150. Buffer<128> gd;
  151. gd.append((uint8_t)VERB_GET_DATA);
  152. gd.append(_latestBinHashPrefix.data,16);
  153. gd.append((uint32_t)_latestBin.length());
  154. _node.sendUserMessage(ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,gd.data(),gd.size());
  155. }
  156. }
  157. break;
  158. default:
  159. break;
  160. }
  161. } catch ( ... ) {
  162. // Discard bad messages
  163. }
  164. }
  165. nlohmann::json SoftwareUpdater::check()
  166. {
  167. if (_latestBinLength > 0) {
  168. if (_latestBin.length() >= _latestBinLength) {
  169. if (_latestBinValid) {
  170. return _latestMeta;
  171. } else {
  172. // This is the important security verification part!
  173. try {
  174. // (1) Check the hash itself to make sure the image is basically okay
  175. uint8_t sha512[ZT_SHA512_DIGEST_LEN];
  176. SHA512::hash(sha512,_latestBin.data(),(unsigned int)_latestBin.length());
  177. if (Utils::hex(sha512,ZT_SHA512_DIGEST_LEN) == OSUtils::jsonString(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH],"")) {
  178. // (2) Check signature by signing authority
  179. std::string sig(OSUtils::jsonBinFromHex(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNATURE]));
  180. if (Identity(ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY).verify(_latestBin.data(),(unsigned int)_latestBin.length(),sig.data(),(unsigned int)sig.length())) {
  181. // If we passed both of these, the update is good!
  182. _latestBinValid = true;
  183. return _latestMeta;
  184. }
  185. }
  186. } catch ( ... ) {} // any exception equals verification failure
  187. // If we get here, checks failed.
  188. _latestMeta = nlohmann::json();
  189. _latestBin = "";
  190. _latestBinLength = 0;
  191. _latestBinValid = false;
  192. }
  193. } else {
  194. Buffer<128> gd;
  195. gd.append((uint8_t)VERB_GET_DATA);
  196. gd.append(_latestBinHashPrefix.data,16);
  197. gd.append((uint32_t)_latestBin.length());
  198. _node.sendUserMessage(ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,gd.data(),gd.size());
  199. }
  200. }
  201. const uint64_t now = OSUtils::now();
  202. if ((now - _lastCheckTime) >= ZT_SOFTWARE_UPDATE_CHECK_PERIOD) {
  203. }
  204. return nlohmann::json();
  205. }
  206. void SoftwareUpdater::apply()
  207. {
  208. if ((_latestBin.length() == _latestBinLength)&&(_latestBinLength > 0)&&(_latestBinValid)) {
  209. }
  210. }
  211. } // namespace ZeroTier