SoftwareUpdater.cpp 11 KB

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