CertificateOfOwnership.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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: 2023-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. friend class Credential;
  39. public:
  40. static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_COO; }
  41. enum Thing
  42. {
  43. THING_NULL = 0,
  44. THING_MAC_ADDRESS = 1,
  45. THING_IPV4_ADDRESS = 2,
  46. THING_IPV6_ADDRESS = 3
  47. };
  48. inline CertificateOfOwnership()
  49. {
  50. memset(reinterpret_cast<void *>(this),0,sizeof(CertificateOfOwnership));
  51. }
  52. inline CertificateOfOwnership(const uint64_t nwid,const int64_t ts,const Address &issuedTo,const uint32_t id)
  53. {
  54. memset(reinterpret_cast<void *>(this),0,sizeof(CertificateOfOwnership));
  55. _networkId = nwid;
  56. _ts = ts;
  57. _id = id;
  58. _issuedTo = issuedTo;
  59. }
  60. ZT_ALWAYS_INLINE uint64_t networkId() const { return _networkId; }
  61. ZT_ALWAYS_INLINE int64_t timestamp() const { return _ts; }
  62. ZT_ALWAYS_INLINE uint32_t id() const { return _id; }
  63. ZT_ALWAYS_INLINE const Address &issuedTo() const { return _issuedTo; }
  64. ZT_ALWAYS_INLINE const Address &signer() const { return _signedBy; }
  65. ZT_ALWAYS_INLINE const uint8_t *signature() const { return _signature; }
  66. ZT_ALWAYS_INLINE unsigned int signatureLength() const { return _signatureLength; }
  67. ZT_ALWAYS_INLINE unsigned int thingCount() const { return (unsigned int)_thingCount; }
  68. ZT_ALWAYS_INLINE Thing thingType(const unsigned int i) const { return (Thing)_thingTypes[i]; }
  69. ZT_ALWAYS_INLINE const uint8_t *thingValue(const unsigned int i) const { return _thingValues[i]; }
  70. inline bool owns(const InetAddress &ip) const
  71. {
  72. if (ip.ss_family == AF_INET)
  73. return this->_owns(THING_IPV4_ADDRESS,&(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr),4);
  74. if (ip.ss_family == AF_INET6)
  75. return this->_owns(THING_IPV6_ADDRESS,reinterpret_cast<const struct sockaddr_in6 *>(&ip)->sin6_addr.s6_addr,16);
  76. return false;
  77. }
  78. inline bool owns(const MAC &mac) const
  79. {
  80. uint8_t tmp[6];
  81. mac.copyTo(tmp,6);
  82. return this->_owns(THING_MAC_ADDRESS,tmp,6);
  83. }
  84. inline void addThing(const InetAddress &ip)
  85. {
  86. if (_thingCount >= ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) return;
  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) return;
  100. _thingTypes[_thingCount] = THING_MAC_ADDRESS;
  101. mac.copyTo(_thingValues[_thingCount],6);
  102. ++_thingCount;
  103. }
  104. /**
  105. * @param signer Signing identity, must have private key
  106. * @return True if signature was successful
  107. */
  108. inline bool sign(const Identity &signer)
  109. {
  110. if (signer.hasPrivate()) {
  111. Buffer<sizeof(CertificateOfOwnership) + 64> tmp;
  112. _signedBy = signer.address();
  113. this->serialize(tmp,true);
  114. _signatureLength = signer.sign(tmp.data(),tmp.size(),_signature,sizeof(_signature));
  115. return true;
  116. }
  117. return false;
  118. }
  119. ZT_ALWAYS_INLINE Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const { return _verify(RR,tPtr,*this); }
  120. template<unsigned int C>
  121. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  122. {
  123. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  124. b.append(_networkId);
  125. b.append(_ts);
  126. b.append(_flags);
  127. b.append(_id);
  128. b.append((uint16_t)_thingCount);
  129. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  130. b.append((uint8_t)_thingTypes[i]);
  131. b.append(_thingValues[i],ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  132. }
  133. _issuedTo.appendTo(b);
  134. _signedBy.appendTo(b);
  135. if (!forSign) {
  136. b.append((uint8_t)1);
  137. b.append((uint16_t)_signatureLength); // length of signature
  138. b.append(_signature,_signatureLength);
  139. }
  140. b.append((uint16_t)0); // length of additional fields, currently 0
  141. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  142. }
  143. template<unsigned int C>
  144. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  145. {
  146. unsigned int p = startAt;
  147. *this = CertificateOfOwnership();
  148. _networkId = b.template at<uint64_t>(p); p += 8;
  149. _ts = b.template at<uint64_t>(p); p += 8;
  150. _flags = b.template at<uint64_t>(p); p += 8;
  151. _id = b.template at<uint32_t>(p); p += 4;
  152. _thingCount = b.template at<uint16_t>(p); p += 2;
  153. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  154. if (i < ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) {
  155. _thingTypes[i] = (uint8_t)b[p++];
  156. memcpy(_thingValues[i],b.field(p,ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE),ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  157. p += ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE;
  158. }
  159. }
  160. _issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  161. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  162. if (b[p++] == 1) {
  163. _signatureLength = b.template at<uint16_t>(p);
  164. if (_signatureLength > sizeof(_signature))
  165. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  166. p += 2;
  167. memcpy(_signature,b.field(p,_signatureLength),_signatureLength); p += _signatureLength;
  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. ZT_ALWAYS_INLINE bool operator<(const CertificateOfOwnership &coo) const { return (_id < coo._id); }
  178. ZT_ALWAYS_INLINE bool operator==(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) == 0); }
  179. ZT_ALWAYS_INLINE bool operator!=(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) != 0); }
  180. private:
  181. inline bool _owns(const Thing &t,const void *v,unsigned int l) const
  182. {
  183. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  184. if (_thingTypes[i] == (uint8_t)t) {
  185. unsigned int k = 0;
  186. while (k < l) {
  187. if (reinterpret_cast<const uint8_t *>(v)[k] != _thingValues[i][k])
  188. break;
  189. ++k;
  190. }
  191. if (k == l)
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. uint64_t _networkId;
  198. int64_t _ts;
  199. uint64_t _flags;
  200. uint32_t _id;
  201. uint16_t _thingCount;
  202. uint8_t _thingTypes[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS];
  203. uint8_t _thingValues[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS][ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE];
  204. Address _issuedTo;
  205. Address _signedBy;
  206. unsigned int _signatureLength;
  207. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  208. };
  209. } // namespace ZeroTier
  210. #endif