2
0

CertificateOfOwnership.hpp 7.1 KB

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