Pack.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifndef _ZT_PACK_HPP
  28. #define _ZT_PACK_HPP
  29. #include <string>
  30. #include <map>
  31. #include <list>
  32. #include <stdexcept>
  33. #include "Address.hpp"
  34. #include "Identity.hpp"
  35. namespace ZeroTier {
  36. /**
  37. * A very simple archive format for distributing packs of files or resources
  38. *
  39. * This is used for things like the auto-updater. It's not suitable for huge
  40. * files, since at present it must work in memory. Packs support signing with
  41. * identities and signature verification.
  42. */
  43. class Pack
  44. {
  45. public:
  46. /**
  47. * Pack entry structure for looking up deserialized entries
  48. */
  49. struct Entry
  50. {
  51. std::string name;
  52. std::string content;
  53. unsigned char sha256[32];
  54. Address signedBy;
  55. std::string signature;
  56. };
  57. Pack() {}
  58. ~Pack() {}
  59. /**
  60. * @return Vector of all entries
  61. */
  62. std::vector<const Entry *> getAll() const;
  63. /**
  64. * Look up an entry
  65. *
  66. * @param name Name to look up
  67. * @return Pointer to entry if it exists or NULL if not found
  68. */
  69. const Entry *get(const std::string &name) const;
  70. /**
  71. * Add an entry to this pack
  72. *
  73. * @param name Entry to add
  74. * @param content Entry's contents
  75. * @return The new entry
  76. */
  77. const Entry *put(const std::string &name,const std::string &content);
  78. /**
  79. * Remove all entries
  80. */
  81. void clear();
  82. /**
  83. * @return Number of entries in pack
  84. */
  85. inline unsigned int numEntries() const { return (unsigned int)_entries.size(); }
  86. /**
  87. * Serialize this pack
  88. *
  89. * @return Serialized form (compressed with LZ4)
  90. */
  91. std::string serialize() const;
  92. /**
  93. * Deserialize this pack
  94. *
  95. * Any current contents are lost. This does not verify signatures,
  96. * but does check SHA256 hashes for entry integrity. If the return
  97. * value is false, the pack's contents are undefined.
  98. *
  99. * @param sd Serialized data
  100. * @param sdlen Length of serialized data
  101. * @return True on success, false on deserialization error
  102. */
  103. bool deserialize(const void *sd,unsigned int sdlen);
  104. inline bool deserialize(const std::string &sd) { return deserialize(sd.data(),sd.length()); }
  105. /**
  106. * Sign all entries in this pack with a given identity
  107. *
  108. * @param id Identity to sign with
  109. * @return True on signature success, false if error
  110. */
  111. bool signAll(const Identity &id);
  112. /**
  113. * Verify all signed entries
  114. *
  115. * @param id Identity to verify against
  116. * @param mandatory If true, require that all entries be signed and fail if no signature
  117. * @return Vector of entries that failed verification or empty vector if all passed
  118. */
  119. std::vector<const Entry *> verifyAll(const Identity &id,bool mandatory) const;
  120. private:
  121. std::map<std::string,Entry> _entries;
  122. };
  123. } // namespace ZeroTier
  124. #endif