CertificateOfOwnership.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. if (ip.ss_family == AF_INET6)
  71. return this->_owns(THING_IPV6_ADDRESS,reinterpret_cast<const struct sockaddr_in6 *>(&ip)->sin6_addr.s6_addr,16);
  72. return false;
  73. }
  74. inline bool owns(const MAC &mac) const
  75. {
  76. uint8_t tmp[6];
  77. mac.copyTo(tmp,6);
  78. return this->_owns(THING_MAC_ADDRESS,tmp,6);
  79. }
  80. inline void addThing(const InetAddress &ip)
  81. {
  82. if (_thingCount >= ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) return;
  83. if (ip.ss_family == AF_INET) {
  84. _thingTypes[_thingCount] = THING_IPV4_ADDRESS;
  85. memcpy(_thingValues[_thingCount],&(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr),4);
  86. ++_thingCount;
  87. } else if (ip.ss_family == AF_INET6) {
  88. _thingTypes[_thingCount] = THING_IPV6_ADDRESS;
  89. memcpy(_thingValues[_thingCount],reinterpret_cast<const struct sockaddr_in6 *>(&ip)->sin6_addr.s6_addr,16);
  90. ++_thingCount;
  91. }
  92. }
  93. inline void addThing(const MAC &mac)
  94. {
  95. if (_thingCount >= ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) return;
  96. _thingTypes[_thingCount] = THING_MAC_ADDRESS;
  97. mac.copyTo(_thingValues[_thingCount],6);
  98. ++_thingCount;
  99. }
  100. /**
  101. * @param signer Signing identity, must have private key
  102. * @return True if signature was successful
  103. */
  104. inline bool sign(const Identity &signer)
  105. {
  106. if (signer.hasPrivate()) {
  107. Buffer<sizeof(CertificateOfOwnership) + 64> tmp;
  108. _signedBy = signer.address();
  109. this->serialize(tmp,true);
  110. _signature = signer.sign(tmp.data(),tmp.size());
  111. return true;
  112. }
  113. return false;
  114. }
  115. /**
  116. * @param RR Runtime environment to allow identity lookup for signedBy
  117. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  118. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature
  119. */
  120. int verify(const RuntimeEnvironment *RR,void *tPtr) const;
  121. template<unsigned int C>
  122. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  123. {
  124. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  125. b.append(_networkId);
  126. b.append(_ts);
  127. b.append(_flags);
  128. b.append(_id);
  129. b.append((uint16_t)_thingCount);
  130. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  131. b.append((uint8_t)_thingTypes[i]);
  132. b.append(_thingValues[i],ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  133. }
  134. _issuedTo.appendTo(b);
  135. _signedBy.appendTo(b);
  136. if (!forSign) {
  137. b.append((uint8_t)1); // 1 == Ed25519
  138. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
  139. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  140. }
  141. b.append((uint16_t)0); // length of additional fields, currently 0
  142. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  143. }
  144. template<unsigned int C>
  145. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  146. {
  147. unsigned int p = startAt;
  148. *this = CertificateOfOwnership();
  149. _networkId = b.template at<uint64_t>(p); p += 8;
  150. _ts = b.template at<uint64_t>(p); p += 8;
  151. _flags = b.template at<uint64_t>(p); p += 8;
  152. _id = b.template at<uint32_t>(p); p += 4;
  153. _thingCount = b.template at<uint16_t>(p); p += 2;
  154. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  155. if (i < ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) {
  156. _thingTypes[i] = (uint8_t)b[p++];
  157. memcpy(_thingValues[i],b.field(p,ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE),ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  158. p += ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE;
  159. }
  160. }
  161. _issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  162. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  163. if (b[p++] == 1) {
  164. if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN)
  165. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  166. p += 2;
  167. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  168. } else {
  169. p += 2 + b.template at<uint16_t>(p);
  170. }
  171. p += 2 + b.template at<uint16_t>(p);
  172. if (p > b.size())
  173. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  174. return (p - startAt);
  175. }
  176. // Provides natural sort order by ID
  177. inline bool operator<(const CertificateOfOwnership &coo) const { return (_id < coo._id); }
  178. inline bool operator==(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) == 0); }
  179. inline bool operator!=(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) != 0); }
  180. private:
  181. bool _owns(const Thing &t,const void *v,unsigned int l) const;
  182. uint64_t _networkId;
  183. int64_t _ts;
  184. uint64_t _flags;
  185. uint32_t _id;
  186. uint16_t _thingCount;
  187. uint8_t _thingTypes[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS];
  188. uint8_t _thingValues[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS][ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE];
  189. Address _issuedTo;
  190. Address _signedBy;
  191. C25519::Signature _signature;
  192. };
  193. } // namespace ZeroTier
  194. #endif