CertificateOfMembership.hpp 8.5 KB

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