IdentificationCertificate.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: 2024-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_IDENTIFICATIONCERTIFICATE_HPP
  14. #define ZT_IDENTIFICATIONCERTIFICATE_HPP
  15. #include "Constants.hpp"
  16. #include "SHA512.hpp"
  17. #include "C25519.hpp"
  18. #include "ECC384.hpp"
  19. #include "SharedPtr.hpp"
  20. #include "Identity.hpp"
  21. #include "Locator.hpp"
  22. #include "Dictionary.hpp"
  23. #include "Utils.hpp"
  24. #include "Containers.hpp"
  25. namespace ZeroTier {
  26. /**
  27. * Certificate identifying the real world owner of an identity or network.
  28. *
  29. * This is a wrapper around the straight C ZT_IdentificationCertificate and
  30. * handles allocating memory for objects added via addXXX() and disposing of
  31. * them on delete. If pointers in the underlying C struct are set manually,
  32. * their memory is not freed on delete. Use the addXXX() methods to fill
  33. * out this structure in C++ code.
  34. *
  35. * The serialNo field is filled in automatically by sign() and decode(), so
  36. * it can be left undefined when building certificates. It contains a SHA384
  37. * hash of the certificate marshalled without the signature field.
  38. *
  39. * The hashCode() method and comparison operators compare the serial number
  40. * field, so these will not work correctly before sign() or decode() is
  41. * called.
  42. */
  43. class IdentificationCertificate : public ZT_IdentificationCertificate
  44. {
  45. public:
  46. ZT_INLINE IdentificationCertificate() noexcept
  47. { this->clear(); }
  48. ZT_INLINE IdentificationCertificate(const ZT_IdentificationCertificate &apiCert)
  49. { *this = apiCert; }
  50. ZT_INLINE IdentificationCertificate(const IdentificationCertificate &cert)
  51. { *this = cert; }
  52. /**
  53. * Zero all fields and release all extra memory
  54. */
  55. void clear();
  56. IdentificationCertificate &operator=(const ZT_IdentificationCertificate &apiCert);
  57. IdentificationCertificate &operator=(const IdentificationCertificate &cert);
  58. /**
  59. * Add a subject node/identity without a locator
  60. *
  61. * @param id Identity
  62. * @return Pointer to C struct
  63. */
  64. ZT_IdentificationCertificate_Node *addSubjectNode(const Identity &id);
  65. /**
  66. * Add a subject node/identity with a locator
  67. *
  68. * @param id Identity
  69. * @param loc Locator signed by identity (signature is NOT checked here)
  70. * @return Pointer to C struct
  71. */
  72. ZT_IdentificationCertificate_Node *addSubjectNode(const Identity &id, const Locator &loc);
  73. /**
  74. * Add a subject network
  75. *
  76. * @param id Network ID
  77. * @param controller Network controller's full fingerprint
  78. * @return Pointer to C struct
  79. */
  80. ZT_IdentificationCertificate_Network *addSubjectNetwork(const uint64_t id, const ZT_Fingerprint &controller);
  81. /**
  82. * Add an update URL to the updateUrls list
  83. *
  84. * @param url Update URL
  85. */
  86. void addUpdateUrl(const char *url);
  87. /**
  88. * Marshal this certificate in binary form
  89. *
  90. * The internal encoding used here is Dictionary to permit easy
  91. * extensibility.
  92. *
  93. * @param omitSignature If true omit the signature field (for signing and verification, default is false)
  94. * @return Marshaled certificate
  95. */
  96. Vector< uint8_t > encode(bool omitSignature = false) const;
  97. /**
  98. * Decode this certificate from marshaled bytes.
  99. *
  100. * @param data Marshalled certificate
  101. * @return True if input is valid and was unmarshalled (signature is NOT checked)
  102. */
  103. bool decode(const Vector< uint8_t > &data);
  104. /**
  105. * Sign this certificate (and also fill in serialNo).
  106. *
  107. * @param issuer Issuer identity (must have secret key)
  108. * @return True on success
  109. */
  110. bool sign(const Identity &issuer);
  111. /**
  112. * Verify certificate signature against the issuer contained therein
  113. *
  114. * @return True if certificate is signed and signature is valid
  115. */
  116. bool verify() const;
  117. ZT_INLINE unsigned long hashCode() const noexcept
  118. { return (unsigned long)Utils::loadAsIsEndian< uint32_t >(this->serialNo); }
  119. ZT_INLINE bool operator==(const ZT_IdentificationCertificate &c) const noexcept
  120. { return memcmp(this->serialNo, c.serialNo, ZT_SHA384_DIGEST_SIZE) == 0; }
  121. ZT_INLINE bool operator!=(const ZT_IdentificationCertificate &c) const noexcept
  122. { return memcmp(this->serialNo, c.serialNo, ZT_SHA384_DIGEST_SIZE) != 0; }
  123. ZT_INLINE bool operator<(const ZT_IdentificationCertificate &c) const noexcept
  124. { return memcmp(this->serialNo, c.serialNo, ZT_SHA384_DIGEST_SIZE) < 0; }
  125. ZT_INLINE bool operator<=(const ZT_IdentificationCertificate &c) const noexcept
  126. { return memcmp(this->serialNo, c.serialNo, ZT_SHA384_DIGEST_SIZE) <= 0; }
  127. ZT_INLINE bool operator>(const ZT_IdentificationCertificate &c) const noexcept
  128. { return memcmp(this->serialNo, c.serialNo, ZT_SHA384_DIGEST_SIZE) > 0; }
  129. ZT_INLINE bool operator>=(const ZT_IdentificationCertificate &c) const noexcept
  130. { return memcmp(this->serialNo, c.serialNo, ZT_SHA384_DIGEST_SIZE) >= 0; }
  131. private:
  132. // These hold any identity or locator objects that are owned by and should
  133. // be deleted with this certificate. Lists are used so the pointers never
  134. // change.
  135. List< Identity > m_identities;
  136. List< Locator > m_locators;
  137. List< String > m_strings;
  138. // These are stored in a vector because the memory needs to be contiguous.
  139. Vector< ZT_IdentificationCertificate_Node > m_nodes;
  140. Vector< ZT_IdentificationCertificate_Network > m_networks;
  141. Vector< const char * > m_updateUrls;
  142. };
  143. } // namespace ZeroTier
  144. #endif