OwnershipCredential.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c)2013-2020 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 <cstdint>
  16. #include <cstdio>
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include "Constants.hpp"
  20. #include "Credential.hpp"
  21. #include "C25519.hpp"
  22. #include "Address.hpp"
  23. #include "Identity.hpp"
  24. #include "InetAddress.hpp"
  25. #include "MAC.hpp"
  26. // Max things per CertificateOfOwnership
  27. #define ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS 16
  28. // Maximum size of a thing's value field in bytes
  29. #define ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE 16
  30. #define ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX (8 + 8 + 8 + 4 + 2 + ((1 + ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE) * ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS) + 5 + 5 + 1 + 2 + ZT_SIGNATURE_BUFFER_SIZE + 2)
  31. namespace ZeroTier {
  32. class RuntimeEnvironment;
  33. /**
  34. * Certificate indicating ownership of a "thing" such as an IP address
  35. *
  36. * These are used in conjunction with the rules engine to make IP addresses and
  37. * other identifiers un-spoofable.
  38. */
  39. class OwnershipCredential : public Credential
  40. {
  41. friend class Credential;
  42. public:
  43. static constexpr ZT_CredentialType credentialType() noexcept { return ZT_CREDENTIAL_TYPE_COO; }
  44. enum Thing
  45. {
  46. THING_NULL = 0,
  47. THING_MAC_ADDRESS = 1,
  48. THING_IPV4_ADDRESS = 2,
  49. THING_IPV6_ADDRESS = 3
  50. };
  51. ZT_INLINE OwnershipCredential() noexcept { memoryZero(this); }
  52. ZT_INLINE OwnershipCredential(const uint64_t nwid, const int64_t ts, const Address &issuedTo, const uint32_t id) noexcept
  53. {
  54. memoryZero(this);
  55. m_networkId = nwid;
  56. m_ts = ts;
  57. m_id = id;
  58. m_issuedTo = issuedTo;
  59. }
  60. ZT_INLINE uint64_t networkId() const noexcept { return m_networkId; }
  61. ZT_INLINE int64_t timestamp() const noexcept { return m_ts; }
  62. ZT_INLINE uint32_t id() const noexcept { return m_id; }
  63. ZT_INLINE const Address &issuedTo() const noexcept { return m_issuedTo; }
  64. ZT_INLINE const Address &signer() const noexcept { return m_signedBy; }
  65. ZT_INLINE const uint8_t *signature() const noexcept { return m_signature; }
  66. ZT_INLINE unsigned int signatureLength() const noexcept { return m_signatureLength; }
  67. ZT_INLINE unsigned int thingCount() const noexcept { return (unsigned int)m_thingCount; }
  68. ZT_INLINE Thing thingType(const unsigned int i) const noexcept { return (Thing)m_thingTypes[i]; }
  69. ZT_INLINE const uint8_t *thingValue(const unsigned int i) const noexcept { return m_thingValues[i]; }
  70. ZT_INLINE bool owns(const InetAddress &ip) const noexcept
  71. {
  72. if (ip.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.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. ZT_INLINE bool owns(const MAC &mac) const noexcept
  79. {
  80. uint8_t tmp[6];
  81. mac.copyTo(tmp);
  82. return this->_owns(THING_MAC_ADDRESS,tmp,6);
  83. }
  84. /**
  85. * Add an IP address to this certificate
  86. *
  87. * @param ip IPv4 or IPv6 address
  88. */
  89. void addThing(const InetAddress &ip);
  90. /**
  91. * Add an Ethernet MAC address
  92. *
  93. * ZeroTier MAC addresses are always un-spoofable. This could in theory be
  94. * used to make bridged MAC addresses un-spoofable as well, but it's not
  95. * currently implemented.
  96. *
  97. * @param mac 48-bit MAC address
  98. */
  99. void addThing(const MAC &mac);
  100. /**
  101. * Sign this certificate
  102. *
  103. * @param signer Signing identity, must have private key
  104. * @return True if signature was successful
  105. */
  106. bool sign(const Identity &signer);
  107. /**
  108. * Verify certificate signature
  109. *
  110. * @param RR Runtime environment
  111. * @param tPtr That pointer we pass around
  112. * @return Credential verification result: OK, bad signature, or identity needed
  113. */
  114. ZT_INLINE Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const { return _verify(RR,tPtr,*this); }
  115. static constexpr int marshalSizeMax() noexcept { return ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX; }
  116. int marshal(uint8_t data[ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX],bool forSign = false) const noexcept;
  117. int unmarshal(const uint8_t *data,int len) noexcept;
  118. // Provides natural sort order by ID
  119. ZT_INLINE bool operator<(const OwnershipCredential &coo) const noexcept { return (m_id < coo.m_id); }
  120. ZT_INLINE bool operator==(const OwnershipCredential &coo) const noexcept { return (memcmp(this, &coo, sizeof(OwnershipCredential)) == 0); }
  121. ZT_INLINE bool operator!=(const OwnershipCredential &coo) const noexcept { return (memcmp(this, &coo, sizeof(OwnershipCredential)) != 0); }
  122. private:
  123. ZT_INLINE bool _owns(const Thing &t,const void *v,unsigned int l) const noexcept
  124. {
  125. for(unsigned int i=0,j=m_thingCount;i < j;++i) {
  126. if (m_thingTypes[i] == (uint8_t)t) {
  127. unsigned int k = 0;
  128. while (k < l) {
  129. if (reinterpret_cast<const uint8_t *>(v)[k] != m_thingValues[i][k])
  130. break;
  131. ++k;
  132. }
  133. if (k == l)
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. uint64_t m_networkId;
  140. int64_t m_ts;
  141. uint64_t m_flags;
  142. uint32_t m_id;
  143. uint16_t m_thingCount;
  144. uint8_t m_thingTypes[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS];
  145. uint8_t m_thingValues[ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS][ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE];
  146. Address m_issuedTo;
  147. Address m_signedBy;
  148. unsigned int m_signatureLength;
  149. uint8_t m_signature[ZT_SIGNATURE_BUFFER_SIZE];
  150. };
  151. } // namespace ZeroTier
  152. #endif