OwnershipCredential.hpp 5.5 KB

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