CertificateOfMembership.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c)2013-2020 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: 2024-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 <cstdint>
  16. #include <cstring>
  17. #include <string>
  18. #include <stdexcept>
  19. #include <algorithm>
  20. #include "Constants.hpp"
  21. #include "Credential.hpp"
  22. #include "Address.hpp"
  23. #include "C25519.hpp"
  24. #include "Identity.hpp"
  25. #include "Utils.hpp"
  26. /**
  27. * Maximum number of qualifiers allowed in a COM (absolute max: 65535)
  28. */
  29. #define ZT_NETWORK_COM_MAX_QUALIFIERS 8
  30. #define ZT_CERTIFICATEOFMEMBERSHIP_MARSHAL_SIZE_MAX (1 + 2 + (24 * ZT_NETWORK_COM_MAX_QUALIFIERS) + 5 + ZT_SIGNATURE_BUFFER_SIZE)
  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. friend class Credential;
  62. public:
  63. static ZT_ALWAYS_INLINE ZT_CredentialType credentialType() { return ZT_CREDENTIAL_TYPE_COM; }
  64. /**
  65. * Reserved qualifier IDs
  66. *
  67. * IDs below 1024 are reserved for use as standard IDs. Others are available
  68. * for user-defined use.
  69. *
  70. * Addition of new required fields requires that code in hasRequiredFields
  71. * be updated as well.
  72. */
  73. enum ReservedId
  74. {
  75. /**
  76. * Timestamp of certificate
  77. */
  78. COM_RESERVED_ID_TIMESTAMP = 0,
  79. /**
  80. * Network ID for which certificate was issued
  81. */
  82. COM_RESERVED_ID_NETWORK_ID = 1,
  83. /**
  84. * ZeroTier address to whom certificate was issued
  85. */
  86. COM_RESERVED_ID_ISSUED_TO = 2
  87. };
  88. /**
  89. * Create an empty certificate of membership
  90. */
  91. ZT_ALWAYS_INLINE CertificateOfMembership() :
  92. _qualifierCount(0),
  93. _signatureLength(0) {}
  94. /**
  95. * Create from required fields common to all networks
  96. *
  97. * @param timestamp Timestamp of certificate
  98. * @param timestampMaxDelta Maximum variation between timestamps on this net
  99. * @param nwid Network ID
  100. * @param issuedTo Certificate recipient
  101. */
  102. CertificateOfMembership(uint64_t timestamp,uint64_t timestampMaxDelta,uint64_t nwid,const Address &issuedTo);
  103. /**
  104. * @return True if there's something here
  105. */
  106. ZT_ALWAYS_INLINE operator bool() const { return (_qualifierCount != 0); }
  107. /**
  108. * @return Credential ID, always 0 for COMs
  109. */
  110. ZT_ALWAYS_INLINE uint32_t id() const { return 0; }
  111. /**
  112. * @return Timestamp for this cert and maximum delta for timestamp
  113. */
  114. ZT_ALWAYS_INLINE int64_t timestamp() const
  115. {
  116. for(unsigned int i=0;i<_qualifierCount;++i) {
  117. if (_qualifiers[i].id == COM_RESERVED_ID_TIMESTAMP)
  118. return (int64_t)_qualifiers[i].value;
  119. }
  120. return 0;
  121. }
  122. /**
  123. * @return Address to which this cert was issued
  124. */
  125. ZT_ALWAYS_INLINE Address issuedTo() const
  126. {
  127. for(unsigned int i=0;i<_qualifierCount;++i) {
  128. if (_qualifiers[i].id == COM_RESERVED_ID_ISSUED_TO)
  129. return Address(_qualifiers[i].value);
  130. }
  131. return Address();
  132. }
  133. /**
  134. * @return Network ID for which this cert was issued
  135. */
  136. ZT_ALWAYS_INLINE uint64_t networkId() const
  137. {
  138. for(unsigned int i=0;i<_qualifierCount;++i) {
  139. if (_qualifiers[i].id == COM_RESERVED_ID_NETWORK_ID)
  140. return _qualifiers[i].value;
  141. }
  142. return 0ULL;
  143. }
  144. /**
  145. * Add or update a qualifier in this certificate
  146. *
  147. * Any signature is invalidated and signedBy is set to null.
  148. *
  149. * @param id Qualifier ID
  150. * @param value Qualifier value
  151. * @param maxDelta Qualifier maximum allowed difference (absolute value of difference)
  152. */
  153. void setQualifier(uint64_t id,uint64_t value,uint64_t maxDelta);
  154. ZT_ALWAYS_INLINE void setQualifier(ReservedId id,uint64_t value,uint64_t maxDelta) { setQualifier((uint64_t)id,value,maxDelta); }
  155. /**
  156. * Compare two certificates for parameter agreement
  157. *
  158. * This compares this certificate with the other and returns true if all
  159. * parameters in this cert are present in the other and if they agree to
  160. * within this cert's max delta value for each given parameter.
  161. *
  162. * Tuples present in other but not in this cert are ignored, but any
  163. * tuples present in this cert but not in other result in 'false'.
  164. *
  165. * @param other Cert to compare with
  166. * @return True if certs agree and 'other' may be communicated with
  167. */
  168. ZT_ALWAYS_INLINE bool agreesWith(const CertificateOfMembership &other) const
  169. {
  170. unsigned int myidx = 0;
  171. unsigned int otheridx = 0;
  172. if ((_qualifierCount == 0)||(other._qualifierCount == 0))
  173. return false;
  174. while (myidx < _qualifierCount) {
  175. // Fail if we're at the end of other, since this means the field is
  176. // missing.
  177. if (otheridx >= other._qualifierCount)
  178. return false;
  179. // Seek to corresponding tuple in other, ignoring tuples that
  180. // we may not have. If we run off the end of other, the tuple is
  181. // missing. This works because tuples are sorted by ID.
  182. while (other._qualifiers[otheridx].id != _qualifiers[myidx].id) {
  183. ++otheridx;
  184. if (otheridx >= other._qualifierCount)
  185. return false;
  186. }
  187. // Compare to determine if the absolute value of the difference
  188. // between these two parameters is within our maxDelta.
  189. const uint64_t a = _qualifiers[myidx].value;
  190. const uint64_t b = other._qualifiers[myidx].value;
  191. if (((a >= b) ? (a - b) : (b - a)) > _qualifiers[myidx].maxDelta)
  192. return false;
  193. ++myidx;
  194. }
  195. return true;
  196. }
  197. /**
  198. * Sign this certificate
  199. *
  200. * @param with Identity to sign with, must include private key
  201. * @return True if signature was successful
  202. */
  203. bool sign(const Identity &with);
  204. /**
  205. * Verify this COM and its signature
  206. *
  207. * @param RR Runtime environment for looking up peers
  208. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  209. */
  210. ZT_ALWAYS_INLINE Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const { return _verify(RR,tPtr,*this); }
  211. /**
  212. * @return Address that signed this certificate or null address if none
  213. */
  214. ZT_ALWAYS_INLINE const Address &signedBy() const { return _signedBy; }
  215. static ZT_ALWAYS_INLINE int marshalSizeMax() { return ZT_CERTIFICATEOFMEMBERSHIP_MARSHAL_SIZE_MAX; }
  216. int marshal(uint8_t data[ZT_CERTIFICATEOFMEMBERSHIP_MARSHAL_SIZE_MAX]) const;
  217. int unmarshal(const uint8_t *data,int len);
  218. bool operator==(const CertificateOfMembership &c) const;
  219. ZT_ALWAYS_INLINE bool operator!=(const CertificateOfMembership &c) const { return (!(*this == c)); }
  220. private:
  221. struct _Qualifier
  222. {
  223. ZT_ALWAYS_INLINE _Qualifier() : id(0),value(0),maxDelta(0) {}
  224. uint64_t id;
  225. uint64_t value;
  226. uint64_t maxDelta;
  227. ZT_ALWAYS_INLINE bool operator<(const _Qualifier &q) const { return (id < q.id); } // sort order
  228. };
  229. Address _signedBy;
  230. _Qualifier _qualifiers[ZT_NETWORK_COM_MAX_QUALIFIERS];
  231. unsigned int _qualifierCount;
  232. unsigned int _signatureLength;
  233. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  234. };
  235. } // namespace ZeroTier
  236. #endif