CertificateOfMembership.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_CERTIFICATEOFMEMBERSHIP_HPP
  9. #define ZT_CERTIFICATEOFMEMBERSHIP_HPP
  10. #include "Address.hpp"
  11. #include "Buffer.hpp"
  12. #include "Constants.hpp"
  13. #include "Credential.hpp"
  14. #include "ECC.hpp"
  15. #include "Identity.hpp"
  16. #include "Utils.hpp"
  17. #include <algorithm>
  18. #include <stdexcept>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <string>
  22. /**
  23. * Maximum number of qualifiers allowed in a COM (absolute max: 65535)
  24. */
  25. #define ZT_NETWORK_COM_MAX_QUALIFIERS 8
  26. namespace ZeroTier {
  27. class RuntimeEnvironment;
  28. /**
  29. * Certificate of network membership
  30. *
  31. * The COM contains a sorted set of three-element tuples called qualifiers.
  32. * These contain an id, a value, and a maximum delta.
  33. *
  34. * The ID is arbitrary and should be assigned using a scheme that makes
  35. * every ID globally unique. IDs beneath 65536 are reserved for global
  36. * assignment by ZeroTier Networks.
  37. *
  38. * The value's meaning is ID-specific and isn't important here. What's
  39. * important is the value and the third member of the tuple: the maximum
  40. * delta. The maximum delta is the maximum difference permitted between
  41. * values for a given ID between certificates for the two certificates to
  42. * themselves agree.
  43. *
  44. * Network membership is checked by checking whether a peer's certificate
  45. * agrees with your own. The timestamp provides the fundamental criterion--
  46. * each member of a private network must constantly obtain new certificates
  47. * often enough to stay within the max delta for this qualifier. But other
  48. * criteria could be added in the future for very special behaviors, things
  49. * like latitude and longitude for instance.
  50. *
  51. * This is a memcpy()'able structure and is safe (in a crash sense) to modify
  52. * without locks.
  53. */
  54. class CertificateOfMembership : public Credential {
  55. public:
  56. static inline Credential::Type credentialType()
  57. {
  58. return Credential::CREDENTIAL_TYPE_COM;
  59. }
  60. /**
  61. * Reserved qualifier IDs
  62. *
  63. * IDs below 1024 are reserved for use as standard IDs. Others are available
  64. * for user-defined use.
  65. *
  66. * Addition of new required fields requires that code in hasRequiredFields
  67. * be updated as well.
  68. */
  69. enum ReservedId {
  70. /**
  71. * Timestamp of certificate
  72. */
  73. COM_RESERVED_ID_TIMESTAMP = 0,
  74. /**
  75. * Network ID for which certificate was issued
  76. */
  77. COM_RESERVED_ID_NETWORK_ID = 1,
  78. /**
  79. * ZeroTier address to whom certificate was issued
  80. */
  81. COM_RESERVED_ID_ISSUED_TO = 2
  82. // IDs 3-6 reserved for full hash of identity to which this COM was issued.
  83. };
  84. /**
  85. * Create an empty certificate of membership
  86. */
  87. CertificateOfMembership() : _qualifierCount(0)
  88. {
  89. }
  90. /**
  91. * Create from required fields common to all networks
  92. *
  93. * @param timestamp Timestamp of certificate
  94. * @param timestampMaxDelta Maximum variation between timestamps on this net
  95. * @param nwid Network ID
  96. * @param issuedTo Certificate recipient
  97. */
  98. CertificateOfMembership(uint64_t timestamp, uint64_t timestampMaxDelta, uint64_t nwid, const Identity& issuedTo);
  99. /**
  100. * Create from binary-serialized COM in buffer
  101. *
  102. * @param b Buffer to deserialize from
  103. * @param startAt Position to start in buffer
  104. */
  105. template <unsigned int C> CertificateOfMembership(const Buffer<C>& b, unsigned int startAt = 0)
  106. {
  107. deserialize(b, startAt);
  108. }
  109. /**
  110. * @return True if there's something here
  111. */
  112. inline operator bool() const
  113. {
  114. return (_qualifierCount != 0);
  115. }
  116. /**
  117. * @return Credential ID, always 0 for COMs
  118. */
  119. inline uint32_t id() const
  120. {
  121. return 0;
  122. }
  123. /**
  124. * @return Timestamp for this cert and maximum delta for timestamp
  125. */
  126. inline int64_t timestamp() const
  127. {
  128. for (unsigned int i = 0; i < _qualifierCount; ++i) {
  129. if (_qualifiers[i].id == COM_RESERVED_ID_TIMESTAMP) {
  130. return _qualifiers[i].value;
  131. }
  132. }
  133. return 0;
  134. }
  135. /**
  136. * @return Address to which this cert was issued
  137. */
  138. inline Address issuedTo() const
  139. {
  140. for (unsigned int i = 0; i < _qualifierCount; ++i) {
  141. if (_qualifiers[i].id == COM_RESERVED_ID_ISSUED_TO) {
  142. return Address(_qualifiers[i].value);
  143. }
  144. }
  145. return Address();
  146. }
  147. /**
  148. * @return Network ID for which this cert was issued
  149. */
  150. inline uint64_t networkId() const
  151. {
  152. for (unsigned int i = 0; i < _qualifierCount; ++i) {
  153. if (_qualifiers[i].id == COM_RESERVED_ID_NETWORK_ID) {
  154. return _qualifiers[i].value;
  155. }
  156. }
  157. return 0ULL;
  158. }
  159. /**
  160. * Compare two certificates for parameter agreement
  161. *
  162. * This compares this certificate with the other and returns true if all
  163. * parameters in this cert are present in the other and if they agree to
  164. * within this cert's max delta value for each given parameter.
  165. *
  166. * Tuples present in other but not in this cert are ignored, but any
  167. * tuples present in this cert but not in other result in 'false'.
  168. *
  169. * @param other Cert to compare with
  170. * @param otherIdentity Identity of other node
  171. * @return True if certs agree and 'other' may be communicated with
  172. */
  173. bool agreesWith(const CertificateOfMembership& other, const Identity& otherIdentity) const;
  174. /**
  175. * Sign this certificate
  176. *
  177. * @param with Identity to sign with, must include private key
  178. * @return True if signature was successful
  179. */
  180. bool sign(const Identity& with);
  181. /**
  182. * Verify this COM and its signature
  183. *
  184. * @param RR Runtime environment for looking up peers
  185. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  186. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential
  187. */
  188. int verify(const RuntimeEnvironment* RR, void* tPtr) const;
  189. /**
  190. * @return True if signed
  191. */
  192. inline bool isSigned() const
  193. {
  194. return (_signedBy);
  195. }
  196. /**
  197. * @return Address that signed this certificate or null address if none
  198. */
  199. inline const Address& signedBy() const
  200. {
  201. return _signedBy;
  202. }
  203. template <unsigned int C> inline void serialize(Buffer<C>& b) const
  204. {
  205. b.append((uint8_t)1);
  206. b.append((uint16_t)_qualifierCount);
  207. for (unsigned int i = 0; i < _qualifierCount; ++i) {
  208. b.append(_qualifiers[i].id);
  209. b.append(_qualifiers[i].value);
  210. b.append(_qualifiers[i].maxDelta);
  211. }
  212. _signedBy.appendTo(b);
  213. if (_signedBy) {
  214. b.append(_signature.data, ZT_ECC_SIGNATURE_LEN);
  215. }
  216. }
  217. template <unsigned int C> inline unsigned int deserialize(const Buffer<C>& b, unsigned int startAt = 0)
  218. {
  219. unsigned int p = startAt;
  220. _qualifierCount = 0;
  221. _signedBy.zero();
  222. if (b[p++] != 1) {
  223. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
  224. }
  225. unsigned int numq = b.template at<uint16_t>(p);
  226. p += sizeof(uint16_t);
  227. uint64_t lastId = 0;
  228. for (unsigned int i = 0; i < numq; ++i) {
  229. const uint64_t qid = b.template at<uint64_t>(p);
  230. if (qid < lastId) {
  231. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_BAD_ENCODING;
  232. }
  233. else {
  234. lastId = qid;
  235. }
  236. if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
  237. _qualifiers[_qualifierCount].id = qid;
  238. _qualifiers[_qualifierCount].value = b.template at<uint64_t>(p + 8);
  239. _qualifiers[_qualifierCount].maxDelta = b.template at<uint64_t>(p + 16);
  240. p += 24;
  241. ++_qualifierCount;
  242. }
  243. else {
  244. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  245. }
  246. }
  247. _signedBy.setTo(b.field(p, ZT_ADDRESS_LENGTH), ZT_ADDRESS_LENGTH);
  248. p += ZT_ADDRESS_LENGTH;
  249. if (_signedBy) {
  250. memcpy(_signature.data, b.field(p, ZT_ECC_SIGNATURE_LEN), ZT_ECC_SIGNATURE_LEN);
  251. p += ZT_ECC_SIGNATURE_LEN;
  252. }
  253. return (p - startAt);
  254. }
  255. inline bool operator==(const CertificateOfMembership& c) const
  256. {
  257. if (_signedBy != c._signedBy) {
  258. return false;
  259. }
  260. if (_qualifierCount != c._qualifierCount) {
  261. return false;
  262. }
  263. for (unsigned int i = 0; i < _qualifierCount; ++i) {
  264. const _Qualifier& a = _qualifiers[i];
  265. const _Qualifier& b = c._qualifiers[i];
  266. if ((a.id != b.id) || (a.value != b.value) || (a.maxDelta != b.maxDelta)) {
  267. return false;
  268. }
  269. }
  270. return (memcmp(_signature.data, c._signature.data, ZT_ECC_SIGNATURE_LEN) == 0);
  271. }
  272. inline bool operator!=(const CertificateOfMembership& c) const
  273. {
  274. return (! (*this == c));
  275. }
  276. private:
  277. struct _Qualifier {
  278. _Qualifier() : id(0), value(0), maxDelta(0)
  279. {
  280. }
  281. uint64_t id;
  282. uint64_t value;
  283. uint64_t maxDelta;
  284. inline bool operator<(const _Qualifier& q) const
  285. {
  286. return (id < q.id);
  287. } // sort order
  288. };
  289. Address _signedBy;
  290. _Qualifier _qualifiers[ZT_NETWORK_COM_MAX_QUALIFIERS];
  291. unsigned int _qualifierCount;
  292. ECC::Signature _signature;
  293. };
  294. } // namespace ZeroTier
  295. #endif