CertificateOfOwnership.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_CERTIFICATEOFOWNERSHIP_HPP
  19. #define ZT_CERTIFICATEOFOWNERSHIP_HPP
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "Constants.hpp"
  25. #include "C25519.hpp"
  26. #include "Address.hpp"
  27. #include "Identity.hpp"
  28. #include "Buffer.hpp"
  29. #include "InetAddress.hpp"
  30. #include "MAC.hpp"
  31. // Max things per CertificateOfOwnership
  32. #define ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS 16
  33. // Maximum size of a thing's value field in bytes
  34. #define ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE 16
  35. namespace ZeroTier {
  36. class RuntimeEnvironment;
  37. /**
  38. * Certificate indicating ownership of a network identifier
  39. */
  40. class CertificateOfOwnership
  41. {
  42. public:
  43. enum Thing
  44. {
  45. THING_NULL = 0,
  46. THING_MAC_ADDRESS = 1,
  47. THING_IPV4_ADDRESS = 2,
  48. THING_IPV6_ADDRESS = 3
  49. };
  50. CertificateOfOwnership()
  51. {
  52. memset(this,0,sizeof(CertificateOfOwnership));
  53. }
  54. CertificateOfOwnership(const uint64_t nwid,const uint64_t ts,const Address &issuedTo,const uint32_t id) :
  55. _networkId(nwid),
  56. _ts(ts),
  57. _flags(0),
  58. _id(id),
  59. _thingCount(0),
  60. _issuedTo(issuedTo)
  61. {
  62. }
  63. inline uint64_t networkId() const { return _networkId; }
  64. inline uint64_t timestamp() const { return _ts; }
  65. inline uint32_t id() const { return _id; }
  66. inline unsigned int thingCount() const { return (unsigned int)_thingCount; }
  67. inline Thing thingType(const unsigned int i) const { return (Thing)_thingTypes[i]; }
  68. inline const uint8_t *thingValue(const unsigned int i) const { return _thingValues[i]; }
  69. inline const Address &issuedTo() const { return _issuedTo; }
  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. _signature = signer.sign(tmp.data(),tmp.size());
  115. return true;
  116. }
  117. return false;
  118. }
  119. /**
  120. * @param RR Runtime environment to allow identity lookup for signedBy
  121. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature
  122. */
  123. int verify(const RuntimeEnvironment *RR) const;
  124. template<unsigned int C>
  125. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  126. {
  127. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  128. b.append(_networkId);
  129. b.append(_ts);
  130. b.append(_flags);
  131. b.append(_id);
  132. b.append((uint16_t)_thingCount);
  133. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  134. b.append((uint8_t)_thingTypes[i]);
  135. b.append(_thingValues[i],ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  136. }
  137. _issuedTo.appendTo(b);
  138. _signedBy.appendTo(b);
  139. if (!forSign) {
  140. b.append((uint8_t)1); // 1 == Ed25519
  141. b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); // length of signature
  142. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  143. }
  144. b.append((uint16_t)0); // length of additional fields, currently 0
  145. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  146. }
  147. template<unsigned int C>
  148. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  149. {
  150. unsigned int p = startAt;
  151. memset(this,0,sizeof(CertificateOfOwnership));
  152. _networkId = b.template at<uint64_t>(p); p += 8;
  153. _ts = b.template at<uint64_t>(p); p += 8;
  154. _flags = b.template at<uint64_t>(p); p += 8;
  155. _id = b.template at<uint32_t>(p); p += 4;
  156. _thingCount = b.template at<uint16_t>(p); p += 2;
  157. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  158. if (i < ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) {
  159. _thingTypes[i] = (uint8_t)b[p++];
  160. memcpy(_thingValues[i],b.field(p,ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE),ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE);
  161. p += ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE;
  162. }
  163. }
  164. _issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  165. _signedBy.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
  166. if (b[p++] == 1) {
  167. if (b.template at<uint16_t>(p) != ZT_C25519_SIGNATURE_LEN)
  168. throw std::runtime_error("invalid signature length");
  169. p += 2;
  170. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  171. } else {
  172. p += 2 + b.template at<uint16_t>(p);
  173. }
  174. p += 2 + b.template at<uint16_t>(p);
  175. if (p > b.size())
  176. throw std::runtime_error("extended field overflow");
  177. return (p - startAt);
  178. }
  179. // Provides natural sort order by ID
  180. inline bool operator<(const CertificateOfOwnership &coo) const { return (_id < coo._id); }
  181. inline bool operator==(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) == 0); }
  182. inline bool operator!=(const CertificateOfOwnership &coo) const { return (memcmp(this,&coo,sizeof(CertificateOfOwnership)) != 0); }
  183. private:
  184. bool _owns(const Thing &t,const void *v,unsigned int l) const;
  185. uint64_t _networkId;
  186. uint64_t _ts;
  187. uint64_t _flags;
  188. uint32_t _id;
  189. uint16_t _thingCount;
  190. uint8_t _thingTypes[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS];
  191. uint8_t _thingValues[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS][ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE];
  192. Address _issuedTo;
  193. Address _signedBy;
  194. C25519::Signature _signature;
  195. };
  196. } // namespace ZeroTier
  197. #endif