CertificateOfOwnership.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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: 2025-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_CERTIFICATEOFOWNERSHIP_HPP
  14. #define ZT_CERTIFICATEOFOWNERSHIP_HPP
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "Constants.hpp"
  20. #include "Credential.hpp"
  21. #include "C25519.hpp"
  22. #include "Address.hpp"
  23. #include "Identity.hpp"
  24. #include "Buffer.hpp"
  25. #include "InetAddress.hpp"
  26. #include "MAC.hpp"
  27. // Max things per CertificateOfOwnership
  28. #define ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS 16
  29. // Maximum size of a thing's value field in bytes
  30. #define ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE 16
  31. namespace ZeroTier {
  32. class RuntimeEnvironment;
  33. /**
  34. * Certificate indicating ownership of a network identifier
  35. */
  36. class CertificateOfOwnership : public Credential
  37. {
  38. public:
  39. static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_COO; }
  40. enum Thing
  41. {
  42. THING_NULL = 0,
  43. THING_MAC_ADDRESS = 1,
  44. THING_IPV4_ADDRESS = 2,
  45. THING_IPV6_ADDRESS = 3
  46. };
  47. CertificateOfOwnership()
  48. {
  49. memset(reinterpret_cast<void *>(this),0,sizeof(CertificateOfOwnership));
  50. }
  51. CertificateOfOwnership(const uint64_t nwid,const int64_t ts,const Address &issuedTo,const uint32_t id)
  52. {
  53. memset(reinterpret_cast<void *>(this),0,sizeof(CertificateOfOwnership));
  54. _networkId = nwid;
  55. _ts = ts;
  56. _id = id;
  57. _issuedTo = issuedTo;
  58. }
  59. inline uint64_t networkId() const { return _networkId; }
  60. inline int64_t timestamp() const { return _ts; }
  61. inline uint32_t id() const { return _id; }
  62. inline unsigned int thingCount() const { return (unsigned int)_thingCount; }
  63. inline Thing thingType(const unsigned int i) const { return (Thing)_thingTypes[i]; }
  64. inline const uint8_t *thingValue(const unsigned int i) const { return _thingValues[i]; }
  65. inline const Address &issuedTo() const { return _issuedTo; }
  66. inline bool owns(const InetAddress &ip) const
  67. {
  68. if (ip.ss_family == AF_INET) {
  69. return this->_owns(THING_IPV4_ADDRESS,&(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr),4);
  70. }
  71. if (ip.ss_family == AF_INET6) {
  72. return this->_owns(THING_IPV6_ADDRESS,reinterpret_cast<const struct sockaddr_in6 *>(&ip)->sin6_addr.s6_addr,16);
  73. }
  74. return false;
  75. }
  76. inline bool owns(const MAC &mac) const
  77. {
  78. uint8_t tmp[6];
  79. mac.copyTo(tmp,6);
  80. return this->_owns(THING_MAC_ADDRESS,tmp,6);
  81. }
  82. inline void addThing(const InetAddress &ip)
  83. {
  84. if (_thingCount >= ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) {
  85. return;
  86. }
  87. if (ip.ss_family == AF_INET) {
  88. _thingTypes[_thingCount] = THING_IPV4_ADDRESS;
  89. memcpy(_thingValues[_thingCount],&(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr),4);
  90. ++_thingCount;
  91. } else if (ip.ss_family == AF_INET6) {
  92. _thingTypes[_thingCount] = THING_IPV6_ADDRESS;
  93. memcpy(_thingValues[_thingCount],reinterpret_cast<const struct sockaddr_in6 *>(&ip)->sin6_addr.s6_addr,16);
  94. ++_thingCount;
  95. }
  96. }
  97. inline void addThing(const MAC &mac)
  98. {
  99. if (_thingCount >= ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) {
  100. return;
  101. }
  102. _thingTypes[_thingCount] = THING_MAC_ADDRESS;
  103. mac.copyTo(_thingValues[_thingCount],6);
  104. ++_thingCount;
  105. }
  106. /**
  107. * @param signer Signing identity, must have private key
  108. * @return True if signature was successful
  109. */
  110. inline bool sign(const Identity &signer)
  111. {
  112. if (signer.hasPrivate()) {
  113. Buffer<sizeof(CertificateOfOwnership) + 64> tmp;
  114. _signedBy = signer.address();
  115. this->serialize(tmp,true);
  116. _signature = signer.sign(tmp.data(),tmp.size());
  117. return true;
  118. }
  119. return false;
  120. }
  121. /**
  122. * @param RR Runtime environment to allow identity lookup for signedBy
  123. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  124. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature
  125. */
  126. int verify(const RuntimeEnvironment *RR,void *tPtr) const;
  127. template<unsigned int C>
  128. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  129. {
  130. if (forSign) {
  131. b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  132. }
  133. b.append(_networkId);
  134. b.append(_ts);
  135. b.append(_flags);
  136. b.append(_id);
  137. b.append((uint16_t)_thingCount);
  138. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  139. b.append((uint8_t)_thingTypes[i]);
  140. b.append(_thingValues[i],ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  141. }
  142. _issuedTo.appendTo(b);
  143. _signedBy.appendTo(b);
  144. if (!forSign) {
  145. b.append((uint8_t)1); // 1 == Ed25519
  146. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
  147. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  148. }
  149. b.append((uint16_t)0); // length of additional fields, currently 0
  150. if (forSign) {
  151. b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  152. }
  153. }
  154. template<unsigned int C>
  155. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  156. {
  157. unsigned int p = startAt;
  158. *this = CertificateOfOwnership();
  159. _networkId = b.template at<uint64_t>(p);
  160. p += 8;
  161. _ts = b.template at<uint64_t>(p);
  162. p += 8;
  163. _flags = b.template at<uint64_t>(p);
  164. p += 8;
  165. _id = b.template at<uint32_t>(p);
  166. p += 4;
  167. _thingCount = b.template at<uint16_t>(p);
  168. p += 2;
  169. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  170. if (i < ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) {
  171. _thingTypes[i] = (uint8_t)b[p++];
  172. memcpy(_thingValues[i],b.field(p,ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE),ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  173. p += ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE;
  174. }
  175. }
  176. _issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
  177. p += ZT_ADDRESS_LENGTH;
  178. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
  179. p += ZT_ADDRESS_LENGTH;
  180. if (b[p++] == 1) {
  181. if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN) {
  182. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  183. }
  184. p += 2;
  185. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN);
  186. p += ZT_C25519_SIGNATURE_LEN;
  187. } else {
  188. p += 2 + b.template at<uint16_t>(p);
  189. }
  190. p += 2 + b.template at<uint16_t>(p);
  191. if (p > b.size()) {
  192. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  193. }
  194. return (p - startAt);
  195. }
  196. // Provides natural sort order by ID
  197. inline bool operator<(const CertificateOfOwnership &coo) const { return (_id < coo._id); }
  198. inline bool operator==(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) == 0); }
  199. inline bool operator!=(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) != 0); }
  200. private:
  201. bool _owns(const Thing &t,const void *v,unsigned int l) const;
  202. uint64_t _networkId;
  203. int64_t _ts;
  204. uint64_t _flags;
  205. uint32_t _id;
  206. uint16_t _thingCount;
  207. uint8_t _thingTypes[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS];
  208. uint8_t _thingValues[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS][ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE];
  209. Address _issuedTo;
  210. Address _signedBy;
  211. C25519::Signature _signature;
  212. };
  213. } // namespace ZeroTier
  214. #endif