2
0

CertificateOfOwnership.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_CERTIFICATEOFOWNERSHIP_HPP
  27. #define ZT_CERTIFICATEOFOWNERSHIP_HPP
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "Constants.hpp"
  33. #include "Credential.hpp"
  34. #include "C25519.hpp"
  35. #include "Address.hpp"
  36. #include "Identity.hpp"
  37. #include "Buffer.hpp"
  38. #include "InetAddress.hpp"
  39. #include "MAC.hpp"
  40. // Max things per CertificateOfOwnership
  41. #define ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS 16
  42. // Maximum size of a thing's value field in bytes
  43. #define ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE 16
  44. namespace ZeroTier {
  45. class RuntimeEnvironment;
  46. /**
  47. * Certificate indicating ownership of a network identifier
  48. */
  49. class CertificateOfOwnership : public Credential
  50. {
  51. friend class Credential;
  52. public:
  53. static inline Credential::Type credentialType() { return Credential::CREDENTIAL_TYPE_COO; }
  54. enum Thing
  55. {
  56. THING_NULL = 0,
  57. THING_MAC_ADDRESS = 1,
  58. THING_IPV4_ADDRESS = 2,
  59. THING_IPV6_ADDRESS = 3
  60. };
  61. inline CertificateOfOwnership()
  62. {
  63. memset(reinterpret_cast<void *>(this),0,sizeof(CertificateOfOwnership));
  64. }
  65. inline CertificateOfOwnership(const uint64_t nwid,const int64_t ts,const Address &issuedTo,const uint32_t id)
  66. {
  67. memset(reinterpret_cast<void *>(this),0,sizeof(CertificateOfOwnership));
  68. _networkId = nwid;
  69. _ts = ts;
  70. _id = id;
  71. _issuedTo = issuedTo;
  72. }
  73. inline uint64_t networkId() const { return _networkId; }
  74. inline int64_t timestamp() const { return _ts; }
  75. inline uint32_t id() const { return _id; }
  76. inline const Address &issuedTo() const { return _issuedTo; }
  77. inline const Address &signer() const { return _signedBy; }
  78. inline const uint8_t *signature() const { return _signature; }
  79. inline unsigned int signatureLength() const { return _signatureLength; }
  80. inline unsigned int thingCount() const { return (unsigned int)_thingCount; }
  81. inline Thing thingType(const unsigned int i) const { return (Thing)_thingTypes[i]; }
  82. inline const uint8_t *thingValue(const unsigned int i) const { return _thingValues[i]; }
  83. inline bool owns(const InetAddress &ip) const
  84. {
  85. if (ip.ss_family == AF_INET)
  86. return this->_owns(THING_IPV4_ADDRESS,&(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr),4);
  87. if (ip.ss_family == AF_INET6)
  88. return this->_owns(THING_IPV6_ADDRESS,reinterpret_cast<const struct sockaddr_in6 *>(&ip)->sin6_addr.s6_addr,16);
  89. return false;
  90. }
  91. inline bool owns(const MAC &mac) const
  92. {
  93. uint8_t tmp[6];
  94. mac.copyTo(tmp,6);
  95. return this->_owns(THING_MAC_ADDRESS,tmp,6);
  96. }
  97. inline void addThing(const InetAddress &ip)
  98. {
  99. if (_thingCount >= ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) return;
  100. if (ip.ss_family == AF_INET) {
  101. _thingTypes[_thingCount] = THING_IPV4_ADDRESS;
  102. memcpy(_thingValues[_thingCount],&(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr),4);
  103. ++_thingCount;
  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) return;
  113. _thingTypes[_thingCount] = THING_MAC_ADDRESS;
  114. mac.copyTo(_thingValues[_thingCount],6);
  115. ++_thingCount;
  116. }
  117. /**
  118. * @param signer Signing identity, must have private key
  119. * @return True if signature was successful
  120. */
  121. inline bool sign(const Identity &signer)
  122. {
  123. if (signer.hasPrivate()) {
  124. Buffer<sizeof(CertificateOfOwnership) + 64> tmp;
  125. _signedBy = signer.address();
  126. this->serialize(tmp,true);
  127. _signatureLength = signer.sign(tmp.data(),tmp.size(),_signature,sizeof(_signature));
  128. return true;
  129. }
  130. return false;
  131. }
  132. inline Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const { return _verify(RR,tPtr,*this); }
  133. template<unsigned int C>
  134. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  135. {
  136. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  137. b.append(_networkId);
  138. b.append(_ts);
  139. b.append(_flags);
  140. b.append(_id);
  141. b.append((uint16_t)_thingCount);
  142. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  143. b.append((uint8_t)_thingTypes[i]);
  144. b.append(_thingValues[i],ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  145. }
  146. _issuedTo.appendTo(b);
  147. _signedBy.appendTo(b);
  148. if (!forSign) {
  149. b.append((uint8_t)1);
  150. b.append((uint16_t)_signatureLength); // length of signature
  151. b.append(_signature,_signatureLength);
  152. }
  153. b.append((uint16_t)0); // length of additional fields, currently 0
  154. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  155. }
  156. template<unsigned int C>
  157. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  158. {
  159. unsigned int p = startAt;
  160. *this = CertificateOfOwnership();
  161. _networkId = b.template at<uint64_t>(p); p += 8;
  162. _ts = b.template at<uint64_t>(p); p += 8;
  163. _flags = b.template at<uint64_t>(p); p += 8;
  164. _id = b.template at<uint32_t>(p); p += 4;
  165. _thingCount = b.template at<uint16_t>(p); p += 2;
  166. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  167. if (i < ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) {
  168. _thingTypes[i] = (uint8_t)b[p++];
  169. memcpy(_thingValues[i],b.field(p,ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE),ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  170. p += ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE;
  171. }
  172. }
  173. _issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  174. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  175. if (b[p++] == 1) {
  176. _signatureLength = b.template at<uint16_t>(p);
  177. if (_signatureLength > sizeof(_signature))
  178. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN;
  179. p += 2;
  180. memcpy(_signature,b.field(p,_signatureLength),_signatureLength); p += _signatureLength;
  181. } else {
  182. p += 2 + b.template at<uint16_t>(p);
  183. }
  184. p += 2 + b.template at<uint16_t>(p);
  185. if (p > b.size())
  186. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  187. return (p - startAt);
  188. }
  189. // Provides natural sort order by ID
  190. inline bool operator<(const CertificateOfOwnership &coo) const { return (_id < coo._id); }
  191. inline bool operator==(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) == 0); }
  192. inline bool operator!=(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) != 0); }
  193. private:
  194. inline bool _owns(const Thing &t,const void *v,unsigned int l) const
  195. {
  196. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  197. if (_thingTypes[i] == (uint8_t)t) {
  198. unsigned int k = 0;
  199. while (k < l) {
  200. if (reinterpret_cast<const uint8_t *>(v)[k] != _thingValues[i][k])
  201. break;
  202. ++k;
  203. }
  204. if (k == l)
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. uint64_t _networkId;
  211. int64_t _ts;
  212. uint64_t _flags;
  213. uint32_t _id;
  214. uint16_t _thingCount;
  215. uint8_t _thingTypes[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS];
  216. uint8_t _thingValues[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS][ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE];
  217. Address _issuedTo;
  218. Address _signedBy;
  219. unsigned int _signatureLength;
  220. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  221. };
  222. } // namespace ZeroTier
  223. #endif